Open History to see the last commit
1import {
2 brandAnthropicProviderTool,
3 getAnthropicProviderToolMetadata,
4} from './anthropic-provider-tool'
5import type {
6 BetaCodeExecutionTool20250522,
7 BetaCodeExecutionTool20250825,
8} from '@anthropic-ai/sdk/resources/beta'
9import { SkillLimitError } from '@tanstack/ai'
10import type { ProviderTool, Tool } from '@tanstack/ai'
11
12export type CodeExecutionToolConfig =
13 | BetaCodeExecutionTool20250522
14 | BetaCodeExecutionTool20250825
15
16/** @deprecated Renamed to `CodeExecutionToolConfig`. Will be removed in a future release. */
17export type CodeExecutionTool = CodeExecutionToolConfig
18
19/**
20 * A hosted/managed Anthropic Skill reference. Lifted by the text adapter into
21 * the top-level `container.skills` request param (NOT serialized into the
22 * `tools[]` entry). Requires the `code_execution` tool to be enabled.
23 */
24export interface AnthropicContainerSkill {
25 /** 1–64 characters. */
26 skill_id: string
27 type: 'anthropic' | 'custom'
28 /** Skill version, or `'latest'` (default) when omitted. */
29 version?: string
30}
31
32export interface CodeExecutionToolOptions {
33 /** Hosted skills to load into the code-execution container (max 8). */
34 skills?: Array<AnthropicContainerSkill>
35}
36
37interface CodeExecutionToolMetadata {
38 config: CodeExecutionToolConfig
39 skills?: Array<AnthropicContainerSkill>
40}
41
42export type AnthropicCodeExecutionTool = ProviderTool<
43 'anthropic',
44 'code_execution'
45>
46
47export function convertCodeExecutionToolToAdapterFormat(
48 tool: Tool,
49): CodeExecutionToolConfig {
50 // The converter is only called for real `code_execution` tools, so a
51 // non-undefined config is expected — but read via optional chaining so an
52 // absent metadata object doesn't throw.
53 return readCodeExecutionConfig(tool) as CodeExecutionToolConfig
54}
55
56/**
57 * Reads the SDK tool config attached to a `code_execution` tool, if any.
58 * Used by the text adapter to select the version-aware code-execution beta.
59 */
60export function readCodeExecutionConfig(
61 tool: Tool,
62): CodeExecutionToolConfig | undefined {
63 return (
64 getAnthropicProviderToolMetadata(tool) as
65 | CodeExecutionToolMetadata
66 | undefined
67 )?.config
68}
69
70/**
71 * Reads the hosted skills attached to a `code_execution` tool, if any.
72 * Used by the text adapter to build the top-level `container.skills` param.
73 */
74export function readCodeExecutionSkills(
75 tool: Tool,
76): Array<AnthropicContainerSkill> | undefined {
77 return (
78 getAnthropicProviderToolMetadata(tool) as
79 | CodeExecutionToolMetadata
80 | undefined
81 )?.skills
82}
83
84export function codeExecutionTool(
85 config: CodeExecutionToolConfig,
86 options: CodeExecutionToolOptions = {},
87): AnthropicCodeExecutionTool {
88 const { skills } = options
89 if (skills) {
90 if (skills.length > 8) {
91 throw new SkillLimitError({
92 provider: 'anthropic',
93 path: 'native',
94 limit: 'code_execution supports at most 8 skills per request',
95 allowed: 8,
96 actual: skills.length,
97 offending: skills.map((s) => s.skill_id),
98 })
99 }
100 for (const skill of skills) {
101 if (skill.skill_id.length < 1 || skill.skill_id.length > 64) {
102 throw new Error('skill_id must be between 1 and 64 characters.')
103 }
104 }
105 }
106 const metadata: CodeExecutionToolMetadata = {
107 config,
108 ...(skills && { skills }),
109 }
110 return brandAnthropicProviderTool<AnthropicCodeExecutionTool>(
111 {
112 name: 'code_execution',
113 description: '',
114 metadata,
115 },
116 'code_execution',
117 )
118}