getResponseAsync method
Implementation
@override
Stream<String> getResponseAsync() {
if (kDebugMode) {
print('π getResponseAsync: Starting async response generation');
}
_controller = StreamController<String>();
try {
final promptArray = _createPromptArray();
if (kDebugMode) {
print('π― getResponseAsync: Prompt array type: ${promptArray.runtimeType}');
print('π― getResponseAsync: Is JSString? ${promptArray is JSString}');
}
// Use appropriate method based on prompt type
if (promptArray is JSString) {
if (kDebugMode) {
print('π getResponseAsync: Using generateResponse for text-only prompt');
}
llmInference.generateResponse(
promptArray,
((JSString partialJs, JSAny completeRaw) {
try {
final complete = completeRaw.parseBool();
final partial = partialJs.toDart;
if (kDebugMode) {
print('π getResponseAsync: Received partial (complete: $complete): ${partial.substring(0, math.min(50, partial.length))}...');
}
_controller!.add(partial);
if (complete) {
if (kDebugMode) {
print('β
getResponseAsync: Text response completed');
}
_controller!.close();
}
} catch (e) {
if (kDebugMode) {
print('β getResponseAsync: Error in text callback: $e');
}
_controller!.addError(e);
}
}).toJS,
);
} else {
if (kDebugMode) {
print('πΌοΈ getResponseAsync: Using generateResponseMultimodal for multimodal prompt');
}
llmInference.generateResponseMultimodal(
promptArray,
((JSString partialJs, JSAny completeRaw) {
try {
final complete = completeRaw.parseBool();
final partial = partialJs.toDart;
if (kDebugMode) {
print('πΌοΈ getResponseAsync: Received multimodal partial (complete: $complete): ${partial.substring(0, math.min(50, partial.length))}...');
}
_controller!.add(partial);
if (complete) {
if (kDebugMode) {
print('β
getResponseAsync: Multimodal response completed');
}
_controller!.close();
}
} catch (e) {
if (kDebugMode) {
print('β getResponseAsync: Error in multimodal callback: $e');
}
_controller!.addError(e);
}
}).toJS,
);
}
} catch (e, stackTrace) {
if (kDebugMode) {
print('β getResponseAsync: Exception during setup: $e');
print('β getResponseAsync: Stack trace: $stackTrace');
}
_controller!.addError(e);
}
return _controller!.stream;
}