runProviderStream function
Future<void>
runProviderStream(
- AssistantMessageEventStream eventStream,
- ProviderStreamState state,
- CancelToken? cancelToken,
- Client httpClient, {
- required bool ownsClient,
- required Future<
void> body(),
Runs a provider adapter's streaming body under the shared terminal
protocol: any caught error becomes an ErrorEvent via
pushStreamErrorEvent (errors-as-events invariant), the stream is always
ended, and the owned HTTP client is closed.
pi wraps each adapter body in the same try/catch; the wrapper exists so the adapters do not duplicate it.
Implementation
Future<void> runProviderStream(
AssistantMessageEventStream eventStream,
ProviderStreamState state,
CancelToken? cancelToken,
http.Client httpClient, {
required bool ownsClient,
required Future<void> Function() body,
}) async {
try {
await body();
} catch (error) {
pushStreamErrorEvent(eventStream, state, error, cancelToken);
} finally {
eventStream.end();
if (ownsClient) {
httpClient.close();
}
}
}