import type { NexusResponse } from '../../types/response.js'; import type { AgentMiddleware, AgentToolCall, AgentToolResult } from '../create-agent.js'; import { type RetryBackoff } from './shared.js'; /** One retry about to happen. */ export interface RetryEvent { /** The attempt that failed, from 1. */ attempt: number; /** How long until the next attempt, in milliseconds. */ delayMs: number; /** The error a model call threw, when it threw. */ error?: unknown; /** The failed tool call's result, for a tool retry. */ result?: AgentToolResult; /** The tool's name, for a tool retry. */ tool?: string; } /** Options for `modelRetry()`. */ export interface ModelRetryOptions { /** Attempts in all, the first included. Defaults to 3. */ attempts?: number; /** How long to wait between attempts. */ backoff?: RetryBackoff; /** * Whether an error is worth another attempt. Defaults to every error not marked * `retryable: false`, such as a provider's authentication error, and never a cancellation. */ retryOn?: (error: unknown, attempt: number) => boolean; /** Whether a response that did arrive is tried again, such as an empty one. Defaults to never. */ retryResponse?: (response: NexusResponse, attempt: number) => boolean; /** Called before each new attempt, for logs and metrics. */ onRetry?: (event: RetryEvent) => void; } /** * Tries a failed model call again, with exponential backoff. * * The client already retries one provider request; this retries the agent's step as a whole, after * the client gave up, and can retry a response that arrived but is unusable. A streamed call that * fails halfway streams again from the start. Waits stop when the run is cancelled. */ export declare function modelRetry(options?: ModelRetryOptions): AgentMiddleware; /** Options for `toolRetry()`. */ export interface ToolRetryOptions { /** Attempts in all, the first included. Defaults to 3. */ attempts?: number; /** How long to wait between attempts. */ backoff?: RetryBackoff; /** * Which calls may run again: tool names, `'all'`, or a function of the call. Defaults to calls * that are safe to repeat, whose tool declares capabilities and none of them writes, runs a * command or code, reaches the network, or is the application's own. A call that may already have * changed something is retried only when you say so. */ tools?: readonly string[] | 'all' | ((call: AgentToolCall) => boolean); /** Whether a result is a failure worth another attempt. Defaults to any result with `ok: false`. */ retryOn?: (result: AgentToolResult, call: AgentToolCall, attempt: number) => boolean; /** Called before each new attempt, for logs and metrics. */ onRetry?: (event: RetryEvent) => void; } /** * Runs a failed tool call again, with exponential backoff. * * By default only a call that cannot have changed anything is repeated: retrying a payment or a * write that timed out after it landed would do it twice. Name the tools that are idempotent to * retry those too. */ export declare function toolRetry(options?: ToolRetryOptions): AgentMiddleware;