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