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,205 @@
|
|
|
1
|
+
import { AccessibilityAdapter } from "./accessibility-adapter.js";
|
|
2
|
+
import { AppleScriptAdapter } from "./applescript-adapter.js";
|
|
3
|
+
import { CdpChromeAdapter } from "./cdp-chrome-adapter.js";
|
|
4
|
+
import { VisionAdapter } from "./vision-adapter.js";
|
|
5
|
+
/** macOS bundle IDs routed to CDP. */
|
|
6
|
+
const BROWSER_BUNDLES = new Set([
|
|
7
|
+
"com.google.Chrome",
|
|
8
|
+
"com.google.Chrome.canary",
|
|
9
|
+
"com.brave.Browser",
|
|
10
|
+
"com.microsoft.edgemac",
|
|
11
|
+
"com.vivaldi.Vivaldi",
|
|
12
|
+
"org.chromium.Chromium",
|
|
13
|
+
]);
|
|
14
|
+
/** Windows process names routed to CDP. */
|
|
15
|
+
const BROWSER_PROCESS_NAMES = new Set([
|
|
16
|
+
"chrome",
|
|
17
|
+
"chrome.exe",
|
|
18
|
+
"brave",
|
|
19
|
+
"brave.exe",
|
|
20
|
+
"msedge",
|
|
21
|
+
"msedge.exe",
|
|
22
|
+
"vivaldi",
|
|
23
|
+
"vivaldi.exe",
|
|
24
|
+
"chromium",
|
|
25
|
+
"chromium.exe",
|
|
26
|
+
]);
|
|
27
|
+
const isWindows = process.platform === "win32";
|
|
28
|
+
/**
|
|
29
|
+
* Composite adapter that auto-selects the best adapter per app:
|
|
30
|
+
* - Chromium browsers → CDP
|
|
31
|
+
* - Scriptable apps → AppleScript (with AX fallback)
|
|
32
|
+
* - Default → Accessibility
|
|
33
|
+
* - Fallback → Vision (if AX locate fails)
|
|
34
|
+
*/
|
|
35
|
+
export class CompositeAdapter {
|
|
36
|
+
bridge;
|
|
37
|
+
cdp;
|
|
38
|
+
accessibility;
|
|
39
|
+
applescript;
|
|
40
|
+
vision;
|
|
41
|
+
sessionRouting = new Map();
|
|
42
|
+
constructor(bridge, cdpOptions) {
|
|
43
|
+
this.bridge = bridge;
|
|
44
|
+
this.cdp = new CdpChromeAdapter(cdpOptions);
|
|
45
|
+
this.accessibility = new AccessibilityAdapter(bridge);
|
|
46
|
+
this.applescript = new AppleScriptAdapter();
|
|
47
|
+
this.vision = new VisionAdapter(bridge);
|
|
48
|
+
}
|
|
49
|
+
async attach(profile) {
|
|
50
|
+
// Default to accessibility adapter; routing is set per-session when app is known
|
|
51
|
+
const info = await this.accessibility.attach(profile);
|
|
52
|
+
this.sessionRouting.set(info.sessionId, {
|
|
53
|
+
adapter: this.accessibility,
|
|
54
|
+
adapterName: "accessibility",
|
|
55
|
+
});
|
|
56
|
+
// Override adapterType
|
|
57
|
+
return { ...info, adapterType: "composite" };
|
|
58
|
+
}
|
|
59
|
+
async getAppContext(sessionId) {
|
|
60
|
+
return this.getAdapter(sessionId).getAppContext(sessionId);
|
|
61
|
+
}
|
|
62
|
+
async getPageMeta(sessionId) {
|
|
63
|
+
return this.getAdapter(sessionId).getPageMeta(sessionId);
|
|
64
|
+
}
|
|
65
|
+
async navigate(sessionId, url, timeoutMs) {
|
|
66
|
+
return this.getAdapter(sessionId).navigate(sessionId, url, timeoutMs);
|
|
67
|
+
}
|
|
68
|
+
async locate(sessionId, target, timeoutMs) {
|
|
69
|
+
const primary = this.getAdapter(sessionId);
|
|
70
|
+
const result = await primary.locate(sessionId, target, timeoutMs);
|
|
71
|
+
if (result)
|
|
72
|
+
return result;
|
|
73
|
+
// Fallback to vision if primary (accessibility/applescript) fails
|
|
74
|
+
const routing = this.sessionRouting.get(sessionId);
|
|
75
|
+
if (routing && routing.adapterName !== "vision" && routing.adapterName !== "cdp") {
|
|
76
|
+
try {
|
|
77
|
+
return await this.vision.locate(sessionId, target, Math.min(timeoutMs, 2000));
|
|
78
|
+
}
|
|
79
|
+
catch {
|
|
80
|
+
// Vision also failed
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
return null;
|
|
84
|
+
}
|
|
85
|
+
async click(sessionId, element) {
|
|
86
|
+
// If the element was found by vision (coordinates-based), use vision adapter for click
|
|
87
|
+
if (element.locatorUsed.startsWith("vision:") && element.coordinates) {
|
|
88
|
+
return this.vision.click(sessionId, element);
|
|
89
|
+
}
|
|
90
|
+
return this.getAdapter(sessionId).click(sessionId, element);
|
|
91
|
+
}
|
|
92
|
+
async setValue(sessionId, element, text, clear) {
|
|
93
|
+
return this.getAdapter(sessionId).setValue(sessionId, element, text, clear);
|
|
94
|
+
}
|
|
95
|
+
async getValue(sessionId, element) {
|
|
96
|
+
return this.getAdapter(sessionId).getValue(sessionId, element);
|
|
97
|
+
}
|
|
98
|
+
async waitFor(sessionId, condition, timeoutMs) {
|
|
99
|
+
return this.getAdapter(sessionId).waitFor(sessionId, condition, timeoutMs);
|
|
100
|
+
}
|
|
101
|
+
async extract(sessionId, target, format) {
|
|
102
|
+
return this.getAdapter(sessionId).extract(sessionId, target, format);
|
|
103
|
+
}
|
|
104
|
+
async screenshot(sessionId, region) {
|
|
105
|
+
return this.getAdapter(sessionId).screenshot(sessionId, region);
|
|
106
|
+
}
|
|
107
|
+
// ── Desktop methods (delegate to the best adapter that supports them) ──
|
|
108
|
+
async launchApp(sessionId, bundleId) {
|
|
109
|
+
// Route to the appropriate adapter based on the app being launched
|
|
110
|
+
this.routeSession(sessionId, bundleId);
|
|
111
|
+
const adapter = this.getAdapter(sessionId);
|
|
112
|
+
if (adapter.launchApp) {
|
|
113
|
+
return adapter.launchApp(sessionId, bundleId);
|
|
114
|
+
}
|
|
115
|
+
// Fallback to accessibility
|
|
116
|
+
return this.accessibility.launchApp(sessionId, bundleId);
|
|
117
|
+
}
|
|
118
|
+
async focusApp(sessionId, bundleId) {
|
|
119
|
+
this.routeSession(sessionId, bundleId);
|
|
120
|
+
const adapter = this.getAdapter(sessionId);
|
|
121
|
+
if (adapter.focusApp) {
|
|
122
|
+
return adapter.focusApp(sessionId, bundleId);
|
|
123
|
+
}
|
|
124
|
+
return this.accessibility.focusApp(sessionId, bundleId);
|
|
125
|
+
}
|
|
126
|
+
async listApps(sessionId) {
|
|
127
|
+
return this.accessibility.listApps(sessionId);
|
|
128
|
+
}
|
|
129
|
+
async listWindows(sessionId) {
|
|
130
|
+
return this.accessibility.listWindows(sessionId);
|
|
131
|
+
}
|
|
132
|
+
async menuClick(sessionId, menuPath) {
|
|
133
|
+
const adapter = this.getAdapter(sessionId);
|
|
134
|
+
if (adapter.menuClick) {
|
|
135
|
+
return adapter.menuClick(sessionId, menuPath);
|
|
136
|
+
}
|
|
137
|
+
return this.accessibility.menuClick(sessionId, menuPath);
|
|
138
|
+
}
|
|
139
|
+
async keyCombo(sessionId, keys) {
|
|
140
|
+
const adapter = this.getAdapter(sessionId);
|
|
141
|
+
if (adapter.keyCombo) {
|
|
142
|
+
return adapter.keyCombo(sessionId, keys);
|
|
143
|
+
}
|
|
144
|
+
return this.accessibility.keyCombo(sessionId, keys);
|
|
145
|
+
}
|
|
146
|
+
async elementTree(sessionId, maxDepth, root) {
|
|
147
|
+
const adapter = this.getAdapter(sessionId);
|
|
148
|
+
if (adapter.elementTree) {
|
|
149
|
+
return adapter.elementTree(sessionId, maxDepth, root);
|
|
150
|
+
}
|
|
151
|
+
return this.accessibility.elementTree(sessionId, maxDepth, root);
|
|
152
|
+
}
|
|
153
|
+
async drag(sessionId, from, to) {
|
|
154
|
+
const adapter = this.getAdapter(sessionId);
|
|
155
|
+
if (adapter.drag) {
|
|
156
|
+
return adapter.drag(sessionId, from, to);
|
|
157
|
+
}
|
|
158
|
+
return this.accessibility.drag(sessionId, from, to);
|
|
159
|
+
}
|
|
160
|
+
async scroll(sessionId, direction, amount, element) {
|
|
161
|
+
const adapter = this.getAdapter(sessionId);
|
|
162
|
+
if (adapter.scroll) {
|
|
163
|
+
return adapter.scroll(sessionId, direction, amount, element);
|
|
164
|
+
}
|
|
165
|
+
return this.accessibility.scroll(sessionId, direction, amount, element);
|
|
166
|
+
}
|
|
167
|
+
// ── Routing logic ──
|
|
168
|
+
routeSession(sessionId, bundleId) {
|
|
169
|
+
let adapter;
|
|
170
|
+
let adapterName;
|
|
171
|
+
if (isWindows) {
|
|
172
|
+
// On Windows: route by process name
|
|
173
|
+
const processName = bundleId.toLowerCase().replace(/\.exe$/, "");
|
|
174
|
+
if (BROWSER_PROCESS_NAMES.has(processName) || BROWSER_PROCESS_NAMES.has(bundleId.toLowerCase())) {
|
|
175
|
+
adapter = this.cdp;
|
|
176
|
+
adapterName = "cdp";
|
|
177
|
+
}
|
|
178
|
+
else {
|
|
179
|
+
// No AppleScript on Windows — always use accessibility (UI Automation)
|
|
180
|
+
adapter = this.accessibility;
|
|
181
|
+
adapterName = "accessibility";
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
else {
|
|
185
|
+
// On macOS: route by bundle ID
|
|
186
|
+
if (BROWSER_BUNDLES.has(bundleId)) {
|
|
187
|
+
adapter = this.cdp;
|
|
188
|
+
adapterName = "cdp";
|
|
189
|
+
}
|
|
190
|
+
else if (AppleScriptAdapter.isScriptable(bundleId)) {
|
|
191
|
+
adapter = this.applescript;
|
|
192
|
+
adapterName = "applescript";
|
|
193
|
+
}
|
|
194
|
+
else {
|
|
195
|
+
adapter = this.accessibility;
|
|
196
|
+
adapterName = "accessibility";
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
this.sessionRouting.set(sessionId, { adapter, adapterName });
|
|
200
|
+
}
|
|
201
|
+
getAdapter(sessionId) {
|
|
202
|
+
const routing = this.sessionRouting.get(sessionId);
|
|
203
|
+
return routing?.adapter ?? this.accessibility;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
import { DEFAULT_ACTION_BUDGET } from "../config.js";
|
|
2
|
+
export class Executor {
|
|
3
|
+
adapter;
|
|
4
|
+
cache;
|
|
5
|
+
logger;
|
|
6
|
+
constructor(adapter, cache, logger) {
|
|
7
|
+
this.adapter = adapter;
|
|
8
|
+
this.cache = cache;
|
|
9
|
+
this.logger = logger;
|
|
10
|
+
}
|
|
11
|
+
async press(input) {
|
|
12
|
+
const telemetry = this.logger.start("press", input.sessionId);
|
|
13
|
+
const budget = this.resolveBudget(input.budget);
|
|
14
|
+
const attempts = [];
|
|
15
|
+
let lastError;
|
|
16
|
+
for (let retry = 0; retry <= budget.maxRetries; retry += 1) {
|
|
17
|
+
telemetry.retries = retry;
|
|
18
|
+
try {
|
|
19
|
+
const siteKey = await this.currentSiteKey(input.sessionId);
|
|
20
|
+
const actionKey = this.targetToKey(input.target);
|
|
21
|
+
const locateResult = await this.locateWithBudget(input.sessionId, siteKey, actionKey, input.target, budget.locateMs, retry > 0);
|
|
22
|
+
attempts.push(...locateResult.attempts);
|
|
23
|
+
telemetry.locateMs += locateResult.attempts.reduce((sum, attempt) => sum + attempt.timeoutMs, 0);
|
|
24
|
+
await this.timed(budget.actMs, async () => {
|
|
25
|
+
await this.adapter.click(input.sessionId, locateResult.element);
|
|
26
|
+
}, "ACTION_FAILED");
|
|
27
|
+
telemetry.actMs += budget.actMs;
|
|
28
|
+
if (input.verify) {
|
|
29
|
+
const verified = await this.timed(budget.verifyMs, () => this.adapter.waitFor(input.sessionId, input.verify, budget.verifyMs), "VERIFY_FAILED");
|
|
30
|
+
telemetry.verifyMs += budget.verifyMs;
|
|
31
|
+
if (!verified) {
|
|
32
|
+
throw this.runtimeError("VERIFY_FAILED", "Verification condition not met.");
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
const page = await this.adapter.getPageMeta(input.sessionId);
|
|
36
|
+
return this.success(page, telemetry);
|
|
37
|
+
}
|
|
38
|
+
catch (error) {
|
|
39
|
+
lastError = this.asRuntimeError(error, attempts);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return this.failure(lastError ??
|
|
43
|
+
this.runtimeError("ACTION_FAILED", "Press failed with unknown runtime error."), telemetry);
|
|
44
|
+
}
|
|
45
|
+
async typeInto(input) {
|
|
46
|
+
const telemetry = this.logger.start("type_into", input.sessionId);
|
|
47
|
+
const budget = this.resolveBudget(input.budget);
|
|
48
|
+
const attempts = [];
|
|
49
|
+
try {
|
|
50
|
+
const siteKey = await this.currentSiteKey(input.sessionId);
|
|
51
|
+
const actionKey = `type:${this.targetToKey(input.target)}`;
|
|
52
|
+
const locateResult = await this.locateWithBudget(input.sessionId, siteKey, actionKey, input.target, budget.locateMs, false);
|
|
53
|
+
attempts.push(...locateResult.attempts);
|
|
54
|
+
telemetry.locateMs += budget.locateMs;
|
|
55
|
+
await this.timed(budget.actMs, async () => {
|
|
56
|
+
await this.adapter.setValue(input.sessionId, locateResult.element, input.text, input.clear ?? true);
|
|
57
|
+
}, "ACTION_FAILED");
|
|
58
|
+
telemetry.actMs += budget.actMs;
|
|
59
|
+
if (input.verifyValue ?? true) {
|
|
60
|
+
const read = await this.adapter.getValue(input.sessionId, locateResult.element);
|
|
61
|
+
if (read !== input.text) {
|
|
62
|
+
throw this.runtimeError("VERIFY_FAILED", `Field value mismatch. Expected "${input.text}", got "${read}".`);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
if (input.verify) {
|
|
66
|
+
const verified = await this.timed(budget.verifyMs, () => this.adapter.waitFor(input.sessionId, input.verify, budget.verifyMs), "VERIFY_FAILED");
|
|
67
|
+
telemetry.verifyMs += budget.verifyMs;
|
|
68
|
+
if (!verified) {
|
|
69
|
+
throw this.runtimeError("VERIFY_FAILED", "Verification condition not met.");
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
const page = await this.adapter.getPageMeta(input.sessionId);
|
|
73
|
+
return this.success(page, telemetry);
|
|
74
|
+
}
|
|
75
|
+
catch (error) {
|
|
76
|
+
return this.failure(this.asRuntimeError(error, attempts), telemetry);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
async locateWithBudget(sessionId, siteKey, actionKey, target, locateBudgetMs, skipCache) {
|
|
80
|
+
const attempts = [];
|
|
81
|
+
const strategyBudget = Math.max(50, Math.floor(locateBudgetMs / 3));
|
|
82
|
+
if (!skipCache) {
|
|
83
|
+
const cachedLocator = this.cache.get(siteKey, actionKey);
|
|
84
|
+
if (cachedLocator) {
|
|
85
|
+
const cachedTarget = { type: "selector", value: cachedLocator };
|
|
86
|
+
const match = await this.tryLocate(sessionId, "cache", cachedTarget, strategyBudget, attempts);
|
|
87
|
+
if (match) {
|
|
88
|
+
return { element: match, attempts };
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
const strategies = this.expandTargetStrategies(target);
|
|
93
|
+
for (const strategy of strategies) {
|
|
94
|
+
const match = await this.tryLocate(sessionId, strategy.strategy, strategy.target, strategyBudget, attempts);
|
|
95
|
+
if (match) {
|
|
96
|
+
if (strategy.target.type === "selector") {
|
|
97
|
+
this.cache.set(siteKey, actionKey, strategy.target.value);
|
|
98
|
+
}
|
|
99
|
+
return { element: match, attempts };
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
throw this.runtimeError("LOCATE_FAILED", "Could not locate target.", attempts);
|
|
103
|
+
}
|
|
104
|
+
async tryLocate(sessionId, strategyName, target, timeoutMs, attempts) {
|
|
105
|
+
try {
|
|
106
|
+
const found = await this.timed(timeoutMs, () => this.adapter.locate(sessionId, target, timeoutMs), "LOCATE_FAILED");
|
|
107
|
+
attempts.push({
|
|
108
|
+
strategy: strategyName,
|
|
109
|
+
target: this.targetToKey(target),
|
|
110
|
+
timeoutMs,
|
|
111
|
+
matched: Boolean(found),
|
|
112
|
+
});
|
|
113
|
+
return found;
|
|
114
|
+
}
|
|
115
|
+
catch (error) {
|
|
116
|
+
attempts.push({
|
|
117
|
+
strategy: strategyName,
|
|
118
|
+
target: this.targetToKey(target),
|
|
119
|
+
timeoutMs,
|
|
120
|
+
matched: false,
|
|
121
|
+
reason: error instanceof Error ? error.message : "Unknown locate error",
|
|
122
|
+
});
|
|
123
|
+
return null;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
expandTargetStrategies(target) {
|
|
127
|
+
if (target.type === "selector") {
|
|
128
|
+
return [{ strategy: "selector", target }];
|
|
129
|
+
}
|
|
130
|
+
if (target.type === "text") {
|
|
131
|
+
return [
|
|
132
|
+
{ strategy: "text_exact", target: { type: "text", value: target.value, exact: true } },
|
|
133
|
+
{ strategy: "text_fuzzy", target: { type: "text", value: target.value, exact: false } },
|
|
134
|
+
];
|
|
135
|
+
}
|
|
136
|
+
if (target.type === "role") {
|
|
137
|
+
return [
|
|
138
|
+
{ strategy: "role_name_exact", target: { type: "role", role: target.role, name: target.name, exact: true } },
|
|
139
|
+
{ strategy: "role_name_fuzzy", target: { type: "role", role: target.role, name: target.name, exact: false } },
|
|
140
|
+
{ strategy: "fallback_text", target: { type: "text", value: target.name } },
|
|
141
|
+
];
|
|
142
|
+
}
|
|
143
|
+
// For new target types (ax_path, ax_attribute, coordinates, image), pass through directly
|
|
144
|
+
return [{ strategy: target.type, target }];
|
|
145
|
+
}
|
|
146
|
+
async currentSiteKey(sessionId) {
|
|
147
|
+
// Try app context first for desktop apps, fall back to page URL for browsers
|
|
148
|
+
try {
|
|
149
|
+
const ctx = await this.adapter.getAppContext(sessionId);
|
|
150
|
+
if (ctx.url) {
|
|
151
|
+
try {
|
|
152
|
+
return new URL(ctx.url).host || ctx.bundleId;
|
|
153
|
+
}
|
|
154
|
+
catch {
|
|
155
|
+
// URL parsing failed, use bundleId + windowTitle
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
return `${ctx.bundleId}::${ctx.windowTitle}`;
|
|
159
|
+
}
|
|
160
|
+
catch {
|
|
161
|
+
// Fallback to page meta
|
|
162
|
+
try {
|
|
163
|
+
const page = await this.adapter.getPageMeta(sessionId);
|
|
164
|
+
return new URL(page.url).host || "unknown-site";
|
|
165
|
+
}
|
|
166
|
+
catch {
|
|
167
|
+
return "unknown-site";
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
resolveBudget(input) {
|
|
172
|
+
return {
|
|
173
|
+
...DEFAULT_ACTION_BUDGET,
|
|
174
|
+
...input,
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
async timed(timeoutMs, operation, errorCode) {
|
|
178
|
+
const timeout = new Promise((_, reject) => {
|
|
179
|
+
setTimeout(() => {
|
|
180
|
+
reject(this.runtimeError("TIMEOUT", `Timed out after ${timeoutMs}ms.`));
|
|
181
|
+
}, timeoutMs);
|
|
182
|
+
});
|
|
183
|
+
try {
|
|
184
|
+
return await Promise.race([operation(), timeout]);
|
|
185
|
+
}
|
|
186
|
+
catch (error) {
|
|
187
|
+
if (this.isRuntimeError(error)) {
|
|
188
|
+
throw error;
|
|
189
|
+
}
|
|
190
|
+
throw this.runtimeError(errorCode, error instanceof Error ? error.message : "Unexpected runtime error");
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
targetToKey(target) {
|
|
194
|
+
switch (target.type) {
|
|
195
|
+
case "selector":
|
|
196
|
+
return `selector:${target.value}`;
|
|
197
|
+
case "text":
|
|
198
|
+
return `text:${target.value}`;
|
|
199
|
+
case "role":
|
|
200
|
+
return `role:${target.role}|name:${target.name}`;
|
|
201
|
+
case "ax_path":
|
|
202
|
+
return `ax_path:${target.path.join("/")}`;
|
|
203
|
+
case "ax_attribute":
|
|
204
|
+
return `ax_attr:${target.attribute}=${target.value}`;
|
|
205
|
+
case "coordinates":
|
|
206
|
+
return `coords:${target.x},${target.y}`;
|
|
207
|
+
case "image":
|
|
208
|
+
return `image:${target.base64.slice(0, 20)}`;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
success(data, telemetry) {
|
|
212
|
+
return {
|
|
213
|
+
ok: true,
|
|
214
|
+
data,
|
|
215
|
+
telemetry: this.logger.finish(telemetry, "success"),
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
failure(error, telemetry) {
|
|
219
|
+
return {
|
|
220
|
+
ok: false,
|
|
221
|
+
error,
|
|
222
|
+
telemetry: this.logger.finish(telemetry, "failed"),
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
runtimeError(code, message, attempts) {
|
|
226
|
+
const error = { code, message };
|
|
227
|
+
if (attempts && attempts.length > 0) {
|
|
228
|
+
error.attempts = attempts;
|
|
229
|
+
}
|
|
230
|
+
return error;
|
|
231
|
+
}
|
|
232
|
+
isRuntimeError(error) {
|
|
233
|
+
if (typeof error !== "object" || error === null) {
|
|
234
|
+
return false;
|
|
235
|
+
}
|
|
236
|
+
return "code" in error && "message" in error;
|
|
237
|
+
}
|
|
238
|
+
asRuntimeError(error, attempts) {
|
|
239
|
+
if (this.isRuntimeError(error)) {
|
|
240
|
+
if (error.attempts || !attempts || attempts.length === 0) {
|
|
241
|
+
return error;
|
|
242
|
+
}
|
|
243
|
+
return {
|
|
244
|
+
...error,
|
|
245
|
+
attempts,
|
|
246
|
+
};
|
|
247
|
+
}
|
|
248
|
+
return this.runtimeError("ACTION_FAILED", error instanceof Error ? error.message : "Unexpected runtime error", attempts);
|
|
249
|
+
}
|
|
250
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export class LocatorCache {
|
|
2
|
+
store = new Map();
|
|
3
|
+
get(siteKey, actionKey) {
|
|
4
|
+
return this.store.get(this.key(siteKey, actionKey));
|
|
5
|
+
}
|
|
6
|
+
set(siteKey, actionKey, locator) {
|
|
7
|
+
this.store.set(this.key(siteKey, actionKey), locator);
|
|
8
|
+
}
|
|
9
|
+
key(siteKey, actionKey) {
|
|
10
|
+
return `${siteKey}::${actionKey}`;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Bidirectional planning loop that buffers UI events between LLM actions
|
|
3
|
+
* and provides state snapshots for the LLM to react to.
|
|
4
|
+
*/
|
|
5
|
+
export class PlanningLoop {
|
|
6
|
+
observer;
|
|
7
|
+
adapter;
|
|
8
|
+
lastActionResults = new Map();
|
|
9
|
+
constructor(observer, adapter) {
|
|
10
|
+
this.observer = observer;
|
|
11
|
+
this.adapter = adapter;
|
|
12
|
+
}
|
|
13
|
+
/** Get a state snapshot for the LLM after an action. */
|
|
14
|
+
async getStateSnapshot(sessionId) {
|
|
15
|
+
const recentEvents = this.observer.drainEvents();
|
|
16
|
+
let appContext = null;
|
|
17
|
+
try {
|
|
18
|
+
appContext = await this.adapter.getAppContext(sessionId);
|
|
19
|
+
}
|
|
20
|
+
catch {
|
|
21
|
+
// May not have an active session
|
|
22
|
+
}
|
|
23
|
+
return {
|
|
24
|
+
recentEvents,
|
|
25
|
+
appContext,
|
|
26
|
+
lastActionResult: this.lastActionResults.get(sessionId) ?? null,
|
|
27
|
+
observing: this.observer.isObserving,
|
|
28
|
+
timestamp: new Date().toISOString(),
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
/** Record the result of the last action for a session. */
|
|
32
|
+
recordActionResult(sessionId, result) {
|
|
33
|
+
this.lastActionResults.set(sessionId, result);
|
|
34
|
+
}
|
|
35
|
+
/** Start observing a process for state changes. */
|
|
36
|
+
async startObserving(sessionId, pid) {
|
|
37
|
+
await this.observer.startObserving(pid);
|
|
38
|
+
}
|
|
39
|
+
/** Stop observing a process. */
|
|
40
|
+
async stopObserving(sessionId, pid) {
|
|
41
|
+
await this.observer.stopObserving(pid);
|
|
42
|
+
}
|
|
43
|
+
/** Peek at recent events without draining. */
|
|
44
|
+
peekEvents(limit = 50) {
|
|
45
|
+
return this.observer.peekEvents(limit);
|
|
46
|
+
}
|
|
47
|
+
}
|