Open History to see the last commit
1---
2title: Computer Use Safety Checks
3id: computer-use-safety-checks
4order: 3
5description: "Pause a computer action until a person approves it. OpenAI attaches safety checks to risky actions, and TanStack AI turns each one into an interrupt."
6keywords:
7 - tanstack ai
8 - computer use
9 - safety checks
10 - pending_safety_checks
11 - computerSafetyCheck
12 - interrupt
13 - approval
14---
15
16Some computer actions are risky. The page can ask the model to do a thing the user did not ask for, or the action can send data. OpenAI attaches safety checks to these actions. The run pauses, and a person approves or declines the action.
17
18## How it works
19
201. The model sends a computer call with `pending_safety_checks`.
212. The `middleware` pauses the run with one interrupt for each call that has checks.
223. A person approves or declines on the client.
23
24- If the person approves all calls, the calls run. The checks go back to OpenAI as acknowledged.
25- If the person declines one call, no call of that turn runs.
26- A call with checks that nobody approved does not run. The tool refuses it.
27
28Safety checks are an OpenAI feature. Anthropic models do not send them, so this code does nothing on Anthropic.
29
30## Server
31
32Give `tools`, `middleware`, and `interrupt` to `chat()`. Also give `parentRunId` and `resume`, so that the approval can continue the run.
33
34```ts
35// app/api/chat/route.ts
36import { chat, chatParamsFromRequest, toServerSentEventsResponse } from '@tanstack/ai'
37import { openaiText } from '@tanstack/ai-openai'
38import { createComputerUse } from '@tanstack/ai-computer-use'
39import { playwrightComputer } from '@tanstack/ai-computer-playwright'
40
41const computerUse = createComputerUse({
42 driver: playwrightComputer({ startUrl: 'https://tanstack.com' }),
43 // A person has up to one hour to answer before the page closes.
44 idleTimeout: 60 * 60_000,
45})
46
47export async function POST(request: Request) {
48 const params = await chatParamsFromRequest(request)
49 const stream = chat({
50 adapter: openaiText('gpt-6.1-sol'),
51 messages: params.messages,
52 threadId: params.threadId,
53 runId: params.runId,
54 ...(params.parentRunId ? { parentRunId: params.parentRunId } : {}),
55 ...(params.resume ? { resume: params.resume } : {}),
56 tools: computerUse.tools,
57 middleware: [computerUse.middleware],
58 interrupts: [computerUse.interrupt],
59 })
60 return toServerSentEventsResponse(stream)
61}
62```
63
64`computerUse` keeps the approval for the thread until the resume request comes. To keep it there:
65
66- Create `computerUse` one time, at module scope. Every request uses the same one.
67- Give `params.threadId` to `chat()`. The resume request has the same thread id as the request that paused.
68- Set `idleTimeout` above the time that a person takes to answer. If the time ends first, the page closes, and the approved action runs on a new page.
69- Send the resume request to the same server process. The approvals live in the memory of that process.
70
71## Client
72
73Register `computerSafetyCheck` with `useChat`. Each item has these fields:
74
75- `payload.checks`: the safety checks. Each check has an `id`, and can have a `code` and a `message`.
76- `payload.action`: the action that waits. For a batch, this is the list of actions.
77
78```tsx
79// app/chat.tsx
80import { fetchServerSentEvents, useChat } from '@tanstack/ai-react'
81import type { GenericInterrupt } from '@tanstack/ai-react'
82import { computerSafetyCheck } from '@tanstack/ai-computer-use'
83
84function SafetyCheckCard({
85 interrupt,
86}: {
87 interrupt: GenericInterrupt<typeof computerSafetyCheck>
88}) {
89 const checks = interrupt.payload?.checks ?? []
90 return (
91 <article>
92 <h2>Approve this computer action?</h2>
93 <ul>
94 {checks.map((check) => (
95 <li key={check.id}>{check.message ?? check.code ?? check.id}</li>
96 ))}
97 </ul>
98 <pre>{JSON.stringify(interrupt.payload?.action, null, 2)}</pre>
99 <button onClick={() => interrupt.resolveInterrupt({ approved: true })}>
100 Approve
101 </button>
102 <button onClick={() => interrupt.resolveInterrupt({ approved: false })}>
103 Decline
104 </button>
105 </article>
106 )
107}
108
109export function Chat() {
110 const { interrupts } = useChat({
111 connection: fetchServerSentEvents('/api/chat'),
112 interrupts: [computerSafetyCheck],
113 })
114
115 return (
116 <>
117 {interrupts.map((interrupt) => {
118 if (interrupt.kind !== 'generic') return null
119 if (!('definitionId' in interrupt)) return null
120 if (interrupt.definitionId !== computerSafetyCheck.id) return null
121 return <SafetyCheckCard key={interrupt.id} interrupt={interrupt} />
122 })}
123 </>
124 )
125}
126```
127
128`interrupt.cancel()` also declines the action. When every interrupt of the batch has an answer, the client sends the answers. For more about generic interrupts, see [Generic Interrupts](../interrupts/generic).
129
130## Try it
131
132Ask the model to do a task that sends data, for example a form submit. When OpenAI adds a safety check, the card shows. Click **Approve**, and the action runs. Click **Decline**, and the page stays as it was.