@tanstack/ai-anthropic 0.13.1 → 0.15.0
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/dist/esm/adapters/text.d.ts +15 -1
- package/dist/esm/adapters/text.js +42 -12
- package/dist/esm/adapters/text.js.map +1 -1
- package/dist/esm/text/text-provider-options.d.ts +26 -26
- package/dist/esm/text/text-provider-options.js.map +1 -1
- package/dist/esm/tools/code-execution-tool.d.ts +27 -1
- package/dist/esm/tools/code-execution-tool.js +27 -5
- package/dist/esm/tools/code-execution-tool.js.map +1 -1
- package/dist/esm/tools/index.d.ts +1 -1
- package/dist/esm/tools/index.js +4 -1
- package/package.json +3 -3
- package/src/adapters/text.ts +99 -28
- package/src/text/text-provider-options.ts +26 -28
- package/src/tools/code-execution-tool.ts +64 -3
- package/src/tools/index.ts +5 -0
|
@@ -1,9 +1,23 @@
|
|
|
1
1
|
import { BaseTextAdapter, StructuredOutputOptions, StructuredOutputResult } from '@tanstack/ai/adapters';
|
|
2
2
|
import { ANTHROPIC_MODELS, AnthropicChatModelProviderOptionsByName, AnthropicChatModelToolCapabilitiesByName, AnthropicModelInputModalitiesByName } from '../model-meta.js';
|
|
3
|
-
import {
|
|
3
|
+
import { AnthropicBeta } from '@anthropic-ai/sdk/resources/beta/beta';
|
|
4
|
+
import { AnyTool, Modality, StreamChunk, TextOptions } from '@tanstack/ai';
|
|
4
5
|
import { AnthropicSystemPromptMetadata, ExternalTextProviderOptions } from '../text/text-provider-options.js';
|
|
5
6
|
import { AnthropicMessageMetadataByModality } from '../message-types.js';
|
|
6
7
|
import { AnthropicClientConfig } from '../utils.js';
|
|
8
|
+
/**
|
|
9
|
+
* Computes the `betas` array for a Messages request. Unions:
|
|
10
|
+
* - `interleaved-thinking-2025-05-14` when interleaved thinking is enabled,
|
|
11
|
+
* - `code-execution-2025-08-25` when a `code_execution` tool is present,
|
|
12
|
+
* - `skills-2025-10-02` when that tool carries skills.
|
|
13
|
+
* Returns `undefined` when none apply (so the call site omits `betas`).
|
|
14
|
+
*/
|
|
15
|
+
export declare function computeAnthropicBetas(tools: Array<AnyTool> | undefined, modelOptions: {
|
|
16
|
+
thinking?: {
|
|
17
|
+
type?: 'enabled' | 'disabled' | 'adaptive';
|
|
18
|
+
budget_tokens?: number;
|
|
19
|
+
};
|
|
20
|
+
} | undefined): Array<AnthropicBeta> | undefined;
|
|
7
21
|
/**
|
|
8
22
|
* Configuration for Anthropic text adapter
|
|
9
23
|
*/
|
|
@@ -2,10 +2,28 @@ import { EventType, normalizeSystemPrompts } from "@tanstack/ai";
|
|
|
2
2
|
import { toRunErrorRawEvent } from "@tanstack/ai/adapter-internals";
|
|
3
3
|
import { BaseTextAdapter } from "@tanstack/ai/adapters";
|
|
4
4
|
import { convertToolsToProviderFormat } from "../tools/tool-converter.js";
|
|
5
|
+
import { readCodeExecutionSkills, readCodeExecutionConfig } from "../tools/code-execution-tool.js";
|
|
5
6
|
import { validateTextProviderOptions } from "../text/text-provider-options.js";
|
|
6
7
|
import { buildAnthropicUsage } from "../usage.js";
|
|
7
8
|
import { createAnthropicClient, generateId, getAnthropicApiKeyFromEnv } from "../utils/client.js";
|
|
8
9
|
import { ANTHROPIC_COMBINED_TOOLS_AND_SCHEMA_MODELS } from "../model-meta.js";
|
|
10
|
+
function computeAnthropicBetas(tools, modelOptions) {
|
|
11
|
+
const betas = /* @__PURE__ */ new Set();
|
|
12
|
+
const useInterleavedThinking = modelOptions?.thinking?.type === "enabled" && typeof modelOptions.thinking.budget_tokens === "number" && modelOptions.thinking.budget_tokens > 0;
|
|
13
|
+
if (useInterleavedThinking) betas.add("interleaved-thinking-2025-05-14");
|
|
14
|
+
const codeExecTool = tools?.find((t) => t.name === "code_execution");
|
|
15
|
+
if (codeExecTool) {
|
|
16
|
+
const cfgType = readCodeExecutionConfig(codeExecTool)?.type;
|
|
17
|
+
betas.add(
|
|
18
|
+
cfgType === "code_execution_20250522" ? "code-execution-2025-05-22" : "code-execution-2025-08-25"
|
|
19
|
+
);
|
|
20
|
+
}
|
|
21
|
+
const hasSkills = tools?.some(
|
|
22
|
+
(t) => t.name === "code_execution" && (readCodeExecutionSkills(t)?.length ?? 0) > 0
|
|
23
|
+
);
|
|
24
|
+
if (hasSkills) betas.add("skills-2025-10-02");
|
|
25
|
+
return betas.size > 0 ? Array.from(betas) : void 0;
|
|
26
|
+
}
|
|
9
27
|
class AnthropicTextAdapter extends BaseTextAdapter {
|
|
10
28
|
kind = "text";
|
|
11
29
|
name = "anthropic";
|
|
@@ -22,9 +40,7 @@ class AnthropicTextAdapter extends BaseTextAdapter {
|
|
|
22
40
|
`activity=chat provider=anthropic model=${this.model} messages=${options.messages.length} tools=${options.tools?.length ?? 0} stream=true`,
|
|
23
41
|
{ provider: "anthropic", model: this.model }
|
|
24
42
|
);
|
|
25
|
-
const
|
|
26
|
-
const useInterleavedThinking = modelOptions?.thinking?.type === "enabled" && typeof modelOptions.thinking.budget_tokens === "number" && modelOptions.thinking.budget_tokens > 0;
|
|
27
|
-
const betas = useInterleavedThinking ? ["interleaved-thinking-2025-05-14"] : void 0;
|
|
43
|
+
const betas = computeAnthropicBetas(options.tools, options.modelOptions);
|
|
28
44
|
const stream = await this.client.beta.messages.create(
|
|
29
45
|
{
|
|
30
46
|
...requestParams,
|
|
@@ -89,12 +105,17 @@ class AnthropicTextAdapter extends BaseTextAdapter {
|
|
|
89
105
|
`activity=chat provider=anthropic model=${this.model} messages=${chatOptions.messages.length} tools=${chatOptions.tools?.length ?? 0} stream=false`,
|
|
90
106
|
{ provider: "anthropic", model: this.model }
|
|
91
107
|
);
|
|
108
|
+
const betas = computeAnthropicBetas(
|
|
109
|
+
chatOptions.tools,
|
|
110
|
+
chatOptions.modelOptions
|
|
111
|
+
);
|
|
92
112
|
const response = await this.client.beta.messages.create(
|
|
93
113
|
{
|
|
94
114
|
...requestParams,
|
|
95
115
|
stream: false,
|
|
96
116
|
tools: [structuredOutputTool],
|
|
97
|
-
tool_choice: { type: "tool", name: "structured_output" }
|
|
117
|
+
tool_choice: { type: "tool", name: "structured_output" },
|
|
118
|
+
...betas && { betas }
|
|
98
119
|
},
|
|
99
120
|
{
|
|
100
121
|
signal: chatOptions.request?.signal,
|
|
@@ -157,11 +178,13 @@ class AnthropicTextAdapter extends BaseTextAdapter {
|
|
|
157
178
|
"stop_sequences",
|
|
158
179
|
"thinking",
|
|
159
180
|
"tool_choice",
|
|
160
|
-
"top_k"
|
|
181
|
+
"top_k",
|
|
182
|
+
"temperature",
|
|
183
|
+
"top_p"
|
|
161
184
|
];
|
|
162
|
-
const
|
|
185
|
+
const droppedKeyExemptSet = /* @__PURE__ */ new Set([...validKeys, "max_tokens"]);
|
|
163
186
|
const droppedKeys = Object.keys(modelOptions).filter(
|
|
164
|
-
(key) => !
|
|
187
|
+
(key) => !droppedKeyExemptSet.has(key)
|
|
165
188
|
);
|
|
166
189
|
if (droppedKeys.length > 0) {
|
|
167
190
|
options.logger.errors(
|
|
@@ -187,7 +210,7 @@ class AnthropicTextAdapter extends BaseTextAdapter {
|
|
|
187
210
|
}
|
|
188
211
|
}
|
|
189
212
|
const thinkingBudget = validProviderOptions.thinking?.type === "enabled" ? validProviderOptions.thinking.budget_tokens : void 0;
|
|
190
|
-
const defaultMaxTokens =
|
|
213
|
+
const defaultMaxTokens = modelOptions?.max_tokens ?? 1024;
|
|
191
214
|
const maxTokens = thinkingBudget && thinkingBudget >= defaultMaxTokens ? thinkingBudget + 1 : defaultMaxTokens;
|
|
192
215
|
const systemBlocks = (() => {
|
|
193
216
|
const normalized = normalizeSystemPrompts(
|
|
@@ -214,13 +237,19 @@ class AnthropicTextAdapter extends BaseTextAdapter {
|
|
|
214
237
|
}
|
|
215
238
|
}
|
|
216
239
|
} : void 0;
|
|
240
|
+
const toolSkills = options.tools?.map(
|
|
241
|
+
(tool) => tool.name === "code_execution" ? readCodeExecutionSkills(tool) : void 0
|
|
242
|
+
).find((skills) => skills && skills.length > 0);
|
|
243
|
+
if (toolSkills && toolSkills.length > 0) {
|
|
244
|
+
const existingContainer = validProviderOptions.container ?? void 0;
|
|
245
|
+
validProviderOptions.container = {
|
|
246
|
+
id: existingContainer?.id ?? null,
|
|
247
|
+
skills: toolSkills
|
|
248
|
+
};
|
|
249
|
+
}
|
|
217
250
|
const requestParams = {
|
|
218
251
|
model: options.model,
|
|
219
252
|
max_tokens: maxTokens,
|
|
220
|
-
...options.temperature !== void 0 && {
|
|
221
|
-
temperature: options.temperature
|
|
222
|
-
},
|
|
223
|
-
...options.topP !== void 0 && { top_p: options.topP },
|
|
224
253
|
messages: formattedMessages,
|
|
225
254
|
...systemBlocks !== void 0 && { system: systemBlocks },
|
|
226
255
|
...tools !== void 0 && { tools },
|
|
@@ -814,6 +843,7 @@ function anthropicText(model, config) {
|
|
|
814
843
|
export {
|
|
815
844
|
AnthropicTextAdapter,
|
|
816
845
|
anthropicText,
|
|
846
|
+
computeAnthropicBetas,
|
|
817
847
|
createAnthropicChat
|
|
818
848
|
};
|
|
819
849
|
//# sourceMappingURL=text.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"text.js","sources":["../../../src/adapters/text.ts"],"sourcesContent":["import { EventType, normalizeSystemPrompts } from '@tanstack/ai'\nimport { toRunErrorRawEvent } from '@tanstack/ai/adapter-internals'\nimport { BaseTextAdapter } from '@tanstack/ai/adapters'\nimport { convertToolsToProviderFormat } from '../tools/tool-converter'\nimport { validateTextProviderOptions } from '../text/text-provider-options'\nimport { buildAnthropicUsage } from '../usage'\nimport {\n createAnthropicClient,\n generateId,\n getAnthropicApiKeyFromEnv,\n} from '../utils'\nimport { ANTHROPIC_COMBINED_TOOLS_AND_SCHEMA_MODELS } from '../model-meta'\nimport type {\n ANTHROPIC_MODELS,\n AnthropicChatModelProviderOptionsByName,\n AnthropicChatModelToolCapabilitiesByName,\n AnthropicModelInputModalitiesByName,\n} from '../model-meta'\nimport type {\n StructuredOutputOptions,\n StructuredOutputResult,\n} from '@tanstack/ai/adapters'\nimport type { InternalLogger } from '@tanstack/ai/adapter-internals'\nimport type {\n Base64ImageSource,\n Base64PDFSource,\n ContentBlockParam,\n DocumentBlockParam,\n ImageBlockParam,\n TextBlockParam,\n ThinkingBlockParam,\n ToolUseBlockParam,\n URLImageSource,\n URLPDFSource,\n} from '@anthropic-ai/sdk/resources/messages'\nimport type Anthropic_SDK from '@anthropic-ai/sdk'\nimport type { AnthropicBeta } from '@anthropic-ai/sdk/resources/beta/beta'\nimport type {\n ContentPart,\n Modality,\n ModelMessage,\n StreamChunk,\n TextOptions,\n} from '@tanstack/ai'\nimport type {\n AnthropicSystemPromptMetadata,\n ExternalTextProviderOptions,\n InternalTextProviderOptions,\n} from '../text/text-provider-options'\nimport type {\n AnthropicDocumentMetadata,\n AnthropicImageMetadata,\n AnthropicMessageMetadataByModality,\n AnthropicTextMetadata,\n} from '../message-types'\nimport type { AnthropicClientConfig } from '../utils'\n\n/**\n * Configuration for Anthropic text adapter\n */\nexport interface AnthropicTextConfig extends AnthropicClientConfig {}\n\n/**\n * Anthropic-specific provider options for text/chat\n */\nexport type AnthropicTextProviderOptions = ExternalTextProviderOptions\n\n// ===========================\n// Type Resolution Helpers\n// ===========================\n\n/**\n * Resolve provider options for a specific model.\n * If the model has explicit options in the map, use those; otherwise use base options.\n */\ntype ResolveProviderOptions<TModel extends string> =\n TModel extends keyof AnthropicChatModelProviderOptionsByName\n ? AnthropicChatModelProviderOptionsByName[TModel]\n : AnthropicTextProviderOptions\n\n/**\n * Resolve input modalities for a specific model.\n * If the model has explicit modalities in the map, use those; otherwise use default.\n */\ntype ResolveInputModalities<TModel extends string> =\n TModel extends keyof AnthropicModelInputModalitiesByName\n ? AnthropicModelInputModalitiesByName[TModel]\n : readonly ['text', 'image', 'document']\n\ntype ResolveToolCapabilities<TModel extends string> =\n TModel extends keyof AnthropicChatModelToolCapabilitiesByName\n ? NonNullable<AnthropicChatModelToolCapabilitiesByName[TModel]>\n : readonly []\n\n// ===========================\n// Adapter Implementation\n// ===========================\n\n/**\n * Anthropic Text (Chat) Adapter\n *\n * Tree-shakeable adapter for Anthropic chat/text completion functionality.\n * Import only what you need for smaller bundle sizes.\n */\nexport class AnthropicTextAdapter<\n TModel extends (typeof ANTHROPIC_MODELS)[number],\n TProviderOptions extends Record<string, any> = ResolveProviderOptions<TModel>,\n TInputModalities extends ReadonlyArray<Modality> =\n ResolveInputModalities<TModel>,\n TToolCapabilities extends ReadonlyArray<string> =\n ResolveToolCapabilities<TModel>,\n> extends BaseTextAdapter<\n TModel,\n TProviderOptions,\n TInputModalities,\n AnthropicMessageMetadataByModality,\n TToolCapabilities,\n // TToolCallMetadata — anthropic has no tool-call metadata round-tripping\n unknown,\n // TSystemPromptMetadata — narrows `systemPrompts[i].metadata` at the\n // chat() call site so users get `cache_control` autocomplete.\n AnthropicSystemPromptMetadata\n> {\n override readonly kind = 'text' as const\n readonly name = 'anthropic' as const\n\n private readonly client: Anthropic_SDK\n\n constructor(config: AnthropicTextConfig, model: TModel) {\n super({}, model)\n this.client = createAnthropicClient(config)\n }\n\n async *chatStream(\n options: TextOptions<TProviderOptions>,\n ): AsyncIterable<StreamChunk> {\n const { logger } = options\n try {\n const requestParams = this.mapCommonOptionsToAnthropic(options)\n\n logger.request(\n `activity=chat provider=anthropic model=${this.model} messages=${options.messages.length} tools=${options.tools?.length ?? 0} stream=true`,\n { provider: 'anthropic', model: this.model },\n )\n\n // `betas` is attached at the call site rather than in the shared mapper\n // because the `interleaved-thinking-2025-05-14` header is only useful for\n // the streaming path.\n const modelOptions = options.modelOptions as\n | InternalTextProviderOptions\n | undefined\n const useInterleavedThinking =\n modelOptions?.thinking?.type === 'enabled' &&\n typeof modelOptions.thinking.budget_tokens === 'number' &&\n modelOptions.thinking.budget_tokens > 0\n const betas: Array<AnthropicBeta> | undefined = useInterleavedThinking\n ? ['interleaved-thinking-2025-05-14']\n : undefined\n\n // `client.beta.messages` is Anthropic's permanent staging surface, not a\n // sunset path: it's a superset of `client.messages` that additionally\n // accepts the `betas: AnthropicBeta[]` header (e.g. interleaved\n // thinking) plus richer `container` (skills) and `context_management`\n // shapes that `InternalTextProviderOptions` carries. We route every\n // Messages call through it so the request mapper stays single-shape.\n const stream = await this.client.beta.messages.create(\n {\n ...requestParams,\n stream: true,\n ...(betas && { betas }),\n },\n {\n signal: options.request?.signal,\n headers: options.request?.headers,\n },\n )\n\n yield* this.processAnthropicStream(\n stream,\n options,\n () => generateId(this.name),\n logger,\n )\n } catch (error: unknown) {\n const err = error as Error & { status?: number; code?: string }\n const rawEvent = toRunErrorRawEvent(error)\n logger.errors('anthropic.chatStream fatal', {\n error,\n source: 'anthropic.chatStream',\n })\n yield {\n type: EventType.RUN_ERROR,\n model: options.model,\n timestamp: Date.now(),\n message: err.message || 'Unknown error occurred',\n code: err.code || String(err.status),\n // Forward the Anthropic SDK error's `.error` response body (e.g.\n // `{ type, message }`) when present; never the raw exception object.\n ...(rawEvent !== undefined && { rawEvent }),\n error: {\n message: err.message || 'Unknown error occurred',\n code: err.code || String(err.status),\n },\n }\n }\n }\n\n /**\n * Generate structured output using Anthropic's tool-based approach.\n * Anthropic doesn't have native structured output, so we use a tool with the schema\n * and force the model to call it.\n * The outputSchema is already JSON Schema (converted in the ai layer).\n */\n async structuredOutput(\n options: StructuredOutputOptions<TProviderOptions>,\n ): Promise<StructuredOutputResult<unknown>> {\n const { chatOptions, outputSchema } = options\n const { logger } = chatOptions\n\n const requestParams = this.mapCommonOptionsToAnthropic(chatOptions)\n\n // Create a tool that will capture the structured output\n // Anthropic's SDK requires input_schema with type: 'object' literal\n const structuredOutputTool = {\n name: 'structured_output',\n description:\n 'Use this tool to provide your response in the required structured format.',\n input_schema: {\n type: 'object' as const,\n properties: outputSchema.properties ?? {},\n required: outputSchema.required ?? [],\n },\n }\n\n try {\n logger.request(\n `activity=chat provider=anthropic model=${this.model} messages=${chatOptions.messages.length} tools=${chatOptions.tools?.length ?? 0} stream=false`,\n { provider: 'anthropic', model: this.model },\n )\n // Make non-streaming request with tool_choice forced to our structured output tool\n const response = await this.client.beta.messages.create(\n {\n ...requestParams,\n stream: false,\n tools: [structuredOutputTool],\n tool_choice: { type: 'tool', name: 'structured_output' },\n },\n {\n signal: chatOptions.request?.signal,\n headers: chatOptions.request?.headers,\n },\n )\n\n // Extract the tool use content from the response\n let parsed: unknown = null\n let rawText = ''\n\n for (const block of response.content) {\n if (block.type === 'tool_use' && block.name === 'structured_output') {\n parsed = block.input\n rawText = JSON.stringify(block.input)\n break\n }\n }\n\n if (parsed === null) {\n // Fallback: try to extract text content and parse as JSON\n rawText = response.content\n .map((b) => {\n if (b.type === 'text') {\n return b.text\n }\n return ''\n })\n .join('')\n try {\n parsed = JSON.parse(rawText)\n } catch {\n throw new Error(\n `Failed to extract structured output from response. Content: ${rawText.slice(0, 200)}${rawText.length > 200 ? '...' : ''}`,\n )\n }\n }\n\n return {\n data: parsed,\n rawText,\n usage: buildAnthropicUsage(response.usage),\n }\n } catch (error: unknown) {\n const err = error as Error\n logger.errors('anthropic.structuredOutput fatal', {\n error,\n source: 'anthropic.structuredOutput',\n })\n throw new Error(\n `Structured output generation failed: ${err.message || 'Unknown error occurred'}`,\n )\n }\n }\n\n private mapCommonOptionsToAnthropic(\n options: TextOptions<AnthropicTextProviderOptions>,\n ) {\n const modelOptions = options.modelOptions as\n | InternalTextProviderOptions\n | undefined\n\n const formattedMessages = this.formatMessages(options.messages)\n const tools = options.tools\n ? convertToolsToProviderFormat(options.tools)\n : undefined\n\n const validProviderOptions: Partial<InternalTextProviderOptions> = {}\n if (modelOptions) {\n const validKeys: Array<keyof InternalTextProviderOptions> = [\n 'container',\n 'context_management',\n 'effort',\n 'mcp_servers',\n 'output_config',\n 'service_tier',\n 'stop_sequences',\n 'thinking',\n 'tool_choice',\n 'top_k',\n ]\n const validKeySet = new Set<string>(validKeys)\n const droppedKeys = Object.keys(modelOptions).filter(\n (key) => !validKeySet.has(key),\n )\n if (droppedKeys.length > 0) {\n // Reachable when callers cast around the public type (e.g.\n // `modelOptions: { system: ... } as any`). Without this warning the\n // unknown keys are silently dropped — `system` in particular was a\n // previously-tested path for attaching `cache_control` and we don't\n // want that to fail in production with no signal.\n options.logger.errors(\n `anthropic.mapCommonOptionsToAnthropic dropped unknown modelOptions key(s): ${droppedKeys.join(', ')}`,\n {\n source: 'anthropic.mapCommonOptionsToAnthropic',\n droppedKeys,\n hint: droppedKeys.includes('system')\n ? 'pass system prompts via the top-level `systemPrompts` option; `modelOptions.system` is no longer honored'\n : undefined,\n },\n )\n }\n for (const key of validKeys) {\n if (key in modelOptions) {\n const value = modelOptions[key]\n if (key === 'tool_choice' && typeof value === 'string') {\n ;(validProviderOptions as Record<string, unknown>)[key] = {\n type: value,\n }\n } else {\n ;(validProviderOptions as Record<string, unknown>)[key] = value\n }\n }\n }\n }\n\n const thinkingBudget =\n validProviderOptions.thinking?.type === 'enabled'\n ? validProviderOptions.thinking.budget_tokens\n : undefined\n const defaultMaxTokens = options.maxTokens || 1024\n const maxTokens =\n thinkingBudget && thinkingBudget >= defaultMaxTokens\n ? thinkingBudget + 1\n : defaultMaxTokens\n\n // `InternalTextProviderOptions.system` is typed\n // `string | Array<TextBlockParam>` (no `| undefined`), so build it\n // outside the literal and spread it conditionally rather than\n // assigning `undefined` under exactOptionalPropertyTypes.\n const systemBlocks = ((): Array<TextBlockParam> | undefined => {\n const normalized = normalizeSystemPrompts<AnthropicSystemPromptMetadata>(\n options.systemPrompts,\n )\n if (normalized.length === 0) return undefined\n return normalized.map(\n (p): TextBlockParam => ({\n type: 'text',\n text: p.content,\n ...(p.metadata?.cache_control && {\n cache_control: p.metadata.cache_control,\n }),\n }),\n )\n })()\n // Wire engine-threaded outputSchema into Messages `output_config.format`\n // alongside any `tools` so the model emits tool calls during the agent\n // loop and a single schema-constrained JSON message on its final turn.\n // Merge into any existing `output_config` so callers can keep tuning\n // `output_config.effort` alongside the schema.\n const combinedSchema = options.outputSchema as\n | Record<string, unknown>\n | undefined\n const outputConfig = combinedSchema\n ? {\n output_config: {\n ...(validProviderOptions.output_config ?? {}),\n format: {\n type: 'json_schema' as const,\n schema: combinedSchema,\n },\n },\n }\n : undefined\n\n // `InternalTextProviderOptions` declares `temperature`, `top_p`,\n // and `tools` as `T?: ...` (no `| undefined`), so spread them\n // conditionally rather than passing explicit `undefined` from the\n // optional common `TextOptions` fields under\n // exactOptionalPropertyTypes.\n const requestParams: InternalTextProviderOptions = {\n model: options.model,\n max_tokens: maxTokens,\n ...(options.temperature !== undefined && {\n temperature: options.temperature,\n }),\n ...(options.topP !== undefined && { top_p: options.topP }),\n messages: formattedMessages,\n ...(systemBlocks !== undefined && { system: systemBlocks }),\n ...(tools !== undefined && { tools }),\n ...validProviderOptions,\n ...(outputConfig ?? {}),\n }\n validateTextProviderOptions(requestParams)\n return requestParams\n }\n\n /**\n * Anthropic supports `output_config.format` + `tools` in a single streaming\n * Messages request only for Claude 4.5+ (GA 2026-01-29). For 4.4 and\n * earlier we keep the forced-tool-use workaround in\n * {@link structuredOutput} via the engine's finalization path.\n */\n supportsCombinedToolsAndSchema(): boolean {\n return ANTHROPIC_COMBINED_TOOLS_AND_SCHEMA_MODELS.has(this.model)\n }\n\n private convertContentPartToAnthropic(\n part: ContentPart,\n ): TextBlockParam | ImageBlockParam | DocumentBlockParam {\n switch (part.type) {\n case 'text': {\n const metadata = part.metadata as AnthropicTextMetadata | undefined\n return {\n type: 'text',\n text: part.content,\n ...metadata,\n }\n }\n\n case 'image': {\n const metadata = part.metadata as AnthropicImageMetadata | undefined\n const imageSource: Base64ImageSource | URLImageSource =\n part.source.type === 'data'\n ? {\n type: 'base64',\n data: part.source.value,\n media_type: part.source.mimeType as\n | 'image/jpeg'\n | 'image/png'\n | 'image/gif'\n | 'image/webp',\n }\n : {\n type: 'url',\n url: part.source.value,\n }\n return {\n type: 'image',\n source: imageSource,\n ...metadata,\n }\n }\n case 'document': {\n const metadata = part.metadata as AnthropicDocumentMetadata | undefined\n const docSource: Base64PDFSource | URLPDFSource =\n part.source.type === 'data'\n ? {\n type: 'base64',\n data: part.source.value,\n media_type: part.source.mimeType as 'application/pdf',\n }\n : {\n type: 'url',\n url: part.source.value,\n }\n return {\n type: 'document',\n source: docSource,\n ...metadata,\n }\n }\n case 'audio':\n case 'video':\n throw new Error(\n `Anthropic does not support ${part.type} content directly`,\n )\n default: {\n const _exhaustiveCheck: never = part\n throw new Error(\n `Unsupported content part type: ${(_exhaustiveCheck as ContentPart).type}`,\n )\n }\n }\n }\n\n private formatMessages(\n messages: Array<ModelMessage>,\n ): InternalTextProviderOptions['messages'] {\n const formattedMessages: InternalTextProviderOptions['messages'] = []\n\n for (const message of messages) {\n const role = message.role\n\n if (role === 'tool' && message.toolCallId) {\n const toolContent = message.content\n formattedMessages.push({\n role: 'user',\n content: [\n {\n type: 'tool_result',\n tool_use_id: message.toolCallId,\n content: Array.isArray(toolContent)\n ? toolContent.map((part) =>\n this.convertContentPartToAnthropic(part),\n )\n : typeof toolContent === 'string'\n ? toolContent\n : '',\n },\n ],\n })\n continue\n }\n\n if (role === 'assistant' && message.toolCalls?.length) {\n const contentBlocks: Array<ContentBlockParam> = []\n\n this.appendThinkingBlocks(contentBlocks, message.thinking)\n\n if (message.content) {\n const content =\n typeof message.content === 'string' ? message.content : ''\n const textBlock: TextBlockParam = {\n type: 'text',\n text: content,\n }\n contentBlocks.push(textBlock)\n }\n\n for (const toolCall of message.toolCalls) {\n let parsedInput: unknown = {}\n try {\n const parsed = toolCall.function.arguments\n ? JSON.parse(toolCall.function.arguments)\n : {}\n parsedInput = parsed && typeof parsed === 'object' ? parsed : {}\n } catch {\n parsedInput = toolCall.function.arguments\n }\n\n const toolUseBlock: ToolUseBlockParam = {\n type: 'tool_use',\n id: toolCall.id,\n name: toolCall.function.name,\n input: parsedInput,\n }\n contentBlocks.push(toolUseBlock)\n }\n\n formattedMessages.push({\n role: 'assistant',\n content: contentBlocks,\n })\n\n continue\n }\n\n if (role === 'assistant') {\n const contentBlocks: Array<ContentBlockParam> = []\n this.appendThinkingBlocks(contentBlocks, message.thinking)\n\n if (Array.isArray(message.content)) {\n for (const part of message.content) {\n contentBlocks.push(this.convertContentPartToAnthropic(part))\n }\n } else if (message.content) {\n contentBlocks.push({\n type: 'text',\n text: message.content,\n })\n }\n\n formattedMessages.push({\n role: 'assistant',\n content: contentBlocks.length > 0 ? contentBlocks : '',\n })\n continue\n }\n\n if (role === 'user' && Array.isArray(message.content)) {\n const contentBlocks = message.content.map((part) =>\n this.convertContentPartToAnthropic(part),\n )\n formattedMessages.push({\n role: 'user',\n content: contentBlocks,\n })\n continue\n }\n\n formattedMessages.push({\n role: 'user',\n content:\n typeof message.content === 'string'\n ? message.content\n : message.content\n ? message.content.map((c) =>\n this.convertContentPartToAnthropic(c),\n )\n : '',\n })\n }\n\n // Post-process: Anthropic requires strictly alternating user/assistant roles.\n // Tool results are sent as role:'user' messages, which can create consecutive\n // user messages when followed by a new user message. Merge them.\n return this.mergeConsecutiveSameRoleMessages(formattedMessages)\n }\n\n private appendThinkingBlocks(\n contentBlocks: Array<ContentBlockParam>,\n thinkingParts: ModelMessage['thinking'],\n ): void {\n if (!thinkingParts?.length) return\n\n for (const thinking of thinkingParts) {\n if (!thinking.signature) continue\n const block: ThinkingBlockParam = {\n type: 'thinking',\n thinking: thinking.content,\n signature: thinking.signature,\n }\n contentBlocks.push(block)\n }\n }\n\n /**\n * Merge consecutive messages of the same role into a single message.\n * Anthropic's API requires strictly alternating user/assistant roles.\n * Tool results are wrapped as role:'user' messages, which can collide\n * with actual user messages in multi-turn conversations.\n *\n * Also filters out empty assistant messages (e.g., from a previous failed request).\n */\n private mergeConsecutiveSameRoleMessages(\n messages: InternalTextProviderOptions['messages'],\n ): InternalTextProviderOptions['messages'] {\n const merged: InternalTextProviderOptions['messages'] = []\n\n for (const msg of messages) {\n // Skip empty assistant messages (no content or empty string)\n if (msg.role === 'assistant') {\n const hasContent = Array.isArray(msg.content)\n ? msg.content.length > 0\n : typeof msg.content === 'string' && msg.content.length > 0\n if (!hasContent) {\n continue\n }\n }\n\n const prev = merged[merged.length - 1]\n if (prev && prev.role === msg.role) {\n // Normalize both contents to arrays and concatenate\n const prevBlocks = Array.isArray(prev.content)\n ? prev.content\n : typeof prev.content === 'string' && prev.content\n ? [{ type: 'text' as const, text: prev.content }]\n : []\n const msgBlocks = Array.isArray(msg.content)\n ? msg.content\n : typeof msg.content === 'string' && msg.content\n ? [{ type: 'text' as const, text: msg.content }]\n : []\n prev.content = [...prevBlocks, ...msgBlocks]\n } else {\n merged.push({ ...msg })\n }\n }\n\n // De-duplicate tool_result blocks with the same tool_use_id.\n // This can happen when the core layer generates tool results from both\n // the tool-result part and the tool-call part's output field.\n for (const msg of merged) {\n if (Array.isArray(msg.content)) {\n const seenToolResultIds = new Set<string>()\n msg.content = msg.content.filter((block: any) => {\n if (block.type === 'tool_result' && block.tool_use_id) {\n if (seenToolResultIds.has(block.tool_use_id)) {\n return false // Remove duplicate\n }\n seenToolResultIds.add(block.tool_use_id)\n }\n return true\n })\n }\n }\n\n return merged\n }\n\n private async *processAnthropicStream(\n stream: AsyncIterable<Anthropic_SDK.Beta.BetaRawMessageStreamEvent>,\n options: TextOptions<AnthropicTextProviderOptions>,\n genId: () => string,\n logger: InternalLogger,\n ): AsyncIterable<StreamChunk> {\n const model = options.model\n let accumulatedContent = ''\n let accumulatedThinking = ''\n let accumulatedSignature = ''\n const toolCallsMap = new Map<\n number,\n { id: string; name: string; input: string; started: boolean }\n >()\n let currentToolIndex = -1\n // Server-side tools share the `input_json_delta` wire format with client\n // `tool_use` blocks; routing both to the same buffer corrupts client tool\n // input.\n let currentServerTool: { id: string; name: string; input: string } | null =\n null\n\n // AG-UI lifecycle tracking\n const runId = options.runId ?? genId()\n const threadId = options.threadId ?? genId()\n const messageId = genId()\n let stepId: string | null = null\n let reasoningMessageId: string | null = null\n let hasClosedReasoning = false\n let hasEmittedRunStarted = false\n let hasEmittedTextMessageStart = false\n let hasEmittedRunFinished = false\n // Track current content block type for proper content_block_stop handling\n let currentBlockType: string | null = null\n\n try {\n for await (const event of stream) {\n logger.provider(`provider=anthropic type=${event.type}`, {\n chunk: event,\n })\n // Emit RUN_STARTED on first event\n if (!hasEmittedRunStarted) {\n hasEmittedRunStarted = true\n yield {\n type: EventType.RUN_STARTED,\n runId,\n threadId,\n model,\n timestamp: Date.now(),\n parentRunId: options.parentRunId,\n }\n }\n\n if (event.type === 'content_block_start') {\n currentBlockType = event.content_block.type\n if (event.content_block.type === 'tool_use') {\n currentToolIndex++\n toolCallsMap.set(currentToolIndex, {\n id: event.content_block.id,\n name: event.content_block.name,\n input: '',\n started: false,\n })\n } else if (event.content_block.type === 'server_tool_use') {\n currentServerTool = {\n id: event.content_block.id,\n name: event.content_block.name,\n input: '',\n }\n } else if (\n event.content_block.type === 'web_fetch_tool_result' ||\n event.content_block.type === 'web_search_tool_result'\n ) {\n // The result content arrives in full at content_block_start (no\n // deltas). Surface error variants so a failed fetch/search isn't\n // invisible to the consumer.\n const content = event.content_block.content as\n | { type?: string; error_code?: string }\n | Array<unknown>\n const errorBlock =\n !Array.isArray(content) &&\n (content.type === 'web_fetch_tool_result_error' ||\n content.type === 'web_search_tool_result_error')\n ? content\n : null\n if (errorBlock) {\n logger.errors(\n `anthropic.${event.content_block.type} error_code=${errorBlock.error_code}`,\n {\n toolUseId: event.content_block.tool_use_id,\n blockType: event.content_block.type,\n errorCode: errorBlock.error_code,\n source: 'anthropic.processAnthropicStream',\n },\n )\n }\n } else if (event.content_block.type === 'thinking') {\n accumulatedThinking = ''\n accumulatedSignature = ''\n // Emit REASONING and STEP_STARTED for thinking\n stepId = genId()\n reasoningMessageId = genId()\n\n // Spec REASONING events\n yield {\n type: EventType.REASONING_START,\n messageId: reasoningMessageId,\n model,\n timestamp: Date.now(),\n }\n yield {\n type: EventType.REASONING_MESSAGE_START,\n messageId: reasoningMessageId,\n role: 'reasoning' as const,\n model,\n timestamp: Date.now(),\n }\n\n // Legacy STEP events (kept during transition)\n yield {\n type: EventType.STEP_STARTED,\n stepName: stepId,\n stepId,\n model,\n timestamp: Date.now(),\n stepType: 'thinking',\n }\n }\n } else if (event.type === 'content_block_delta') {\n if (event.delta.type === 'text_delta') {\n // Close reasoning before text starts\n if (reasoningMessageId && !hasClosedReasoning) {\n hasClosedReasoning = true\n yield {\n type: EventType.REASONING_MESSAGE_END,\n messageId: reasoningMessageId,\n model,\n timestamp: Date.now(),\n }\n yield {\n type: EventType.REASONING_END,\n messageId: reasoningMessageId,\n model,\n timestamp: Date.now(),\n }\n }\n\n // Emit TEXT_MESSAGE_START on first text content\n if (!hasEmittedTextMessageStart) {\n hasEmittedTextMessageStart = true\n yield {\n type: EventType.TEXT_MESSAGE_START,\n messageId,\n model,\n timestamp: Date.now(),\n role: 'assistant',\n }\n }\n\n const delta = event.delta.text\n accumulatedContent += delta\n yield {\n type: EventType.TEXT_MESSAGE_CONTENT,\n messageId,\n model,\n timestamp: Date.now(),\n delta,\n content: accumulatedContent,\n }\n } else if (\n event.delta.type === 'thinking_delta' &&\n reasoningMessageId\n ) {\n const delta = event.delta.thinking\n accumulatedThinking += delta\n\n // Spec REASONING content event\n yield {\n type: EventType.REASONING_MESSAGE_CONTENT,\n messageId: reasoningMessageId,\n delta,\n model,\n timestamp: Date.now(),\n }\n\n // Legacy STEP event\n yield {\n type: EventType.STEP_FINISHED,\n stepName: stepId || genId(),\n stepId: stepId || genId(),\n model,\n timestamp: Date.now(),\n delta,\n content: accumulatedThinking,\n }\n } else if (\n (event.delta as { type: string }).type === 'signature_delta'\n ) {\n accumulatedSignature +=\n (event.delta as { signature: string }).signature || ''\n } else if (event.delta.type === 'input_json_delta') {\n // Route deltas by current block type so server_tool_use input\n // never appends onto the prior client tool's buffer.\n if (currentBlockType === 'tool_use') {\n const existing = toolCallsMap.get(currentToolIndex)\n if (existing) {\n // Emit TOOL_CALL_START on first args delta\n if (!existing.started) {\n existing.started = true\n yield {\n type: EventType.TOOL_CALL_START,\n toolCallId: existing.id,\n toolCallName: existing.name,\n toolName: existing.name,\n model,\n timestamp: Date.now(),\n index: currentToolIndex,\n }\n }\n\n existing.input += event.delta.partial_json\n\n yield {\n type: EventType.TOOL_CALL_ARGS,\n toolCallId: existing.id,\n model,\n timestamp: Date.now(),\n delta: event.delta.partial_json,\n args: existing.input,\n }\n }\n } else if (\n currentBlockType === 'server_tool_use' &&\n currentServerTool\n ) {\n // Accumulate server tool input internally. We don't emit\n // TOOL_CALL_* events: the call is executed by Anthropic, not\n // by our agent loop, so surfacing it as a client tool call\n // would cause downstream code to try (and fail) to run it.\n currentServerTool.input += event.delta.partial_json\n }\n }\n } else if (event.type === 'content_block_stop') {\n if (currentBlockType === 'thinking') {\n // Emit signature so it can be replayed in multi-turn context\n if (accumulatedSignature && stepId) {\n yield {\n type: EventType.STEP_FINISHED,\n stepName: stepId,\n stepId,\n model,\n timestamp: Date.now(),\n delta: '',\n content: accumulatedThinking,\n signature: accumulatedSignature,\n }\n }\n } else if (currentBlockType === 'tool_use') {\n const existing = toolCallsMap.get(currentToolIndex)\n if (existing) {\n // If tool call wasn't started yet (no args), start it now\n if (!existing.started) {\n existing.started = true\n yield {\n type: EventType.TOOL_CALL_START,\n toolCallId: existing.id,\n toolCallName: existing.name,\n toolName: existing.name,\n model,\n timestamp: Date.now(),\n index: currentToolIndex,\n }\n }\n\n // Emit TOOL_CALL_END\n let parsedInput: unknown = {}\n try {\n const parsed = existing.input ? JSON.parse(existing.input) : {}\n parsedInput = parsed && typeof parsed === 'object' ? parsed : {}\n } catch {\n parsedInput = {}\n }\n\n yield {\n type: EventType.TOOL_CALL_END,\n toolCallId: existing.id,\n toolCallName: existing.name,\n toolName: existing.name,\n model,\n timestamp: Date.now(),\n input: parsedInput,\n }\n\n // Reset so a new TEXT_MESSAGE_START is emitted if text follows tool calls\n hasEmittedTextMessageStart = false\n }\n } else if (currentBlockType === 'server_tool_use') {\n if (currentServerTool) {\n // Anthropic executes the call; we only need a breadcrumb so\n // consumers (devtools, telemetry) can see what ran.\n logger.provider(\n `provider=anthropic server_tool_use name=${currentServerTool.name}`,\n {\n toolUseId: currentServerTool.id,\n name: currentServerTool.name,\n input: currentServerTool.input,\n },\n )\n }\n currentServerTool = null\n } else if (\n currentBlockType === 'web_fetch_tool_result' ||\n currentBlockType === 'web_search_tool_result'\n ) {\n // The model already consumed the result; error variants were\n // already surfaced at content_block_start.\n } else {\n // Emit TEXT_MESSAGE_END only for text blocks (not tool_use blocks)\n if (hasEmittedTextMessageStart && accumulatedContent) {\n yield {\n type: EventType.TEXT_MESSAGE_END,\n messageId,\n model,\n timestamp: Date.now(),\n }\n }\n }\n currentBlockType = null\n } else if (event.type === 'message_stop') {\n // Close reasoning events if still open\n if (reasoningMessageId && !hasClosedReasoning) {\n hasClosedReasoning = true\n yield {\n type: EventType.REASONING_MESSAGE_END,\n messageId: reasoningMessageId,\n model,\n timestamp: Date.now(),\n }\n yield {\n type: EventType.REASONING_END,\n messageId: reasoningMessageId,\n model,\n timestamp: Date.now(),\n }\n }\n\n // Only emit RUN_FINISHED from message_stop if message_delta didn't already emit one.\n // message_delta carries the real stop_reason (tool_use, end_turn, etc.),\n // while message_stop is just a completion signal.\n if (!hasEmittedRunFinished) {\n yield {\n type: EventType.RUN_FINISHED,\n runId,\n threadId,\n model,\n timestamp: Date.now(),\n finishReason: 'stop',\n }\n }\n } else if (event.type === 'message_delta') {\n if (event.delta.stop_reason) {\n hasEmittedRunFinished = true\n\n // Close reasoning events if still open\n if (reasoningMessageId && !hasClosedReasoning) {\n hasClosedReasoning = true\n yield {\n type: EventType.REASONING_MESSAGE_END,\n messageId: reasoningMessageId,\n model,\n timestamp: Date.now(),\n }\n yield {\n type: EventType.REASONING_END,\n messageId: reasoningMessageId,\n model,\n timestamp: Date.now(),\n }\n }\n\n switch (event.delta.stop_reason) {\n case 'tool_use': {\n yield {\n type: EventType.RUN_FINISHED,\n runId,\n threadId,\n model,\n timestamp: Date.now(),\n finishReason: 'tool_calls',\n usage: buildAnthropicUsage(event.usage),\n }\n break\n }\n case 'max_tokens': {\n yield {\n type: EventType.RUN_ERROR,\n model,\n timestamp: Date.now(),\n message:\n 'The response was cut off because the maximum token limit was reached.',\n code: 'max_tokens',\n error: {\n message:\n 'The response was cut off because the maximum token limit was reached.',\n code: 'max_tokens',\n },\n }\n break\n }\n case 'stop_sequence':\n case 'end_turn':\n case 'pause_turn':\n case 'refusal':\n case 'model_context_window_exceeded':\n case 'compaction':\n default: {\n // All remaining Anthropic stop_reason variants map to the\n // generic \"stop\" finish reason — they describe *why* the\n // stream ended, but for AG-UI consumers the resulting event\n // shape is identical.\n yield {\n type: EventType.RUN_FINISHED,\n runId,\n threadId,\n model,\n timestamp: Date.now(),\n finishReason: 'stop',\n usage: buildAnthropicUsage(event.usage),\n }\n }\n }\n }\n }\n }\n } catch (error: unknown) {\n const err = error as Error & { status?: number; code?: string }\n const rawEvent = toRunErrorRawEvent(error)\n\n logger.errors('anthropic.processAnthropicStream fatal', {\n error,\n source: 'anthropic.processAnthropicStream',\n })\n yield {\n type: EventType.RUN_ERROR,\n model,\n timestamp: Date.now(),\n message: err.message || 'Unknown error occurred',\n code: err.code || String(err.status),\n // Forward the Anthropic SDK error's `.error` response body when present.\n ...(rawEvent !== undefined && { rawEvent }),\n error: {\n message: err.message || 'Unknown error occurred',\n code: err.code || String(err.status),\n },\n }\n }\n }\n}\n\n/**\n * Creates an Anthropic chat adapter with explicit API key.\n * Type resolution happens here at the call site.\n */\nexport function createAnthropicChat<\n TModel extends (typeof ANTHROPIC_MODELS)[number],\n>(\n model: TModel,\n apiKey: string,\n config?: Omit<AnthropicTextConfig, 'apiKey'>,\n): AnthropicTextAdapter<\n TModel,\n ResolveProviderOptions<TModel>,\n ResolveInputModalities<TModel>\n> {\n return new AnthropicTextAdapter({ apiKey, ...config }, model)\n}\n\n/**\n * Creates an Anthropic text adapter with automatic API key detection.\n * Type resolution happens here at the call site.\n */\nexport function anthropicText<TModel extends (typeof ANTHROPIC_MODELS)[number]>(\n model: TModel,\n config?: Omit<AnthropicTextConfig, 'apiKey'>,\n): AnthropicTextAdapter<\n TModel,\n ResolveProviderOptions<TModel>,\n ResolveInputModalities<TModel>\n> {\n const apiKey = getAnthropicApiKeyFromEnv()\n return createAnthropicChat(model, apiKey, config)\n}\n"],"names":[],"mappings":";;;;;;;;AAwGO,MAAM,6BAOH,gBAWR;AAAA,EACkB,OAAO;AAAA,EAChB,OAAO;AAAA,EAEC;AAAA,EAEjB,YAAY,QAA6B,OAAe;AACtD,UAAM,CAAA,GAAI,KAAK;AACf,SAAK,SAAS,sBAAsB,MAAM;AAAA,EAC5C;AAAA,EAEA,OAAO,WACL,SAC4B;AAC5B,UAAM,EAAE,WAAW;AACnB,QAAI;AACF,YAAM,gBAAgB,KAAK,4BAA4B,OAAO;AAE9D,aAAO;AAAA,QACL,0CAA0C,KAAK,KAAK,aAAa,QAAQ,SAAS,MAAM,UAAU,QAAQ,OAAO,UAAU,CAAC;AAAA,QAC5H,EAAE,UAAU,aAAa,OAAO,KAAK,MAAA;AAAA,MAAM;AAM7C,YAAM,eAAe,QAAQ;AAG7B,YAAM,yBACJ,cAAc,UAAU,SAAS,aACjC,OAAO,aAAa,SAAS,kBAAkB,YAC/C,aAAa,SAAS,gBAAgB;AACxC,YAAM,QAA0C,yBAC5C,CAAC,iCAAiC,IAClC;AAQJ,YAAM,SAAS,MAAM,KAAK,OAAO,KAAK,SAAS;AAAA,QAC7C;AAAA,UACE,GAAG;AAAA,UACH,QAAQ;AAAA,UACR,GAAI,SAAS,EAAE,MAAA;AAAA,QAAM;AAAA,QAEvB;AAAA,UACE,QAAQ,QAAQ,SAAS;AAAA,UACzB,SAAS,QAAQ,SAAS;AAAA,QAAA;AAAA,MAC5B;AAGF,aAAO,KAAK;AAAA,QACV;AAAA,QACA;AAAA,QACA,MAAM,WAAW,KAAK,IAAI;AAAA,QAC1B;AAAA,MAAA;AAAA,IAEJ,SAAS,OAAgB;AACvB,YAAM,MAAM;AACZ,YAAM,WAAW,mBAAmB,KAAK;AACzC,aAAO,OAAO,8BAA8B;AA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AA,cACL,KAAK;AAAA,cACL,KAAK;AAAA,cACL,KAAK;AAAA,cACL,KAAK;AAAA,cACL,KAAK;AAAA,cACL,SAAS;AAKP,sBAAM;AAAA,kBACJ,MAAM,UAAU;AAAA,kBAChB;AAAA,kBACA;AAAA,kBACA;AAAA,kBACA,WAAW,KAAK,IAAA;AAAA,kBAChB,cAAc;AAAA,kBACd,OAAO,oBAAoB,MAAM,KAAK;AAAA,gBAAA;AAAA,cAE1C;AAAA,YAAA;AAAA,UAEJ;AAAA,QACF;AAAA,MACF;AAAA,IACF,SAAS,OAAgB;AACvB,YAAM,MAAM;AACZ,YAAM,WAAW,mBAAmB,KAAK;AAEzC,aAAO,OAAO,0CAA0C;AAAA,QACtD;AAAA,QACA,QAAQ;AAAA,MAAA,CACT;AACD,YAAM;AAAA,QACJ,MAAM,UAAU;AAAA,QAChB;AAAA,QACA,WAAW,KAAK,IAAA;AAAA,QAChB,SAAS,IAAI,WAAW;AAAA,QACxB,MAAM,IAAI,QAAQ,OAAO,IAAI,MAAM;AAAA;AAAA,QAEnC,GAAI,aAAa,UAAa,EAAE,SAAA;AAAA,QAChC,OAAO;AAAA,UACL,SAAS,IAAI,WAAW;AAAA,UACxB,MAAM,IAAI,QAAQ,OAAO,IAAI,MAAM;AAAA,QAAA;AAAA,MACrC;AAAA,IAEJ;AAAA,EACF;AACF;AAMO,SAAS,oBAGd,OACA,QACA,QAKA;AACA,SAAO,IAAI,qBAAqB,EAAE,QAAQ,GAAG,OAAA,GAAU,KAAK;AAC9D;AAMO,SAAS,cACd,OACA,QAKA;AACA,QAAM,SAAS,0BAAA;AACf,SAAO,oBAAoB,OAAO,QAAQ,MAAM;AAClD;"}
|
|
1
|
+
{"version":3,"file":"text.js","sources":["../../../src/adapters/text.ts"],"sourcesContent":["import { EventType, normalizeSystemPrompts } from '@tanstack/ai'\nimport { toRunErrorRawEvent } from '@tanstack/ai/adapter-internals'\nimport { BaseTextAdapter } from '@tanstack/ai/adapters'\nimport { convertToolsToProviderFormat } from '../tools/tool-converter'\nimport { readCodeExecutionConfig, readCodeExecutionSkills } from '../tools'\nimport { validateTextProviderOptions } from '../text/text-provider-options'\nimport { buildAnthropicUsage } from '../usage'\nimport {\n createAnthropicClient,\n generateId,\n getAnthropicApiKeyFromEnv,\n} from '../utils'\nimport { ANTHROPIC_COMBINED_TOOLS_AND_SCHEMA_MODELS } from '../model-meta'\nimport type {\n ANTHROPIC_MODELS,\n AnthropicChatModelProviderOptionsByName,\n AnthropicChatModelToolCapabilitiesByName,\n AnthropicModelInputModalitiesByName,\n} from '../model-meta'\nimport type {\n StructuredOutputOptions,\n StructuredOutputResult,\n} from '@tanstack/ai/adapters'\nimport type { InternalLogger } from '@tanstack/ai/adapter-internals'\nimport type {\n Base64ImageSource,\n Base64PDFSource,\n ContentBlockParam,\n DocumentBlockParam,\n ImageBlockParam,\n TextBlockParam,\n ThinkingBlockParam,\n ToolUseBlockParam,\n URLImageSource,\n URLPDFSource,\n} from '@anthropic-ai/sdk/resources/messages'\nimport type Anthropic_SDK from '@anthropic-ai/sdk'\nimport type { AnthropicBeta } from '@anthropic-ai/sdk/resources/beta/beta'\nimport type {\n AnyTool,\n ContentPart,\n Modality,\n ModelMessage,\n StreamChunk,\n TextOptions,\n} from '@tanstack/ai'\nimport type {\n AnthropicSystemPromptMetadata,\n ExternalTextProviderOptions,\n InternalTextProviderOptions,\n} from '../text/text-provider-options'\nimport type {\n AnthropicDocumentMetadata,\n AnthropicImageMetadata,\n AnthropicMessageMetadataByModality,\n AnthropicTextMetadata,\n} from '../message-types'\nimport type { AnthropicClientConfig } from '../utils'\n\n/**\n * Computes the `betas` array for a Messages request. Unions:\n * - `interleaved-thinking-2025-05-14` when interleaved thinking is enabled,\n * - `code-execution-2025-08-25` when a `code_execution` tool is present,\n * - `skills-2025-10-02` when that tool carries skills.\n * Returns `undefined` when none apply (so the call site omits `betas`).\n */\nexport function computeAnthropicBetas(\n tools: Array<AnyTool> | undefined,\n modelOptions:\n | {\n thinking?: {\n type?: 'enabled' | 'disabled' | 'adaptive'\n budget_tokens?: number\n }\n }\n | undefined,\n): Array<AnthropicBeta> | undefined {\n const betas = new Set<AnthropicBeta>()\n\n const useInterleavedThinking =\n modelOptions?.thinking?.type === 'enabled' &&\n typeof modelOptions.thinking.budget_tokens === 'number' &&\n modelOptions.thinking.budget_tokens > 0\n if (useInterleavedThinking) betas.add('interleaved-thinking-2025-05-14')\n\n // Code-execution beta is version-aware: select from the FIRST code_execution\n // tool's config type.\n const codeExecTool = tools?.find((t) => t.name === 'code_execution')\n if (codeExecTool) {\n const cfgType = readCodeExecutionConfig(codeExecTool)?.type\n // Each code_execution tool version pairs with a specific beta. Known\n // legacy variant maps explicitly; current/future variants (e.g.\n // `code_execution_20250825` and later) use the latest `-08-25` beta.\n betas.add(\n cfgType === 'code_execution_20250522'\n ? 'code-execution-2025-05-22'\n : 'code-execution-2025-08-25',\n )\n }\n\n // Skills beta: scan ALL code_execution tools so this AGREES with the\n // container-lift, which lifts skills from any code_execution tool that\n // carries them (not just the first).\n const hasSkills = tools?.some(\n (t) =>\n t.name === 'code_execution' &&\n (readCodeExecutionSkills(t)?.length ?? 0) > 0,\n )\n if (hasSkills) betas.add('skills-2025-10-02')\n\n return betas.size > 0 ? Array.from(betas) : undefined\n}\n\n/**\n * Configuration for Anthropic text adapter\n */\nexport interface AnthropicTextConfig extends AnthropicClientConfig {}\n\n/**\n * Anthropic-specific provider options for text/chat\n */\nexport type AnthropicTextProviderOptions = ExternalTextProviderOptions\n\n// ===========================\n// Type Resolution Helpers\n// ===========================\n\n/**\n * Resolve provider options for a specific model.\n * If the model has explicit options in the map, use those; otherwise use base options.\n */\ntype ResolveProviderOptions<TModel extends string> =\n TModel extends keyof AnthropicChatModelProviderOptionsByName\n ? AnthropicChatModelProviderOptionsByName[TModel]\n : AnthropicTextProviderOptions\n\n/**\n * Resolve input modalities for a specific model.\n * If the model has explicit modalities in the map, use those; otherwise use default.\n */\ntype ResolveInputModalities<TModel extends string> =\n TModel extends keyof AnthropicModelInputModalitiesByName\n ? AnthropicModelInputModalitiesByName[TModel]\n : readonly ['text', 'image', 'document']\n\ntype ResolveToolCapabilities<TModel extends string> =\n TModel extends keyof AnthropicChatModelToolCapabilitiesByName\n ? NonNullable<AnthropicChatModelToolCapabilitiesByName[TModel]>\n : readonly []\n\n// ===========================\n// Adapter Implementation\n// ===========================\n\n/**\n * Anthropic Text (Chat) Adapter\n *\n * Tree-shakeable adapter for Anthropic chat/text completion functionality.\n * Import only what you need for smaller bundle sizes.\n */\nexport class AnthropicTextAdapter<\n TModel extends (typeof ANTHROPIC_MODELS)[number],\n TProviderOptions extends Record<string, any> = ResolveProviderOptions<TModel>,\n TInputModalities extends ReadonlyArray<Modality> =\n ResolveInputModalities<TModel>,\n TToolCapabilities extends ReadonlyArray<string> =\n ResolveToolCapabilities<TModel>,\n> extends BaseTextAdapter<\n TModel,\n TProviderOptions,\n TInputModalities,\n AnthropicMessageMetadataByModality,\n TToolCapabilities,\n // TToolCallMetadata — anthropic has no tool-call metadata round-tripping\n unknown,\n // TSystemPromptMetadata — narrows `systemPrompts[i].metadata` at the\n // chat() call site so users get `cache_control` autocomplete.\n AnthropicSystemPromptMetadata\n> {\n override readonly kind = 'text' as const\n readonly name = 'anthropic' as const\n\n private readonly client: Anthropic_SDK\n\n constructor(config: AnthropicTextConfig, model: TModel) {\n super({}, model)\n this.client = createAnthropicClient(config)\n }\n\n async *chatStream(\n options: TextOptions<TProviderOptions>,\n ): AsyncIterable<StreamChunk> {\n const { logger } = options\n try {\n const requestParams = this.mapCommonOptionsToAnthropic(options)\n\n logger.request(\n `activity=chat provider=anthropic model=${this.model} messages=${options.messages.length} tools=${options.tools?.length ?? 0} stream=true`,\n { provider: 'anthropic', model: this.model },\n )\n\n // `betas` is attached at the call site rather than in the shared mapper\n // because the beta set depends on both the tools and the modelOptions.\n const betas = computeAnthropicBetas(options.tools, options.modelOptions)\n\n // `client.beta.messages` is Anthropic's permanent staging surface, not a\n // sunset path: it's a superset of `client.messages` that additionally\n // accepts the `betas: AnthropicBeta[]` header (e.g. interleaved\n // thinking) plus richer `container` (skills) and `context_management`\n // shapes that `InternalTextProviderOptions` carries. We route every\n // Messages call through it so the request mapper stays single-shape.\n const stream = await this.client.beta.messages.create(\n {\n ...requestParams,\n stream: true,\n ...(betas && { betas }),\n },\n {\n signal: options.request?.signal,\n headers: options.request?.headers,\n },\n )\n\n yield* this.processAnthropicStream(\n stream,\n options,\n () => generateId(this.name),\n logger,\n )\n } catch (error: unknown) {\n const err = error as Error & { status?: number; code?: string }\n const rawEvent = toRunErrorRawEvent(error)\n logger.errors('anthropic.chatStream fatal', {\n error,\n source: 'anthropic.chatStream',\n })\n yield {\n type: EventType.RUN_ERROR,\n model: options.model,\n timestamp: Date.now(),\n message: err.message || 'Unknown error occurred',\n code: err.code || String(err.status),\n // Forward the Anthropic SDK error's `.error` response body (e.g.\n // `{ type, message }`) when present; never the raw exception object.\n ...(rawEvent !== undefined && { rawEvent }),\n error: {\n message: err.message || 'Unknown error occurred',\n code: err.code || String(err.status),\n },\n }\n }\n }\n\n /**\n * Generate structured output using Anthropic's tool-based approach.\n * Anthropic doesn't have native structured output, so we use a tool with the schema\n * and force the model to call it.\n * The outputSchema is already JSON Schema (converted in the ai layer).\n */\n async structuredOutput(\n options: StructuredOutputOptions<TProviderOptions>,\n ): Promise<StructuredOutputResult<unknown>> {\n const { chatOptions, outputSchema } = options\n const { logger } = chatOptions\n\n const requestParams = this.mapCommonOptionsToAnthropic(chatOptions)\n\n // Create a tool that will capture the structured output\n // Anthropic's SDK requires input_schema with type: 'object' literal\n const structuredOutputTool = {\n name: 'structured_output',\n description:\n 'Use this tool to provide your response in the required structured format.',\n input_schema: {\n type: 'object' as const,\n properties: outputSchema.properties ?? {},\n required: outputSchema.required ?? [],\n },\n }\n\n try {\n logger.request(\n `activity=chat provider=anthropic model=${this.model} messages=${chatOptions.messages.length} tools=${chatOptions.tools?.length ?? 0} stream=false`,\n { provider: 'anthropic', model: this.model },\n )\n const betas = computeAnthropicBetas(\n chatOptions.tools,\n chatOptions.modelOptions,\n )\n // Make non-streaming request with tool_choice forced to our structured output tool\n const response = await this.client.beta.messages.create(\n {\n ...requestParams,\n stream: false,\n tools: [structuredOutputTool],\n tool_choice: { type: 'tool', name: 'structured_output' },\n ...(betas && { betas }),\n },\n {\n signal: chatOptions.request?.signal,\n headers: chatOptions.request?.headers,\n },\n )\n\n // Extract the tool use content from the response\n let parsed: unknown = null\n let rawText = ''\n\n for (const block of response.content) {\n if (block.type === 'tool_use' && block.name === 'structured_output') {\n parsed = block.input\n rawText = JSON.stringify(block.input)\n break\n }\n }\n\n if (parsed === null) {\n // Fallback: try to extract text content and parse as JSON\n rawText = response.content\n .map((b) => {\n if (b.type === 'text') {\n return b.text\n }\n return ''\n })\n .join('')\n try {\n parsed = JSON.parse(rawText)\n } catch {\n throw new Error(\n `Failed to extract structured output from response. Content: ${rawText.slice(0, 200)}${rawText.length > 200 ? '...' : ''}`,\n )\n }\n }\n\n return {\n data: parsed,\n rawText,\n usage: buildAnthropicUsage(response.usage),\n }\n } catch (error: unknown) {\n const err = error as Error\n logger.errors('anthropic.structuredOutput fatal', {\n error,\n source: 'anthropic.structuredOutput',\n })\n throw new Error(\n `Structured output generation failed: ${err.message || 'Unknown error occurred'}`,\n )\n }\n }\n\n private mapCommonOptionsToAnthropic(\n options: TextOptions<AnthropicTextProviderOptions>,\n ) {\n const modelOptions = options.modelOptions\n\n const formattedMessages = this.formatMessages(options.messages)\n const tools = options.tools\n ? convertToolsToProviderFormat(options.tools)\n : undefined\n\n const validProviderOptions: Partial<InternalTextProviderOptions> = {}\n if (modelOptions) {\n const validKeys: Array<keyof AnthropicTextProviderOptions> = [\n 'container',\n 'context_management',\n 'effort',\n 'mcp_servers',\n 'output_config',\n 'service_tier',\n 'stop_sequences',\n 'thinking',\n 'tool_choice',\n 'top_k',\n 'temperature',\n 'top_p',\n ]\n // `max_tokens` is a legitimate public modelOptions field, but it is read\n // via a dedicated path (defaultMaxTokens below) rather than copied into\n // validProviderOptions. Exempt it from the dropped-key warning here so a\n // correct `modelOptions: { max_tokens }` call doesn't log a spurious\n // \"dropped unknown key\" error, while keeping it out of the copy loop.\n const droppedKeyExemptSet = new Set<string>([...validKeys, 'max_tokens'])\n const droppedKeys = Object.keys(modelOptions).filter(\n (key) => !droppedKeyExemptSet.has(key),\n )\n if (droppedKeys.length > 0) {\n // Reachable when callers cast around the public type (e.g.\n // `modelOptions: { system: ... } as any`). Without this warning the\n // unknown keys are silently dropped — `system` in particular was a\n // previously-tested path for attaching `cache_control` and we don't\n // want that to fail in production with no signal.\n options.logger.errors(\n `anthropic.mapCommonOptionsToAnthropic dropped unknown modelOptions key(s): ${droppedKeys.join(', ')}`,\n {\n source: 'anthropic.mapCommonOptionsToAnthropic',\n droppedKeys,\n hint: droppedKeys.includes('system')\n ? 'pass system prompts via the top-level `systemPrompts` option; `modelOptions.system` is no longer honored'\n : undefined,\n },\n )\n }\n for (const key of validKeys) {\n if (key in modelOptions) {\n const value = modelOptions[key]\n if (key === 'tool_choice' && typeof value === 'string') {\n ;(validProviderOptions as Record<string, unknown>)[key] = {\n type: value,\n }\n } else {\n ;(validProviderOptions as Record<string, unknown>)[key] = value\n }\n }\n }\n }\n\n const thinkingBudget =\n validProviderOptions.thinking?.type === 'enabled'\n ? validProviderOptions.thinking.budget_tokens\n : undefined\n const defaultMaxTokens = modelOptions?.max_tokens ?? 1024\n const maxTokens =\n thinkingBudget && thinkingBudget >= defaultMaxTokens\n ? thinkingBudget + 1\n : defaultMaxTokens\n\n // `InternalTextProviderOptions.system` is typed\n // `string | Array<TextBlockParam>` (no `| undefined`), so build it\n // outside the literal and spread it conditionally rather than\n // assigning `undefined` under exactOptionalPropertyTypes.\n const systemBlocks = ((): Array<TextBlockParam> | undefined => {\n const normalized = normalizeSystemPrompts<AnthropicSystemPromptMetadata>(\n options.systemPrompts,\n )\n if (normalized.length === 0) return undefined\n return normalized.map(\n (p): TextBlockParam => ({\n type: 'text',\n text: p.content,\n ...(p.metadata?.cache_control && {\n cache_control: p.metadata.cache_control,\n }),\n }),\n )\n })()\n // Wire engine-threaded outputSchema into Messages `output_config.format`\n // alongside any `tools` so the model emits tool calls during the agent\n // loop and a single schema-constrained JSON message on its final turn.\n // Merge into any existing `output_config` so callers can keep tuning\n // `output_config.effort` alongside the schema.\n const combinedSchema = options.outputSchema as\n | Record<string, unknown>\n | undefined\n const outputConfig = combinedSchema\n ? {\n output_config: {\n ...(validProviderOptions.output_config ?? {}),\n format: {\n type: 'json_schema' as const,\n schema: combinedSchema,\n },\n },\n }\n : undefined\n\n // Lift skills attached to a `code_execution` tool into the top-level\n // `container.skills` request param (Anthropic's required shape). Preserve any\n // `container.id` supplied via modelOptions for container reuse. This is the\n // canonical path for skills; `modelOptions.container.skills` is deprecated.\n const toolSkills = options.tools\n ?.map((tool) =>\n tool.name === 'code_execution'\n ? readCodeExecutionSkills(tool)\n : undefined,\n )\n .find((skills) => skills && skills.length > 0)\n\n if (toolSkills && toolSkills.length > 0) {\n const existingContainer = validProviderOptions.container ?? undefined\n validProviderOptions.container = {\n id: existingContainer?.id ?? null,\n skills: toolSkills,\n }\n }\n\n // `temperature`/`top_p` arrive via `...validProviderOptions` (sourced from\n // `modelOptions`). `InternalTextProviderOptions` declares `system` and\n // `tools` as `T?: ...` (no `| undefined`), so spread them conditionally\n // rather than passing explicit `undefined` under exactOptionalPropertyTypes.\n const requestParams: InternalTextProviderOptions = {\n model: options.model,\n max_tokens: maxTokens,\n messages: formattedMessages,\n ...(systemBlocks !== undefined && { system: systemBlocks }),\n ...(tools !== undefined && { tools }),\n ...validProviderOptions,\n ...(outputConfig ?? {}),\n }\n validateTextProviderOptions(requestParams)\n return requestParams\n }\n\n /**\n * Anthropic supports `output_config.format` + `tools` in a single streaming\n * Messages request only for Claude 4.5+ (GA 2026-01-29). For 4.4 and\n * earlier we keep the forced-tool-use workaround in\n * {@link structuredOutput} via the engine's finalization path.\n */\n supportsCombinedToolsAndSchema(): boolean {\n return ANTHROPIC_COMBINED_TOOLS_AND_SCHEMA_MODELS.has(this.model)\n }\n\n private convertContentPartToAnthropic(\n part: ContentPart,\n ): TextBlockParam | ImageBlockParam | DocumentBlockParam {\n switch (part.type) {\n case 'text': {\n const metadata = part.metadata as AnthropicTextMetadata | undefined\n return {\n type: 'text',\n text: part.content,\n ...metadata,\n }\n }\n\n case 'image': {\n const metadata = part.metadata as AnthropicImageMetadata | undefined\n const imageSource: Base64ImageSource | URLImageSource =\n part.source.type === 'data'\n ? {\n type: 'base64',\n data: part.source.value,\n media_type: part.source.mimeType as\n | 'image/jpeg'\n | 'image/png'\n | 'image/gif'\n | 'image/webp',\n }\n : {\n type: 'url',\n url: part.source.value,\n }\n return {\n type: 'image',\n source: imageSource,\n ...metadata,\n }\n }\n case 'document': {\n const metadata = part.metadata as AnthropicDocumentMetadata | undefined\n const docSource: Base64PDFSource | URLPDFSource =\n part.source.type === 'data'\n ? {\n type: 'base64',\n data: part.source.value,\n media_type: part.source.mimeType as 'application/pdf',\n }\n : {\n type: 'url',\n url: part.source.value,\n }\n return {\n type: 'document',\n source: docSource,\n ...metadata,\n }\n }\n case 'audio':\n case 'video':\n throw new Error(\n `Anthropic does not support ${part.type} content directly`,\n )\n default: {\n const _exhaustiveCheck: never = part\n throw new Error(\n `Unsupported content part type: ${(_exhaustiveCheck as ContentPart).type}`,\n )\n }\n }\n }\n\n private formatMessages(\n messages: Array<ModelMessage>,\n ): InternalTextProviderOptions['messages'] {\n const formattedMessages: InternalTextProviderOptions['messages'] = []\n\n for (const message of messages) {\n const role = message.role\n\n if (role === 'tool' && message.toolCallId) {\n const toolContent = message.content\n formattedMessages.push({\n role: 'user',\n content: [\n {\n type: 'tool_result',\n tool_use_id: message.toolCallId,\n content: Array.isArray(toolContent)\n ? toolContent.map((part) =>\n this.convertContentPartToAnthropic(part),\n )\n : typeof toolContent === 'string'\n ? toolContent\n : '',\n },\n ],\n })\n continue\n }\n\n if (role === 'assistant' && message.toolCalls?.length) {\n const contentBlocks: Array<ContentBlockParam> = []\n\n this.appendThinkingBlocks(contentBlocks, message.thinking)\n\n if (message.content) {\n const content =\n typeof message.content === 'string' ? message.content : ''\n const textBlock: TextBlockParam = {\n type: 'text',\n text: content,\n }\n contentBlocks.push(textBlock)\n }\n\n for (const toolCall of message.toolCalls) {\n let parsedInput: unknown = {}\n try {\n const parsed = toolCall.function.arguments\n ? JSON.parse(toolCall.function.arguments)\n : {}\n parsedInput = parsed && typeof parsed === 'object' ? parsed : {}\n } catch {\n parsedInput = toolCall.function.arguments\n }\n\n const toolUseBlock: ToolUseBlockParam = {\n type: 'tool_use',\n id: toolCall.id,\n name: toolCall.function.name,\n input: parsedInput,\n }\n contentBlocks.push(toolUseBlock)\n }\n\n formattedMessages.push({\n role: 'assistant',\n content: contentBlocks,\n })\n\n continue\n }\n\n if (role === 'assistant') {\n const contentBlocks: Array<ContentBlockParam> = []\n this.appendThinkingBlocks(contentBlocks, message.thinking)\n\n if (Array.isArray(message.content)) {\n for (const part of message.content) {\n contentBlocks.push(this.convertContentPartToAnthropic(part))\n }\n } else if (message.content) {\n contentBlocks.push({\n type: 'text',\n text: message.content,\n })\n }\n\n formattedMessages.push({\n role: 'assistant',\n content: contentBlocks.length > 0 ? contentBlocks : '',\n })\n continue\n }\n\n if (role === 'user' && Array.isArray(message.content)) {\n const contentBlocks = message.content.map((part) =>\n this.convertContentPartToAnthropic(part),\n )\n formattedMessages.push({\n role: 'user',\n content: contentBlocks,\n })\n continue\n }\n\n formattedMessages.push({\n role: 'user',\n content:\n typeof message.content === 'string'\n ? message.content\n : message.content\n ? message.content.map((c) =>\n this.convertContentPartToAnthropic(c),\n )\n : '',\n })\n }\n\n // Post-process: Anthropic requires strictly alternating user/assistant roles.\n // Tool results are sent as role:'user' messages, which can create consecutive\n // user messages when followed by a new user message. Merge them.\n return this.mergeConsecutiveSameRoleMessages(formattedMessages)\n }\n\n private appendThinkingBlocks(\n contentBlocks: Array<ContentBlockParam>,\n thinkingParts: ModelMessage['thinking'],\n ): void {\n if (!thinkingParts?.length) return\n\n for (const thinking of thinkingParts) {\n if (!thinking.signature) continue\n const block: ThinkingBlockParam = {\n type: 'thinking',\n thinking: thinking.content,\n signature: thinking.signature,\n }\n contentBlocks.push(block)\n }\n }\n\n /**\n * Merge consecutive messages of the same role into a single message.\n * Anthropic's API requires strictly alternating user/assistant roles.\n * Tool results are wrapped as role:'user' messages, which can collide\n * with actual user messages in multi-turn conversations.\n *\n * Also filters out empty assistant messages (e.g., from a previous failed request).\n */\n private mergeConsecutiveSameRoleMessages(\n messages: InternalTextProviderOptions['messages'],\n ): InternalTextProviderOptions['messages'] {\n const merged: InternalTextProviderOptions['messages'] = []\n\n for (const msg of messages) {\n // Skip empty assistant messages (no content or empty string)\n if (msg.role === 'assistant') {\n const hasContent = Array.isArray(msg.content)\n ? msg.content.length > 0\n : typeof msg.content === 'string' && msg.content.length > 0\n if (!hasContent) {\n continue\n }\n }\n\n const prev = merged[merged.length - 1]\n if (prev && prev.role === msg.role) {\n // Normalize both contents to arrays and concatenate\n const prevBlocks = Array.isArray(prev.content)\n ? prev.content\n : typeof prev.content === 'string' && prev.content\n ? [{ type: 'text' as const, text: prev.content }]\n : []\n const msgBlocks = Array.isArray(msg.content)\n ? msg.content\n : typeof msg.content === 'string' && msg.content\n ? [{ type: 'text' as const, text: msg.content }]\n : []\n prev.content = [...prevBlocks, ...msgBlocks]\n } else {\n merged.push({ ...msg })\n }\n }\n\n // De-duplicate tool_result blocks with the same tool_use_id.\n // This can happen when the core layer generates tool results from both\n // the tool-result part and the tool-call part's output field.\n for (const msg of merged) {\n if (Array.isArray(msg.content)) {\n const seenToolResultIds = new Set<string>()\n msg.content = msg.content.filter((block: any) => {\n if (block.type === 'tool_result' && block.tool_use_id) {\n if (seenToolResultIds.has(block.tool_use_id)) {\n return false // Remove duplicate\n }\n seenToolResultIds.add(block.tool_use_id)\n }\n return true\n })\n }\n }\n\n return merged\n }\n\n private async *processAnthropicStream(\n stream: AsyncIterable<Anthropic_SDK.Beta.BetaRawMessageStreamEvent>,\n options: TextOptions<AnthropicTextProviderOptions>,\n genId: () => string,\n logger: InternalLogger,\n ): AsyncIterable<StreamChunk> {\n const model = options.model\n let accumulatedContent = ''\n let accumulatedThinking = ''\n let accumulatedSignature = ''\n const toolCallsMap = new Map<\n number,\n { id: string; name: string; input: string; started: boolean }\n >()\n let currentToolIndex = -1\n // Server-side tools share the `input_json_delta` wire format with client\n // `tool_use` blocks; routing both to the same buffer corrupts client tool\n // input.\n let currentServerTool: { id: string; name: string; input: string } | null =\n null\n\n // AG-UI lifecycle tracking\n const runId = options.runId ?? genId()\n const threadId = options.threadId ?? genId()\n const messageId = genId()\n let stepId: string | null = null\n let reasoningMessageId: string | null = null\n let hasClosedReasoning = false\n let hasEmittedRunStarted = false\n let hasEmittedTextMessageStart = false\n let hasEmittedRunFinished = false\n // Track current content block type for proper content_block_stop handling\n let currentBlockType: string | null = null\n\n try {\n for await (const event of stream) {\n logger.provider(`provider=anthropic type=${event.type}`, {\n chunk: event,\n })\n // Emit RUN_STARTED on first event\n if (!hasEmittedRunStarted) {\n hasEmittedRunStarted = true\n yield {\n type: EventType.RUN_STARTED,\n runId,\n threadId,\n model,\n timestamp: Date.now(),\n parentRunId: options.parentRunId,\n }\n }\n\n if (event.type === 'content_block_start') {\n currentBlockType = event.content_block.type\n if (event.content_block.type === 'tool_use') {\n currentToolIndex++\n toolCallsMap.set(currentToolIndex, {\n id: event.content_block.id,\n name: event.content_block.name,\n input: '',\n started: false,\n })\n } else if (event.content_block.type === 'server_tool_use') {\n currentServerTool = {\n id: event.content_block.id,\n name: event.content_block.name,\n input: '',\n }\n } else if (\n event.content_block.type === 'web_fetch_tool_result' ||\n event.content_block.type === 'web_search_tool_result'\n ) {\n // The result content arrives in full at content_block_start (no\n // deltas). Surface error variants so a failed fetch/search isn't\n // invisible to the consumer.\n const content = event.content_block.content as\n | { type?: string; error_code?: string }\n | Array<unknown>\n const errorBlock =\n !Array.isArray(content) &&\n (content.type === 'web_fetch_tool_result_error' ||\n content.type === 'web_search_tool_result_error')\n ? content\n : null\n if (errorBlock) {\n logger.errors(\n `anthropic.${event.content_block.type} error_code=${errorBlock.error_code}`,\n {\n toolUseId: event.content_block.tool_use_id,\n blockType: event.content_block.type,\n errorCode: errorBlock.error_code,\n source: 'anthropic.processAnthropicStream',\n },\n )\n }\n } else if (event.content_block.type === 'thinking') {\n accumulatedThinking = ''\n accumulatedSignature = ''\n // Emit REASONING and STEP_STARTED for thinking\n stepId = genId()\n reasoningMessageId = genId()\n\n // Spec REASONING events\n yield {\n type: EventType.REASONING_START,\n messageId: reasoningMessageId,\n model,\n timestamp: Date.now(),\n }\n yield {\n type: EventType.REASONING_MESSAGE_START,\n messageId: reasoningMessageId,\n role: 'reasoning' as const,\n model,\n timestamp: Date.now(),\n }\n\n // Legacy STEP events (kept during transition)\n yield {\n type: EventType.STEP_STARTED,\n stepName: stepId,\n stepId,\n model,\n timestamp: Date.now(),\n stepType: 'thinking',\n }\n }\n } else if (event.type === 'content_block_delta') {\n if (event.delta.type === 'text_delta') {\n // Close reasoning before text starts\n if (reasoningMessageId && !hasClosedReasoning) {\n hasClosedReasoning = true\n yield {\n type: EventType.REASONING_MESSAGE_END,\n messageId: reasoningMessageId,\n model,\n timestamp: Date.now(),\n }\n yield {\n type: EventType.REASONING_END,\n messageId: reasoningMessageId,\n model,\n timestamp: Date.now(),\n }\n }\n\n // Emit TEXT_MESSAGE_START on first text content\n if (!hasEmittedTextMessageStart) {\n hasEmittedTextMessageStart = true\n yield {\n type: EventType.TEXT_MESSAGE_START,\n messageId,\n model,\n timestamp: Date.now(),\n role: 'assistant',\n }\n }\n\n const delta = event.delta.text\n accumulatedContent += delta\n yield {\n type: EventType.TEXT_MESSAGE_CONTENT,\n messageId,\n model,\n timestamp: Date.now(),\n delta,\n content: accumulatedContent,\n }\n } else if (\n event.delta.type === 'thinking_delta' &&\n reasoningMessageId\n ) {\n const delta = event.delta.thinking\n accumulatedThinking += delta\n\n // Spec REASONING content event\n yield {\n type: EventType.REASONING_MESSAGE_CONTENT,\n messageId: reasoningMessageId,\n delta,\n model,\n timestamp: Date.now(),\n }\n\n // Legacy STEP event\n yield {\n type: EventType.STEP_FINISHED,\n stepName: stepId || genId(),\n stepId: stepId || genId(),\n model,\n timestamp: Date.now(),\n delta,\n content: accumulatedThinking,\n }\n } else if (\n (event.delta as { type: string }).type === 'signature_delta'\n ) {\n accumulatedSignature +=\n (event.delta as { signature: string }).signature || ''\n } else if (event.delta.type === 'input_json_delta') {\n // Route deltas by current block type so server_tool_use input\n // never appends onto the prior client tool's buffer.\n if (currentBlockType === 'tool_use') {\n const existing = toolCallsMap.get(currentToolIndex)\n if (existing) {\n // Emit TOOL_CALL_START on first args delta\n if (!existing.started) {\n existing.started = true\n yield {\n type: EventType.TOOL_CALL_START,\n toolCallId: existing.id,\n toolCallName: existing.name,\n toolName: existing.name,\n model,\n timestamp: Date.now(),\n index: currentToolIndex,\n }\n }\n\n existing.input += event.delta.partial_json\n\n yield {\n type: EventType.TOOL_CALL_ARGS,\n toolCallId: existing.id,\n model,\n timestamp: Date.now(),\n delta: event.delta.partial_json,\n args: existing.input,\n }\n }\n } else if (\n currentBlockType === 'server_tool_use' &&\n currentServerTool\n ) {\n // Accumulate server tool input internally. We don't emit\n // TOOL_CALL_* events: the call is executed by Anthropic, not\n // by our agent loop, so surfacing it as a client tool call\n // would cause downstream code to try (and fail) to run it.\n currentServerTool.input += event.delta.partial_json\n }\n }\n } else if (event.type === 'content_block_stop') {\n if (currentBlockType === 'thinking') {\n // Emit signature so it can be replayed in multi-turn context\n if (accumulatedSignature && stepId) {\n yield {\n type: EventType.STEP_FINISHED,\n stepName: stepId,\n stepId,\n model,\n timestamp: Date.now(),\n delta: '',\n content: accumulatedThinking,\n signature: accumulatedSignature,\n }\n }\n } else if (currentBlockType === 'tool_use') {\n const existing = toolCallsMap.get(currentToolIndex)\n if (existing) {\n // If tool call wasn't started yet (no args), start it now\n if (!existing.started) {\n existing.started = true\n yield {\n type: EventType.TOOL_CALL_START,\n toolCallId: existing.id,\n toolCallName: existing.name,\n toolName: existing.name,\n model,\n timestamp: Date.now(),\n index: currentToolIndex,\n }\n }\n\n // Emit TOOL_CALL_END\n let parsedInput: unknown = {}\n try {\n const parsed = existing.input ? JSON.parse(existing.input) : {}\n parsedInput = parsed && typeof parsed === 'object' ? parsed : {}\n } catch {\n parsedInput = {}\n }\n\n yield {\n type: EventType.TOOL_CALL_END,\n toolCallId: existing.id,\n toolCallName: existing.name,\n toolName: existing.name,\n model,\n timestamp: Date.now(),\n input: parsedInput,\n }\n\n // Reset so a new TEXT_MESSAGE_START is emitted if text follows tool calls\n hasEmittedTextMessageStart = false\n }\n } else if (currentBlockType === 'server_tool_use') {\n if (currentServerTool) {\n // Anthropic executes the call; we only need a breadcrumb so\n // consumers (devtools, telemetry) can see what ran.\n logger.provider(\n `provider=anthropic server_tool_use name=${currentServerTool.name}`,\n {\n toolUseId: currentServerTool.id,\n name: currentServerTool.name,\n input: currentServerTool.input,\n },\n )\n }\n currentServerTool = null\n } else if (\n currentBlockType === 'web_fetch_tool_result' ||\n currentBlockType === 'web_search_tool_result'\n ) {\n // The model already consumed the result; error variants were\n // already surfaced at content_block_start.\n } else {\n // Emit TEXT_MESSAGE_END only for text blocks (not tool_use blocks)\n if (hasEmittedTextMessageStart && accumulatedContent) {\n yield {\n type: EventType.TEXT_MESSAGE_END,\n messageId,\n model,\n timestamp: Date.now(),\n }\n }\n }\n currentBlockType = null\n } else if (event.type === 'message_stop') {\n // Close reasoning events if still open\n if (reasoningMessageId && !hasClosedReasoning) {\n hasClosedReasoning = true\n yield {\n type: EventType.REASONING_MESSAGE_END,\n messageId: reasoningMessageId,\n model,\n timestamp: Date.now(),\n }\n yield {\n type: EventType.REASONING_END,\n messageId: reasoningMessageId,\n model,\n timestamp: Date.now(),\n }\n }\n\n // Only emit RUN_FINISHED from message_stop if message_delta didn't already emit one.\n // message_delta carries the real stop_reason (tool_use, end_turn, etc.),\n // while message_stop is just a completion signal.\n if (!hasEmittedRunFinished) {\n yield {\n type: EventType.RUN_FINISHED,\n runId,\n threadId,\n model,\n timestamp: Date.now(),\n finishReason: 'stop',\n }\n }\n } else if (event.type === 'message_delta') {\n if (event.delta.stop_reason) {\n hasEmittedRunFinished = true\n\n // Close reasoning events if still open\n if (reasoningMessageId && !hasClosedReasoning) {\n hasClosedReasoning = true\n yield {\n type: EventType.REASONING_MESSAGE_END,\n messageId: reasoningMessageId,\n model,\n timestamp: Date.now(),\n }\n yield {\n type: EventType.REASONING_END,\n messageId: reasoningMessageId,\n model,\n timestamp: Date.now(),\n }\n }\n\n switch (event.delta.stop_reason) {\n case 'tool_use': {\n yield {\n type: EventType.RUN_FINISHED,\n runId,\n threadId,\n model,\n timestamp: Date.now(),\n finishReason: 'tool_calls',\n usage: buildAnthropicUsage(event.usage),\n }\n break\n }\n case 'max_tokens': {\n yield {\n type: EventType.RUN_ERROR,\n model,\n timestamp: Date.now(),\n message:\n 'The response was cut off because the maximum token limit was reached.',\n code: 'max_tokens',\n error: {\n message:\n 'The response was cut off because the maximum token limit was reached.',\n code: 'max_tokens',\n },\n }\n break\n }\n case 'stop_sequence':\n case 'end_turn':\n case 'pause_turn':\n case 'refusal':\n case 'model_context_window_exceeded':\n case 'compaction':\n default: {\n // All remaining Anthropic stop_reason variants map to the\n // generic \"stop\" finish reason — they describe *why* the\n // stream ended, but for AG-UI consumers the resulting event\n // shape is identical.\n yield {\n type: EventType.RUN_FINISHED,\n runId,\n threadId,\n model,\n timestamp: Date.now(),\n finishReason: 'stop',\n usage: buildAnthropicUsage(event.usage),\n }\n }\n }\n }\n }\n }\n } catch (error: unknown) {\n const err = error as Error & { status?: number; code?: string }\n const rawEvent = toRunErrorRawEvent(error)\n\n logger.errors('anthropic.processAnthropicStream fatal', {\n error,\n source: 'anthropic.processAnthropicStream',\n })\n yield {\n type: EventType.RUN_ERROR,\n model,\n timestamp: Date.now(),\n message: err.message || 'Unknown error occurred',\n code: err.code || String(err.status),\n // Forward the Anthropic SDK error's `.error` response body when present.\n ...(rawEvent !== undefined && { rawEvent }),\n error: {\n message: err.message || 'Unknown error occurred',\n code: err.code || String(err.status),\n },\n }\n }\n }\n}\n\n/**\n * Creates an Anthropic chat adapter with explicit API key.\n * Type resolution happens here at the call site.\n */\nexport function createAnthropicChat<\n TModel extends (typeof ANTHROPIC_MODELS)[number],\n>(\n model: TModel,\n apiKey: string,\n config?: Omit<AnthropicTextConfig, 'apiKey'>,\n): AnthropicTextAdapter<\n TModel,\n ResolveProviderOptions<TModel>,\n ResolveInputModalities<TModel>\n> {\n return new AnthropicTextAdapter({ apiKey, ...config }, model)\n}\n\n/**\n * Creates an Anthropic text adapter with automatic API key detection.\n * Type resolution happens here at the call site.\n */\nexport function anthropicText<TModel extends (typeof ANTHROPIC_MODELS)[number]>(\n model: TModel,\n config?: Omit<AnthropicTextConfig, 'apiKey'>,\n): AnthropicTextAdapter<\n TModel,\n ResolveProviderOptions<TModel>,\n ResolveInputModalities<TModel>\n> {\n const apiKey = getAnthropicApiKeyFromEnv()\n return createAnthropicChat(model, apiKey, config)\n}\n"],"names":[],"mappings":";;;;;;;;;AAkEO,SAAS,sBACd,OACA,cAQkC;AAClC,QAAM,4BAAY,IAAA;AAElB,QAAM,yBACJ,cAAc,UAAU,SAAS,aACjC,OAAO,aAAa,SAAS,kBAAkB,YAC/C,aAAa,SAAS,gBAAgB;AACxC,MAAI,uBAAwB,OAAM,IAAI,iCAAiC;AAIvE,QAAM,eAAe,OAAO,KAAK,CAAC,MAAM,EAAE,SAAS,gBAAgB;AACnE,MAAI,cAAc;AAChB,UAAM,UAAU,wBAAwB,YAAY,GAAG;AAIvD,UAAM;AAAA,MACJ,YAAY,4BACR,8BACA;AAAA,IAAA;AAAA,EAER;AAKA,QAAM,YAAY,OAAO;AAAA,IACvB,CAAC,MACC,EAAE,SAAS,qBACV,wBAAwB,CAAC,GAAG,UAAU,KAA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-1,6 +1,6 @@
|
|
|
1
1
|
import { BetaContextManagementConfig, BetaToolChoiceAny, BetaToolChoiceAuto, BetaToolChoiceTool } from '@anthropic-ai/sdk/resources/beta/messages/messages';
|
|
2
2
|
import { CacheControlEphemeral } from '@anthropic-ai/sdk/resources';
|
|
3
|
-
import { AnthropicTool } from '../tools.js';
|
|
3
|
+
import { AnthropicContainerSkill, AnthropicTool } from '../tools.js';
|
|
4
4
|
import { MessageParam, TextBlockParam } from '@anthropic-ai/sdk/resources/messages';
|
|
5
5
|
/**
|
|
6
6
|
* Per-prompt metadata Anthropic understands on `systemPrompts` entries.
|
|
@@ -39,19 +39,14 @@ export interface AnthropicContainerOptions {
|
|
|
39
39
|
container?: {
|
|
40
40
|
id: string | null;
|
|
41
41
|
/**
|
|
42
|
-
* List of skills to load into the container
|
|
42
|
+
* List of skills to load into the container.
|
|
43
|
+
*
|
|
44
|
+
* @deprecated Configure skills on the `code_execution` tool instead:
|
|
45
|
+
* `codeExecutionTool(config, { skills })`. The adapter lifts those into
|
|
46
|
+
* `container.skills` and attaches the required beta headers. Setting
|
|
47
|
+
* skills here bypasses the beta-header wiring and may stop working.
|
|
43
48
|
*/
|
|
44
|
-
skills: Array<
|
|
45
|
-
/**
|
|
46
|
-
* Between 1-64 characters
|
|
47
|
-
*/
|
|
48
|
-
skill_id: string;
|
|
49
|
-
type: 'anthropic' | 'custom';
|
|
50
|
-
/**
|
|
51
|
-
* Skill version or latest by default
|
|
52
|
-
*/
|
|
53
|
-
version?: string;
|
|
54
|
-
}> | null;
|
|
49
|
+
skills: Array<AnthropicContainerSkill> | null;
|
|
55
50
|
} | null;
|
|
56
51
|
}
|
|
57
52
|
export interface AnthropicContextManagementOptions {
|
|
@@ -179,6 +174,24 @@ export interface AnthropicSamplingOptions {
|
|
|
179
174
|
Required range: x >= 0
|
|
180
175
|
*/
|
|
181
176
|
top_k?: number;
|
|
177
|
+
/**
|
|
178
|
+
* Amount of randomness injected into the response.
|
|
179
|
+
* Either use this or top_p, but not both.
|
|
180
|
+
* Defaults to 1.0. Ranges from 0.0 to 1.0. Use temperature closer to 0.0 for analytical / multiple choice, and closer to 1.0 for creative and generative tasks.
|
|
181
|
+
* @default 1.0
|
|
182
|
+
*/
|
|
183
|
+
temperature?: number;
|
|
184
|
+
/**
|
|
185
|
+
* Use nucleus sampling.
|
|
186
|
+
*
|
|
187
|
+
* In nucleus sampling, we compute the cumulative distribution over all the options for each subsequent token in decreasing probability order and cut it off once it reaches a particular probability specified by top_p. You should either alter temperature or top_p, but not both.
|
|
188
|
+
*/
|
|
189
|
+
top_p?: number;
|
|
190
|
+
/**
|
|
191
|
+
* The maximum number of tokens to generate before stopping. This parameter only specifies the absolute maximum number of tokens to generate. Required by the API; the adapter defaults to 1024 when omitted.
|
|
192
|
+
* Range x >= 1.
|
|
193
|
+
*/
|
|
194
|
+
max_tokens?: number;
|
|
182
195
|
}
|
|
183
196
|
export type ExternalTextProviderOptions = AnthropicContainerOptions & AnthropicContextManagementOptions & AnthropicMCPOptions & AnthropicServiceTierOptions & AnthropicStopSequencesOptions & AnthropicThinkingOptions & AnthropicToolChoiceOptions & AnthropicSamplingOptions & Partial<AnthropicAdaptiveThinkingOptions> & Partial<AnthropicEffortOptions> & Partial<AnthropicOutputConfigOptions>;
|
|
184
197
|
export interface InternalTextProviderOptions extends ExternalTextProviderOptions {
|
|
@@ -203,13 +216,6 @@ export interface InternalTextProviderOptions extends ExternalTextProviderOptions
|
|
|
203
216
|
* such as specifying a particular goal or role.
|
|
204
217
|
*/
|
|
205
218
|
system?: string | Array<TextBlockParam>;
|
|
206
|
-
/**
|
|
207
|
-
* Amount of randomness injected into the response.
|
|
208
|
-
* Either use this or top_p, but not both.
|
|
209
|
-
* Defaults to 1.0. Ranges from 0.0 to 1.0. Use temperature closer to 0.0 for analytical / multiple choice, and closer to 1.0 for creative and generative tasks.
|
|
210
|
-
* @default 1.0
|
|
211
|
-
*/
|
|
212
|
-
temperature?: number;
|
|
213
219
|
tools?: Array<AnthropicTool>;
|
|
214
220
|
/**
|
|
215
221
|
* Schema-constrained final answer in a single Messages request (issue
|
|
@@ -233,12 +239,6 @@ export interface InternalTextProviderOptions extends ExternalTextProviderOptions
|
|
|
233
239
|
schema: Record<string, unknown>;
|
|
234
240
|
};
|
|
235
241
|
};
|
|
236
|
-
/**
|
|
237
|
-
* Use nucleus sampling.
|
|
238
|
-
|
|
239
|
-
In nucleus sampling, we compute the cumulative distribution over all the options for each subsequent token in decreasing probability order and cut it off once it reaches a particular probability specified by top_p. You should either alter temperature or top_p, but not both.
|
|
240
|
-
*/
|
|
241
|
-
top_p?: number;
|
|
242
242
|
}
|
|
243
243
|
export interface CacheControl {
|
|
244
244
|
type: 'ephemeral';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"text-provider-options.js","sources":["../../../src/text/text-provider-options.ts"],"sourcesContent":["import type {\n BetaContextManagementConfig,\n BetaToolChoiceAny,\n BetaToolChoiceAuto,\n BetaToolChoiceTool,\n} from '@anthropic-ai/sdk/resources/beta/messages/messages'\nimport type { CacheControlEphemeral } from '@anthropic-ai/sdk/resources'\nimport type { AnthropicTool } from '../tools'\nimport type {\n MessageParam,\n TextBlockParam,\n} from '@anthropic-ai/sdk/resources/messages'\n\n/**\n * Per-prompt metadata Anthropic understands on `systemPrompts` entries.\n *\n * Used via the structured form of `systemPrompts`:\n *\n * @example\n * import type { AnthropicSystemPromptMetadata } from '@tanstack/ai-anthropic'\n *\n * chat({\n * adapter: anthropicText(),\n * model: 'claude-sonnet-4-6',\n * systemPrompts: [\n * {\n * content: 'Stable instructions — cache me.',\n * metadata: { cache_control: { type: 'ephemeral' } } satisfies AnthropicSystemPromptMetadata,\n * },\n * 'Volatile per-request instruction.',\n * ],\n * })\n */\nexport interface AnthropicSystemPromptMetadata {\n /**\n * Anthropic prompt-caching control applied to this system prompt's\n * `TextBlockParam`.\n *\n * @see https://docs.anthropic.com/en/docs/build-with-claude/prompt-caching\n */\n cache_control?: CacheControlEphemeral\n}\n\nexport interface AnthropicContainerOptions {\n /**\n * Container identifier for reuse across requests.\n * Container parameters with skills to be loaded.\n */\n container?: {\n id: string | null\n /**\n * List of skills to load into the container\n */\n skills: Array<{\n /**\n * Between 1-64 characters\n */\n skill_id: string\n\n type: 'anthropic' | 'custom'\n /**\n * Skill version or latest by default\n */\n version?: string\n }> | null\n } | null\n}\n\nexport interface AnthropicContextManagementOptions {\n /**\n * Context management configuration.\n\nThis allows you to control how Claude manages context across multiple requests, such as whether to clear function results or not.\n */\n context_management?: BetaContextManagementConfig | null\n}\n\nexport interface AnthropicMCPOptions {\n /**\n * MCP servers to be utilized in this request\n * Maximum of 20 servers\n */\n mcp_servers?: Array<MCPServer>\n}\n\nexport interface AnthropicServiceTierOptions {\n /**\n * Determines whether to use priority capacity (if available) or standard capacity for this request.\n */\n service_tier?: 'auto' | 'standard_only'\n}\n\nexport interface AnthropicStopSequencesOptions {\n /**\n * Custom text sequences that will cause the model to stop generating.\n\nAnthropic models will normally stop when they have naturally completed their turn, which will result in a response stop_reason of \"end_turn\".\n\nIf you want the model to stop generating when it encounters custom strings of text, you can use the stop_sequences parameter. If the model encounters one of the custom sequences, the response stop_reason value will be \"stop_sequence\" and the response stop_sequence value will contain the matched stop sequence.\n */\n stop_sequences?: Array<string>\n}\n\nexport interface AnthropicThinkingOptions {\n /**\n * Configuration for enabling Claude's extended thinking.\n\nWhen enabled, responses include thinking content blocks showing Claude's thinking process before the final answer. Requires a minimum budget of 1,024 tokens and counts towards your max_tokens limit.\n */\n thinking?:\n | {\n /**\n* Determines how many tokens Claude can use for its internal reasoning process. Larger budgets can enable more thorough analysis for complex problems, improving response quality.\n\nMust be ≥1024 and less than max_tokens\n*/\n budget_tokens: number\n\n type: 'enabled'\n }\n | {\n type: 'disabled'\n }\n}\n\nexport interface AnthropicAdaptiveThinkingOptions {\n /**\n * Configuration for Claude's adaptive thinking (Opus 4.6+).\n *\n * In adaptive mode, Claude dynamically decides when and how much to think.\n * Use the effort parameter to control thinking depth.\n * `thinking: {type: \"enabled\"}` with `budget_tokens` is deprecated on Opus 4.6.\n */\n thinking?:\n | {\n type: 'adaptive'\n /**\n * Controls what (if any) thinking content is streamed back.\n *\n * - `'summarized'`: stream summarized thinking via `thinking_delta`\n * events (the user-visible reasoning text).\n * - `'omitted'`: stream the thinking block's `signature_delta` only\n * (no reasoning text reaches the client).\n *\n * On Claude Opus 4.6 the default is `'summarized'`. On\n * Claude Opus 4.7 the default flipped to `'omitted'` — callers\n * must set `'summarized'` explicitly to get the reasoning text.\n */\n display?: 'summarized' | 'omitted'\n }\n | {\n /**\n * @deprecated Use `type: 'adaptive'` with the effort parameter on Opus 4.6+.\n */\n budget_tokens: number\n type: 'enabled'\n }\n | {\n type: 'disabled'\n }\n}\n\nexport interface AnthropicEffortOptions {\n /**\n * Controls the thinking depth for adaptive thinking mode (Opus 4.6+).\n *\n * - `max`: Absolute highest capability\n * - `high`: Default - Claude will almost always think\n * - `medium`: Balanced cost-quality\n * - `low`: May skip thinking for simpler problems\n */\n effort?: 'max' | 'high' | 'medium' | 'low'\n}\n\nexport interface AnthropicOutputConfigOptions {\n /**\n * Output configuration for the model's response.\n *\n * On Claude 4.7+ the top-level `effort` field was relocated under\n * `output_config.effort`, and `thinking: { type: 'enabled', budget_tokens }`\n * was replaced by `thinking: { type: 'adaptive' }` paired with\n * `output_config.effort`. Earlier models continue to accept the legacy\n * top-level `effort` / `thinking.type: 'enabled'` shape.\n *\n * The engine also writes `output_config.format` here when the caller\n * passes `outputSchema` to a Claude 4.5+ adapter (issue #605 native\n * combined mode). Both fields coexist: user-supplied `effort` is\n * preserved when the engine adds `format`.\n */\n output_config?: {\n effort?: 'low' | 'medium' | 'high' | 'max' | null\n }\n}\n\nexport interface AnthropicToolChoiceOptions {\n tool_choice?: BetaToolChoiceAny | BetaToolChoiceTool | BetaToolChoiceAuto\n}\n\nexport interface AnthropicSamplingOptions {\n /**\n * Only sample from the top K options for each subsequent token.\n\nUsed to remove \"long tail\" low probability responses.\nRecommended for advanced use cases only. You usually only need to use temperature.\n\nRequired range: x >= 0\n */\n top_k?: number\n}\n\nexport type ExternalTextProviderOptions = AnthropicContainerOptions &\n AnthropicContextManagementOptions &\n AnthropicMCPOptions &\n AnthropicServiceTierOptions &\n AnthropicStopSequencesOptions &\n AnthropicThinkingOptions &\n AnthropicToolChoiceOptions &\n AnthropicSamplingOptions &\n Partial<AnthropicAdaptiveThinkingOptions> &\n Partial<AnthropicEffortOptions> &\n Partial<AnthropicOutputConfigOptions>\n\nexport interface InternalTextProviderOptions extends ExternalTextProviderOptions {\n model: string\n\n messages: Array<MessageParam>\n\n /**\n * The maximum number of tokens to generate before stopping. This parameter only specifies the absolute maximum number of tokens to generate.\n * Range x >= 1.\n */\n max_tokens: number\n /**\n * Whether to incrementally stream the response using server-sent events.\n */\n stream?: boolean\n /**\n * System prompt — built by the adapter from the user-facing\n * `systemPrompts: Array<string>` on the chat call. This field is internal:\n * users should pass system prompts via `systemPrompts`, not via\n * `modelOptions`.\n *\n * A system prompt is a way of providing context and instructions to Claude,\n * such as specifying a particular goal or role.\n */\n system?: string | Array<TextBlockParam>\n /**\n * Amount of randomness injected into the response.\n * Either use this or top_p, but not both.\n * Defaults to 1.0. Ranges from 0.0 to 1.0. Use temperature closer to 0.0 for analytical / multiple choice, and closer to 1.0 for creative and generative tasks.\n * @default 1.0\n */\n temperature?: number\n\n tools?: Array<AnthropicTool>\n\n /**\n * Schema-constrained final answer in a single Messages request (issue\n * #605). Set by the engine when the adapter declared\n * `supportsCombinedToolsAndSchema` and a caller passed `outputSchema`\n * to `chat()`. The model emits tool calls during the agent loop and a\n * schema-matching JSON message on the natural final turn — no separate\n * finalization round-trip needed.\n *\n * The SDK type (`BetaOutputConfig`) currently exposes only `effort`;\n * `format` is accepted at runtime per the deprecation notice on the\n * older `output_format` field\n * (https://platform.claude.com/docs/en/build-with-claude/structured-outputs).\n * We type it explicitly here so the adapter call site doesn't need a\n * cast.\n */\n output_config?: {\n effort?: 'low' | 'medium' | 'high' | 'max' | null\n format?: {\n type: 'json_schema'\n schema: Record<string, unknown>\n }\n }\n\n /**\n * Use nucleus sampling.\n\nIn nucleus sampling, we compute the cumulative distribution over all the options for each subsequent token in decreasing probability order and cut it off once it reaches a particular probability specified by top_p. You should either alter temperature or top_p, but not both.\n */\n top_p?: number\n}\n\nconst validateTopPandTemperature = (options: InternalTextProviderOptions) => {\n if (options.top_p !== undefined && options.temperature !== undefined) {\n throw new Error('You should either set top_p or temperature, but not both.')\n }\n}\n\nexport interface CacheControl {\n type: 'ephemeral'\n ttl: '5m' | '1h'\n}\n\nconst validateThinking = (options: InternalTextProviderOptions) => {\n const thinking = options.thinking\n if (thinking && thinking.type === 'enabled') {\n if (thinking.budget_tokens < 1024) {\n throw new Error('thinking.budget_tokens must be at least 1024.')\n }\n if (thinking.budget_tokens >= options.max_tokens) {\n throw new Error('thinking.budget_tokens must be less than max_tokens.')\n }\n }\n}\n\ninterface MCPServer {\n name: string\n url: string\n type: 'url'\n authorization_token?: string | null\n tool_configuration: {\n allowed_tools?: Array<string> | null\n enabled?: boolean | null\n } | null\n}\n\nconst validateMaxTokens = (options: InternalTextProviderOptions) => {\n if (options.max_tokens < 1) {\n throw new Error('max_tokens must be at least 1.')\n }\n}\n\nexport const validateTextProviderOptions = (\n options: InternalTextProviderOptions,\n) => {\n validateTopPandTemperature(options)\n validateThinking(options)\n validateMaxTokens(options)\n}\n"],"names":[],"mappings":"AA+RA,MAAM,6BAA6B,CAAC,YAAyC;AAC3E,MAAI,QAAQ,UAAU,UAAa,QAAQ,gBAAgB,QAAW;AACpE,UAAM,IAAI,MAAM,2DAA2D;AAAA,EAC7E;AACF;AAOA,MAAM,mBAAmB,CAAC,YAAyC;AACjE,QAAM,WAAW,QAAQ;AACzB,MAAI,YAAY,SAAS,SAAS,WAAW;AAC3C,QAAI,SAAS,gBAAgB,MAAM;AACjC,YAAM,IAAI,MAAM,+CAA+C;AAAA,IACjE;AACA,QAAI,SAAS,iBAAiB,QAAQ,YAAY;AAChD,YAAM,IAAI,MAAM,sDAAsD;AAAA,IACxE;AAAA,EACF;AACF;AAaA,MAAM,oBAAoB,CAAC,YAAyC;AAClE,MAAI,QAAQ,aAAa,GAAG;AAC1B,UAAM,IAAI,MAAM,gCAAgC;AAAA,EAClD;AACF;AAEO,MAAM,8BAA8B,CACzC,YACG;AACH,6BAA2B,OAAO;AAClC,mBAAiB,OAAO;AACxB,oBAAkB,OAAO;AAC3B;"}
|
|
1
|
+
{"version":3,"file":"text-provider-options.js","sources":["../../../src/text/text-provider-options.ts"],"sourcesContent":["import type {\n BetaContextManagementConfig,\n BetaToolChoiceAny,\n BetaToolChoiceAuto,\n BetaToolChoiceTool,\n} from '@anthropic-ai/sdk/resources/beta/messages/messages'\nimport type { CacheControlEphemeral } from '@anthropic-ai/sdk/resources'\nimport type { AnthropicContainerSkill, AnthropicTool } from '../tools'\nimport type {\n MessageParam,\n TextBlockParam,\n} from '@anthropic-ai/sdk/resources/messages'\n\n/**\n * Per-prompt metadata Anthropic understands on `systemPrompts` entries.\n *\n * Used via the structured form of `systemPrompts`:\n *\n * @example\n * import type { AnthropicSystemPromptMetadata } from '@tanstack/ai-anthropic'\n *\n * chat({\n * adapter: anthropicText(),\n * model: 'claude-sonnet-4-6',\n * systemPrompts: [\n * {\n * content: 'Stable instructions — cache me.',\n * metadata: { cache_control: { type: 'ephemeral' } } satisfies AnthropicSystemPromptMetadata,\n * },\n * 'Volatile per-request instruction.',\n * ],\n * })\n */\nexport interface AnthropicSystemPromptMetadata {\n /**\n * Anthropic prompt-caching control applied to this system prompt's\n * `TextBlockParam`.\n *\n * @see https://docs.anthropic.com/en/docs/build-with-claude/prompt-caching\n */\n cache_control?: CacheControlEphemeral\n}\n\nexport interface AnthropicContainerOptions {\n /**\n * Container identifier for reuse across requests.\n * Container parameters with skills to be loaded.\n */\n container?: {\n id: string | null\n /**\n * List of skills to load into the container.\n *\n * @deprecated Configure skills on the `code_execution` tool instead:\n * `codeExecutionTool(config, { skills })`. The adapter lifts those into\n * `container.skills` and attaches the required beta headers. Setting\n * skills here bypasses the beta-header wiring and may stop working.\n */\n skills: Array<AnthropicContainerSkill> | null\n } | null\n}\n\nexport interface AnthropicContextManagementOptions {\n /**\n * Context management configuration.\n\nThis allows you to control how Claude manages context across multiple requests, such as whether to clear function results or not.\n */\n context_management?: BetaContextManagementConfig | null\n}\n\nexport interface AnthropicMCPOptions {\n /**\n * MCP servers to be utilized in this request\n * Maximum of 20 servers\n */\n mcp_servers?: Array<MCPServer>\n}\n\nexport interface AnthropicServiceTierOptions {\n /**\n * Determines whether to use priority capacity (if available) or standard capacity for this request.\n */\n service_tier?: 'auto' | 'standard_only'\n}\n\nexport interface AnthropicStopSequencesOptions {\n /**\n * Custom text sequences that will cause the model to stop generating.\n\nAnthropic models will normally stop when they have naturally completed their turn, which will result in a response stop_reason of \"end_turn\".\n\nIf you want the model to stop generating when it encounters custom strings of text, you can use the stop_sequences parameter. If the model encounters one of the custom sequences, the response stop_reason value will be \"stop_sequence\" and the response stop_sequence value will contain the matched stop sequence.\n */\n stop_sequences?: Array<string>\n}\n\nexport interface AnthropicThinkingOptions {\n /**\n * Configuration for enabling Claude's extended thinking.\n\nWhen enabled, responses include thinking content blocks showing Claude's thinking process before the final answer. Requires a minimum budget of 1,024 tokens and counts towards your max_tokens limit.\n */\n thinking?:\n | {\n /**\n* Determines how many tokens Claude can use for its internal reasoning process. Larger budgets can enable more thorough analysis for complex problems, improving response quality.\n\nMust be ≥1024 and less than max_tokens\n*/\n budget_tokens: number\n\n type: 'enabled'\n }\n | {\n type: 'disabled'\n }\n}\n\nexport interface AnthropicAdaptiveThinkingOptions {\n /**\n * Configuration for Claude's adaptive thinking (Opus 4.6+).\n *\n * In adaptive mode, Claude dynamically decides when and how much to think.\n * Use the effort parameter to control thinking depth.\n * `thinking: {type: \"enabled\"}` with `budget_tokens` is deprecated on Opus 4.6.\n */\n thinking?:\n | {\n type: 'adaptive'\n /**\n * Controls what (if any) thinking content is streamed back.\n *\n * - `'summarized'`: stream summarized thinking via `thinking_delta`\n * events (the user-visible reasoning text).\n * - `'omitted'`: stream the thinking block's `signature_delta` only\n * (no reasoning text reaches the client).\n *\n * On Claude Opus 4.6 the default is `'summarized'`. On\n * Claude Opus 4.7 the default flipped to `'omitted'` — callers\n * must set `'summarized'` explicitly to get the reasoning text.\n */\n display?: 'summarized' | 'omitted'\n }\n | {\n /**\n * @deprecated Use `type: 'adaptive'` with the effort parameter on Opus 4.6+.\n */\n budget_tokens: number\n type: 'enabled'\n }\n | {\n type: 'disabled'\n }\n}\n\nexport interface AnthropicEffortOptions {\n /**\n * Controls the thinking depth for adaptive thinking mode (Opus 4.6+).\n *\n * - `max`: Absolute highest capability\n * - `high`: Default - Claude will almost always think\n * - `medium`: Balanced cost-quality\n * - `low`: May skip thinking for simpler problems\n */\n effort?: 'max' | 'high' | 'medium' | 'low'\n}\n\nexport interface AnthropicOutputConfigOptions {\n /**\n * Output configuration for the model's response.\n *\n * On Claude 4.7+ the top-level `effort` field was relocated under\n * `output_config.effort`, and `thinking: { type: 'enabled', budget_tokens }`\n * was replaced by `thinking: { type: 'adaptive' }` paired with\n * `output_config.effort`. Earlier models continue to accept the legacy\n * top-level `effort` / `thinking.type: 'enabled'` shape.\n *\n * The engine also writes `output_config.format` here when the caller\n * passes `outputSchema` to a Claude 4.5+ adapter (issue #605 native\n * combined mode). Both fields coexist: user-supplied `effort` is\n * preserved when the engine adds `format`.\n */\n output_config?: {\n effort?: 'low' | 'medium' | 'high' | 'max' | null\n }\n}\n\nexport interface AnthropicToolChoiceOptions {\n tool_choice?: BetaToolChoiceAny | BetaToolChoiceTool | BetaToolChoiceAuto\n}\n\nexport interface AnthropicSamplingOptions {\n /**\n * Only sample from the top K options for each subsequent token.\n\nUsed to remove \"long tail\" low probability responses.\nRecommended for advanced use cases only. You usually only need to use temperature.\n\nRequired range: x >= 0\n */\n top_k?: number\n /**\n * Amount of randomness injected into the response.\n * Either use this or top_p, but not both.\n * Defaults to 1.0. Ranges from 0.0 to 1.0. Use temperature closer to 0.0 for analytical / multiple choice, and closer to 1.0 for creative and generative tasks.\n * @default 1.0\n */\n temperature?: number\n /**\n * Use nucleus sampling.\n *\n * In nucleus sampling, we compute the cumulative distribution over all the options for each subsequent token in decreasing probability order and cut it off once it reaches a particular probability specified by top_p. You should either alter temperature or top_p, but not both.\n */\n top_p?: number\n /**\n * The maximum number of tokens to generate before stopping. This parameter only specifies the absolute maximum number of tokens to generate. Required by the API; the adapter defaults to 1024 when omitted.\n * Range x >= 1.\n */\n max_tokens?: number\n}\n\nexport type ExternalTextProviderOptions = AnthropicContainerOptions &\n AnthropicContextManagementOptions &\n AnthropicMCPOptions &\n AnthropicServiceTierOptions &\n AnthropicStopSequencesOptions &\n AnthropicThinkingOptions &\n AnthropicToolChoiceOptions &\n AnthropicSamplingOptions &\n Partial<AnthropicAdaptiveThinkingOptions> &\n Partial<AnthropicEffortOptions> &\n Partial<AnthropicOutputConfigOptions>\n\nexport interface InternalTextProviderOptions extends ExternalTextProviderOptions {\n model: string\n\n messages: Array<MessageParam>\n\n /**\n * The maximum number of tokens to generate before stopping. This parameter only specifies the absolute maximum number of tokens to generate.\n * Range x >= 1.\n */\n max_tokens: number\n /**\n * Whether to incrementally stream the response using server-sent events.\n */\n stream?: boolean\n /**\n * System prompt — built by the adapter from the user-facing\n * `systemPrompts: Array<string>` on the chat call. This field is internal:\n * users should pass system prompts via `systemPrompts`, not via\n * `modelOptions`.\n *\n * A system prompt is a way of providing context and instructions to Claude,\n * such as specifying a particular goal or role.\n */\n system?: string | Array<TextBlockParam>\n\n tools?: Array<AnthropicTool>\n\n /**\n * Schema-constrained final answer in a single Messages request (issue\n * #605). Set by the engine when the adapter declared\n * `supportsCombinedToolsAndSchema` and a caller passed `outputSchema`\n * to `chat()`. The model emits tool calls during the agent loop and a\n * schema-matching JSON message on the natural final turn — no separate\n * finalization round-trip needed.\n *\n * The SDK type (`BetaOutputConfig`) currently exposes only `effort`;\n * `format` is accepted at runtime per the deprecation notice on the\n * older `output_format` field\n * (https://platform.claude.com/docs/en/build-with-claude/structured-outputs).\n * We type it explicitly here so the adapter call site doesn't need a\n * cast.\n */\n output_config?: {\n effort?: 'low' | 'medium' | 'high' | 'max' | null\n format?: {\n type: 'json_schema'\n schema: Record<string, unknown>\n }\n }\n}\n\nconst validateTopPandTemperature = (options: InternalTextProviderOptions) => {\n if (options.top_p !== undefined && options.temperature !== undefined) {\n throw new Error('You should either set top_p or temperature, but not both.')\n }\n}\n\nexport interface CacheControl {\n type: 'ephemeral'\n ttl: '5m' | '1h'\n}\n\nconst validateThinking = (options: InternalTextProviderOptions) => {\n const thinking = options.thinking\n if (thinking && thinking.type === 'enabled') {\n if (thinking.budget_tokens < 1024) {\n throw new Error('thinking.budget_tokens must be at least 1024.')\n }\n if (thinking.budget_tokens >= options.max_tokens) {\n throw new Error('thinking.budget_tokens must be less than max_tokens.')\n }\n }\n}\n\ninterface MCPServer {\n name: string\n url: string\n type: 'url'\n authorization_token?: string | null\n tool_configuration: {\n allowed_tools?: Array<string> | null\n enabled?: boolean | null\n } | null\n}\n\nconst validateMaxTokens = (options: InternalTextProviderOptions) => {\n if (options.max_tokens < 1) {\n throw new Error('max_tokens must be at least 1.')\n }\n}\n\nexport const validateTextProviderOptions = (\n options: InternalTextProviderOptions,\n) => {\n validateTopPandTemperature(options)\n validateThinking(options)\n validateMaxTokens(options)\n}\n"],"names":[],"mappings":"AA6RA,MAAM,6BAA6B,CAAC,YAAyC;AAC3E,MAAI,QAAQ,UAAU,UAAa,QAAQ,gBAAgB,QAAW;AACpE,UAAM,IAAI,MAAM,2DAA2D;AAAA,EAC7E;AACF;AAOA,MAAM,mBAAmB,CAAC,YAAyC;AACjE,QAAM,WAAW,QAAQ;AACzB,MAAI,YAAY,SAAS,SAAS,WAAW;AAC3C,QAAI,SAAS,gBAAgB,MAAM;AACjC,YAAM,IAAI,MAAM,+CAA+C;AAAA,IACjE;AACA,QAAI,SAAS,iBAAiB,QAAQ,YAAY;AAChD,YAAM,IAAI,MAAM,sDAAsD;AAAA,IACxE;AAAA,EACF;AACF;AAaA,MAAM,oBAAoB,CAAC,YAAyC;AAClE,MAAI,QAAQ,aAAa,GAAG;AAC1B,UAAM,IAAI,MAAM,gCAAgC;AAAA,EAClD;AACF;AAEO,MAAM,8BAA8B,CACzC,YACG;AACH,6BAA2B,OAAO;AAClC,mBAAiB,OAAO;AACxB,oBAAkB,OAAO;AAC3B;"}
|
|
@@ -3,6 +3,32 @@ import { ProviderTool, Tool } from '@tanstack/ai';
|
|
|
3
3
|
export type CodeExecutionToolConfig = BetaCodeExecutionTool20250522 | BetaCodeExecutionTool20250825;
|
|
4
4
|
/** @deprecated Renamed to `CodeExecutionToolConfig`. Will be removed in a future release. */
|
|
5
5
|
export type CodeExecutionTool = CodeExecutionToolConfig;
|
|
6
|
+
/**
|
|
7
|
+
* A hosted/managed Anthropic Skill reference. Lifted by the text adapter into
|
|
8
|
+
* the top-level `container.skills` request param (NOT serialized into the
|
|
9
|
+
* `tools[]` entry). Requires the `code_execution` tool to be enabled.
|
|
10
|
+
*/
|
|
11
|
+
export interface AnthropicContainerSkill {
|
|
12
|
+
/** 1–64 characters. */
|
|
13
|
+
skill_id: string;
|
|
14
|
+
type: 'anthropic' | 'custom';
|
|
15
|
+
/** Skill version, or `'latest'` (default) when omitted. */
|
|
16
|
+
version?: string;
|
|
17
|
+
}
|
|
18
|
+
export interface CodeExecutionToolOptions {
|
|
19
|
+
/** Hosted skills to load into the code-execution container (max 8). */
|
|
20
|
+
skills?: Array<AnthropicContainerSkill>;
|
|
21
|
+
}
|
|
6
22
|
export type AnthropicCodeExecutionTool = ProviderTool<'anthropic', 'code_execution'>;
|
|
7
23
|
export declare function convertCodeExecutionToolToAdapterFormat(tool: Tool): CodeExecutionToolConfig;
|
|
8
|
-
|
|
24
|
+
/**
|
|
25
|
+
* Reads the SDK tool config attached to a `code_execution` tool, if any.
|
|
26
|
+
* Used by the text adapter to select the version-aware code-execution beta.
|
|
27
|
+
*/
|
|
28
|
+
export declare function readCodeExecutionConfig(tool: Tool): CodeExecutionToolConfig | undefined;
|
|
29
|
+
/**
|
|
30
|
+
* Reads the hosted skills attached to a `code_execution` tool, if any.
|
|
31
|
+
* Used by the text adapter to build the top-level `container.skills` param.
|
|
32
|
+
*/
|
|
33
|
+
export declare function readCodeExecutionSkills(tool: Tool): Array<AnthropicContainerSkill> | undefined;
|
|
34
|
+
export declare function codeExecutionTool(config: CodeExecutionToolConfig, options?: CodeExecutionToolOptions): AnthropicCodeExecutionTool;
|
|
@@ -1,17 +1,39 @@
|
|
|
1
1
|
import { brandProviderTool } from "@tanstack/ai";
|
|
2
2
|
function convertCodeExecutionToolToAdapterFormat(tool) {
|
|
3
|
-
|
|
4
|
-
return metadata;
|
|
3
|
+
return readCodeExecutionConfig(tool);
|
|
5
4
|
}
|
|
6
|
-
function
|
|
5
|
+
function readCodeExecutionConfig(tool) {
|
|
6
|
+
return tool.metadata?.config;
|
|
7
|
+
}
|
|
8
|
+
function readCodeExecutionSkills(tool) {
|
|
9
|
+
return tool.metadata?.skills;
|
|
10
|
+
}
|
|
11
|
+
function codeExecutionTool(config, options = {}) {
|
|
12
|
+
const { skills } = options;
|
|
13
|
+
if (skills) {
|
|
14
|
+
if (skills.length > 8) {
|
|
15
|
+
throw new Error("code_execution supports at most 8 skills per request.");
|
|
16
|
+
}
|
|
17
|
+
for (const skill of skills) {
|
|
18
|
+
if (skill.skill_id.length < 1 || skill.skill_id.length > 64) {
|
|
19
|
+
throw new Error("skill_id must be between 1 and 64 characters.");
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
const metadata = {
|
|
24
|
+
config,
|
|
25
|
+
...skills && { skills }
|
|
26
|
+
};
|
|
7
27
|
return brandProviderTool({
|
|
8
28
|
name: "code_execution",
|
|
9
29
|
description: "",
|
|
10
|
-
metadata
|
|
30
|
+
metadata
|
|
11
31
|
});
|
|
12
32
|
}
|
|
13
33
|
export {
|
|
14
34
|
codeExecutionTool,
|
|
15
|
-
convertCodeExecutionToolToAdapterFormat
|
|
35
|
+
convertCodeExecutionToolToAdapterFormat,
|
|
36
|
+
readCodeExecutionConfig,
|
|
37
|
+
readCodeExecutionSkills
|
|
16
38
|
};
|
|
17
39
|
//# sourceMappingURL=code-execution-tool.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"code-execution-tool.js","sources":["../../../src/tools/code-execution-tool.ts"],"sourcesContent":["import { brandProviderTool } from '@tanstack/ai'\nimport type {\n BetaCodeExecutionTool20250522,\n BetaCodeExecutionTool20250825,\n} from '@anthropic-ai/sdk/resources/beta'\nimport type { ProviderTool, Tool } from '@tanstack/ai'\n\nexport type CodeExecutionToolConfig =\n | BetaCodeExecutionTool20250522\n | BetaCodeExecutionTool20250825\n\n/** @deprecated Renamed to `CodeExecutionToolConfig`. Will be removed in a future release. */\nexport type CodeExecutionTool = CodeExecutionToolConfig\n\nexport type AnthropicCodeExecutionTool = ProviderTool<\n 'anthropic',\n 'code_execution'\n>\n\nexport function convertCodeExecutionToolToAdapterFormat(\n tool: Tool,\n): CodeExecutionToolConfig {\n
|
|
1
|
+
{"version":3,"file":"code-execution-tool.js","sources":["../../../src/tools/code-execution-tool.ts"],"sourcesContent":["import { brandProviderTool } from '@tanstack/ai'\nimport type {\n BetaCodeExecutionTool20250522,\n BetaCodeExecutionTool20250825,\n} from '@anthropic-ai/sdk/resources/beta'\nimport type { ProviderTool, Tool } from '@tanstack/ai'\n\nexport type CodeExecutionToolConfig =\n | BetaCodeExecutionTool20250522\n | BetaCodeExecutionTool20250825\n\n/** @deprecated Renamed to `CodeExecutionToolConfig`. Will be removed in a future release. */\nexport type CodeExecutionTool = CodeExecutionToolConfig\n\n/**\n * A hosted/managed Anthropic Skill reference. Lifted by the text adapter into\n * the top-level `container.skills` request param (NOT serialized into the\n * `tools[]` entry). Requires the `code_execution` tool to be enabled.\n */\nexport interface AnthropicContainerSkill {\n /** 1–64 characters. */\n skill_id: string\n type: 'anthropic' | 'custom'\n /** Skill version, or `'latest'` (default) when omitted. */\n version?: string\n}\n\nexport interface CodeExecutionToolOptions {\n /** Hosted skills to load into the code-execution container (max 8). */\n skills?: Array<AnthropicContainerSkill>\n}\n\ninterface CodeExecutionToolMetadata {\n config: CodeExecutionToolConfig\n skills?: Array<AnthropicContainerSkill>\n}\n\nexport type AnthropicCodeExecutionTool = ProviderTool<\n 'anthropic',\n 'code_execution'\n>\n\nexport function convertCodeExecutionToolToAdapterFormat(\n tool: Tool,\n): CodeExecutionToolConfig {\n // The converter is only called for real `code_execution` tools, so a\n // non-undefined config is expected — but read via optional chaining so an\n // absent metadata object doesn't throw.\n return readCodeExecutionConfig(tool) as CodeExecutionToolConfig\n}\n\n/**\n * Reads the SDK tool config attached to a `code_execution` tool, if any.\n * Used by the text adapter to select the version-aware code-execution beta.\n */\nexport function readCodeExecutionConfig(\n tool: Tool,\n): CodeExecutionToolConfig | undefined {\n return (tool.metadata as CodeExecutionToolMetadata | undefined)?.config\n}\n\n/**\n * Reads the hosted skills attached to a `code_execution` tool, if any.\n * Used by the text adapter to build the top-level `container.skills` param.\n */\nexport function readCodeExecutionSkills(\n tool: Tool,\n): Array<AnthropicContainerSkill> | undefined {\n return (tool.metadata as CodeExecutionToolMetadata | undefined)?.skills\n}\n\nexport function codeExecutionTool(\n config: CodeExecutionToolConfig,\n options: CodeExecutionToolOptions = {},\n): AnthropicCodeExecutionTool {\n const { skills } = options\n if (skills) {\n if (skills.length > 8) {\n throw new Error('code_execution supports at most 8 skills per request.')\n }\n for (const skill of skills) {\n if (skill.skill_id.length < 1 || skill.skill_id.length > 64) {\n throw new Error('skill_id must be between 1 and 64 characters.')\n }\n }\n }\n const metadata: CodeExecutionToolMetadata = {\n config,\n ...(skills && { skills }),\n }\n return brandProviderTool<AnthropicCodeExecutionTool>({\n name: 'code_execution',\n description: '',\n metadata,\n })\n}\n"],"names":[],"mappings":";AA0CO,SAAS,wCACd,MACyB;AAIzB,SAAO,wBAAwB,IAAI;AACrC;AAMO,SAAS,wBACd,MACqC;AACrC,SAAQ,KAAK,UAAoD;AACnE;AAMO,SAAS,wBACd,MAC4C;AAC5C,SAAQ,KAAK,UAAoD;AACnE;AAEO,SAAS,kBACd,QACA,UAAoC,IACR;AAC5B,QAAM,EAAE,WAAW;AACnB,MAAI,QAAQ;AACV,QAAI,OAAO,SAAS,GAAG;AACrB,YAAM,IAAI,MAAM,uDAAuD;AAAA,IACzE;AACA,eAAW,SAAS,QAAQ;AAC1B,UAAI,MAAM,SAAS,SAAS,KAAK,MAAM,SAAS,SAAS,IAAI;AAC3D,cAAM,IAAI,MAAM,+CAA+C;AAAA,MACjE;AAAA,IACF;AAAA,EACF;AACA,QAAM,WAAsC;AAAA,IAC1C;AAAA,IACA,GAAI,UAAU,EAAE,OAAA;AAAA,EAAO;AAEzB,SAAO,kBAA8C;AAAA,IACnD,MAAM;AAAA,IACN,aAAa;AAAA,IACb;AAAA,EAAA,CACD;AACH;"}
|
|
@@ -7,7 +7,7 @@ import { TextEditorTool } from './text-editor-tool.js';
|
|
|
7
7
|
import { WebFetchTool } from './web-fetch-tool.js';
|
|
8
8
|
import { WebSearchTool } from './web-search-tool.js';
|
|
9
9
|
export { bashTool, type AnthropicBashTool, type BashToolConfig, type BashTool, } from './bash-tool.js';
|
|
10
|
-
export { codeExecutionTool, type AnthropicCodeExecutionTool, type CodeExecutionToolConfig, type CodeExecutionTool, } from './code-execution-tool.js';
|
|
10
|
+
export { codeExecutionTool, convertCodeExecutionToolToAdapterFormat, readCodeExecutionConfig, readCodeExecutionSkills, type AnthropicCodeExecutionTool, type AnthropicContainerSkill, type CodeExecutionToolConfig, type CodeExecutionToolOptions, type CodeExecutionTool, } from './code-execution-tool.js';
|
|
11
11
|
export { computerUseTool, type AnthropicComputerUseTool, type ComputerUseToolConfig, type ComputerUseTool, } from './computer-use-tool.js';
|
|
12
12
|
export { customTool, type CustomToolConfig, type CustomTool, } from './custom-tool.js';
|
|
13
13
|
export { memoryTool, type AnthropicMemoryTool, type MemoryToolConfig, type MemoryTool, } from './memory-tool.js';
|
package/dist/esm/tools/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { bashTool } from "./bash-tool.js";
|
|
2
|
-
import { codeExecutionTool } from "./code-execution-tool.js";
|
|
2
|
+
import { codeExecutionTool, convertCodeExecutionToolToAdapterFormat, readCodeExecutionConfig, readCodeExecutionSkills } from "./code-execution-tool.js";
|
|
3
3
|
import { computerUseTool } from "./computer-use-tool.js";
|
|
4
4
|
import { customTool } from "./custom-tool.js";
|
|
5
5
|
import { memoryTool } from "./memory-tool.js";
|
|
@@ -10,8 +10,11 @@ export {
|
|
|
10
10
|
bashTool,
|
|
11
11
|
codeExecutionTool,
|
|
12
12
|
computerUseTool,
|
|
13
|
+
convertCodeExecutionToolToAdapterFormat,
|
|
13
14
|
customTool,
|
|
14
15
|
memoryTool,
|
|
16
|
+
readCodeExecutionConfig,
|
|
17
|
+
readCodeExecutionSkills,
|
|
15
18
|
textEditorTool,
|
|
16
19
|
webFetchTool,
|
|
17
20
|
webSearchTool
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/ai-anthropic",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.15.0",
|
|
4
4
|
"description": "Anthropic Claude adapter for TanStack AI chat, tool calling, thinking, and structured outputs.",
|
|
5
5
|
"author": "",
|
|
6
6
|
"license": "MIT",
|
|
@@ -45,12 +45,12 @@
|
|
|
45
45
|
},
|
|
46
46
|
"peerDependencies": {
|
|
47
47
|
"zod": "^4.0.0",
|
|
48
|
-
"@tanstack/ai": "^0.
|
|
48
|
+
"@tanstack/ai": "^0.27.0"
|
|
49
49
|
},
|
|
50
50
|
"devDependencies": {
|
|
51
51
|
"@vitest/coverage-v8": "4.0.14",
|
|
52
52
|
"zod": "^4.2.0",
|
|
53
|
-
"@tanstack/ai": "0.
|
|
53
|
+
"@tanstack/ai": "0.27.0"
|
|
54
54
|
},
|
|
55
55
|
"scripts": {
|
|
56
56
|
"build": "vite build",
|
package/src/adapters/text.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { EventType, normalizeSystemPrompts } from '@tanstack/ai'
|
|
|
2
2
|
import { toRunErrorRawEvent } from '@tanstack/ai/adapter-internals'
|
|
3
3
|
import { BaseTextAdapter } from '@tanstack/ai/adapters'
|
|
4
4
|
import { convertToolsToProviderFormat } from '../tools/tool-converter'
|
|
5
|
+
import { readCodeExecutionConfig, readCodeExecutionSkills } from '../tools'
|
|
5
6
|
import { validateTextProviderOptions } from '../text/text-provider-options'
|
|
6
7
|
import { buildAnthropicUsage } from '../usage'
|
|
7
8
|
import {
|
|
@@ -36,6 +37,7 @@ import type {
|
|
|
36
37
|
import type Anthropic_SDK from '@anthropic-ai/sdk'
|
|
37
38
|
import type { AnthropicBeta } from '@anthropic-ai/sdk/resources/beta/beta'
|
|
38
39
|
import type {
|
|
40
|
+
AnyTool,
|
|
39
41
|
ContentPart,
|
|
40
42
|
Modality,
|
|
41
43
|
ModelMessage,
|
|
@@ -55,6 +57,60 @@ import type {
|
|
|
55
57
|
} from '../message-types'
|
|
56
58
|
import type { AnthropicClientConfig } from '../utils'
|
|
57
59
|
|
|
60
|
+
/**
|
|
61
|
+
* Computes the `betas` array for a Messages request. Unions:
|
|
62
|
+
* - `interleaved-thinking-2025-05-14` when interleaved thinking is enabled,
|
|
63
|
+
* - `code-execution-2025-08-25` when a `code_execution` tool is present,
|
|
64
|
+
* - `skills-2025-10-02` when that tool carries skills.
|
|
65
|
+
* Returns `undefined` when none apply (so the call site omits `betas`).
|
|
66
|
+
*/
|
|
67
|
+
export function computeAnthropicBetas(
|
|
68
|
+
tools: Array<AnyTool> | undefined,
|
|
69
|
+
modelOptions:
|
|
70
|
+
| {
|
|
71
|
+
thinking?: {
|
|
72
|
+
type?: 'enabled' | 'disabled' | 'adaptive'
|
|
73
|
+
budget_tokens?: number
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
| undefined,
|
|
77
|
+
): Array<AnthropicBeta> | undefined {
|
|
78
|
+
const betas = new Set<AnthropicBeta>()
|
|
79
|
+
|
|
80
|
+
const useInterleavedThinking =
|
|
81
|
+
modelOptions?.thinking?.type === 'enabled' &&
|
|
82
|
+
typeof modelOptions.thinking.budget_tokens === 'number' &&
|
|
83
|
+
modelOptions.thinking.budget_tokens > 0
|
|
84
|
+
if (useInterleavedThinking) betas.add('interleaved-thinking-2025-05-14')
|
|
85
|
+
|
|
86
|
+
// Code-execution beta is version-aware: select from the FIRST code_execution
|
|
87
|
+
// tool's config type.
|
|
88
|
+
const codeExecTool = tools?.find((t) => t.name === 'code_execution')
|
|
89
|
+
if (codeExecTool) {
|
|
90
|
+
const cfgType = readCodeExecutionConfig(codeExecTool)?.type
|
|
91
|
+
// Each code_execution tool version pairs with a specific beta. Known
|
|
92
|
+
// legacy variant maps explicitly; current/future variants (e.g.
|
|
93
|
+
// `code_execution_20250825` and later) use the latest `-08-25` beta.
|
|
94
|
+
betas.add(
|
|
95
|
+
cfgType === 'code_execution_20250522'
|
|
96
|
+
? 'code-execution-2025-05-22'
|
|
97
|
+
: 'code-execution-2025-08-25',
|
|
98
|
+
)
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// Skills beta: scan ALL code_execution tools so this AGREES with the
|
|
102
|
+
// container-lift, which lifts skills from any code_execution tool that
|
|
103
|
+
// carries them (not just the first).
|
|
104
|
+
const hasSkills = tools?.some(
|
|
105
|
+
(t) =>
|
|
106
|
+
t.name === 'code_execution' &&
|
|
107
|
+
(readCodeExecutionSkills(t)?.length ?? 0) > 0,
|
|
108
|
+
)
|
|
109
|
+
if (hasSkills) betas.add('skills-2025-10-02')
|
|
110
|
+
|
|
111
|
+
return betas.size > 0 ? Array.from(betas) : undefined
|
|
112
|
+
}
|
|
113
|
+
|
|
58
114
|
/**
|
|
59
115
|
* Configuration for Anthropic text adapter
|
|
60
116
|
*/
|
|
@@ -144,18 +200,8 @@ export class AnthropicTextAdapter<
|
|
|
144
200
|
)
|
|
145
201
|
|
|
146
202
|
// `betas` is attached at the call site rather than in the shared mapper
|
|
147
|
-
// because the
|
|
148
|
-
|
|
149
|
-
const modelOptions = options.modelOptions as
|
|
150
|
-
| InternalTextProviderOptions
|
|
151
|
-
| undefined
|
|
152
|
-
const useInterleavedThinking =
|
|
153
|
-
modelOptions?.thinking?.type === 'enabled' &&
|
|
154
|
-
typeof modelOptions.thinking.budget_tokens === 'number' &&
|
|
155
|
-
modelOptions.thinking.budget_tokens > 0
|
|
156
|
-
const betas: Array<AnthropicBeta> | undefined = useInterleavedThinking
|
|
157
|
-
? ['interleaved-thinking-2025-05-14']
|
|
158
|
-
: undefined
|
|
203
|
+
// because the beta set depends on both the tools and the modelOptions.
|
|
204
|
+
const betas = computeAnthropicBetas(options.tools, options.modelOptions)
|
|
159
205
|
|
|
160
206
|
// `client.beta.messages` is Anthropic's permanent staging surface, not a
|
|
161
207
|
// sunset path: it's a superset of `client.messages` that additionally
|
|
@@ -237,6 +283,10 @@ export class AnthropicTextAdapter<
|
|
|
237
283
|
`activity=chat provider=anthropic model=${this.model} messages=${chatOptions.messages.length} tools=${chatOptions.tools?.length ?? 0} stream=false`,
|
|
238
284
|
{ provider: 'anthropic', model: this.model },
|
|
239
285
|
)
|
|
286
|
+
const betas = computeAnthropicBetas(
|
|
287
|
+
chatOptions.tools,
|
|
288
|
+
chatOptions.modelOptions,
|
|
289
|
+
)
|
|
240
290
|
// Make non-streaming request with tool_choice forced to our structured output tool
|
|
241
291
|
const response = await this.client.beta.messages.create(
|
|
242
292
|
{
|
|
@@ -244,6 +294,7 @@ export class AnthropicTextAdapter<
|
|
|
244
294
|
stream: false,
|
|
245
295
|
tools: [structuredOutputTool],
|
|
246
296
|
tool_choice: { type: 'tool', name: 'structured_output' },
|
|
297
|
+
...(betas && { betas }),
|
|
247
298
|
},
|
|
248
299
|
{
|
|
249
300
|
signal: chatOptions.request?.signal,
|
|
@@ -302,9 +353,7 @@ export class AnthropicTextAdapter<
|
|
|
302
353
|
private mapCommonOptionsToAnthropic(
|
|
303
354
|
options: TextOptions<AnthropicTextProviderOptions>,
|
|
304
355
|
) {
|
|
305
|
-
const modelOptions = options.modelOptions
|
|
306
|
-
| InternalTextProviderOptions
|
|
307
|
-
| undefined
|
|
356
|
+
const modelOptions = options.modelOptions
|
|
308
357
|
|
|
309
358
|
const formattedMessages = this.formatMessages(options.messages)
|
|
310
359
|
const tools = options.tools
|
|
@@ -313,7 +362,7 @@ export class AnthropicTextAdapter<
|
|
|
313
362
|
|
|
314
363
|
const validProviderOptions: Partial<InternalTextProviderOptions> = {}
|
|
315
364
|
if (modelOptions) {
|
|
316
|
-
const validKeys: Array<keyof
|
|
365
|
+
const validKeys: Array<keyof AnthropicTextProviderOptions> = [
|
|
317
366
|
'container',
|
|
318
367
|
'context_management',
|
|
319
368
|
'effort',
|
|
@@ -324,10 +373,17 @@ export class AnthropicTextAdapter<
|
|
|
324
373
|
'thinking',
|
|
325
374
|
'tool_choice',
|
|
326
375
|
'top_k',
|
|
376
|
+
'temperature',
|
|
377
|
+
'top_p',
|
|
327
378
|
]
|
|
328
|
-
|
|
379
|
+
// `max_tokens` is a legitimate public modelOptions field, but it is read
|
|
380
|
+
// via a dedicated path (defaultMaxTokens below) rather than copied into
|
|
381
|
+
// validProviderOptions. Exempt it from the dropped-key warning here so a
|
|
382
|
+
// correct `modelOptions: { max_tokens }` call doesn't log a spurious
|
|
383
|
+
// "dropped unknown key" error, while keeping it out of the copy loop.
|
|
384
|
+
const droppedKeyExemptSet = new Set<string>([...validKeys, 'max_tokens'])
|
|
329
385
|
const droppedKeys = Object.keys(modelOptions).filter(
|
|
330
|
-
(key) => !
|
|
386
|
+
(key) => !droppedKeyExemptSet.has(key),
|
|
331
387
|
)
|
|
332
388
|
if (droppedKeys.length > 0) {
|
|
333
389
|
// Reachable when callers cast around the public type (e.g.
|
|
@@ -364,7 +420,7 @@ export class AnthropicTextAdapter<
|
|
|
364
420
|
validProviderOptions.thinking?.type === 'enabled'
|
|
365
421
|
? validProviderOptions.thinking.budget_tokens
|
|
366
422
|
: undefined
|
|
367
|
-
const defaultMaxTokens =
|
|
423
|
+
const defaultMaxTokens = modelOptions?.max_tokens ?? 1024
|
|
368
424
|
const maxTokens =
|
|
369
425
|
thinkingBudget && thinkingBudget >= defaultMaxTokens
|
|
370
426
|
? thinkingBudget + 1
|
|
@@ -409,18 +465,33 @@ export class AnthropicTextAdapter<
|
|
|
409
465
|
}
|
|
410
466
|
: undefined
|
|
411
467
|
|
|
412
|
-
// `
|
|
413
|
-
//
|
|
414
|
-
//
|
|
415
|
-
//
|
|
416
|
-
|
|
468
|
+
// Lift skills attached to a `code_execution` tool into the top-level
|
|
469
|
+
// `container.skills` request param (Anthropic's required shape). Preserve any
|
|
470
|
+
// `container.id` supplied via modelOptions for container reuse. This is the
|
|
471
|
+
// canonical path for skills; `modelOptions.container.skills` is deprecated.
|
|
472
|
+
const toolSkills = options.tools
|
|
473
|
+
?.map((tool) =>
|
|
474
|
+
tool.name === 'code_execution'
|
|
475
|
+
? readCodeExecutionSkills(tool)
|
|
476
|
+
: undefined,
|
|
477
|
+
)
|
|
478
|
+
.find((skills) => skills && skills.length > 0)
|
|
479
|
+
|
|
480
|
+
if (toolSkills && toolSkills.length > 0) {
|
|
481
|
+
const existingContainer = validProviderOptions.container ?? undefined
|
|
482
|
+
validProviderOptions.container = {
|
|
483
|
+
id: existingContainer?.id ?? null,
|
|
484
|
+
skills: toolSkills,
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
// `temperature`/`top_p` arrive via `...validProviderOptions` (sourced from
|
|
489
|
+
// `modelOptions`). `InternalTextProviderOptions` declares `system` and
|
|
490
|
+
// `tools` as `T?: ...` (no `| undefined`), so spread them conditionally
|
|
491
|
+
// rather than passing explicit `undefined` under exactOptionalPropertyTypes.
|
|
417
492
|
const requestParams: InternalTextProviderOptions = {
|
|
418
493
|
model: options.model,
|
|
419
494
|
max_tokens: maxTokens,
|
|
420
|
-
...(options.temperature !== undefined && {
|
|
421
|
-
temperature: options.temperature,
|
|
422
|
-
}),
|
|
423
|
-
...(options.topP !== undefined && { top_p: options.topP }),
|
|
424
495
|
messages: formattedMessages,
|
|
425
496
|
...(systemBlocks !== undefined && { system: systemBlocks }),
|
|
426
497
|
...(tools !== undefined && { tools }),
|
|
@@ -5,7 +5,7 @@ import type {
|
|
|
5
5
|
BetaToolChoiceTool,
|
|
6
6
|
} from '@anthropic-ai/sdk/resources/beta/messages/messages'
|
|
7
7
|
import type { CacheControlEphemeral } from '@anthropic-ai/sdk/resources'
|
|
8
|
-
import type { AnthropicTool } from '../tools'
|
|
8
|
+
import type { AnthropicContainerSkill, AnthropicTool } from '../tools'
|
|
9
9
|
import type {
|
|
10
10
|
MessageParam,
|
|
11
11
|
TextBlockParam,
|
|
@@ -49,20 +49,14 @@ export interface AnthropicContainerOptions {
|
|
|
49
49
|
container?: {
|
|
50
50
|
id: string | null
|
|
51
51
|
/**
|
|
52
|
-
* List of skills to load into the container
|
|
52
|
+
* List of skills to load into the container.
|
|
53
|
+
*
|
|
54
|
+
* @deprecated Configure skills on the `code_execution` tool instead:
|
|
55
|
+
* `codeExecutionTool(config, { skills })`. The adapter lifts those into
|
|
56
|
+
* `container.skills` and attaches the required beta headers. Setting
|
|
57
|
+
* skills here bypasses the beta-header wiring and may stop working.
|
|
53
58
|
*/
|
|
54
|
-
skills: Array<
|
|
55
|
-
/**
|
|
56
|
-
* Between 1-64 characters
|
|
57
|
-
*/
|
|
58
|
-
skill_id: string
|
|
59
|
-
|
|
60
|
-
type: 'anthropic' | 'custom'
|
|
61
|
-
/**
|
|
62
|
-
* Skill version or latest by default
|
|
63
|
-
*/
|
|
64
|
-
version?: string
|
|
65
|
-
}> | null
|
|
59
|
+
skills: Array<AnthropicContainerSkill> | null
|
|
66
60
|
} | null
|
|
67
61
|
}
|
|
68
62
|
|
|
@@ -206,6 +200,24 @@ Recommended for advanced use cases only. You usually only need to use temperatur
|
|
|
206
200
|
Required range: x >= 0
|
|
207
201
|
*/
|
|
208
202
|
top_k?: number
|
|
203
|
+
/**
|
|
204
|
+
* Amount of randomness injected into the response.
|
|
205
|
+
* Either use this or top_p, but not both.
|
|
206
|
+
* Defaults to 1.0. Ranges from 0.0 to 1.0. Use temperature closer to 0.0 for analytical / multiple choice, and closer to 1.0 for creative and generative tasks.
|
|
207
|
+
* @default 1.0
|
|
208
|
+
*/
|
|
209
|
+
temperature?: number
|
|
210
|
+
/**
|
|
211
|
+
* Use nucleus sampling.
|
|
212
|
+
*
|
|
213
|
+
* In nucleus sampling, we compute the cumulative distribution over all the options for each subsequent token in decreasing probability order and cut it off once it reaches a particular probability specified by top_p. You should either alter temperature or top_p, but not both.
|
|
214
|
+
*/
|
|
215
|
+
top_p?: number
|
|
216
|
+
/**
|
|
217
|
+
* The maximum number of tokens to generate before stopping. This parameter only specifies the absolute maximum number of tokens to generate. Required by the API; the adapter defaults to 1024 when omitted.
|
|
218
|
+
* Range x >= 1.
|
|
219
|
+
*/
|
|
220
|
+
max_tokens?: number
|
|
209
221
|
}
|
|
210
222
|
|
|
211
223
|
export type ExternalTextProviderOptions = AnthropicContainerOptions &
|
|
@@ -244,13 +256,6 @@ export interface InternalTextProviderOptions extends ExternalTextProviderOptions
|
|
|
244
256
|
* such as specifying a particular goal or role.
|
|
245
257
|
*/
|
|
246
258
|
system?: string | Array<TextBlockParam>
|
|
247
|
-
/**
|
|
248
|
-
* Amount of randomness injected into the response.
|
|
249
|
-
* Either use this or top_p, but not both.
|
|
250
|
-
* Defaults to 1.0. Ranges from 0.0 to 1.0. Use temperature closer to 0.0 for analytical / multiple choice, and closer to 1.0 for creative and generative tasks.
|
|
251
|
-
* @default 1.0
|
|
252
|
-
*/
|
|
253
|
-
temperature?: number
|
|
254
259
|
|
|
255
260
|
tools?: Array<AnthropicTool>
|
|
256
261
|
|
|
@@ -276,13 +281,6 @@ export interface InternalTextProviderOptions extends ExternalTextProviderOptions
|
|
|
276
281
|
schema: Record<string, unknown>
|
|
277
282
|
}
|
|
278
283
|
}
|
|
279
|
-
|
|
280
|
-
/**
|
|
281
|
-
* Use nucleus sampling.
|
|
282
|
-
|
|
283
|
-
In nucleus sampling, we compute the cumulative distribution over all the options for each subsequent token in decreasing probability order and cut it off once it reaches a particular probability specified by top_p. You should either alter temperature or top_p, but not both.
|
|
284
|
-
*/
|
|
285
|
-
top_p?: number
|
|
286
284
|
}
|
|
287
285
|
|
|
288
286
|
const validateTopPandTemperature = (options: InternalTextProviderOptions) => {
|
|
@@ -12,6 +12,29 @@ export type CodeExecutionToolConfig =
|
|
|
12
12
|
/** @deprecated Renamed to `CodeExecutionToolConfig`. Will be removed in a future release. */
|
|
13
13
|
export type CodeExecutionTool = CodeExecutionToolConfig
|
|
14
14
|
|
|
15
|
+
/**
|
|
16
|
+
* A hosted/managed Anthropic Skill reference. Lifted by the text adapter into
|
|
17
|
+
* the top-level `container.skills` request param (NOT serialized into the
|
|
18
|
+
* `tools[]` entry). Requires the `code_execution` tool to be enabled.
|
|
19
|
+
*/
|
|
20
|
+
export interface AnthropicContainerSkill {
|
|
21
|
+
/** 1–64 characters. */
|
|
22
|
+
skill_id: string
|
|
23
|
+
type: 'anthropic' | 'custom'
|
|
24
|
+
/** Skill version, or `'latest'` (default) when omitted. */
|
|
25
|
+
version?: string
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface CodeExecutionToolOptions {
|
|
29
|
+
/** Hosted skills to load into the code-execution container (max 8). */
|
|
30
|
+
skills?: Array<AnthropicContainerSkill>
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
interface CodeExecutionToolMetadata {
|
|
34
|
+
config: CodeExecutionToolConfig
|
|
35
|
+
skills?: Array<AnthropicContainerSkill>
|
|
36
|
+
}
|
|
37
|
+
|
|
15
38
|
export type AnthropicCodeExecutionTool = ProviderTool<
|
|
16
39
|
'anthropic',
|
|
17
40
|
'code_execution'
|
|
@@ -20,16 +43,54 @@ export type AnthropicCodeExecutionTool = ProviderTool<
|
|
|
20
43
|
export function convertCodeExecutionToolToAdapterFormat(
|
|
21
44
|
tool: Tool,
|
|
22
45
|
): CodeExecutionToolConfig {
|
|
23
|
-
|
|
24
|
-
|
|
46
|
+
// The converter is only called for real `code_execution` tools, so a
|
|
47
|
+
// non-undefined config is expected — but read via optional chaining so an
|
|
48
|
+
// absent metadata object doesn't throw.
|
|
49
|
+
return readCodeExecutionConfig(tool) as CodeExecutionToolConfig
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Reads the SDK tool config attached to a `code_execution` tool, if any.
|
|
54
|
+
* Used by the text adapter to select the version-aware code-execution beta.
|
|
55
|
+
*/
|
|
56
|
+
export function readCodeExecutionConfig(
|
|
57
|
+
tool: Tool,
|
|
58
|
+
): CodeExecutionToolConfig | undefined {
|
|
59
|
+
return (tool.metadata as CodeExecutionToolMetadata | undefined)?.config
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Reads the hosted skills attached to a `code_execution` tool, if any.
|
|
64
|
+
* Used by the text adapter to build the top-level `container.skills` param.
|
|
65
|
+
*/
|
|
66
|
+
export function readCodeExecutionSkills(
|
|
67
|
+
tool: Tool,
|
|
68
|
+
): Array<AnthropicContainerSkill> | undefined {
|
|
69
|
+
return (tool.metadata as CodeExecutionToolMetadata | undefined)?.skills
|
|
25
70
|
}
|
|
26
71
|
|
|
27
72
|
export function codeExecutionTool(
|
|
28
73
|
config: CodeExecutionToolConfig,
|
|
74
|
+
options: CodeExecutionToolOptions = {},
|
|
29
75
|
): AnthropicCodeExecutionTool {
|
|
76
|
+
const { skills } = options
|
|
77
|
+
if (skills) {
|
|
78
|
+
if (skills.length > 8) {
|
|
79
|
+
throw new Error('code_execution supports at most 8 skills per request.')
|
|
80
|
+
}
|
|
81
|
+
for (const skill of skills) {
|
|
82
|
+
if (skill.skill_id.length < 1 || skill.skill_id.length > 64) {
|
|
83
|
+
throw new Error('skill_id must be between 1 and 64 characters.')
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
const metadata: CodeExecutionToolMetadata = {
|
|
88
|
+
config,
|
|
89
|
+
...(skills && { skills }),
|
|
90
|
+
}
|
|
30
91
|
return brandProviderTool<AnthropicCodeExecutionTool>({
|
|
31
92
|
name: 'code_execution',
|
|
32
93
|
description: '',
|
|
33
|
-
metadata
|
|
94
|
+
metadata,
|
|
34
95
|
})
|
|
35
96
|
}
|
package/src/tools/index.ts
CHANGED
|
@@ -15,8 +15,13 @@ export {
|
|
|
15
15
|
} from './bash-tool'
|
|
16
16
|
export {
|
|
17
17
|
codeExecutionTool,
|
|
18
|
+
convertCodeExecutionToolToAdapterFormat,
|
|
19
|
+
readCodeExecutionConfig,
|
|
20
|
+
readCodeExecutionSkills,
|
|
18
21
|
type AnthropicCodeExecutionTool,
|
|
22
|
+
type AnthropicContainerSkill,
|
|
19
23
|
type CodeExecutionToolConfig,
|
|
24
|
+
type CodeExecutionToolOptions,
|
|
20
25
|
type CodeExecutionTool,
|
|
21
26
|
} from './code-execution-tool'
|
|
22
27
|
export {
|