run method
Stream<LlmEvent>
run({
- required String sessionId,
- required AgentSdkServerTransport transport,
- required AgentSdkCompletionRequest request,
- required List<
LlmEvent> prelude, - required Duration stallGap,
- void onFailure(
- LlmTransportException failure
- void onConversation(
- String conversationId
- bool queueWhenBusy = false,
Runs request in sessionId over transport, under stallGap.
A session holds one run at a time. When it already has one, the call
throws LlmSessionBusyException — or, with queueWhenBusy, waits for
the session to be free and then runs (a run cancelled while it waits
never starts). 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,
void Function(String conversationId)? onConversation,
bool queueWhenBusy = false,
}) {
if (_disposed) throw StateError('The AI sessions were closed.');
final session = _sessions.putIfAbsent(
sessionId,
() => _Session(sessionId, this),
);
if (session.run != null) {
if (!queueWhenBusy) throw LlmSessionBusyException(sessionId);
return Stream.fromFuture(session.whenFree()).asyncExpand(
(_) => this.run(
sessionId: sessionId,
transport: transport,
request: request,
prelude: prelude,
stallGap: stallGap,
onFailure: onFailure,
onConversation: onConversation,
queueWhenBusy: true,
),
);
}
final run = _Run(
id: 'r${++_nextRun}',
request: request,
prelude: prelude,
stallGap: stallGap,
transportId: transport.id,
onFailure: onFailure,
onConversation: onConversation,
);
session.run = run;
run.controller.onListen = () => unawaited(session.start(run, transport));
run.controller.onCancel = () => session.cancel(run);
return run.controller.stream;
}