u-foo 4.0.10 → 4.1.2
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/OPTIONAL_SKILLS/ufoo-bus-poll/SKILL.md +27 -0
- package/README.md +279 -789
- package/README.zh-CN.md +290 -439
- package/SKILLS/ufoo/SKILL.md +7 -0
- package/bin/ufoo.js +36 -3
- package/dist/tui/darwin-arm64/ufoo-tui +0 -0
- package/docs/OBSERVABILITY.md +4 -2
- package/docs/README.md +1 -0
- package/package.json +6 -2
- package/scripts/pack-desktop.js +174 -0
- package/src/agents/capabilities/coding/index.js +26 -3
- package/src/agents/capabilities/index.js +2 -1
- package/src/agents/capabilities/mac/index.js +145 -0
- package/src/agents/capabilities/planning/index.js +1 -1
- package/src/agents/capabilities/turnRecall/index.js +5 -16
- package/src/agents/profiles/coding.js +1 -1
- package/src/agents/profiles/main.js +1 -1
- package/src/agents/providers/nativeTransport.js +105 -116
- package/src/agents/providers/runtimeConfig.js +15 -4
- package/src/agents/providers/transports/anthropicCache.js +78 -0
- package/src/agents/providers/transports/antigravityTransport.js +3 -2
- package/src/agents/providers/transports/openaiResponsesTransport.js +7 -12
- package/src/agents/providers/transports/requestCacheObservation.js +61 -0
- package/src/agents/providers/transports/responsesProtocol.js +54 -46
- package/src/agents/providers/upstreamRuntime.js +2 -2
- package/src/agents/runtime/context/contextWindow.js +10 -0
- package/src/agents/runtime/context/slidingWindowManager.js +97 -41
- package/src/agents/runtime/context/topicSelector.js +4 -2
- package/src/agents/runtime/context/turnAnalyzer.js +17 -29
- package/src/agents/runtime/core/agentLoop.js +23 -7
- package/src/agents/runtime/createAgentRuntime.js +1 -1
- package/src/agents/runtime/protocol/auxiliaryModelRequest.js +28 -0
- package/src/app/chat/ChatController.js +1 -0
- package/src/app/chat/commandExecutor.js +25 -0
- package/src/app/chat/commands.js +2 -0
- package/src/app/chat/daemonMessageRouter.js +30 -3
- package/src/app/cli/appCommands.js +107 -0
- package/src/app/cli/features/skills.js +2 -0
- package/src/app/cli/macCommands.js +321 -0
- package/src/app/cli/metricsCommands.js +6 -1
- package/src/app/cli/run.js +26 -0
- package/src/app/cli/setupCommands.js +1 -0
- package/src/code/agent.js +1 -0
- package/src/code/context/assembler.js +8 -6
- package/src/code/context/promptLayers.js +3 -1
- package/src/code/context/stateCommit.js +2 -2
- package/src/code/context/transcript.js +11 -5
- package/src/code/nativeRunner.js +25 -5
- package/src/code/skills/loader.js +10 -2
- package/src/code/skills/render.js +8 -1
- package/src/config.js +42 -0
- package/src/coordination/bus/activate.js +44 -19
- package/src/coordination/history/agentPerformance.js +88 -11
- package/src/coordination/history/agentTrace.js +2 -0
- package/src/coordination/history/modelInputCost.js +52 -0
- package/src/runtime/contracts/uiProtocol.js +26 -0
- package/src/runtime/daemon/agentHost.js +35 -12
- package/src/runtime/daemon/mcpStdioProxy.js +3 -2
- package/src/runtime/mac/helperClient.js +279 -0
- package/src/runtime/mac/helperCompiler.js +235 -0
- package/src/runtime/mac/macSession.js +866 -0
- package/src/runtime/mac/native/ufoo_mac_helper.swift +2493 -0
- package/src/runtime/mac/paths.js +63 -0
- package/src/ui/agentSettingsController.js +12 -5
- package/src/ui/cliHandoff.js +484 -0
- package/src/ui/desktopArchive.js +41 -0
- package/src/ui/desktopUiHost.js +1601 -0
- package/src/ui/desktopUiHostCli.js +66 -0
- package/src/ui/format/index.js +17 -0
- package/src/ui/rustChatHost.js +45 -6
- package/src/ui/workspaceInspector.js +621 -0
|
@@ -0,0 +1,866 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const { createHash, randomUUID } = require("crypto");
|
|
4
|
+
const fs = require("fs");
|
|
5
|
+
const path = require("path");
|
|
6
|
+
const { loadGlobalMacConfig } = require("../../config");
|
|
7
|
+
const {
|
|
8
|
+
resolveCachedHelperBinary,
|
|
9
|
+
detectSwiftCompiler,
|
|
10
|
+
runHelperCheck,
|
|
11
|
+
} = require("./helperCompiler");
|
|
12
|
+
const {
|
|
13
|
+
pingHelperDaemon,
|
|
14
|
+
callHelperRpc,
|
|
15
|
+
acquireMacInputLease,
|
|
16
|
+
} = require("./helperClient");
|
|
17
|
+
|
|
18
|
+
const ALL_OPERATIONS = Object.freeze([
|
|
19
|
+
"status",
|
|
20
|
+
"list_apps",
|
|
21
|
+
"list_windows",
|
|
22
|
+
"launch_app",
|
|
23
|
+
"focus_app",
|
|
24
|
+
"manage_window",
|
|
25
|
+
"snapshot",
|
|
26
|
+
"ax_action",
|
|
27
|
+
"screenshot",
|
|
28
|
+
"move",
|
|
29
|
+
"click",
|
|
30
|
+
"double_click",
|
|
31
|
+
"right_click",
|
|
32
|
+
"drag",
|
|
33
|
+
"scroll",
|
|
34
|
+
"type",
|
|
35
|
+
"press_key",
|
|
36
|
+
"wait",
|
|
37
|
+
"resume_control",
|
|
38
|
+
"release",
|
|
39
|
+
]);
|
|
40
|
+
|
|
41
|
+
const READ_OPERATIONS = new Set([
|
|
42
|
+
"status",
|
|
43
|
+
"list_apps",
|
|
44
|
+
"list_windows",
|
|
45
|
+
"snapshot",
|
|
46
|
+
"screenshot",
|
|
47
|
+
"release",
|
|
48
|
+
]);
|
|
49
|
+
|
|
50
|
+
const INPUT_OPERATIONS = new Set([
|
|
51
|
+
"move",
|
|
52
|
+
"click",
|
|
53
|
+
"double_click",
|
|
54
|
+
"right_click",
|
|
55
|
+
"drag",
|
|
56
|
+
"scroll",
|
|
57
|
+
"type",
|
|
58
|
+
"press_key",
|
|
59
|
+
]);
|
|
60
|
+
|
|
61
|
+
const MUTATING_OPERATIONS = new Set([
|
|
62
|
+
"launch_app",
|
|
63
|
+
"focus_app",
|
|
64
|
+
"manage_window",
|
|
65
|
+
"ax_action",
|
|
66
|
+
"move",
|
|
67
|
+
"click",
|
|
68
|
+
"double_click",
|
|
69
|
+
"right_click",
|
|
70
|
+
"drag",
|
|
71
|
+
"scroll",
|
|
72
|
+
"type",
|
|
73
|
+
"press_key",
|
|
74
|
+
]);
|
|
75
|
+
|
|
76
|
+
const SETUP_HINT = "Use `ufoo mac status` or `ufoo mac permissions` to check/grant macOS Accessibility & Screen Recording permissions, and `ufoo mac enable` if disabled.";
|
|
77
|
+
|
|
78
|
+
const hash = (text) => createHash("sha256").update(String(text)).digest("hex").slice(0, 24);
|
|
79
|
+
const failure = (code, error, extra = {}) => ({ ok: false, code, error, ...extra });
|
|
80
|
+
|
|
81
|
+
function normalizeRef(raw = "") {
|
|
82
|
+
const trimmed = String(raw || "").trim();
|
|
83
|
+
if (!trimmed) return "";
|
|
84
|
+
return trimmed.startsWith("@") ? trimmed : `@${trimmed}`;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function formatSnapshotText(meta, elements = []) {
|
|
88
|
+
const header = `UI snapshot for "${meta.app || "App"}" (pid=${meta.pid}${meta.bundleId ? `, ${meta.bundleId}` : ""}) — ${elements.length} elements${meta.truncated ? " [truncated]" : ""}:`;
|
|
89
|
+
if (!elements.length) {
|
|
90
|
+
return `${header}\n(no matching accessible elements found; try screenshot + coordinate click or set includeMenuBar=true)`;
|
|
91
|
+
}
|
|
92
|
+
const lines = [header];
|
|
93
|
+
for (const el of elements) {
|
|
94
|
+
const indent = " ".repeat(Math.min(6, Math.max(0, Number(el.depth) || 0)));
|
|
95
|
+
const parts = [`${el.ref} [${el.role}]`];
|
|
96
|
+
if (el.title) parts.push(JSON.stringify(el.title));
|
|
97
|
+
if (el.label) parts.push(`label=${JSON.stringify(el.label)}`);
|
|
98
|
+
if (el.value) parts.push(`value=${JSON.stringify(el.value)}`);
|
|
99
|
+
if (el.enabled === false) parts.push("(disabled)");
|
|
100
|
+
if (el.focused) parts.push("(focused)");
|
|
101
|
+
if (el.frame) {
|
|
102
|
+
parts.push(`bounds=(${el.frame.x},${el.frame.y},${el.frame.width},${el.frame.height})`);
|
|
103
|
+
}
|
|
104
|
+
if (Array.isArray(el.actions) && el.actions.length) {
|
|
105
|
+
parts.push(`actions=[${el.actions.join(",")}]`);
|
|
106
|
+
}
|
|
107
|
+
lines.push(`${indent}${parts.join(" ")}`);
|
|
108
|
+
}
|
|
109
|
+
return lines.join("\n");
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function formatAppsText(apps = []) {
|
|
113
|
+
if (!apps.length) return "No running GUI applications found.";
|
|
114
|
+
const lines = [`Running Mac apps (${apps.length}):`];
|
|
115
|
+
for (const app of apps) {
|
|
116
|
+
const tags = [];
|
|
117
|
+
if (app.active) tags.push("frontmost");
|
|
118
|
+
if (app.hidden) tags.push("hidden");
|
|
119
|
+
const tagText = tags.length ? ` [${tags.join(", ")}]` : "";
|
|
120
|
+
lines.push(`- ${app.name || "(unnamed)"} (pid=${app.pid}, bundleId=${app.bundleId || "-"}, windows=${app.windowCount ?? 0})${tagText}`);
|
|
121
|
+
}
|
|
122
|
+
return lines.join("\n");
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function formatWindowsText(windows = []) {
|
|
126
|
+
if (!windows.length) return "No matching on-screen Mac windows found.";
|
|
127
|
+
const lines = [`Mac windows (${windows.length}):`];
|
|
128
|
+
for (const w of windows) {
|
|
129
|
+
const b = w.bounds || {};
|
|
130
|
+
const front = w.frontmost ? " [frontmost]" : "";
|
|
131
|
+
lines.push(`- windowId=${w.windowId}${front} app="${w.app}" (pid=${w.pid}) title=${JSON.stringify(w.title || "")} bounds=(${b.x},${b.y},${b.width},${b.height})`);
|
|
132
|
+
}
|
|
133
|
+
return lines.join("\n");
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function createMacSessions({
|
|
137
|
+
env = process.env,
|
|
138
|
+
platform = process.platform,
|
|
139
|
+
loadMacConfig = loadGlobalMacConfig,
|
|
140
|
+
rpcCall = callHelperRpc,
|
|
141
|
+
pingDaemon = pingHelperDaemon,
|
|
142
|
+
checkHelper = runHelperCheck,
|
|
143
|
+
acquireLease = acquireMacInputLease,
|
|
144
|
+
} = {}) {
|
|
145
|
+
const sessions = new Map();
|
|
146
|
+
|
|
147
|
+
function isEnabled(cfg) {
|
|
148
|
+
if (String(env.UFOO_MAC_ENABLED || "").trim() === "0") return false;
|
|
149
|
+
if (String(env.UFOO_MAC_ENABLED || "").trim() === "1") return true;
|
|
150
|
+
return cfg.enabled !== false;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
function identity(context = {}) {
|
|
154
|
+
if (!context.sessionId) throw new Error("Mac operations require a sessionId");
|
|
155
|
+
const root = fs.realpathSync(context.workspaceRoot || process.cwd());
|
|
156
|
+
const namespace = String(context.artifactNamespace || "ucode");
|
|
157
|
+
const id = hash(`${root}\0${namespace}\0${context.sessionId}`);
|
|
158
|
+
const agentLabel = namespace === "ucode" ? "ucode" : (context.agentName || "ufoo");
|
|
159
|
+
return { root, id, namespace, agentLabel };
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
function get(context = {}) {
|
|
163
|
+
const { root, id, agentLabel } = identity(context);
|
|
164
|
+
if (!sessions.has(id)) {
|
|
165
|
+
sessions.set(id, {
|
|
166
|
+
id,
|
|
167
|
+
root,
|
|
168
|
+
agentLabel,
|
|
169
|
+
outputDir: path.join(root, ".ufoo", "mac", id),
|
|
170
|
+
queue: Promise.resolve(),
|
|
171
|
+
elements: new Map(),
|
|
172
|
+
screenshots: new Map(),
|
|
173
|
+
screenshotSeq: 0,
|
|
174
|
+
lastScreenshotId: "",
|
|
175
|
+
activeTarget: null,
|
|
176
|
+
touchedHelper: false,
|
|
177
|
+
});
|
|
178
|
+
} else {
|
|
179
|
+
sessions.get(id).agentLabel = agentLabel;
|
|
180
|
+
}
|
|
181
|
+
return sessions.get(id);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
function turnScopeFromContext(context = {}) {
|
|
185
|
+
return String(context.requestId || context.taskRunId || context.turnId || context.sessionId || "").trim();
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
async function buildStatus(entry, context = {}) {
|
|
189
|
+
if (platform !== "darwin") {
|
|
190
|
+
return {
|
|
191
|
+
ok: true,
|
|
192
|
+
supported: false,
|
|
193
|
+
platform,
|
|
194
|
+
state: "unsupported_platform",
|
|
195
|
+
text: `mac_app is only supported on macOS (current platform: ${platform}).`,
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
const cfg = loadMacConfig();
|
|
199
|
+
const enabled = isEnabled(cfg);
|
|
200
|
+
if (!enabled) {
|
|
201
|
+
return {
|
|
202
|
+
ok: true,
|
|
203
|
+
supported: true,
|
|
204
|
+
platform,
|
|
205
|
+
enabled: false,
|
|
206
|
+
hud: cfg.hud,
|
|
207
|
+
takeoverGuard: cfg.takeoverGuard,
|
|
208
|
+
state: "disabled",
|
|
209
|
+
setup: SETUP_HINT,
|
|
210
|
+
text: "Mac app automation is currently disabled. Run `ufoo mac enable` to enable it.",
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
const live = await pingDaemon({ env });
|
|
215
|
+
if (live) {
|
|
216
|
+
const perms = live.permissions || {};
|
|
217
|
+
const ready = Boolean(perms.accessibility && perms.screenRecording);
|
|
218
|
+
return {
|
|
219
|
+
ok: true,
|
|
220
|
+
supported: true,
|
|
221
|
+
platform,
|
|
222
|
+
enabled: true,
|
|
223
|
+
hud: cfg.hud,
|
|
224
|
+
takeoverGuard: cfg.takeoverGuard,
|
|
225
|
+
daemonRunning: true,
|
|
226
|
+
state: ready ? "ready" : "permissions_needed",
|
|
227
|
+
...live,
|
|
228
|
+
setup: SETUP_HINT,
|
|
229
|
+
text: ready
|
|
230
|
+
? `Mac automation ready (Accessibility=granted, ScreenRecording=granted, displays=${(live.displays || []).length}).`
|
|
231
|
+
: `Mac automation needs permissions: Accessibility=${perms.accessibility ? "granted" : "MISSING"}, ScreenRecording=${perms.screenRecording ? "granted" : "MISSING"}. Run \`ufoo mac permissions\` to grant access.`,
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
const cached = resolveCachedHelperBinary({ env, platform });
|
|
236
|
+
const compiler = detectSwiftCompiler({ env });
|
|
237
|
+
if (cached.exists) {
|
|
238
|
+
try {
|
|
239
|
+
const checked = await checkHelper({ env, platform, prompt: false, openSettings: false });
|
|
240
|
+
const perms = checked.permissions || {};
|
|
241
|
+
const ready = Boolean(perms.accessibility && perms.screenRecording);
|
|
242
|
+
return {
|
|
243
|
+
ok: true,
|
|
244
|
+
supported: true,
|
|
245
|
+
platform,
|
|
246
|
+
enabled: true,
|
|
247
|
+
hud: cfg.hud,
|
|
248
|
+
takeoverGuard: cfg.takeoverGuard,
|
|
249
|
+
daemonRunning: false,
|
|
250
|
+
helperCompiled: true,
|
|
251
|
+
state: ready ? "ready" : "permissions_needed",
|
|
252
|
+
...checked,
|
|
253
|
+
setup: SETUP_HINT,
|
|
254
|
+
};
|
|
255
|
+
} catch {
|
|
256
|
+
// Fall back to lightweight cold status below
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
return {
|
|
261
|
+
ok: true,
|
|
262
|
+
supported: true,
|
|
263
|
+
platform,
|
|
264
|
+
enabled: true,
|
|
265
|
+
hud: cfg.hud,
|
|
266
|
+
takeoverGuard: cfg.takeoverGuard,
|
|
267
|
+
daemonRunning: false,
|
|
268
|
+
helperCompiled: cached.exists,
|
|
269
|
+
swiftCompilerAvailable: compiler.available,
|
|
270
|
+
state: compiler.available || cached.exists ? "standby" : "swiftc_missing",
|
|
271
|
+
setup: SETUP_HINT,
|
|
272
|
+
text: compiler.available || cached.exists
|
|
273
|
+
? "Mac automation is enabled (helper will start automatically on first operation)."
|
|
274
|
+
: "Xcode Command Line Tools (`swiftc`) not found. Install with `xcode-select --install`.",
|
|
275
|
+
};
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
async function invokeHelper(entry, method, params = {}, context = {}, { mutating = false, timeoutMs = 20000 } = {}) {
|
|
279
|
+
const cfg = loadMacConfig();
|
|
280
|
+
const enrichedParams = {
|
|
281
|
+
...params,
|
|
282
|
+
hud: cfg.hud !== false,
|
|
283
|
+
takeoverGuard: cfg.takeoverGuard !== false,
|
|
284
|
+
agentLabel: entry.agentLabel,
|
|
285
|
+
sessionId: entry.id,
|
|
286
|
+
turnScope: turnScopeFromContext(context),
|
|
287
|
+
};
|
|
288
|
+
const res = await rpcCall(method, enrichedParams, {
|
|
289
|
+
env,
|
|
290
|
+
platform,
|
|
291
|
+
timeoutMs,
|
|
292
|
+
signal: context.signal,
|
|
293
|
+
mutating,
|
|
294
|
+
});
|
|
295
|
+
entry.touchedHelper = true;
|
|
296
|
+
if (!res || res.ok === false) {
|
|
297
|
+
return failure(
|
|
298
|
+
res?.code || "mac_action_failed",
|
|
299
|
+
res?.error || `Mac helper '${method}' failed`,
|
|
300
|
+
res?.permission ? { permission: res.permission, setup: SETUP_HINT } : (res?.reason ? { reason: res.reason } : {}),
|
|
301
|
+
);
|
|
302
|
+
}
|
|
303
|
+
if ((method.startsWith("mouse_") || method.startsWith("key_")) && res.result?.pid && res.result?.windowId) {
|
|
304
|
+
entry.activeTarget = { pid: res.result.pid, windowId: res.result.windowId };
|
|
305
|
+
}
|
|
306
|
+
return { ok: true, ...(res.result || {}) };
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
function inputTarget(entry, input, point = null) {
|
|
310
|
+
// Explicit or snapshot-derived targets must not inherit an unrelated
|
|
311
|
+
// session target. Unqualified keyboard/scroll calls reuse this session only.
|
|
312
|
+
if (input.pid || input.windowId || input.app || input.bundleId || point?.pid || point?.windowId) {
|
|
313
|
+
return {
|
|
314
|
+
app: input.app || "", bundleId: input.bundleId || "",
|
|
315
|
+
pid: input.pid || point?.pid || 0, windowId: input.windowId || point?.windowId || 0,
|
|
316
|
+
};
|
|
317
|
+
}
|
|
318
|
+
return point ? {} : (entry.activeTarget || {});
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
async function resolveCoordinates(entry, rawX, rawY, rawRef, input = {}, context = {}) {
|
|
322
|
+
if (rawRef) {
|
|
323
|
+
const refKey = normalizeRef(rawRef);
|
|
324
|
+
const elem = entry.elements.get(refKey);
|
|
325
|
+
if (!elem) {
|
|
326
|
+
return failure("unknown_element_ref", `Unknown element ref '${refKey}'. Call snapshot first to obtain current @eN refs.`);
|
|
327
|
+
}
|
|
328
|
+
if (!elem.center) {
|
|
329
|
+
return failure("element_no_bounds", `Element ${refKey} (${elem.role}) has no screen bounds; try ax_action with ref="${refKey}" instead.`);
|
|
330
|
+
}
|
|
331
|
+
return {
|
|
332
|
+
ok: true,
|
|
333
|
+
x: elem.center.x,
|
|
334
|
+
y: elem.center.y,
|
|
335
|
+
pid: elem.pid,
|
|
336
|
+
windowId: elem.windowId || 0,
|
|
337
|
+
resolvedFrom: refKey,
|
|
338
|
+
};
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
const x = Number(rawX);
|
|
342
|
+
const y = Number(rawY);
|
|
343
|
+
if (!Number.isFinite(x) || !Number.isFinite(y)) {
|
|
344
|
+
return failure("invalid_mac_arguments", "Provide either ref (e.g. \"@e4\") or numeric x and y coordinates.");
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
const space = String(input.space || "auto").trim().toLowerCase();
|
|
348
|
+
const ssId = String(input.screenshotId || "").trim();
|
|
349
|
+
|
|
350
|
+
if (ssId || space === "screenshot") {
|
|
351
|
+
const targetId = ssId || entry.lastScreenshotId;
|
|
352
|
+
const shot = entry.screenshots.get(targetId);
|
|
353
|
+
if (!shot) {
|
|
354
|
+
return failure("unknown_screenshot_id", `Unknown screenshotId '${targetId}'. Take a fresh screenshot or pass space="screen" with global coordinates.`);
|
|
355
|
+
}
|
|
356
|
+
let originX = shot.bounds.x;
|
|
357
|
+
let originY = shot.bounds.y;
|
|
358
|
+
if (shot.windowId > 0 && shot.coordinateSpace === "window") {
|
|
359
|
+
const winCheck = await invokeHelper(entry, "list_windows", { windowId: shot.windowId, includeOffscreen: true }, context, { mutating: false });
|
|
360
|
+
if (winCheck.ok && Array.isArray(winCheck.windows) && winCheck.windows[0]?.bounds) {
|
|
361
|
+
originX = winCheck.windows[0].bounds.x;
|
|
362
|
+
originY = winCheck.windows[0].bounds.y;
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
return {
|
|
366
|
+
ok: true,
|
|
367
|
+
x: Math.round(originX + x * shot.scaleX),
|
|
368
|
+
y: Math.round(originY + y * shot.scaleY),
|
|
369
|
+
windowId: shot.windowId || 0,
|
|
370
|
+
pid: shot.pid || 0,
|
|
371
|
+
resolvedFrom: `screenshot:${targetId}`,
|
|
372
|
+
};
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
if (space === "window") {
|
|
376
|
+
const winCheck = await invokeHelper(entry, "list_windows", {
|
|
377
|
+
windowId: input.windowId || 0,
|
|
378
|
+
app: input.app || "",
|
|
379
|
+
bundleId: input.bundleId || "",
|
|
380
|
+
pid: input.pid || 0,
|
|
381
|
+
}, context, { mutating: false });
|
|
382
|
+
const win = winCheck.ok && Array.isArray(winCheck.windows) ? winCheck.windows[0] : null;
|
|
383
|
+
if (!win || !win.bounds) {
|
|
384
|
+
return failure("window_not_found", "Could not resolve target window bounds for space=\"window\".");
|
|
385
|
+
}
|
|
386
|
+
return {
|
|
387
|
+
ok: true,
|
|
388
|
+
x: Math.round(win.bounds.x + x),
|
|
389
|
+
y: Math.round(win.bounds.y + y),
|
|
390
|
+
windowId: win.windowId || 0,
|
|
391
|
+
pid: win.pid || 0,
|
|
392
|
+
resolvedFrom: `window:${win.windowId}`,
|
|
393
|
+
};
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
return {
|
|
397
|
+
ok: true,
|
|
398
|
+
x: Math.round(x),
|
|
399
|
+
y: Math.round(y),
|
|
400
|
+
windowId: Number(input.windowId) || 0,
|
|
401
|
+
pid: Number(input.pid) || 0,
|
|
402
|
+
resolvedFrom: "screen",
|
|
403
|
+
};
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
async function runOperation(entry, input = {}, context = {}) {
|
|
407
|
+
if (context.signal?.aborted) {
|
|
408
|
+
return failure("cancelled", "Mac operation cancelled before execution");
|
|
409
|
+
}
|
|
410
|
+
const operation = String(input.operation || "").trim().toLowerCase();
|
|
411
|
+
if (!ALL_OPERATIONS.includes(operation)) {
|
|
412
|
+
return failure("invalid_mac_operation", `Unsupported operation '${operation}'. Supported: ${ALL_OPERATIONS.join(", ")}`);
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
if (operation === "status") {
|
|
416
|
+
return buildStatus(entry, context);
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
if (platform !== "darwin") {
|
|
420
|
+
return failure("unsupported_platform", `mac_app automation is only supported on macOS (current: ${platform}).`);
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
if (context.executionState?.planMode && !READ_OPERATIONS.has(operation)) {
|
|
424
|
+
return failure("plan_mode", "Mac app actions that change UI state require plan mode to be off (inspection via status, list_apps, list_windows, snapshot, and screenshot remains available).");
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
if (operation === "release") {
|
|
428
|
+
if (entry.touchedHelper) {
|
|
429
|
+
await invokeHelper(entry, "hide_hud", {}, context, { mutating: false }).catch(() => {});
|
|
430
|
+
entry.touchedHelper = false;
|
|
431
|
+
}
|
|
432
|
+
entry.elements.clear();
|
|
433
|
+
entry.activeTarget = null;
|
|
434
|
+
entry.screenshots.clear();
|
|
435
|
+
entry.lastScreenshotId = "";
|
|
436
|
+
return { ok: true, released: true, text: "Released Mac session state and hid Agent Mouse HUD." };
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
const cfg = loadMacConfig();
|
|
440
|
+
if (!isEnabled(cfg)) {
|
|
441
|
+
return failure("mac_disabled", "Mac app automation is currently disabled. Run `ufoo mac enable` to enable it.", { setup: SETUP_HINT });
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
if (operation === "wait") {
|
|
445
|
+
const ms = Math.max(50, Math.min(Number(input.ms ?? input.durationMs ?? 500) || 500, 10000));
|
|
446
|
+
await new Promise((resolve, reject) => {
|
|
447
|
+
const t = setTimeout(resolve, ms);
|
|
448
|
+
if (context.signal) {
|
|
449
|
+
context.signal.addEventListener("abort", () => {
|
|
450
|
+
clearTimeout(t);
|
|
451
|
+
reject(Object.assign(new Error("Wait cancelled"), { code: "cancelled" }));
|
|
452
|
+
}, { once: true });
|
|
453
|
+
}
|
|
454
|
+
});
|
|
455
|
+
return { ok: true, waitedMs: ms, text: `Waited ${ms}ms.` };
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
if (operation === "resume_control") {
|
|
459
|
+
const cleared = await invokeHelper(entry, "clear_takeover", {}, context, { mutating: false });
|
|
460
|
+
if (!cleared.ok) return cleared;
|
|
461
|
+
return { ok: true, text: "Cleared takeover pause state. Inspect current window/screen before acting." };
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
if (operation === "list_apps") {
|
|
465
|
+
const res = await invokeHelper(entry, "list_apps", {
|
|
466
|
+
includeBackground: Boolean(input.includeBackground),
|
|
467
|
+
}, context, { mutating: false });
|
|
468
|
+
if (!res.ok) return res;
|
|
469
|
+
return { ok: true, apps: res.apps || [], text: formatAppsText(res.apps || []) };
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
if (operation === "list_windows") {
|
|
473
|
+
const res = await invokeHelper(entry, "list_windows", {
|
|
474
|
+
app: input.app || "",
|
|
475
|
+
bundleId: input.bundleId || "",
|
|
476
|
+
pid: input.pid || 0,
|
|
477
|
+
windowId: input.windowId || 0,
|
|
478
|
+
includeOffscreen: Boolean(input.includeOffscreen),
|
|
479
|
+
}, context, { mutating: false });
|
|
480
|
+
if (!res.ok) return res;
|
|
481
|
+
return { ok: true, windows: res.windows || [], text: formatWindowsText(res.windows || []) };
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
if (operation === "launch_app") {
|
|
485
|
+
const res = await invokeHelper(entry, "launch_app", {
|
|
486
|
+
app: input.app || "",
|
|
487
|
+
bundleId: input.bundleId || "",
|
|
488
|
+
waitMs: input.waitMs || 4000,
|
|
489
|
+
}, context, { mutating: true });
|
|
490
|
+
if (!res.ok) return res;
|
|
491
|
+
entry.activeTarget = { pid: res.pid, windowId: input.windowId || res.windows?.[0]?.windowId || 0 };
|
|
492
|
+
return {
|
|
493
|
+
...res,
|
|
494
|
+
text: `${res.launched ? "Launched" : "Activated"} "${res.name}" (pid=${res.pid}, windows=${(res.windows || []).length}).`,
|
|
495
|
+
};
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
if (operation === "focus_app") {
|
|
499
|
+
const res = await invokeHelper(entry, "focus_app", {
|
|
500
|
+
app: input.app || "",
|
|
501
|
+
bundleId: input.bundleId || "",
|
|
502
|
+
pid: input.pid || 0,
|
|
503
|
+
windowId: input.windowId || 0,
|
|
504
|
+
}, context, { mutating: true });
|
|
505
|
+
if (!res.ok) return res;
|
|
506
|
+
entry.activeTarget = { pid: res.pid, windowId: input.windowId || res.windows?.[0]?.windowId || 0 };
|
|
507
|
+
return {
|
|
508
|
+
...res,
|
|
509
|
+
text: `Focused "${res.name}" (pid=${res.pid}).`,
|
|
510
|
+
};
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
if (operation === "manage_window") {
|
|
514
|
+
const windowAction = String(input.windowAction || "focus").trim().toLowerCase();
|
|
515
|
+
const res = await invokeHelper(entry, "manage_window", {
|
|
516
|
+
windowAction,
|
|
517
|
+
app: input.app || "",
|
|
518
|
+
bundleId: input.bundleId || "",
|
|
519
|
+
pid: input.pid || 0,
|
|
520
|
+
windowId: input.windowId || 0,
|
|
521
|
+
x: input.x,
|
|
522
|
+
y: input.y,
|
|
523
|
+
width: input.width,
|
|
524
|
+
height: input.height,
|
|
525
|
+
}, context, { mutating: true });
|
|
526
|
+
if (!res.ok) return res;
|
|
527
|
+
const b = res.bounds || {};
|
|
528
|
+
return {
|
|
529
|
+
...res,
|
|
530
|
+
text: `Window ${res.windowId || ""} (${res.app}) action=${windowAction} bounds=(${b.x},${b.y},${b.width},${b.height}).`,
|
|
531
|
+
};
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
if (operation === "snapshot") {
|
|
535
|
+
const res = await invokeHelper(entry, "snapshot", {
|
|
536
|
+
app: input.app || "",
|
|
537
|
+
bundleId: input.bundleId || "",
|
|
538
|
+
pid: input.pid || 0,
|
|
539
|
+
windowId: input.windowId || 0,
|
|
540
|
+
maxDepth: input.maxDepth || 7,
|
|
541
|
+
maxNodes: input.maxNodes || 350,
|
|
542
|
+
interactiveOnly: Boolean(input.interactiveOnly),
|
|
543
|
+
includeMenuBar: Boolean(input.includeMenuBar),
|
|
544
|
+
query: input.query || "",
|
|
545
|
+
}, context, { mutating: false });
|
|
546
|
+
if (!res.ok) return res;
|
|
547
|
+
|
|
548
|
+
entry.elements.clear();
|
|
549
|
+
const annotated = (res.elements || []).map((el, idx) => {
|
|
550
|
+
const ref = `@e${idx + 1}`;
|
|
551
|
+
const record = { ...el, ref, pid: res.pid };
|
|
552
|
+
entry.elements.set(ref, record);
|
|
553
|
+
return record;
|
|
554
|
+
});
|
|
555
|
+
entry.activeTarget = { pid: res.pid, windowId: input.windowId || annotated.find((el) => el.windowId)?.windowId || 0 };
|
|
556
|
+
|
|
557
|
+
const fullText = formatSnapshotText(res, annotated);
|
|
558
|
+
const output = {
|
|
559
|
+
ok: true,
|
|
560
|
+
app: res.app,
|
|
561
|
+
bundleId: res.bundleId,
|
|
562
|
+
pid: res.pid,
|
|
563
|
+
elementCount: annotated.length,
|
|
564
|
+
truncated: Boolean(res.truncated),
|
|
565
|
+
};
|
|
566
|
+
if (fullText.length > 6000) {
|
|
567
|
+
fs.mkdirSync(entry.outputDir, { recursive: true, mode: 0o700 });
|
|
568
|
+
output.outputPath = path.join(entry.outputDir, `snapshot-${randomUUID()}.txt`);
|
|
569
|
+
fs.writeFileSync(output.outputPath, fullText, { mode: 0o600 });
|
|
570
|
+
output.textTruncated = true;
|
|
571
|
+
output.text = `${fullText.slice(0, 6000)}\n[Truncated; use read on outputPath for the remaining UI tree.]`;
|
|
572
|
+
} else {
|
|
573
|
+
output.text = fullText;
|
|
574
|
+
}
|
|
575
|
+
return output;
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
if (operation === "ax_action") {
|
|
579
|
+
const refKey = normalizeRef(input.ref);
|
|
580
|
+
if (!refKey) {
|
|
581
|
+
return failure("invalid_mac_arguments", "ref (e.g. \"@e3\" from snapshot) is required for ax_action.");
|
|
582
|
+
}
|
|
583
|
+
const elem = entry.elements.get(refKey);
|
|
584
|
+
if (!elem) {
|
|
585
|
+
return failure("unknown_element_ref", `Unknown element ref '${refKey}'. Call snapshot first to obtain current @eN refs.`);
|
|
586
|
+
}
|
|
587
|
+
const axAction = String(input.axAction || "press").trim().toLowerCase();
|
|
588
|
+
const res = await invokeHelper(entry, "ax_action", {
|
|
589
|
+
pid: elem.pid,
|
|
590
|
+
rootKind: elem.rootKind || "window",
|
|
591
|
+
rootIndex: elem.rootIndex || 0,
|
|
592
|
+
windowId: elem.windowId || 0,
|
|
593
|
+
axPath: elem.axPath || [],
|
|
594
|
+
expectedRole: elem.role || "",
|
|
595
|
+
axAction,
|
|
596
|
+
value: input.value != null ? String(input.value) : (input.text != null ? String(input.text) : ""),
|
|
597
|
+
}, context, { mutating: true });
|
|
598
|
+
if (!res.ok) return res;
|
|
599
|
+
entry.activeTarget = { pid: elem.pid, windowId: elem.windowId || 0 };
|
|
600
|
+
return {
|
|
601
|
+
...res,
|
|
602
|
+
ref: refKey,
|
|
603
|
+
text: `Executed AX '${axAction}' on ${refKey} [${res.role || elem.role}] ${res.title ? JSON.stringify(res.title) : ""}.`.trim(),
|
|
604
|
+
};
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
if (operation === "screenshot") {
|
|
608
|
+
fs.mkdirSync(entry.outputDir, { recursive: true, mode: 0o700 });
|
|
609
|
+
const rawFile = path.join(entry.outputDir, `shot-${randomUUID()}.png`);
|
|
610
|
+
const res = await invokeHelper(entry, "screenshot", {
|
|
611
|
+
outputPath: rawFile,
|
|
612
|
+
app: input.app || "",
|
|
613
|
+
bundleId: input.bundleId || "",
|
|
614
|
+
pid: input.pid || 0,
|
|
615
|
+
windowId: input.windowId || 0,
|
|
616
|
+
display: input.display || 1,
|
|
617
|
+
region: input.region || null,
|
|
618
|
+
retina: Boolean(input.retina),
|
|
619
|
+
}, context, { mutating: false, timeoutMs: 25000 });
|
|
620
|
+
if (!res.ok) return res;
|
|
621
|
+
|
|
622
|
+
const finalPath = res.path || rawFile;
|
|
623
|
+
const bytesBuf = fs.readFileSync(finalPath);
|
|
624
|
+
try { fs.chmodSync(finalPath, 0o600); } catch {}
|
|
625
|
+
|
|
626
|
+
entry.screenshotSeq += 1;
|
|
627
|
+
const screenshotId = `s${entry.screenshotSeq}`;
|
|
628
|
+
const shotRecord = {
|
|
629
|
+
screenshotId,
|
|
630
|
+
coordinateSpace: res.coordinateSpace || "screen",
|
|
631
|
+
bounds: res.bounds || { x: 0, y: 0, width: res.imageWidth || 0, height: res.imageHeight || 0 },
|
|
632
|
+
imageWidth: res.imageWidth,
|
|
633
|
+
imageHeight: res.imageHeight,
|
|
634
|
+
scaleX: res.scaleX || 1,
|
|
635
|
+
scaleY: res.scaleY || 1,
|
|
636
|
+
windowId: res.windowId || 0,
|
|
637
|
+
pid: res.pid || 0,
|
|
638
|
+
app: res.app || "",
|
|
639
|
+
};
|
|
640
|
+
entry.screenshots.set(screenshotId, shotRecord);
|
|
641
|
+
entry.lastScreenshotId = screenshotId;
|
|
642
|
+
if (shotRecord.pid && shotRecord.windowId) {
|
|
643
|
+
entry.activeTarget = { pid: shotRecord.pid, windowId: shotRecord.windowId };
|
|
644
|
+
}
|
|
645
|
+
if (entry.screenshots.size > 20) {
|
|
646
|
+
const oldestKey = entry.screenshots.keys().next().value;
|
|
647
|
+
entry.screenshots.delete(oldestKey);
|
|
648
|
+
}
|
|
649
|
+
|
|
650
|
+
const b = shotRecord.bounds;
|
|
651
|
+
const coordNote = shotRecord.coordinateSpace === "window"
|
|
652
|
+
? `Pass screenshotId="${screenshotId}" with image (x, y) coordinates (0..${res.imageWidth}, 0..${res.imageHeight}) to click/drag inside this capture, or space="screen" for global coordinates (origin=${b.x},${b.y}).`
|
|
653
|
+
: `Image size=${res.imageWidth}x${res.imageHeight} matches logical screen coordinates (pass screenshotId="${screenshotId}" or direct x, y).`;
|
|
654
|
+
|
|
655
|
+
return {
|
|
656
|
+
ok: true,
|
|
657
|
+
kind: "image",
|
|
658
|
+
screenshotId,
|
|
659
|
+
captureKind: res.captureKind,
|
|
660
|
+
coordinateSpace: shotRecord.coordinateSpace,
|
|
661
|
+
bounds: shotRecord.bounds,
|
|
662
|
+
imageWidth: res.imageWidth,
|
|
663
|
+
imageHeight: res.imageHeight,
|
|
664
|
+
windowId: res.windowId || undefined,
|
|
665
|
+
app: res.app || undefined,
|
|
666
|
+
path: finalPath,
|
|
667
|
+
mediaType: res.mediaType || "image/png",
|
|
668
|
+
bytes: bytesBuf.length,
|
|
669
|
+
base64: bytesBuf.toString("base64"),
|
|
670
|
+
text: `Captured ${res.captureKind || "screen"} screenshot id=${screenshotId}${res.app ? ` ("${res.app}")` : ""}. ${coordNote}`,
|
|
671
|
+
};
|
|
672
|
+
}
|
|
673
|
+
|
|
674
|
+
// Serialize agent input; the physical user pointer remains independent.
|
|
675
|
+
if (INPUT_OPERATIONS.has(operation)) {
|
|
676
|
+
const releaseInput = await acquireLease({ env });
|
|
677
|
+
try {
|
|
678
|
+
if (operation === "move") {
|
|
679
|
+
const pt = await resolveCoordinates(entry, input.x, input.y, input.ref, input, context);
|
|
680
|
+
if (!pt.ok) return pt;
|
|
681
|
+
const res = await invokeHelper(entry, "mouse_move", { x: pt.x, y: pt.y, ...inputTarget(entry, input, pt) }, context, { mutating: true });
|
|
682
|
+
if (!res.ok) return res;
|
|
683
|
+
return { ...res, resolvedFrom: pt.resolvedFrom, text: `Moved Agent Mouse to (${res.x}, ${res.y}).` };
|
|
684
|
+
}
|
|
685
|
+
|
|
686
|
+
if (operation === "click" || operation === "double_click" || operation === "right_click") {
|
|
687
|
+
const pt = await resolveCoordinates(entry, input.x, input.y, input.ref, input, context);
|
|
688
|
+
if (!pt.ok) return pt;
|
|
689
|
+
const button = operation === "right_click" ? "right" : String(input.button || "left").toLowerCase();
|
|
690
|
+
const clickCount = operation === "double_click" ? 2 : (Number(input.clickCount) || 1);
|
|
691
|
+
const focusFirst = input.focusApp === true;
|
|
692
|
+
const res = await invokeHelper(entry, "mouse_click", {
|
|
693
|
+
x: pt.x,
|
|
694
|
+
y: pt.y,
|
|
695
|
+
button,
|
|
696
|
+
clickCount,
|
|
697
|
+
modifiers: Array.isArray(input.modifiers) ? input.modifiers : [],
|
|
698
|
+
focusFirst,
|
|
699
|
+
...inputTarget(entry, input, pt),
|
|
700
|
+
}, context, { mutating: true });
|
|
701
|
+
if (!res.ok) return res;
|
|
702
|
+
return {
|
|
703
|
+
...res,
|
|
704
|
+
resolvedFrom: pt.resolvedFrom,
|
|
705
|
+
text: `Clicked (${res.button}, ${res.clickCount}x) at (${res.x}, ${res.y}).`,
|
|
706
|
+
};
|
|
707
|
+
}
|
|
708
|
+
|
|
709
|
+
if (operation === "drag") {
|
|
710
|
+
const fromPt = await resolveCoordinates(entry, input.fromX ?? input.x, input.fromY ?? input.y, input.fromRef ?? input.ref, input, context);
|
|
711
|
+
if (!fromPt.ok) return fromPt;
|
|
712
|
+
const toPt = await resolveCoordinates(entry, input.toX, input.toY, input.toRef, input, context);
|
|
713
|
+
if (!toPt.ok) return toPt;
|
|
714
|
+
if ((fromPt.pid && toPt.pid && fromPt.pid !== toPt.pid)
|
|
715
|
+
|| (fromPt.windowId && toPt.windowId && fromPt.windowId !== toPt.windowId)) {
|
|
716
|
+
return failure("cross_window_drag_unsupported", "Independent dragging must stay within one target window.");
|
|
717
|
+
}
|
|
718
|
+
const res = await invokeHelper(entry, "mouse_drag", {
|
|
719
|
+
...inputTarget(entry, input, fromPt),
|
|
720
|
+
fromX: fromPt.x,
|
|
721
|
+
fromY: fromPt.y,
|
|
722
|
+
toX: toPt.x,
|
|
723
|
+
toY: toPt.y,
|
|
724
|
+
holdMs: input.holdMs || 80,
|
|
725
|
+
modifiers: Array.isArray(input.modifiers) ? input.modifiers : [],
|
|
726
|
+
}, context, { mutating: true });
|
|
727
|
+
if (!res.ok) return res;
|
|
728
|
+
return {
|
|
729
|
+
...res,
|
|
730
|
+
text: `Dragged Agent Mouse from (${res.from.x}, ${res.from.y}) to (${res.to.x}, ${res.to.y}).`,
|
|
731
|
+
};
|
|
732
|
+
}
|
|
733
|
+
|
|
734
|
+
if (operation === "scroll") {
|
|
735
|
+
let pt = null;
|
|
736
|
+
if (input.ref || (input.x !== undefined && input.y !== undefined)) {
|
|
737
|
+
const resolved = await resolveCoordinates(entry, input.x, input.y, input.ref, input, context);
|
|
738
|
+
if (!resolved.ok) return resolved;
|
|
739
|
+
pt = resolved;
|
|
740
|
+
}
|
|
741
|
+
const res = await invokeHelper(entry, "mouse_scroll", {
|
|
742
|
+
...inputTarget(entry, input, pt),
|
|
743
|
+
...(pt ? { x: pt.x, y: pt.y } : {}),
|
|
744
|
+
deltaX: Number(input.deltaX) || 0,
|
|
745
|
+
deltaY: input.deltaY !== undefined ? Number(input.deltaY) : -240,
|
|
746
|
+
}, context, { mutating: true });
|
|
747
|
+
if (!res.ok) return res;
|
|
748
|
+
return {
|
|
749
|
+
...res,
|
|
750
|
+
text: `Scrolled (deltaX=${res.deltaX}, deltaY=${res.deltaY}) at (${res.x}, ${res.y}).`,
|
|
751
|
+
};
|
|
752
|
+
}
|
|
753
|
+
|
|
754
|
+
if (operation === "type") {
|
|
755
|
+
if (input.focusApp === true) {
|
|
756
|
+
const focused = await invokeHelper(entry, "focus_app", {
|
|
757
|
+
...inputTarget(entry, input),
|
|
758
|
+
}, context, { mutating: true });
|
|
759
|
+
if (!focused.ok) return focused;
|
|
760
|
+
}
|
|
761
|
+
const res = await invokeHelper(entry, "key_type", {
|
|
762
|
+
...inputTarget(entry, input),
|
|
763
|
+
text: input.text != null ? String(input.text) : "",
|
|
764
|
+
submit: Boolean(input.submit),
|
|
765
|
+
}, context, { mutating: true, timeoutMs: 30000 });
|
|
766
|
+
if (!res.ok) return res;
|
|
767
|
+
return {
|
|
768
|
+
...res,
|
|
769
|
+
text: `Typed ${res.typedLength} characters${res.submitted ? " and pressed Return" : ""}.`,
|
|
770
|
+
};
|
|
771
|
+
}
|
|
772
|
+
|
|
773
|
+
if (operation === "press_key") {
|
|
774
|
+
if (input.focusApp === true) {
|
|
775
|
+
const focused = await invokeHelper(entry, "focus_app", {
|
|
776
|
+
...inputTarget(entry, input),
|
|
777
|
+
}, context, { mutating: true });
|
|
778
|
+
if (!focused.ok) return focused;
|
|
779
|
+
}
|
|
780
|
+
const res = await invokeHelper(entry, "key_press", {
|
|
781
|
+
...inputTarget(entry, input),
|
|
782
|
+
key: input.key || "",
|
|
783
|
+
modifiers: Array.isArray(input.modifiers) ? input.modifiers : [],
|
|
784
|
+
repeat: input.repeat || 1,
|
|
785
|
+
}, context, { mutating: true });
|
|
786
|
+
if (!res.ok) return res;
|
|
787
|
+
return {
|
|
788
|
+
...res,
|
|
789
|
+
text: `Pressed key '${res.key}'${res.modifiers?.length ? ` with [${res.modifiers.join("+")}]` : ""}${res.repeat > 1 ? ` (${res.repeat}x)` : ""}.`,
|
|
790
|
+
};
|
|
791
|
+
}
|
|
792
|
+
} finally {
|
|
793
|
+
await releaseInput().catch(() => {});
|
|
794
|
+
}
|
|
795
|
+
}
|
|
796
|
+
|
|
797
|
+
return failure("invalid_mac_operation", `Unhandled Mac operation: ${operation}`);
|
|
798
|
+
}
|
|
799
|
+
|
|
800
|
+
async function execute(input = {}, context = {}) {
|
|
801
|
+
let entry;
|
|
802
|
+
try {
|
|
803
|
+
entry = get(context);
|
|
804
|
+
} catch (err) {
|
|
805
|
+
return failure("mac_context_required", err.message);
|
|
806
|
+
}
|
|
807
|
+
|
|
808
|
+
const task = entry.queue.then(async () => {
|
|
809
|
+
try {
|
|
810
|
+
return await runOperation(entry, input, context);
|
|
811
|
+
} catch (err) {
|
|
812
|
+
return failure(
|
|
813
|
+
err?.code || (MUTATING_OPERATIONS.has(input.operation) ? "mac_action_uncertain" : "mac_action_failed"),
|
|
814
|
+
err?.message || "Mac operation failed",
|
|
815
|
+
);
|
|
816
|
+
}
|
|
817
|
+
});
|
|
818
|
+
entry.queue = task.then(() => undefined, () => undefined);
|
|
819
|
+
return task;
|
|
820
|
+
}
|
|
821
|
+
|
|
822
|
+
async function releaseSession(context = {}) {
|
|
823
|
+
let id;
|
|
824
|
+
try {
|
|
825
|
+
id = identity(context).id;
|
|
826
|
+
} catch {
|
|
827
|
+
return;
|
|
828
|
+
}
|
|
829
|
+
const entry = sessions.get(id);
|
|
830
|
+
if (!entry) return;
|
|
831
|
+
sessions.delete(id);
|
|
832
|
+
if (entry.touchedHelper) {
|
|
833
|
+
await rpcCall("hide_hud", {}, { env, platform, timeoutMs: 1200, mutating: false }).catch(() => {});
|
|
834
|
+
}
|
|
835
|
+
}
|
|
836
|
+
|
|
837
|
+
async function closeAll() {
|
|
838
|
+
const entries = [...sessions.values()];
|
|
839
|
+
sessions.clear();
|
|
840
|
+
if (entries.some((e) => e.touchedHelper)) {
|
|
841
|
+
await rpcCall("hide_hud", {}, { env, platform, timeoutMs: 1200, mutating: false }).catch(() => {});
|
|
842
|
+
}
|
|
843
|
+
}
|
|
844
|
+
|
|
845
|
+
return {
|
|
846
|
+
execute,
|
|
847
|
+
releaseSession,
|
|
848
|
+
closeAll,
|
|
849
|
+
};
|
|
850
|
+
}
|
|
851
|
+
|
|
852
|
+
let sharedSessions = null;
|
|
853
|
+
function getMacSessions() {
|
|
854
|
+
if (!sharedSessions) {
|
|
855
|
+
sharedSessions = createMacSessions();
|
|
856
|
+
}
|
|
857
|
+
return sharedSessions;
|
|
858
|
+
}
|
|
859
|
+
|
|
860
|
+
module.exports = {
|
|
861
|
+
ALL_OPERATIONS,
|
|
862
|
+
READ_OPERATIONS,
|
|
863
|
+
INPUT_OPERATIONS,
|
|
864
|
+
createMacSessions,
|
|
865
|
+
getMacSessions,
|
|
866
|
+
};
|