Open History to see the last commit
1import { beforeEach, describe, expect, it, vi } from 'vitest'
2import { chat } from '@tanstack/ai'
3import { AnthropicTextAdapter } from '../src/adapters/text'
4import { computerUseTool } from '../src/tools'
5
6const mocks = vi.hoisted(() => {
7 const betaMessagesCreate = vi.fn()
8 const client = { beta: { messages: { create: betaMessagesCreate } } }
9 return { betaMessagesCreate, client }
10})
11
12vi.mock('@anthropic-ai/sdk', () => {
13 const { client } = mocks
14 class MockAnthropic {
15 beta = client.beta
16 constructor(_: { apiKey: string }) {}
17 }
18 return { default: MockAnthropic }
19})
20
21function stream(events: Array<unknown>) {
22 mocks.betaMessagesCreate.mockResolvedValueOnce(
23 (async function* () {
24 yield* events
25 })(),
26 )
27}
28
29/** A computer toolset call, as the model streams it. */
30function memberCall(index: number, id: string, name: string, json?: string) {
31 return [
32 {
33 type: 'content_block_start',
34 index,
35 content_block: {
36 type: 'tool_use',
37 id,
38 name,
39 toolset_name: 'computer',
40 input: {},
41 },
42 },
43 ...(json === undefined
44 ? []
45 : [
46 {
47 type: 'content_block_delta',
48 index,
49 delta: { type: 'input_json_delta', partial_json: json },
50 },
51 ]),
52 { type: 'content_block_stop', index },
53 ]
54}
55
56const end = (stopReason: string) => [
57 {
58 type: 'message_delta',
59 delta: { stop_reason: stopReason },
60 usage: { output_tokens: 1 },
61 },
62 { type: 'message_stop' },
63]
64
65const screenshot = [
66 {
67 type: 'image' as const,
68 source: { type: 'data' as const, value: 'AAAA', mimeType: 'image/png' },
69 },
70]
71
72beforeEach(() => {
73 mocks.betaMessagesCreate.mockReset()
74})
75
76describe('anthropic computer toolset', () => {
77 it('sends the toolset entry with no name and no beta', async () => {
78 stream(end('end_turn'))
79 const adapter = new AnthropicTextAdapter({ apiKey: 'k' }, 'claude-opus-5-5')
80
81 for await (const _ of chat({
82 adapter,
83 messages: [{ role: 'user', content: 'Open the settings.' }],
84 tools: [
85 computerUseTool({
86 type: 'computer_toolset_20260801',
87 configs: { zoom: { enabled: false } },
88 }),
89 ],
90 })) {
91 // consume
92 }
93
94 const [payload] = mocks.betaMessagesCreate.mock.calls[0]!
95 // The prompt cache marker can also sit on the entry.
96 expect(payload.tools).toMatchObject([
97 {
98 type: 'computer_toolset_20260801',
99 configs: { zoom: { enabled: false } },
100 },
101 ])
102 expect(payload.tools[0]).not.toHaveProperty('name')
103 expect(payload.tools[0]).not.toHaveProperty('__kind')
104 expect(payload.betas).toBeUndefined()
105 })
106
107 it('runs each toolset call as one computer call and sends it back', async () => {
108 stream([
109 ...memberCall(0, 'toolu_click', 'left_click', '{"coordinate":[500,300]}'),
110 ...memberCall(1, 'toolu_shot', 'screenshot'),
111 ...end('tool_use'),
112 ])
113 stream(end('end_turn'))
114 const calls: Array<unknown> = []
115 const tool = {
116 ...computerUseTool({ type: 'computer_toolset_20260801' }),
117 execute: (input: unknown) => {
118 calls.push(input)
119 return calls.length === 1 ? 'OK' : screenshot
120 },
121 }
122 const adapter = new AnthropicTextAdapter({ apiKey: 'k' }, 'claude-opus-5-5')
123
124 for await (const _ of chat({
125 adapter,
126 messages: [{ role: 'user', content: 'Click the button.' }],
127 tools: [tool],
128 toolExecution: 'sequential',
129 })) {
130 // consume
131 }
132
133 expect(calls).toEqual([
134 { action: 'left_click', coordinate: [500, 300] },
135 { action: 'screenshot' },
136 ])
137 const [payload] = mocks.betaMessagesCreate.mock.calls[1]!
138 const [, assistant, results] = payload.messages
139 expect(assistant.content).toEqual([
140 {
141 type: 'tool_use',
142 id: 'toolu_click',
143 name: 'left_click',
144 toolset_name: 'computer',
145 input: { coordinate: [500, 300] },
146 },
147 {
148 type: 'tool_use',
149 id: 'toolu_shot',
150 name: 'screenshot',
151 toolset_name: 'computer',
152 input: {},
153 },
154 ])
155 expect(results.content).toMatchObject([
156 {
157 type: 'tool_result',
158 tool_use_id: 'toolu_click',
159 toolset_name: 'computer',
160 content: 'OK',
161 },
162 {
163 type: 'tool_result',
164 tool_use_id: 'toolu_shot',
165 toolset_name: 'computer',
166 content: [
167 {
168 type: 'image',
169 source: { type: 'base64', data: 'AAAA', media_type: 'image/png' },
170 },
171 ],
172 },
173 ])
174 })
175
176 it('sends a computer call back as the single tool when the toolset is not in tools', async () => {
177 stream(end('end_turn'))
178 const adapter = new AnthropicTextAdapter({ apiKey: 'k' }, 'claude-opus-4-6')
179
180 for await (const _ of chat({
181 adapter,
182 messages: [
183 { role: 'user', content: 'Click the button.' },
184 {
185 role: 'assistant',
186 content: '',
187 toolCalls: [
188 {
189 id: 'toolu_click',
190 type: 'function',
191 function: {
192 name: 'computer',
193 arguments: '{"action":"left_click","coordinate":[1,2]}',
194 },
195 },
196 ],
197 },
198 { role: 'tool', toolCallId: 'toolu_click', content: 'OK' },
199 ],
200 tools: [
201 computerUseTool({
202 type: 'computer_20251124',
203 name: 'computer',
204 display_width_px: 1024,
205 display_height_px: 768,
206 }),
207 ],
208 })) {
209 // consume
210 }
211
212 const [payload] = mocks.betaMessagesCreate.mock.calls[0]!
213 expect(payload.betas).toContain('computer-use-2025-11-24')
214 const [, assistant, results] = payload.messages
215 expect(assistant.content).toEqual([
216 {
217 type: 'tool_use',
218 id: 'toolu_click',
219 name: 'computer',
220 input: { action: 'left_click', coordinate: [1, 2] },
221 },
222 ])
223 expect(results.content[0]).not.toHaveProperty('toolset_name')
224 })
225})