$ show netdur/llama_cpp_dart
This repository provides a Dart FFI binding for llama.cpp, targeting Flutter mobile apps on iOS, Android, and macOS. It wraps the native C API with a modular, isolate-safe architecture: a worker isolate (LlamaEngine) owns the native model and context, while the main isolate communicates via message passing. The binding exposes streaming token generation, chat template rendering, multimodal (vision/audio) support, speculative decoding, context shifting, and session persistence. The codebase is well-structured with clear separation of concerns: FFI bindings, model/context wrappers, sampling, tokenization, and generation logic are each in their own modules. It uses modern Dart features (sealed classes, records, pattern matching) and demonstrates high craftsmanship in error handling, resource management, and cross-isolate communication. The package is a Flutter plugin with native-assets build hooks for Android and Swift Package Manager support for Apple platforms.
The LLM identified 4 files worth highlighting for reuse:
A self-contained UTF-8 streaming accumulator that buffers partial codepoints from token pieces and emits only complete characters. Handles multi-byte sequences split across arbitrary boundaries, with flush() for trailing fragments and clear() for reset. This is a reusable utility for any Dart project that needs to decode streaming UTF-8 bytes (e.g., from LLM tokenizers, network streams, or file readers) without corrupting multi-byte characters.
Defines a sealed class hierarchy for generation stream events: TokenEvent (sampled token with id, bytes, text, position), ShiftEvent (context shift notification), and DoneEvent (terminal with stop reason, counts, trailing text). This pattern cleanly models a streaming generation loop where consumers can pattern-match on event types. The design is reusable for any streaming token generation system (e.g., chat bots, code completion) and demonstrates idiomatic Dart sealed class usage.
A binary codec for serializing and deserializing LLM session state (KV cache, token history, metadata). Uses magic headers, versioning, FNV-1a checksums, and JSON metadata to ensure compatibility on reload. The encode/decode functions are self-contained and could be extracted for any project needing a robust, self-describing binary state format with validation. Demonstrates careful design for forward/backward compatibility and error discrimination.
Implements synchronous speculative decoding over two LlamaContexts (target and draft) sharing a vocabulary. Supports both greedy (temperature=0) and exact stochastic speculative sampling with residual resampling. The algorithm is well-documented and includes proper KV cache rollback, acceptance rate tracking, and reproducibility via seed. This is a complex, non-trivial implementation that could be extracted as a standalone speculative decoding module for other LLM inference engines.