screenhand 0.1.0 → 0.1.1
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/.claude/commands/automate.md +28 -0
- package/.claude/commands/debug-ui.md +19 -0
- package/.claude/commands/screenshot.md +15 -0
- package/.github/FUNDING.yml +1 -0
- package/.github/ISSUE_TEMPLATE/bug_report.md +27 -0
- package/.github/ISSUE_TEMPLATE/feature_request.md +20 -0
- package/.mcp.json +8 -0
- package/DESKTOP_MCP_GUIDE.md +92 -0
- package/LICENSE +661 -21
- package/README.md +97 -292
- package/SECURITY.md +44 -0
- package/docs/architecture.md +47 -0
- package/install-skills.sh +19 -0
- package/mcp-bridge.ts +271 -0
- package/mcp-desktop.ts +1221 -0
- package/native/macos-bridge/Package.swift +21 -0
- package/native/macos-bridge/Sources/AccessibilityBridge.swift +261 -0
- package/native/macos-bridge/Sources/AppManagement.swift +129 -0
- package/native/macos-bridge/Sources/CoreGraphicsBridge.swift +242 -0
- package/native/macos-bridge/Sources/ObserverBridge.swift +120 -0
- package/native/macos-bridge/Sources/VisionBridge.swift +80 -0
- package/native/macos-bridge/Sources/main.swift +345 -0
- package/native/windows-bridge/AppManagement.cs +234 -0
- package/native/windows-bridge/InputBridge.cs +436 -0
- package/native/windows-bridge/Program.cs +265 -0
- package/native/windows-bridge/ScreenCapture.cs +329 -0
- package/native/windows-bridge/UIAutomationBridge.cs +571 -0
- package/native/windows-bridge/WindowsBridge.csproj +17 -0
- package/package.json +3 -14
- package/playbooks/devpost.json +186 -0
- package/playbooks/instagram.json +41 -0
- package/playbooks/instagram_v2.json +201 -0
- package/playbooks/x_v1.json +211 -0
- package/scripts/devpost-live-loop.mjs +421 -0
- package/src/config.ts +30 -0
- package/src/index.ts +92 -0
- package/src/logging/timeline-logger.ts +55 -0
- package/src/mcp/server.ts +449 -0
- package/src/memory/recall.ts +191 -0
- package/src/memory/research.ts +146 -0
- package/src/memory/seeds.ts +123 -0
- package/src/memory/session.ts +201 -0
- package/src/memory/store.ts +434 -0
- package/src/memory/types.ts +69 -0
- package/src/native/bridge-client.ts +239 -0
- package/src/native/macos-bridge-client.ts +22 -0
- package/src/runtime/accessibility-adapter.ts +487 -0
- package/src/runtime/app-adapter.ts +169 -0
- package/src/runtime/applescript-adapter.ts +376 -0
- package/src/runtime/ax-role-map.ts +102 -0
- package/src/runtime/browser-adapter.ts +129 -0
- package/src/runtime/cdp-chrome-adapter.ts +676 -0
- package/src/runtime/composite-adapter.ts +274 -0
- package/src/runtime/executor.ts +396 -0
- package/src/runtime/locator-cache.ts +33 -0
- package/src/runtime/planning-loop.ts +81 -0
- package/src/runtime/service.ts +448 -0
- package/src/runtime/session-manager.ts +50 -0
- package/src/runtime/state-observer.ts +136 -0
- package/src/runtime/vision-adapter.ts +297 -0
- package/src/types.ts +297 -0
- package/tests/bridge-client.test.ts +176 -0
- package/tests/browser-stealth.test.ts +210 -0
- package/tests/composite-adapter.test.ts +64 -0
- package/tests/mcp-server.test.ts +151 -0
- package/tests/memory-recall.test.ts +339 -0
- package/tests/memory-research.test.ts +159 -0
- package/tests/memory-seeds.test.ts +120 -0
- package/tests/memory-store.test.ts +392 -0
- package/tests/types.test.ts +92 -0
- package/tsconfig.check.json +17 -0
- package/tsconfig.json +19 -0
- package/vitest.config.ts +8 -0
- package/dist/config.js +0 -9
- package/dist/index.js +0 -55
- package/dist/logging/timeline-logger.js +0 -29
- package/dist/mcp/mcp-stdio-server.js +0 -284
- package/dist/mcp/server.js +0 -347
- package/dist/mcp-entry.js +0 -62
- package/dist/memory/recall.js +0 -160
- package/dist/memory/research.js +0 -98
- package/dist/memory/seeds.js +0 -89
- package/dist/memory/session.js +0 -161
- package/dist/memory/store.js +0 -391
- package/dist/memory/types.js +0 -4
- package/dist/native/bridge-client.js +0 -173
- package/dist/native/macos-bridge-client.js +0 -5
- package/dist/runtime/accessibility-adapter.js +0 -377
- package/dist/runtime/app-adapter.js +0 -48
- package/dist/runtime/applescript-adapter.js +0 -283
- package/dist/runtime/ax-role-map.js +0 -80
- package/dist/runtime/browser-adapter.js +0 -36
- package/dist/runtime/cdp-chrome-adapter.js +0 -505
- package/dist/runtime/composite-adapter.js +0 -205
- package/dist/runtime/executor.js +0 -250
- package/dist/runtime/locator-cache.js +0 -12
- package/dist/runtime/planning-loop.js +0 -47
- package/dist/runtime/service.js +0 -372
- package/dist/runtime/session-manager.js +0 -28
- package/dist/runtime/state-observer.js +0 -105
- package/dist/runtime/vision-adapter.js +0 -208
- package/dist/test-mcp-protocol.js +0 -138
- package/dist/types.js +0 -1
|
@@ -1,173 +0,0 @@
|
|
|
1
|
-
import { spawn } from "node:child_process";
|
|
2
|
-
import { EventEmitter } from "node:events";
|
|
3
|
-
import path from "node:path";
|
|
4
|
-
import { createInterface } from "node:readline";
|
|
5
|
-
/**
|
|
6
|
-
* Per-method timeout overrides (ms).
|
|
7
|
-
* Methods not listed here use the default 10s timeout.
|
|
8
|
-
*/
|
|
9
|
-
const METHOD_TIMEOUTS = {
|
|
10
|
-
"app.launch": 30_000,
|
|
11
|
-
"cg.captureScreen": 15_000,
|
|
12
|
-
"cg.captureWindow": 15_000,
|
|
13
|
-
"vision.ocr": 20_000,
|
|
14
|
-
"vision.findText": 20_000,
|
|
15
|
-
};
|
|
16
|
-
/**
|
|
17
|
-
* Resolves the correct native bridge binary path for the current platform.
|
|
18
|
-
*/
|
|
19
|
-
function defaultBinaryPath() {
|
|
20
|
-
const base = import.meta.dirname ?? process.cwd();
|
|
21
|
-
if (process.platform === "win32") {
|
|
22
|
-
return path.resolve(base, "../../native/windows-bridge/bin/Release/net8.0-windows/windows-bridge.exe");
|
|
23
|
-
}
|
|
24
|
-
// macOS (default)
|
|
25
|
-
return path.resolve(base, "../../native/macos-bridge/.build/release/macos-bridge");
|
|
26
|
-
}
|
|
27
|
-
/**
|
|
28
|
-
* Platform-aware native bridge client.
|
|
29
|
-
* Spawns the correct bridge binary (macOS Swift or Windows C#) based on the OS,
|
|
30
|
-
* communicating via the same JSON-RPC-over-stdio protocol.
|
|
31
|
-
*
|
|
32
|
-
* Drop-in replacement for the original MacOSBridgeClient.
|
|
33
|
-
*/
|
|
34
|
-
export class BridgeClient extends EventEmitter {
|
|
35
|
-
process = null;
|
|
36
|
-
nextId = 1;
|
|
37
|
-
pending = new Map();
|
|
38
|
-
binaryPath;
|
|
39
|
-
restarting = false;
|
|
40
|
-
started = false;
|
|
41
|
-
constructor(binaryPath) {
|
|
42
|
-
super();
|
|
43
|
-
this.binaryPath = binaryPath ?? defaultBinaryPath();
|
|
44
|
-
}
|
|
45
|
-
async start() {
|
|
46
|
-
if (this.started)
|
|
47
|
-
return;
|
|
48
|
-
await this.spawn();
|
|
49
|
-
this.started = true;
|
|
50
|
-
}
|
|
51
|
-
async stop() {
|
|
52
|
-
this.started = false;
|
|
53
|
-
if (this.process) {
|
|
54
|
-
this.process.kill();
|
|
55
|
-
this.process = null;
|
|
56
|
-
}
|
|
57
|
-
// Reject all pending
|
|
58
|
-
for (const [id, pending] of this.pending) {
|
|
59
|
-
clearTimeout(pending.timer);
|
|
60
|
-
pending.reject(new Error("Bridge stopped"));
|
|
61
|
-
this.pending.delete(id);
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
async call(method, params, timeoutMs) {
|
|
65
|
-
const effectiveTimeout = timeoutMs ?? METHOD_TIMEOUTS[method] ?? 10_000;
|
|
66
|
-
if (!this.process || this.process.exitCode !== null) {
|
|
67
|
-
await this.restart();
|
|
68
|
-
}
|
|
69
|
-
const id = this.nextId++;
|
|
70
|
-
const request = { id, method };
|
|
71
|
-
if (params) {
|
|
72
|
-
request.params = params;
|
|
73
|
-
}
|
|
74
|
-
return new Promise((resolve, reject) => {
|
|
75
|
-
const timer = setTimeout(() => {
|
|
76
|
-
this.pending.delete(id);
|
|
77
|
-
reject(new Error(`Bridge call "${method}" timed out after ${effectiveTimeout}ms`));
|
|
78
|
-
}, effectiveTimeout);
|
|
79
|
-
this.pending.set(id, {
|
|
80
|
-
resolve: resolve,
|
|
81
|
-
reject,
|
|
82
|
-
timer,
|
|
83
|
-
});
|
|
84
|
-
const line = JSON.stringify(request) + "\n";
|
|
85
|
-
this.process.stdin.write(line);
|
|
86
|
-
});
|
|
87
|
-
}
|
|
88
|
-
async ping() {
|
|
89
|
-
return this.call("ping");
|
|
90
|
-
}
|
|
91
|
-
async checkPermissions() {
|
|
92
|
-
return this.call("check_permissions");
|
|
93
|
-
}
|
|
94
|
-
async spawn() {
|
|
95
|
-
const child = spawn(this.binaryPath, [], {
|
|
96
|
-
stdio: ["pipe", "pipe", "pipe"],
|
|
97
|
-
});
|
|
98
|
-
child.on("error", (err) => {
|
|
99
|
-
this.emit("error", err);
|
|
100
|
-
if (this.started) {
|
|
101
|
-
this.restart().catch(() => { });
|
|
102
|
-
}
|
|
103
|
-
});
|
|
104
|
-
child.on("exit", (code) => {
|
|
105
|
-
this.emit("exit", code);
|
|
106
|
-
if (this.started && !this.restarting) {
|
|
107
|
-
this.restart().catch(() => { });
|
|
108
|
-
}
|
|
109
|
-
});
|
|
110
|
-
// Parse stdout line by line
|
|
111
|
-
const rl = createInterface({ input: child.stdout });
|
|
112
|
-
rl.on("line", (line) => {
|
|
113
|
-
this.handleLine(line);
|
|
114
|
-
});
|
|
115
|
-
// Log stderr
|
|
116
|
-
child.stderr?.on("data", (data) => {
|
|
117
|
-
this.emit("stderr", data.toString());
|
|
118
|
-
});
|
|
119
|
-
this.process = child;
|
|
120
|
-
}
|
|
121
|
-
handleLine(line) {
|
|
122
|
-
let response;
|
|
123
|
-
try {
|
|
124
|
-
response = JSON.parse(line);
|
|
125
|
-
}
|
|
126
|
-
catch {
|
|
127
|
-
return; // Ignore malformed lines
|
|
128
|
-
}
|
|
129
|
-
// Event (streaming notification from observer)
|
|
130
|
-
if (response.event) {
|
|
131
|
-
this.emit("ax-event", response.event);
|
|
132
|
-
return;
|
|
133
|
-
}
|
|
134
|
-
// Response to a pending request
|
|
135
|
-
const pending = this.pending.get(response.id);
|
|
136
|
-
if (!pending)
|
|
137
|
-
return;
|
|
138
|
-
this.pending.delete(response.id);
|
|
139
|
-
clearTimeout(pending.timer);
|
|
140
|
-
if (response.error) {
|
|
141
|
-
pending.reject(new Error(response.error.message));
|
|
142
|
-
}
|
|
143
|
-
else {
|
|
144
|
-
pending.resolve(response.result);
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
async restart() {
|
|
148
|
-
if (this.restarting)
|
|
149
|
-
return;
|
|
150
|
-
this.restarting = true;
|
|
151
|
-
// Reject all pending requests
|
|
152
|
-
for (const [id, pending] of this.pending) {
|
|
153
|
-
clearTimeout(pending.timer);
|
|
154
|
-
pending.reject(new Error("Bridge process crashed, restarting"));
|
|
155
|
-
this.pending.delete(id);
|
|
156
|
-
}
|
|
157
|
-
try {
|
|
158
|
-
if (this.process) {
|
|
159
|
-
this.process.kill();
|
|
160
|
-
this.process = null;
|
|
161
|
-
}
|
|
162
|
-
await this.spawn();
|
|
163
|
-
this.emit("restart");
|
|
164
|
-
}
|
|
165
|
-
finally {
|
|
166
|
-
this.restarting = false;
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
/**
|
|
171
|
-
* @deprecated Use BridgeClient instead. This alias exists for backward compatibility.
|
|
172
|
-
*/
|
|
173
|
-
export const MacOSBridgeClient = BridgeClient;
|
|
@@ -1,377 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,48 +0,0 @@
|
|
|
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
|
-
}
|