Zum Inhalt springen

Provider streaming

Dieser Inhalt ist noch nicht in deiner Sprache verfügbar.

Provider adapters translate distinct wire events into one AssistantMessageEvent stream consumed by the agent loop. Text deltas, reasoning, tool starts, tool arguments, completion, usage, and errors retain their meaning across adapters.

provider wire events adapter agent loop
text delta -> text delta -> append to message
reasoning delta -> reasoning delta -> append to message
tool start -> buffer call id
tool arg fragment -> accumulate JSON (no event yet)
tool arg complete -> tool request -> dispatch tool
usage -> usage -> account tokens
error or cancel -> terminal settle (once) -> stop the turn

How are tool calls normalized incrementally?

Section titled “How are tool calls normalized incrementally?”

Tool arguments can arrive as JavaScript Object Notation (JSON) fragments. The adapter accumulates fragments per call identifier and emits a tool request only after a complete valid value exists. Parse failure remains an error with provider context; silently truncating or inventing arguments would cross the provider boundary.

How does a stream cancel and finish exactly once?

Section titled “How does a stream cancel and finish exactly once?”

Cancellation propagates to the active provider request and prevents later deltas from reopening the turn. A terminal success, cancellation, or error settles the stream once. Retry and model fallback happen above normalization so an adapter does not conceal which provider produced an event.

Read packages/ai/src/stream.ts for the normalized event stream and packages/ai/src/providers for the per-vendor adapters.

The adapter may buffer incomplete tool arguments, but it must not execute them or expose them as a complete call. Once a terminal event wins, later provider frames are ignored and cleanup still runs. Test a normal text turn, fragmented tool JSON, malformed JSON, caller cancellation, provider error, and a late event after settlement. The final session record should identify the actual provider and retain the error or cancellation boundary.