@tanstack/ai-anthropic 0.10.0 → 0.10.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -33,7 +33,7 @@ type ResolveToolCapabilities<TModel extends string> = TModel extends keyof Anthr
33
33
  export declare class AnthropicTextAdapter<TModel extends (typeof ANTHROPIC_MODELS)[number], TProviderOptions extends Record<string, any> = ResolveProviderOptions<TModel>, TInputModalities extends ReadonlyArray<Modality> = ResolveInputModalities<TModel>, TToolCapabilities extends ReadonlyArray<string> = ResolveToolCapabilities<TModel>> extends BaseTextAdapter<TModel, TProviderOptions, TInputModalities, AnthropicMessageMetadataByModality, TToolCapabilities, unknown, AnthropicSystemPromptMetadata> {
34
34
  readonly kind: "text";
35
35
  readonly name: "anthropic";
36
- private client;
36
+ private readonly client;
37
37
  constructor(config: AnthropicTextConfig, model: TModel);
38
38
  chatStream(options: TextOptions<TProviderOptions>): AsyncIterable<StreamChunk>;
39
39
  /**
@@ -4,10 +4,11 @@ import { convertToolsToProviderFormat } from "../tools/tool-converter.js";
4
4
  import { validateTextProviderOptions } from "../text/text-provider-options.js";
5
5
  import { createAnthropicClient, generateId, getAnthropicApiKeyFromEnv } from "../utils/client.js";
6
6
  class AnthropicTextAdapter extends BaseTextAdapter {
7
+ kind = "text";
8
+ name = "anthropic";
9
+ client;
7
10
  constructor(config, model) {
8
11
  super({}, model);
9
- this.kind = "text";
10
- this.name = "anthropic";
11
12
  this.client = createAnthropicClient(config);
12
13
  }
13
14
  async *chatStream(options) {
@@ -175,28 +176,31 @@ class AnthropicTextAdapter extends BaseTextAdapter {
175
176
  const thinkingBudget = validProviderOptions.thinking?.type === "enabled" ? validProviderOptions.thinking.budget_tokens : void 0;
176
177
  const defaultMaxTokens = options.maxTokens || 1024;
177
178
  const maxTokens = thinkingBudget && thinkingBudget >= defaultMaxTokens ? thinkingBudget + 1 : defaultMaxTokens;
179
+ const systemBlocks = (() => {
180
+ const normalized = normalizeSystemPrompts(
181
+ options.systemPrompts
182
+ );
183
+ if (normalized.length === 0) return void 0;
184
+ return normalized.map(
185
+ (p) => ({
186
+ type: "text",
187
+ text: p.content,
188
+ ...p.metadata?.cache_control && {
189
+ cache_control: p.metadata.cache_control
190
+ }
191
+ })
192
+ );
193
+ })();
178
194
  const requestParams = {
179
195
  model: options.model,
180
196
  max_tokens: maxTokens,
181
- temperature: options.temperature,
182
- top_p: options.topP,
197
+ ...options.temperature !== void 0 && {
198
+ temperature: options.temperature
199
+ },
200
+ ...options.topP !== void 0 && { top_p: options.topP },
183
201
  messages: formattedMessages,
184
- system: (() => {
185
- const normalized = normalizeSystemPrompts(
186
- options.systemPrompts
187
- );
188
- if (normalized.length === 0) return void 0;
189
- return normalized.map(
190
- (p) => ({
191
- type: "text",
192
- text: p.content,
193
- ...p.metadata?.cache_control && {
194
- cache_control: p.metadata.cache_control
195
- }
196
- })
197
- );
198
- })(),
199
- tools,
202
+ ...systemBlocks !== void 0 && { system: systemBlocks },
203
+ ...tools !== void 0 && { tools },
200
204
  ...validProviderOptions
201
205
  };
202
206
  validateTextProviderOptions(requestParams);
@@ -349,11 +353,12 @@ class AnthropicTextAdapter extends BaseTextAdapter {
349
353
  if (!thinkingParts?.length) return;
350
354
  for (const thinking of thinkingParts) {
351
355
  if (!thinking.signature) continue;
352
- contentBlocks.push({
356
+ const block = {
353
357
  type: "thinking",
354
358
  thinking: thinking.content,
355
359
  signature: thinking.signature
356
- });
360
+ };
361
+ contentBlocks.push(block);
357
362
  }
358
363
  }
359
364
  /**
@@ -505,7 +510,7 @@ class AnthropicTextAdapter extends BaseTextAdapter {
505
510
  delta,
506
511
  content: accumulatedContent
507
512
  };
508
- } else if (event.delta.type === "thinking_delta") {
513
+ } else if (event.delta.type === "thinking_delta" && reasoningMessageId) {
509
514
  const delta = event.delta.thinking;
510
515
  accumulatedThinking += delta;
511
516
  yield {
@@ -685,6 +690,11 @@ class AnthropicTextAdapter extends BaseTextAdapter {
685
690
  };
686
691
  break;
687
692
  }
693
+ case "stop_sequence":
694
+ case "end_turn":
695
+ case "pause_turn":
696
+ case "refusal":
697
+ case "model_context_window_exceeded":
688
698
  default: {
689
699
  yield {
690
700
  type: EventType.RUN_FINISHED,
@@ -1 +1 @@
1
- {"version":3,"file":"text.js","sources":["../../../src/adapters/text.ts"],"sourcesContent":["import { EventType, normalizeSystemPrompts } from '@tanstack/ai'\nimport { BaseTextAdapter } from '@tanstack/ai/adapters'\nimport { convertToolsToProviderFormat } from '../tools/tool-converter'\nimport { validateTextProviderOptions } from '../text/text-provider-options'\nimport {\n createAnthropicClient,\n generateId,\n getAnthropicApiKeyFromEnv,\n} from '../utils'\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 DocumentBlockParam,\n ImageBlockParam,\n MessageParam,\n TextBlockParam,\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\ntype AnthropicContentBlocks =\n Extract<MessageParam['content'], Array<unknown>> extends Array<infer Block>\n ? Array<Block>\n : never\ntype AnthropicContentBlock =\n AnthropicContentBlocks extends Array<infer Block> ? Block : never\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 readonly kind = 'text' as const\n readonly name = 'anthropic' as const\n\n private 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 // Interleaved thinking is only supported on the beta messages endpoint,\n // so the `betas` flag is attached here rather than in the shared mapper\n // (structuredOutput uses the non-beta endpoint which rejects `betas`).\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 const stream = await this.client.beta.messages.create(\n { ...requestParams, stream: true, ...(betas && { betas }) },\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 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 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.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 }\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 '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 const requestParams: InternalTextProviderOptions = {\n model: options.model,\n max_tokens: maxTokens,\n temperature: options.temperature,\n top_p: options.topP,\n messages: formattedMessages,\n system: (() => {\n const normalized =\n 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 tools: tools,\n ...validProviderOptions,\n }\n validateTextProviderOptions(requestParams)\n return requestParams\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 formattedMessages.push({\n role: 'user',\n content: [\n {\n type: 'tool_result',\n tool_use_id: message.toolCallId,\n content:\n typeof message.content === 'string' ? message.content : '',\n },\n ],\n })\n continue\n }\n\n if (role === 'assistant' && message.toolCalls?.length) {\n const contentBlocks: AnthropicContentBlocks = []\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: AnthropicContentBlock = {\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: AnthropicContentBlock = {\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: AnthropicContentBlocks = []\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: AnthropicContentBlocks,\n thinkingParts: ModelMessage['thinking'],\n ): void {\n if (!thinkingParts?.length) return\n\n for (const thinking of thinkingParts) {\n if (!thinking.signature) continue\n contentBlocks.push({\n type: 'thinking',\n thinking: thinking.content,\n signature: thinking.signature,\n } as unknown as AnthropicContentBlock)\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\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 === '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 (event.delta.type === 'thinking_delta') {\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 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 }\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 {\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: {\n promptTokens: event.usage.input_tokens || 0,\n completionTokens: event.usage.output_tokens || 0,\n totalTokens:\n (event.usage.input_tokens || 0) +\n (event.usage.output_tokens || 0),\n },\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 default: {\n yield {\n type: EventType.RUN_FINISHED,\n runId,\n threadId,\n model,\n timestamp: Date.now(),\n finishReason: 'stop',\n usage: {\n promptTokens: event.usage.input_tokens || 0,\n completionTokens: event.usage.output_tokens || 0,\n totalTokens:\n (event.usage.input_tokens || 0) +\n (event.usage.output_tokens || 0),\n },\n }\n }\n }\n }\n }\n }\n } catch (error: unknown) {\n const err = error as Error & { status?: number; code?: string }\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 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":";;;;;AA0GO,MAAM,6BAOH,gBAWR;AAAA,EAMA,YAAY,QAA6B,OAAe;AACtD,UAAM,CAAA,GAAI,KAAK;AANjB,SAAS,OAAO;AAChB,SAAS,OAAO;AAMd,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;A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A,cACA,SAAS;AACP,sBAAM;AAAA,kBACJ,MAAM,UAAU;AAAA,kBAChB;AAAA,kBACA;AAAA,kBACA;AAAA,kBACA,WAAW,KAAK,IAAA;AAAA,kBAChB,cAAc;AAAA,kBACd,OAAO;AAAA,oBACL,cAAc,MAAM,MAAM,gBAAgB;AAAA,oBAC1C,kBAAkB,MAAM,MAAM,iBAAiB;AAAA,oBAC/C,cACG,MAAM,MAAM,gBAAgB,MAC5B,MAAM,MAAM,iBAAiB;AAAA,kBAAA;AAAA,gBAClC;AAAA,cAEJ;AAAA,YAAA;AAAA,UAEJ;AAAA,QACF;AAAA,MACF;AAAA,IACF,SAAS,OAAgB;AACvB,YAAM,MAAM;AAEZ,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,QACnC,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 { BaseTextAdapter } from '@tanstack/ai/adapters'\nimport { convertToolsToProviderFormat } from '../tools/tool-converter'\nimport { validateTextProviderOptions } from '../text/text-provider-options'\nimport {\n createAnthropicClient,\n generateId,\n getAnthropicApiKeyFromEnv,\n} from '../utils'\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 // Interleaved thinking is only supported on the beta messages endpoint,\n // so the `betas` flag is attached here rather than in the shared mapper\n // (structuredOutput uses the non-beta endpoint which rejects `betas`).\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 const stream = await this.client.beta.messages.create(\n { ...requestParams, stream: true, ...(betas && { betas }) },\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 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 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.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 }\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 '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 // `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 }\n validateTextProviderOptions(requestParams)\n return requestParams\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 formattedMessages.push({\n role: 'user',\n content: [\n {\n type: 'tool_result',\n tool_use_id: message.toolCallId,\n content:\n typeof message.content === 'string' ? message.content : '',\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\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 === '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 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 }\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 {\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: {\n promptTokens: event.usage.input_tokens || 0,\n completionTokens: event.usage.output_tokens || 0,\n totalTokens:\n (event.usage.input_tokens || 0) +\n (event.usage.output_tokens || 0),\n },\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 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: {\n promptTokens: event.usage.input_tokens || 0,\n completionTokens: event.usage.output_tokens || 0,\n totalTokens:\n (event.usage.input_tokens || 0) +\n (event.usage.output_tokens || 0),\n },\n }\n }\n }\n }\n }\n }\n } catch (error: unknown) {\n const err = error as Error & { status?: number; code?: string }\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 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":";;;;;AAqGO,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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A,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,13 +1,14 @@
1
+ import { brandProviderTool } from "@tanstack/ai";
1
2
  function convertBashToolToAdapterFormat(tool) {
2
3
  const metadata = tool.metadata;
3
4
  return metadata;
4
5
  }
5
6
  function bashTool(config) {
6
- return {
7
+ return brandProviderTool({
7
8
  name: "bash",
8
9
  description: "",
9
10
  metadata: config
10
- };
11
+ });
11
12
  }
12
13
  export {
13
14
  bashTool,
@@ -1 +1 @@
1
- {"version":3,"file":"bash-tool.js","sources":["../../../src/tools/bash-tool.ts"],"sourcesContent":["import type {\n BetaToolBash20241022,\n BetaToolBash20250124,\n} from '@anthropic-ai/sdk/resources/beta'\nimport type { ProviderTool, Tool } from '@tanstack/ai'\n\nexport type BashToolConfig = BetaToolBash20241022 | BetaToolBash20250124\n\n/** @deprecated Renamed to `BashToolConfig`. Will be removed in a future release. */\nexport type BashTool = BashToolConfig\n\nexport type AnthropicBashTool = ProviderTool<'anthropic', 'bash'>\n\nexport function convertBashToolToAdapterFormat(tool: Tool): BashToolConfig {\n const metadata = tool.metadata as BashToolConfig\n return metadata\n}\n\nexport function bashTool(config: BashToolConfig): AnthropicBashTool {\n // Phantom-brand cast: '~provider'/'~toolKind' are type-only and never assigned at runtime.\n return {\n name: 'bash',\n description: '',\n metadata: config,\n } as unknown as AnthropicBashTool\n}\n"],"names":[],"mappings":"AAaO,SAAS,+BAA+B,MAA4B;AACzE,QAAM,WAAW,KAAK;AACtB,SAAO;AACT;AAEO,SAAS,SAAS,QAA2C;AAElE,SAAO;AAAA,IACL,MAAM;AAAA,IACN,aAAa;AAAA,IACb,UAAU;AAAA,EAAA;AAEd;"}
1
+ {"version":3,"file":"bash-tool.js","sources":["../../../src/tools/bash-tool.ts"],"sourcesContent":["import { brandProviderTool } from '@tanstack/ai'\nimport type {\n BetaToolBash20241022,\n BetaToolBash20250124,\n} from '@anthropic-ai/sdk/resources/beta'\nimport type { ProviderTool, Tool } from '@tanstack/ai'\n\nexport type BashToolConfig = BetaToolBash20241022 | BetaToolBash20250124\n\n/** @deprecated Renamed to `BashToolConfig`. Will be removed in a future release. */\nexport type BashTool = BashToolConfig\n\nexport type AnthropicBashTool = ProviderTool<'anthropic', 'bash'>\n\nexport function convertBashToolToAdapterFormat(tool: Tool): BashToolConfig {\n const metadata = tool.metadata as BashToolConfig\n return metadata\n}\n\nexport function bashTool(config: BashToolConfig): AnthropicBashTool {\n return brandProviderTool<AnthropicBashTool>({\n name: 'bash',\n description: '',\n metadata: config,\n })\n}\n"],"names":[],"mappings":";AAcO,SAAS,+BAA+B,MAA4B;AACzE,QAAM,WAAW,KAAK;AACtB,SAAO;AACT;AAEO,SAAS,SAAS,QAA2C;AAClE,SAAO,kBAAqC;AAAA,IAC1C,MAAM;AAAA,IACN,aAAa;AAAA,IACb,UAAU;AAAA,EAAA,CACX;AACH;"}
@@ -1,13 +1,14 @@
1
+ import { brandProviderTool } from "@tanstack/ai";
1
2
  function convertCodeExecutionToolToAdapterFormat(tool) {
2
3
  const metadata = tool.metadata;
3
4
  return metadata;
4
5
  }
5
6
  function codeExecutionTool(config) {
6
- return {
7
+ return brandProviderTool({
7
8
  name: "code_execution",
8
9
  description: "",
9
10
  metadata: config
10
- };
11
+ });
11
12
  }
12
13
  export {
13
14
  codeExecutionTool,
@@ -1 +1 @@
1
- {"version":3,"file":"code-execution-tool.js","sources":["../../../src/tools/code-execution-tool.ts"],"sourcesContent":["import 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 const metadata = tool.metadata as CodeExecutionToolConfig\n return metadata\n}\n\nexport function codeExecutionTool(\n config: CodeExecutionToolConfig,\n): AnthropicCodeExecutionTool {\n // Phantom-brand cast: '~provider'/'~toolKind' are type-only and never assigned at runtime.\n return {\n name: 'code_execution',\n description: '',\n metadata: config,\n } as unknown as AnthropicCodeExecutionTool\n}\n"],"names":[],"mappings":"AAkBO,SAAS,wCACd,MACyB;AACzB,QAAM,WAAW,KAAK;AACtB,SAAO;AACT;AAEO,SAAS,kBACd,QAC4B;AAE5B,SAAO;AAAA,IACL,MAAM;AAAA,IACN,aAAa;AAAA,IACb,UAAU;AAAA,EAAA;AAEd;"}
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 const metadata = tool.metadata as CodeExecutionToolConfig\n return metadata\n}\n\nexport function codeExecutionTool(\n config: CodeExecutionToolConfig,\n): AnthropicCodeExecutionTool {\n return brandProviderTool<AnthropicCodeExecutionTool>({\n name: 'code_execution',\n description: '',\n metadata: config,\n })\n}\n"],"names":[],"mappings":";AAmBO,SAAS,wCACd,MACyB;AACzB,QAAM,WAAW,KAAK;AACtB,SAAO;AACT;AAEO,SAAS,kBACd,QAC4B;AAC5B,SAAO,kBAA8C;AAAA,IACnD,MAAM;AAAA,IACN,aAAa;AAAA,IACb,UAAU;AAAA,EAAA,CACX;AACH;"}
@@ -1,13 +1,14 @@
1
+ import { brandProviderTool } from "@tanstack/ai";
1
2
  function convertComputerUseToolToAdapterFormat(tool) {
2
3
  const metadata = tool.metadata;
3
4
  return metadata;
4
5
  }
5
6
  function computerUseTool(config) {
6
- return {
7
+ return brandProviderTool({
7
8
  name: "computer",
8
9
  description: "",
9
10
  metadata: config
10
- };
11
+ });
11
12
  }
12
13
  export {
13
14
  computerUseTool,
@@ -1 +1 @@
1
- {"version":3,"file":"computer-use-tool.js","sources":["../../../src/tools/computer-use-tool.ts"],"sourcesContent":["import type {\n BetaToolComputerUse20241022,\n BetaToolComputerUse20250124,\n} from '@anthropic-ai/sdk/resources/beta'\nimport type { ProviderTool, Tool } from '@tanstack/ai'\n\nexport type ComputerUseToolConfig =\n | BetaToolComputerUse20241022\n | BetaToolComputerUse20250124\n\n/** @deprecated Renamed to `ComputerUseToolConfig`. Will be removed in a future release. */\nexport type ComputerUseTool = ComputerUseToolConfig\n\nexport type AnthropicComputerUseTool = ProviderTool<'anthropic', 'computer_use'>\n\nexport function convertComputerUseToolToAdapterFormat(\n tool: Tool,\n): ComputerUseToolConfig {\n const metadata = tool.metadata as ComputerUseToolConfig\n return metadata\n}\n\nexport function computerUseTool(\n config: ComputerUseToolConfig,\n): AnthropicComputerUseTool {\n // Phantom-brand cast: '~provider'/'~toolKind' are type-only and never assigned at runtime.\n return {\n name: 'computer',\n description: '',\n metadata: config,\n } as unknown as AnthropicComputerUseTool\n}\n"],"names":[],"mappings":"AAeO,SAAS,sCACd,MACuB;AACvB,QAAM,WAAW,KAAK;AACtB,SAAO;AACT;AAEO,SAAS,gBACd,QAC0B;AAE1B,SAAO;AAAA,IACL,MAAM;AAAA,IACN,aAAa;AAAA,IACb,UAAU;AAAA,EAAA;AAEd;"}
1
+ {"version":3,"file":"computer-use-tool.js","sources":["../../../src/tools/computer-use-tool.ts"],"sourcesContent":["import { brandProviderTool } from '@tanstack/ai'\nimport type {\n BetaToolComputerUse20241022,\n BetaToolComputerUse20250124,\n} from '@anthropic-ai/sdk/resources/beta'\nimport type { ProviderTool, Tool } from '@tanstack/ai'\n\nexport type ComputerUseToolConfig =\n | BetaToolComputerUse20241022\n | BetaToolComputerUse20250124\n\n/** @deprecated Renamed to `ComputerUseToolConfig`. Will be removed in a future release. */\nexport type ComputerUseTool = ComputerUseToolConfig\n\nexport type AnthropicComputerUseTool = ProviderTool<'anthropic', 'computer_use'>\n\nexport function convertComputerUseToolToAdapterFormat(\n tool: Tool,\n): ComputerUseToolConfig {\n const metadata = tool.metadata as ComputerUseToolConfig\n return metadata\n}\n\nexport function computerUseTool(\n config: ComputerUseToolConfig,\n): AnthropicComputerUseTool {\n return brandProviderTool<AnthropicComputerUseTool>({\n name: 'computer',\n description: '',\n metadata: config,\n })\n}\n"],"names":[],"mappings":";AAgBO,SAAS,sCACd,MACuB;AACvB,QAAM,WAAW,KAAK;AACtB,SAAO;AACT;AAEO,SAAS,gBACd,QAC0B;AAC1B,SAAO,kBAA4C;AAAA,IACjD,MAAM;AAAA,IACN,aAAa;AAAA,IACb,UAAU;AAAA,EAAA,CACX;AACH;"}
@@ -1,3 +1,4 @@
1
+ import { brandProviderTool } from "@tanstack/ai";
1
2
  function convertMemoryToolToAdapterFormat(tool) {
2
3
  const metadata = tool.metadata;
3
4
  return {
@@ -6,11 +7,11 @@ function convertMemoryToolToAdapterFormat(tool) {
6
7
  };
7
8
  }
8
9
  function memoryTool(config) {
9
- return {
10
+ return brandProviderTool({
10
11
  name: "memory",
11
12
  description: "",
12
13
  metadata: config
13
- };
14
+ });
14
15
  }
15
16
  export {
16
17
  convertMemoryToolToAdapterFormat,
@@ -1 +1 @@
1
- {"version":3,"file":"memory-tool.js","sources":["../../../src/tools/memory-tool.ts"],"sourcesContent":["import type { BetaMemoryTool20250818 } from '@anthropic-ai/sdk/resources/beta'\nimport type { ProviderTool, Tool } from '@tanstack/ai'\n\nexport type MemoryToolConfig = BetaMemoryTool20250818\n\n/** @deprecated Renamed to `MemoryToolConfig`. Will be removed in a future release. */\nexport type MemoryTool = MemoryToolConfig\n\nexport type AnthropicMemoryTool = ProviderTool<'anthropic', 'memory'>\n\nexport function convertMemoryToolToAdapterFormat(tool: Tool): MemoryToolConfig {\n const metadata = tool.metadata as Omit<MemoryToolConfig, 'type'>\n return {\n type: 'memory_20250818',\n ...metadata,\n }\n}\n\nexport function memoryTool(config?: MemoryToolConfig): AnthropicMemoryTool {\n // Phantom-brand cast: '~provider'/'~toolKind' are type-only and never assigned at runtime.\n return {\n name: 'memory',\n description: '',\n metadata: config,\n } as unknown as AnthropicMemoryTool\n}\n"],"names":[],"mappings":"AAUO,SAAS,iCAAiC,MAA8B;AAC7E,QAAM,WAAW,KAAK;AACtB,SAAO;AAAA,IACL,MAAM;AAAA,IACN,GAAG;AAAA,EAAA;AAEP;AAEO,SAAS,WAAW,QAAgD;AAEzE,SAAO;AAAA,IACL,MAAM;AAAA,IACN,aAAa;AAAA,IACb,UAAU;AAAA,EAAA;AAEd;"}
1
+ {"version":3,"file":"memory-tool.js","sources":["../../../src/tools/memory-tool.ts"],"sourcesContent":["import { brandProviderTool } from '@tanstack/ai'\nimport type { BetaMemoryTool20250818 } from '@anthropic-ai/sdk/resources/beta'\nimport type { ProviderTool, Tool } from '@tanstack/ai'\n\nexport type MemoryToolConfig = BetaMemoryTool20250818\n\n/** @deprecated Renamed to `MemoryToolConfig`. Will be removed in a future release. */\nexport type MemoryTool = MemoryToolConfig\n\nexport type AnthropicMemoryTool = ProviderTool<'anthropic', 'memory'>\n\nexport function convertMemoryToolToAdapterFormat(tool: Tool): MemoryToolConfig {\n const metadata = tool.metadata as Omit<MemoryToolConfig, 'type'>\n return {\n type: 'memory_20250818',\n ...metadata,\n }\n}\n\nexport function memoryTool(config?: MemoryToolConfig): AnthropicMemoryTool {\n return brandProviderTool<AnthropicMemoryTool>({\n name: 'memory',\n description: '',\n metadata: config,\n })\n}\n"],"names":[],"mappings":";AAWO,SAAS,iCAAiC,MAA8B;AAC7E,QAAM,WAAW,KAAK;AACtB,SAAO;AAAA,IACL,MAAM;AAAA,IACN,GAAG;AAAA,EAAA;AAEP;AAEO,SAAS,WAAW,QAAgD;AACzE,SAAO,kBAAuC;AAAA,IAC5C,MAAM;AAAA,IACN,aAAa;AAAA,IACb,UAAU;AAAA,EAAA,CACX;AACH;"}
@@ -1,3 +1,4 @@
1
+ import { brandProviderTool } from "@tanstack/ai";
1
2
  function convertTextEditorToolToAdapterFormat(tool) {
2
3
  const metadata = tool.metadata;
3
4
  return {
@@ -5,11 +6,11 @@ function convertTextEditorToolToAdapterFormat(tool) {
5
6
  };
6
7
  }
7
8
  function textEditorTool(config) {
8
- return {
9
+ return brandProviderTool({
9
10
  name: "str_replace_editor",
10
11
  description: "",
11
12
  metadata: config
12
- };
13
+ });
13
14
  }
14
15
  export {
15
16
  convertTextEditorToolToAdapterFormat,
@@ -1 +1 @@
1
- {"version":3,"file":"text-editor-tool.js","sources":["../../../src/tools/text-editor-tool.ts"],"sourcesContent":["import type {\n ToolTextEditor20250124,\n ToolTextEditor20250429,\n ToolTextEditor20250728,\n} from '@anthropic-ai/sdk/resources/messages'\nimport type { ProviderTool, Tool } from '@tanstack/ai'\n\nexport type TextEditorToolConfig =\n | ToolTextEditor20250124\n | ToolTextEditor20250429\n | ToolTextEditor20250728\n\n/** @deprecated Renamed to `TextEditorToolConfig`. Will be removed in a future release. */\nexport type TextEditorTool = TextEditorToolConfig\n\nexport type AnthropicTextEditorTool = ProviderTool<'anthropic', 'text_editor'>\n\nexport function convertTextEditorToolToAdapterFormat(\n tool: Tool,\n): TextEditorToolConfig {\n const metadata = tool.metadata as TextEditorToolConfig\n return {\n ...metadata,\n }\n}\n\nexport function textEditorTool<T extends TextEditorToolConfig>(\n config: T,\n): AnthropicTextEditorTool {\n // Phantom-brand cast: '~provider'/'~toolKind' are type-only and never assigned at runtime.\n return {\n name: 'str_replace_editor',\n description: '',\n metadata: config,\n } as unknown as AnthropicTextEditorTool\n}\n"],"names":[],"mappings":"AAiBO,SAAS,qCACd,MACsB;AACtB,QAAM,WAAW,KAAK;AACtB,SAAO;AAAA,IACL,GAAG;AAAA,EAAA;AAEP;AAEO,SAAS,eACd,QACyB;AAEzB,SAAO;AAAA,IACL,MAAM;AAAA,IACN,aAAa;AAAA,IACb,UAAU;AAAA,EAAA;AAEd;"}
1
+ {"version":3,"file":"text-editor-tool.js","sources":["../../../src/tools/text-editor-tool.ts"],"sourcesContent":["import { brandProviderTool } from '@tanstack/ai'\nimport type {\n ToolTextEditor20250124,\n ToolTextEditor20250429,\n ToolTextEditor20250728,\n} from '@anthropic-ai/sdk/resources/messages'\nimport type { ProviderTool, Tool } from '@tanstack/ai'\n\nexport type TextEditorToolConfig =\n | ToolTextEditor20250124\n | ToolTextEditor20250429\n | ToolTextEditor20250728\n\n/** @deprecated Renamed to `TextEditorToolConfig`. Will be removed in a future release. */\nexport type TextEditorTool = TextEditorToolConfig\n\nexport type AnthropicTextEditorTool = ProviderTool<'anthropic', 'text_editor'>\n\nexport function convertTextEditorToolToAdapterFormat(\n tool: Tool,\n): TextEditorToolConfig {\n const metadata = tool.metadata as TextEditorToolConfig\n return {\n ...metadata,\n }\n}\n\nexport function textEditorTool<T extends TextEditorToolConfig>(\n config: T,\n): AnthropicTextEditorTool {\n return brandProviderTool<AnthropicTextEditorTool>({\n name: 'str_replace_editor',\n description: '',\n metadata: config,\n })\n}\n"],"names":[],"mappings":";AAkBO,SAAS,qCACd,MACsB;AACtB,QAAM,WAAW,KAAK;AACtB,SAAO;AAAA,IACL,GAAG;AAAA,EAAA;AAEP;AAEO,SAAS,eACd,QACyB;AACzB,SAAO,kBAA2C;AAAA,IAChD,MAAM;AAAA,IACN,aAAa;AAAA,IACb,UAAU;AAAA,EAAA,CACX;AACH;"}
@@ -1,3 +1,4 @@
1
+ import { brandProviderTool } from "@tanstack/ai";
1
2
  function convertWebFetchToolToAdapterFormat(tool) {
2
3
  const metadata = tool.metadata;
3
4
  return {
@@ -7,11 +8,11 @@ function convertWebFetchToolToAdapterFormat(tool) {
7
8
  };
8
9
  }
9
10
  function webFetchTool(config) {
10
- return {
11
+ return brandProviderTool({
11
12
  name: "web_fetch",
12
13
  description: "",
13
14
  metadata: config
14
- };
15
+ });
15
16
  }
16
17
  export {
17
18
  convertWebFetchToolToAdapterFormat,
@@ -1 +1 @@
1
- {"version":3,"file":"web-fetch-tool.js","sources":["../../../src/tools/web-fetch-tool.ts"],"sourcesContent":["import type { BetaWebFetchTool20250910 } from '@anthropic-ai/sdk/resources/beta'\nimport type { ProviderTool, Tool } from '@tanstack/ai'\n\nexport type WebFetchToolConfig = BetaWebFetchTool20250910\n\n/** @deprecated Renamed to `WebFetchToolConfig`. Will be removed in a future release. */\nexport type WebFetchTool = WebFetchToolConfig\n\nexport type AnthropicWebFetchTool = ProviderTool<'anthropic', 'web_fetch'>\n\nexport function convertWebFetchToolToAdapterFormat(\n tool: Tool,\n): WebFetchToolConfig {\n const metadata = tool.metadata as Omit<WebFetchToolConfig, 'type' | 'name'>\n return {\n name: 'web_fetch',\n type: 'web_fetch_20250910',\n ...metadata,\n }\n}\n\nexport function webFetchTool(\n config?: Omit<WebFetchToolConfig, 'type' | 'name'>,\n): AnthropicWebFetchTool {\n // Phantom-brand cast: '~provider'/'~toolKind' are type-only and never assigned at runtime.\n return {\n name: 'web_fetch',\n description: '',\n metadata: config,\n } as unknown as AnthropicWebFetchTool\n}\n"],"names":[],"mappings":"AAUO,SAAS,mCACd,MACoB;AACpB,QAAM,WAAW,KAAK;AACtB,SAAO;AAAA,IACL,MAAM;AAAA,IACN,MAAM;AAAA,IACN,GAAG;AAAA,EAAA;AAEP;AAEO,SAAS,aACd,QACuB;AAEvB,SAAO;AAAA,IACL,MAAM;AAAA,IACN,aAAa;AAAA,IACb,UAAU;AAAA,EAAA;AAEd;"}
1
+ {"version":3,"file":"web-fetch-tool.js","sources":["../../../src/tools/web-fetch-tool.ts"],"sourcesContent":["import { brandProviderTool } from '@tanstack/ai'\nimport type { BetaWebFetchTool20250910 } from '@anthropic-ai/sdk/resources/beta'\nimport type { ProviderTool, Tool } from '@tanstack/ai'\n\nexport type WebFetchToolConfig = BetaWebFetchTool20250910\n\n/** @deprecated Renamed to `WebFetchToolConfig`. Will be removed in a future release. */\nexport type WebFetchTool = WebFetchToolConfig\n\nexport type AnthropicWebFetchTool = ProviderTool<'anthropic', 'web_fetch'>\n\nexport function convertWebFetchToolToAdapterFormat(\n tool: Tool,\n): WebFetchToolConfig {\n const metadata = tool.metadata as Omit<WebFetchToolConfig, 'type' | 'name'>\n return {\n name: 'web_fetch',\n type: 'web_fetch_20250910',\n ...metadata,\n }\n}\n\nexport function webFetchTool(\n config?: Omit<WebFetchToolConfig, 'type' | 'name'>,\n): AnthropicWebFetchTool {\n return brandProviderTool<AnthropicWebFetchTool>({\n name: 'web_fetch',\n description: '',\n metadata: config,\n })\n}\n"],"names":[],"mappings":";AAWO,SAAS,mCACd,MACoB;AACpB,QAAM,WAAW,KAAK;AACtB,SAAO;AAAA,IACL,MAAM;AAAA,IACN,MAAM;AAAA,IACN,GAAG;AAAA,EAAA;AAEP;AAEO,SAAS,aACd,QACuB;AACvB,SAAO,kBAAyC;AAAA,IAC9C,MAAM;AAAA,IACN,aAAa;AAAA,IACb,UAAU;AAAA,EAAA,CACX;AACH;"}
@@ -1,3 +1,4 @@
1
+ import { brandProviderTool } from "@tanstack/ai";
1
2
  const validateDomains = (tool) => {
2
3
  if (tool.allowed_domains && tool.blocked_domains) {
3
4
  throw new Error(
@@ -33,21 +34,27 @@ function convertWebSearchToolToAdapterFormat(tool) {
33
34
  return {
34
35
  name: "web_search",
35
36
  type: "web_search_20250305",
36
- allowed_domains: metadata.allowedDomains,
37
- blocked_domains: metadata.blockedDomains,
38
- max_uses: metadata.maxUses,
39
- user_location: metadata.userLocation,
37
+ ...metadata.allowedDomains !== void 0 && {
38
+ allowed_domains: metadata.allowedDomains
39
+ },
40
+ ...metadata.blockedDomains !== void 0 && {
41
+ blocked_domains: metadata.blockedDomains
42
+ },
43
+ ...metadata.maxUses !== void 0 && { max_uses: metadata.maxUses },
44
+ ...metadata.userLocation !== void 0 && {
45
+ user_location: metadata.userLocation
46
+ },
40
47
  cache_control: metadata.cacheControl || null
41
48
  };
42
49
  }
43
50
  function webSearchTool(config) {
44
51
  validateDomains(config);
45
52
  validateUserLocation(config);
46
- return {
53
+ return brandProviderTool({
47
54
  name: "web_search",
48
55
  description: "",
49
56
  metadata: config
50
- };
57
+ });
51
58
  }
52
59
  export {
53
60
  convertWebSearchToolToAdapterFormat,
@@ -1 +1 @@
1
- {"version":3,"file":"web-search-tool.js","sources":["../../../src/tools/web-search-tool.ts"],"sourcesContent":["import type { WebSearchTool20250305 } from '@anthropic-ai/sdk/resources/messages'\nimport type { CacheControl } from '../text/text-provider-options'\nimport type { ProviderTool, Tool } from '@tanstack/ai'\n\nexport type WebSearchToolConfig = WebSearchTool20250305\n\n/** @deprecated Renamed to `WebSearchToolConfig`. Will be removed in a future release. */\nexport type WebSearchTool = WebSearchToolConfig\n\nexport type AnthropicWebSearchTool = ProviderTool<'anthropic', 'web_search'>\n\nconst validateDomains = (tool: WebSearchToolConfig) => {\n if (tool.allowed_domains && tool.blocked_domains) {\n throw new Error(\n 'allowed_domains and blocked_domains cannot be used together.',\n )\n }\n}\n\nconst validateUserLocation = (tool: WebSearchToolConfig) => {\n const userLocation = tool.user_location\n if (userLocation) {\n if (\n userLocation.city &&\n (userLocation.city.length < 1 || userLocation.city.length > 255)\n ) {\n throw new Error(\n 'user_location.city must be between 1 and 255 characters.',\n )\n }\n if (userLocation.country && userLocation.country.length !== 2) {\n throw new Error('user_location.country must be exactly 2 characters.')\n }\n if (\n userLocation.region &&\n (userLocation.region.length < 1 || userLocation.region.length > 255)\n ) {\n throw new Error(\n 'user_location.region must be between 1 and 255 characters.',\n )\n }\n if (\n userLocation.timezone &&\n (userLocation.timezone.length < 1 || userLocation.timezone.length > 255)\n ) {\n throw new Error(\n 'user_location.timezone must be between 1 and 255 characters.',\n )\n }\n }\n}\n\nexport function convertWebSearchToolToAdapterFormat(\n tool: Tool,\n): WebSearchToolConfig {\n const metadata = tool.metadata as {\n allowedDomains?: Array<string> | null\n blockedDomains?: Array<string> | null\n maxUses?: number | null\n userLocation?: {\n type: 'approximate'\n city?: string | null\n country?: string | null\n region?: string | null\n timezone?: string | null\n } | null\n cacheControl?: CacheControl | null\n }\n return {\n name: 'web_search',\n type: 'web_search_20250305',\n allowed_domains: metadata.allowedDomains,\n blocked_domains: metadata.blockedDomains,\n max_uses: metadata.maxUses,\n user_location: metadata.userLocation,\n cache_control: metadata.cacheControl || null,\n }\n}\n\nexport function webSearchTool(\n config: WebSearchToolConfig,\n): AnthropicWebSearchTool {\n validateDomains(config)\n validateUserLocation(config)\n // Phantom-brand cast: '~provider'/'~toolKind' are type-only and never assigned at runtime.\n return {\n name: 'web_search',\n description: '',\n metadata: config,\n } as unknown as AnthropicWebSearchTool\n}\n"],"names":[],"mappings":"AAWA,MAAM,kBAAkB,CAAC,SAA8B;AACrD,MAAI,KAAK,mBAAmB,KAAK,iBAAiB;AAChD,UAAM,IAAI;AAAA,MACR;AAAA,IAAA;AAAA,EAEJ;AACF;AAEA,MAAM,uBAAuB,CAAC,SAA8B;AAC1D,QAAM,eAAe,KAAK;AAC1B,MAAI,cAAc;AAChB,QACE,aAAa,SACZ,aAAa,KAAK,SAAS,KAAK,aAAa,KAAK,SAAS,MAC5D;AACA,YAAM,IAAI;AAAA,QACR;AAAA,MAAA;AAAA,IAEJ;AACA,QAAI,aAAa,WAAW,aAAa,QAAQ,WAAW,GAAG;AAC7D,YAAM,IAAI,MAAM,qDAAqD;AAAA,IACvE;AACA,QACE,aAAa,WACZ,aAAa,OAAO,SAAS,KAAK,aAAa,OAAO,SAAS,MAChE;AACA,YAAM,IAAI;AAAA,QACR;AAAA,MAAA;AAAA,IAEJ;AACA,QACE,aAAa,aACZ,aAAa,SAAS,SAAS,KAAK,aAAa,SAAS,SAAS,MACpE;AACA,YAAM,IAAI;AAAA,QACR;AAAA,MAAA;AAAA,IAEJ;AAAA,EACF;AACF;AAEO,SAAS,oCACd,MACqB;AACrB,QAAM,WAAW,KAAK;AAatB,SAAO;AAAA,IACL,MAAM;AAAA,IACN,MAAM;AAAA,IACN,iBAAiB,SAAS;AAAA,IAC1B,iBAAiB,SAAS;AAAA,IAC1B,UAAU,SAAS;AAAA,IACnB,eAAe,SAAS;AAAA,IACxB,eAAe,SAAS,gBAAgB;AAAA,EAAA;AAE5C;AAEO,SAAS,cACd,QACwB;AACxB,kBAAgB,MAAM;AACtB,uBAAqB,MAAM;AAE3B,SAAO;AAAA,IACL,MAAM;AAAA,IACN,aAAa;AAAA,IACb,UAAU;AAAA,EAAA;AAEd;"}
1
+ {"version":3,"file":"web-search-tool.js","sources":["../../../src/tools/web-search-tool.ts"],"sourcesContent":["import { brandProviderTool } from '@tanstack/ai'\nimport type { WebSearchTool20250305 } from '@anthropic-ai/sdk/resources/messages'\nimport type { CacheControl } from '../text/text-provider-options'\nimport type { ProviderTool, Tool } from '@tanstack/ai'\n\nexport type WebSearchToolConfig = WebSearchTool20250305\n\n/** @deprecated Renamed to `WebSearchToolConfig`. Will be removed in a future release. */\nexport type WebSearchTool = WebSearchToolConfig\n\nexport type AnthropicWebSearchTool = ProviderTool<'anthropic', 'web_search'>\n\nconst validateDomains = (tool: WebSearchToolConfig) => {\n if (tool.allowed_domains && tool.blocked_domains) {\n throw new Error(\n 'allowed_domains and blocked_domains cannot be used together.',\n )\n }\n}\n\nconst validateUserLocation = (tool: WebSearchToolConfig) => {\n const userLocation = tool.user_location\n if (userLocation) {\n if (\n userLocation.city &&\n (userLocation.city.length < 1 || userLocation.city.length > 255)\n ) {\n throw new Error(\n 'user_location.city must be between 1 and 255 characters.',\n )\n }\n if (userLocation.country && userLocation.country.length !== 2) {\n throw new Error('user_location.country must be exactly 2 characters.')\n }\n if (\n userLocation.region &&\n (userLocation.region.length < 1 || userLocation.region.length > 255)\n ) {\n throw new Error(\n 'user_location.region must be between 1 and 255 characters.',\n )\n }\n if (\n userLocation.timezone &&\n (userLocation.timezone.length < 1 || userLocation.timezone.length > 255)\n ) {\n throw new Error(\n 'user_location.timezone must be between 1 and 255 characters.',\n )\n }\n }\n}\n\nexport function convertWebSearchToolToAdapterFormat(\n tool: Tool,\n): WebSearchToolConfig {\n const metadata = tool.metadata as {\n allowedDomains?: Array<string> | null\n blockedDomains?: Array<string> | null\n maxUses?: number | null\n userLocation?: {\n type: 'approximate'\n city?: string | null\n country?: string | null\n region?: string | null\n timezone?: string | null\n } | null\n cacheControl?: CacheControl | null\n }\n // Vendor `WebSearchTool20250305` declares these fields as `T | null`\n // (no `| undefined`) under exactOptionalPropertyTypes, so spread them\n // conditionally rather than passing explicit `undefined`.\n return {\n name: 'web_search',\n type: 'web_search_20250305',\n ...(metadata.allowedDomains !== undefined && {\n allowed_domains: metadata.allowedDomains,\n }),\n ...(metadata.blockedDomains !== undefined && {\n blocked_domains: metadata.blockedDomains,\n }),\n ...(metadata.maxUses !== undefined && { max_uses: metadata.maxUses }),\n ...(metadata.userLocation !== undefined && {\n user_location: metadata.userLocation,\n }),\n cache_control: metadata.cacheControl || null,\n }\n}\n\nexport function webSearchTool(\n config: WebSearchToolConfig,\n): AnthropicWebSearchTool {\n validateDomains(config)\n validateUserLocation(config)\n return brandProviderTool<AnthropicWebSearchTool>({\n name: 'web_search',\n description: '',\n metadata: config,\n })\n}\n"],"names":[],"mappings":";AAYA,MAAM,kBAAkB,CAAC,SAA8B;AACrD,MAAI,KAAK,mBAAmB,KAAK,iBAAiB;AAChD,UAAM,IAAI;AAAA,MACR;AAAA,IAAA;AAAA,EAEJ;AACF;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",
3
- "version": "0.10.0",
3
+ "version": "0.10.1",
4
4
  "description": "Anthropic Claude adapter for TanStack AI",
5
5
  "author": "",
6
6
  "license": "MIT",
@@ -39,12 +39,12 @@
39
39
  },
40
40
  "peerDependencies": {
41
41
  "zod": "^4.0.0",
42
- "@tanstack/ai": "^0.20.0"
42
+ "@tanstack/ai": "^0.20.1"
43
43
  },
44
44
  "devDependencies": {
45
45
  "@vitest/coverage-v8": "4.0.14",
46
46
  "zod": "^4.2.0",
47
- "@tanstack/ai": "0.20.0"
47
+ "@tanstack/ai": "0.20.1"
48
48
  },
49
49
  "scripts": {
50
50
  "build": "vite build",
@@ -21,10 +21,12 @@ import type { InternalLogger } from '@tanstack/ai/adapter-internals'
21
21
  import type {
22
22
  Base64ImageSource,
23
23
  Base64PDFSource,
24
+ ContentBlockParam,
24
25
  DocumentBlockParam,
25
26
  ImageBlockParam,
26
- MessageParam,
27
27
  TextBlockParam,
28
+ ThinkingBlockParam,
29
+ ToolUseBlockParam,
28
30
  URLImageSource,
29
31
  URLPDFSource,
30
32
  } from '@anthropic-ai/sdk/resources/messages'
@@ -60,13 +62,6 @@ export interface AnthropicTextConfig extends AnthropicClientConfig {}
60
62
  */
61
63
  export type AnthropicTextProviderOptions = ExternalTextProviderOptions
62
64
 
63
- type AnthropicContentBlocks =
64
- Extract<MessageParam['content'], Array<unknown>> extends Array<infer Block>
65
- ? Array<Block>
66
- : never
67
- type AnthropicContentBlock =
68
- AnthropicContentBlocks extends Array<infer Block> ? Block : never
69
-
70
65
  // ===========================
71
66
  // Type Resolution Helpers
72
67
  // ===========================
@@ -123,10 +118,10 @@ export class AnthropicTextAdapter<
123
118
  // chat() call site so users get `cache_control` autocomplete.
124
119
  AnthropicSystemPromptMetadata
125
120
  > {
126
- readonly kind = 'text' as const
121
+ override readonly kind = 'text' as const
127
122
  readonly name = 'anthropic' as const
128
123
 
129
- private client: Anthropic_SDK
124
+ private readonly client: Anthropic_SDK
130
125
 
131
126
  constructor(config: AnthropicTextConfig, model: TModel) {
132
127
  super({}, model)
@@ -356,29 +351,40 @@ export class AnthropicTextAdapter<
356
351
  ? thinkingBudget + 1
357
352
  : defaultMaxTokens
358
353
 
354
+ // `InternalTextProviderOptions.system` is typed
355
+ // `string | Array<TextBlockParam>` (no `| undefined`), so build it
356
+ // outside the literal and spread it conditionally rather than
357
+ // assigning `undefined` under exactOptionalPropertyTypes.
358
+ const systemBlocks = ((): Array<TextBlockParam> | undefined => {
359
+ const normalized = normalizeSystemPrompts<AnthropicSystemPromptMetadata>(
360
+ options.systemPrompts,
361
+ )
362
+ if (normalized.length === 0) return undefined
363
+ return normalized.map(
364
+ (p): TextBlockParam => ({
365
+ type: 'text',
366
+ text: p.content,
367
+ ...(p.metadata?.cache_control && {
368
+ cache_control: p.metadata.cache_control,
369
+ }),
370
+ }),
371
+ )
372
+ })()
373
+ // `InternalTextProviderOptions` declares `temperature`, `top_p`,
374
+ // and `tools` as `T?: ...` (no `| undefined`), so spread them
375
+ // conditionally rather than passing explicit `undefined` from the
376
+ // optional common `TextOptions` fields under
377
+ // exactOptionalPropertyTypes.
359
378
  const requestParams: InternalTextProviderOptions = {
360
379
  model: options.model,
361
380
  max_tokens: maxTokens,
362
- temperature: options.temperature,
363
- top_p: options.topP,
381
+ ...(options.temperature !== undefined && {
382
+ temperature: options.temperature,
383
+ }),
384
+ ...(options.topP !== undefined && { top_p: options.topP }),
364
385
  messages: formattedMessages,
365
- system: (() => {
366
- const normalized =
367
- normalizeSystemPrompts<AnthropicSystemPromptMetadata>(
368
- options.systemPrompts,
369
- )
370
- if (normalized.length === 0) return undefined
371
- return normalized.map(
372
- (p): TextBlockParam => ({
373
- type: 'text',
374
- text: p.content,
375
- ...(p.metadata?.cache_control && {
376
- cache_control: p.metadata.cache_control,
377
- }),
378
- }),
379
- )
380
- })(),
381
- tools: tools,
386
+ ...(systemBlocks !== undefined && { system: systemBlocks }),
387
+ ...(tools !== undefined && { tools }),
382
388
  ...validProviderOptions,
383
389
  }
384
390
  validateTextProviderOptions(requestParams)
@@ -478,14 +484,14 @@ export class AnthropicTextAdapter<
478
484
  }
479
485
 
480
486
  if (role === 'assistant' && message.toolCalls?.length) {
481
- const contentBlocks: AnthropicContentBlocks = []
487
+ const contentBlocks: Array<ContentBlockParam> = []
482
488
 
483
489
  this.appendThinkingBlocks(contentBlocks, message.thinking)
484
490
 
485
491
  if (message.content) {
486
492
  const content =
487
493
  typeof message.content === 'string' ? message.content : ''
488
- const textBlock: AnthropicContentBlock = {
494
+ const textBlock: TextBlockParam = {
489
495
  type: 'text',
490
496
  text: content,
491
497
  }
@@ -503,7 +509,7 @@ export class AnthropicTextAdapter<
503
509
  parsedInput = toolCall.function.arguments
504
510
  }
505
511
 
506
- const toolUseBlock: AnthropicContentBlock = {
512
+ const toolUseBlock: ToolUseBlockParam = {
507
513
  type: 'tool_use',
508
514
  id: toolCall.id,
509
515
  name: toolCall.function.name,
@@ -521,7 +527,7 @@ export class AnthropicTextAdapter<
521
527
  }
522
528
 
523
529
  if (role === 'assistant') {
524
- const contentBlocks: AnthropicContentBlocks = []
530
+ const contentBlocks: Array<ContentBlockParam> = []
525
531
  this.appendThinkingBlocks(contentBlocks, message.thinking)
526
532
 
527
533
  if (Array.isArray(message.content)) {
@@ -573,18 +579,19 @@ export class AnthropicTextAdapter<
573
579
  }
574
580
 
575
581
  private appendThinkingBlocks(
576
- contentBlocks: AnthropicContentBlocks,
582
+ contentBlocks: Array<ContentBlockParam>,
577
583
  thinkingParts: ModelMessage['thinking'],
578
584
  ): void {
579
585
  if (!thinkingParts?.length) return
580
586
 
581
587
  for (const thinking of thinkingParts) {
582
588
  if (!thinking.signature) continue
583
- contentBlocks.push({
589
+ const block: ThinkingBlockParam = {
584
590
  type: 'thinking',
585
591
  thinking: thinking.content,
586
592
  signature: thinking.signature,
587
- } as unknown as AnthropicContentBlock)
593
+ }
594
+ contentBlocks.push(block)
588
595
  }
589
596
  }
590
597
 
@@ -782,14 +789,17 @@ export class AnthropicTextAdapter<
782
789
  delta,
783
790
  content: accumulatedContent,
784
791
  }
785
- } else if (event.delta.type === 'thinking_delta') {
792
+ } else if (
793
+ event.delta.type === 'thinking_delta' &&
794
+ reasoningMessageId
795
+ ) {
786
796
  const delta = event.delta.thinking
787
797
  accumulatedThinking += delta
788
798
 
789
799
  // Spec REASONING content event
790
800
  yield {
791
801
  type: EventType.REASONING_MESSAGE_CONTENT,
792
- messageId: reasoningMessageId!,
802
+ messageId: reasoningMessageId,
793
803
  delta,
794
804
  model,
795
805
  timestamp: Date.now(),
@@ -992,7 +1002,16 @@ export class AnthropicTextAdapter<
992
1002
  }
993
1003
  break
994
1004
  }
1005
+ case 'stop_sequence':
1006
+ case 'end_turn':
1007
+ case 'pause_turn':
1008
+ case 'refusal':
1009
+ case 'model_context_window_exceeded':
995
1010
  default: {
1011
+ // All remaining Anthropic stop_reason variants map to the
1012
+ // generic "stop" finish reason — they describe *why* the
1013
+ // stream ended, but for AG-UI consumers the resulting event
1014
+ // shape is identical.
996
1015
  yield {
997
1016
  type: EventType.RUN_FINISHED,
998
1017
  runId,
@@ -1,3 +1,4 @@
1
+ import { brandProviderTool } from '@tanstack/ai'
1
2
  import type {
2
3
  BetaToolBash20241022,
3
4
  BetaToolBash20250124,
@@ -17,10 +18,9 @@ export function convertBashToolToAdapterFormat(tool: Tool): BashToolConfig {
17
18
  }
18
19
 
19
20
  export function bashTool(config: BashToolConfig): AnthropicBashTool {
20
- // Phantom-brand cast: '~provider'/'~toolKind' are type-only and never assigned at runtime.
21
- return {
21
+ return brandProviderTool<AnthropicBashTool>({
22
22
  name: 'bash',
23
23
  description: '',
24
24
  metadata: config,
25
- } as unknown as AnthropicBashTool
25
+ })
26
26
  }
@@ -1,3 +1,4 @@
1
+ import { brandProviderTool } from '@tanstack/ai'
1
2
  import type {
2
3
  BetaCodeExecutionTool20250522,
3
4
  BetaCodeExecutionTool20250825,
@@ -26,10 +27,9 @@ export function convertCodeExecutionToolToAdapterFormat(
26
27
  export function codeExecutionTool(
27
28
  config: CodeExecutionToolConfig,
28
29
  ): AnthropicCodeExecutionTool {
29
- // Phantom-brand cast: '~provider'/'~toolKind' are type-only and never assigned at runtime.
30
- return {
30
+ return brandProviderTool<AnthropicCodeExecutionTool>({
31
31
  name: 'code_execution',
32
32
  description: '',
33
33
  metadata: config,
34
- } as unknown as AnthropicCodeExecutionTool
34
+ })
35
35
  }
@@ -1,3 +1,4 @@
1
+ import { brandProviderTool } from '@tanstack/ai'
1
2
  import type {
2
3
  BetaToolComputerUse20241022,
3
4
  BetaToolComputerUse20250124,
@@ -23,10 +24,9 @@ export function convertComputerUseToolToAdapterFormat(
23
24
  export function computerUseTool(
24
25
  config: ComputerUseToolConfig,
25
26
  ): AnthropicComputerUseTool {
26
- // Phantom-brand cast: '~provider'/'~toolKind' are type-only and never assigned at runtime.
27
- return {
27
+ return brandProviderTool<AnthropicComputerUseTool>({
28
28
  name: 'computer',
29
29
  description: '',
30
30
  metadata: config,
31
- } as unknown as AnthropicComputerUseTool
31
+ })
32
32
  }
@@ -1,3 +1,4 @@
1
+ import { brandProviderTool } from '@tanstack/ai'
1
2
  import type { BetaMemoryTool20250818 } from '@anthropic-ai/sdk/resources/beta'
2
3
  import type { ProviderTool, Tool } from '@tanstack/ai'
3
4
 
@@ -17,10 +18,9 @@ export function convertMemoryToolToAdapterFormat(tool: Tool): MemoryToolConfig {
17
18
  }
18
19
 
19
20
  export function memoryTool(config?: MemoryToolConfig): AnthropicMemoryTool {
20
- // Phantom-brand cast: '~provider'/'~toolKind' are type-only and never assigned at runtime.
21
- return {
21
+ return brandProviderTool<AnthropicMemoryTool>({
22
22
  name: 'memory',
23
23
  description: '',
24
24
  metadata: config,
25
- } as unknown as AnthropicMemoryTool
25
+ })
26
26
  }
@@ -1,3 +1,4 @@
1
+ import { brandProviderTool } from '@tanstack/ai'
1
2
  import type {
2
3
  ToolTextEditor20250124,
3
4
  ToolTextEditor20250429,
@@ -27,10 +28,9 @@ export function convertTextEditorToolToAdapterFormat(
27
28
  export function textEditorTool<T extends TextEditorToolConfig>(
28
29
  config: T,
29
30
  ): AnthropicTextEditorTool {
30
- // Phantom-brand cast: '~provider'/'~toolKind' are type-only and never assigned at runtime.
31
- return {
31
+ return brandProviderTool<AnthropicTextEditorTool>({
32
32
  name: 'str_replace_editor',
33
33
  description: '',
34
34
  metadata: config,
35
- } as unknown as AnthropicTextEditorTool
35
+ })
36
36
  }
@@ -1,3 +1,4 @@
1
+ import { brandProviderTool } from '@tanstack/ai'
1
2
  import type { BetaWebFetchTool20250910 } from '@anthropic-ai/sdk/resources/beta'
2
3
  import type { ProviderTool, Tool } from '@tanstack/ai'
3
4
 
@@ -22,10 +23,9 @@ export function convertWebFetchToolToAdapterFormat(
22
23
  export function webFetchTool(
23
24
  config?: Omit<WebFetchToolConfig, 'type' | 'name'>,
24
25
  ): AnthropicWebFetchTool {
25
- // Phantom-brand cast: '~provider'/'~toolKind' are type-only and never assigned at runtime.
26
- return {
26
+ return brandProviderTool<AnthropicWebFetchTool>({
27
27
  name: 'web_fetch',
28
28
  description: '',
29
29
  metadata: config,
30
- } as unknown as AnthropicWebFetchTool
30
+ })
31
31
  }
@@ -1,3 +1,4 @@
1
+ import { brandProviderTool } from '@tanstack/ai'
1
2
  import type { WebSearchTool20250305 } from '@anthropic-ai/sdk/resources/messages'
2
3
  import type { CacheControl } from '../text/text-provider-options'
3
4
  import type { ProviderTool, Tool } from '@tanstack/ai'
@@ -66,13 +67,22 @@ export function convertWebSearchToolToAdapterFormat(
66
67
  } | null
67
68
  cacheControl?: CacheControl | null
68
69
  }
70
+ // Vendor `WebSearchTool20250305` declares these fields as `T | null`
71
+ // (no `| undefined`) under exactOptionalPropertyTypes, so spread them
72
+ // conditionally rather than passing explicit `undefined`.
69
73
  return {
70
74
  name: 'web_search',
71
75
  type: 'web_search_20250305',
72
- allowed_domains: metadata.allowedDomains,
73
- blocked_domains: metadata.blockedDomains,
74
- max_uses: metadata.maxUses,
75
- user_location: metadata.userLocation,
76
+ ...(metadata.allowedDomains !== undefined && {
77
+ allowed_domains: metadata.allowedDomains,
78
+ }),
79
+ ...(metadata.blockedDomains !== undefined && {
80
+ blocked_domains: metadata.blockedDomains,
81
+ }),
82
+ ...(metadata.maxUses !== undefined && { max_uses: metadata.maxUses }),
83
+ ...(metadata.userLocation !== undefined && {
84
+ user_location: metadata.userLocation,
85
+ }),
76
86
  cache_control: metadata.cacheControl || null,
77
87
  }
78
88
  }
@@ -82,10 +92,9 @@ export function webSearchTool(
82
92
  ): AnthropicWebSearchTool {
83
93
  validateDomains(config)
84
94
  validateUserLocation(config)
85
- // Phantom-brand cast: '~provider'/'~toolKind' are type-only and never assigned at runtime.
86
- return {
95
+ return brandProviderTool<AnthropicWebSearchTool>({
87
96
  name: 'web_search',
88
97
  description: '',
89
98
  metadata: config,
90
- } as unknown as AnthropicWebSearchTool
99
+ })
91
100
  }