Open History to see the last commit
1---
2title: Custom Computer Use Driver
3id: computer-use-custom-driver
4order: 4
5description: "Let the model use your own machine. Write a ComputerDriver with a small set of mouse, keyboard, and screenshot methods, and a ComputerDriverFactory to give each thread its own machine."
6keywords:
7 - tanstack ai
8 - computer use
9 - ComputerDriver
10 - ComputerDriverFactory
11 - custom driver
12 - virtual machine
13---
14
15You have your own machine: a VM, a remote desktop, or a device in a lab. No driver package controls it. Write a `ComputerDriver`, and [computer use](./computer-use) runs on it with no other change.
16
17## Write the driver
18
19A driver is an object with a screen size and a small set of methods. This example sends each method to an HTTP agent that runs on the machine.
20
21```ts
22// app/my-driver.ts
23import type { ComputerDriver } from '@tanstack/ai-computer-use'
24
25export function createMyDriver(base: string) {
26 async function send(path: string, body: object) {
27 const response = await fetch(`${base}${path}`, {
28 method: 'POST',
29 headers: { 'Content-Type': 'application/json' },
30 body: JSON.stringify(body),
31 })
32 if (!response.ok) throw new Error(`${path} failed: ${response.status}`)
33 }
34
35 const driver: ComputerDriver = {
36 display: { width: 1920, height: 1080 },
37 environment: 'windows',
38 async screenshot() {
39 const response = await fetch(`${base}/screenshot`)
40 if (!response.ok) throw new Error(`/screenshot failed: ${response.status}`)
41 return new Uint8Array(await response.arrayBuffer())
42 },
43 click: (x, y, { button, count }) => send('/click', { x, y, button, count }),
44 move: (x, y) => send('/move', { x, y }),
45 mouseDown: (button) => send('/mouse-down', { button }),
46 mouseUp: (button) => send('/mouse-up', { button }),
47 scroll: (x, y, deltaX, deltaY) => send('/scroll', { x, y, deltaX, deltaY }),
48 keyDown: (key) => send('/key-down', { key }),
49 keyUp: (key) => send('/key-up', { key }),
50 press: (keys) => send('/press', { keys }),
51 type: (text) => send('/type', { text }),
52 }
53 return driver
54}
55```
56
57Then give it to `createComputerUse`:
58
59```ts
60import { createComputerUse } from '@tanstack/ai-computer-use'
61import type { ComputerDriver } from '@tanstack/ai-computer-use'
62
63// From app/my-driver.ts above.
64declare function createMyDriver(base: string): ComputerDriver
65
66const computerUse = createComputerUse({
67 driver: createMyDriver('http://localhost:8000'),
68})
69```
70
71A driver is one machine. Every thread shares it, so it serves one conversation at a time. `createComputerUse` never closes it. To give each thread its own machine, write a [driver factory](#one-machine-for-each-thread).
72
73## The contract
74
75Coordinates are in pixels, in `display` space. Key names are `KeyboardEvent.key` names, for example `'Enter'`, `'Control'`, or `'a'`.
76
77| Member | What it does |
78| --- | --- |
79| `display` | The screen size in pixels. Screenshots have this size. |
80| `environment` | `'browser'`, `'linux'`, `'mac'`, or `'windows'`. |
81| `screenshot()` | Returns PNG bytes as a `Uint8Array`. |
82| `click(x, y, { button, count })` | Clicks `count` times. `button` is `left`, `right`, `middle`, `back`, or `forward`. |
83| `move(x, y)` | Moves the mouse. |
84| `mouseDown(button)`, `mouseUp(button)` | Presses or releases a button at the mouse position. |
85| `scroll(x, y, deltaX, deltaY)` | Scrolls at the point. The deltas are in pixels. |
86| `keyDown(key)`, `keyUp(key)` | Presses or releases one key. |
87| `press(keys)` | Presses one combo, like `['Control', 's']`. |
88| `type(text)` | Types text as a user does. |
89
90## Add the optional methods
91
92Four methods are optional:
93
94- `zoom(region)`: returns PNG bytes of one region at full size. The region has `x`, `y`, `width`, and `height`. `computer_toolset_20260801` and `computer_20251124` turn on `zoom` when the driver has this method. `computer_20250124` does not.
95- `cursorPosition()`: returns the mouse position as `{ x, y }`. Only `computer_toolset_20260801` turns on `cursor_position`, and only when the driver has this method.
96- `navigate(url)`: opens the URL. `computerUse.tools` then has the `navigate` tool. The tool accepts only `http:` and `https:` URLs.
97- `close()`: frees the machine. `createComputerUse` calls it only on a driver that a factory made.
98
99Add this function to `app/my-driver.ts`. It narrows the JSON from your agent before it returns it:
100
101```ts
102export async function readCursorPosition(base: string) {
103 const response = await fetch(`${base}/cursor`)
104 const data: unknown = await response.json()
105 const isPoint =
106 typeof data === 'object' &&
107 data !== null &&
108 'x' in data &&
109 'y' in data &&
110 typeof data.x === 'number' &&
111 typeof data.y === 'number'
112 if (!isPoint) throw new Error('The agent sent no cursor position.')
113 return { x: data.x, y: data.y }
114}
115```
116
117Then add `cursorPosition: () => readCursorPosition(base)` to `driver` in `createMyDriver`.
118
119## One machine for each thread
120
121One shared machine serves one conversation at a time. To give each thread its own machine, write a `ComputerDriverFactory`. `createComputerUse` calls `create` on the first action of each thread.
122
123This example asks a pool service for one VM for each thread, and stops the VM when the thread closes:
124
125```ts
126// app/my-machines.ts
127import type {
128 ComputerDriver,
129 ComputerDriverFactory,
130} from '@tanstack/ai-computer-use'
131
132// From app/my-driver.ts above.
133declare function createMyDriver(base: string): ComputerDriver
134
135const pool = 'http://localhost:9000'
136
137export const myMachines: ComputerDriverFactory = {
138 display: { width: 1920, height: 1080 },
139 environment: 'windows',
140 capabilities: { zoom: false, cursorPosition: false, navigate: false },
141 async create({ threadId }) {
142 const machine = `${pool}/machines/${encodeURIComponent(threadId)}`
143 const response = await fetch(machine, { method: 'PUT' })
144 if (!response.ok) throw new Error(`Start failed: ${response.status}`)
145 return {
146 ...createMyDriver(`${machine}/agent`),
147 close: async () => {
148 await fetch(machine, { method: 'DELETE' })
149 },
150 }
151 },
152}
153```
154
155Then give the factory to `createComputerUse`:
156
157```ts
158import { createComputerUse } from '@tanstack/ai-computer-use'
159import type { ComputerDriverFactory } from '@tanstack/ai-computer-use'
160
161// From app/my-machines.ts above.
162declare const myMachines: ComputerDriverFactory
163
164const computerUse = createComputerUse({ driver: myMachines })
165```
166
167The factory has these members:
168
169| Member | What it does |
170| --- | --- |
171| `display` | The screen size of every machine. Each driver from `create` has this `display`. |
172| `environment` | The kind of every machine. |
173| `capabilities` | `{ zoom, cursorPosition, navigate }`: the optional methods that every driver from `create` has. The tool is built before any machine exists, so the factory tells it. |
174| `create({ threadId })` | Makes the driver of one thread. If it throws, the model gets the error. The next model turn calls `create` again. |
175| `close()` | Optional. Frees what the threads share. `computerUse.close()` calls it after it closes every thread. |
176
177When a thread closes or goes idle, `createComputerUse` calls `close()` on the driver of that thread.
178
179## What the core builds for you
180
181Your driver does only the small steps. The core builds the rest from them:
182
183- **Drags**: `move`, `mouseDown`, more `move` calls, then `mouseUp`.
184- **Modifier clicks and scrolls**: `keyDown` for each modifier, the action, then `keyUp` in reverse order.
185- **`hold_key` and `wait`**: the core holds the keys or waits, then continues.
186- **Key names**: the core changes OpenAI and Anthropic key names to `KeyboardEvent.key` names.
187- **Screenshots**: after a normal action, the core waits `screenshotDelay` ms and calls `screenshot()`. An OpenAI batch waits once, after the last action, then takes one screenshot. `zoom` and `cursor_position` return their own result. The core does not call `screenshot()` for those two.
188
189## Try it
190
191Start your agent on the machine, and send a task in the chat. Your agent gets the first `/screenshot` request, then the clicks and the key presses. The model now uses your own machine.