import type { Model } from "../types.js"; import type { MessageCreateParamsStreaming, MessageParam } from "./anthropic-wire.js"; /** Session thread chains keyed by routing session id; held in the Anthropic provider session state. */ export type AnthropicThreadChains = Map; /** One session's thread bookkeeping. */ export interface AnthropicThreadChain { /** Stored response the next request may continue. A continue that names it removes it: each id continues once. */ anchor?: AnthropicThreadAnchor; /** Continues the server refused since the last unforced success; {@link MAX_REFUSED_CONTINUES} disables the chain. */ refusedContinues: number; /** The server cannot thread this session's requests; later requests go without `thread`. */ disabled: boolean; } interface AnthropicThreadAnchor { /** Response `id` a continue names as `previous_message_id`. */ messageId: string; /** Conversation the server holds for `messageId`: the request's full messages, then the response. */ messages: readonly MessageParam[]; /** Fixed and create-only fields of the thread; any change starts a new thread. */ fingerprint: ThreadFingerprint; /** When the response was stored (`Date.now()`). */ storedAt: number; } /** * Fields a thread fixes at create time. `system` and `tools` are create-only, * the rest must match on every request; `betas` is the normalized * `anthropic-beta` set without the threads beta itself. */ type ThreadFingerprint = Pick & { fallbacks: boolean; betas: string; }; /** A thread-shaped request. */ export interface AnthropicThreadRequest { kind: "create" | "continue"; /** Wire body: the full params plus `thread`, or only the appended messages for a continue. */ body: MessageCreateParamsStreaming; } /** * What the provider does with a request the server refused for a thread * reason: `replay` resends it as a create, `unthreaded` resends it without * `thread`. */ export type AnthropicThreadRecovery = "replay" | "unthreaded"; /** A response the server stored and the next request may continue. */ export interface AnthropicStoredResponse { messageId: string; /** The response as the next request's history serializes it. */ message: MessageParam; } /** * Whether requests for `model` to `baseUrl` use message threads. A caller's * `statefulResponses` wins; otherwise `PI_ANTHROPIC_MESSAGE_THREADS`, then the * model policy, limited to the official endpoint: a reroute through * `ANTHROPIC_BASE_URL` leaves the policy flag on for a gateway that may not * know the field. */ export declare function wantsAnthropicThreads(model: Model<"anthropic-messages">, statefulResponses: boolean | undefined, baseUrl: string): boolean; /** The chain for `sessionId`, created on first use and marked most recently used. */ export declare function anthropicThreadChain(chains: AnthropicThreadChains, sessionId: string): AnthropicThreadChain; /** * One provider call's use of a session thread. The provider calls * {@link plan} before each attempt, {@link recover} and * {@link dropAfterUnexplained400} from its retry ladder, and {@link settle} * once the turn completes. */ export declare class AnthropicThreadTurn { #private; constructor(chain: AnthropicThreadChain, options: { resendSystemAndTools: boolean; }); /** * Shape `params` for the wire, or `undefined` to send it without a thread. * Repeated calls with the same params and betas return the same request, so * a transient retry resends the same continue. */ plan(params: MessageCreateParamsStreaming, betaHeader: string, now?: number): AnthropicThreadRequest | undefined; /** * Handle a pre-stream failure of the planned request. Returns how to resend * it when the server refused it for a thread reason; `undefined` leaves the * error to the rest of the retry ladder. */ recover(error: unknown): AnthropicThreadRecovery | undefined; /** * Resend the planned request without `thread` after a 400 nothing else * explained (a context overflow is not one). If that resend carries the * turn, the thread was the problem and the chain is disabled. */ dropAfterUnexplained400(error: unknown): boolean; /** * Record the completed turn. `stored` is the response when the server keeps * it continuable (a finished turn from the requested model); otherwise the * next request starts a new thread. */ settle(stored: AnthropicStoredResponse | undefined, now?: number): void; } export {};