Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

code-execution-tool.ts

96 lines3.0 KB

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