screenhand 0.1.0 → 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.
Files changed (86) hide show
  1. package/LICENSE +661 -21
  2. package/README.md +208 -38
  3. package/dist/.audit-log.jsonl +55 -0
  4. package/dist/.screenhand/memory/.lock +1 -0
  5. package/dist/.screenhand/memory/actions.jsonl +85 -0
  6. package/dist/.screenhand/memory/errors.jsonl +5 -0
  7. package/dist/.screenhand/memory/errors.jsonl.bak +4 -0
  8. package/dist/.screenhand/memory/state.json +35 -0
  9. package/dist/.screenhand/memory/state.json.bak +35 -0
  10. package/dist/.screenhand/memory/strategies.jsonl +12 -0
  11. package/dist/agent/cli.js +73 -0
  12. package/dist/agent/loop.js +258 -0
  13. package/dist/index.js +1 -0
  14. package/dist/mcp/mcp-stdio-server.js +164 -0
  15. package/dist/mcp-desktop.js +2731 -0
  16. package/dist/mcp-entry.js +7 -10
  17. package/dist/monitor/codex-monitor.js +377 -0
  18. package/dist/monitor/task-queue.js +84 -0
  19. package/dist/monitor/types.js +49 -0
  20. package/dist/native/bridge-client.js +2 -1
  21. package/dist/npm-publish-helper.js +117 -0
  22. package/dist/npm-token-cdp.js +113 -0
  23. package/dist/npm-token-create.js +135 -0
  24. package/dist/npm-token-finish.js +126 -0
  25. package/dist/playbook/engine.js +193 -0
  26. package/dist/playbook/index.js +4 -0
  27. package/dist/playbook/recorder.js +519 -0
  28. package/dist/playbook/runner.js +392 -0
  29. package/dist/playbook/store.js +166 -0
  30. package/dist/playbook/types.js +4 -0
  31. package/dist/scripts/codex-monitor-daemon.js +335 -0
  32. package/dist/scripts/supervisor-daemon.js +272 -0
  33. package/dist/scripts/worker-daemon.js +228 -0
  34. package/dist/src/agent/cli.js +82 -0
  35. package/dist/src/agent/loop.js +274 -0
  36. package/dist/src/config.js +25 -0
  37. package/dist/src/index.js +72 -0
  38. package/dist/src/jobs/manager.js +237 -0
  39. package/dist/src/jobs/runner.js +683 -0
  40. package/dist/src/jobs/store.js +102 -0
  41. package/dist/src/jobs/types.js +30 -0
  42. package/dist/src/jobs/worker.js +97 -0
  43. package/dist/src/logging/timeline-logger.js +45 -0
  44. package/dist/src/mcp/mcp-stdio-server.js +464 -0
  45. package/dist/src/mcp/server.js +363 -0
  46. package/dist/src/mcp-entry.js +60 -0
  47. package/dist/src/memory/recall.js +170 -0
  48. package/dist/src/memory/research.js +104 -0
  49. package/dist/src/memory/seeds.js +101 -0
  50. package/dist/src/memory/service.js +421 -0
  51. package/dist/src/memory/session.js +169 -0
  52. package/dist/src/memory/store.js +422 -0
  53. package/dist/src/memory/types.js +17 -0
  54. package/dist/src/monitor/codex-monitor.js +382 -0
  55. package/dist/src/monitor/task-queue.js +97 -0
  56. package/dist/src/monitor/types.js +62 -0
  57. package/dist/src/native/bridge-client.js +190 -0
  58. package/dist/src/native/macos-bridge-client.js +21 -0
  59. package/dist/src/playbook/engine.js +201 -0
  60. package/dist/src/playbook/index.js +20 -0
  61. package/dist/src/playbook/recorder.js +535 -0
  62. package/dist/src/playbook/runner.js +408 -0
  63. package/dist/src/playbook/store.js +183 -0
  64. package/dist/src/playbook/types.js +17 -0
  65. package/dist/src/runtime/accessibility-adapter.js +393 -0
  66. package/dist/src/runtime/app-adapter.js +64 -0
  67. package/dist/src/runtime/applescript-adapter.js +299 -0
  68. package/dist/src/runtime/ax-role-map.js +96 -0
  69. package/dist/src/runtime/browser-adapter.js +52 -0
  70. package/dist/src/runtime/cdp-chrome-adapter.js +521 -0
  71. package/dist/src/runtime/composite-adapter.js +221 -0
  72. package/dist/src/runtime/execution-contract.js +159 -0
  73. package/dist/src/runtime/executor.js +266 -0
  74. package/dist/src/runtime/locator-cache.js +28 -0
  75. package/dist/src/runtime/planning-loop.js +63 -0
  76. package/dist/src/runtime/service.js +388 -0
  77. package/dist/src/runtime/session-manager.js +60 -0
  78. package/dist/src/runtime/state-observer.js +121 -0
  79. package/dist/src/runtime/vision-adapter.js +224 -0
  80. package/dist/src/supervisor/locks.js +186 -0
  81. package/dist/src/supervisor/supervisor.js +403 -0
  82. package/dist/src/supervisor/types.js +30 -0
  83. package/dist/src/test-mcp-protocol.js +154 -0
  84. package/dist/src/types.js +17 -0
  85. package/dist/src/util/atomic-write.js +118 -0
  86. package/package.json +12 -9
@@ -0,0 +1,393 @@
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 { toAXRole } from "./ax-role-map.js";
18
+ const POLL_INTERVAL_MS = 100;
19
+ export class AccessibilityAdapter {
20
+ bridge;
21
+ sessions = new Map();
22
+ sessionsByProfile = new Map();
23
+ constructor(bridge) {
24
+ this.bridge = bridge;
25
+ }
26
+ async attach(profile, reuseSessionId) {
27
+ const existing = this.sessionsByProfile.get(profile);
28
+ if (existing)
29
+ return existing.info;
30
+ // Ensure bridge is started
31
+ await this.bridge.start();
32
+ // Check accessibility permissions
33
+ const perms = await this.bridge.checkPermissions();
34
+ if (!perms.trusted) {
35
+ throw new Error("Accessibility permission not granted. Go to System Settings → Privacy & Security → Accessibility and enable this app.");
36
+ }
37
+ const info = {
38
+ sessionId: reuseSessionId ?? `ax_session_${profile}_${Date.now()}`,
39
+ profile,
40
+ createdAt: new Date().toISOString(),
41
+ adapterType: "accessibility",
42
+ };
43
+ // Default to frontmost app
44
+ const frontmost = await this.bridge.call("app.frontmost");
45
+ const state = {
46
+ info,
47
+ pid: frontmost.pid,
48
+ bundleId: frontmost.bundleId,
49
+ appName: frontmost.name,
50
+ };
51
+ this.sessions.set(info.sessionId, state);
52
+ this.sessionsByProfile.set(profile, state);
53
+ return info;
54
+ }
55
+ async getAppContext(sessionId) {
56
+ const state = this.requireSession(sessionId);
57
+ // Get window title from AX tree
58
+ let windowTitle = "";
59
+ try {
60
+ const tree = await this.bridge.call("ax.getElementTree", {
61
+ pid: state.pid,
62
+ maxDepth: 1,
63
+ });
64
+ const window = tree.children?.find((c) => c.role === "AXWindow");
65
+ windowTitle = window?.title ?? "";
66
+ }
67
+ catch {
68
+ // Ignore tree errors
69
+ }
70
+ return {
71
+ bundleId: state.bundleId,
72
+ appName: state.appName,
73
+ pid: state.pid,
74
+ windowTitle,
75
+ };
76
+ }
77
+ async getPageMeta(sessionId) {
78
+ const ctx = await this.getAppContext(sessionId);
79
+ return {
80
+ url: ctx.url ?? `app://${ctx.bundleId}`,
81
+ title: ctx.windowTitle || ctx.appName,
82
+ };
83
+ }
84
+ async navigate(sessionId, url, _timeoutMs) {
85
+ // For desktop apps, "navigate" means launching/focusing an app by bundle ID
86
+ const state = this.requireSession(sessionId);
87
+ if (url.startsWith("app://")) {
88
+ const bundleId = url.slice(6);
89
+ const result = await this.bridge.call("app.launch", { bundleId });
90
+ state.pid = result.pid;
91
+ state.bundleId = result.bundleId;
92
+ state.appName = result.appName;
93
+ }
94
+ return this.getPageMeta(sessionId);
95
+ }
96
+ async locate(sessionId, target, timeoutMs) {
97
+ const state = this.requireSession(sessionId);
98
+ const deadline = Date.now() + timeoutMs;
99
+ while (Date.now() < deadline) {
100
+ try {
101
+ const params = this.buildFindParams(target, state.pid);
102
+ const result = await this.bridge.call("ax.findElement", params);
103
+ if (result) {
104
+ const located = {
105
+ handleId: result.handleId,
106
+ locatorUsed: `ax:${target.type}`,
107
+ role: result.role,
108
+ label: result.title,
109
+ };
110
+ if (result.bounds) {
111
+ located.coordinates = result.bounds;
112
+ }
113
+ return located;
114
+ }
115
+ }
116
+ catch {
117
+ // Element not found yet, keep polling
118
+ }
119
+ await sleep(POLL_INTERVAL_MS);
120
+ }
121
+ return null;
122
+ }
123
+ async click(sessionId, element) {
124
+ const state = this.requireSession(sessionId);
125
+ const elementPath = this.parseElementPath(element.handleId);
126
+ if (elementPath) {
127
+ await this.bridge.call("ax.performAction", {
128
+ pid: state.pid,
129
+ elementPath,
130
+ action: "AXPress",
131
+ });
132
+ }
133
+ else if (element.coordinates) {
134
+ // Fallback to coordinate click
135
+ const cx = element.coordinates.x + element.coordinates.width / 2;
136
+ const cy = element.coordinates.y + element.coordinates.height / 2;
137
+ await this.bridge.call("cg.mouseClick", { x: cx, y: cy });
138
+ }
139
+ else {
140
+ throw new Error("Cannot click: no element path or coordinates");
141
+ }
142
+ }
143
+ async setValue(sessionId, element, text, clear) {
144
+ const state = this.requireSession(sessionId);
145
+ const elementPath = this.parseElementPath(element.handleId);
146
+ if (clear && elementPath) {
147
+ // Try AX value set first
148
+ try {
149
+ await this.bridge.call("ax.setElementValue", {
150
+ pid: state.pid,
151
+ elementPath,
152
+ value: text,
153
+ });
154
+ return;
155
+ }
156
+ catch {
157
+ // Fallback: click, select all, type
158
+ }
159
+ }
160
+ // Fallback: click to focus, select all if clearing, then type
161
+ await this.click(sessionId, element);
162
+ await sleep(50);
163
+ if (clear) {
164
+ await this.bridge.call("cg.keyCombo", { keys: ["cmd", "a"] });
165
+ await sleep(50);
166
+ }
167
+ await this.bridge.call("cg.typeText", { text });
168
+ }
169
+ async getValue(sessionId, element) {
170
+ const state = this.requireSession(sessionId);
171
+ const elementPath = this.parseElementPath(element.handleId);
172
+ if (!elementPath)
173
+ return "";
174
+ const result = await this.bridge.call("ax.getElementValue", {
175
+ pid: state.pid,
176
+ elementPath,
177
+ });
178
+ return result.value;
179
+ }
180
+ async waitFor(sessionId, condition, timeoutMs) {
181
+ const deadline = Date.now() + timeoutMs;
182
+ while (Date.now() < deadline) {
183
+ const met = await this.checkCondition(sessionId, condition);
184
+ if (met)
185
+ return true;
186
+ await sleep(POLL_INTERVAL_MS);
187
+ }
188
+ return false;
189
+ }
190
+ async extract(sessionId, target, format) {
191
+ const state = this.requireSession(sessionId);
192
+ if (format === "text") {
193
+ // Get element tree and extract text content
194
+ const element = await this.locate(sessionId, target, 1500);
195
+ if (!element)
196
+ throw new Error("Extract target not found");
197
+ const result = await this.getValue(sessionId, element);
198
+ return result || element.label || "";
199
+ }
200
+ if (format === "json") {
201
+ // Return the AX tree as JSON
202
+ const tree = await this.bridge.call("ax.getElementTree", {
203
+ pid: state.pid,
204
+ maxDepth: 3,
205
+ });
206
+ return tree;
207
+ }
208
+ // table format: return element tree in tabular form
209
+ const tree = await this.bridge.call("ax.getElementTree", {
210
+ pid: state.pid,
211
+ maxDepth: 2,
212
+ });
213
+ return {
214
+ headers: ["role", "title", "value"],
215
+ rows: this.flattenTree(tree).map((n) => [n.role, n.title ?? "", n.value ?? ""]),
216
+ };
217
+ }
218
+ async screenshot(sessionId, region) {
219
+ const state = this.requireSession(sessionId);
220
+ // Get window list to find the window ID for the app
221
+ const windows = await this.bridge.call("app.windows");
222
+ const appWindow = windows.find((w) => w.pid === state.pid);
223
+ if (appWindow) {
224
+ const result = await this.bridge.call("cg.captureWindow", { windowId: appWindow.windowId });
225
+ return result.path;
226
+ }
227
+ // Fallback to screen capture with region
228
+ const result = await this.bridge.call("cg.captureScreen", region ? { region } : {});
229
+ return result.path;
230
+ }
231
+ // ── Desktop-specific methods ──
232
+ async launchApp(sessionId, bundleId) {
233
+ const state = this.requireSession(sessionId);
234
+ const result = await this.bridge.call("app.launch", { bundleId });
235
+ // Update session to track new app
236
+ state.pid = result.pid;
237
+ state.bundleId = result.bundleId;
238
+ state.appName = result.appName;
239
+ return {
240
+ bundleId: result.bundleId,
241
+ appName: result.appName,
242
+ pid: result.pid,
243
+ windowTitle: "",
244
+ };
245
+ }
246
+ async focusApp(sessionId, bundleId) {
247
+ const state = this.requireSession(sessionId);
248
+ await this.bridge.call("app.focus", { bundleId });
249
+ // Update PID if different app
250
+ if (bundleId !== state.bundleId) {
251
+ const apps = await this.bridge.call("app.list");
252
+ const app = apps.find((a) => a.bundleId === bundleId);
253
+ if (app) {
254
+ state.pid = app.pid;
255
+ state.bundleId = bundleId;
256
+ state.appName = app.name;
257
+ }
258
+ }
259
+ }
260
+ async listApps(_sessionId) {
261
+ return this.bridge.call("app.list");
262
+ }
263
+ async listWindows(_sessionId) {
264
+ return this.bridge.call("app.windows");
265
+ }
266
+ async menuClick(sessionId, menuPath) {
267
+ const state = this.requireSession(sessionId);
268
+ await this.bridge.call("ax.menuClick", { pid: state.pid, menuPath });
269
+ }
270
+ async keyCombo(_sessionId, keys) {
271
+ await this.bridge.call("cg.keyCombo", { keys });
272
+ }
273
+ async elementTree(sessionId, maxDepth, _root) {
274
+ const state = this.requireSession(sessionId);
275
+ return this.bridge.call("ax.getElementTree", {
276
+ pid: state.pid,
277
+ maxDepth: maxDepth ?? 5,
278
+ });
279
+ }
280
+ async drag(sessionId, from, to) {
281
+ if (!from.coordinates || !to.coordinates) {
282
+ throw new Error("Drag requires elements with coordinates");
283
+ }
284
+ const fromX = from.coordinates.x + from.coordinates.width / 2;
285
+ const fromY = from.coordinates.y + from.coordinates.height / 2;
286
+ const toX = to.coordinates.x + to.coordinates.width / 2;
287
+ const toY = to.coordinates.y + to.coordinates.height / 2;
288
+ await this.bridge.call("cg.mouseDrag", { fromX, fromY, toX, toY });
289
+ }
290
+ async scroll(_sessionId, direction, amount, element) {
291
+ let x = 500;
292
+ let y = 400;
293
+ if (element?.coordinates) {
294
+ x = element.coordinates.x + element.coordinates.width / 2;
295
+ y = element.coordinates.y + element.coordinates.height / 2;
296
+ }
297
+ const deltaMap = {
298
+ up: { deltaX: 0, deltaY: -amount },
299
+ down: { deltaX: 0, deltaY: amount },
300
+ left: { deltaX: -amount, deltaY: 0 },
301
+ right: { deltaX: amount, deltaY: 0 },
302
+ };
303
+ const delta = deltaMap[direction];
304
+ await this.bridge.call("cg.scroll", { x, y, ...delta });
305
+ }
306
+ // ── Private helpers ──
307
+ requireSession(sessionId) {
308
+ const state = this.sessions.get(sessionId);
309
+ if (!state)
310
+ throw new Error(`Session not found: ${sessionId}`);
311
+ return state;
312
+ }
313
+ buildFindParams(target, pid) {
314
+ const params = { pid };
315
+ switch (target.type) {
316
+ case "role":
317
+ params.role = toAXRole(target.role);
318
+ params.title = target.name;
319
+ params.exact = target.exact ?? true;
320
+ break;
321
+ case "text":
322
+ params.title = target.value;
323
+ params.exact = target.exact ?? true;
324
+ break;
325
+ case "selector":
326
+ // For AX, treat selector as an identifier
327
+ params.identifier = target.value;
328
+ break;
329
+ case "ax_path":
330
+ // Direct path resolution handled differently
331
+ params.role = target.path[target.path.length - 1];
332
+ break;
333
+ case "ax_attribute":
334
+ params[target.attribute] = target.value;
335
+ break;
336
+ case "coordinates":
337
+ // Can't find by coordinates via AX, will fallback to vision
338
+ throw new Error("Cannot locate by coordinates using accessibility adapter");
339
+ case "image":
340
+ throw new Error("Cannot locate by image using accessibility adapter");
341
+ }
342
+ return params;
343
+ }
344
+ parseElementPath(handleId) {
345
+ // Handle IDs from the bridge are formatted as "ax_0_1_2"
346
+ if (!handleId.startsWith("ax_"))
347
+ return null;
348
+ const parts = handleId.slice(3).split("_");
349
+ const indices = parts.map(Number).filter((n) => !isNaN(n));
350
+ return indices.length > 0 ? indices : null;
351
+ }
352
+ async checkCondition(sessionId, condition) {
353
+ switch (condition.type) {
354
+ case "element_exists": {
355
+ const found = await this.locate(sessionId, condition.target, 100);
356
+ return found !== null;
357
+ }
358
+ case "element_gone": {
359
+ const found = await this.locate(sessionId, condition.target, 100);
360
+ return found === null;
361
+ }
362
+ case "window_title_matches": {
363
+ const ctx = await this.getAppContext(sessionId);
364
+ return new RegExp(condition.regex).test(ctx.windowTitle);
365
+ }
366
+ case "text_appears": {
367
+ const found = await this.locate(sessionId, { type: "text", value: condition.text }, 100);
368
+ return found !== null;
369
+ }
370
+ case "app_idle":
371
+ // Simplified: always return true after a short delay
372
+ return true;
373
+ case "selector_visible":
374
+ case "selector_hidden":
375
+ case "url_matches":
376
+ case "spinner_disappears":
377
+ // Browser-specific conditions not fully supported
378
+ return false;
379
+ }
380
+ }
381
+ flattenTree(node) {
382
+ const result = [node];
383
+ if (node.children) {
384
+ for (const child of node.children) {
385
+ result.push(...this.flattenTree(child));
386
+ }
387
+ }
388
+ return result;
389
+ }
390
+ }
391
+ function sleep(ms) {
392
+ return new Promise((resolve) => setTimeout(resolve, ms));
393
+ }
@@ -0,0 +1,64 @@
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
+ /**
18
+ * Placeholder adapter that returns stubs for all methods.
19
+ * Used for testing or when no real adapter is configured.
20
+ */
21
+ export class PlaceholderAppAdapter {
22
+ async attach(profile, reuseSessionId) {
23
+ return {
24
+ sessionId: reuseSessionId ?? `session_${profile}_${Date.now()}`,
25
+ profile,
26
+ createdAt: new Date().toISOString(),
27
+ };
28
+ }
29
+ async getAppContext(_sessionId) {
30
+ return {
31
+ bundleId: "com.placeholder",
32
+ appName: "Placeholder",
33
+ pid: 0,
34
+ windowTitle: "Placeholder Session",
35
+ };
36
+ }
37
+ async getPageMeta(_sessionId) {
38
+ return { url: "about:blank", title: "Placeholder Session" };
39
+ }
40
+ async navigate(_sessionId, url, _timeoutMs) {
41
+ return { url, title: "Placeholder Navigation" };
42
+ }
43
+ async locate(_sessionId, _target, _timeoutMs) {
44
+ throw new Error("App adapter not implemented: locate");
45
+ }
46
+ async click(_sessionId, _element) {
47
+ throw new Error("App adapter not implemented: click");
48
+ }
49
+ async setValue(_sessionId, _element, _text, _clear) {
50
+ throw new Error("App adapter not implemented: setValue");
51
+ }
52
+ async getValue(_sessionId, _element) {
53
+ throw new Error("App adapter not implemented: getValue");
54
+ }
55
+ async waitFor(_sessionId, _condition, _timeoutMs) {
56
+ throw new Error("App adapter not implemented: waitFor");
57
+ }
58
+ async extract(_sessionId, _target, _format) {
59
+ throw new Error("App adapter not implemented: extract");
60
+ }
61
+ async screenshot(_sessionId, _region) {
62
+ throw new Error("App adapter not implemented: screenshot");
63
+ }
64
+ }