run method

Stream<LlmEvent> run({
  1. required String sessionId,
  2. required AgentSdkServerTransport transport,
  3. required AgentSdkCompletionRequest request,
  4. required List<LlmEvent> prelude,
  5. required Duration stallGap,
  6. void onFailure(
    1. LlmTransportException failure
    )?,
})

Runs request in sessionId over transport, under stallGap.

Throws LlmSessionBusyException when the session already has a run. The stream carries prelude and then what the session reports, ends with one LlmRunEnded, and does not error. Cancelling it cancels the run. onFailure observes a transport failure (the provider closes the availability gate on unreachable); the request's tools answers the run's tool calls, here, in the main isolate.

Implementation

Stream<LlmEvent> run({
  required String sessionId,
  required AgentSdkServerTransport transport,
  required AgentSdkCompletionRequest request,
  required List<LlmEvent> prelude,
  required Duration stallGap,
  void Function(LlmTransportException failure)? onFailure,
}) {
  if (_disposed) throw StateError('The AI sessions were closed.');
  final session = _sessions.putIfAbsent(
    sessionId,
    () => _Session(sessionId, this),
  );
  if (session.run != null) throw LlmSessionBusyException(sessionId);
  final run = _Run(
    id: 'r${++_nextRun}',
    request: request,
    prelude: prelude,
    stallGap: stallGap,
    transportId: transport.id,
    onFailure: onFailure,
  );
  session.run = run;
  run.controller.onListen = () => unawaited(session.start(run, transport));
  run.controller.onCancel = () => session.cancel(run);
  return run.controller.stream;
}