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

computer-use-generic.test.ts

183 lines5.4 KB

1import { describe, expect, it, vi } from 'vitest'
2import { chat } from '@tanstack/ai'
3import { OpenAIBaseChatCompletionsTextAdapter } from '../src/adapters/chat-completions-text'
4import { OpenAIBaseResponsesTextAdapter } from '../src/adapters/responses-text'
5import { convertToolsToProviderFormat } from '../src/tools/tool-converter'
6import type OpenAI from 'openai'
7import type { StreamChunk, Tool } from '@tanstack/ai'
8​
9const safetyCheck = {
10 id: 'sc_1',
11 code: 'malicious_instructions',
12 message: 'The page asks the model to do something else.',
14​
15async function* completed(output: Array<Record<string, unknown>>) {
16 yield* [
17 {
18 type: 'response.created',
19 response: { id: 'resp_1', model: 'm', status: 'in_progress' },
20 },
21 {
22 type: 'response.completed',
23 response: {
24 id: 'resp_1',
25 model: 'm',
26 status: 'completed',
27 output,
28 usage: { input_tokens: 1, output_tokens: 1, total_tokens: 2 },
29 },
30 },
31 ]
33​
34const answer = {
35 type: 'message',
36 id: 'msg_1',
37 role: 'assistant',
38 status: 'completed',
39 content: [{ type: 'output_text', text: 'Done.', annotations: [] }],
41​
42const screenshot = [
43 {
44 type: 'image',
45 source: { type: 'data', value: 'iVBORw0KGgo=', mimeType: 'image/png' },
46 },
48​
49const screenshotOutput = {
50 type: 'computer_screenshot',
51 image_url: 'data:image/png;base64,iVBORw0KGgo=',
52 detail: 'original',
54​
55// The stub clients below implement only the one SDK method the adapter calls.
56// A full OpenAI client is too large to build, so they are cast.
57​
58// The OpenAI adapter sends provider tools in their native shape.
59class TestAdapter extends OpenAIBaseResponsesTextAdapter<string> {
60 protected override convertTools(tools: Array<Tool>) {
61 return convertToolsToProviderFormat(tools)
62 }
64​
65// The portable tool, as `@tanstack/ai-computer-use` builds it.
66function genericComputerTool() {
67 return {
68 name: 'computer',
69 description: 'Use the computer.',
70 metadata: {
71 __kind: 'computer_use',
72 display: { width: 1280, height: 800 },
73 environment: 'linux',
74 capabilities: { zoom: false, cursorPosition: true },
75 },
76 execute: vi.fn(async (_input: unknown) => screenshot),
77 } satisfies Tool
79​
80async function runLoop(model: string, call: Record<string, unknown>) {
81 const create = vi
82 .fn()
83 .mockResolvedValueOnce(completed([call]))
84 .mockResolvedValueOnce(completed([answer]))
85 const client = {
86 responses: { create: (params: unknown) => create(params) },
87 } as unknown as OpenAI
88 const tool = genericComputerTool()
89 for await (const _ of chat({
90 threadId: 'thread-1',
91 adapter: new TestAdapter(model, 'openai', client),
92 messages: [{ role: 'user', content: 'Click the button.' }],
93 tools: [tool],
94 })) {
95 // Drain the stream.
96 }
97 const [first, second] = create.mock.calls.map(([params]) => params)
98 return { first, second, execute: tool.execute }
100​
101describe('portable computer tool on the Responses API', () => {
102 it('sends the computer tool and runs the batch', async () => {
103 const actions = [
104 { type: 'click', button: 'left', x: 10, y: 20 },
105 { type: 'type', text: 'hello' },
106 ]
107 const { first, second, execute } = await runLoop('gpt-6.1-sol', {
108 type: 'computer_call',
109 id: 'cu_2',
110 call_id: 'call_batch',
111 status: 'completed',
112 actions,
113 pending_safety_checks: [safetyCheck],
114 })
115​
116 expect(first.tools).toEqual([{ type: 'computer' }])
117 expect(execute.mock.calls[0]?.[0]).toEqual({
118 actions,
119 pending_safety_checks: [safetyCheck],
120 })
121 expect(second.input).toContainEqual({
122 type: 'computer_call_output',
123 call_id: 'call_batch',
124 output: screenshotOutput,
125 acknowledged_safety_checks: [safetyCheck],
126 })
127 })
128​
129 it('keeps safety checks that do not parse on the tool input', async () => {
130 const action = { type: 'click', button: 'left', x: 10, y: 20 }
131 const badChecks = [{ code: 'x' }]
132 const { second, execute } = await runLoop('gpt-6.1-sol', {
133 type: 'computer_call',
134 id: 'cu_bad',
135 call_id: 'call_bad',
136 status: 'completed',
137 action,
138 pending_safety_checks: badChecks,
139 })
140​
141 expect(execute.mock.calls[0]?.[0]).toEqual({
142 action,
143 pending_safety_checks: badChecks,
144 })
145 expect(second.input).toContainEqual({
146 type: 'computer_call_output',
147 call_id: 'call_bad',
148 output: screenshotOutput,
149 })
150 expect(JSON.stringify(second.input)).not.toContain(
151 'acknowledged_safety_checks',
152 )
153 })
154})
155​
156class TestChatCompletionsAdapter extends OpenAIBaseChatCompletionsTextAdapter<string> {}
157​
158describe('portable computer tool on the Chat Completions API', () => {
159 it('fails before any request', async () => {
160 const create = vi.fn()
161 const client = {
162 chat: { completions: { create } },
163 } as unknown as OpenAI
164 const chunks: Array<StreamChunk> = []
165 for await (const chunk of chat({
166 threadId: 'thread-1',
167 adapter: new TestChatCompletionsAdapter('gpt-6.1-sol', 'openai', client),
168 messages: [{ role: 'user', content: 'Click the button.' }],
169 tools: [genericComputerTool()],
170 })) {
171 chunks.push(chunk)
172 }
173​
174 expect(create).not.toHaveBeenCalled()
175 expect(chunks).toContainEqual(
176 expect.objectContaining({
177 type: 'RUN_ERROR',
178 message:
179 'Computer use needs the Responses API. The Chat Completions API has no computer tool.',
180 }),
181 )
182 })
183})