runProviderStream function

Future<void> runProviderStream(
  1. AssistantMessageEventStream eventStream,
  2. ProviderStreamState state,
  3. CancelToken? cancelToken,
  4. Client httpClient, {
  5. required bool ownsClient,
  6. 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();
    }
  }
}