Streaming Model
stream and streamSimple return an AssistantMessageEventStream, which is an async iterable of events.
This stream is produced by provider adapters and is normalized to a single event model.
Event Types
AssistantMessageEvent in indusagi/src/ai/types.ts includes:
startwith a partialAssistantMessage.text_start,text_delta,text_endfor text blocks.thinking_start,thinking_delta,thinking_endfor reasoning blocks.toolcall_start,toolcall_delta,toolcall_endfor tool calls.donewith final message and stop reason.errorwith error message and stop reason.
Event Order
A typical streaming response:
start- zero or more block events in any order
doneorerror
Tool call events can interleave with text or thinking blocks depending on provider.
The final message can contain multiple blocks in message.content.
Result Handling
AssistantMessageEventStream implements result() which resolves to the final AssistantMessage.
complete and completeSimple are simple wrappers that await result().
Tool Calls
Tool calls are represented as content blocks:
ToolCallwithid,name, andarguments.- Arguments are parsed incrementally using
parseStreamingJson.
Tool call IDs may be normalized to satisfy provider constraints.
transformMessages in indusagi/src/ai/providers/transform-messages.ts:
- Normalizes tool call IDs for cross-provider replay.
- Removes unsupported signatures for other providers.
- Inserts synthetic tool results for orphaned tool calls.
- Drops assistant messages that ended with
errororaborted.
Aborts
All streaming functions accept an AbortSignal in StreamOptions.
Providers check signal.aborted and convert it to stopReason: "aborted".
Reasoning and Thinking
streamSimple converts SimpleStreamOptions into provider-specific options.
It supports reasoning levels and optional thinkingBudgets.
Provider adapters may clamp levels or convert them to provider-specific parameters.
