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

drivers.md

297 lines12.8 KB

1---
2title: Computer Use Drivers
3id: computer-use-drivers
4order: 2
5description: "Pick the machine that the model uses: a browser page for each thread with Playwright, a full Linux desktop for each thread in a sandbox, or your own computer."
6keywords:
7 - tanstack ai
8 - computer use
9 - driver
10 - playwrightComputer
11 - sandboxComputer
12 - localComputer
13 - playwright
14 - e2b
15 - daytona
16 - linux desktop
17 - sandbox
18 - robotjs
19 - local desktop
20---
21​
22A driver is the surface the model controls. Pick a browser, a desktop, or a terminal.
23​
24A driver decides which machine the model uses. Change the driver, and the rest of your [computer use](./computer-use) code stays the same.
25​
26## Pick a driver
27​
28| | Playwright | Sandbox | Your own computer |
29| --------------------- | ----------------------------------------- | --------------------------------------- | ------------------------------------------ |
30| **Machine** | A browser page for each thread | A Linux desktop sandbox for each thread | Your mouse, keyboard, and main screen |
31| **Environment** | `browser` | `linux` | `windows`, `mac`, or `linux` |
32| **Runs on** | Your server | Any sandbox provider | The machine that runs `chat()` |
33| **`navigate` tool** | Yes | No | No |
34| **`zoom`** | Yes | No | Yes |
35| **`cursor_position`** | No | Yes | Yes |
36| **Setup** | `playwright-core`, then download Chromium | A provider and a desktop image | `@jitsi/robotjs`, and permissions on macOS |
37​
38## Playwright
39​
40If the task stays inside a web page, use Playwright.
41​
42Install the packages:
43​
44<!-- ::start:tabs variant="package-manager" mode="install" -->
45​
46react: @tanstack/ai-computer-use @tanstack/ai-computer-playwright playwright-core zod
47vue: @tanstack/ai-computer-use @tanstack/ai-computer-playwright playwright-core zod
48solid: @tanstack/ai-computer-use @tanstack/ai-computer-playwright playwright-core zod
49svelte: @tanstack/ai-computer-use @tanstack/ai-computer-playwright playwright-core zod
50preact: @tanstack/ai-computer-use @tanstack/ai-computer-playwright playwright-core zod
51angular: @tanstack/ai-computer-use @tanstack/ai-computer-playwright playwright-core zod
52vanilla: @tanstack/ai-computer-use @tanstack/ai-computer-playwright playwright-core zod
53octane: @tanstack/ai-computer-use @tanstack/ai-computer-playwright playwright-core zod
54​
55<!-- ::end:tabs -->
56​
57Then download Chromium one time:
58​
59```bash
60npx playwright-core install chromium
61```
62​
63Then give `playwrightComputer()` to `createComputerUse`:
64​
65```ts
66import { createComputerUse } from '@tanstack/ai-computer-use'
67import { playwrightComputer } from '@tanstack/ai-computer-playwright'
68​
69const computerUse = createComputerUse({
70 driver: playwrightComputer({
71 startUrl: 'https://tanstack.com',
72 display: { width: 1280, height: 800 },
73 }),
74})
75```
76​
77`playwrightComputer()` takes these options. All of them are optional:
78​
79- `startUrl`: the URL that each new page opens. Without it, the page stays blank.
80- `display`: the viewport of each page in pixels. The default is 1280 x 800.
81- `browser`: a Playwright `Browser` that makes the pages. `createComputerUse` never closes it.
82- `launch`: the options for `chromium.launch()`. When you give `browser`, the factory ignores them.
83​
84Without `browser`, Chromium starts on the first action. `computerUse.close()` closes it.
85​
86The `back` and `forward` mouse buttons go back and forward in the page history.
87​
88Do you already have a Playwright page, for example in a test? See [Share one page](#share-one-page).
89​
90## Linux desktop in a sandbox
91​
92If the model must use any app on a Linux desktop, use a sandbox. Each thread gets its own desktop, away from your server. The driver works with every [sandbox provider](../sandbox/overview): E2B, Daytona, Docker, and the others.
93​
94Install the packages. This example uses E2B:
95​
96<!-- ::start:tabs variant="package-manager" mode="install" -->
97​
98react: @tanstack/ai-computer-use @tanstack/ai-computer-sandbox @tanstack/ai-sandbox @tanstack/ai-sandbox-e2b zod
99vue: @tanstack/ai-computer-use @tanstack/ai-computer-sandbox @tanstack/ai-sandbox @tanstack/ai-sandbox-e2b zod
100solid: @tanstack/ai-computer-use @tanstack/ai-computer-sandbox @tanstack/ai-sandbox @tanstack/ai-sandbox-e2b zod
101svelte: @tanstack/ai-computer-use @tanstack/ai-computer-sandbox @tanstack/ai-sandbox @tanstack/ai-sandbox-e2b zod
102preact: @tanstack/ai-computer-use @tanstack/ai-computer-sandbox @tanstack/ai-sandbox @tanstack/ai-sandbox-e2b zod
103angular: @tanstack/ai-computer-use @tanstack/ai-computer-sandbox @tanstack/ai-sandbox @tanstack/ai-sandbox-e2b zod
104vanilla: @tanstack/ai-computer-use @tanstack/ai-computer-sandbox @tanstack/ai-sandbox @tanstack/ai-sandbox-e2b zod
105octane: @tanstack/ai-computer-use @tanstack/ai-computer-sandbox @tanstack/ai-sandbox @tanstack/ai-sandbox-e2b zod
106​
107<!-- ::end:tabs -->
108​
109Then do these steps:
110​
1111. Set `E2B_API_KEY` in your server environment.
1122. Give `sandboxComputer()` a provider that makes desktop sandboxes. The E2B `desktop` template is one.
113​
114```ts
115import { createComputerUse } from '@tanstack/ai-computer-use'
116import { sandboxComputer } from '@tanstack/ai-computer-sandbox'
117import { e2bSandbox } from '@tanstack/ai-sandbox-e2b'
118​
119const computerUse = createComputerUse({
120 driver: sandboxComputer({
121 provider: e2bSandbox({ template: 'desktop', timeoutMs: 60 * 60_000 }),
122 }),
123})
124```
125​
126> [!WARNING]
127> Set the provider timeout above `idleTimeout` plus the longest time that one thread stays in use. E2B kills a sandbox at its `timeoutMs`, also when the thread is busy.
128​
129`sandboxComputer()` takes these options:
130​
131- `provider`: the sandbox provider. It makes one sandbox for each thread.
132- `display`: the screen size of each desktop in pixels. The default is 1024 x 768.
133​
134How the sandbox driver behaves:
135​
136- There is no `navigate` tool, because a desktop has no address. The model opens the browser on the desktop.
137- When a thread closes or goes idle, `createComputerUse` destroys its sandbox.
138- One scroll step is 100 pixels.
139- The driver quotes every key name and all typed text before they go to the shell.
140​
141### The sandbox image
142​
143The image must have these tools:
144​
145- `xdotool` and `xdpyinfo`.
146- `scrot`. Without it, the driver takes screenshots with ImageMagick `import`, but they do not show the mouse pointer.
147​
148When no X display runs, the driver starts one with `Xvfb`. If the image has `startxfce4`, the driver also starts it.
149​
150The E2B `desktop` template has all of these. For Docker, Daytona, and the other providers, use an image with the tools. On Debian or Ubuntu:
151​
152```dockerfile
153RUN apt-get update && apt-get install -y xvfb xdotool scrot x11-utils
154```
155​
156If a tool is missing, the driver throws an error with its name.
157​
158Do you already have a desktop sandbox? See [Share one sandbox](#share-one-sandbox).
159​
160## Your own computer
161​
162If the model must use the apps on the machine that runs `chat()`, use `localComputer()`. The model moves your real mouse, presses your real keys, and reads your main screen.
163​
164> [!WARNING]
165> Watch the model while it uses your computer. It can do what you can do there: read files, send messages, and delete data. If you cannot watch it, use a [sandbox](#linux-desktop-in-a-sandbox) or a VM.
166​
167Install the packages:
168​
169<!-- ::start:tabs variant="package-manager" mode="install" -->
170​
171react: @tanstack/ai-computer-use @tanstack/ai-computer-local @jitsi/robotjs zod
172vue: @tanstack/ai-computer-use @tanstack/ai-computer-local @jitsi/robotjs zod
173solid: @tanstack/ai-computer-use @tanstack/ai-computer-local @jitsi/robotjs zod
174svelte: @tanstack/ai-computer-use @tanstack/ai-computer-local @jitsi/robotjs zod
175preact: @tanstack/ai-computer-use @tanstack/ai-computer-local @jitsi/robotjs zod
176angular: @tanstack/ai-computer-use @tanstack/ai-computer-local @jitsi/robotjs zod
177vanilla: @tanstack/ai-computer-use @tanstack/ai-computer-local @jitsi/robotjs zod
178octane: @tanstack/ai-computer-use @tanstack/ai-computer-local @jitsi/robotjs zod
179​
180<!-- ::end:tabs -->
181​
182`@jitsi/robotjs` has prebuilt binaries for Windows, macOS, and Linux. If pnpm asks to allow its install script, you can deny it.
183​
184Then do these steps:
185​
1861. On macOS, open System Settings > Privacy & Security. Give the Accessibility and Screen Recording permissions to the app that runs Node, for example your terminal.
1872. On Linux, use an X11 session. Wayland is not supported.
1883. Give `localComputer()` to `createComputerUse`:
189​
190```ts
191import { createComputerUse } from '@tanstack/ai-computer-use'
192import { localComputer } from '@tanstack/ai-computer-local'
193​
194const computerUse = createComputerUse({
195 driver: localComputer(),
196})
197```
198​
199`localComputer()` takes these options. Both are optional:
200​
201- `display`: the size of the screenshots that the model sees. The driver scales each screenshot down to this size, and scales each click up to the screen. The default is your screen size, scaled down to fit in 1280 x 800. Keep the aspect ratio of your screen.
202- `robot`: the object that moves the mouse and presses the keys. The default is `@jitsi/robotjs`. A test can give a fake with the same methods.
203​
204Your computer is one machine:
205​
206- Every thread shares it, so it serves one conversation at a time.
207- The model sees only the main screen.
208- There is no `navigate` tool. The model opens the browser on your desktop.
209- There are no `back` and `forward` mouse buttons.
210​
211## Use it in chat
212​
213With every driver, `createComputerUse` gives `tools`, `middleware`, and `interrupt`. Give them to `chat()` with the thread id:
214​
215```ts
216import { chat } from '@tanstack/ai'
217import type { ModelMessage } from '@tanstack/ai'
218import { anthropicText } from '@tanstack/ai-anthropic'
219import { createComputerUse } from '@tanstack/ai-computer-use'
220import { sandboxComputer } from '@tanstack/ai-computer-sandbox'
221import { e2bSandbox } from '@tanstack/ai-sandbox-e2b'
222​
223declare const messages: Array<ModelMessage>
224declare const threadId: string
225​
226const computerUse = createComputerUse({
227 driver: sandboxComputer({
228 provider: e2bSandbox({ template: 'desktop', timeoutMs: 60 * 60_000 }),
229 }),
230})
231​
232const stream = chat({
233 adapter: anthropicText('claude-opus-5-5'),
234 messages,
235 threadId,
236 tools: computerUse.tools,
237 middleware: [computerUse.middleware],
238 interrupts: [computerUse.interrupt],
239})
240```
241​
242The model now uses a browser page or a full Linux desktop for each thread, or your own computer. For a machine that no driver controls, write a [custom driver](./custom-driver).
243​
244## Advanced
245​
246### Share one page
247​
248You already have a Playwright page, for example in a test. Give it to `createPlaywrightComputerDriver`. Use the Playwright package that your app already has.
249​
250```ts
251import { createComputerUse } from '@tanstack/ai-computer-use'
252import { createPlaywrightComputerDriver } from '@tanstack/ai-computer-playwright'
253import type { PlaywrightPageLike } from '@tanstack/ai-computer-playwright'
254​
255// Your Playwright page. It must have a viewport size.
256declare const page: PlaywrightPageLike
257​
258const computerUse = createComputerUse({
259 driver: createPlaywrightComputerDriver({ page }),
260})
261```
262​
263- Every thread shares this page, so it serves one conversation at a time.
264- The page must have a viewport size. If it has none, the driver throws an error.
265- The driver reads the viewport size one time, when you create it.
266- A page from `playwright`, `playwright-core`, or `@playwright/test` works.
267​
268Your app owns this page. `createComputerUse` never closes it.
269​
270### Share one sandbox
271​
272You already have a desktop sandbox. Give it to `createSandboxComputerDriver`:
273​
274```ts
275import { createComputerUse } from '@tanstack/ai-computer-use'
276import { createSandboxComputerDriver } from '@tanstack/ai-computer-sandbox'
277import { e2bSandbox } from '@tanstack/ai-sandbox-e2b'
278​
279const sandbox = await e2bSandbox({ template: 'desktop' }).create({})
280​
281const computerUse = createComputerUse({
282 driver: await createSandboxComputerDriver({ sandbox }),
283})
284```
285​
286- Every thread shares this sandbox, so it serves one conversation at a time.
287- `createSandboxComputerDriver` is async. It starts the desktop if needed, and reads the screen size one time.
288​
289Your app owns this sandbox. `createComputerUse` never destroys it. When you are done, call `await sandbox.destroy()`.
290​
291### `zoom` and `cursor_position` on Anthropic
292​
293The Anthropic adapter turns on `zoom` and `cursor_position` only on the computer tool versions that have those flags:
294​
295- `computer_toolset_20260801` turns on both when the driver has the method.
296- `computer_20251124` turns on `zoom` only, and only when the driver has `zoom`.
297- `computer_20250124` turns on neither.