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