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 threadId: 'thread-1',
83 adapter,
84 messages: [{ role: 'user', content: 'Open the settings.' }],
85 tools: [
86 computerUseTool({
87 type: 'computer_toolset_20260801',
88 configs: { zoom: { enabled: false } },
89 }),
90 ],
91 })) {
92 // consume
93 }
94
95 const [payload] = mocks.betaMessagesCreate.mock.calls[0]!
96 // The prompt cache marker can also sit on the entry.
97 expect(payload.tools).toMatchObject([
98 {
99 type: 'computer_toolset_20260801',
100 configs: { zoom: { enabled: false } },
101 },
102 ])
103 expect(payload.tools[0]).not.toHaveProperty('name')
104 expect(payload.tools[0]).not.toHaveProperty('__kind')
105 expect(payload.betas).toBeUndefined()
106 })
107
108 it('runs each toolset call as one computer call and sends it back', async () => {
109 stream([
110 ...memberCall(0, 'toolu_click', 'left_click', '{"coordinate":[500,300]}'),
111 ...memberCall(1, 'toolu_shot', 'screenshot'),
112 ...end('tool_use'),
113 ])
114 stream(end('end_turn'))
115 const calls: Array<unknown> = []
116 const tool = {
117 ...computerUseTool({ type: 'computer_toolset_20260801' }),
118 execute: (input: unknown) => {
119 calls.push(input)
120 return calls.length === 1 ? 'OK' : screenshot
121 },
122 }
123 const adapter = new AnthropicTextAdapter({ apiKey: 'k' }, 'claude-opus-5-5')
124
125 for await (const _ of chat({
126 threadId: 'thread-1',
127 adapter,
128 messages: [{ role: 'user', content: 'Click the button.' }],
129 tools: [tool],
130 toolExecution: 'sequential',
131 })) {
132 // consume
133 }
134
135 expect(calls).toEqual([
136 { action: 'left_click', coordinate: [500, 300] },
137 { action: 'screenshot' },
138 ])
139 const [payload] = mocks.betaMessagesCreate.mock.calls[1]!
140 const [, assistant, results] = payload.messages
141 expect(assistant.content).toEqual([
142 {
143 type: 'tool_use',
144 id: 'toolu_click',
145 name: 'left_click',
146 toolset_name: 'computer',
147 input: { coordinate: [500, 300] },
148 },
149 {
150 type: 'tool_use',
151 id: 'toolu_shot',
152 name: 'screenshot',
153 toolset_name: 'computer',
154 input: {},
155 },
156 ])
157 expect(results.content).toMatchObject([
158 {
159 type: 'tool_result',
160 tool_use_id: 'toolu_click',
161 toolset_name: 'computer',
162 content: 'OK',
163 },
164 {
165 type: 'tool_result',
166 tool_use_id: 'toolu_shot',
167 toolset_name: 'computer',
168 content: [
169 {
170 type: 'image',
171 source: { type: 'base64', data: 'AAAA', media_type: 'image/png' },
172 },
173 ],
174 },
175 ])
176 })
177
178 it('sends a computer call back as the single tool when the toolset is not in tools', async () => {
179 stream(end('end_turn'))
180 const adapter = new AnthropicTextAdapter({ apiKey: 'k' }, 'claude-opus-4-6')
181
182 for await (const _ of chat({
183 threadId: 'thread-1',
184 adapter,
185 messages: [
186 { role: 'user', content: 'Click the button.' },
187 {
188 role: 'assistant',
189 content: '',
190 toolCalls: [
191 {
192 id: 'toolu_click',
193 type: 'function',
194 function: {
195 name: 'computer',
196 arguments: '{"action":"left_click","coordinate":[1,2]}',
197 },
198 },
199 ],
200 },
201 { role: 'tool', toolCallId: 'toolu_click', content: 'OK' },
202 ],
203 tools: [
204 computerUseTool({
205 type: 'computer_20251124',
206 name: 'computer',
207 display_width_px: 1024,
208 display_height_px: 768,
209 }),
210 ],
211 })) {
212 // consume
213 }
214
215 const [payload] = mocks.betaMessagesCreate.mock.calls[0]!
216 expect(payload.betas).toContain('computer-use-2025-11-24')
217 const [, assistant, results] = payload.messages
218 expect(assistant.content).toEqual([
219 {
220 type: 'tool_use',
221 id: 'toolu_click',
222 name: 'computer',
223 input: { action: 'left_click', coordinate: [1, 2] },
224 },
225 ])
226 expect(results.content[0]).not.toHaveProperty('toolset_name')
227 })
228})