sessionGetResponseStream method

  1. @override
Stream<String> sessionGetResponseStream(
  1. String sessionId, {
  2. GenerationConfig? config,
})
override

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;
  }
}