chatWithImage method

Stream<String> chatWithImage(
  1. String text,
  2. Uint8List imageBytes, {
  3. String? conversationId,
})

Send a multimodal chat message (text + image)

Implementation

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

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

  final request = ChatWithImageRequest()
    ..conversationId = convId
    ..text = text
    ..image = imageBytes;

  // Add timeout to prevent infinite hanging
  await for (final response in _client!.chatWithImage(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;
    }
  }
}