docs(agent): clarify non-throwing stream contracts fixes #2119
This commit is contained in:
@@ -11,7 +11,15 @@ import type {
|
|||||||
} from "@mariozechner/pi-ai";
|
} from "@mariozechner/pi-ai";
|
||||||
import type { Static, TSchema } from "@sinclair/typebox";
|
import type { Static, TSchema } from "@sinclair/typebox";
|
||||||
|
|
||||||
/** Stream function - can return sync or Promise for async config lookup */
|
/**
|
||||||
|
* Stream function used by the agent loop.
|
||||||
|
*
|
||||||
|
* Contract:
|
||||||
|
* - Must not throw or return a rejected promise for request/model/runtime failures.
|
||||||
|
* - Must return an AssistantMessageEventStream.
|
||||||
|
* - Failures must be encoded in the returned stream via protocol events and a
|
||||||
|
* final AssistantMessage with stopReason "error" or "aborted" and errorMessage.
|
||||||
|
*/
|
||||||
export type StreamFn = (
|
export type StreamFn = (
|
||||||
...args: Parameters<typeof streamSimple>
|
...args: Parameters<typeof streamSimple>
|
||||||
) => ReturnType<typeof streamSimple> | Promise<ReturnType<typeof streamSimple>>;
|
) => ReturnType<typeof streamSimple> | Promise<ReturnType<typeof streamSimple>>;
|
||||||
@@ -29,6 +37,9 @@ export interface AgentLoopConfig extends SimpleStreamOptions {
|
|||||||
* that the LLM can understand. AgentMessages that cannot be converted (e.g., UI-only notifications,
|
* that the LLM can understand. AgentMessages that cannot be converted (e.g., UI-only notifications,
|
||||||
* status messages) should be filtered out.
|
* status messages) should be filtered out.
|
||||||
*
|
*
|
||||||
|
* Contract: must not throw or reject. Return a safe fallback value instead.
|
||||||
|
* Throwing interrupts the low-level agent loop without producing a normal event sequence.
|
||||||
|
*
|
||||||
* @example
|
* @example
|
||||||
* ```typescript
|
* ```typescript
|
||||||
* convertToLlm: (messages) => messages.flatMap(m => {
|
* convertToLlm: (messages) => messages.flatMap(m => {
|
||||||
@@ -54,6 +65,9 @@ export interface AgentLoopConfig extends SimpleStreamOptions {
|
|||||||
* - Context window management (pruning old messages)
|
* - Context window management (pruning old messages)
|
||||||
* - Injecting context from external sources
|
* - Injecting context from external sources
|
||||||
*
|
*
|
||||||
|
* Contract: must not throw or reject. Return the original messages or another
|
||||||
|
* safe fallback value instead.
|
||||||
|
*
|
||||||
* @example
|
* @example
|
||||||
* ```typescript
|
* ```typescript
|
||||||
* transformContext: async (messages) => {
|
* transformContext: async (messages) => {
|
||||||
@@ -71,6 +85,8 @@ export interface AgentLoopConfig extends SimpleStreamOptions {
|
|||||||
*
|
*
|
||||||
* Useful for short-lived OAuth tokens (e.g., GitHub Copilot) that may expire
|
* Useful for short-lived OAuth tokens (e.g., GitHub Copilot) that may expire
|
||||||
* during long-running tool execution phases.
|
* during long-running tool execution phases.
|
||||||
|
*
|
||||||
|
* Contract: must not throw or reject. Return undefined when no key is available.
|
||||||
*/
|
*/
|
||||||
getApiKey?: (provider: string) => Promise<string | undefined> | string | undefined;
|
getApiKey?: (provider: string) => Promise<string | undefined> | string | undefined;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user