docs(agent,ai): clarify non-throwing stream and hook contracts

This commit is contained in:
Mario Zechner
2026-03-13 22:54:41 +01:00
parent ea1a1f8266
commit 4aa25bfb72
2 changed files with 20 additions and 1 deletions

View File

@@ -114,7 +114,14 @@ export interface SimpleStreamOptions extends StreamOptions {
thinkingBudgets?: ThinkingBudgets;
}
// Generic StreamFunction with typed options
// Generic StreamFunction with typed options.
//
// Contract:
// - Must return an AssistantMessageEventStream.
// - Once invoked, request/model/runtime failures should be encoded in the
// returned stream, not thrown.
// - Error termination must produce an AssistantMessage with stopReason
// "error" or "aborted" and errorMessage, emitted via the stream protocol.
export type StreamFunction<TApi extends Api = Api, TOptions extends StreamOptions = StreamOptions> = (
model: Model<TApi>,
context: Context,
@@ -218,6 +225,14 @@ export interface Context {
tools?: Tool[];
}
/**
* Event protocol for AssistantMessageEventStream.
*
* Streams should emit `start` before partial updates, then terminate with either:
* - `done` carrying the final successful AssistantMessage, or
* - `error` carrying the final AssistantMessage with stopReason "error" or "aborted"
* and errorMessage.
*/
export type AssistantMessageEvent =
| { type: "start"; partial: AssistantMessage }
| { type: "text_start"; contentIndex: number; partial: AssistantMessage }