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

openai-computer-use-wire.spec.ts

103 lines3.1 KB

1import { test, expect } from './fixtures'
2​
3/**
4 * Wire format of OpenAI computer use. The model sends a `computer_call`, the
5 * app runs it, and the next request sends the call back with its
6 * `computer_call_output`.
7 *
8 * `/api/openai-computer-use-wire` serves the model responses with a custom
9 * `fetch` and returns the tool inputs and the request bodies as JSON.
10 */
11test.describe('openai — computer use wire format', () => {
12 test('runs the computer call and sends the screenshot back', async ({
13 request,
14 }) => {
15 const res = await request.post('/api/openai-computer-use-wire')
16 expect(res.ok()).toBe(true)
17 const { ok, error, toolInputs, requestBodies } = (await res.json()) as {
18 ok: boolean
19 error?: string
20 toolInputs: Array<unknown>
21 requestBodies: Array<{ input: Array<unknown> }>
22 }
23​
24 if (!ok) {
25 throw new Error(`Route failed: ${error}`)
26 }
27​
28 const safetyCheck = {
29 id: 'sc_e2e',
30 code: 'malicious_instructions',
31 message: 'The page asks the model to do something else.',
32 }
33 expect(toolInputs).toEqual([
34 {
35 action: { type: 'click', button: 'left', x: 10, y: 20 },
36 pending_safety_checks: [safetyCheck],
37 },
38 ])
39 expect(requestBodies).toHaveLength(2)
40 expect(requestBodies[1]!.input).toEqual(
41 expect.arrayContaining([
42 expect.objectContaining({
43 type: 'computer_call',
44 call_id: 'call_cu_e2e',
45 action: { type: 'click', button: 'left', x: 10, y: 20 },
46 }),
47 {
48 type: 'computer_call_output',
49 call_id: 'call_cu_e2e',
50 output: {
51 type: 'computer_screenshot',
52 image_url: 'data:image/png;base64,iVBORw0KGgo=',
53 detail: 'original',
54 },
55 acknowledged_safety_checks: [safetyCheck],
56 },
57 ]),
58 )
59 })
60​
61 test('runs the batched actions of the computer tool', async ({ request }) => {
62 const res = await request.post(
63 '/api/openai-computer-use-wire?tool=computer',
64 )
65 expect(res.ok()).toBe(true)
66 const { ok, error, toolInputs, requestBodies } = (await res.json()) as {
67 ok: boolean
68 error?: string
69 toolInputs: Array<unknown>
70 requestBodies: Array<{ tools?: Array<unknown>; input: Array<unknown> }>
71 }
72​
73 if (!ok) {
74 throw new Error(`Route failed: ${error}`)
75 }
76​
77 const actions = [
78 { type: 'click', button: 'left', x: 10, y: 20 },
79 { type: 'type', text: 'hello' },
80 ]
81 expect(toolInputs).toEqual([{ actions, pending_safety_checks: [] }])
82 expect(requestBodies).toHaveLength(2)
83 expect(requestBodies[0]!.tools).toEqual([{ type: 'computer' }])
84 expect(requestBodies[1]!.input).toEqual(
85 expect.arrayContaining([
86 expect.objectContaining({
87 type: 'computer_call',
88 call_id: 'call_cu_e2e_batch',
89 actions,
90 }),
91 {
92 type: 'computer_call_output',
93 call_id: 'call_cu_e2e_batch',
94 output: {
95 type: 'computer_screenshot',
96 image_url: 'data:image/png;base64,iVBORw0KGgo=',
97 detail: 'original',
98 },
99 },
100 ]),
101 )
102 })
103})