import { Agent, AgentOutputType } from '../agent'; import { MaxTurnsExceededError, ModelBehaviorError, ModelRefusalError } from '../errors'; import { RunItem, RunMessageOutputItem } from '../items'; import { ModelResponse } from '../model'; import { StreamedRunResult } from '../result'; import { RunContext } from '../runContext'; import { RunState } from '../runState'; import type { AgentInputItem, AgentOutputItem, ResolvedAgentOutput } from '../types'; /** * Error kinds supported by run error handlers. */ export type RunErrorKind = 'maxTurns' | 'modelRefusal' | 'invalidFinalOutput'; /** * Snapshot of run data passed to error handlers. */ export type RunErrorData> = { input: string | AgentInputItem[]; newItems: RunItem[]; history: AgentInputItem[]; output: AgentOutputItem[]; rawResponses: ModelResponse[]; lastAgent?: TAgent; state?: RunState; }; export type RunErrorHandlerInput> = { error: MaxTurnsExceededError | ModelRefusalError | ModelBehaviorError; context: RunContext; runData: RunErrorData; }; export type RunErrorHandlerResult> = { /** * The final output to return for the run. */ finalOutput: ResolvedAgentOutput; /** * Whether to append the synthesized output to history for subsequent runs. */ includeInHistory?: boolean; }; export type RunErrorHandler> = (input: RunErrorHandlerInput) => RunErrorHandlerResult | void | Promise | void>; export type RunErrorHandlers> = Partial>> & { /** * Fallback handler for supported error kinds. */ default?: RunErrorHandler; }; type TryHandleRunErrorArgs> = { error: unknown; state: RunState; errorHandlers?: RunErrorHandlers; streamResult?: StreamedRunResult; responseAccepted: boolean; attemptedErrors?: WeakSet; }; export type PreparedRunErrorFinalOutput = { deferredItem?: RunMessageOutputItem; }; type ResolveRunErrorHandlerArgs> = { error: unknown; errorKind?: RunErrorKind; errorHandlers?: RunErrorHandlers; context: RunContext; runData: RunErrorData; attemptedErrors?: WeakSet; }; export declare function preserveInvalidFinalOutputRedaction(callback: (redactFromStart: boolean) => T | Promise, inheritedRedaction?: boolean): Promise; /** * Attaches the active run state to errors that need resumable caller context. */ export declare const attachRunStateToError: >(error: unknown, state: RunState) => void; declare function validateRunErrorFinalOutput>(agent: TAgent, outputText: string, redactInvalidOutputDetails?: boolean, redactFromStart?: boolean): void; export declare const formatRunErrorFinalOutput: >(agent: TAgent, finalOutput: ResolvedAgentOutput) => string; export declare const createRunErrorFinalOutputItem: >(agent: TAgent, outputText: string) => RunMessageOutputItem; export declare const validateRunErrorHandlerFinalOutput: typeof validateRunErrorFinalOutput; export declare function invalidateAcceptedResponseReplayEvidence(state: RunState | undefined): void; export declare const resolveRunErrorHandler: >({ error, errorKind, errorHandlers, context, runData, attemptedErrors, }: ResolveRunErrorHandlerArgs) => Promise | undefined>; export declare const prepareRunErrorFinalOutput: >({ error, state, errorHandlers, streamResult, responseAccepted, attemptedErrors, }: TryHandleRunErrorArgs) => Promise; export {};