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

drivers.md

266 lines12.3 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 browser page is not always enough. Sometimes the model must use a desktop app, a file manager, or a terminal. A driver decides which machine the model uses. Change the driver, and the rest of your [computer use](./computer-use) code stays the same.
23​
24## Pick a driver
25​
26| | Playwright | Sandbox | Your own computer |
27| --------------------- | ----------------------------------------- | --------------------------------------- | ------------------------------------------ |
28| **Machine** | A browser page for each thread | A Linux desktop sandbox for each thread | Your mouse, keyboard, and main screen |
29| **Environment** | `browser` | `linux` | `windows`, `mac`, or `linux` |
30| **Runs on** | Your server | Any sandbox provider | The machine that runs `chat()` |
31| **`navigate` tool** | Yes | No | No |
32| **`zoom`** | Yes | No | Yes |
33| **`cursor_position`** | No | Yes | Yes |
34| **Setup** | `playwright-core`, then download Chromium | A provider and a desktop image | `@jitsi/robotjs`, and permissions on macOS |
35​
36The Anthropic adapter turns on `zoom` and `cursor_position` only on the computer versions that have those flags.
37​
38- `computer_toolset_20260801` turns on both when the driver has the method.
39- `computer_20251124` turns on `zoom` only, and only when the driver has `zoom`.
40- `computer_20250124` turns on neither.
41​
42## Playwright
43​
44If the task stays inside a web page, use Playwright.
45​
46<!-- ::start:tabs variant="package-manager" mode="install" -->
47​
48react: @tanstack/ai-computer-use @tanstack/ai-computer-playwright playwright-core zod
49vue: @tanstack/ai-computer-use @tanstack/ai-computer-playwright playwright-core zod
50solid: @tanstack/ai-computer-use @tanstack/ai-computer-playwright playwright-core zod
51svelte: @tanstack/ai-computer-use @tanstack/ai-computer-playwright playwright-core zod
52preact: @tanstack/ai-computer-use @tanstack/ai-computer-playwright playwright-core zod
53angular: @tanstack/ai-computer-use @tanstack/ai-computer-playwright playwright-core zod
54vanilla: @tanstack/ai-computer-use @tanstack/ai-computer-playwright playwright-core zod
55octane: @tanstack/ai-computer-use @tanstack/ai-computer-playwright playwright-core zod
56​
57<!-- ::end:tabs -->
58​
59Then download Chromium one time:
60​
61```bash
62npx playwright-core install chromium
63```
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​
88### Share one page
89​
90You already have a Playwright page, for example in a test. Give it to `createPlaywrightComputerDriver`. Use the Playwright package that your app already has.
91​
92```ts
93import { createComputerUse } from '@tanstack/ai-computer-use'
94import { createPlaywrightComputerDriver } from '@tanstack/ai-computer-playwright'
95import type { PlaywrightPageLike } from '@tanstack/ai-computer-playwright'
96​
97// Your Playwright page. It must have a viewport size.
98declare const page: PlaywrightPageLike
99​
100const computerUse = createComputerUse({
101 driver: createPlaywrightComputerDriver({ page }),
102})
103```
104​
105- Every thread shares this page, so it serves one conversation at a time.
106- The page must have a viewport size. If it has none, the driver throws an error.
107- The driver reads the viewport size one time, when you create it.
108- A page from `playwright`, `playwright-core`, or `@playwright/test` works.
109​
110Your app owns this page. `createComputerUse` never closes it.
111​
112## Linux desktop in a sandbox
113​
114If 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.
115​
116<!-- ::start:tabs variant="package-manager" mode="install" -->
117​
118react: @tanstack/ai-computer-use @tanstack/ai-computer-sandbox @tanstack/ai-sandbox @tanstack/ai-sandbox-e2b zod
119vue: @tanstack/ai-computer-use @tanstack/ai-computer-sandbox @tanstack/ai-sandbox @tanstack/ai-sandbox-e2b zod
120solid: @tanstack/ai-computer-use @tanstack/ai-computer-sandbox @tanstack/ai-sandbox @tanstack/ai-sandbox-e2b zod
121svelte: @tanstack/ai-computer-use @tanstack/ai-computer-sandbox @tanstack/ai-sandbox @tanstack/ai-sandbox-e2b zod
122preact: @tanstack/ai-computer-use @tanstack/ai-computer-sandbox @tanstack/ai-sandbox @tanstack/ai-sandbox-e2b zod
123angular: @tanstack/ai-computer-use @tanstack/ai-computer-sandbox @tanstack/ai-sandbox @tanstack/ai-sandbox-e2b zod
124vanilla: @tanstack/ai-computer-use @tanstack/ai-computer-sandbox @tanstack/ai-sandbox @tanstack/ai-sandbox-e2b zod
125octane: @tanstack/ai-computer-use @tanstack/ai-computer-sandbox @tanstack/ai-sandbox @tanstack/ai-sandbox-e2b zod
126​
127<!-- ::end:tabs -->
128​
1291. Set `E2B_API_KEY` in your server environment.
1302. Give `sandboxComputer()` a provider that makes desktop sandboxes. The E2B `desktop` template is one.
131​
132```ts
133import { createComputerUse } from '@tanstack/ai-computer-use'
134import { sandboxComputer } from '@tanstack/ai-computer-sandbox'
135import { e2bSandbox } from '@tanstack/ai-sandbox-e2b'
136​
137const computerUse = createComputerUse({
138 driver: sandboxComputer({
139 provider: e2bSandbox({ template: 'desktop', timeoutMs: 60 * 60_000 }),
140 }),
141})
142```
143​
144`sandboxComputer()` takes these options:
145​
146- `provider`: the sandbox provider. It makes one sandbox for each thread.
147- `display`: the screen size of each desktop in pixels. The default is 1024 x 768.
148​
149CAUTION: 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.
150​
151- There is no `navigate` tool, because a desktop has no address. The model opens the browser on the desktop.
152- When a thread closes or goes idle, `createComputerUse` destroys its sandbox.
153- One scroll step is 100 pixels.
154- The driver quotes every key name and all typed text before they go to the shell.
155​
156### The sandbox image
157​
158The image must have `xdotool`, `xdpyinfo`, and `scrot`. Without `scrot`, the driver takes screenshots with ImageMagick `import`, but they do not show the mouse pointer. When no X display runs, the driver starts one with `Xvfb`. It also starts `startxfce4` when the image has it.
159​
160The E2B `desktop` template has all of these. For Docker, Daytona, and the other providers, use an image with the tools. On Debian or Ubuntu:
161​
162```dockerfile
163RUN apt-get update && apt-get install -y xvfb xdotool scrot x11-utils
164```
165​
166If a tool is missing, the driver throws an error with its name.
167​
168### Share one sandbox
169​
170You already have a desktop sandbox. Give it to `createSandboxComputerDriver`:
171​
172```ts
173import { createComputerUse } from '@tanstack/ai-computer-use'
174import { createSandboxComputerDriver } from '@tanstack/ai-computer-sandbox'
175import { e2bSandbox } from '@tanstack/ai-sandbox-e2b'
176​
177const sandbox = await e2bSandbox({ template: 'desktop' }).create({})
178​
179const computerUse = createComputerUse({
180 driver: await createSandboxComputerDriver({ sandbox }),
181})
182```
183​
184- Every thread shares this sandbox, so it serves one conversation at a time.
185- `createSandboxComputerDriver` is async. It starts the desktop if needed, and reads the screen size one time.
186​
187Your app owns this sandbox. `createComputerUse` never destroys it. When you are done, call `await sandbox.destroy()`.
188​
189## Your own computer
190​
191If 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.
192​
193CAUTION: 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.
194​
195<!-- ::start:tabs variant="package-manager" mode="install" -->
196​
197react: @tanstack/ai-computer-use @tanstack/ai-computer-local @jitsi/robotjs zod
198vue: @tanstack/ai-computer-use @tanstack/ai-computer-local @jitsi/robotjs zod
199solid: @tanstack/ai-computer-use @tanstack/ai-computer-local @jitsi/robotjs zod
200svelte: @tanstack/ai-computer-use @tanstack/ai-computer-local @jitsi/robotjs zod
201preact: @tanstack/ai-computer-use @tanstack/ai-computer-local @jitsi/robotjs zod
202angular: @tanstack/ai-computer-use @tanstack/ai-computer-local @jitsi/robotjs zod
203vanilla: @tanstack/ai-computer-use @tanstack/ai-computer-local @jitsi/robotjs zod
204octane: @tanstack/ai-computer-use @tanstack/ai-computer-local @jitsi/robotjs zod
205​
206<!-- ::end:tabs -->
207​
208`@jitsi/robotjs` has prebuilt binaries for Windows, macOS, and Linux. If pnpm asks to allow its install script, you can deny it.
209​
2101. On macOS, open System Settings > Privacy & Security. Give the Accessibility and Screen Recording permissions to the app that runs Node, for example your terminal.
2112. On Linux, use an X11 session. Wayland is not supported.
2123. Give `localComputer()` to `createComputerUse`.
213​
214```ts
215import { createComputerUse } from '@tanstack/ai-computer-use'
216import { localComputer } from '@tanstack/ai-computer-local'
217​
218const computerUse = createComputerUse({
219 driver: localComputer(),
220})
221```
222​
223`localComputer()` takes these options. Both are optional:
224​
225- `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.
226- `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.
227​
228Your computer is one machine:
229​
230- Every thread shares it, so it serves one conversation at a time.
231- The model sees only the main screen.
232- There is no `navigate` tool. The model opens the browser on your desktop.
233- There are no `back` and `forward` mouse buttons.
234​
235## Use it in chat
236​
237With every driver, `createComputerUse` gives `tools`, `middleware`, and `interrupt`. Give them to `chat()` with the thread id:
238​
239```ts
240import { chat } from '@tanstack/ai'
241import type { ModelMessage } from '@tanstack/ai'
242import { anthropicText } from '@tanstack/ai-anthropic'
243import { createComputerUse } from '@tanstack/ai-computer-use'
244import { sandboxComputer } from '@tanstack/ai-computer-sandbox'
245import { e2bSandbox } from '@tanstack/ai-sandbox-e2b'
246​
247declare const messages: Array<ModelMessage>
248declare const threadId: string
249​
250const computerUse = createComputerUse({
251 driver: sandboxComputer({
252 provider: e2bSandbox({ template: 'desktop', timeoutMs: 60 * 60_000 }),
253 }),
254})
255​
256const stream = chat({
257 adapter: anthropicText('claude-opus-5-5'),
258 messages,
259 threadId,
260 tools: computerUse.tools,
261 middleware: [computerUse.middleware],
262 interrupts: [computerUse.interrupt],
263})
264```
265​
266The 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).