/** * Translation helpers between the AgentEngine normalized types and * @anthropic-ai/sdk's wire types. * * AnthropicEngine does very little translation because the framework's * EngineMessage / EngineTool shapes were modeled on Anthropic's types. * The main differences are: camelCase vs snake_case, and that * Anthropic uses `input_schema` while we use `inputSchema`. * * Builder's Gemini-backed gateway requires `tool_name` and `tool_input` on * every `tool_result` block. Use `engineMessagesToBuilderGatewayAnthropic` for * that path. The native Anthropic API keeps the strict `tool_result` shape * (`engineMessagesToAnthropic`). */ import type Anthropic from "@anthropic-ai/sdk"; import type { EngineTool, EngineMessage, EngineContentPart, EngineEvent } from "./types.js"; export declare function engineToolToAnthropic(tool: EngineTool): Anthropic.Tool; export declare function engineToolsToAnthropic(tools: EngineTool[]): Anthropic.Tool[]; /** JSON.stringify for tool_use inputs; never throws. */ export declare function stringifyToolUseInputForGateway(input: unknown): string; /** Same lead-in as structured-history replay when a tool_result cannot be paired. */ export declare const UNMATCHED_TOOL_RESULT_REPLAY_PREFIX = "(Omitted unmatched tool results from replayed history.)"; /** * Human/LLM-visible note when a tool_result cannot be matched to a tool_use * (replay from DB, or malformed engine history). Preserves tool_use_id and * a truncated payload instead of silently dropping the turn. */ export declare function unmatchedToolResultReplayText(part: { toolCallId: string; content: unknown; isError?: boolean; }): string; /** * Ensure every `tool-result` has a non-empty `toolName` and `toolInput` string, * using the matching assistant `tool-call` in the same conversation. * Assistant `tool-call` blocks without an immediately following result get a * synthetic interrupted result so replayed history stays provider-protocol safe. * Orphan tool-results (no resolvable tool name) become `text` notes so nothing * is silently dropped from replayed history. */ export declare function backfillEngineMessagesToolResults(messages: EngineMessage[]): EngineMessage[]; export declare function engineMessageToAnthropic(msg: EngineMessage, opts?: { builderGateway?: boolean; }): Anthropic.MessageParam; /** Messages for the Anthropic HTTP API (strict schema — no extra tool_result fields). */ export declare function engineMessagesToAnthropic(messages: EngineMessage[]): Anthropic.MessageParam[]; /** * Messages for the Builder LLM gateway (Gemini-backed). Same Anthropic-shaped * envelope, but every `tool_result` includes `tool_name` and `tool_input`. */ export declare function engineMessagesToBuilderGatewayAnthropic(messages: EngineMessage[]): Anthropic.MessageParam[]; export declare function anthropicContentToEngine(content: Anthropic.ContentBlock[]): EngineContentPart[]; /** * Mutable state threaded across `anthropicChunkToEngineEvents` calls within a * single stream. Anthropic's `content_block_delta` chunks carry only the block * `index`, not the tool-call id/name — those arrive once on the matching * `content_block_start`. We remember `index → { id, name }` here so each * `input_json_delta` can be surfaced as a `tool-input-delta` carrying the same * id/name the consumer expects (mirroring the Builder gateway shape). */ export interface AnthropicChunkStreamState { toolUseByIndex: Map; } export declare function createAnthropicChunkStreamState(): AnthropicChunkStreamState; /** * Translate an Anthropic stream chunk into zero or more EngineEvents. * Called in a loop as chunks arrive from client.messages.stream(). * * Pass a per-stream `state` (from `createAnthropicChunkStreamState`) to also * emit `tool-input-start` / `tool-input-delta` progress events while a tool * call's JSON input streams in. These are progress-only signals: the * authoritative `tool-call` blocks are still emitted from `finalMessage()` by * the engine, so omitting `state` simply drops the progress events without * changing tool dispatch. */ export declare function anthropicChunkToEngineEvents(chunk: any, state?: AnthropicChunkStreamState): EngineEvent[]; /** * Tool arguments arrive split across an arbitrary number of deltas. Every * engine announces a tool call the moment the first delta lands, so a stream * that dies mid-arguments leaves the turn advertising a call it never * delivered. Accumulating the delta text is the only way to tell "assembled * fine" from "cut off", instead of dropping the call and reporting success. */ export interface StreamedToolInputState { byId: Map; } export declare function createStreamedToolInputState(): StreamedToolInputState; /** * Feed an engine's own outgoing event into the accumulator. Works for every * adapter because they all emit the same normalized progress events. */ export declare function observeStreamedToolInput(state: StreamedToolInputState, event: EngineEvent): void; export declare function markStreamedToolInputDelivered(state: StreamedToolInputState, id: string): void; /** * Reconcile what the stream announced against what it actually delivered. * Announced calls whose accumulated arguments parse are handed back as real * tool calls; the rest become in-band tool-call errors the model can read and * retry from. Nothing is dropped. */ export declare function finalizeStreamedToolInputs(state: StreamedToolInputState, deliveredIds?: Iterable): EngineEvent[]; export declare function buildToolResultPart(toolCallId: string, toolName: string, content: string, toolInput?: unknown, isError?: boolean): EngineContentPart; //# sourceMappingURL=translate-anthropic.d.ts.map