flutter_ai_core 0.1.14
flutter_ai_core: ^0.1.14 copied to clipboard
Dependency-free Dart foundation for AI chat: message models, a streaming MessageProcessor, and the provider and renderer contracts the flutter_ai family builds on.
flutter_ai_core
The dependency-free Dart engine under flutter_ai — immutable conversation models, a streaming-event reducer, and the LlmProvider contract every provider speaks.
Family: flutter_ai ·
client · elements ·
openai · anthropic · gemini ·
tools · mcp · voice
Recipes · Migrating from the Vercel AI SDK
The transcript above is produced by this package's MessageProcessor folding provider events into messages (rendered with flutter_ai_elements).
Dependency-free Dart foundation for building AI chat experiences — the shared
contract layer of the flutter_ai package family.
flutter_ai_core has no runtime dependencies beyond dart:core and
dart:convert: no Flutter, no code generation, no build_runner. That keeps it
safe to depend on from anywhere and free of version conflicts.
What's inside #
- Models —
AiConversation,AiMessage, and the sealedAiParthierarchy (TextPart,ReasoningPart,ToolCallPart,ToolResultPart,FilePart,SourcePart,DataPart). All immutable value types with manual, hand-written JSON. - Streaming — the sealed
AiStreamEventset and aMessageProcessorthat folds events into conversation state, reporting exactly which messages changed so a UI can rebuild only those nodes. - Tolerant JSON —
JsonAccumulatorparses partial tool-call arguments as they stream, repairing incomplete JSON without ever throwing. - Contracts —
LlmProvider(provider abstraction) andTextRenderer(pluggable text rendering), withAiRequestOptionsandToolDefinition.
Design principles #
- Un-opinionated. No bundled state manager. The processor is a pure, synchronous reducer; batching updates to the frame boundary is the consumer's job, which keeps this package UI-agnostic and trivially testable.
- Granular by construction.
MutationResult.changedMessageIdslets the UI avoid rebuilding the whole transcript on every token. - Fails soft. Malformed streamed tool arguments mark a single call errored rather than crashing the stream.
Example #
import 'package:flutter_ai_core/flutter_ai_core.dart';
void main() {
final processor = MessageProcessor();
// Events would normally come from an LlmProvider's stream.
processor.apply(const MessageStarted(messageId: 'a1', role: AiRole.assistant));
processor.apply(const TextDelta(messageId: 'a1', delta: 'Hello, '));
final result = processor.apply(const TextDelta(messageId: 'a1', delta: 'world!'));
print(result.conversation.messageById('a1')!.text); // Hello, world!
print(result.changedMessageIds); // {a1}
}
See example/ for a fuller walkthrough including tool calls.
Status #
Part of the flutter_ai ecosystem; the UI layer (flutter_ai_elements) and
provider/controller layer (flutter_ai_client) build on these types. See the
CHANGELOG for version history.
If flutter_ai saves you time, you can buy me a coffee ☕.