sendStream method

Stream<AIStreamChunk> sendStream(
  1. String conversationId,
  2. String message
)

Sends a message in a conversation and returns a stream of chunks.

Implementation

Stream<AIStreamChunk> sendStream(
  String conversationId,
  String message,
) async* {
  final conversation = _conversations[conversationId];
  if (conversation == null) {
    throw StateError('Conversation $conversationId not found');
  }

  // Add user message
  conversation.addMessage(AIMessage.user(message));
  storage?.saveConversation(conversation);

  // Trim context if needed
  final messages = _trimToFit(conversation);

  // Stream from provider
  final buffer = StringBuffer();
  await for (final chunk in provider.completeStream(messages)) {
    buffer.write(chunk.text);
    yield chunk;

    // Track tokens from final chunk
    if (chunk.isComplete && chunk.usage != null) {
      conversation.totalTokensUsed += chunk.usage!.totalTokens;
      tokenTracker.record(chunk.usage!);
    }
  }

  // Add assistant response
  conversation.addMessage(AIMessage.assistant(buffer.toString()));
  storage?.saveConversation(conversation);
}