Open History to see the last commit
1import { brandProviderTool } from '@tanstack/ai'
2import { getComputerUseToolMetadata } from '@tanstack/ai/adapter-internals'
3import { ANTHROPIC_MODEL_TOOLS } from '../model-meta'
4import { getAnthropicProviderToolMetadata } from './anthropic-provider-tool'
5import type { ComputerUseToolMetadata } from '@tanstack/ai/adapter-internals'
6import type {
7 BetaComputerToolset20260801,
8 BetaToolComputerUse20241022,
9 BetaToolComputerUse20250124,
10 BetaToolComputerUse20251124,
11} from '@anthropic-ai/sdk/resources/beta'
12import type { ProviderTool, Tool } from '@tanstack/ai'
13
14/** The single `computer` tool that sends its action in `input.action`. */
15export type ComputerUseSingleToolConfig =
16 | BetaToolComputerUse20241022
17 | BetaToolComputerUse20250124
18 | BetaToolComputerUse20251124
19
20/**
21 * The computer toolset: one entry with no name. The model calls each action
22 * as its own tool (`left_click`, `screenshot`, …).
23 */
24export type ComputerToolsetConfig = BetaComputerToolset20260801
25
26export type ComputerUseToolConfig =
27 | ComputerUseSingleToolConfig
28 | ComputerToolsetConfig
29
30/** @deprecated Renamed to `ComputerUseToolConfig`. Will be removed in a future release. */
31export type ComputerUseTool = ComputerUseToolConfig
32
33/** `computer_20241022` or `computer_20250124`. */
34export type AnthropicComputerUseTool = ProviderTool<'anthropic', 'computer_use'>
35
36/** `computer_20251124`. */
37export type AnthropicComputerUse20251124Tool = ProviderTool<
38 'anthropic',
39 'computer_use_20251124'
40>
41
42/** `computer_toolset_20260801`. */
43export type AnthropicComputerToolset = ProviderTool<
44 'anthropic',
45 'computer_toolset'
46>
47
48/** The name of the tool that every computer use version runs as. */
49export const COMPUTER_TOOL_NAME = 'computer'
50
51export function convertComputerUseToolToAdapterFormat(
52 tool: Tool,
53): ComputerUseToolConfig {
54 const metadata = getAnthropicProviderToolMetadata(
55 tool,
56 ) as ComputerUseToolConfig
57 return metadata
58}
59
60/** True for the `computer_toolset_20260801` entry. */
61export function isComputerToolset(tool: Tool) {
62 return tool.metadata?.['type'] === 'computer_toolset_20260801'
63}
64
65/**
66 * One toolset call (`left_click` with `{ coordinate }`) as one `computer`
67 * call: `{ action: 'left_click', coordinate }`. So one `execute` works for
68 * every version.
69 */
70export function joinComputerToolsetCall(member: string, input: object) {
71 return { action: member, ...input }
72}
73
74/** A `computer` call as the toolset call that the model sent. */
75export function splitComputerToolsetCall(input: object) {
76 if (!('action' in input) || typeof input.action !== 'string') {
77 return undefined
78 }
79 const { action, ...memberInput } = input
80 return { name: action, input: memberInput }
81}
82
83/**
84 * Computer use. Every version runs as one tool named `computer`, with the
85 * action in `input.action`:
86 *
87 * - `computer_toolset_20260801`: Claude Opus 5.5, Sonnet 5.5, Haiku 5.5, and
88 * the other models that support the toolset. No beta header.
89 * - `computer_20251124`: the earlier tool of Claude Opus 4.5 to Claude Opus 5,
90 * Sonnet 4.6, and Sonnet 5.
91 * - `computer_20250124`: Claude Sonnet 4.5, Haiku 4.5, and Opus 4.1.
92 */
93export function computerUseTool(
94 config: ComputerToolsetConfig,
95): AnthropicComputerToolset
96export function computerUseTool(
97 config: BetaToolComputerUse20251124,
98): AnthropicComputerUse20251124Tool
99export function computerUseTool(
100 config: BetaToolComputerUse20241022 | BetaToolComputerUse20250124,
101): AnthropicComputerUseTool
102export function computerUseTool(config: ComputerUseToolConfig) {
103 return brandProviderTool<
104 | AnthropicComputerToolset
105 | AnthropicComputerUse20251124Tool
106 | AnthropicComputerUseTool
107 >({
108 name: COMPUTER_TOOL_NAME,
109 description: '',
110 // Every version has the same runtime kind. The type kind picks the
111 // models that accept it.
112 metadata: { ...config, __kind: 'anthropic.computer_use' },
113 })
114}
115
116/** The native tool for the newest computer version in the model's kinds. */
117function nativeComputerTool(
118 { display, capabilities }: ComputerUseToolMetadata,
119 kinds: ReadonlyArray<string>,
120) {
121 if (kinds.includes('computer_toolset')) {
122 // The toolset type has no display size field.
123 return computerUseTool({
124 type: 'computer_toolset_20260801',
125 configs: {
126 zoom: { enabled: capabilities.zoom },
127 cursor_position: { enabled: capabilities.cursorPosition },
128 },
129 })
130 }
131 const size = {
132 display_width_px: display.width,
133 display_height_px: display.height,
134 }
135 if (kinds.includes('computer_use_20251124')) {
136 return computerUseTool({
137 type: 'computer_20251124',
138 name: COMPUTER_TOOL_NAME,
139 ...size,
140 enable_zoom: capabilities.zoom,
141 })
142 }
143 if (kinds.includes('computer_use')) {
144 return computerUseTool({
145 type: 'computer_20250124',
146 name: COMPUTER_TOOL_NAME,
147 ...size,
148 })
149 }
150 return undefined
151}
152
153/**
154 * Turn the generic computer tool from `@tanstack/ai-computer-use` into the
155 * native computer tool of the model. The tool keeps its name `computer` and
156 * its `execute`. Every other tool comes back unchanged.
157 *
158 * The version is the newest one in the model meta: `computer_toolset_20260801`,
159 * then `computer_20251124`, then `computer_20250124`.
160 *
161 * @param tool - A tool from the chat tool list.
162 * @param model - The Anthropic model id of the request.
163 * @throws Error when the model has no computer version, or the model id is
164 * not in the model meta.
165 */
166export function resolveComputerUseTool(tool: Tool, model: string) {
167 const computer = getComputerUseToolMetadata(tool)
168 if (!computer) return tool
169 const native = nativeComputerTool(
170 computer,
171 ANTHROPIC_MODEL_TOOLS[model] ?? [],
172 )
173 if (!native) {
174 throw new Error(`Model "${model}" does not support computer use.`)
175 }
176 return { ...tool, metadata: native.metadata }
177}