chatStream method

  1. @override
Stream<ChatStreamEvent> chatStream(
  1. List<ChatMessage> messages, {
  2. List<Tool>? tools,
})
override

Sends a streaming chat request to the provider

messages - The conversation history as a list of chat messages tools - Optional list of tools to use in the chat

Returns a stream of chat events

Implementation

@override
Stream<ChatStreamEvent> chatStream(
  List<ChatMessage> messages, {
  List<Tool>? tools,
}) async* {
  final effectiveTools = tools ?? config.tools;
  final requestBody = _buildRequestBody(messages, effectiveTools, true);

  // Reset stream state
  _resetStreamState();

  try {
    // Create SSE stream
    final stream = client.postStreamRaw(chatEndpoint, requestBody);

    await for (final chunk in stream) {
      try {
        final events = _parseStreamEvents(chunk);
        for (final event in events) {
          yield event;
        }
      } catch (e) {
        // Log parsing errors but continue processing
        client.logger.warning('Failed to parse stream chunk: $e');
        // Optionally yield an error event instead of throwing
        // yield ErrorEvent(GenericError('Stream parsing error: $e'));
      }
    }
  } catch (e) {
    // Handle stream creation or connection errors
    if (e is LLMError) {
      rethrow;
    } else {
      throw GenericError('Stream error: $e');
    }
  }
}