import type * as plugins from './plugins.js'; /** The Anthropic error types this service answers with. */ export type TAnthropicMessagesErrorType = | 'invalid_request_error' | 'authentication_error' | 'permission_error' | 'not_found_error' | 'request_too_large' | 'rate_limit_error' | 'api_error' | 'overloaded_error'; /** A failure as the Anthropic Messages API states it: an HTTP status, an error type and a message. */ export interface IAnthropicMessagesFailure { status: number; type: TAnthropicMessagesErrorType; message: string; /** Seconds a client waits before the next attempt, sent as `retry-after`. */ retryAfterSeconds?: number; /** Sent as `x-should-retry`. */ shouldRetry?: boolean; } /** The effort levels an Anthropic client asks for in `output_config.effort`. */ export type TAnthropicMessagesEffort = 'low' | 'medium' | 'high' | 'xhigh' | 'max'; /** * What one request asks of the model beyond its prompt and tools, in provider-independent terms. * A model's `providerOptions` turns these into its provider's own options. */ export interface IAnthropicMessagesControls { /** The reasoning effort to send, already fitted to the model's `reasoningEfforts`; absent for the model's default. */ reasoningEffort?: string; /** Whether the client asked for thinking blocks; without it no reasoning text is requested or returned. */ thinking: boolean; /** False when the client set `disable_parallel_tool_use`. */ parallelToolCalls: boolean; /** The client's session id (`x-claude-code-session-id`), for a provider prompt cache key. */ sessionId?: string; } /** One model this service serves, as the consumer resolves it for a request. */ export interface IAnthropicMessagesModel { /** The language model that answers the request. */ model: plugins.LanguageModelV4; /** The model's provider options for one request. */ providerOptions: (controls: IAnthropicMessagesControls) => plugins.SharedV4ProviderOptions; /** * The reasoning efforts the model accepts, lowest first. A requested effort the model lacks is fitted to * the nearest lower one it has, and the response names the substitution in `x-flexharness-effort-fitted`. * Unset, the requested effort is sent as it is. */ reasoningEfforts?: readonly string[]; /** * Names the account and provider a thinking signature belongs to. A signature from another scope, for * example one written while the conversation ran on another account, is not sent back to the model. */ signatureScope: string; /** * Whether `max_tokens` is sent as the model's output limit. A backend that takes no output limit, such as * ChatGPT's Codex backend, is served without one, and the response says so in `x-flexharness-max-tokens`. */ sendsMaxTokens: boolean; /** The longest tool name the model accepts; longer names are sent under a stable alias and mapped back. */ toolNameLimit?: number; /** Reads the model provider's own failures first; the generic rules handle the rest. */ classifyFailure?: (error: unknown) => IAnthropicMessagesFailure | undefined; } /** A model as `GET /v1/models` lists it. */ export interface IAnthropicMessagesModelListing { id: string; displayName?: string; description?: string; } /** Token usage of one answered request, as the model reported it. */ export interface IAnthropicMessagesUsageEvent { context: TContext; modelId: string; usage: plugins.LanguageModelV4Usage; } /** * Where the usage of answered requests goes. `record` runs after the answer is complete, so bookkeeping can * never change an answer; a `record` that throws or rejects is handed to `failed` with its event, which the * host must handle. A `failed` that throws itself is an unhandled rejection of the host. */ export interface IAnthropicMessagesUsageReporter { record: (event: IAnthropicMessagesUsageEvent) => void | Promise; failed: (error: unknown, event: IAnthropicMessagesUsageEvent) => void; } export interface IAnthropicMessagesServiceOptions { /** * Authenticates a request and returns what the other callbacks need to serve it. Throw an * `AnthropicMessagesError` to refuse it, for example with status 401 for a wrong credential. */ authenticate: (request: Request) => Promise; /** * The model a request names, or `undefined` when this service does not serve that id. `signal` aborts when * the client leaves, so a slow resolution (such as renewing access) can stop early. */ resolveModel: (modelId: string, context: TContext, signal: AbortSignal) => Promise; /** The models `GET /v1/models` lists and an unknown model id's refusal names. */ listModels: (context: TContext) => Promise; /** * The HMAC-SHA256 key thinking signatures are written and verified with: at least 32 random bytes, kept * secret by the host and used for nothing else. A signature this key did not write is not sent back to the * model, so a host that keeps the key across restarts keeps the reasoning of conversations it answered. */ signatureKey: Uint8Array; /** Receives the usage of every request the model finished, once its answer has been delivered. */ usage?: IAnthropicMessagesUsageReporter; /** Interval of `ping` events while a stream is otherwise silent. Defaults to 15 seconds. */ pingIntervalMs?: number; /** Largest accepted request body. Defaults to 32 MiB, the Messages API limit. */ maxRequestBytes?: number; } export interface IAnthropicMessagesService { /** Answers one HTTP request of the Anthropic Messages API. */ handle: (request: Request) => Promise; } /** * Usage in the Anthropic shape: input tokens exclude the ones read from or written to the cache. A cache count the * model did not state is `null` (Anthropic documents both as `number or null`); `message_start` states them `null`, as * the model's usage arrives only with its end. */ export interface IAnthropicMessagesUsage { input_tokens: number; output_tokens: number; cache_creation_input_tokens: number | null; cache_read_input_tokens: number | null; } export type TAnthropicMessagesStopReason = 'end_turn' | 'max_tokens' | 'stop_sequence' | 'tool_use' | 'refusal'; export type TAnthropicMessagesContentBlock = | { type: 'text'; text: string } | { type: 'thinking'; thinking: string; signature: string } | { type: 'tool_use'; id: string; name: string; input: unknown }; /** A complete answer, as a non-streaming request receives it. */ export interface IAnthropicMessagesMessage { id: string; type: 'message'; role: 'assistant'; model: string; content: TAnthropicMessagesContentBlock[]; stop_reason: TAnthropicMessagesStopReason | null; stop_sequence: string | null; usage: IAnthropicMessagesUsage; } export type TAnthropicMessagesContentDelta = | { type: 'text_delta'; text: string } | { type: 'thinking_delta'; thinking: string } | { type: 'signature_delta'; signature: string } | { type: 'input_json_delta'; partial_json: string }; /** One server-sent event of a streamed answer. */ export type TAnthropicMessagesStreamEvent = | { type: 'message_start'; message: IAnthropicMessagesMessage } | { type: 'content_block_start'; index: number; content_block: TAnthropicMessagesContentBlock } | { type: 'content_block_delta'; index: number; delta: TAnthropicMessagesContentDelta } | { type: 'content_block_stop'; index: number } | { type: 'message_delta'; delta: { stop_reason: TAnthropicMessagesStopReason; stop_sequence: string | null }; usage: IAnthropicMessagesUsage; } | { type: 'message_stop' } | { type: 'ping' } | { type: 'error'; error: { type: TAnthropicMessagesErrorType; message: string } };