intercept method

  1. @override
Stream<AgUiEvent> intercept(
  1. InterceptorChain chain,
  2. RunAgentInput input
)

Wraps the execution of input, delegating to chain to reach the next interceptor (or the terminal agent). Returns the — possibly transformed — event stream the run produces.

Implementation

@override
Stream<AgUiEvent> intercept(InterceptorChain chain, RunAgentInput input) {
  // Resolve asynchronously, then delegate. `Stream.fromFuture(...).asyncExpand`
  // is single-subscription and cancel-correct: a cancel during resolution
  // cancels the source and `chain.proceed` never runs (no transport opens); a
  // cancel during the delegated stream forwards to `chain.proceed`. A resolve
  // throw routes as a stream error the chain classifies (see [_resolve]).
  return Stream.fromFuture(_resolve()).asyncExpand((resolved) {
    // Merge over any reserved map a prior (inner) AuthInterceptor wrote, later
    // keys winning, so stacked auth interceptors compose.
    final prior = (input.forwardedProps[transportHeadersKey] as Map?)
        ?.cast<String, String>();
    return chain.proceed(
      input.copyWith(
        forwardedProps: {
          ...input.forwardedProps,
          transportHeadersKey: <String, String>{...?prior, ...resolved},
        },
      ),
    );
  });
}