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

computer-use-tool.ts

174 lines5.6 KB

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