chat_pilot_kit_dart 1.0.0-beta.6
chat_pilot_kit_dart: ^1.0.0-beta.6 copied to clipboard
Headless chat SDK for AI-powered conversation apps. Pure Dart, no Flutter dependency. Supports streaming, plugins, and multiple transport layers.
example/main.dart
import 'dart:async';
import 'package:chat_pilot_kit_dart/chat_pilot_kit_dart.dart';
/// Mock agent service for testing (simulates streaming response).
class MockAgentService extends BaseAgentService {
@override
Future<void> query(
String text, {
List<AttachmentInput>? attachments,
QueryOptions? options,
}) async {
// Simulate streaming response with delays
final chunks = [
'Hello! ',
'I received your message: ',
'"$text". ',
'Let me think about that...',
];
for (final chunk in chunks) {
await Future.delayed(const Duration(milliseconds: 100));
onData(
AgentMessageData(
answer: chunk,
nodeType: 'markdown',
queryId: queryId,
sessionId: sessionId,
),
);
}
// Simulate thinking block
onData(
AgentMessageData(
answer: 'Analyzing the request...',
nodeType: 'thinking',
queryId: queryId,
sessionId: sessionId,
nodeCompleted: true,
),
);
// Final response
await Future.delayed(const Duration(milliseconds: 200));
onData(
AgentMessageData(
answer: '\n\nHere is my response to your query.',
nodeType: 'markdown',
nodeBehavior: NodeBehavior.create,
queryId: queryId,
sessionId: sessionId,
),
);
onCompleted(queryId: queryId, sessionId: sessionId);
}
@override
void abort([String? reason]) {
// No-op for mock
}
}
void main() async {
print('=== Chat Pilot Kit Dart Example ===\n');
// Create controller with mock service
final controller = createChatPilotKit(
agentService: MockAgentService(),
);
// Listen to events
final subscription = controller.events.listen((event) {
switch (event) {
case ConversationAddEvent(:final conversationId, :final role):
print('[Event] Conversation added: $conversationId ($role)');
case NodeAddEvent(:final nodeId, :final nodeType):
print('[Event] Node added: $nodeId ($nodeType)');
case NodeUpdateEvent(:final nodeId, :final nodeType):
print('[Event] Node updated: $nodeId ($nodeType)');
case ConversationChangeEvent(:final conversationId, :final completed):
if (completed) {
print('[Event] Conversation completed: $conversationId');
}
case ErrorEvent(:final error):
print('[Error] $error');
default:
break;
}
});
// Send a query
print('Sending query: "What is Flutter?"\n');
await controller.query('What is Flutter?');
// Wait for response to complete
await Future.delayed(const Duration(seconds: 2));
// Print conversations
print('\n=== Conversations ===');
for (final conv in controller.conversations) {
print('Role: ${conv.role.name} (${conv.nodes.length} nodes)');
for (final node in conv.nodes) {
print(' [${node.type}] ${node.content}');
}
}
// Export/Import test
final snapshots = controller.exportConversations();
print('\nExported ${snapshots.length} conversations');
// Clean up
subscription.cancel();
controller.dispose();
print('\nDone!');
}