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

computer-use.md

125 lines6.2 KB

1---
2title: Give your agent a computer
3id: harness-computer-use
4order: 8
5description: "Let a harness agent use a web page. Each thread gets its own browser page. Screenshots go to the media store of the session, and OpenAI safety checks ask the user."
6keywords:
7 - tanstack ai
8 - harness
9 - computer use
10 - computerUse
11 - browser agent
12 - playwright
13 - screenshots
14---
15​
16Your agent must do a task on a web page that has no API, for example fill in a form or find a price. `computerUse()` gives the agent a screen, a mouse, and a keyboard. Each thread gets its own browser page. Each screenshot goes to the media store of the session, so the log stays small.
17​
18To use computer use in a `chat()` route with no harness, read [Computer Use](../computer-use/computer-use).
19​
20## 1. Add the plugin
21​
22<!-- ::start:tabs variant="package-manager" mode="install" -->
23​
24react: @tanstack/ai-harness @tanstack/ai-computer-use @tanstack/ai-computer-playwright playwright-core zod
25vue: @tanstack/ai-harness @tanstack/ai-computer-use @tanstack/ai-computer-playwright playwright-core zod
26solid: @tanstack/ai-harness @tanstack/ai-computer-use @tanstack/ai-computer-playwright playwright-core zod
27svelte: @tanstack/ai-harness @tanstack/ai-computer-use @tanstack/ai-computer-playwright playwright-core zod
28preact: @tanstack/ai-harness @tanstack/ai-computer-use @tanstack/ai-computer-playwright playwright-core zod
29angular: @tanstack/ai-harness @tanstack/ai-computer-use @tanstack/ai-computer-playwright playwright-core zod
30octane: @tanstack/ai-harness @tanstack/ai-computer-use @tanstack/ai-computer-playwright playwright-core zod
31vanilla: @tanstack/ai-harness @tanstack/ai-computer-use @tanstack/ai-computer-playwright playwright-core zod
32​
33<!-- ::end:tabs -->
34​
35Then download Chromium one time:
36​
37```bash
38npx playwright-core install chromium
39```
40​
41```ts group=harness-computer-use
42import { createHarnessHost, defineHarness } from '@tanstack/ai-harness'
43import { permissions } from '@tanstack/ai-harness/plugins'
44import { computerUse } from '@tanstack/ai-computer-use/harness'
45import { playwrightComputer } from '@tanstack/ai-computer-playwright'
46import { openaiText } from '@tanstack/ai-openai'
47import { memoryPersistence } from '@tanstack/ai-persistence'
48​
49// One time, outside `plugins`, so the sessions share one Chromium.
50const browser = computerUse({
51 driver: playwrightComputer({ startUrl: 'https://tanstack.com' }),
52})
53​
54export const agent = defineHarness({
55 name: 'acme/browser-agent',
56 adapter: openaiText('gpt-6.1-sol'),
57 plugins: () => [permissions(), browser],
58})
59​
60const host = createHarnessHost({ persistence: memoryPersistence() })
61const session = await host.open(agent, { threadId: 'browser-1' })
62await session.send('Go to https://tanstack.com/ai and tell me the title of the page.')
63```
64​
65- The model gets the `computer` tool. With it, the model clicks, types, scrolls, and takes screenshots.
66- The model also gets the `navigate` tool when the driver can open a URL. `playwrightComputer()` can.
67- Chromium starts on the first action. Each thread gets a new page, and the page opens `startUrl`.
68​
69To use Anthropic, change only the adapter, for example to `anthropicText('claude-opus-5-5')`.
70​
71## 2. Answer a safety check
72​
73OpenAI can mark an action as risky, for example when the page tells the model to do something else. Before that action runs, `permissions()` asks the user. The turn stops, and you answer the approval:
74​
75```ts group=harness-computer-use
76// wait.message is, for example: Allow computer {"action":{"type":"click",...},"pending_safety_checks":[...]}?
77const waits = session.snapshot().pendingWaits
78await session.resolve({
79 inputId: 'approve-1',
80 resume: waits.map((wait) => ({
81 interruptId: wait.waitId,
82 status: 'resolved',
83 payload: 'once',
84 })),
85})
86await session.settled({ inputId: 'approve-1' })
87```
88​
89- `once` runs the action. The model gets the screenshot, and OpenAI gets the acknowledged check.
90- `always` runs the action too, but it saves no rule. The next safety check asks again.
91- `reject` does not run the action. The model gets an error result.
92​
93The other actions run with no question. Anthropic sends no safety checks, so its actions never ask.
94​
95In the `plan` mode, the `computer` tool does not run. To ask before every action, add a rule: `permissions({ rules: [{ tool: 'computer', decision: 'ask' }] })`.
96​
97CAUTION: Keep `permissions()` in the plugins. Without it, an action with a safety check does not run. In the `bypass` mode, it runs with no question. See [Pick a mode](./permissions#pick-a-mode).
98​
99## 3. Choose how many screenshots the model sees
100​
101Each action returns a screenshot. The plugin stores it in the media store of the session. The transcript and the log keep a small `harness-media:` reference to each one.
102​
103The model gets only the 3 newest screenshots. An older screenshot goes to the model as a 1x1 image, so a long task does not send every old screenshot again. To change the number, set `keepScreenshots`:
104​
105```ts group=harness-computer-use
106export const browserWithHistory = computerUse({
107 driver: playwrightComputer({ startUrl: 'https://tanstack.com' }),
108 keepScreenshots: 5,
109})
110```
111​
112The media store keeps every screenshot, so a client can still show all of them. To keep the files after a restart, give the host a blob store. See [Keep media across restarts](./media#keep-media-across-restarts).
113​
114## When the pages close
115​
116- All the sessions of a host that use the same `computerUse()` object share one Chromium.
117- A thread that stays idle for `idleTimeout` loses its page. The default is 10 minutes. The next action of that thread opens a new page at `startUrl`.
118- `host.close()` closes every page, then Chromium.
119- The pages live in the memory of the host. When another host opens the thread, the thread gets a new page there.
120​
121The browser can open every address that your server can reach. See [Keep the browser away from private addresses](../computer-use/computer-use#keep-the-browser-away-from-private-addresses). To use a Linux desktop or your own computer, pass another driver from [Drivers](../computer-use/drivers).
122​
123## What you have now
124​
125Run the session from step 1. The model calls `navigate`, reads the screenshot, and answers with the title of the page. Your harness agent now uses a browser, and each screenshot is in the media store of the session.