screenhand 0.1.1 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +458 -93
- package/dist/.audit-log.jsonl +55 -0
- package/dist/.screenhand/memory/.lock +1 -0
- package/dist/.screenhand/memory/actions.jsonl +85 -0
- package/dist/.screenhand/memory/errors.jsonl +5 -0
- package/dist/.screenhand/memory/errors.jsonl.bak +4 -0
- package/dist/.screenhand/memory/state.json +35 -0
- package/dist/.screenhand/memory/state.json.bak +35 -0
- package/dist/.screenhand/memory/strategies.jsonl +12 -0
- package/dist/agent/cli.js +73 -0
- package/dist/agent/loop.js +258 -0
- package/dist/config.js +9 -0
- package/dist/index.js +56 -0
- package/dist/logging/timeline-logger.js +29 -0
- package/dist/mcp/mcp-stdio-server.js +448 -0
- package/dist/mcp/server.js +347 -0
- package/dist/mcp-desktop.js +2731 -0
- package/dist/mcp-entry.js +59 -0
- package/dist/memory/recall.js +160 -0
- package/dist/memory/research.js +98 -0
- package/dist/memory/seeds.js +89 -0
- package/dist/memory/session.js +161 -0
- package/dist/memory/store.js +391 -0
- package/dist/memory/types.js +4 -0
- package/dist/monitor/codex-monitor.js +377 -0
- package/dist/monitor/task-queue.js +84 -0
- package/dist/monitor/types.js +49 -0
- package/dist/native/bridge-client.js +174 -0
- package/dist/native/macos-bridge-client.js +5 -0
- package/dist/npm-publish-helper.js +117 -0
- package/dist/npm-token-cdp.js +113 -0
- package/dist/npm-token-create.js +135 -0
- package/dist/npm-token-finish.js +126 -0
- package/dist/playbook/engine.js +193 -0
- package/dist/playbook/index.js +4 -0
- package/dist/playbook/recorder.js +519 -0
- package/dist/playbook/runner.js +392 -0
- package/dist/playbook/store.js +166 -0
- package/dist/playbook/types.js +4 -0
- package/dist/runtime/accessibility-adapter.js +377 -0
- package/dist/runtime/app-adapter.js +48 -0
- package/dist/runtime/applescript-adapter.js +283 -0
- package/dist/runtime/ax-role-map.js +80 -0
- package/dist/runtime/browser-adapter.js +36 -0
- package/dist/runtime/cdp-chrome-adapter.js +505 -0
- package/dist/runtime/composite-adapter.js +205 -0
- package/dist/runtime/executor.js +250 -0
- package/dist/runtime/locator-cache.js +12 -0
- package/dist/runtime/planning-loop.js +47 -0
- package/dist/runtime/service.js +372 -0
- package/dist/runtime/session-manager.js +28 -0
- package/dist/runtime/state-observer.js +105 -0
- package/dist/runtime/vision-adapter.js +208 -0
- package/dist/scripts/codex-monitor-daemon.js +335 -0
- package/dist/scripts/supervisor-daemon.js +272 -0
- package/dist/scripts/worker-daemon.js +228 -0
- package/dist/src/agent/cli.js +82 -0
- package/dist/src/agent/loop.js +274 -0
- package/{src/config.ts → dist/src/config.js} +5 -10
- package/{src/index.ts → dist/src/index.js} +32 -52
- package/dist/src/jobs/manager.js +237 -0
- package/dist/src/jobs/runner.js +683 -0
- package/dist/src/jobs/store.js +102 -0
- package/dist/src/jobs/types.js +30 -0
- package/dist/src/jobs/worker.js +97 -0
- package/dist/src/logging/timeline-logger.js +45 -0
- package/dist/src/mcp/mcp-stdio-server.js +464 -0
- package/dist/src/mcp/server.js +363 -0
- package/dist/src/mcp-entry.js +60 -0
- package/dist/src/memory/recall.js +170 -0
- package/dist/src/memory/research.js +104 -0
- package/dist/src/memory/seeds.js +101 -0
- package/dist/src/memory/service.js +421 -0
- package/dist/src/memory/session.js +169 -0
- package/dist/src/memory/store.js +422 -0
- package/dist/src/memory/types.js +17 -0
- package/dist/src/monitor/codex-monitor.js +382 -0
- package/dist/src/monitor/task-queue.js +97 -0
- package/dist/src/monitor/types.js +62 -0
- package/dist/src/native/bridge-client.js +190 -0
- package/{src/native/macos-bridge-client.ts → dist/src/native/macos-bridge-client.js} +0 -1
- package/dist/src/playbook/engine.js +201 -0
- package/dist/src/playbook/index.js +20 -0
- package/dist/src/playbook/recorder.js +535 -0
- package/dist/src/playbook/runner.js +408 -0
- package/dist/src/playbook/store.js +183 -0
- package/dist/src/playbook/types.js +17 -0
- package/dist/src/runtime/accessibility-adapter.js +393 -0
- package/dist/src/runtime/app-adapter.js +64 -0
- package/dist/src/runtime/applescript-adapter.js +299 -0
- package/dist/src/runtime/ax-role-map.js +96 -0
- package/dist/src/runtime/browser-adapter.js +52 -0
- package/dist/src/runtime/cdp-chrome-adapter.js +521 -0
- package/dist/src/runtime/composite-adapter.js +221 -0
- package/dist/src/runtime/execution-contract.js +159 -0
- package/dist/src/runtime/executor.js +266 -0
- package/{src/runtime/locator-cache.ts → dist/src/runtime/locator-cache.js} +10 -15
- package/dist/src/runtime/planning-loop.js +63 -0
- package/dist/src/runtime/service.js +388 -0
- package/dist/src/runtime/session-manager.js +60 -0
- package/dist/src/runtime/state-observer.js +121 -0
- package/dist/src/runtime/vision-adapter.js +224 -0
- package/dist/src/supervisor/locks.js +186 -0
- package/dist/src/supervisor/supervisor.js +403 -0
- package/dist/src/supervisor/types.js +30 -0
- package/dist/src/test-mcp-protocol.js +154 -0
- package/dist/src/types.js +17 -0
- package/dist/src/util/atomic-write.js +118 -0
- package/dist/test-mcp-protocol.js +138 -0
- package/dist/types.js +1 -0
- package/package.json +18 -4
- package/.claude/commands/automate.md +0 -28
- package/.claude/commands/debug-ui.md +0 -19
- package/.claude/commands/screenshot.md +0 -15
- package/.github/FUNDING.yml +0 -1
- package/.github/ISSUE_TEMPLATE/bug_report.md +0 -27
- package/.github/ISSUE_TEMPLATE/feature_request.md +0 -20
- package/.mcp.json +0 -8
- package/DESKTOP_MCP_GUIDE.md +0 -92
- package/SECURITY.md +0 -44
- package/docs/architecture.md +0 -47
- package/install-skills.sh +0 -19
- package/mcp-bridge.ts +0 -271
- package/mcp-desktop.ts +0 -1221
- package/native/macos-bridge/Package.swift +0 -21
- package/native/macos-bridge/Sources/AccessibilityBridge.swift +0 -261
- package/native/macos-bridge/Sources/AppManagement.swift +0 -129
- package/native/macos-bridge/Sources/CoreGraphicsBridge.swift +0 -242
- package/native/macos-bridge/Sources/ObserverBridge.swift +0 -120
- package/native/macos-bridge/Sources/VisionBridge.swift +0 -80
- package/native/macos-bridge/Sources/main.swift +0 -345
- package/native/windows-bridge/AppManagement.cs +0 -234
- package/native/windows-bridge/InputBridge.cs +0 -436
- package/native/windows-bridge/Program.cs +0 -265
- package/native/windows-bridge/ScreenCapture.cs +0 -329
- package/native/windows-bridge/UIAutomationBridge.cs +0 -571
- package/native/windows-bridge/WindowsBridge.csproj +0 -17
- package/playbooks/devpost.json +0 -186
- package/playbooks/instagram.json +0 -41
- package/playbooks/instagram_v2.json +0 -201
- package/playbooks/x_v1.json +0 -211
- package/scripts/devpost-live-loop.mjs +0 -421
- package/src/logging/timeline-logger.ts +0 -55
- package/src/mcp/server.ts +0 -449
- package/src/memory/recall.ts +0 -191
- package/src/memory/research.ts +0 -146
- package/src/memory/seeds.ts +0 -123
- package/src/memory/session.ts +0 -201
- package/src/memory/store.ts +0 -434
- package/src/memory/types.ts +0 -69
- package/src/native/bridge-client.ts +0 -239
- package/src/runtime/accessibility-adapter.ts +0 -487
- package/src/runtime/app-adapter.ts +0 -169
- package/src/runtime/applescript-adapter.ts +0 -376
- package/src/runtime/ax-role-map.ts +0 -102
- package/src/runtime/browser-adapter.ts +0 -129
- package/src/runtime/cdp-chrome-adapter.ts +0 -676
- package/src/runtime/composite-adapter.ts +0 -274
- package/src/runtime/executor.ts +0 -396
- package/src/runtime/planning-loop.ts +0 -81
- package/src/runtime/service.ts +0 -448
- package/src/runtime/session-manager.ts +0 -50
- package/src/runtime/state-observer.ts +0 -136
- package/src/runtime/vision-adapter.ts +0 -297
- package/src/types.ts +0 -297
- package/tests/bridge-client.test.ts +0 -176
- package/tests/browser-stealth.test.ts +0 -210
- package/tests/composite-adapter.test.ts +0 -64
- package/tests/mcp-server.test.ts +0 -151
- package/tests/memory-recall.test.ts +0 -339
- package/tests/memory-research.test.ts +0 -159
- package/tests/memory-seeds.test.ts +0 -120
- package/tests/memory-store.test.ts +0 -392
- package/tests/types.test.ts +0 -92
- package/tsconfig.check.json +0 -17
- package/tsconfig.json +0 -19
- package/vitest.config.ts +0 -8
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
// Copyright (C) 2025 Clazro Technology Private Limited
|
|
2
|
+
// SPDX-License-Identifier: AGPL-3.0-only
|
|
3
|
+
//
|
|
4
|
+
// This file is part of ScreenHand.
|
|
5
|
+
//
|
|
6
|
+
// ScreenHand is free software: you can redistribute it and/or modify
|
|
7
|
+
// it under the terms of the GNU Affero General Public License as
|
|
8
|
+
// published by the Free Software Foundation, version 3.
|
|
9
|
+
//
|
|
10
|
+
// ScreenHand is distributed in the hope that it will be useful,
|
|
11
|
+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12
|
+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
13
|
+
// GNU Affero General Public License for more details.
|
|
14
|
+
//
|
|
15
|
+
// You should have received a copy of the GNU Affero General Public License
|
|
16
|
+
// along with ScreenHand. If not, see <https://www.gnu.org/licenses/>.
|
|
17
|
+
import { spawn } from "node:child_process";
|
|
18
|
+
import { EventEmitter } from "node:events";
|
|
19
|
+
import path from "node:path";
|
|
20
|
+
import { createInterface } from "node:readline";
|
|
21
|
+
/**
|
|
22
|
+
* Per-method timeout overrides (ms).
|
|
23
|
+
* Methods not listed here use the default 10s timeout.
|
|
24
|
+
*/
|
|
25
|
+
const METHOD_TIMEOUTS = {
|
|
26
|
+
"app.launch": 30_000,
|
|
27
|
+
"cg.captureScreen": 15_000,
|
|
28
|
+
"cg.captureWindow": 15_000,
|
|
29
|
+
"vision.ocr": 20_000,
|
|
30
|
+
"vision.findText": 20_000,
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* Resolves the correct native bridge binary path for the current platform.
|
|
34
|
+
*/
|
|
35
|
+
function defaultBinaryPath() {
|
|
36
|
+
// import.meta.dirname is Node 20+; for Node 18 derive from import.meta.url
|
|
37
|
+
const base = import.meta.dirname ?? path.dirname(new URL(import.meta.url).pathname);
|
|
38
|
+
if (process.platform === "win32") {
|
|
39
|
+
return path.resolve(base, "../../native/windows-bridge/bin/Release/net8.0-windows/windows-bridge.exe");
|
|
40
|
+
}
|
|
41
|
+
// macOS (default)
|
|
42
|
+
return path.resolve(base, "../../native/macos-bridge/.build/release/macos-bridge");
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Platform-aware native bridge client.
|
|
46
|
+
* Spawns the correct bridge binary (macOS Swift or Windows C#) based on the OS,
|
|
47
|
+
* communicating via the same JSON-RPC-over-stdio protocol.
|
|
48
|
+
*
|
|
49
|
+
* Drop-in replacement for the original MacOSBridgeClient.
|
|
50
|
+
*/
|
|
51
|
+
export class BridgeClient extends EventEmitter {
|
|
52
|
+
process = null;
|
|
53
|
+
nextId = 1;
|
|
54
|
+
pending = new Map();
|
|
55
|
+
binaryPath;
|
|
56
|
+
restarting = false;
|
|
57
|
+
started = false;
|
|
58
|
+
constructor(binaryPath) {
|
|
59
|
+
super();
|
|
60
|
+
this.binaryPath = binaryPath ?? defaultBinaryPath();
|
|
61
|
+
}
|
|
62
|
+
async start() {
|
|
63
|
+
if (this.started)
|
|
64
|
+
return;
|
|
65
|
+
await this.spawn();
|
|
66
|
+
this.started = true;
|
|
67
|
+
}
|
|
68
|
+
async stop() {
|
|
69
|
+
this.started = false;
|
|
70
|
+
if (this.process) {
|
|
71
|
+
this.process.kill();
|
|
72
|
+
this.process = null;
|
|
73
|
+
}
|
|
74
|
+
// Reject all pending
|
|
75
|
+
for (const [id, pending] of this.pending) {
|
|
76
|
+
clearTimeout(pending.timer);
|
|
77
|
+
pending.reject(new Error("Bridge stopped"));
|
|
78
|
+
this.pending.delete(id);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
async call(method, params, timeoutMs) {
|
|
82
|
+
const effectiveTimeout = timeoutMs ?? METHOD_TIMEOUTS[method] ?? 10_000;
|
|
83
|
+
if (!this.process || this.process.exitCode !== null) {
|
|
84
|
+
await this.restart();
|
|
85
|
+
}
|
|
86
|
+
const id = this.nextId++;
|
|
87
|
+
const request = { id, method };
|
|
88
|
+
if (params) {
|
|
89
|
+
request.params = params;
|
|
90
|
+
}
|
|
91
|
+
return new Promise((resolve, reject) => {
|
|
92
|
+
const timer = setTimeout(() => {
|
|
93
|
+
this.pending.delete(id);
|
|
94
|
+
reject(new Error(`Bridge call "${method}" timed out after ${effectiveTimeout}ms`));
|
|
95
|
+
}, effectiveTimeout);
|
|
96
|
+
this.pending.set(id, {
|
|
97
|
+
resolve: resolve,
|
|
98
|
+
reject,
|
|
99
|
+
timer,
|
|
100
|
+
});
|
|
101
|
+
const line = JSON.stringify(request) + "\n";
|
|
102
|
+
this.process.stdin.write(line);
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
async ping() {
|
|
106
|
+
return this.call("ping");
|
|
107
|
+
}
|
|
108
|
+
async checkPermissions() {
|
|
109
|
+
return this.call("check_permissions");
|
|
110
|
+
}
|
|
111
|
+
async spawn() {
|
|
112
|
+
const child = spawn(this.binaryPath, [], {
|
|
113
|
+
stdio: ["pipe", "pipe", "pipe"],
|
|
114
|
+
});
|
|
115
|
+
child.on("error", (err) => {
|
|
116
|
+
this.emit("error", err);
|
|
117
|
+
if (this.started) {
|
|
118
|
+
this.restart().catch(() => { });
|
|
119
|
+
}
|
|
120
|
+
});
|
|
121
|
+
child.on("exit", (code) => {
|
|
122
|
+
this.emit("exit", code);
|
|
123
|
+
if (this.started && !this.restarting) {
|
|
124
|
+
this.restart().catch(() => { });
|
|
125
|
+
}
|
|
126
|
+
});
|
|
127
|
+
// Parse stdout line by line
|
|
128
|
+
const rl = createInterface({ input: child.stdout });
|
|
129
|
+
rl.on("line", (line) => {
|
|
130
|
+
this.handleLine(line);
|
|
131
|
+
});
|
|
132
|
+
// Log stderr
|
|
133
|
+
child.stderr?.on("data", (data) => {
|
|
134
|
+
this.emit("stderr", data.toString());
|
|
135
|
+
});
|
|
136
|
+
this.process = child;
|
|
137
|
+
}
|
|
138
|
+
handleLine(line) {
|
|
139
|
+
let response;
|
|
140
|
+
try {
|
|
141
|
+
response = JSON.parse(line);
|
|
142
|
+
}
|
|
143
|
+
catch {
|
|
144
|
+
return; // Ignore malformed lines
|
|
145
|
+
}
|
|
146
|
+
// Event (streaming notification from observer)
|
|
147
|
+
if (response.event) {
|
|
148
|
+
this.emit("ax-event", response.event);
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
// Response to a pending request
|
|
152
|
+
const pending = this.pending.get(response.id);
|
|
153
|
+
if (!pending)
|
|
154
|
+
return;
|
|
155
|
+
this.pending.delete(response.id);
|
|
156
|
+
clearTimeout(pending.timer);
|
|
157
|
+
if (response.error) {
|
|
158
|
+
pending.reject(new Error(response.error.message));
|
|
159
|
+
}
|
|
160
|
+
else {
|
|
161
|
+
pending.resolve(response.result);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
async restart() {
|
|
165
|
+
if (this.restarting)
|
|
166
|
+
return;
|
|
167
|
+
this.restarting = true;
|
|
168
|
+
// Reject all pending requests
|
|
169
|
+
for (const [id, pending] of this.pending) {
|
|
170
|
+
clearTimeout(pending.timer);
|
|
171
|
+
pending.reject(new Error("Bridge process crashed, restarting"));
|
|
172
|
+
this.pending.delete(id);
|
|
173
|
+
}
|
|
174
|
+
try {
|
|
175
|
+
if (this.process) {
|
|
176
|
+
this.process.kill();
|
|
177
|
+
this.process = null;
|
|
178
|
+
}
|
|
179
|
+
await this.spawn();
|
|
180
|
+
this.emit("restart");
|
|
181
|
+
}
|
|
182
|
+
finally {
|
|
183
|
+
this.restarting = false;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* @deprecated Use BridgeClient instead. This alias exists for backward compatibility.
|
|
189
|
+
*/
|
|
190
|
+
export const MacOSBridgeClient = BridgeClient;
|
|
@@ -14,7 +14,6 @@
|
|
|
14
14
|
//
|
|
15
15
|
// You should have received a copy of the GNU Affero General Public License
|
|
16
16
|
// along with ScreenHand. If not, see <https://www.gnu.org/licenses/>.
|
|
17
|
-
|
|
18
17
|
/**
|
|
19
18
|
* @deprecated Import from "./bridge-client.js" instead.
|
|
20
19
|
* This file re-exports for backward compatibility.
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
// Copyright (C) 2025 Clazro Technology Private Limited
|
|
2
|
+
// SPDX-License-Identifier: AGPL-3.0-only
|
|
3
|
+
//
|
|
4
|
+
// This file is part of ScreenHand.
|
|
5
|
+
//
|
|
6
|
+
// ScreenHand is free software: you can redistribute it and/or modify
|
|
7
|
+
// it under the terms of the GNU Affero General Public License as
|
|
8
|
+
// published by the Free Software Foundation, version 3.
|
|
9
|
+
//
|
|
10
|
+
// ScreenHand is distributed in the hope that it will be useful,
|
|
11
|
+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12
|
+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
13
|
+
// GNU Affero General Public License for more details.
|
|
14
|
+
//
|
|
15
|
+
// You should have received a copy of the GNU Affero General Public License
|
|
16
|
+
// along with ScreenHand. If not, see <https://www.gnu.org/licenses/>.
|
|
17
|
+
const DEFAULT_VERIFY_TIMEOUT = 5000;
|
|
18
|
+
const STEP_DELAY_MS = 300;
|
|
19
|
+
export class PlaybookEngine {
|
|
20
|
+
runtime;
|
|
21
|
+
constructor(runtime) {
|
|
22
|
+
this.runtime = runtime;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Execute a playbook against a live session.
|
|
26
|
+
* Returns result with success/failure and which step broke.
|
|
27
|
+
*/
|
|
28
|
+
async run(sessionId, playbook, options = {}) {
|
|
29
|
+
const start = Date.now();
|
|
30
|
+
let stepsCompleted = 0;
|
|
31
|
+
for (let i = 0; i < playbook.steps.length; i++) {
|
|
32
|
+
const step = playbook.steps[i];
|
|
33
|
+
try {
|
|
34
|
+
const result = await this.executeStep(sessionId, step);
|
|
35
|
+
stepsCompleted++;
|
|
36
|
+
if (options.onStep) {
|
|
37
|
+
options.onStep(i, step, result);
|
|
38
|
+
}
|
|
39
|
+
// Verify step if needed
|
|
40
|
+
if (step.verify) {
|
|
41
|
+
const verified = await this.verifyStep(sessionId, step);
|
|
42
|
+
if (!verified && !step.optional) {
|
|
43
|
+
return {
|
|
44
|
+
playbook: playbook.id,
|
|
45
|
+
success: false,
|
|
46
|
+
stepsCompleted,
|
|
47
|
+
totalSteps: playbook.steps.length,
|
|
48
|
+
failedAtStep: i,
|
|
49
|
+
error: `Verification failed at step ${i}: ${step.description ?? step.action}`,
|
|
50
|
+
durationMs: Date.now() - start,
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
// Small delay between steps for UI to settle
|
|
55
|
+
await sleep(STEP_DELAY_MS);
|
|
56
|
+
}
|
|
57
|
+
catch (err) {
|
|
58
|
+
if (step.optional) {
|
|
59
|
+
stepsCompleted++;
|
|
60
|
+
if (options.onStep) {
|
|
61
|
+
options.onStep(i, step, `Skipped (optional): ${err instanceof Error ? err.message : String(err)}`);
|
|
62
|
+
}
|
|
63
|
+
continue;
|
|
64
|
+
}
|
|
65
|
+
return {
|
|
66
|
+
playbook: playbook.id,
|
|
67
|
+
success: false,
|
|
68
|
+
stepsCompleted,
|
|
69
|
+
totalSteps: playbook.steps.length,
|
|
70
|
+
failedAtStep: i,
|
|
71
|
+
error: err instanceof Error ? err.message : String(err),
|
|
72
|
+
durationMs: Date.now() - start,
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
return {
|
|
77
|
+
playbook: playbook.id,
|
|
78
|
+
success: true,
|
|
79
|
+
stepsCompleted,
|
|
80
|
+
totalSteps: playbook.steps.length,
|
|
81
|
+
failedAtStep: -1,
|
|
82
|
+
durationMs: Date.now() - start,
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Execute a single playbook step.
|
|
87
|
+
*/
|
|
88
|
+
async executeStep(sessionId, step) {
|
|
89
|
+
const target = this.resolveTarget(step.target);
|
|
90
|
+
switch (step.action) {
|
|
91
|
+
case "navigate": {
|
|
92
|
+
if (!step.url)
|
|
93
|
+
throw new Error("navigate step missing url");
|
|
94
|
+
const r = await this.runtime.navigate({ sessionId, url: step.url });
|
|
95
|
+
if (!r.ok)
|
|
96
|
+
throw new Error(r.error.message);
|
|
97
|
+
return `Navigated to ${step.url}`;
|
|
98
|
+
}
|
|
99
|
+
case "press": {
|
|
100
|
+
if (!target)
|
|
101
|
+
throw new Error("press step missing target");
|
|
102
|
+
const r = await this.runtime.press({ sessionId, target });
|
|
103
|
+
if (!r.ok)
|
|
104
|
+
throw new Error(r.error.message);
|
|
105
|
+
return `Pressed ${JSON.stringify(step.target)}`;
|
|
106
|
+
}
|
|
107
|
+
case "type_into": {
|
|
108
|
+
if (!target)
|
|
109
|
+
throw new Error("type_into step missing target");
|
|
110
|
+
if (!step.text)
|
|
111
|
+
throw new Error("type_into step missing text");
|
|
112
|
+
const r = await this.runtime.typeInto({ sessionId, target, text: step.text });
|
|
113
|
+
if (!r.ok)
|
|
114
|
+
throw new Error(r.error.message);
|
|
115
|
+
return `Typed "${step.text}" into ${JSON.stringify(step.target)}`;
|
|
116
|
+
}
|
|
117
|
+
case "extract": {
|
|
118
|
+
if (!target)
|
|
119
|
+
throw new Error("extract step missing target");
|
|
120
|
+
const r = await this.runtime.extract({
|
|
121
|
+
sessionId,
|
|
122
|
+
target,
|
|
123
|
+
format: step.format ?? "text",
|
|
124
|
+
});
|
|
125
|
+
if (!r.ok)
|
|
126
|
+
throw new Error(r.error.message);
|
|
127
|
+
return `Extracted: ${JSON.stringify(r.data).slice(0, 200)}`;
|
|
128
|
+
}
|
|
129
|
+
case "key_combo": {
|
|
130
|
+
if (!step.keys || step.keys.length === 0)
|
|
131
|
+
throw new Error("key_combo step missing keys");
|
|
132
|
+
const r = await this.runtime.keyCombo({ sessionId, keys: step.keys });
|
|
133
|
+
if (!r.ok)
|
|
134
|
+
throw new Error(r.error.message);
|
|
135
|
+
return `Key combo: ${step.keys.join("+")}`;
|
|
136
|
+
}
|
|
137
|
+
case "scroll": {
|
|
138
|
+
const input = {
|
|
139
|
+
sessionId,
|
|
140
|
+
direction: step.direction ?? "down",
|
|
141
|
+
};
|
|
142
|
+
if (step.amount != null)
|
|
143
|
+
input.amount = step.amount;
|
|
144
|
+
const r = await this.runtime.scroll(input);
|
|
145
|
+
if (!r.ok)
|
|
146
|
+
throw new Error(r.error.message);
|
|
147
|
+
return `Scrolled ${step.direction ?? "down"}`;
|
|
148
|
+
}
|
|
149
|
+
case "wait": {
|
|
150
|
+
await sleep(step.ms ?? 1000);
|
|
151
|
+
return `Waited ${step.ms ?? 1000}ms`;
|
|
152
|
+
}
|
|
153
|
+
case "screenshot": {
|
|
154
|
+
const r = await this.runtime.screenshot({ sessionId });
|
|
155
|
+
if (!r.ok)
|
|
156
|
+
throw new Error(r.error.message);
|
|
157
|
+
return `Screenshot taken`;
|
|
158
|
+
}
|
|
159
|
+
default:
|
|
160
|
+
throw new Error(`Unknown action: ${step.action}`);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Verify a step's postcondition via CSS selector check.
|
|
165
|
+
*/
|
|
166
|
+
async verifyStep(sessionId, step) {
|
|
167
|
+
if (!step.verify)
|
|
168
|
+
return true;
|
|
169
|
+
const timeout = step.verifyTimeoutMs ?? DEFAULT_VERIFY_TIMEOUT;
|
|
170
|
+
const r = await this.runtime.waitFor({
|
|
171
|
+
sessionId,
|
|
172
|
+
condition: { type: "selector_visible", selector: step.verify },
|
|
173
|
+
timeoutMs: timeout,
|
|
174
|
+
});
|
|
175
|
+
return r.ok && r.data.matched;
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* Convert playbook target format to runtime Target format.
|
|
179
|
+
*/
|
|
180
|
+
resolveTarget(target) {
|
|
181
|
+
if (!target)
|
|
182
|
+
return undefined;
|
|
183
|
+
if (typeof target === "string") {
|
|
184
|
+
// CSS selector if starts with common patterns, else treat as text
|
|
185
|
+
if (target.startsWith("[") || target.startsWith("#") || target.startsWith(".") || target.startsWith("css=")) {
|
|
186
|
+
return { type: "selector", value: target.replace(/^css=/, "") };
|
|
187
|
+
}
|
|
188
|
+
return { type: "text", value: target };
|
|
189
|
+
}
|
|
190
|
+
if ("selector" in target) {
|
|
191
|
+
return { type: "selector", value: target.selector };
|
|
192
|
+
}
|
|
193
|
+
if ("x" in target && "y" in target) {
|
|
194
|
+
return { type: "coordinates", x: target.x, y: target.y };
|
|
195
|
+
}
|
|
196
|
+
return undefined;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
function sleep(ms) {
|
|
200
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
201
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
// Copyright (C) 2025 Clazro Technology Private Limited
|
|
2
|
+
// SPDX-License-Identifier: AGPL-3.0-only
|
|
3
|
+
//
|
|
4
|
+
// This file is part of ScreenHand.
|
|
5
|
+
//
|
|
6
|
+
// ScreenHand is free software: you can redistribute it and/or modify
|
|
7
|
+
// it under the terms of the GNU Affero General Public License as
|
|
8
|
+
// published by the Free Software Foundation, version 3.
|
|
9
|
+
//
|
|
10
|
+
// ScreenHand is distributed in the hope that it will be useful,
|
|
11
|
+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12
|
+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
13
|
+
// GNU Affero General Public License for more details.
|
|
14
|
+
//
|
|
15
|
+
// You should have received a copy of the GNU Affero General Public License
|
|
16
|
+
// along with ScreenHand. If not, see <https://www.gnu.org/licenses/>.
|
|
17
|
+
export { PlaybookEngine } from "./engine.js";
|
|
18
|
+
export { PlaybookStore } from "./store.js";
|
|
19
|
+
export { PlaybookRunner } from "./runner.js";
|
|
20
|
+
export { PlaybookRecorder } from "./recorder.js";
|