Open History to see the last commit
1import { createFileRoute } from '@tanstack/react-router'
2import { chat, createChatOptions } from '@tanstack/ai'
3import { createAnthropicChat } from '@tanstack/ai-anthropic'
4import { computerUseTool } from '@tanstack/ai-anthropic/tools'
5
6const DUMMY_KEY = 'sk-ant-e2e-test-dummy-key'
7
8/**
9 * Drives one computer toolset loop on `claude-opus-5-5`. A custom `fetch`
10 * captures each outgoing request. The first answer has two toolset calls
11 * (`left_click` and `screenshot`). The second answer ends the turn.
12 *
13 * The companion spec reads the captured requests and the inputs that
14 * `execute` got, so it can check the toolset wire format without an API key.
15 */
16function sseStream(events: Array<{ type: string }>) {
17 const encoder = new TextEncoder()
18 return new ReadableStream<Uint8Array>({
19 start(controller) {
20 for (const event of events) {
21 controller.enqueue(
22 encoder.encode(
23 `event: ${event.type}\ndata: ${JSON.stringify(event)}\n\n`,
24 ),
25 )
26 }
27 controller.close()
28 },
29 })
30}
31
32const messageStart = {
33 type: 'message_start',
34 message: {
35 id: 'msg_computer_toolset_wire',
36 type: 'message',
37 role: 'assistant',
38 content: [],
39 model: 'claude-opus-5-5',
40 stop_reason: null,
41 stop_sequence: null,
42 usage: { input_tokens: 5, output_tokens: 0 },
43 },
44}
45
46const end = (stopReason: string) => [
47 {
48 type: 'message_delta',
49 delta: { stop_reason: stopReason, stop_sequence: null },
50 usage: { output_tokens: 1 },
51 },
52 { type: 'message_stop' },
53]
54
55const toolsetCalls = [
56 messageStart,
57 {
58 type: 'content_block_start',
59 index: 0,
60 content_block: {
61 type: 'tool_use',
62 id: 'toolu_click',
63 name: 'left_click',
64 toolset_name: 'computer',
65 input: {},
66 },
67 },
68 {
69 type: 'content_block_delta',
70 index: 0,
71 delta: {
72 type: 'input_json_delta',
73 partial_json: '{"coordinate":[500,300]}',
74 },
75 },
76 { type: 'content_block_stop', index: 0 },
77 {
78 type: 'content_block_start',
79 index: 1,
80 content_block: {
81 type: 'tool_use',
82 id: 'toolu_shot',
83 name: 'screenshot',
84 toolset_name: 'computer',
85 input: {},
86 },
87 },
88 { type: 'content_block_stop', index: 1 },
89 ...end('tool_use'),
90]
91
92export const Route = createFileRoute('/api/anthropic-computer-toolset-wire')({
93 server: {
94 handlers: {
95 POST: async () => {
96 const requests: Array<{ anthropicBeta: string | null; body: unknown }> =
97 []
98 const capturingFetch: typeof fetch = async (input, init) => {
99 const req =
100 input instanceof Request ? input : new Request(input, init)
101 const rawBody = await req.text()
102 requests.push({
103 anthropicBeta: req.headers.get('anthropic-beta'),
104 body: rawBody ? JSON.parse(rawBody) : null,
105 })
106 const events =
107 requests.length === 1
108 ? toolsetCalls
109 : [messageStart, ...end('end_turn')]
110 return new Response(sseStream(events), {
111 status: 200,
112 headers: { 'Content-Type': 'text/event-stream' },
113 })
114 }
115
116 const calls: Array<unknown> = []
117 const computer = {
118 ...computerUseTool({
119 type: 'computer_toolset_20260801',
120 configs: { zoom: { enabled: false } },
121 }),
122 execute: (input: { action: string }) => {
123 calls.push(input)
124 if (input.action !== 'screenshot') return 'OK'
125 return [
126 {
127 type: 'image' as const,
128 source: {
129 type: 'data' as const,
130 value: 'AAAA',
131 mimeType: 'image/png',
132 },
133 },
134 ]
135 },
136 }
137
138 const adapter = createAnthropicChat('claude-opus-5-5', DUMMY_KEY, {
139 fetch: capturingFetch,
140 })
141
142 try {
143 for await (const _ of chat({
144 threadId: 'anthropic-computer-toolset-wire',
145 ...createChatOptions({ adapter }),
146 messages: [{ role: 'user', content: 'Click the button.' }],
147 tools: [computer],
148 toolExecution: 'sequential',
149 })) {
150 // Drain the stream.
151 }
152 } catch (error) {
153 return Response.json({
154 ok: false,
155 error: error instanceof Error ? error.message : String(error),
156 })
157 }
158
159 return Response.json({ ok: true, calls, requests })
160 },
161 },
162 },
163})