sessionGetResponseStream method
Answers the pending turn as a stream of text deltas.
Implementation
@override
Stream<String> sessionGetResponseStream(
String sessionId, {
GenerationConfig? config,
}) async* {
final s = _session(sessionId);
final input = s.takeTurn();
try {
final model = await _ensureModel(s, config);
final stream = model.callMethod<JSObject>(
'promptStreaming'.toJS,
input.toJS,
_promptOptions(s, config) as JSObject,
);
final reader = stream.callMethod<JSObject>('getReader'.toJS);
var full = '';
while (true) {
final result = await reader
.callMethod<JSPromise<JSObject>>('read'.toJS)
.toDart;
if (result.getProperty<JSBoolean>('done'.toJS).toDart) break;
final chunk = result.getProperty<JSString>('value'.toJS).toDart;
// Older Chrome builds stream the whole text so far, newer ones deltas.
final isCumulative = full.isNotEmpty && chunk.startsWith(full);
final delta = isCumulative ? chunk.substring(full.length) : chunk;
full = isCumulative ? chunk : full + chunk;
if (delta.isNotEmpty) yield delta;
}
} catch (e) {
throw _translate(e);
} finally {
s.abort = null;
}
}