Skip to content

Commit 848a7e3

Browse files
authored
refactor(computer-use): one canonical wire contract + node-host provider seam (#123509)
* refactor(computer-use): add provider seam * refactor(computer-use): retry provider open after failure; drop changelog entry
1 parent 0b0fe6e commit 848a7e3

14 files changed

Lines changed: 516 additions & 249 deletions

‎docs/plugins/sdk-entrypoints.md‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,22 @@ export default definePluginEntry({
185185
startup config; command handlers should still validate availability when
186186
invoked.
187187

188+
### Computer Use providers
189+
190+
**Import:** `openclaw/plugin-sdk/computer-use`
191+
192+
Node-local Computer Use plugins register one provider through
193+
`registerComputerUseProvider(api, provider)`. The helper owns the
194+
`screen.snapshot` and dangerous `computer.act` command registrations and the
195+
matching Gateway invoke policy; the provider owns availability, execution,
196+
serialization, frame state, driver lifecycle, and cleanup.
197+
198+
The same entry point exports the canonical TypeBox schemas, static types, and
199+
compiled validators for the two command payloads and the snapshot result. A
200+
node host accepts one provider for the command pair; registering another
201+
provider conflicts with the existing command registration instead of creating
202+
a fallback stack.
203+
188204
## `defineChannelPluginEntry`
189205

190206
**Import:** `openclaw/plugin-sdk/channel-core`

‎docs/plugins/sdk-overview.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,11 @@ or fully dynamic tool registration.
154154
| `api.registerCommand(def)` | Custom command (bypasses the LLM) |
155155
| `api.registerNodeHostCommand(command)` | Command handled by `openclaw node run`; optional `agentTool` metadata can expose it as an agent-visible tool while the node is connected |
156156

157+
Computer Use providers use `registerComputerUseProvider(api, provider)` from
158+
`openclaw/plugin-sdk/computer-use`. It registers the shared
159+
`screen.snapshot`/`computer.act` node-host envelope once while the provider
160+
keeps its driver, frame, availability, and execution lifecycle local.
161+
157162
Plugin commands can set `agentPromptGuidance` when the agent needs a short,
158163
command-owned routing hint. Keep that text about the command itself; do not add
159164
provider- or plugin-specific policy to core prompt builders.

‎extensions/cua-computer/index.ts‎

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
import { registerComputerUseProvider } from "openclaw/plugin-sdk/computer-use";
12
import { buildPluginConfigSchema, definePluginEntry } from "openclaw/plugin-sdk/plugin-entry";
23
import { z } from "zod";
3-
import { createCuaComputerCommands } from "./src/commands.js";
4+
import { createCuaComputerProvider } from "./src/commands.js";
45

56
const CuaComputerConfigSchema = z.strictObject({
67
// Keep the shipped daemon setting as a named no-op: strict validation accepts
@@ -22,17 +23,6 @@ export default definePluginEntry({
2223
`Invalid cua-computer plugin config: ${parsed.error.issues[0]?.message ?? "invalid config"}`,
2324
);
2425
}
25-
for (const command of createCuaComputerCommands()) {
26-
api.registerNodeHostCommand(command);
27-
}
28-
// computer.act is dangerous-by-default and therefore also requires the
29-
// operator's explicit gateway.nodes.commands.allow entry. The plugin
30-
// policy is the final Gateway guard and the only path that may forward the
31-
// already-allowlisted invocation to the paired node.
32-
api.registerNodeInvokePolicy({
33-
commands: ["computer.act"],
34-
dangerous: true,
35-
handle: async (ctx) => await ctx.invokeNode(),
36-
});
26+
registerComputerUseProvider(api, createCuaComputerProvider());
3727
},
3828
});

‎extensions/cua-computer/src/actions.ts‎

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,5 @@
1-
import { z } from "zod";
21
import type { CuaLastFrame } from "./frame.js";
32

4-
const COMPUTER_ACTIONS = [
5-
"left_click",
6-
"right_click",
7-
"middle_click",
8-
"double_click",
9-
"triple_click",
10-
"mouse_move",
11-
"left_click_drag",
12-
"left_mouse_down",
13-
"left_mouse_up",
14-
"scroll",
15-
"type",
16-
"key",
17-
"hold_key",
18-
] as const;
19-
20-
export const ComputerActParamsSchema = z.strictObject({
21-
action: z.enum(COMPUTER_ACTIONS),
22-
displayFrameId: z.string().optional(),
23-
x: z.number().finite().nonnegative().optional(),
24-
y: z.number().finite().nonnegative().optional(),
25-
fromX: z.number().finite().nonnegative().optional(),
26-
fromY: z.number().finite().nonnegative().optional(),
27-
text: z.string().optional(),
28-
keys: z.string().optional(),
29-
modifiers: z.string().optional(),
30-
scrollDirection: z.enum(["up", "down", "left", "right"]).optional(),
31-
scrollAmount: z.number().int().positive().optional(),
32-
durationMs: z.number().int().nonnegative().optional(),
33-
screenIndex: z.number().int().nonnegative().optional(),
34-
refWidth: z.number().int().positive().optional(),
35-
});
36-
37-
export type ComputerActParams = z.infer<typeof ComputerActParamsSchema>;
38-
393
const MODIFIER_ALIASES = new Map<string, string>([
404
["ctrl", "ctrl"],
415
["control", "ctrl"],

‎extensions/cua-computer/src/commands.test.ts‎

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, expect, it, vi } from "vitest";
2-
import { createCuaComputerCommands } from "./commands.js";
2+
import { createCuaComputerProvider } from "./commands.js";
33
import {
44
ClickButton,
55
ScrollDirection,
@@ -68,25 +68,25 @@ function driver() {
6868
};
6969
}
7070

71-
function commands(session: CuaDriverSession) {
72-
return createCuaComputerCommands({
71+
async function execution(session: CuaDriverSession) {
72+
return await createCuaComputerProvider({
7373
platform: "linux",
7474
driver: session,
7575
imageProcessor: {
7676
encode: vi.fn(async () => ({ data: Buffer.from("jpeg"), width: 100, height: 50 })),
7777
},
78-
});
78+
}).openExecution({});
7979
}
8080

81-
describe("cua-computer direct SDK commands", () => {
81+
describe("cua-computer provider", () => {
8282
it("uses one typed session for snapshot and frame-authorized click", async () => {
8383
const { session, getDesktopState, getScreenSize, click } = driver();
84-
const [snapshot, act] = commands(session);
85-
const screen = JSON.parse(await snapshot!.handle('{"format":"png","maxWidth":100}')) as {
84+
const computer = await execution(session);
85+
const screen = JSON.parse(await computer.snapshot('{"format":"png","maxWidth":100}')) as {
8686
displayFrameId: string;
8787
width: number;
8888
};
89-
await act!.handle(
89+
await computer.act(
9090
JSON.stringify({
9191
action: "left_click",
9292
displayFrameId: screen.displayFrameId,
@@ -110,24 +110,24 @@ describe("cua-computer direct SDK commands", () => {
110110

111111
it("maps scroll and key through typed SDK enums", async () => {
112112
const { session, typeText, pressKey } = driver();
113-
const [, act] = commands(session);
114-
await act!.handle('{"action":"type","text":"hello"}');
115-
await act!.handle('{"action":"key","keys":"ctrl+enter"}');
113+
const computer = await execution(session);
114+
await computer.act('{"action":"type","text":"hello"}');
115+
await computer.act('{"action":"key","keys":"ctrl+enter"}');
116116
expect(typeText).toHaveBeenCalledWith("hello", undefined);
117117
expect(pressKey).toHaveBeenCalledWith({ key: "enter", modifiers: ["ctrl"] }, undefined);
118118
expect(ScrollDirection.Down).toBeTypeOf("number");
119119
});
120120

121121
it("maps all remaining projected desktop actions through direct SDK methods", async () => {
122122
const { session, scroll, moveCursor, drag } = driver();
123-
const [snapshot, act] = commands(session);
124-
const screen = JSON.parse(await snapshot!.handle('{"format":"png","maxWidth":100}')) as {
123+
const computer = await execution(session);
124+
const screen = JSON.parse(await computer.snapshot('{"format":"png","maxWidth":100}')) as {
125125
displayFrameId: string;
126126
width: number;
127127
};
128128
const frame = { displayFrameId: screen.displayFrameId, refWidth: screen.width };
129129

130-
await act!.handle(
130+
await computer.act(
131131
JSON.stringify({
132132
action: "scroll",
133133
...frame,
@@ -137,8 +137,8 @@ describe("cua-computer direct SDK commands", () => {
137137
scrollAmount: 4,
138138
}),
139139
);
140-
await act!.handle(JSON.stringify({ action: "mouse_move", ...frame, x: 11, y: 21 }));
141-
await act!.handle(
140+
await computer.act(JSON.stringify({ action: "mouse_move", ...frame, x: 11, y: 21 }));
141+
await computer.act(
142142
JSON.stringify({
143143
action: "left_click_drag",
144144
...frame,
@@ -169,13 +169,13 @@ describe("cua-computer direct SDK commands", () => {
169169
errorCode: "desktop_unavailable",
170170
text: "desktop input is unavailable",
171171
});
172-
const [snapshot, act] = commands(session);
173-
const screen = JSON.parse(await snapshot!.handle('{"format":"png","maxWidth":100}')) as {
172+
const computer = await execution(session);
173+
const screen = JSON.parse(await computer.snapshot('{"format":"png","maxWidth":100}')) as {
174174
displayFrameId: string;
175175
width: number;
176176
};
177177
await expect(
178-
act!.handle(
178+
computer.act(
179179
JSON.stringify({
180180
action: "left_click",
181181
displayFrameId: screen.displayFrameId,
@@ -189,14 +189,14 @@ describe("cua-computer direct SDK commands", () => {
189189

190190
it("rejects a mismatched reference width before desktop input", async () => {
191191
const { session, click } = driver();
192-
const [snapshot, act] = commands(session);
193-
const screen = JSON.parse(await snapshot!.handle('{"format":"png","maxWidth":100}')) as {
192+
const computer = await execution(session);
193+
const screen = JSON.parse(await computer.snapshot('{"format":"png","maxWidth":100}')) as {
194194
displayFrameId: string;
195195
width: number;
196196
};
197197

198198
await expect(
199-
act!.handle(
199+
computer.act(
200200
JSON.stringify({
201201
action: "left_click",
202202
displayFrameId: screen.displayFrameId,
@@ -213,7 +213,7 @@ describe("cua-computer direct SDK commands", () => {
213213
const { session, dispose } = driver();
214214
const createDriver = vi.fn(() => session);
215215
const clearInterval = vi.fn();
216-
const [snapshot] = createCuaComputerCommands({
216+
const provider = createCuaComputerProvider({
217217
platform: "linux",
218218
createDriver,
219219
imageProcessor: {
@@ -224,10 +224,11 @@ describe("cua-computer direct SDK commands", () => {
224224
});
225225
expect(createDriver).not.toHaveBeenCalled();
226226

227-
await snapshot!.handle('{"format":"png","maxWidth":100}');
227+
const computer = await provider.openExecution({});
228+
await computer.snapshot('{"format":"png","maxWidth":100}');
228229
expect(createDriver).toHaveBeenCalledOnce();
229230

230-
const stop = snapshot!.watchAvailability?.({ config: {} as never, env: {} }, vi.fn());
231+
const stop = provider.watchAvailability?.({ config: {} as never, env: {} }, vi.fn());
231232
stop?.();
232233
await Promise.resolve();
233234
expect(clearInterval).toHaveBeenCalledOnce();
@@ -236,12 +237,9 @@ describe("cua-computer direct SDK commands", () => {
236237

237238
it("passes node invocation cancellation to the direct SDK", async () => {
238239
const { session, getDesktopState } = driver();
239-
const [snapshot] = commands(session);
240+
const computer = await execution(session);
240241
const signal = AbortSignal.abort();
241-
await snapshot!.handle('{"format":"png","maxWidth":100}', undefined, {
242-
sendNodeEvent: vi.fn(),
243-
signal,
244-
});
242+
await computer.snapshot('{"format":"png","maxWidth":100}', signal);
245243
expect(getDesktopState).toHaveBeenCalledWith(signal);
246244
});
247245
});

0 commit comments

Comments
 (0)