@warlock.js/ai-anthropic 4.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cjs/index.cjs +785 -0
- package/cjs/index.cjs.map +1 -0
- package/esm/config.type.d.mts +72 -0
- package/esm/config.type.d.mts.map +1 -0
- package/esm/index.d.mts +3 -0
- package/esm/index.mjs +3 -0
- package/esm/known-vision-models.mjs +44 -0
- package/esm/known-vision-models.mjs.map +1 -0
- package/esm/model.mjs +303 -0
- package/esm/model.mjs.map +1 -0
- package/esm/sdk.d.mts +70 -0
- package/esm/sdk.d.mts.map +1 -0
- package/esm/sdk.mjs +85 -0
- package/esm/sdk.mjs.map +1 -0
- package/esm/utils/index.mjs +6 -0
- package/esm/utils/map-stop-reason.mjs +30 -0
- package/esm/utils/map-stop-reason.mjs.map +1 -0
- package/esm/utils/to-anthropic-messages.mjs +125 -0
- package/esm/utils/to-anthropic-messages.mjs.map +1 -0
- package/esm/utils/to-anthropic-tools.mjs +37 -0
- package/esm/utils/to-anthropic-tools.mjs.map +1 -0
- package/esm/utils/wrap-anthropic-error.mjs +158 -0
- package/esm/utils/wrap-anthropic-error.mjs.map +1 -0
- package/llms-full.txt +143 -0
- package/llms.txt +9 -0
- package/package.json +39 -0
- package/skills/README.md +9 -0
- package/skills/setup-anthropic/SKILL.md +133 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.cjs","names":["AIError","ProviderTimeoutError","ProviderAuthError","QuotaExceededError","ProviderRateLimitError","ContextLengthExceededError","InvalidRequestError","ProviderError","APIError","APIConnectionTimeoutError","log","Anthropic"],"sources":["../../../../../@warlock.js/ai-anthropic/src/known-vision-models.ts","../../../../../@warlock.js/ai-anthropic/src/utils/map-stop-reason.ts","../../../../../@warlock.js/ai-anthropic/src/utils/to-anthropic-messages.ts","../../../../../@warlock.js/ai-anthropic/src/utils/to-anthropic-tools.ts","../../../../../@warlock.js/ai-anthropic/src/utils/wrap-anthropic-error.ts","../../../../../@warlock.js/ai-anthropic/src/model.ts","../../../../../@warlock.js/ai-anthropic/src/sdk.ts"],"sourcesContent":["/**\n * Model-name prefixes for Claude families that accept image input\n * (vision) on the Messages API.\n *\n * Every Claude 3, Claude 3.5/3.7, and Claude 4 family model is\n * multimodal, so the list covers both the dotted legacy naming\n * (`claude-3-haiku-...`, `claude-3-5-sonnet-...`) and the current\n * `claude-<tier>-4-*` naming (`claude-opus-4-7`, `claude-sonnet-4-6`,\n * `claude-haiku-4-5`). Pre-3 families (`claude-2`, `claude-instant`)\n * are text-only and intentionally absent.\n *\n * Matched as a prefix so dated variants (`claude-opus-4-20250514`) are\n * covered without listing every release tag. Devs can always override\n * per-model via `anthropic.model({ name, vision: true | false })` —\n * explicit config wins over inference in either direction.\n */\nconst VISION_CAPABLE_PREFIXES = [\n \"claude-3\",\n \"claude-4\",\n \"claude-opus-4\",\n \"claude-sonnet-4\",\n \"claude-haiku-4\",\n];\n\n/**\n * Infer whether a given Claude model name supports vision based on the\n * known-prefix list. Unknown models default to `false` so that passing\n * an image attachment to an unsupported model surfaces a clear,\n * agent-side capability error instead of an opaque Anthropic 400.\n *\n * @example\n * inferVisionCapability(\"claude-sonnet-4-6\"); // → true\n * inferVisionCapability(\"claude-3-5-sonnet-latest\"); // → true\n * inferVisionCapability(\"claude-2.1\"); // → false\n * inferVisionCapability(\"custom-proxy-llm\"); // → false\n */\nexport function inferVisionCapability(modelName: string): boolean {\n const normalized = modelName.toLowerCase();\n\n return VISION_CAPABLE_PREFIXES.some((prefix) => normalized.startsWith(prefix));\n}\n","import type { FinishReason } from \"@warlock.js/ai\";\n\nconst stopReasonMap: Record<string, FinishReason> = {\n end_turn: \"stop\",\n stop_sequence: \"stop\",\n max_tokens: \"length\",\n tool_use: \"tool_calls\",\n};\n\n/**\n * Map Anthropic's `stop_reason` to the normalized `FinishReason` union.\n *\n * `end_turn` / `stop_sequence` are both natural stops. `max_tokens`\n * maps to `length`. `tool_use` maps to `tool_calls`. Everything else —\n * `refusal` (policy intervention), `pause_turn` (incomplete\n * long-running turn), `null`, or any value a future API version adds —\n * falls through to `\"error\"` so the agent treats the trip as a\n * non-clean terminal rather than silently accepting a partial result.\n *\n * @example\n * mapStopReason(\"end_turn\"); // \"stop\"\n * mapStopReason(\"tool_use\"); // \"tool_calls\"\n * mapStopReason(\"refusal\"); // \"error\"\n * mapStopReason(null); // \"error\"\n */\nexport function mapStopReason(raw: string | null | undefined): FinishReason {\n return stopReasonMap[raw ?? \"\"] ?? \"error\";\n}\n","import type { ContentPart, Message } from \"@warlock.js/ai\";\nimport type Anthropic from \"@anthropic-ai/sdk\";\n\n/**\n * Result of splitting a vendor-neutral `Message[]` for the Anthropic\n * Messages API: the system prompt is hoisted to a top-level `system`\n * string (Anthropic has no `\"system\"` role inside `messages`), and the\n * remaining turns are mapped to `MessageParam[]`.\n */\nexport type AnthropicMessages = {\n system: string | undefined;\n messages: Anthropic.MessageParam[];\n};\n\nconst ANTHROPIC_IMAGE_MEDIA_TYPES = [\"image/jpeg\", \"image/png\", \"image/gif\", \"image/webp\"] as const;\n\ntype AnthropicImageMediaType = (typeof ANTHROPIC_IMAGE_MEDIA_TYPES)[number];\n\n/**\n * Convert vendor-neutral `Message[]` into Anthropic's request shape.\n *\n * Anthropic differs from the OpenAI Chat protocol in three ways this\n * function absorbs:\n *\n * 1. **No `system` role.** System messages are concatenated (newline-\n * separated) and returned separately as the top-level `system`\n * parameter.\n * 2. **Tool results are `user` turns.** A neutral `tool` message becomes\n * a `user` message whose content is a single `tool_result` block\n * keyed by `tool_use_id`.\n * 3. **Tool calls are `tool_use` content blocks.** An assistant message\n * carrying `toolCalls` becomes an `assistant` message whose content\n * is an optional leading `text` block followed by one `tool_use`\n * block per call.\n *\n * Consecutive same-role turns are left as-is — the Messages API merges\n * them server-side.\n *\n * @example\n * const { system, messages } = toAnthropicMessages([\n * { role: \"system\", content: \"Be concise.\" },\n * { role: \"user\", content: \"Hi\" },\n * ]);\n * // system === \"Be concise.\"\n * // messages === [{ role: \"user\", content: \"Hi\" }]\n */\nexport function toAnthropicMessages(messages: Message[]): AnthropicMessages {\n const systemParts: string[] = [];\n const mapped: Anthropic.MessageParam[] = [];\n\n for (const message of messages) {\n if (message.role === \"system\") {\n systemParts.push(stringifyContent(message.content));\n\n continue;\n }\n\n if (message.role === \"tool\") {\n mapped.push({\n role: \"user\",\n content: [\n {\n type: \"tool_result\",\n tool_use_id: message.toolCallId ?? \"\",\n content: stringifyContent(message.content),\n },\n ],\n });\n\n continue;\n }\n\n if (message.role === \"assistant\" && message.toolCalls && message.toolCalls.length > 0) {\n const blocks: Anthropic.ContentBlockParam[] = [];\n const text = stringifyContent(message.content);\n\n if (text) {\n blocks.push({ type: \"text\", text });\n }\n\n for (const toolCall of message.toolCalls) {\n blocks.push({\n type: \"tool_use\",\n id: toolCall.id,\n name: toolCall.name,\n input: toolCall.input ?? {},\n });\n }\n\n mapped.push({ role: \"assistant\", content: blocks });\n\n continue;\n }\n\n if (message.role === \"user\" && Array.isArray(message.content)) {\n mapped.push({\n role: \"user\",\n content: message.content.map(toAnthropicContentBlock),\n });\n\n continue;\n }\n\n mapped.push({\n role: message.role === \"assistant\" ? \"assistant\" : \"user\",\n content: stringifyContent(message.content),\n });\n }\n\n return {\n system: systemParts.length > 0 ? systemParts.join(\"\\n\\n\") : undefined,\n messages: mapped,\n };\n}\n\n/**\n * Multipart content is only meaningful on user messages — for any other\n * role collapse a `ContentPart[]` to its concatenated text so the wire\n * format stays valid. Plain strings pass through unchanged.\n */\nfunction stringifyContent(content: string | ContentPart[]): string {\n if (typeof content === \"string\") {\n return content;\n }\n\n return content\n .filter((part): part is { type: \"text\"; text: string } => part.type === \"text\")\n .map((part) => part.text)\n .join(\"\");\n}\n\n/**\n * Map a single resolved `ContentPart` to an Anthropic content block.\n * Text passes straight through; images become an `image` block with a\n * `url` source (remote) or a `base64` source (inlined bytes). The\n * agent has already resolved every attachment before it reaches here,\n * so this never reads files or fetches URLs.\n */\nfunction toAnthropicContentBlock(part: ContentPart): Anthropic.ContentBlockParam {\n if (part.type === \"text\") {\n return { type: \"text\", text: part.text };\n }\n\n if (\"url\" in part.source) {\n return { type: \"image\", source: { type: \"url\", url: part.source.url } };\n }\n\n return {\n type: \"image\",\n source: {\n type: \"base64\",\n media_type: part.source.mediaType as AnthropicImageMediaType,\n data: part.source.base64,\n },\n };\n}\n","import { extractJsonSchema, type ToolConfig } from \"@warlock.js/ai\";\nimport type Anthropic from \"@anthropic-ai/sdk\";\n\n/**\n * Convert vendor-neutral `ToolConfig[]` into Anthropic's `tools` array.\n * Uses the shared `extractJsonSchema` helper; Anthropic requires the\n * input schema to be a JSON-Schema object, so a non-object extraction\n * is coerced into an empty-object schema rather than rejected — the\n * tool still registers and the model simply sees no parameters.\n *\n * @example\n * const tools = toAnthropicTools([weatherTool, calculatorTool]);\n * await client.messages.create({ model, max_tokens, messages, tools });\n */\nexport function toAnthropicTools(\n tools: ToolConfig<unknown, unknown>[] | undefined,\n): Anthropic.Tool[] | undefined {\n if (!tools || tools.length === 0) {\n return undefined;\n }\n\n return tools.map((tool) => ({\n name: tool.name,\n description: tool.description,\n input_schema: toInputSchema(tool.input),\n }));\n}\n\n/**\n * Coerce the extracted JSON Schema into Anthropic's `Tool.InputSchema`\n * shape (root must be `{ type: \"object\" }`). Anything that isn't an\n * object schema degrades to a parameterless object so registration\n * never fails on a malformed extractor result.\n */\nfunction toInputSchema(input: ToolConfig<unknown, unknown>[\"input\"]): Anthropic.Tool.InputSchema {\n const schema = extractJsonSchema(input);\n\n if (schema && schema.type === \"object\") {\n return schema as Anthropic.Tool.InputSchema;\n }\n\n return { type: \"object\" };\n}\n","import {\n AIError,\n ContextLengthExceededError,\n InvalidRequestError,\n ProviderAuthError,\n ProviderError,\n ProviderRateLimitError,\n ProviderTimeoutError,\n QuotaExceededError,\n} from \"@warlock.js/ai\";\nimport { APIConnectionTimeoutError, APIError } from \"@anthropic-ai/sdk\";\n\n/**\n * Raw-error fields the wrapper reads off an Anthropic SDK error.\n *\n * `APIError` exposes `status`, `type` (the `error.type` from the\n * response body, e.g. `\"rate_limit_error\"`), `message`, `headers`, and\n * `requestID`. We duck-type because wrapped retries, proxied errors,\n * and custom subclasses sometimes lose the `instanceof` relationship.\n */\ntype AnthropicErrorShape = {\n status?: number;\n type?: string | null;\n message?: string;\n headers?: HeaderBag | undefined;\n name?: string;\n requestId?: string;\n};\n\n/** Either a fetch `Headers` instance or a plain record (duck-typed tests). */\ntype HeaderBag = Headers | Record<string, string>;\n\n/**\n * Wrap any thrown value caught inside the Anthropic adapter into the\n * appropriate `@warlock.js/ai` `AIError` subclass.\n *\n * **Dispatch strategy.** Anthropic has no per-error machine `code`; the\n * stable identifier is `error.type` on the response body (surfaced as\n * `APIError.type`). Dispatch prefers `type`, falls back to `status`\n * when the body was stripped (common with proxies). Name-based\n * detection catches transport-layer timeouts that never produced an\n * HTTP response. The `invalid_request_error` branch additionally\n * sniffs the message for Anthropic's \"prompt is too long\" phrasing,\n * which is the only signal that a 400 was a context-length overflow.\n *\n * `AIError` instances pass through unchanged so `catch/throw wrap(e)`\n * pipelines never double-wrap.\n *\n * @example\n * try {\n * return await this.client.messages.create(...);\n * } catch (thrown) {\n * throw wrapAnthropicError(thrown);\n * }\n */\nexport function wrapAnthropicError(thrown: unknown): AIError {\n if (thrown instanceof AIError) {\n return thrown;\n }\n\n const shape = toShape(thrown);\n const context = buildContext(shape);\n const message = shape.message ?? (thrown instanceof Error ? thrown.message : String(thrown));\n\n if (isTimeout(thrown, shape)) {\n return new ProviderTimeoutError(message, { cause: thrown, context });\n }\n\n if (shape.type === \"authentication_error\" || shape.type === \"permission_error\") {\n return new ProviderAuthError(message, { cause: thrown, context });\n }\n\n if (shape.status === 401 || shape.status === 403) {\n return new ProviderAuthError(message, { cause: thrown, context });\n }\n\n if (shape.type === \"billing_error\") {\n return new QuotaExceededError(message, { cause: thrown, context });\n }\n\n if (shape.type === \"rate_limit_error\" || shape.status === 429) {\n return new ProviderRateLimitError(message, {\n cause: thrown,\n context,\n retryAfter: parseRetryAfter(shape.headers),\n });\n }\n\n if (shape.type === \"invalid_request_error\" || isClientStatus(shape.status)) {\n if (/prompt is too long/i.test(message)) {\n return new ContextLengthExceededError(message, { cause: thrown, context });\n }\n\n return new InvalidRequestError(message, { cause: thrown, context });\n }\n\n return new ProviderError(message, { cause: thrown, context });\n}\n\n/**\n * Read the raw error shape without depending on `instanceof APIError`\n * — proxies and re-wrappers strip the prototype chain. Duck-typing on\n * the visible fields is resilient to both.\n */\nfunction toShape(thrown: unknown): AnthropicErrorShape {\n if (thrown instanceof APIError) {\n return {\n status: typeof thrown.status === \"number\" ? thrown.status : undefined,\n type: thrown.type,\n message: thrown.message,\n headers: thrown.headers,\n name: thrown.name,\n requestId: thrown.requestID ?? undefined,\n };\n }\n\n if (typeof thrown === \"object\" && thrown !== null) {\n const raw = thrown as Record<string, unknown>;\n\n return {\n status: typeof raw.status === \"number\" ? raw.status : undefined,\n type: typeof raw.type === \"string\" ? raw.type : undefined,\n message: typeof raw.message === \"string\" ? raw.message : undefined,\n headers: isHeaderBag(raw.headers) ? raw.headers : undefined,\n name: typeof raw.name === \"string\" ? raw.name : undefined,\n requestId: readRequestId(raw),\n };\n }\n\n return {};\n}\n\n/**\n * Decide whether the thrown value represents a timeout. The Anthropic\n * SDK throws `APIConnectionTimeoutError` for transport-level timeouts;\n * Node surfaces `ETIMEDOUT` / `ECONNABORTED` on the socket layer.\n * Either signal counts.\n */\nfunction isTimeout(thrown: unknown, shape: AnthropicErrorShape): boolean {\n if (thrown instanceof APIConnectionTimeoutError) {\n return true;\n }\n\n if (shape.name === \"APIConnectionTimeoutError\") {\n return true;\n }\n\n if (typeof thrown === \"object\" && thrown !== null) {\n const code = (thrown as Record<string, unknown>).code;\n\n if (code === \"ETIMEDOUT\" || code === \"ECONNABORTED\") {\n return true;\n }\n }\n\n return false;\n}\n\n/** True for HTTP 4xx — a client-side request problem, not a server fault. */\nfunction isClientStatus(status: number | undefined): boolean {\n return typeof status === \"number\" && status >= 400 && status < 500;\n}\n\n/**\n * Attach the raw diagnostic fields to `error.context` so consumers\n * have everything the provider surfaced without each subclass having\n * to redeclare them. Never includes `cause` — that lives on\n * `error.cause`.\n */\nfunction buildContext(shape: AnthropicErrorShape): Record<string, unknown> {\n const context: Record<string, unknown> = {};\n\n if (shape.status !== undefined) {\n context.status = shape.status;\n }\n\n if (shape.type) {\n context.type = shape.type;\n }\n\n if (shape.requestId) {\n context.requestId = shape.requestId;\n }\n\n return context;\n}\n\n/** Anthropic exposes the request id as `requestID`; some proxies use `request_id`. */\nfunction readRequestId(raw: Record<string, unknown>): string | undefined {\n if (typeof raw.requestID === \"string\") {\n return raw.requestID;\n }\n\n if (typeof raw.request_id === \"string\") {\n return raw.request_id;\n }\n\n return undefined;\n}\n\nfunction isHeaderBag(value: unknown): value is HeaderBag {\n return typeof value === \"object\" && value !== null;\n}\n\n/** Read a header value from either a `Headers` instance or a plain record. */\nfunction readHeader(headers: HeaderBag, name: string): string | undefined {\n if (typeof (headers as Headers).get === \"function\") {\n return (headers as Headers).get(name) ?? undefined;\n }\n\n const record = headers as Record<string, string>;\n\n return record[name] ?? record[name.toLowerCase()];\n}\n\n/**\n * Parse the `Retry-After` response header (seconds per HTTP spec) into\n * milliseconds so consumers can feed it straight to `setTimeout`.\n * Returns `undefined` when missing or unparseable.\n */\nfunction parseRetryAfter(headers: HeaderBag | undefined): number | undefined {\n if (!headers) {\n return undefined;\n }\n\n const raw = readHeader(headers, \"retry-after\") ?? readHeader(headers, \"Retry-After\");\n\n if (!raw) {\n return undefined;\n }\n\n const seconds = Number(raw);\n\n if (!Number.isFinite(seconds) || seconds < 0) {\n return undefined;\n }\n\n return Math.round(seconds * 1000);\n}\n","import {\n safeJsonParse,\n type Message,\n type ModelCallOptions,\n type ModelCapabilities,\n type ModelContract,\n type ModelPricing,\n type ModelResponse,\n type ModelStreamChunk,\n type ModelToolCallRequest,\n type Usage,\n} from \"@warlock.js/ai\";\nimport { log, type Logger } from \"@warlock.js/logger\";\nimport type Anthropic from \"@anthropic-ai/sdk\";\nimport type { AnthropicModelConfig } from \"./config.type\";\nimport { inferVisionCapability } from \"./known-vision-models\";\nimport { mapStopReason, toAnthropicMessages, toAnthropicTools, wrapAnthropicError } from \"./utils\";\n\nconst LOG_MODULE = \"ai.anthropic\";\n\n/**\n * Anthropic requires `max_tokens` on every request (unlike OpenAI,\n * where it is optional). When neither the per-call option nor the\n * model config supplies one, fall back to a generous default so a\n * caller who never thought about token caps still gets a complete\n * answer instead of a 400.\n */\nconst DEFAULT_MAX_TOKENS = 4096;\n\n/**\n * Anthropic-backed implementation of `ModelContract`.\n *\n * **Role.** The provider-facing bridge between the vendor-neutral\n * `@warlock.js/ai` agent runtime and the official `@anthropic-ai/sdk`\n * Messages API. Agents, workflows, and supervisors never talk to\n * Anthropic directly — they hold a `ModelContract`, and this class is\n * what makes that contract concrete for Claude models.\n *\n * **Responsibility.**\n * - Owns: a long-lived `Anthropic` client + frozen `ModelConfig`\n * (name, temperature, maxTokens) used as defaults for every call.\n * - Owns: translating vendor-neutral `Message[]` / `ToolConfig[]` into\n * Anthropic wire shapes (system hoisting, `tool_use` / `tool_result`\n * blocks) on the way out, and translating Anthropic's content-block\n * response (text, tool calls, stop reason, usage) back into the\n * neutral shapes on the way in.\n * - Does NOT own: dispatching tools, deciding whether to loop, tracking\n * conversation history, or retrying on failure — those are agent\n * concerns. The model is a stateless (per-call) protocol adapter.\n *\n * Because it holds a live client and shared defaults, it is modeled as\n * a class (see §4.2 of code-style.md — \"long-lived state across\n * calls\").\n *\n * @example\n * import Anthropic from \"@anthropic-ai/sdk\";\n * const client = new Anthropic({ apiKey: process.env.ANTHROPIC_API_KEY });\n * const model = new AnthropicModel(client, { name: \"claude-sonnet-4-6\" });\n *\n * const myAgent = agent({\n * model,\n * systemPrompt: \"You are a helpful assistant.\",\n * tools: [searchTool],\n * });\n *\n * const result = await myAgent.execute(\"Summarize today's news.\");\n */\nexport class AnthropicModel implements ModelContract {\n public readonly name: string;\n public readonly provider: string;\n public readonly capabilities: ModelCapabilities;\n public readonly pricing?: ModelPricing;\n\n private readonly client: Anthropic;\n private readonly config: AnthropicModelConfig;\n private readonly logger: Logger = log;\n\n public constructor(\n client: Anthropic,\n config: AnthropicModelConfig,\n provider: string = \"anthropic\",\n ) {\n this.client = client;\n this.config = config;\n this.name = config.name;\n this.provider = provider;\n this.pricing = config.pricing;\n this.capabilities = {\n structuredOutput: config.structuredOutput ?? true,\n vision: config.vision ?? inferVisionCapability(config.name),\n };\n }\n\n /**\n * Single-shot completion. Sends the full message list to the Messages\n * endpoint, waits for the terminal response, and reshapes it into a\n * vendor-neutral `ModelResponse`. Per-call `options` override the\n * instance's `ModelConfig` defaults for this call only.\n */\n public async complete(messages: Message[], options?: ModelCallOptions): Promise<ModelResponse> {\n this.logger.debug(LOG_MODULE, \"request\", \"Starting call to messages.create\", {\n model: this.name,\n messageCount: messages.length,\n streaming: false,\n toolCount: options?.tools?.length ?? 0,\n });\n\n let response: Anthropic.Message;\n\n try {\n response = await this.client.messages.create(\n { ...this.buildParams(messages, options), stream: false },\n options?.signal ? { signal: options.signal } : undefined,\n );\n } catch (thrown) {\n throw this.logAndWrap(thrown);\n }\n\n const finishReason = mapStopReason(response.stop_reason);\n const usage = this.extractUsage(response.usage);\n const toolCalls = this.extractToolCalls(response.content);\n\n this.logger.debug(LOG_MODULE, \"response\", \"call to messages.create succeeded\", {\n finishReason,\n usage,\n });\n\n return {\n content: this.extractText(response.content),\n finishReason,\n usage,\n toolCalls,\n };\n }\n\n /**\n * Incremental streaming completion. Yields neutral `ModelStreamChunk`s\n * — `delta` for text tokens, `tool-call` once a `tool_use` block's\n * arguments have fully accumulated, and a terminal `done` carrying the\n * final finish reason + usage totals. Callers consume it with\n * `for await`.\n */\n public async *stream(\n messages: Message[],\n options?: ModelCallOptions,\n ): AsyncIterable<ModelStreamChunk> {\n this.logger.debug(LOG_MODULE, \"request\", \"Starting streaming call to messages.create\", {\n model: this.name,\n messageCount: messages.length,\n streaming: true,\n toolCount: options?.tools?.length ?? 0,\n });\n\n let stream: Awaited<ReturnType<Anthropic[\"messages\"][\"create\"]>>;\n\n try {\n stream = await this.client.messages.create(\n { ...this.buildParams(messages, options), stream: true },\n options?.signal ? { signal: options.signal } : undefined,\n );\n } catch (thrown) {\n throw this.logAndWrap(thrown);\n }\n\n let rawStopReason: string | null = null;\n const usage: Usage = { input: 0, output: 0, total: 0 };\n const toolBlocks = new Map<number, { id: string; name: string; json: string }>();\n\n try {\n for await (const event of stream as AsyncIterable<Anthropic.RawMessageStreamEvent>) {\n if (event.type === \"message_start\") {\n usage.input = event.message.usage.input_tokens ?? 0;\n\n const cached = event.message.usage.cache_read_input_tokens;\n\n if (cached !== null && cached !== undefined && cached > 0) {\n usage.cachedTokens = cached;\n }\n\n continue;\n }\n\n if (event.type === \"content_block_start\") {\n const block = event.content_block;\n\n if (block.type === \"tool_use\") {\n toolBlocks.set(event.index, { id: block.id, name: block.name, json: \"\" });\n }\n\n continue;\n }\n\n if (event.type === \"content_block_delta\") {\n if (event.delta.type === \"text_delta\") {\n yield { type: \"delta\", content: event.delta.text };\n } else if (event.delta.type === \"input_json_delta\") {\n const accumulator = toolBlocks.get(event.index);\n\n if (accumulator) {\n accumulator.json += event.delta.partial_json;\n }\n }\n\n continue;\n }\n\n if (event.type === \"content_block_stop\") {\n const accumulator = toolBlocks.get(event.index);\n\n if (accumulator) {\n yield {\n type: \"tool-call\",\n id: accumulator.id,\n name: accumulator.name,\n input: safeJsonParse<Record<string, unknown>>(accumulator.json, {}),\n };\n\n toolBlocks.delete(event.index);\n }\n\n continue;\n }\n\n if (event.type === \"message_delta\") {\n rawStopReason = event.delta.stop_reason ?? rawStopReason;\n usage.output = event.usage.output_tokens ?? usage.output;\n }\n }\n } catch (thrown) {\n throw this.logAndWrap(thrown);\n }\n\n usage.total = usage.input + usage.output;\n\n const finishReason = mapStopReason(rawStopReason);\n\n this.logger.debug(LOG_MODULE, \"response\", \"Streaming call to messages.create succeeded\", {\n finishReason,\n usage,\n });\n\n yield { type: \"done\", finishReason, usage };\n }\n\n /**\n * Assemble the Anthropic request body shared by `complete()` and\n * `stream()` (each adds its own `stream` literal so the SDK's create\n * overload resolves to the right return type). Hoists the system\n * prompt out of `messages`, resolves `max_tokens` (required by\n * Anthropic) with the documented default, and conditionally attaches\n * temperature, tools, and native structured output.\n */\n private buildParams(\n messages: Message[],\n options: ModelCallOptions | undefined,\n ): Omit<Anthropic.MessageCreateParamsNonStreaming, \"stream\"> {\n const { system, messages: anthropicMessages } = toAnthropicMessages(messages);\n const temperature = options?.temperature ?? this.config.temperature;\n\n return {\n model: this.name,\n max_tokens: options?.maxTokens ?? this.config.maxTokens ?? DEFAULT_MAX_TOKENS,\n messages: anthropicMessages,\n ...(system ? { system } : {}),\n ...(temperature !== undefined ? { temperature } : {}),\n ...this.buildTools(options?.tools),\n ...this.buildStructuredOutput(options?.responseSchema),\n };\n }\n\n /**\n * Spread-friendly tools fragment. Returns an empty object when no\n * tools were supplied so the caller can unconditionally spread it.\n */\n private buildTools(tools: ModelCallOptions[\"tools\"]): { tools?: Anthropic.Tool[] } {\n const mapped = toAnthropicTools(tools);\n\n return mapped ? { tools: mapped } : {};\n }\n\n /**\n * Translate the neutral `responseSchema` option into Anthropic's\n * native `output_config.format` (JSON-schema structured outputs).\n *\n * Only emitted when the model declares the `structuredOutput`\n * capability AND the schema is a proper root-object JSON Schema —\n * Anthropic rejects non-object roots. When the capability is off\n * (config override) or the schema is non-object, returns an empty\n * object: the agent has already injected a soft schema hint into the\n * system prompt as the fallback, and client-side `validate()` still\n * enforces shape.\n */\n private buildStructuredOutput(responseSchema: Record<string, unknown> | undefined): {\n output_config?: Anthropic.OutputConfig;\n } {\n if (!responseSchema || !this.capabilities.structuredOutput) {\n return {};\n }\n\n if (responseSchema.type !== \"object\" || typeof responseSchema.properties !== \"object\") {\n return {};\n }\n\n return {\n output_config: {\n format: { type: \"json_schema\", schema: responseSchema },\n },\n };\n }\n\n /**\n * Concatenate every `text` content block into the single neutral\n * `content` string. `tool_use` and other block types are ignored\n * here — tool calls are surfaced separately via `extractToolCalls`.\n */\n private extractText(content: Anthropic.ContentBlock[]): string {\n return content\n .filter((block): block is Anthropic.TextBlock => block.type === \"text\")\n .map((block) => block.text)\n .join(\"\");\n }\n\n /**\n * Reshape Anthropic's `tool_use` content blocks into the neutral\n * `ModelToolCallRequest[]`. Returns `undefined` when the model\n * requested no tools so callers can branch on presence.\n */\n private extractToolCalls(\n content: Anthropic.ContentBlock[],\n ): ModelToolCallRequest[] | undefined {\n const toolUses = content.filter(\n (block): block is Anthropic.ToolUseBlock => block.type === \"tool_use\",\n );\n\n if (toolUses.length === 0) {\n return undefined;\n }\n\n return toolUses.map((block) => ({\n id: block.id,\n name: block.name,\n input: (block.input ?? {}) as Record<string, unknown>,\n }));\n }\n\n /**\n * Normalize Anthropic's `usage` block into the neutral `Usage` shape.\n * Anthropic reports `input_tokens` / `output_tokens` separately with\n * no pre-summed total, so `total` is computed. Cache-read tokens are\n * surfaced as `cachedTokens` only when non-zero.\n */\n private extractUsage(raw: Anthropic.Usage): Usage {\n const input = raw.input_tokens ?? 0;\n const output = raw.output_tokens ?? 0;\n const cached = raw.cache_read_input_tokens;\n\n return {\n input,\n output,\n total: input + output,\n ...(cached !== null && cached !== undefined && cached > 0 ? { cachedTokens: cached } : {}),\n };\n }\n\n /**\n * Wrap a thrown provider error into the typed `AIError` hierarchy and\n * emit the standard error log line before it propagates. Shared by\n * every catch site so the log shape stays identical.\n */\n private logAndWrap(thrown: unknown) {\n const wrapped = wrapAnthropicError(thrown);\n\n this.logger.error(LOG_MODULE, \"error\", wrapped.message, {\n code: wrapped.code,\n context: wrapped.context,\n });\n\n return wrapped;\n }\n}\n","import Anthropic from \"@anthropic-ai/sdk\";\nimport type { ModelContract, ModelPricing, SDKAdapterContract } from \"@warlock.js/ai\";\nimport { approximateTokenCount } from \"@warlock.js/ai\";\nimport type { AnthropicModelConfig, AnthropicSDKConfig } from \"./config.type\";\nimport { AnthropicModel } from \"./model\";\n\n/**\n * Anthropic-backed implementation of `SDKAdapterContract`.\n *\n * **Role.** The package entry point for Claude models via the official\n * `@anthropic-ai/sdk`. A single `AnthropicSDK` instance holds one live\n * `Anthropic` client, shared by every `ModelContract` it produces via\n * `model()`. Users construct one SDK per account and reuse it across\n * all agents, workflows, and supervisors that target Anthropic.\n *\n * **Responsibility.**\n * - Owns: a long-lived `Anthropic` client (authentication, base URL)\n * and its lifetime scope. Factory for `AnthropicModel` instances —\n * each model call gets a reference to the same client.\n * - Does NOT own: anything per-call (tool execution, message history,\n * streaming loop) — those live in `AnthropicModel` and the agent\n * runtime. Does NOT implement `embedder()`: Anthropic ships no\n * first-party embeddings API (the contract marks it optional).\n *\n * Modeled as a class (see §4.2 of code-style.md — \"long-lived state\n * across many calls\"): the `Anthropic` client is heavy to construct\n * and designed to be reused; keeping it on `this` makes that reuse\n * explicit and aligns with the `new Anthropic(...)` upstream\n * convention.\n *\n * @example\n * const anthropic = new AnthropicSDK({ apiKey: process.env.ANTHROPIC_API_KEY! });\n * const model = anthropic.model({ name: \"claude-sonnet-4-6\", temperature: 0.7 });\n * const tokens = await anthropic.count(\"Hello world\");\n *\n * @example\n * // Compose into an `ai.anthropic` namespace for ergonomic agent wiring\n * const ai = { agent, tool, systemPrompt, anthropic: new AnthropicSDK({ apiKey }) };\n * const myAgent = ai.agent({ model: ai.anthropic.model({ name: \"claude-haiku-4-5\" }) });\n */\nexport class AnthropicSDK implements SDKAdapterContract {\n private readonly client: Anthropic;\n private readonly provider: string;\n private readonly pricing?: Record<string, ModelPricing>;\n\n public constructor(config: AnthropicSDKConfig) {\n this.client = new Anthropic({\n apiKey: config.apiKey,\n baseURL: config.baseURL,\n });\n this.provider = config.provider ?? \"anthropic\";\n this.pricing = config.pricing;\n }\n\n /**\n * Build an `AnthropicModel` bound to this SDK's client. Each call\n * returns a fresh model instance, but all instances share the\n * underlying `Anthropic` client — connection pools, rate limits, and\n * authentication stay unified across every model produced here. The\n * SDK's `provider` label is forwarded so every model self-identifies\n * as coming from the same upstream.\n *\n * Pricing resolution: per-model `config.pricing` wins; otherwise the\n * SDK-level registry entry keyed by `config.name`; otherwise\n * `undefined` (no cost computed).\n */\n public model(config: AnthropicModelConfig): ModelContract {\n const resolvedPricing = config.pricing ?? this.pricing?.[config.name];\n const resolvedConfig: AnthropicModelConfig =\n resolvedPricing === config.pricing ? config : { ...config, pricing: resolvedPricing };\n\n return new AnthropicModel(this.client, resolvedConfig, this.provider);\n }\n\n /**\n * Rough token-count estimate for a given text. Uses the\n * character-heuristic (`approximateTokenCount`) from the core package\n * — good enough for budgeting and quota guards, not for billing.\n * Anthropic does expose a `messages.countTokens` endpoint, but that\n * is a network round-trip; `count()` is intentionally offline and\n * synchronous-cost. The optional model id is reserved for future\n * per-model tokenizer dispatch; currently ignored.\n */\n public async count(text: string, _model?: string): Promise<number> {\n return approximateTokenCount(text);\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAgBA,MAAM,0BAA0B;CAC9B;CACA;CACA;CACA;CACA;AACF;;;;;;;;;;;;;AAcA,SAAgB,sBAAsB,WAA4B;CAChE,MAAM,aAAa,UAAU,YAAY;CAEzC,OAAO,wBAAwB,MAAM,WAAW,WAAW,WAAW,MAAM,CAAC;AAC/E;;;;ACtCA,MAAM,gBAA8C;CAClD,UAAU;CACV,eAAe;CACf,YAAY;CACZ,UAAU;AACZ;;;;;;;;;;;;;;;;;AAkBA,SAAgB,cAAc,KAA8C;CAC1E,OAAO,cAAc,OAAO,OAAO;AACrC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;ACmBA,SAAgB,oBAAoB,UAAwC;CAC1E,MAAM,cAAwB,CAAC;CAC/B,MAAM,SAAmC,CAAC;CAE1C,KAAK,MAAM,WAAW,UAAU;EAC9B,IAAI,QAAQ,SAAS,UAAU;GAC7B,YAAY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d.ts
|
|
5
|
+
/**
|
|
6
|
+
* Configuration for the Anthropic SDK adapter.
|
|
7
|
+
*
|
|
8
|
+
* `provider` labels the SDK with the upstream you're pointing at —
|
|
9
|
+
* useful when the same Anthropic-compatible client is wired to a
|
|
10
|
+
* gateway or proxy. The label flows through to `ModelContract.provider`
|
|
11
|
+
* and surfaces on `AgentReport.model`, logs, and any middleware that
|
|
12
|
+
* branches on provider identity. Defaults to `"anthropic"` when omitted.
|
|
13
|
+
*
|
|
14
|
+
* `pricing` is an optional SDK-level registry keyed by model name. One
|
|
15
|
+
* source of truth per provider; matches how providers publish pricing
|
|
16
|
+
* tables. Resolution at `model()` call time: per-model `pricing` (on
|
|
17
|
+
* `AnthropicModelConfig`) > this SDK registry > `undefined`. When
|
|
18
|
+
* neither is set, `Usage.cost` stays `undefined` on every report this
|
|
19
|
+
* SDK's models produce.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* new AnthropicSDK({ apiKey: process.env.ANTHROPIC_API_KEY! });
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* // SDK-level pricing registry — pricing in USD per million tokens.
|
|
26
|
+
* new AnthropicSDK({
|
|
27
|
+
* apiKey,
|
|
28
|
+
* pricing: {
|
|
29
|
+
* "claude-haiku-4-5": { input: 1, output: 5, cachedInput: 0.1 },
|
|
30
|
+
* "claude-sonnet-4-6": { input: 3, output: 15, cachedInput: 0.3 },
|
|
31
|
+
* },
|
|
32
|
+
* });
|
|
33
|
+
*/
|
|
34
|
+
type AnthropicSDKConfig = ClientOptions & {
|
|
35
|
+
provider?: string;
|
|
36
|
+
/**
|
|
37
|
+
* Per-model USD pricing registry, keyed by model name. Surfaced onto
|
|
38
|
+
* every `AnthropicModel` produced by `model()`; per-model
|
|
39
|
+
* `AnthropicModelConfig.pricing` still wins when both are set.
|
|
40
|
+
*/
|
|
41
|
+
pricing?: Record<string, ModelPricing>;
|
|
42
|
+
};
|
|
43
|
+
/**
|
|
44
|
+
* Per-model configuration for `AnthropicSDK.model()`. Extends the
|
|
45
|
+
* neutral `ModelConfig` with Anthropic-specific capability overrides.
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
* anthropic.model({ name: "claude-sonnet-4-6" }); // vision auto-true
|
|
49
|
+
* anthropic.model({ name: "custom-proxy-model", vision: false }); // dev override
|
|
50
|
+
*/
|
|
51
|
+
type AnthropicModelConfig = ModelConfig & {
|
|
52
|
+
/**
|
|
53
|
+
* Override the auto-inferred vision capability. When omitted, the
|
|
54
|
+
* adapter checks the model name against the known Claude vision
|
|
55
|
+
* families (see `known-vision-models.ts`). Setting `true` or `false`
|
|
56
|
+
* explicitly always wins over inference — useful for proxied
|
|
57
|
+
* deployments or capability-degraded testing.
|
|
58
|
+
*/
|
|
59
|
+
vision?: boolean;
|
|
60
|
+
/**
|
|
61
|
+
* Override the inferred `structuredOutput` capability. When omitted,
|
|
62
|
+
* the adapter treats the model as capable and forwards
|
|
63
|
+
* `responseSchema` via Anthropic's native `output_config.format`
|
|
64
|
+
* (JSON-schema structured outputs). Set to `false` for models or
|
|
65
|
+
* proxies that don't support native structured output — the agent
|
|
66
|
+
* then re-injects a soft schema hint into the system prompt instead.
|
|
67
|
+
*/
|
|
68
|
+
structuredOutput?: boolean;
|
|
69
|
+
};
|
|
70
|
+
//#endregion
|
|
71
|
+
export { AnthropicModelConfig, AnthropicSDKConfig };
|
|
72
|
+
//# sourceMappingURL=config.type.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"config.type.d.mts","names":[],"sources":["../../../../../@warlock.js/ai-anthropic/src/config.type.ts"],"mappings":";;;;;;AAgCA;;;;;;;;;;;;;;AAOuC;AAWvC;;;;;;;;AAiBkB;;;;KAnCN,kBAAA,GAAqB,aAAA;EAC/B,QAAA;;;;;;EAMA,OAAA,GAAU,MAAA,SAAe,YAAA;AAAA;;;;;;;;;KAWf,oBAAA,GAAuB,WAAW;;;;;;;;EAQ5C,MAAA;;;;;;;;;EASA,gBAAA;AAAA"}
|
package/esm/index.d.mts
ADDED
package/esm/index.mjs
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
//#region ../../@warlock.js/ai-anthropic/src/known-vision-models.ts
|
|
2
|
+
/**
|
|
3
|
+
* Model-name prefixes for Claude families that accept image input
|
|
4
|
+
* (vision) on the Messages API.
|
|
5
|
+
*
|
|
6
|
+
* Every Claude 3, Claude 3.5/3.7, and Claude 4 family model is
|
|
7
|
+
* multimodal, so the list covers both the dotted legacy naming
|
|
8
|
+
* (`claude-3-haiku-...`, `claude-3-5-sonnet-...`) and the current
|
|
9
|
+
* `claude-<tier>-4-*` naming (`claude-opus-4-7`, `claude-sonnet-4-6`,
|
|
10
|
+
* `claude-haiku-4-5`). Pre-3 families (`claude-2`, `claude-instant`)
|
|
11
|
+
* are text-only and intentionally absent.
|
|
12
|
+
*
|
|
13
|
+
* Matched as a prefix so dated variants (`claude-opus-4-20250514`) are
|
|
14
|
+
* covered without listing every release tag. Devs can always override
|
|
15
|
+
* per-model via `anthropic.model({ name, vision: true | false })` —
|
|
16
|
+
* explicit config wins over inference in either direction.
|
|
17
|
+
*/
|
|
18
|
+
const VISION_CAPABLE_PREFIXES = [
|
|
19
|
+
"claude-3",
|
|
20
|
+
"claude-4",
|
|
21
|
+
"claude-opus-4",
|
|
22
|
+
"claude-sonnet-4",
|
|
23
|
+
"claude-haiku-4"
|
|
24
|
+
];
|
|
25
|
+
/**
|
|
26
|
+
* Infer whether a given Claude model name supports vision based on the
|
|
27
|
+
* known-prefix list. Unknown models default to `false` so that passing
|
|
28
|
+
* an image attachment to an unsupported model surfaces a clear,
|
|
29
|
+
* agent-side capability error instead of an opaque Anthropic 400.
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* inferVisionCapability("claude-sonnet-4-6"); // → true
|
|
33
|
+
* inferVisionCapability("claude-3-5-sonnet-latest"); // → true
|
|
34
|
+
* inferVisionCapability("claude-2.1"); // → false
|
|
35
|
+
* inferVisionCapability("custom-proxy-llm"); // → false
|
|
36
|
+
*/
|
|
37
|
+
function inferVisionCapability(modelName) {
|
|
38
|
+
const normalized = modelName.toLowerCase();
|
|
39
|
+
return VISION_CAPABLE_PREFIXES.some((prefix) => normalized.startsWith(prefix));
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
//#endregion
|
|
43
|
+
export { inferVisionCapability };
|
|
44
|
+
//# sourceMappingURL=known-vision-models.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"known-vision-models.mjs","names":[],"sources":["../../../../../@warlock.js/ai-anthropic/src/known-vision-models.ts"],"sourcesContent":["/**\n * Model-name prefixes for Claude families that accept image input\n * (vision) on the Messages API.\n *\n * Every Claude 3, Claude 3.5/3.7, and Claude 4 family model is\n * multimodal, so the list covers both the dotted legacy naming\n * (`claude-3-haiku-...`, `claude-3-5-sonnet-...`) and the current\n * `claude-<tier>-4-*` naming (`claude-opus-4-7`, `claude-sonnet-4-6`,\n * `claude-haiku-4-5`). Pre-3 families (`claude-2`, `claude-instant`)\n * are text-only and intentionally absent.\n *\n * Matched as a prefix so dated variants (`claude-opus-4-20250514`) are\n * covered without listing every release tag. Devs can always override\n * per-model via `anthropic.model({ name, vision: true | false })` —\n * explicit config wins over inference in either direction.\n */\nconst VISION_CAPABLE_PREFIXES = [\n \"claude-3\",\n \"claude-4\",\n \"claude-opus-4\",\n \"claude-sonnet-4\",\n \"claude-haiku-4\",\n];\n\n/**\n * Infer whether a given Claude model name supports vision based on the\n * known-prefix list. Unknown models default to `false` so that passing\n * an image attachment to an unsupported model surfaces a clear,\n * agent-side capability error instead of an opaque Anthropic 400.\n *\n * @example\n * inferVisionCapability(\"claude-sonnet-4-6\"); // → true\n * inferVisionCapability(\"claude-3-5-sonnet-latest\"); // → true\n * inferVisionCapability(\"claude-2.1\"); // → false\n * inferVisionCapability(\"custom-proxy-llm\"); // → false\n */\nexport function inferVisionCapability(modelName: string): boolean {\n const normalized = modelName.toLowerCase();\n\n return VISION_CAPABLE_PREFIXES.some((prefix) => normalized.startsWith(prefix));\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AAgBA,MAAM,0BAA0B;CAC9B;CACA;CACA;CACA;CACA;AACF;;;;;;;;;;;;;AAcA,SAAgB,sBAAsB,WAA4B;CAChE,MAAM,aAAa,UAAU,YAAY;CAEzC,OAAO,wBAAwB,MAAM,WAAW,WAAW,WAAW,MAAM,CAAC;AAC/E"}
|
package/esm/model.mjs
ADDED
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
import { inferVisionCapability } from "./known-vision-models.mjs";
|
|
2
|
+
import { mapStopReason } from "./utils/map-stop-reason.mjs";
|
|
3
|
+
import { toAnthropicMessages } from "./utils/to-anthropic-messages.mjs";
|
|
4
|
+
import { toAnthropicTools } from "./utils/to-anthropic-tools.mjs";
|
|
5
|
+
import { wrapAnthropicError } from "./utils/wrap-anthropic-error.mjs";
|
|
6
|
+
import "./utils/index.mjs";
|
|
7
|
+
import { safeJsonParse } from "@warlock.js/ai";
|
|
8
|
+
import { log } from "@warlock.js/logger";
|
|
9
|
+
|
|
10
|
+
//#region ../../@warlock.js/ai-anthropic/src/model.ts
|
|
11
|
+
const LOG_MODULE = "ai.anthropic";
|
|
12
|
+
/**
|
|
13
|
+
* Anthropic requires `max_tokens` on every request (unlike OpenAI,
|
|
14
|
+
* where it is optional). When neither the per-call option nor the
|
|
15
|
+
* model config supplies one, fall back to a generous default so a
|
|
16
|
+
* caller who never thought about token caps still gets a complete
|
|
17
|
+
* answer instead of a 400.
|
|
18
|
+
*/
|
|
19
|
+
const DEFAULT_MAX_TOKENS = 4096;
|
|
20
|
+
/**
|
|
21
|
+
* Anthropic-backed implementation of `ModelContract`.
|
|
22
|
+
*
|
|
23
|
+
* **Role.** The provider-facing bridge between the vendor-neutral
|
|
24
|
+
* `@warlock.js/ai` agent runtime and the official `@anthropic-ai/sdk`
|
|
25
|
+
* Messages API. Agents, workflows, and supervisors never talk to
|
|
26
|
+
* Anthropic directly — they hold a `ModelContract`, and this class is
|
|
27
|
+
* what makes that contract concrete for Claude models.
|
|
28
|
+
*
|
|
29
|
+
* **Responsibility.**
|
|
30
|
+
* - Owns: a long-lived `Anthropic` client + frozen `ModelConfig`
|
|
31
|
+
* (name, temperature, maxTokens) used as defaults for every call.
|
|
32
|
+
* - Owns: translating vendor-neutral `Message[]` / `ToolConfig[]` into
|
|
33
|
+
* Anthropic wire shapes (system hoisting, `tool_use` / `tool_result`
|
|
34
|
+
* blocks) on the way out, and translating Anthropic's content-block
|
|
35
|
+
* response (text, tool calls, stop reason, usage) back into the
|
|
36
|
+
* neutral shapes on the way in.
|
|
37
|
+
* - Does NOT own: dispatching tools, deciding whether to loop, tracking
|
|
38
|
+
* conversation history, or retrying on failure — those are agent
|
|
39
|
+
* concerns. The model is a stateless (per-call) protocol adapter.
|
|
40
|
+
*
|
|
41
|
+
* Because it holds a live client and shared defaults, it is modeled as
|
|
42
|
+
* a class (see §4.2 of code-style.md — "long-lived state across
|
|
43
|
+
* calls").
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* import Anthropic from "@anthropic-ai/sdk";
|
|
47
|
+
* const client = new Anthropic({ apiKey: process.env.ANTHROPIC_API_KEY });
|
|
48
|
+
* const model = new AnthropicModel(client, { name: "claude-sonnet-4-6" });
|
|
49
|
+
*
|
|
50
|
+
* const myAgent = agent({
|
|
51
|
+
* model,
|
|
52
|
+
* systemPrompt: "You are a helpful assistant.",
|
|
53
|
+
* tools: [searchTool],
|
|
54
|
+
* });
|
|
55
|
+
*
|
|
56
|
+
* const result = await myAgent.execute("Summarize today's news.");
|
|
57
|
+
*/
|
|
58
|
+
var AnthropicModel = class {
|
|
59
|
+
constructor(client, config, provider = "anthropic") {
|
|
60
|
+
this.logger = log;
|
|
61
|
+
this.client = client;
|
|
62
|
+
this.config = config;
|
|
63
|
+
this.name = config.name;
|
|
64
|
+
this.provider = provider;
|
|
65
|
+
this.pricing = config.pricing;
|
|
66
|
+
this.capabilities = {
|
|
67
|
+
structuredOutput: config.structuredOutput ?? true,
|
|
68
|
+
vision: config.vision ?? inferVisionCapability(config.name)
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Single-shot completion. Sends the full message list to the Messages
|
|
73
|
+
* endpoint, waits for the terminal response, and reshapes it into a
|
|
74
|
+
* vendor-neutral `ModelResponse`. Per-call `options` override the
|
|
75
|
+
* instance's `ModelConfig` defaults for this call only.
|
|
76
|
+
*/
|
|
77
|
+
async complete(messages, options) {
|
|
78
|
+
this.logger.debug(LOG_MODULE, "request", "Starting call to messages.create", {
|
|
79
|
+
model: this.name,
|
|
80
|
+
messageCount: messages.length,
|
|
81
|
+
streaming: false,
|
|
82
|
+
toolCount: options?.tools?.length ?? 0
|
|
83
|
+
});
|
|
84
|
+
let response;
|
|
85
|
+
try {
|
|
86
|
+
response = await this.client.messages.create({
|
|
87
|
+
...this.buildParams(messages, options),
|
|
88
|
+
stream: false
|
|
89
|
+
}, options?.signal ? { signal: options.signal } : void 0);
|
|
90
|
+
} catch (thrown) {
|
|
91
|
+
throw this.logAndWrap(thrown);
|
|
92
|
+
}
|
|
93
|
+
const finishReason = mapStopReason(response.stop_reason);
|
|
94
|
+
const usage = this.extractUsage(response.usage);
|
|
95
|
+
const toolCalls = this.extractToolCalls(response.content);
|
|
96
|
+
this.logger.debug(LOG_MODULE, "response", "call to messages.create succeeded", {
|
|
97
|
+
finishReason,
|
|
98
|
+
usage
|
|
99
|
+
});
|
|
100
|
+
return {
|
|
101
|
+
content: this.extractText(response.content),
|
|
102
|
+
finishReason,
|
|
103
|
+
usage,
|
|
104
|
+
toolCalls
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Incremental streaming completion. Yields neutral `ModelStreamChunk`s
|
|
109
|
+
* — `delta` for text tokens, `tool-call` once a `tool_use` block's
|
|
110
|
+
* arguments have fully accumulated, and a terminal `done` carrying the
|
|
111
|
+
* final finish reason + usage totals. Callers consume it with
|
|
112
|
+
* `for await`.
|
|
113
|
+
*/
|
|
114
|
+
async *stream(messages, options) {
|
|
115
|
+
this.logger.debug(LOG_MODULE, "request", "Starting streaming call to messages.create", {
|
|
116
|
+
model: this.name,
|
|
117
|
+
messageCount: messages.length,
|
|
118
|
+
streaming: true,
|
|
119
|
+
toolCount: options?.tools?.length ?? 0
|
|
120
|
+
});
|
|
121
|
+
let stream;
|
|
122
|
+
try {
|
|
123
|
+
stream = await this.client.messages.create({
|
|
124
|
+
...this.buildParams(messages, options),
|
|
125
|
+
stream: true
|
|
126
|
+
}, options?.signal ? { signal: options.signal } : void 0);
|
|
127
|
+
} catch (thrown) {
|
|
128
|
+
throw this.logAndWrap(thrown);
|
|
129
|
+
}
|
|
130
|
+
let rawStopReason = null;
|
|
131
|
+
const usage = {
|
|
132
|
+
input: 0,
|
|
133
|
+
output: 0,
|
|
134
|
+
total: 0
|
|
135
|
+
};
|
|
136
|
+
const toolBlocks = /* @__PURE__ */ new Map();
|
|
137
|
+
try {
|
|
138
|
+
for await (const event of stream) {
|
|
139
|
+
if (event.type === "message_start") {
|
|
140
|
+
usage.input = event.message.usage.input_tokens ?? 0;
|
|
141
|
+
const cached = event.message.usage.cache_read_input_tokens;
|
|
142
|
+
if (cached !== null && cached !== void 0 && cached > 0) usage.cachedTokens = cached;
|
|
143
|
+
continue;
|
|
144
|
+
}
|
|
145
|
+
if (event.type === "content_block_start") {
|
|
146
|
+
const block = event.content_block;
|
|
147
|
+
if (block.type === "tool_use") toolBlocks.set(event.index, {
|
|
148
|
+
id: block.id,
|
|
149
|
+
name: block.name,
|
|
150
|
+
json: ""
|
|
151
|
+
});
|
|
152
|
+
continue;
|
|
153
|
+
}
|
|
154
|
+
if (event.type === "content_block_delta") {
|
|
155
|
+
if (event.delta.type === "text_delta") yield {
|
|
156
|
+
type: "delta",
|
|
157
|
+
content: event.delta.text
|
|
158
|
+
};
|
|
159
|
+
else if (event.delta.type === "input_json_delta") {
|
|
160
|
+
const accumulator = toolBlocks.get(event.index);
|
|
161
|
+
if (accumulator) accumulator.json += event.delta.partial_json;
|
|
162
|
+
}
|
|
163
|
+
continue;
|
|
164
|
+
}
|
|
165
|
+
if (event.type === "content_block_stop") {
|
|
166
|
+
const accumulator = toolBlocks.get(event.index);
|
|
167
|
+
if (accumulator) {
|
|
168
|
+
yield {
|
|
169
|
+
type: "tool-call",
|
|
170
|
+
id: accumulator.id,
|
|
171
|
+
name: accumulator.name,
|
|
172
|
+
input: safeJsonParse(accumulator.json, {})
|
|
173
|
+
};
|
|
174
|
+
toolBlocks.delete(event.index);
|
|
175
|
+
}
|
|
176
|
+
continue;
|
|
177
|
+
}
|
|
178
|
+
if (event.type === "message_delta") {
|
|
179
|
+
rawStopReason = event.delta.stop_reason ?? rawStopReason;
|
|
180
|
+
usage.output = event.usage.output_tokens ?? usage.output;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
} catch (thrown) {
|
|
184
|
+
throw this.logAndWrap(thrown);
|
|
185
|
+
}
|
|
186
|
+
usage.total = usage.input + usage.output;
|
|
187
|
+
const finishReason = mapStopReason(rawStopReason);
|
|
188
|
+
this.logger.debug(LOG_MODULE, "response", "Streaming call to messages.create succeeded", {
|
|
189
|
+
finishReason,
|
|
190
|
+
usage
|
|
191
|
+
});
|
|
192
|
+
yield {
|
|
193
|
+
type: "done",
|
|
194
|
+
finishReason,
|
|
195
|
+
usage
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* Assemble the Anthropic request body shared by `complete()` and
|
|
200
|
+
* `stream()` (each adds its own `stream` literal so the SDK's create
|
|
201
|
+
* overload resolves to the right return type). Hoists the system
|
|
202
|
+
* prompt out of `messages`, resolves `max_tokens` (required by
|
|
203
|
+
* Anthropic) with the documented default, and conditionally attaches
|
|
204
|
+
* temperature, tools, and native structured output.
|
|
205
|
+
*/
|
|
206
|
+
buildParams(messages, options) {
|
|
207
|
+
const { system, messages: anthropicMessages } = toAnthropicMessages(messages);
|
|
208
|
+
const temperature = options?.temperature ?? this.config.temperature;
|
|
209
|
+
return {
|
|
210
|
+
model: this.name,
|
|
211
|
+
max_tokens: options?.maxTokens ?? this.config.maxTokens ?? DEFAULT_MAX_TOKENS,
|
|
212
|
+
messages: anthropicMessages,
|
|
213
|
+
...system ? { system } : {},
|
|
214
|
+
...temperature !== void 0 ? { temperature } : {},
|
|
215
|
+
...this.buildTools(options?.tools),
|
|
216
|
+
...this.buildStructuredOutput(options?.responseSchema)
|
|
217
|
+
};
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* Spread-friendly tools fragment. Returns an empty object when no
|
|
221
|
+
* tools were supplied so the caller can unconditionally spread it.
|
|
222
|
+
*/
|
|
223
|
+
buildTools(tools) {
|
|
224
|
+
const mapped = toAnthropicTools(tools);
|
|
225
|
+
return mapped ? { tools: mapped } : {};
|
|
226
|
+
}
|
|
227
|
+
/**
|
|
228
|
+
* Translate the neutral `responseSchema` option into Anthropic's
|
|
229
|
+
* native `output_config.format` (JSON-schema structured outputs).
|
|
230
|
+
*
|
|
231
|
+
* Only emitted when the model declares the `structuredOutput`
|
|
232
|
+
* capability AND the schema is a proper root-object JSON Schema —
|
|
233
|
+
* Anthropic rejects non-object roots. When the capability is off
|
|
234
|
+
* (config override) or the schema is non-object, returns an empty
|
|
235
|
+
* object: the agent has already injected a soft schema hint into the
|
|
236
|
+
* system prompt as the fallback, and client-side `validate()` still
|
|
237
|
+
* enforces shape.
|
|
238
|
+
*/
|
|
239
|
+
buildStructuredOutput(responseSchema) {
|
|
240
|
+
if (!responseSchema || !this.capabilities.structuredOutput) return {};
|
|
241
|
+
if (responseSchema.type !== "object" || typeof responseSchema.properties !== "object") return {};
|
|
242
|
+
return { output_config: { format: {
|
|
243
|
+
type: "json_schema",
|
|
244
|
+
schema: responseSchema
|
|
245
|
+
} } };
|
|
246
|
+
}
|
|
247
|
+
/**
|
|
248
|
+
* Concatenate every `text` content block into the single neutral
|
|
249
|
+
* `content` string. `tool_use` and other block types are ignored
|
|
250
|
+
* here — tool calls are surfaced separately via `extractToolCalls`.
|
|
251
|
+
*/
|
|
252
|
+
extractText(content) {
|
|
253
|
+
return content.filter((block) => block.type === "text").map((block) => block.text).join("");
|
|
254
|
+
}
|
|
255
|
+
/**
|
|
256
|
+
* Reshape Anthropic's `tool_use` content blocks into the neutral
|
|
257
|
+
* `ModelToolCallRequest[]`. Returns `undefined` when the model
|
|
258
|
+
* requested no tools so callers can branch on presence.
|
|
259
|
+
*/
|
|
260
|
+
extractToolCalls(content) {
|
|
261
|
+
const toolUses = content.filter((block) => block.type === "tool_use");
|
|
262
|
+
if (toolUses.length === 0) return;
|
|
263
|
+
return toolUses.map((block) => ({
|
|
264
|
+
id: block.id,
|
|
265
|
+
name: block.name,
|
|
266
|
+
input: block.input ?? {}
|
|
267
|
+
}));
|
|
268
|
+
}
|
|
269
|
+
/**
|
|
270
|
+
* Normalize Anthropic's `usage` block into the neutral `Usage` shape.
|
|
271
|
+
* Anthropic reports `input_tokens` / `output_tokens` separately with
|
|
272
|
+
* no pre-summed total, so `total` is computed. Cache-read tokens are
|
|
273
|
+
* surfaced as `cachedTokens` only when non-zero.
|
|
274
|
+
*/
|
|
275
|
+
extractUsage(raw) {
|
|
276
|
+
const input = raw.input_tokens ?? 0;
|
|
277
|
+
const output = raw.output_tokens ?? 0;
|
|
278
|
+
const cached = raw.cache_read_input_tokens;
|
|
279
|
+
return {
|
|
280
|
+
input,
|
|
281
|
+
output,
|
|
282
|
+
total: input + output,
|
|
283
|
+
...cached !== null && cached !== void 0 && cached > 0 ? { cachedTokens: cached } : {}
|
|
284
|
+
};
|
|
285
|
+
}
|
|
286
|
+
/**
|
|
287
|
+
* Wrap a thrown provider error into the typed `AIError` hierarchy and
|
|
288
|
+
* emit the standard error log line before it propagates. Shared by
|
|
289
|
+
* every catch site so the log shape stays identical.
|
|
290
|
+
*/
|
|
291
|
+
logAndWrap(thrown) {
|
|
292
|
+
const wrapped = wrapAnthropicError(thrown);
|
|
293
|
+
this.logger.error(LOG_MODULE, "error", wrapped.message, {
|
|
294
|
+
code: wrapped.code,
|
|
295
|
+
context: wrapped.context
|
|
296
|
+
});
|
|
297
|
+
return wrapped;
|
|
298
|
+
}
|
|
299
|
+
};
|
|
300
|
+
|
|
301
|
+
//#endregion
|
|
302
|
+
export { AnthropicModel };
|
|
303
|
+
//# sourceMappingURL=model.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"model.mjs","names":[],"sources":["../../../../../@warlock.js/ai-anthropic/src/model.ts"],"sourcesContent":["import {\n safeJsonParse,\n type Message,\n type ModelCallOptions,\n type ModelCapabilities,\n type ModelContract,\n type ModelPricing,\n type ModelResponse,\n type ModelStreamChunk,\n type ModelToolCallRequest,\n type Usage,\n} from \"@warlock.js/ai\";\nimport { log, type Logger } from \"@warlock.js/logger\";\nimport type Anthropic from \"@anthropic-ai/sdk\";\nimport type { AnthropicModelConfig } from \"./config.type\";\nimport { inferVisionCapability } from \"./known-vision-models\";\nimport { mapStopReason, toAnthropicMessages, toAnthropicTools, wrapAnthropicError } from \"./utils\";\n\nconst LOG_MODULE = \"ai.anthropic\";\n\n/**\n * Anthropic requires `max_tokens` on every request (unlike OpenAI,\n * where it is optional). When neither the per-call option nor the\n * model config supplies one, fall back to a generous default so a\n * caller who never thought about token caps still gets a complete\n * answer instead of a 400.\n */\nconst DEFAULT_MAX_TOKENS = 4096;\n\n/**\n * Anthropic-backed implementation of `ModelContract`.\n *\n * **Role.** The provider-facing bridge between the vendor-neutral\n * `@warlock.js/ai` agent runtime and the official `@anthropic-ai/sdk`\n * Messages API. Agents, workflows, and supervisors never talk to\n * Anthropic directly — they hold a `ModelContract`, and this class is\n * what makes that contract concrete for Claude models.\n *\n * **Responsibility.**\n * - Owns: a long-lived `Anthropic` client + frozen `ModelConfig`\n * (name, temperature, maxTokens) used as defaults for every call.\n * - Owns: translating vendor-neutral `Message[]` / `ToolConfig[]` into\n * Anthropic wire shapes (system hoisting, `tool_use` / `tool_result`\n * blocks) on the way out, and translating Anthropic's content-block\n * response (text, tool calls, stop reason, usage) back into the\n * neutral shapes on the way in.\n * - Does NOT own: dispatching tools, deciding whether to loop, tracking\n * conversation history, or retrying on failure — those are agent\n * concerns. The model is a stateless (per-call) protocol adapter.\n *\n * Because it holds a live client and shared defaults, it is modeled as\n * a class (see §4.2 of code-style.md — \"long-lived state across\n * calls\").\n *\n * @example\n * import Anthropic from \"@anthropic-ai/sdk\";\n * const client = new Anthropic({ apiKey: process.env.ANTHROPIC_API_KEY });\n * const model = new AnthropicModel(client, { name: \"claude-sonnet-4-6\" });\n *\n * const myAgent = agent({\n * model,\n * systemPrompt: \"You are a helpful assistant.\",\n * tools: [searchTool],\n * });\n *\n * const result = await myAgent.execute(\"Summarize today's news.\");\n */\nexport class AnthropicModel implements ModelContract {\n public readonly name: string;\n public readonly provider: string;\n public readonly capabilities: ModelCapabilities;\n public readonly pricing?: ModelPricing;\n\n private readonly client: Anthropic;\n private readonly config: AnthropicModelConfig;\n private readonly logger: Logger = log;\n\n public constructor(\n client: Anthropic,\n config: AnthropicModelConfig,\n provider: string = \"anthropic\",\n ) {\n this.client = client;\n this.config = config;\n this.name = config.name;\n this.provider = provider;\n this.pricing = config.pricing;\n this.capabilities = {\n structuredOutput: config.structuredOutput ?? true,\n vision: config.vision ?? inferVisionCapability(config.name),\n };\n }\n\n /**\n * Single-shot completion. Sends the full message list to the Messages\n * endpoint, waits for the terminal response, and reshapes it into a\n * vendor-neutral `ModelResponse`. Per-call `options` override the\n * instance's `ModelConfig` defaults for this call only.\n */\n public async complete(messages: Message[], options?: ModelCallOptions): Promise<ModelResponse> {\n this.logger.debug(LOG_MODULE, \"request\", \"Starting call to messages.create\", {\n model: this.name,\n messageCount: messages.length,\n streaming: false,\n toolCount: options?.tools?.length ?? 0,\n });\n\n let response: Anthropic.Message;\n\n try {\n response = await this.client.messages.create(\n { ...this.buildParams(messages, options), stream: false },\n options?.signal ? { signal: options.signal } : undefined,\n );\n } catch (thrown) {\n throw this.logAndWrap(thrown);\n }\n\n const finishReason = mapStopReason(response.stop_reason);\n const usage = this.extractUsage(response.usage);\n const toolCalls = this.extractToolCalls(response.content);\n\n this.logger.debug(LOG_MODULE, \"response\", \"call to messages.create succeeded\", {\n finishReason,\n usage,\n });\n\n return {\n content: this.extractText(response.content),\n finishReason,\n usage,\n toolCalls,\n };\n }\n\n /**\n * Incremental streaming completion. Yields neutral `ModelStreamChunk`s\n * — `delta` for text tokens, `tool-call` once a `tool_use` block's\n * arguments have fully accumulated, and a terminal `done` carrying the\n * final finish reason + usage totals. Callers consume it with\n * `for await`.\n */\n public async *stream(\n messages: Message[],\n options?: ModelCallOptions,\n ): AsyncIterable<ModelStreamChunk> {\n this.logger.debug(LOG_MODULE, \"request\", \"Starting streaming call to messages.create\", {\n model: this.name,\n messageCount: messages.length,\n streaming: true,\n toolCount: options?.tools?.length ?? 0,\n });\n\n let stream: Awaited<ReturnType<Anthropic[\"messages\"][\"create\"]>>;\n\n try {\n stream = await this.client.messages.create(\n { ...this.buildParams(messages, options), stream: true },\n options?.signal ? { signal: options.signal } : undefined,\n );\n } catch (thrown) {\n throw this.logAndWrap(thrown);\n }\n\n let rawStopReason: string | null = null;\n const usage: Usage = { input: 0, output: 0, total: 0 };\n const toolBlocks = new Map<number, { id: string; name: string; json: string }>();\n\n try {\n for await (const event of stream as AsyncIterable<Anthropic.RawMessageStreamEvent>) {\n if (event.type === \"message_start\") {\n usage.input = event.message.usage.input_tokens ?? 0;\n\n const cached = event.message.usage.cache_read_input_tokens;\n\n if (cached !== null && cached !== undefined && cached > 0) {\n usage.cachedTokens = cached;\n }\n\n continue;\n }\n\n if (event.type === \"content_block_start\") {\n const block = event.content_block;\n\n if (block.type === \"tool_use\") {\n toolBlocks.set(event.index, { id: block.id, name: block.name, json: \"\" });\n }\n\n continue;\n }\n\n if (event.type === \"content_block_delta\") {\n if (event.delta.type === \"text_delta\") {\n yield { type: \"delta\", content: event.delta.text };\n } else if (event.delta.type === \"input_json_delta\") {\n const accumulator = toolBlocks.get(event.index);\n\n if (accumulator) {\n accumulator.json += event.delta.partial_json;\n }\n }\n\n continue;\n }\n\n if (event.type === \"content_block_stop\") {\n const accumulator = toolBlocks.get(event.index);\n\n if (accumulator) {\n yield {\n type: \"tool-call\",\n id: accumulator.id,\n name: accumulator.name,\n input: safeJsonParse<Record<string, unknown>>(accumulator.json, {}),\n };\n\n toolBlocks.delete(event.index);\n }\n\n continue;\n }\n\n if (event.type === \"message_delta\") {\n rawStopReason = event.delta.stop_reason ?? rawStopReason;\n usage.output = event.usage.output_tokens ?? usage.output;\n }\n }\n } catch (thrown) {\n throw this.logAndWrap(thrown);\n }\n\n usage.total = usage.input + usage.output;\n\n const finishReason = mapStopReason(rawStopReason);\n\n this.logger.debug(LOG_MODULE, \"response\", \"Streaming call to messages.create succeeded\", {\n finishReason,\n usage,\n });\n\n yield { type: \"done\", finishReason, usage };\n }\n\n /**\n * Assemble the Anthropic request body shared by `complete()` and\n * `stream()` (each adds its own `stream` literal so the SDK's create\n * overload resolves to the right return type). Hoists the system\n * prompt out of `messages`, resolves `max_tokens` (required by\n * Anthropic) with the documented default, and conditionally attaches\n * temperature, tools, and native structured output.\n */\n private buildParams(\n messages: Message[],\n options: ModelCallOptions | undefined,\n ): Omit<Anthropic.MessageCreateParamsNonStreaming, \"stream\"> {\n const { system, messages: anthropicMessages } = toAnthropicMessages(messages);\n const temperature = options?.temperature ?? this.config.temperature;\n\n return {\n model: this.name,\n max_tokens: options?.maxTokens ?? this.config.maxTokens ?? DEFAULT_MAX_TOKENS,\n messages: anthropicMessages,\n ...(system ? { system } : {}),\n ...(temperature !== undefined ? { temperature } : {}),\n ...this.buildTools(options?.tools),\n ...this.buildStructuredOutput(options?.responseSchema),\n };\n }\n\n /**\n * Spread-friendly tools fragment. Returns an empty object when no\n * tools were supplied so the caller can unconditionally spread it.\n */\n private buildTools(tools: ModelCallOptions[\"tools\"]): { tools?: Anthropic.Tool[] } {\n const mapped = toAnthropicTools(tools);\n\n return mapped ? { tools: mapped } : {};\n }\n\n /**\n * Translate the neutral `responseSchema` option into Anthropic's\n * native `output_config.format` (JSON-schema structured outputs).\n *\n * Only emitted when the model declares the `structuredOutput`\n * capability AND the schema is a proper root-object JSON Schema —\n * Anthropic rejects non-object roots. When the capability is off\n * (config override) or the schema is non-object, returns an empty\n * object: the agent has already injected a soft schema hint into the\n * system prompt as the fallback, and client-side `validate()` still\n * enforces shape.\n */\n private buildStructuredOutput(responseSchema: Record<string, unknown> | undefined): {\n output_config?: Anthropic.OutputConfig;\n } {\n if (!responseSchema || !this.capabilities.structuredOutput) {\n return {};\n }\n\n if (responseSchema.type !== \"object\" || typeof responseSchema.properties !== \"object\") {\n return {};\n }\n\n return {\n output_config: {\n format: { type: \"json_schema\", schema: responseSchema },\n },\n };\n }\n\n /**\n * Concatenate every `text` content block into the single neutral\n * `content` string. `tool_use` and other block types are ignored\n * here — tool calls are surfaced separately via `extractToolCalls`.\n */\n private extractText(content: Anthropic.ContentBlock[]): string {\n return content\n .filter((block): block is Anthropic.TextBlock => block.type === \"text\")\n .map((block) => block.text)\n .join(\"\");\n }\n\n /**\n * Reshape Anthropic's `tool_use` content blocks into the neutral\n * `ModelToolCallRequest[]`. Returns `undefined` when the model\n * requested no tools so callers can branch on presence.\n */\n private extractToolCalls(\n content: Anthropic.ContentBlock[],\n ): ModelToolCallRequest[] | undefined {\n const toolUses = content.filter(\n (block): block is Anthropic.ToolUseBlock => block.type === \"tool_use\",\n );\n\n if (toolUses.length === 0) {\n return undefined;\n }\n\n return toolUses.map((block) => ({\n id: block.id,\n name: block.name,\n input: (block.input ?? {}) as Record<string, unknown>,\n }));\n }\n\n /**\n * Normalize Anthropic's `usage` block into the neutral `Usage` shape.\n * Anthropic reports `input_tokens` / `output_tokens` separately with\n * no pre-summed total, so `total` is computed. Cache-read tokens are\n * surfaced as `cachedTokens` only when non-zero.\n */\n private extractUsage(raw: Anthropic.Usage): Usage {\n const input = raw.input_tokens ?? 0;\n const output = raw.output_tokens ?? 0;\n const cached = raw.cache_read_input_tokens;\n\n return {\n input,\n output,\n total: input + output,\n ...(cached !== null && cached !== undefined && cached > 0 ? { cachedTokens: cached } : {}),\n };\n }\n\n /**\n * Wrap a thrown provider error into the typed `AIError` hierarchy and\n * emit the standard error log line before it propagates. Shared by\n * every catch site so the log shape stays identical.\n */\n private logAndWrap(thrown: unknown) {\n const wrapped = wrapAnthropicError(thrown);\n\n this.logger.error(LOG_MODULE, \"error\", wrapped.message, {\n code: wrapped.code,\n context: wrapped.context,\n });\n\n return wrapped;\n }\n}\n"],"mappings":";;;;;;;;;;AAkBA,MAAM,aAAa;;;;;;;;AASnB,MAAM,qBAAqB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAwC3B,IAAa,iBAAb,MAAqD;CAUnD,AAAO,YACL,QACA,QACA,WAAmB,aACnB;gBANgC;EAOhC,KAAK,SAAS;EACd,KAAK,SAAS;EACd,KAAK,OAAO,OAAO;EACnB,KAAK,WAAW;EAChB,KAAK,UAAU,OAAO;EACtB,KAAK,eAAe;GAClB,kBAAkB,OAAO,oBAAoB;GAC7C,QAAQ,OAAO,UAAU,sBAAsB,OAAO,IAAI;EAC5D;CACF;;;;;;;CAQA,MAAa,SAAS,UAAqB,SAAoD;EAC7F,KAAK,OAAO,MAAM,YAAY,WAAW,oCAAoC;GAC3E,OAAO,KAAK;GACZ,cAAc,SAAS;GACvB,WAAW;GACX,WAAW,SAAS,OAAO,UAAU;EACvC,CAAC;EAED,IAAI;EAEJ,IAAI;GACF,WAAW,MAAM,KAAK,OAAO,SAAS,OACpC;IAAE,GAAG,KAAK,YAAY,UAAU,OAAO;IAAG,QAAQ;GAAM,GACxD,SAAS,SAAS,EAAE,QAAQ,QAAQ,OAAO,IAAI,MACjD;EACF,SAAS,QAAQ;GACf,MAAM,KAAK,WAAW,MAAM;EAC9B;EAEA,MAAM,eAAe,cAAc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