diff --git a/packages/agent/src/types.ts b/packages/agent/src/types.ts index c291dae2..a38a9e76 100644 --- a/packages/agent/src/types.ts +++ b/packages/agent/src/types.ts @@ -11,7 +11,15 @@ import type { } from "@mariozechner/pi-ai"; 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 = ( ...args: Parameters ) => ReturnType | Promise>; @@ -29,6 +37,9 @@ export interface AgentLoopConfig extends SimpleStreamOptions { * that the LLM can understand. AgentMessages that cannot be converted (e.g., UI-only notifications, * 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 * ```typescript * convertToLlm: (messages) => messages.flatMap(m => { @@ -54,6 +65,9 @@ export interface AgentLoopConfig extends SimpleStreamOptions { * - Context window management (pruning old messages) * - Injecting context from external sources * + * Contract: must not throw or reject. Return the original messages or another + * safe fallback value instead. + * * @example * ```typescript * 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 * 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;