@roam-agent/roam-computer-use 0.0.0-stage → 0.5.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/LICENSE +21 -0
- package/README.md +80 -2
- package/assets/reference/img.jpg +0 -0
- package/docs/README.md +45 -0
- package/docs/content/docs/explanation/action-pipeline.mdx +36 -0
- package/docs/content/docs/explanation/architecture.mdx +161 -0
- package/docs/content/docs/explanation/index.mdx +19 -0
- package/docs/content/docs/explanation/meta.json +5 -0
- package/docs/content/docs/explanation/observation-pipeline.mdx +37 -0
- package/docs/content/docs/explanation/platform-boundary.mdx +37 -0
- package/docs/content/docs/explanation/state-and-scheduling.mdx +35 -0
- package/docs/content/docs/how-to-guides/develop-and-validate.mdx +112 -0
- package/docs/content/docs/how-to-guides/index.mdx +12 -0
- package/docs/content/docs/how-to-guides/linux.mdx +68 -0
- package/docs/content/docs/how-to-guides/meta.json +1 -0
- package/docs/content/docs/how-to-guides/troubleshooting.mdx +126 -0
- package/docs/content/docs/index.mdx +67 -0
- package/docs/content/docs/meta.json +5 -0
- package/docs/content/docs/reference/agent/contract.mdx +162 -0
- package/docs/content/docs/reference/agent/index.mdx +8 -0
- package/docs/content/docs/reference/agent/meta.json +8 -0
- package/docs/content/docs/reference/agent/tools.mdx +221 -0
- package/docs/content/docs/reference/codebase.mdx +139 -0
- package/docs/content/docs/reference/configuration.mdx +113 -0
- package/docs/content/docs/reference/index.mdx +28 -0
- package/docs/content/docs/reference/meta.json +10 -0
- package/docs/content/docs/reference/platforms/index.mdx +18 -0
- package/docs/content/docs/reference/platforms/macos/accessibility.mdx +61 -0
- package/docs/content/docs/reference/platforms/macos/actions.mdx +63 -0
- package/docs/content/docs/reference/platforms/macos/capture.mdx +58 -0
- package/docs/content/docs/reference/platforms/macos/implementation.mdx +98 -0
- package/docs/content/docs/reference/platforms/macos/index.mdx +16 -0
- package/docs/content/docs/reference/platforms/macos/meta.json +10 -0
- package/docs/content/docs/reference/platforms/macos/root-targeting.mdx +47 -0
- package/docs/content/docs/reference/platforms/meta.json +8 -0
- package/docs/content/docs/reference/platforms/windows.mdx +75 -0
- package/docs/content/docs/tutorials/getting-started.mdx +46 -0
- package/docs/content/docs/tutorials/index.mdx +10 -0
- package/docs/content/docs/tutorials/meta.json +1 -0
- package/extensions/computer-use.ts +235 -0
- package/native/linux/bridge-rs/Cargo.lock +1204 -0
- package/native/linux/bridge-rs/Cargo.toml +21 -0
- package/native/linux/bridge-rs/src/atspi.rs +670 -0
- package/native/linux/bridge-rs/src/error.rs +65 -0
- package/native/linux/bridge-rs/src/lib.rs +11 -0
- package/native/linux/bridge-rs/src/main.rs +1110 -0
- package/native/linux/bridge-rs/src/protocol.rs +49 -0
- package/native/linux/bridge-rs/src/state.rs +293 -0
- package/native/linux/bridge-rs/src/wayland.rs +89 -0
- package/native/linux/bridge-rs/src/x11.rs +909 -0
- package/native/linux/bridge-rs/tests/protocol_tests.rs +73 -0
- package/native/macos/activity_glow.swift +169 -0
- package/native/macos/bridge.swift +3968 -0
- package/native/windows/bridge-rs/Cargo.lock +396 -0
- package/native/windows/bridge-rs/Cargo.toml +30 -0
- package/native/windows/bridge-rs/src/capture.rs +561 -0
- package/native/windows/bridge-rs/src/error.rs +81 -0
- package/native/windows/bridge-rs/src/input.rs +501 -0
- package/native/windows/bridge-rs/src/lib.rs +85 -0
- package/native/windows/bridge-rs/src/main.rs +1683 -0
- package/native/windows/bridge-rs/src/protocol.rs +53 -0
- package/native/windows/bridge-rs/src/refs.rs +237 -0
- package/native/windows/bridge-rs/src/state.rs +55 -0
- package/native/windows/bridge-rs/src/uia.rs +1252 -0
- package/native/windows/bridge-rs/src/window.rs +718 -0
- package/native/windows/bridge-rs/tests/protocol_tests.rs +252 -0
- package/native/windows/bridge-rs/tests/refs_tests.rs +86 -0
- package/native/windows/bridge-rs/tests/state_tests.rs +33 -0
- package/package.json +104 -5
- package/prebuilt/macos/arm64/bridge +0 -0
- package/prebuilt/macos/x64/bridge +0 -0
- package/roam/README.md +52 -0
- package/roam/bridge/macos/arm64/bridge +0 -0
- package/roam/bridge/macos/x64/bridge +0 -0
- package/roam/bridge/manifest.json +20 -0
- package/roam/helper-path.d.mts +9 -0
- package/roam/helper-path.mjs +36 -0
- package/roam/identity.d.mts +16 -0
- package/roam/identity.mjs +64 -0
- package/roam/index.ts +51 -0
- package/roam/redirect.ts +51 -0
- package/roam/repair.ts +120 -0
- package/scripts/build-native.mjs +286 -0
- package/scripts/setup-helper.mjs +635 -0
- package/src/actions.ts +130 -0
- package/src/bridge.ts +2594 -0
- package/src/cdp.ts +658 -0
- package/src/config.ts +226 -0
- package/src/contract.ts +107 -0
- package/src/extension/index.ts +9 -0
- package/src/find-params.ts +21 -0
- package/src/note.ts +195 -0
- package/src/outline.ts +702 -0
- package/src/output.ts +134 -0
- package/src/permissions.ts +111 -0
- package/src/platform/architecture.ts +23 -0
- package/src/platform/coerce.ts +16 -0
- package/src/platform/index.ts +61 -0
- package/src/platform/linux/backend.ts +186 -0
- package/src/platform/linux/helper.ts +238 -0
- package/src/platform/macos/backend.ts +147 -0
- package/src/platform/macos/browser.ts +110 -0
- package/src/platform/macos/helper.ts +369 -0
- package/src/platform/macos/permissions.ts +146 -0
- package/src/platform/types.ts +229 -0
- package/src/platform/windows/backend.ts +142 -0
- package/src/platform/windows/helper.ts +140 -0
- package/src/root-selection.ts +25 -0
- package/src/runtime.ts +129 -0
- package/src/state.ts +146 -0
- package/src/view.ts +147 -0
|
@@ -0,0 +1,369 @@
|
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
2
|
+
import { mkdir } from "node:fs/promises";
|
|
3
|
+
import net from "node:net";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import { fileURLToPath } from "node:url";
|
|
6
|
+
// Fork overlay: redirection wiring and helper defect fixing live outside upstream `src/`.
|
|
7
|
+
import {
|
|
8
|
+
ROAM_HELPER_APP_EXECUTABLE_PATH,
|
|
9
|
+
ROAM_HELPER_APP_PATH,
|
|
10
|
+
ROAM_HELPER_BUNDLE_ID,
|
|
11
|
+
ROAM_HELPER_SETUP_TIMEOUT_MS,
|
|
12
|
+
ROAM_HELPER_SOCKET_PATH,
|
|
13
|
+
createHelperRepair,
|
|
14
|
+
isExecutable,
|
|
15
|
+
usingRedirectedHelperSocket,
|
|
16
|
+
} from "../../../roam/index.ts";
|
|
17
|
+
import { toBoolean, toFiniteNumber, toOptionalString } from "../coerce.ts";
|
|
18
|
+
import type { PlatformDiagnostics } from "../types.ts";
|
|
19
|
+
|
|
20
|
+
const COMMAND_TIMEOUT_MS = 15_000;
|
|
21
|
+
|
|
22
|
+
// Backwards-compatible aliases so `src/` consumers keep importing one module.
|
|
23
|
+
export const HELPER_BUNDLE_ID = ROAM_HELPER_BUNDLE_ID;
|
|
24
|
+
export const HELPER_APP_PATH = ROAM_HELPER_APP_PATH;
|
|
25
|
+
export const HELPER_APP_EXECUTABLE_PATH = ROAM_HELPER_APP_EXECUTABLE_PATH;
|
|
26
|
+
export const HELPER_SOCKET_PATH = ROAM_HELPER_SOCKET_PATH;
|
|
27
|
+
const usingExternalHelperSocket = usingRedirectedHelperSocket;
|
|
28
|
+
|
|
29
|
+
const PACKAGE_ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..", "..", "..");
|
|
30
|
+
const SETUP_HELPER_SCRIPT = path.join(PACKAGE_ROOT, "scripts", "setup-helper.mjs");
|
|
31
|
+
|
|
32
|
+
export class HelperTransportError extends Error {
|
|
33
|
+
constructor(message: string) {
|
|
34
|
+
super(message);
|
|
35
|
+
this.name = "HelperTransportError";
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export class HelperCommandError extends Error {
|
|
40
|
+
readonly code?: string;
|
|
41
|
+
|
|
42
|
+
constructor(message: string, code?: string) {
|
|
43
|
+
super(message);
|
|
44
|
+
this.name = "HelperCommandError";
|
|
45
|
+
this.code = code;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function throwIfAborted(signal?: AbortSignal): void {
|
|
50
|
+
if (signal?.aborted) throw new Error("Operation aborted.");
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
async function sleep(ms: number, signal?: AbortSignal): Promise<void> {
|
|
54
|
+
throwIfAborted(signal);
|
|
55
|
+
await new Promise<void>((resolve, reject) => {
|
|
56
|
+
const timer = setTimeout(resolve, ms);
|
|
57
|
+
const onAbort = () => {
|
|
58
|
+
clearTimeout(timer);
|
|
59
|
+
reject(new Error("Operation aborted."));
|
|
60
|
+
};
|
|
61
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
62
|
+
}).finally(() => signal?.throwIfAborted?.());
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export async function runProcess(
|
|
66
|
+
command: string,
|
|
67
|
+
args: string[],
|
|
68
|
+
timeoutMs: number,
|
|
69
|
+
signal?: AbortSignal,
|
|
70
|
+
env?: NodeJS.ProcessEnv,
|
|
71
|
+
): Promise<void> {
|
|
72
|
+
throwIfAborted(signal);
|
|
73
|
+
|
|
74
|
+
await new Promise<void>((resolve, reject) => {
|
|
75
|
+
const child = spawn(command, args, {
|
|
76
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
77
|
+
env,
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
let stderr = "";
|
|
81
|
+
let stdout = "";
|
|
82
|
+
|
|
83
|
+
const timer = setTimeout(() => {
|
|
84
|
+
child.kill("SIGTERM");
|
|
85
|
+
cleanup();
|
|
86
|
+
reject(new Error(`Command timed out after ${timeoutMs}ms: ${command} ${args.join(" ")}`));
|
|
87
|
+
}, timeoutMs);
|
|
88
|
+
|
|
89
|
+
const onAbort = () => {
|
|
90
|
+
child.kill("SIGTERM");
|
|
91
|
+
cleanup();
|
|
92
|
+
reject(new Error("Operation aborted."));
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
const cleanup = () => {
|
|
96
|
+
clearTimeout(timer);
|
|
97
|
+
signal?.removeEventListener("abort", onAbort);
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
child.stdout.on("data", (chunk) => {
|
|
101
|
+
stdout += String(chunk);
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
child.stderr.on("data", (chunk) => {
|
|
105
|
+
stderr += String(chunk);
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
child.on("error", (error) => {
|
|
109
|
+
cleanup();
|
|
110
|
+
reject(error);
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
child.on("close", (code) => {
|
|
114
|
+
cleanup();
|
|
115
|
+
if (code === 0) {
|
|
116
|
+
resolve();
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
const output = [stderr.trim(), stdout.trim()].filter(Boolean).join("\n");
|
|
120
|
+
reject(new Error(`Command failed (${code}): ${command} ${args.join(" ")}\n${output}`.trim()));
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export class MacosHelperClient {
|
|
128
|
+
private daemonAvailable = false;
|
|
129
|
+
private requestSequence = 0;
|
|
130
|
+
private diagnosticsCache?: PlatformDiagnostics;
|
|
131
|
+
|
|
132
|
+
// Fork overlay: install/protocol repair policy lives in `roam/repair.ts`.
|
|
133
|
+
private readonly repair = createHelperRepair({
|
|
134
|
+
reinstall: async (signal) => {
|
|
135
|
+
// Re-enter Electron and Bun standalone hosts as their JavaScript runtimes.
|
|
136
|
+
await runProcess(process.execPath, [SETUP_HELPER_SCRIPT, "--runtime"], ROAM_HELPER_SETUP_TIMEOUT_MS, signal, {
|
|
137
|
+
...process.env,
|
|
138
|
+
ELECTRON_RUN_AS_NODE: "1",
|
|
139
|
+
BUN_BE_BUN: "1",
|
|
140
|
+
});
|
|
141
|
+
if (!(await isExecutable(ROAM_HELPER_APP_EXECUTABLE_PATH))) {
|
|
142
|
+
throw new Error(`Failed to install pi-computer-use helper app at ${ROAM_HELPER_APP_PATH}.`);
|
|
143
|
+
}
|
|
144
|
+
},
|
|
145
|
+
restart: async (signal) => {
|
|
146
|
+
await this.restart(signal);
|
|
147
|
+
},
|
|
148
|
+
readDiagnostics: async (signal) => {
|
|
149
|
+
return await this.diagnosticsCommand(signal);
|
|
150
|
+
},
|
|
151
|
+
markDaemonUnavailable: () => {
|
|
152
|
+
this.daemonAvailable = false;
|
|
153
|
+
},
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
get diagnostics(): PlatformDiagnostics | undefined {
|
|
157
|
+
return this.diagnosticsCache;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
async ensureInstalled(signal?: AbortSignal): Promise<void> {
|
|
161
|
+
await this.repair.ensureInstalled(signal);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
async launchDaemon(signal?: AbortSignal): Promise<void> {
|
|
165
|
+
if (usingExternalHelperSocket) throw new HelperTransportError(`External helper socket is unavailable at ${HELPER_SOCKET_PATH}.`);
|
|
166
|
+
await mkdir(path.dirname(HELPER_SOCKET_PATH), { recursive: true });
|
|
167
|
+
// Open the resolved bundle directly so a legacy system-wide copy with the
|
|
168
|
+
// same bundle id cannot win LaunchServices resolution.
|
|
169
|
+
await runProcess("open", ["-n", "-g", HELPER_APP_PATH, "--args", "serve", "--socket", HELPER_SOCKET_PATH], COMMAND_TIMEOUT_MS, signal);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
async daemonCommand<T>(cmd: string, args: Record<string, unknown>, timeoutMs: number, signal?: AbortSignal): Promise<T> {
|
|
173
|
+
return await new Promise<T>((resolve, reject) => {
|
|
174
|
+
const id = `req_${++this.requestSequence}`;
|
|
175
|
+
const socket = net.createConnection(HELPER_SOCKET_PATH);
|
|
176
|
+
let buffer = "";
|
|
177
|
+
const timer = setTimeout(() => { socket.destroy(); reject(new HelperTransportError(`Daemon command '${cmd}' timed out after ${timeoutMs}ms.`)); }, timeoutMs);
|
|
178
|
+
const cleanup = () => { clearTimeout(timer); signal?.removeEventListener("abort", onAbort); };
|
|
179
|
+
const onAbort = () => { socket.destroy(); cleanup(); reject(new Error("Operation aborted.")); };
|
|
180
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
181
|
+
socket.setEncoding("utf8");
|
|
182
|
+
socket.on("connect", () => socket.write(`${JSON.stringify({ id, cmd, ...args })}\n`));
|
|
183
|
+
socket.on("data", (chunk) => {
|
|
184
|
+
buffer += chunk;
|
|
185
|
+
const newline = buffer.indexOf("\n");
|
|
186
|
+
if (newline < 0) return;
|
|
187
|
+
cleanup();
|
|
188
|
+
socket.end();
|
|
189
|
+
try {
|
|
190
|
+
const parsed = JSON.parse(buffer.slice(0, newline));
|
|
191
|
+
if (parsed.ok === true) resolve(parsed.result as T);
|
|
192
|
+
else reject(new HelperCommandError(parsed?.error?.message ?? `Daemon command '${cmd}' failed.`, parsed?.error?.code));
|
|
193
|
+
} catch (error) {
|
|
194
|
+
reject(error);
|
|
195
|
+
}
|
|
196
|
+
});
|
|
197
|
+
socket.on("error", (error) => { cleanup(); reject(new HelperTransportError(error.message)); });
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
async ensureDaemon(signal?: AbortSignal): Promise<boolean> {
|
|
202
|
+
if (this.daemonAvailable) return true;
|
|
203
|
+
try {
|
|
204
|
+
await this.daemonCommand("diagnostics", {}, 1_000, signal);
|
|
205
|
+
this.daemonAvailable = true;
|
|
206
|
+
return true;
|
|
207
|
+
} catch {}
|
|
208
|
+
await this.launchDaemon(signal).catch(() => undefined);
|
|
209
|
+
for (let index = 0; index < 30; index += 1) {
|
|
210
|
+
try {
|
|
211
|
+
await this.daemonCommand("diagnostics", {}, 1_000, signal);
|
|
212
|
+
this.daemonAvailable = true;
|
|
213
|
+
return true;
|
|
214
|
+
} catch {
|
|
215
|
+
await sleep(100, signal);
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
return false;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
async command<T>(cmd: string, args: Record<string, unknown> = {}, options?: { timeoutMs?: number; signal?: AbortSignal }): Promise<T> {
|
|
222
|
+
const timeoutMs = options?.timeoutMs ?? COMMAND_TIMEOUT_MS;
|
|
223
|
+
if (!(await this.ensureDaemon(options?.signal))) {
|
|
224
|
+
throw new HelperTransportError(`pi-computer-use helper app daemon is unavailable at ${HELPER_APP_PATH}.`);
|
|
225
|
+
}
|
|
226
|
+
try {
|
|
227
|
+
return await this.daemonCommand<T>(cmd, args, timeoutMs, options?.signal);
|
|
228
|
+
} catch (error) {
|
|
229
|
+
this.daemonAvailable = false;
|
|
230
|
+
throw error instanceof Error ? error : new Error(String(error));
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
async restart(signal?: AbortSignal): Promise<void> {
|
|
235
|
+
await this.command("shutdown", {}, { signal, timeoutMs: 2_000 }).catch(() => undefined);
|
|
236
|
+
this.daemonAvailable = false;
|
|
237
|
+
await sleep(400, signal);
|
|
238
|
+
if (!(await this.ensureDaemon(signal))) {
|
|
239
|
+
throw new Error(`pi-computer-use helper did not come back after restart. Helper app: ${HELPER_APP_PATH}`);
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
async diagnosticsCommand(signal?: AbortSignal): Promise<PlatformDiagnostics> {
|
|
244
|
+
const result = await this.command<any>("diagnostics", {}, { signal });
|
|
245
|
+
const diagnostics = {
|
|
246
|
+
protocolVersion: Math.trunc(toFiniteNumber(result?.protocolVersion, 0)),
|
|
247
|
+
architectureVersion: Math.trunc(toFiniteNumber(result?.architectureVersion, 0)),
|
|
248
|
+
invariants: Array.isArray(result?.invariants) ? result.invariants.filter((value: unknown): value is string => typeof value === "string") : [],
|
|
249
|
+
pid: Math.trunc(toFiniteNumber(result?.pid, 0)),
|
|
250
|
+
parentPid: Math.trunc(toFiniteNumber(result?.parentPid, 0)) || undefined,
|
|
251
|
+
parentAppName: toOptionalString(result?.parentAppName),
|
|
252
|
+
parentBundleId: toOptionalString(result?.parentBundleId),
|
|
253
|
+
parentPath: toOptionalString(result?.parentPath),
|
|
254
|
+
executablePath: toOptionalString(result?.executablePath),
|
|
255
|
+
os: toOptionalString(result?.macOS),
|
|
256
|
+
arch: toOptionalString(result?.arch),
|
|
257
|
+
accessibility: toBoolean(result?.accessibility),
|
|
258
|
+
screenRecording: toBoolean(result?.screenRecording),
|
|
259
|
+
};
|
|
260
|
+
this.diagnosticsCache = diagnostics;
|
|
261
|
+
return diagnostics;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
async ensureProtocol(signal?: AbortSignal): Promise<PlatformDiagnostics> {
|
|
265
|
+
// Defect fixing (detect -> relaunch -> reinstall -> re-verify) is owned by the fork overlay.
|
|
266
|
+
return await this.repair.ensureProtocol(signal);
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
export const macosHelper = new MacosHelperClient();
|
|
271
|
+
|
|
272
|
+
class MacosActivityGlowConnection {
|
|
273
|
+
private socket?: net.Socket;
|
|
274
|
+
private connecting?: Promise<net.Socket>;
|
|
275
|
+
private tail: Promise<void> = Promise.resolve();
|
|
276
|
+
private requestSequence = 0;
|
|
277
|
+
|
|
278
|
+
async setActive(active: boolean): Promise<void> {
|
|
279
|
+
const operation = this.tail.then(async () => {
|
|
280
|
+
const socket = await this.ensureConnection();
|
|
281
|
+
await this.send(socket, active);
|
|
282
|
+
});
|
|
283
|
+
this.tail = operation.catch(() => undefined);
|
|
284
|
+
await operation;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
async close(): Promise<void> {
|
|
288
|
+
await this.tail.catch(() => undefined);
|
|
289
|
+
const socket = this.socket;
|
|
290
|
+
this.socket = undefined;
|
|
291
|
+
this.connecting = undefined;
|
|
292
|
+
if (!socket) return;
|
|
293
|
+
if (socket.writable) socket.end();
|
|
294
|
+
else socket.destroy();
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
private async ensureConnection(): Promise<net.Socket> {
|
|
298
|
+
if (this.socket && !this.socket.destroyed) return this.socket;
|
|
299
|
+
if (this.connecting) return await this.connecting;
|
|
300
|
+
|
|
301
|
+
this.connecting = (async () => {
|
|
302
|
+
if (!(await macosHelper.ensureDaemon())) {
|
|
303
|
+
throw new HelperTransportError(`pi-computer-use helper app daemon is unavailable at ${HELPER_APP_PATH}.`);
|
|
304
|
+
}
|
|
305
|
+
return await new Promise<net.Socket>((resolve, reject) => {
|
|
306
|
+
const socket = net.createConnection(HELPER_SOCKET_PATH);
|
|
307
|
+
const onError = (error: Error) => {
|
|
308
|
+
socket.destroy();
|
|
309
|
+
reject(new HelperTransportError(error.message));
|
|
310
|
+
};
|
|
311
|
+
socket.once("error", onError);
|
|
312
|
+
socket.once("connect", () => {
|
|
313
|
+
socket.off("error", onError);
|
|
314
|
+
socket.on("error", () => socket.destroy());
|
|
315
|
+
socket.on("close", () => {
|
|
316
|
+
if (this.socket === socket) this.socket = undefined;
|
|
317
|
+
});
|
|
318
|
+
this.socket = socket;
|
|
319
|
+
resolve(socket);
|
|
320
|
+
});
|
|
321
|
+
});
|
|
322
|
+
})();
|
|
323
|
+
|
|
324
|
+
try {
|
|
325
|
+
return await this.connecting;
|
|
326
|
+
} finally {
|
|
327
|
+
this.connecting = undefined;
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
private async send(socket: net.Socket, active: boolean): Promise<void> {
|
|
332
|
+
await new Promise<void>((resolve, reject) => {
|
|
333
|
+
const id = `activity_${++this.requestSequence}`;
|
|
334
|
+
let buffer = "";
|
|
335
|
+
const timer = setTimeout(() => {
|
|
336
|
+
cleanup();
|
|
337
|
+
socket.destroy();
|
|
338
|
+
reject(new HelperTransportError("Activity glow command timed out."));
|
|
339
|
+
}, 2_000);
|
|
340
|
+
const cleanup = () => {
|
|
341
|
+
clearTimeout(timer);
|
|
342
|
+
socket.off("data", onData);
|
|
343
|
+
socket.off("close", onClose);
|
|
344
|
+
};
|
|
345
|
+
const onClose = () => {
|
|
346
|
+
cleanup();
|
|
347
|
+
reject(new HelperTransportError("Activity glow connection closed."));
|
|
348
|
+
};
|
|
349
|
+
const onData = (chunk: Buffer | string) => {
|
|
350
|
+
buffer += String(chunk);
|
|
351
|
+
const newline = buffer.indexOf("\n");
|
|
352
|
+
if (newline < 0) return;
|
|
353
|
+
cleanup();
|
|
354
|
+
try {
|
|
355
|
+
const response = JSON.parse(buffer.slice(0, newline));
|
|
356
|
+
if (response.id === id && response.ok === true) resolve();
|
|
357
|
+
else reject(new HelperCommandError(response?.error?.message ?? "Activity glow command failed.", response?.error?.code));
|
|
358
|
+
} catch (error) {
|
|
359
|
+
reject(error);
|
|
360
|
+
}
|
|
361
|
+
};
|
|
362
|
+
socket.on("data", onData);
|
|
363
|
+
socket.once("close", onClose);
|
|
364
|
+
socket.write(`${JSON.stringify({ id, cmd: "setActivityGlow", active })}\n`);
|
|
365
|
+
});
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
export const macosActivityGlow = new MacosActivityGlowConnection();
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import { ensurePermissions, type PermissionKind, type PermissionStatus } from "../../permissions.ts";
|
|
3
|
+
import { toBoolean, toFiniteNumber, toOptionalString } from "../coerce.ts";
|
|
4
|
+
import type { PlatformReadyState } from "../types.ts";
|
|
5
|
+
import { HELPER_APP_PATH, macosHelper } from "./helper.ts";
|
|
6
|
+
import { assertPlatformArchitecture } from "../architecture.ts";
|
|
7
|
+
|
|
8
|
+
const GRANT_INSTRUCTIONS =
|
|
9
|
+
"Grant Accessibility and Screen Recording to pi-computer-use.app in System Settings → Privacy & Security. " +
|
|
10
|
+
"Screen Recording lets the agent see the window; Accessibility lets it interact with the window.";
|
|
11
|
+
|
|
12
|
+
const SIGNING_MIGRATION_WARNING =
|
|
13
|
+
"If these permissions were enabled before this install/update, macOS invalidated the old grants because " +
|
|
14
|
+
"pi-computer-use.app was re-signed. Re-enable both toggles for the newly signed helper. " +
|
|
15
|
+
"If a toggle is already on, switch it off and on again.";
|
|
16
|
+
|
|
17
|
+
const macosPermissionKinds = [
|
|
18
|
+
{ kind: "accessibility" as const, openOption: "Open Accessibility Settings (missing)" },
|
|
19
|
+
{ kind: "screenRecording" as const, openOption: "Open Screen Recording Settings (missing)" },
|
|
20
|
+
];
|
|
21
|
+
|
|
22
|
+
function permissionStatusSummary(status: PermissionStatus): string {
|
|
23
|
+
const lines = [
|
|
24
|
+
`Accessibility: ${status.accessibility ? "granted" : "missing"}`,
|
|
25
|
+
`Screen Recording: ${status.screenRecording ? "granted" : "missing"}`,
|
|
26
|
+
];
|
|
27
|
+
if (status.screenRecordingPreflight && !status.screenRecording) {
|
|
28
|
+
lines.push(
|
|
29
|
+
"(Screen Recording reads granted in the TCC database but a live capture probe failed — " +
|
|
30
|
+
"the grant likely belongs to a different app identity, or the helper needs a restart.)",
|
|
31
|
+
);
|
|
32
|
+
}
|
|
33
|
+
return lines.join("; ");
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function permissionPrompt(status: PermissionStatus, helperPath: string, hint?: string): string {
|
|
37
|
+
return [
|
|
38
|
+
"pi-computer-use needs macOS permissions for its helper app.",
|
|
39
|
+
permissionStatusSummary(status),
|
|
40
|
+
"",
|
|
41
|
+
`Helper: pi-computer-use.app (${helperPath})`,
|
|
42
|
+
hint,
|
|
43
|
+
"",
|
|
44
|
+
`Important: ${SIGNING_MIGRATION_WARNING}`,
|
|
45
|
+
"",
|
|
46
|
+
"pi-computer-use.app is already listed in the pane(s) — enable its toggle, then choose Recheck.",
|
|
47
|
+
].filter(Boolean).join("\n");
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function missingPermissionMessage(kinds: PermissionKind[]): string {
|
|
51
|
+
return `Still missing after restart: ${kinds.join(" and ")}. ${SIGNING_MIGRATION_WARNING} Then choose Recheck again.`;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
async function checkPermissions(signal?: AbortSignal): Promise<PermissionStatus> {
|
|
55
|
+
const result = await macosHelper.command<any>("checkPermissions", {}, { signal });
|
|
56
|
+
const rawSource = result?.source;
|
|
57
|
+
return {
|
|
58
|
+
accessibility: toBoolean(result?.accessibility),
|
|
59
|
+
// Authoritative: the helper's live ScreenCaptureKit probe.
|
|
60
|
+
screenRecording: toBoolean(result?.screenRecordingCapturable),
|
|
61
|
+
// Keep the preflight value separate: disagreement means stale per-process
|
|
62
|
+
// TCC cache or a grant row belonging to another app identity.
|
|
63
|
+
screenRecordingPreflight: toBoolean(result?.screenRecordingPreflight),
|
|
64
|
+
source: rawSource && typeof rawSource === "object"
|
|
65
|
+
? {
|
|
66
|
+
// macOS attributes Accessibility / Screen Recording grants to the
|
|
67
|
+
// responsible process at the top of the launch chain. "helper-app"
|
|
68
|
+
// is the canonical installed app via LaunchServices; "caller" means
|
|
69
|
+
// grants would attach to the launching app instead.
|
|
70
|
+
attribution: rawSource.attribution === "helper-app" ? "helper-app" : "caller",
|
|
71
|
+
pid: Math.trunc(toFiniteNumber(rawSource.pid, 0)) || undefined,
|
|
72
|
+
parentPid: Math.trunc(toFiniteNumber(rawSource.parentPid, 0)) || undefined,
|
|
73
|
+
executablePath: toOptionalString(rawSource.executablePath),
|
|
74
|
+
parentPath: toOptionalString(rawSource.parentPath),
|
|
75
|
+
parentBundleId: toOptionalString(rawSource.parentBundleId),
|
|
76
|
+
os: toOptionalString(rawSource.macOS),
|
|
77
|
+
}
|
|
78
|
+
: undefined,
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
async function registerPermissions(signal?: AbortSignal): Promise<void> {
|
|
83
|
+
// Raises the Accessibility prompt and performs a real ScreenCaptureKit
|
|
84
|
+
// capture attempt so pi-computer-use.app is pre-listed in both Settings
|
|
85
|
+
// panes; the user only flips toggles, no "+" path picking.
|
|
86
|
+
await macosHelper.command("registerPermissions", {}, { signal, timeoutMs: 15_000 });
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export async function ensureMacosReady(
|
|
90
|
+
ctx: ExtensionContext,
|
|
91
|
+
state: PlatformReadyState,
|
|
92
|
+
signal?: AbortSignal,
|
|
93
|
+
): Promise<PlatformReadyState> {
|
|
94
|
+
await macosHelper.ensureInstalled(signal);
|
|
95
|
+
if (!(await macosHelper.ensureDaemon(signal))) {
|
|
96
|
+
throw new Error(`pi-computer-use helper app daemon did not start. Helper app: ${HELPER_APP_PATH}`);
|
|
97
|
+
}
|
|
98
|
+
const helperDiagnostics = await macosHelper.ensureProtocol(signal);
|
|
99
|
+
assertPlatformArchitecture("macOS", helperDiagnostics);
|
|
100
|
+
|
|
101
|
+
const now = Date.now();
|
|
102
|
+
const cachedStatus = state.permissionStatus;
|
|
103
|
+
const canUseCachedPermissions =
|
|
104
|
+
cachedStatus?.accessibility &&
|
|
105
|
+
cachedStatus.screenRecording &&
|
|
106
|
+
now - state.lastPermissionCheckAt < 2_000;
|
|
107
|
+
if (canUseCachedPermissions) {
|
|
108
|
+
return { ...state, helperDiagnostics };
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
let permissionStatus = await checkPermissions(signal);
|
|
112
|
+
let lastPermissionCheckAt = now;
|
|
113
|
+
|
|
114
|
+
if (!permissionStatus.accessibility || !permissionStatus.screenRecording) {
|
|
115
|
+
// Attribution "caller" means the helper is not running as the
|
|
116
|
+
// canonical installed app — grants would attach to the wrong identity.
|
|
117
|
+
const attributionHint = permissionStatus.source?.attribution === "caller"
|
|
118
|
+
? `Warning: the helper is not running as the installed pi-computer-use.app (executable: ${permissionStatus.source?.executablePath ?? "unknown"}). Grants made now would attach to the launching app instead. Restart Pi so the canonical helper is used.`
|
|
119
|
+
: undefined;
|
|
120
|
+
permissionStatus = await ensurePermissions(
|
|
121
|
+
ctx,
|
|
122
|
+
{
|
|
123
|
+
kinds: macosPermissionKinds,
|
|
124
|
+
copy: {
|
|
125
|
+
nonInteractiveError: (helperPath) => `pi-computer-use setup requires an interactive session. Start pi in interactive mode. ${GRANT_INSTRUCTIONS}\nHelper path: ${helperPath}`,
|
|
126
|
+
prompt: permissionPrompt,
|
|
127
|
+
incompleteError: (helperPath) => `pi-computer-use setup is incomplete. ${GRANT_INSTRUCTIONS} Helper path: ${helperPath}`,
|
|
128
|
+
readyMessage: "pi-computer-use is ready.",
|
|
129
|
+
stillMissing: missingPermissionMessage,
|
|
130
|
+
},
|
|
131
|
+
checkPermissions: (permissionSignal) => checkPermissions(permissionSignal ?? signal),
|
|
132
|
+
registerPermissions: (permissionSignal) => registerPermissions(permissionSignal ?? signal),
|
|
133
|
+
openPermissionPane: async (kind, permissionSignal) => {
|
|
134
|
+
await macosHelper.command("openPermissionPane", { kind }, { signal: permissionSignal ?? signal });
|
|
135
|
+
},
|
|
136
|
+
restartHelper: (permissionSignal) => macosHelper.restart(permissionSignal ?? signal),
|
|
137
|
+
permissionHint: attributionHint,
|
|
138
|
+
},
|
|
139
|
+
HELPER_APP_PATH,
|
|
140
|
+
signal,
|
|
141
|
+
);
|
|
142
|
+
lastPermissionCheckAt = Date.now();
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
return { permissionStatus, lastPermissionCheckAt, helperDiagnostics };
|
|
146
|
+
}
|