sendMessageStream method
Sends a message to the conversation and streams the response.
Implementation
Stream<Message> sendMessageStream(
Message message, {
Map<String, Object?>? extraContext,
}) async* {
final handle = _handle;
if (handle == null) {
throw const LiteRtLmException('Conversation is already disposed.');
}
final extraContextJson = extraContext == null || extraContext.isEmpty
? null
: jsonEncode(extraContext);
var currentMessage = message;
for (var round = 0; round < _recurringToolCallLimit; round += 1) {
final pendingToolCalls = <ToolCall>[];
await for (final chunk
in LiteRtLmNativeRuntime.instance.sendMessageStream(
handle,
currentMessage.toJsonString(),
extraContextJson: extraContextJson,
)) {
if (_automaticToolCalling && chunk.toolCalls.isNotEmpty) {
pendingToolCalls.addAll(chunk.toolCalls);
}
if (!_automaticToolCalling ||
!chunk.contents.isEmpty ||
chunk.channels.isNotEmpty) {
yield chunk;
}
}
if (!_automaticToolCalling || pendingToolCalls.isEmpty) {
return;
}
currentMessage = await _handleToolCalls(pendingToolCalls);
}
throw const LiteRtLmException(
'Recurring tool call limit exceeded: $_recurringToolCallLimit.',
);
}