chat method

Stream<String> chat(
  1. String text, {
  2. String? conversationId,
})

Send a chat message and get streaming response

Implementation

Stream<String> chat(String text, {String? conversationId}) async* {
  _assertInitialized();

  final convId = conversationId ?? _currentConversationId;
  if (convId == null) {
    throw StateError('No conversation. Call createConversation() first.');
  }

  final request = ChatRequest()
    ..conversationId = convId
    ..text = text;

  // Add timeout to prevent infinite hanging
  await for (final response in _client!.chat(request).timeout(
    _streamTimeout,
    onTimeout: (sink) {
      sink.addError(TimeoutException(
        'Model response timed out after ${_streamTimeout.inMinutes} minutes',
      ));
      sink.close();
    },
  )) {
    if (response.hasError() && response.error.isNotEmpty) {
      throw Exception('Chat error: ${response.error}');
    }

    if (response.hasText()) {
      yield response.text;
    }
  }
}