Open History to see the last commit
1import { test, expect } from './fixtures'
2import type { APIRequestContext } from '@playwright/test'
3
4/**
5 * Wire format of the Anthropic computer toolset (`computer_toolset_20260801`).
6 *
7 * The model calls each action as its own tool (`left_click`, `screenshot`)
8 * with `toolset_name: 'computer'`. The adapter runs each call as one
9 * `computer` call with the action in `input.action`. On the next request it
10 * sends each call back in the form that the model sent, and each result
11 * names the toolset.
12 *
13 * `/api/anthropic-computer-toolset-wire` runs one loop against a custom
14 * `fetch`, and returns the captured requests and the inputs that `execute`
15 * got.
16 */
17interface WireResult {
18 ok: boolean
19 error?: string
20 calls: Array<unknown>
21 requests: Array<{
22 anthropicBeta: string | null
23 body: {
24 tools: Array<Record<string, unknown>>
25 messages: Array<{ role: string; content: Array<Record<string, unknown>> }>
26 }
27 }>
28}
29
30async function runLoop(request: APIRequestContext) {
31 const res = await request.post('/api/anthropic-computer-toolset-wire')
32 expect(res.ok()).toBe(true)
33 const result = (await res.json()) as WireResult
34 if (!result.ok) {
35 throw new Error(`Route failed: ${result.error}`)
36 }
37 return result
38}
39
40test.describe('anthropic — computer toolset wire format', () => {
41 test('sends the toolset entry with no name and no computer use beta', async ({
42 request,
43 }) => {
44 const { requests } = await runLoop(request)
45 const [first] = requests
46 expect(first?.body.tools[0]).toMatchObject({
47 type: 'computer_toolset_20260801',
48 configs: { zoom: { enabled: false } },
49 })
50 expect(first?.body.tools[0]).not.toHaveProperty('name')
51 expect(first?.anthropicBeta ?? '').not.toContain('computer-use')
52 })
53
54 test('runs each toolset call as one computer call', async ({ request }) => {
55 const { calls } = await runLoop(request)
56 expect(calls).toEqual([
57 { action: 'left_click', coordinate: [500, 300] },
58 { action: 'screenshot' },
59 ])
60 })
61
62 test('sends the calls back as toolset calls, and the results name the toolset', async ({
63 request,
64 }) => {
65 const { requests } = await runLoop(request)
66 const second = requests[1]
67 expect(second).toBeDefined()
68 const assistant = second?.body.messages.find((m) => m.role === 'assistant')
69 expect(assistant?.content).toMatchObject([
70 {
71 type: 'tool_use',
72 id: 'toolu_click',
73 name: 'left_click',
74 toolset_name: 'computer',
75 input: { coordinate: [500, 300] },
76 },
77 {
78 type: 'tool_use',
79 id: 'toolu_shot',
80 name: 'screenshot',
81 toolset_name: 'computer',
82 input: {},
83 },
84 ])
85 const results = second?.body.messages
86 .flatMap((m) => m.content)
87 .filter((block) => block['type'] === 'tool_result')
88 expect(results).toMatchObject([
89 { tool_use_id: 'toolu_click', toolset_name: 'computer', content: 'OK' },
90 {
91 tool_use_id: 'toolu_shot',
92 toolset_name: 'computer',
93 content: [{ type: 'image', source: { type: 'base64', data: 'AAAA' } }],
94 },
95 ])
96 })
97})