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,363 @@
|
|
|
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 class MvpMcpServer {
|
|
18
|
+
runtime;
|
|
19
|
+
constructor(runtime) {
|
|
20
|
+
this.runtime = runtime;
|
|
21
|
+
}
|
|
22
|
+
async invoke(request) {
|
|
23
|
+
switch (request.tool) {
|
|
24
|
+
case "session_start":
|
|
25
|
+
return this.runtime.sessionStart(optionalString(request.args, "profile"));
|
|
26
|
+
case "navigate": {
|
|
27
|
+
const timeoutMs = optionalNumber(request.args, "timeoutMs");
|
|
28
|
+
const input = {
|
|
29
|
+
sessionId: requiredString(request.args, "sessionId"),
|
|
30
|
+
url: requiredString(request.args, "url"),
|
|
31
|
+
};
|
|
32
|
+
if (typeof timeoutMs === "number") {
|
|
33
|
+
input.timeoutMs = timeoutMs;
|
|
34
|
+
}
|
|
35
|
+
return this.runtime.navigate(input);
|
|
36
|
+
}
|
|
37
|
+
case "press": {
|
|
38
|
+
const verify = parseOptionalWaitCondition(request.args.verify);
|
|
39
|
+
const input = {
|
|
40
|
+
sessionId: requiredString(request.args, "sessionId"),
|
|
41
|
+
target: parseTarget(request.args.target),
|
|
42
|
+
};
|
|
43
|
+
if (verify) {
|
|
44
|
+
input.verify = verify;
|
|
45
|
+
}
|
|
46
|
+
return this.runtime.press(input);
|
|
47
|
+
}
|
|
48
|
+
case "type_into": {
|
|
49
|
+
const clear = optionalBoolean(request.args, "clear");
|
|
50
|
+
const verify = parseOptionalWaitCondition(request.args.verify);
|
|
51
|
+
const input = {
|
|
52
|
+
sessionId: requiredString(request.args, "sessionId"),
|
|
53
|
+
target: parseTarget(request.args.target),
|
|
54
|
+
text: requiredString(request.args, "text"),
|
|
55
|
+
};
|
|
56
|
+
if (typeof clear === "boolean") {
|
|
57
|
+
input.clear = clear;
|
|
58
|
+
}
|
|
59
|
+
if (verify) {
|
|
60
|
+
input.verify = verify;
|
|
61
|
+
}
|
|
62
|
+
return this.runtime.typeInto(input);
|
|
63
|
+
}
|
|
64
|
+
case "wait_for": {
|
|
65
|
+
const timeoutMs = optionalNumber(request.args, "timeoutMs");
|
|
66
|
+
const input = {
|
|
67
|
+
sessionId: requiredString(request.args, "sessionId"),
|
|
68
|
+
condition: parseWaitCondition(request.args.condition),
|
|
69
|
+
};
|
|
70
|
+
if (typeof timeoutMs === "number") {
|
|
71
|
+
input.timeoutMs = timeoutMs;
|
|
72
|
+
}
|
|
73
|
+
return this.runtime.waitFor(input);
|
|
74
|
+
}
|
|
75
|
+
case "extract":
|
|
76
|
+
return this.runtime.extract({
|
|
77
|
+
sessionId: requiredString(request.args, "sessionId"),
|
|
78
|
+
target: parseTarget(request.args.target),
|
|
79
|
+
format: parseExtractFormat(request.args.format),
|
|
80
|
+
});
|
|
81
|
+
case "screenshot": {
|
|
82
|
+
const region = parseOptionalRegion(request.args.region);
|
|
83
|
+
const input = {
|
|
84
|
+
sessionId: requiredString(request.args, "sessionId"),
|
|
85
|
+
};
|
|
86
|
+
if (region) {
|
|
87
|
+
input.region = region;
|
|
88
|
+
}
|
|
89
|
+
return this.runtime.screenshot(input);
|
|
90
|
+
}
|
|
91
|
+
// ── Desktop automation tools ──
|
|
92
|
+
case "app_launch":
|
|
93
|
+
return this.runtime.appLaunch({
|
|
94
|
+
sessionId: requiredString(request.args, "sessionId"),
|
|
95
|
+
bundleId: requiredString(request.args, "bundleId"),
|
|
96
|
+
});
|
|
97
|
+
case "app_focus":
|
|
98
|
+
return this.runtime.appFocus({
|
|
99
|
+
sessionId: requiredString(request.args, "sessionId"),
|
|
100
|
+
bundleId: requiredString(request.args, "bundleId"),
|
|
101
|
+
});
|
|
102
|
+
case "app_list":
|
|
103
|
+
return this.runtime.appList(requiredString(request.args, "sessionId"));
|
|
104
|
+
case "window_list":
|
|
105
|
+
return this.runtime.windowList(requiredString(request.args, "sessionId"));
|
|
106
|
+
case "menu_click":
|
|
107
|
+
return this.runtime.menuClick({
|
|
108
|
+
sessionId: requiredString(request.args, "sessionId"),
|
|
109
|
+
menuPath: requiredStringArray(request.args, "menuPath"),
|
|
110
|
+
});
|
|
111
|
+
case "key_combo":
|
|
112
|
+
return this.runtime.keyCombo({
|
|
113
|
+
sessionId: requiredString(request.args, "sessionId"),
|
|
114
|
+
keys: requiredStringArray(request.args, "keys"),
|
|
115
|
+
});
|
|
116
|
+
case "element_tree": {
|
|
117
|
+
const maxDepth = optionalNumber(request.args, "maxDepth");
|
|
118
|
+
const root = request.args.root ? parseTarget(request.args.root) : undefined;
|
|
119
|
+
const etInput = {
|
|
120
|
+
sessionId: requiredString(request.args, "sessionId"),
|
|
121
|
+
};
|
|
122
|
+
if (typeof maxDepth === "number")
|
|
123
|
+
etInput.maxDepth = maxDepth;
|
|
124
|
+
if (root)
|
|
125
|
+
etInput.root = root;
|
|
126
|
+
return this.runtime.elementTree(etInput);
|
|
127
|
+
}
|
|
128
|
+
case "observe_start": {
|
|
129
|
+
const events = request.args.events;
|
|
130
|
+
const osInput = {
|
|
131
|
+
sessionId: requiredString(request.args, "sessionId"),
|
|
132
|
+
};
|
|
133
|
+
if (Array.isArray(events)) {
|
|
134
|
+
osInput.events = events;
|
|
135
|
+
}
|
|
136
|
+
return this.runtime.observeStart(osInput);
|
|
137
|
+
}
|
|
138
|
+
case "observe_stop":
|
|
139
|
+
return this.runtime.observeStop({
|
|
140
|
+
sessionId: requiredString(request.args, "sessionId"),
|
|
141
|
+
});
|
|
142
|
+
case "drag":
|
|
143
|
+
return this.runtime.drag({
|
|
144
|
+
sessionId: requiredString(request.args, "sessionId"),
|
|
145
|
+
from: parseTarget(request.args.from),
|
|
146
|
+
to: parseTarget(request.args.to),
|
|
147
|
+
});
|
|
148
|
+
case "scroll": {
|
|
149
|
+
const scrollTarget = request.args.target ? parseTarget(request.args.target) : undefined;
|
|
150
|
+
const scrollAmount = optionalNumber(request.args, "amount");
|
|
151
|
+
const scrollInput = {
|
|
152
|
+
sessionId: requiredString(request.args, "sessionId"),
|
|
153
|
+
direction: requiredString(request.args, "direction"),
|
|
154
|
+
};
|
|
155
|
+
if (scrollTarget)
|
|
156
|
+
scrollInput.target = scrollTarget;
|
|
157
|
+
if (typeof scrollAmount === "number")
|
|
158
|
+
scrollInput.amount = scrollAmount;
|
|
159
|
+
return this.runtime.scroll(scrollInput);
|
|
160
|
+
}
|
|
161
|
+
default:
|
|
162
|
+
throw new Error(`Unsupported tool: ${String(request.tool)}`);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
function parseTarget(input) {
|
|
167
|
+
if (typeof input === "string") {
|
|
168
|
+
if (input.startsWith("css=")) {
|
|
169
|
+
return { type: "selector", value: input.slice(4) };
|
|
170
|
+
}
|
|
171
|
+
if (input.startsWith("text=")) {
|
|
172
|
+
return { type: "text", value: input.slice(5), exact: true };
|
|
173
|
+
}
|
|
174
|
+
if (input.startsWith("ax_id=")) {
|
|
175
|
+
return { type: "ax_attribute", attribute: "identifier", value: input.slice(6) };
|
|
176
|
+
}
|
|
177
|
+
return { type: "text", value: input };
|
|
178
|
+
}
|
|
179
|
+
if (!isRecord(input)) {
|
|
180
|
+
throw new Error("target must be a string or object");
|
|
181
|
+
}
|
|
182
|
+
if (typeof input.selector === "string") {
|
|
183
|
+
return { type: "selector", value: input.selector };
|
|
184
|
+
}
|
|
185
|
+
if (typeof input.text === "string") {
|
|
186
|
+
return {
|
|
187
|
+
type: "text",
|
|
188
|
+
value: input.text,
|
|
189
|
+
exact: input.exact === true,
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
if (typeof input.role === "string" && typeof input.name === "string") {
|
|
193
|
+
return {
|
|
194
|
+
type: "role",
|
|
195
|
+
role: input.role,
|
|
196
|
+
name: input.name,
|
|
197
|
+
exact: input.exact === true,
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
if (Array.isArray(input.path)) {
|
|
201
|
+
return { type: "ax_path", path: input.path };
|
|
202
|
+
}
|
|
203
|
+
if (typeof input.attribute === "string" && typeof input.value === "string") {
|
|
204
|
+
return { type: "ax_attribute", attribute: input.attribute, value: input.value };
|
|
205
|
+
}
|
|
206
|
+
if (typeof input.x === "number" && typeof input.y === "number") {
|
|
207
|
+
return { type: "coordinates", x: input.x, y: input.y };
|
|
208
|
+
}
|
|
209
|
+
if (typeof input.base64 === "string") {
|
|
210
|
+
const target = { type: "image", base64: input.base64 };
|
|
211
|
+
if (typeof input.confidence === "number") {
|
|
212
|
+
target.confidence = input.confidence;
|
|
213
|
+
}
|
|
214
|
+
return target;
|
|
215
|
+
}
|
|
216
|
+
throw new Error("target object must contain selector, text, role+name, path, attribute+value, x+y, or base64");
|
|
217
|
+
}
|
|
218
|
+
function parseWaitCondition(input) {
|
|
219
|
+
if (!isRecord(input) || typeof input.type !== "string") {
|
|
220
|
+
throw new Error("condition must be an object with a type");
|
|
221
|
+
}
|
|
222
|
+
switch (input.type) {
|
|
223
|
+
case "selector_visible":
|
|
224
|
+
return {
|
|
225
|
+
type: "selector_visible",
|
|
226
|
+
selector: requiredObjectString(input, "selector"),
|
|
227
|
+
};
|
|
228
|
+
case "selector_hidden":
|
|
229
|
+
return {
|
|
230
|
+
type: "selector_hidden",
|
|
231
|
+
selector: requiredObjectString(input, "selector"),
|
|
232
|
+
};
|
|
233
|
+
case "url_matches":
|
|
234
|
+
return {
|
|
235
|
+
type: "url_matches",
|
|
236
|
+
regex: requiredObjectString(input, "regex"),
|
|
237
|
+
};
|
|
238
|
+
case "text_appears":
|
|
239
|
+
return {
|
|
240
|
+
type: "text_appears",
|
|
241
|
+
text: requiredObjectString(input, "text"),
|
|
242
|
+
};
|
|
243
|
+
case "spinner_disappears":
|
|
244
|
+
return {
|
|
245
|
+
type: "spinner_disappears",
|
|
246
|
+
selector: requiredObjectString(input, "selector"),
|
|
247
|
+
};
|
|
248
|
+
case "element_exists":
|
|
249
|
+
return {
|
|
250
|
+
type: "element_exists",
|
|
251
|
+
target: parseTarget(input.target),
|
|
252
|
+
};
|
|
253
|
+
case "element_gone":
|
|
254
|
+
return {
|
|
255
|
+
type: "element_gone",
|
|
256
|
+
target: parseTarget(input.target),
|
|
257
|
+
};
|
|
258
|
+
case "window_title_matches":
|
|
259
|
+
return {
|
|
260
|
+
type: "window_title_matches",
|
|
261
|
+
regex: requiredObjectString(input, "regex"),
|
|
262
|
+
};
|
|
263
|
+
case "app_idle": {
|
|
264
|
+
const cond = {
|
|
265
|
+
type: "app_idle",
|
|
266
|
+
bundleId: requiredObjectString(input, "bundleId"),
|
|
267
|
+
};
|
|
268
|
+
if (typeof input.timeoutMs === "number") {
|
|
269
|
+
cond.timeoutMs = input.timeoutMs;
|
|
270
|
+
}
|
|
271
|
+
return cond;
|
|
272
|
+
}
|
|
273
|
+
default:
|
|
274
|
+
throw new Error(`Unsupported condition type: ${input.type}`);
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
function parseOptionalWaitCondition(input) {
|
|
278
|
+
if (typeof input === "undefined") {
|
|
279
|
+
return undefined;
|
|
280
|
+
}
|
|
281
|
+
return parseWaitCondition(input);
|
|
282
|
+
}
|
|
283
|
+
function parseExtractFormat(input) {
|
|
284
|
+
if (input === "text" || input === "table" || input === "json") {
|
|
285
|
+
return input;
|
|
286
|
+
}
|
|
287
|
+
throw new Error("format must be one of: text, table, json");
|
|
288
|
+
}
|
|
289
|
+
function parseOptionalRegion(input) {
|
|
290
|
+
if (typeof input === "undefined") {
|
|
291
|
+
return undefined;
|
|
292
|
+
}
|
|
293
|
+
if (!isRecord(input)) {
|
|
294
|
+
throw new Error("region must be an object");
|
|
295
|
+
}
|
|
296
|
+
return {
|
|
297
|
+
x: requiredObjectNumber(input, "x"),
|
|
298
|
+
y: requiredObjectNumber(input, "y"),
|
|
299
|
+
width: requiredObjectNumber(input, "width"),
|
|
300
|
+
height: requiredObjectNumber(input, "height"),
|
|
301
|
+
};
|
|
302
|
+
}
|
|
303
|
+
function requiredString(input, key) {
|
|
304
|
+
const value = input[key];
|
|
305
|
+
if (typeof value !== "string") {
|
|
306
|
+
throw new Error(`${key} must be a string`);
|
|
307
|
+
}
|
|
308
|
+
return value;
|
|
309
|
+
}
|
|
310
|
+
function requiredStringArray(input, key) {
|
|
311
|
+
const value = input[key];
|
|
312
|
+
if (!Array.isArray(value) || !value.every((v) => typeof v === "string")) {
|
|
313
|
+
throw new Error(`${key} must be an array of strings`);
|
|
314
|
+
}
|
|
315
|
+
return value;
|
|
316
|
+
}
|
|
317
|
+
function optionalString(input, key) {
|
|
318
|
+
const value = input[key];
|
|
319
|
+
if (typeof value === "undefined") {
|
|
320
|
+
return undefined;
|
|
321
|
+
}
|
|
322
|
+
if (typeof value !== "string") {
|
|
323
|
+
throw new Error(`${key} must be a string`);
|
|
324
|
+
}
|
|
325
|
+
return value;
|
|
326
|
+
}
|
|
327
|
+
function optionalNumber(input, key) {
|
|
328
|
+
const value = input[key];
|
|
329
|
+
if (typeof value === "undefined") {
|
|
330
|
+
return undefined;
|
|
331
|
+
}
|
|
332
|
+
if (typeof value !== "number") {
|
|
333
|
+
throw new Error(`${key} must be a number`);
|
|
334
|
+
}
|
|
335
|
+
return value;
|
|
336
|
+
}
|
|
337
|
+
function optionalBoolean(input, key) {
|
|
338
|
+
const value = input[key];
|
|
339
|
+
if (typeof value === "undefined") {
|
|
340
|
+
return undefined;
|
|
341
|
+
}
|
|
342
|
+
if (typeof value !== "boolean") {
|
|
343
|
+
throw new Error(`${key} must be a boolean`);
|
|
344
|
+
}
|
|
345
|
+
return value;
|
|
346
|
+
}
|
|
347
|
+
function requiredObjectString(input, key) {
|
|
348
|
+
const value = input[key];
|
|
349
|
+
if (typeof value !== "string") {
|
|
350
|
+
throw new Error(`${key} must be a string`);
|
|
351
|
+
}
|
|
352
|
+
return value;
|
|
353
|
+
}
|
|
354
|
+
function requiredObjectNumber(input, key) {
|
|
355
|
+
const value = input[key];
|
|
356
|
+
if (typeof value !== "number") {
|
|
357
|
+
throw new Error(`${key} must be a number`);
|
|
358
|
+
}
|
|
359
|
+
return value;
|
|
360
|
+
}
|
|
361
|
+
function isRecord(input) {
|
|
362
|
+
return typeof input === "object" && input !== null;
|
|
363
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// Copyright (C) 2025 Clazro Technology Private Limited
|
|
3
|
+
// SPDX-License-Identifier: AGPL-3.0-only
|
|
4
|
+
//
|
|
5
|
+
// This file is part of ScreenHand.
|
|
6
|
+
//
|
|
7
|
+
// ScreenHand is free software: you can redistribute it and/or modify
|
|
8
|
+
// it under the terms of the GNU Affero General Public License as
|
|
9
|
+
// published by the Free Software Foundation, version 3.
|
|
10
|
+
//
|
|
11
|
+
// ScreenHand is distributed in the hope that it will be useful,
|
|
12
|
+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
13
|
+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
14
|
+
// GNU Affero General Public License for more details.
|
|
15
|
+
//
|
|
16
|
+
// You should have received a copy of the GNU Affero General Public License
|
|
17
|
+
// along with ScreenHand. If not, see <https://www.gnu.org/licenses/>.
|
|
18
|
+
import { PlaceholderAppAdapter } from "./runtime/app-adapter.js";
|
|
19
|
+
import { CdpChromeAdapter } from "./runtime/cdp-chrome-adapter.js";
|
|
20
|
+
import { TimelineLogger } from "./logging/timeline-logger.js";
|
|
21
|
+
import { AutomationRuntimeService } from "./runtime/service.js";
|
|
22
|
+
import { startMcpStdioServer } from "./mcp/mcp-stdio-server.js";
|
|
23
|
+
async function createAdapter() {
|
|
24
|
+
const adapterType = process.env.SCREENHAND_ADAPTER ?? process.env.AUTOMATOR_ADAPTER ?? "accessibility";
|
|
25
|
+
switch (adapterType) {
|
|
26
|
+
case "placeholder":
|
|
27
|
+
return new PlaceholderAppAdapter();
|
|
28
|
+
case "cdp":
|
|
29
|
+
return new CdpChromeAdapter({
|
|
30
|
+
headless: process.env.SCREENHAND_HEADLESS === "1" || process.env.AUTOMATOR_HEADLESS === "1",
|
|
31
|
+
});
|
|
32
|
+
case "composite": {
|
|
33
|
+
const { BridgeClient } = await import("./native/bridge-client.js");
|
|
34
|
+
const { CompositeAdapter } = await import("./runtime/composite-adapter.js");
|
|
35
|
+
const bridge = new BridgeClient();
|
|
36
|
+
return new CompositeAdapter(bridge, {
|
|
37
|
+
headless: process.env.SCREENHAND_HEADLESS === "1" || process.env.AUTOMATOR_HEADLESS === "1",
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
case "accessibility":
|
|
41
|
+
default: {
|
|
42
|
+
const { BridgeClient } = await import("./native/bridge-client.js");
|
|
43
|
+
const { AccessibilityAdapter } = await import("./runtime/accessibility-adapter.js");
|
|
44
|
+
const bridge = new BridgeClient();
|
|
45
|
+
return new AccessibilityAdapter(bridge);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
try {
|
|
50
|
+
const adapter = await createAdapter();
|
|
51
|
+
const logger = new TimelineLogger();
|
|
52
|
+
const runtime = new AutomationRuntimeService(adapter, logger);
|
|
53
|
+
process.stderr.write("ScreenHand MCP server starting...\n");
|
|
54
|
+
await startMcpStdioServer(runtime);
|
|
55
|
+
process.stderr.write("ScreenHand MCP server connected.\n");
|
|
56
|
+
}
|
|
57
|
+
catch (e) {
|
|
58
|
+
process.stderr.write(`ScreenHand startup error: ${e instanceof Error ? e.message : String(e)}\n`);
|
|
59
|
+
process.exit(1);
|
|
60
|
+
}
|
|
@@ -0,0 +1,170 @@
|
|
|
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 { MemoryStore } from "./store.js";
|
|
18
|
+
export class RecallEngine {
|
|
19
|
+
store;
|
|
20
|
+
constructor(store) {
|
|
21
|
+
this.store = store;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Find strategies matching a task description (~0ms, in-memory).
|
|
25
|
+
* Strategies with high fail rates are penalized.
|
|
26
|
+
*/
|
|
27
|
+
recallStrategies(query, limit = 5) {
|
|
28
|
+
const strategies = this.store.readStrategies();
|
|
29
|
+
if (strategies.length === 0)
|
|
30
|
+
return [];
|
|
31
|
+
const queryTokens = tokenize(query);
|
|
32
|
+
if (queryTokens.length === 0)
|
|
33
|
+
return [];
|
|
34
|
+
const scored = strategies.map((s) => {
|
|
35
|
+
const targetTokens = new Set([
|
|
36
|
+
...tokenize(s.task),
|
|
37
|
+
...s.tags,
|
|
38
|
+
...s.steps.map((step) => step.tool),
|
|
39
|
+
...s.steps.flatMap((step) => Object.values(step.params)
|
|
40
|
+
.filter((v) => typeof v === "string")
|
|
41
|
+
.flatMap(tokenize)),
|
|
42
|
+
]);
|
|
43
|
+
let matches = 0;
|
|
44
|
+
for (const qt of queryTokens) {
|
|
45
|
+
for (const tt of targetTokens) {
|
|
46
|
+
if (tt.includes(qt) || qt.includes(tt)) {
|
|
47
|
+
matches++;
|
|
48
|
+
break;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
const relevance = matches / queryTokens.length;
|
|
53
|
+
const ageMs = Date.now() - new Date(s.lastUsed).getTime();
|
|
54
|
+
const ageDays = ageMs / (1000 * 60 * 60 * 24);
|
|
55
|
+
const recency = Math.max(0.5, 1.0 - ageDays / 365);
|
|
56
|
+
const successBoost = 1 + Math.log2(Math.max(1, s.successCount)) * 0.1;
|
|
57
|
+
// Penalty for strategies that have failed — reduces score proportionally
|
|
58
|
+
const failCount = s.failCount ?? 0;
|
|
59
|
+
const totalAttempts = s.successCount + failCount;
|
|
60
|
+
const reliabilityPenalty = totalAttempts > 0
|
|
61
|
+
? s.successCount / totalAttempts
|
|
62
|
+
: 1;
|
|
63
|
+
const score = relevance * recency * successBoost * reliabilityPenalty;
|
|
64
|
+
return { ...s, score };
|
|
65
|
+
});
|
|
66
|
+
return scored
|
|
67
|
+
.filter((s) => s.score > 0)
|
|
68
|
+
.sort((a, b) => b.score - a.score)
|
|
69
|
+
.slice(0, limit);
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* O(1) exact match by tool sequence fingerprint.
|
|
73
|
+
* Returns the strategy if found and it has a positive reliability score.
|
|
74
|
+
*/
|
|
75
|
+
recallByFingerprint(tools) {
|
|
76
|
+
const fp = MemoryStore.makeFingerprint(tools);
|
|
77
|
+
const strategy = this.store.lookupByFingerprint(fp);
|
|
78
|
+
if (!strategy)
|
|
79
|
+
return null;
|
|
80
|
+
// Skip strategies that fail more than they succeed
|
|
81
|
+
const failCount = strategy.failCount ?? 0;
|
|
82
|
+
if (failCount > strategy.successCount)
|
|
83
|
+
return null;
|
|
84
|
+
return strategy;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Quick error lookup for a tool — used by interceptor on every call (~0ms).
|
|
88
|
+
* Returns the most relevant error pattern or null.
|
|
89
|
+
*/
|
|
90
|
+
quickErrorCheck(tool) {
|
|
91
|
+
const errors = this.store.readErrors();
|
|
92
|
+
let best = null;
|
|
93
|
+
for (const e of errors) {
|
|
94
|
+
if (e.tool === tool && e.resolution) {
|
|
95
|
+
if (!best || e.occurrences > best.occurrences)
|
|
96
|
+
best = e;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
return best;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Quick strategy hint for a tool sequence — used by interceptor.
|
|
103
|
+
* Tries fingerprint prefix match first (O(1)), then falls back to scan.
|
|
104
|
+
* Skips unreliable strategies (failCount > successCount).
|
|
105
|
+
*/
|
|
106
|
+
quickStrategyHint(recentTools) {
|
|
107
|
+
if (recentTools.length === 0)
|
|
108
|
+
return null;
|
|
109
|
+
const strategies = this.store.readStrategies();
|
|
110
|
+
for (const s of strategies) {
|
|
111
|
+
if (s.steps.length <= recentTools.length)
|
|
112
|
+
continue;
|
|
113
|
+
// Skip unreliable strategies
|
|
114
|
+
const failCount = s.failCount ?? 0;
|
|
115
|
+
if (failCount > s.successCount)
|
|
116
|
+
continue;
|
|
117
|
+
const strategyToolPrefix = s.steps.slice(0, recentTools.length).map((st) => st.tool);
|
|
118
|
+
const matches = recentTools.every((t, i) => t === strategyToolPrefix[i]);
|
|
119
|
+
if (matches) {
|
|
120
|
+
return {
|
|
121
|
+
strategy: s,
|
|
122
|
+
nextStep: s.steps[recentTools.length],
|
|
123
|
+
fingerprint: s.fingerprint ?? MemoryStore.makeFingerprint(s.steps.map((st) => st.tool)),
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
return null;
|
|
128
|
+
}
|
|
129
|
+
/** Find error patterns for a specific tool or all tools */
|
|
130
|
+
recallErrors(tool, params) {
|
|
131
|
+
const errors = this.store.readErrors();
|
|
132
|
+
if (!tool)
|
|
133
|
+
return errors;
|
|
134
|
+
let filtered = errors.filter((e) => e.tool === tool);
|
|
135
|
+
if (params && filtered.length > 1) {
|
|
136
|
+
const paramStr = JSON.stringify(params).toLowerCase();
|
|
137
|
+
filtered.sort((a, b) => {
|
|
138
|
+
const aScore = stringSimilarity(paramStr, JSON.stringify(a.params).toLowerCase());
|
|
139
|
+
const bScore = stringSimilarity(paramStr, JSON.stringify(b.params).toLowerCase());
|
|
140
|
+
return bScore - aScore;
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
return filtered;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
/** Tokenize a string into lowercase keywords (3+ chars) */
|
|
147
|
+
function tokenize(text) {
|
|
148
|
+
return text
|
|
149
|
+
.toLowerCase()
|
|
150
|
+
.split(/[\W_]+/)
|
|
151
|
+
.filter((w) => w.length >= 3);
|
|
152
|
+
}
|
|
153
|
+
/** Simple string similarity: shared character bigrams / total bigrams */
|
|
154
|
+
function stringSimilarity(a, b) {
|
|
155
|
+
if (a === b)
|
|
156
|
+
return 1;
|
|
157
|
+
if (a.length < 2 || b.length < 2)
|
|
158
|
+
return 0;
|
|
159
|
+
const bigramsA = new Set();
|
|
160
|
+
for (let i = 0; i < a.length - 1; i++)
|
|
161
|
+
bigramsA.add(a.slice(i, i + 2));
|
|
162
|
+
let matches = 0;
|
|
163
|
+
let total = 0;
|
|
164
|
+
for (let i = 0; i < b.length - 1; i++) {
|
|
165
|
+
total++;
|
|
166
|
+
if (bigramsA.has(b.slice(i, i + 2)))
|
|
167
|
+
matches++;
|
|
168
|
+
}
|
|
169
|
+
return total > 0 ? matches / total : 0;
|
|
170
|
+
}
|
|
@@ -0,0 +1,104 @@
|
|
|
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 function backgroundResearch(store, tool, params, errorMessage) {
|
|
18
|
+
// Fire-and-forget — never blocks, never throws
|
|
19
|
+
doResearch(store, tool, params, errorMessage).catch(() => { });
|
|
20
|
+
}
|
|
21
|
+
async function doResearch(store, tool, params, errorMessage) {
|
|
22
|
+
const query = `macOS automation: "${tool}" failed with "${errorMessage.slice(0, 200)}"`;
|
|
23
|
+
let resolution = null;
|
|
24
|
+
const apiKey = process.env["ANTHROPIC_API_KEY"];
|
|
25
|
+
if (apiKey) {
|
|
26
|
+
resolution = await tryClaudeAPI(apiKey, query);
|
|
27
|
+
}
|
|
28
|
+
if (!resolution) {
|
|
29
|
+
resolution = await tryDuckDuckGo(query);
|
|
30
|
+
}
|
|
31
|
+
if (!resolution)
|
|
32
|
+
return;
|
|
33
|
+
// Save resolution to error cache
|
|
34
|
+
store.appendError({
|
|
35
|
+
id: "err_research_" + Date.now().toString(36),
|
|
36
|
+
tool,
|
|
37
|
+
params,
|
|
38
|
+
error: errorMessage,
|
|
39
|
+
resolution,
|
|
40
|
+
occurrences: 1,
|
|
41
|
+
lastSeen: new Date().toISOString(),
|
|
42
|
+
});
|
|
43
|
+
// Save as a reusable strategy
|
|
44
|
+
store.appendStrategy({
|
|
45
|
+
id: "str_research_" + Date.now().toString(36),
|
|
46
|
+
task: `Fix: ${tool} — ${errorMessage.slice(0, 100)}`,
|
|
47
|
+
steps: [{ tool, params }],
|
|
48
|
+
totalDurationMs: 0,
|
|
49
|
+
successCount: 1,
|
|
50
|
+
failCount: 0,
|
|
51
|
+
lastUsed: new Date().toISOString(),
|
|
52
|
+
tags: [tool, "research", "fix"],
|
|
53
|
+
fingerprint: "",
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
async function tryClaudeAPI(apiKey, query) {
|
|
57
|
+
try {
|
|
58
|
+
const controller = new AbortController();
|
|
59
|
+
const timeout = setTimeout(() => controller.abort(), 10000);
|
|
60
|
+
const response = await fetch("https://api.anthropic.com/v1/messages", {
|
|
61
|
+
method: "POST",
|
|
62
|
+
headers: {
|
|
63
|
+
"Content-Type": "application/json",
|
|
64
|
+
"x-api-key": apiKey,
|
|
65
|
+
"anthropic-version": "2023-06-01",
|
|
66
|
+
},
|
|
67
|
+
body: JSON.stringify({
|
|
68
|
+
model: "claude-haiku-4-5-20251001",
|
|
69
|
+
max_tokens: 200,
|
|
70
|
+
messages: [{
|
|
71
|
+
role: "user",
|
|
72
|
+
content: `You are a macOS automation expert. Give a brief fix (1-2 sentences) for this error:\n\n${query}`,
|
|
73
|
+
}],
|
|
74
|
+
}),
|
|
75
|
+
signal: controller.signal,
|
|
76
|
+
});
|
|
77
|
+
clearTimeout(timeout);
|
|
78
|
+
if (!response.ok)
|
|
79
|
+
return null;
|
|
80
|
+
const data = await response.json();
|
|
81
|
+
const text = data.content?.[0]?.text;
|
|
82
|
+
return text && text.length > 10 ? text.trim() : null;
|
|
83
|
+
}
|
|
84
|
+
catch {
|
|
85
|
+
return null;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
async function tryDuckDuckGo(query) {
|
|
89
|
+
try {
|
|
90
|
+
const controller = new AbortController();
|
|
91
|
+
const timeout = setTimeout(() => controller.abort(), 5000);
|
|
92
|
+
const url = `https://api.duckduckgo.com/?q=${encodeURIComponent(query)}&format=json&no_html=1`;
|
|
93
|
+
const response = await fetch(url, { signal: controller.signal });
|
|
94
|
+
clearTimeout(timeout);
|
|
95
|
+
if (!response.ok)
|
|
96
|
+
return null;
|
|
97
|
+
const data = await response.json();
|
|
98
|
+
const text = data.AbstractText || data.Abstract || data.Answer;
|
|
99
|
+
return text && text.length > 10 ? text.trim() : null;
|
|
100
|
+
}
|
|
101
|
+
catch {
|
|
102
|
+
return null;
|
|
103
|
+
}
|
|
104
|
+
}
|