Skip to content

ai_chat_sdk Pro

Streaming LLM chat UI with multi-provider support (OpenAI, Anthropic). Includes chat bubbles, streaming indicator, and SharedPreferences persistence.

Version: 1.0.0

Variables

VariableTypeDefaultDescription
aiChatProviderenumopenaiWhich AI provider(s) to include: openai, anthropic, both
defaultModelstringgpt-4o-miniDefault model ID used for chat requests

Generated Packages

PackageConditionDescription
ai_chat_apiAlwaysContract + models (no provider deps)
ai_chat_implAlwaysOrchestrator + chat UI pages and widgets
ai_chat_openai_implopenai or bothOpenAI streaming provider
ai_chat_anthropic_implanthropic or bothAnthropic streaming provider

Usage

Interactive

bash
archipelago generate ai_chat_sdk

Non-interactive (CI)

bash
archipelago generate ai_chat_sdk --config my_config.json

Generated Structure

features/
└── ai_chat/
    ├── ai_chat_api/
    │   └── lib/src/
    │       └── models/
    ├── ai_chat_impl/
    │   └── lib/src/
    │       ├── data/
    │       ├── di/
    │       ├── presentation/
    │       │   └── widgets/
    │       └── router/
    ├── ai_chat_openai_impl/       # if aiChatProvider=openai or both
    │   └── lib/src/
    │       └── di/
    └── ai_chat_anthropic_impl/    # if aiChatProvider=anthropic or both
        └── lib/src/
            └── di/

Post-Install Steps

OpenAI

Set your API key in the DI module override:

dart
@module
abstract class AppAIModule {
  @lazySingleton
  AIChatConfig get aiChatConfig => const AIChatConfig(
    apiKey: String.fromEnvironment('OPENAI_API_KEY'),
    defaultModel: 'gpt-4o-mini',
  );
}

Anthropic

dart
@module
abstract class AppAIModule {
  @lazySingleton
  AIChatConfig get aiChatConfig => const AIChatConfig(
    apiKey: String.fromEnvironment('ANTHROPIC_API_KEY'),
    defaultModel: 'claude-3-haiku-20240307',
  );
}

WARNING

Never hardcode API keys in source code. Use compile-time String.fromEnvironment with your CI secrets or a .env file managed by shared_app_config.

Runtime API

dart
final chatSDK = getIt<AIChatSDK>();

// Start a streaming message:
final stream = chatSDK.sendMessage(
  ChatMessage(role: MessageRole.user, content: 'Explain dependency injection'),
);

await for (final chunk in stream) {
  print(chunk.delta); // partial text chunks
}

// Load persisted conversation history:
final history = await chatSDK.loadHistory();

Persistence

Chat history is persisted via SharedPreferences. Conversations are stored per-session with an auto-generated session ID. Clear history via chatSDK.clearHistory().

Known Limitations

  • Streaming is not supported in debug builds on iOS simulator (throttled by the network stack). Test on device or Android emulator for accurate streaming behaviour.
  • The both provider option generates both providers but only one is active at runtime — the orchestrator uses whichever AIChatProviderHandler is registered first.

Built by Banua Coder