trusted-user-agent 0.0.0-stage → 0.4.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 +232 -2
- package/dist/acp/agent.js +719 -0
- package/dist/acp/agents/connection.js +298 -0
- package/dist/acp/agents/manager.js +363 -0
- package/dist/acp/agents/registry.js +201 -0
- package/dist/acp/agents/relay-session.js +371 -0
- package/dist/acp/agents/session-index.js +102 -0
- package/dist/acp/backend.js +2 -0
- package/dist/acp/config-options.js +26 -0
- package/dist/acp/mock-backend.js +277 -0
- package/dist/acp/sdk-backend.js +495 -0
- package/dist/browser/mcp-acp-client.js +81 -0
- package/dist/browser/provider.js +1049 -0
- package/dist/browser/schemas.js +221 -0
- package/dist/capability-registry.js +92 -0
- package/dist/client-heartbeat.js +118 -0
- package/dist/compose/schemas.js +90 -0
- package/dist/contacts/schemas.js +26 -0
- package/dist/installer/cli.js +70 -0
- package/dist/installer/common.js +93 -0
- package/dist/installer/index.js +366 -0
- package/dist/installer/mcp-clients.js +555 -0
- package/dist/installer/mcp.js +166 -0
- package/dist/installer/platforms.js +277 -0
- package/dist/logger.js +54 -0
- package/dist/mail/schemas.js +98 -0
- package/dist/mutation/schemas.js +31 -0
- package/dist/native-host/broker-ipc.js +344 -0
- package/dist/native-host/framing.js +76 -0
- package/dist/native-host/main.js +545 -0
- package/dist/native-host/mcp-server.js +333 -0
- package/dist/native-host/mcp-stdio-bridge.js +184 -0
- package/dist/native-host/mcp-stdio-main.js +13 -0
- package/dist/native-host/relay.js +84 -0
- package/dist/native-host/skills-startup.js +167 -0
- package/dist/native-host/transport.js +248 -0
- package/dist/permission-store.js +207 -0
- package/dist/repl/fs-sandbox.js +187 -0
- package/dist/repl/provider.js +252 -0
- package/dist/repl/runtime.js +436 -0
- package/dist/repl/types.js +10 -0
- package/dist/repl/worker.js +731 -0
- package/dist/skills/extension.js +99 -0
- package/dist/skills/grants-file.js +145 -0
- package/dist/skills/isolated-runner.js +259 -0
- package/dist/skills/mock-session-factory.js +49 -0
- package/dist/skills/package-roots.js +25 -0
- package/dist/skills/pi-bridge.js +8 -0
- package/dist/skills/pi-integration.js +110 -0
- package/dist/skills/planner.js +128 -0
- package/dist/skills/registry.js +104 -0
- package/dist/skills/skills-protocol.js +253 -0
- package/dist/skills/skills-service.js +195 -0
- package/dist/tool-bridge.js +8 -0
- package/dist/workspace.js +70 -0
- package/extensions/pi-browser-tools.ts +65 -0
- package/extensions/pi-browser.ts +60 -0
- package/extensions/pi-skills.ts +14 -0
- package/native/pi-agent-host +2 -0
- package/native/pi-browser-host +5 -0
- package/native/pi-browser-host.cmd +3 -0
- package/node_modules/@pi-browser/protocol/.bundled-by-trusted-user-agent +0 -0
- package/node_modules/@pi-browser/protocol/dist/acp.d.ts +10 -0
- package/node_modules/@pi-browser/protocol/dist/acp.js +9 -0
- package/node_modules/@pi-browser/protocol/dist/agents.d.ts +51 -0
- package/node_modules/@pi-browser/protocol/dist/agents.js +44 -0
- package/node_modules/@pi-browser/protocol/dist/browser-tools.d.ts +33 -0
- package/node_modules/@pi-browser/protocol/dist/browser-tools.js +478 -0
- package/node_modules/@pi-browser/protocol/dist/callers.d.ts +123 -0
- package/node_modules/@pi-browser/protocol/dist/callers.js +58 -0
- package/node_modules/@pi-browser/protocol/dist/compose-tools.d.ts +30 -0
- package/node_modules/@pi-browser/protocol/dist/compose-tools.js +175 -0
- package/node_modules/@pi-browser/protocol/dist/contacts-tools.d.ts +23 -0
- package/node_modules/@pi-browser/protocol/dist/contacts-tools.js +63 -0
- package/node_modules/@pi-browser/protocol/dist/control-tools.d.ts +32 -0
- package/node_modules/@pi-browser/protocol/dist/control-tools.js +201 -0
- package/node_modules/@pi-browser/protocol/dist/errors.d.ts +63 -0
- package/node_modules/@pi-browser/protocol/dist/errors.js +109 -0
- package/node_modules/@pi-browser/protocol/dist/index.d.ts +17 -0
- package/node_modules/@pi-browser/protocol/dist/index.js +17 -0
- package/node_modules/@pi-browser/protocol/dist/integration.d.ts +414 -0
- package/node_modules/@pi-browser/protocol/dist/integration.js +270 -0
- package/node_modules/@pi-browser/protocol/dist/jsonrpc.d.ts +33 -0
- package/node_modules/@pi-browser/protocol/dist/jsonrpc.js +31 -0
- package/node_modules/@pi-browser/protocol/dist/mail-mutation-tools.d.ts +27 -0
- package/node_modules/@pi-browser/protocol/dist/mail-mutation-tools.js +88 -0
- package/node_modules/@pi-browser/protocol/dist/mail-tools.d.ts +133 -0
- package/node_modules/@pi-browser/protocol/dist/mail-tools.js +245 -0
- package/node_modules/@pi-browser/protocol/dist/permission.d.ts +113 -0
- package/node_modules/@pi-browser/protocol/dist/permission.js +257 -0
- package/node_modules/@pi-browser/protocol/dist/skills.d.ts +304 -0
- package/node_modules/@pi-browser/protocol/dist/skills.js +473 -0
- package/node_modules/@pi-browser/protocol/dist/tool-capabilities.d.ts +18 -0
- package/node_modules/@pi-browser/protocol/dist/tool-capabilities.js +52 -0
- package/node_modules/@pi-browser/protocol/dist/transport.d.ts +17 -0
- package/node_modules/@pi-browser/protocol/dist/transport.js +17 -0
- package/node_modules/@pi-browser/protocol/package.json +14 -0
- package/package.json +69 -4
- package/skills/browser-walk/SKILL.md +98 -0
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Native Messaging host installation (PRODUCT.md §9–11; THUNDERBIRD-PLAN.md
|
|
3
|
+
* §2, §23 — application-neutral `dev.pi.agent` host shared by Firefox and
|
|
4
|
+
* Thunderbird).
|
|
5
|
+
*
|
|
6
|
+
* One host implementation, per-app manifest locations:
|
|
7
|
+
*
|
|
8
|
+
* Linux: ~/.mozilla/native-messaging-hosts/dev.pi.agent.json
|
|
9
|
+
* (single file: both Firefox and Thunderbird read this directory)
|
|
10
|
+
* macOS: Firefox: ~/Library/Application Support/Mozilla/NativeMessagingHosts/dev.pi.agent.json
|
|
11
|
+
* Thunderbird: ~/Library/Mozilla/NativeMessagingHosts/dev.pi.agent.json
|
|
12
|
+
* Windows: manifest inside the package +
|
|
13
|
+
* HKCU\SOFTWARE\Mozilla\NativeMessagingHosts\dev.pi.agent
|
|
14
|
+
* (single key: both applications read the same registry location)
|
|
15
|
+
*
|
|
16
|
+
* No separately downloaded bridge: the launcher execs the bundled Node
|
|
17
|
+
* host (same JS implementation on every platform).
|
|
18
|
+
*/
|
|
19
|
+
import { PI_AGENT, PI_BROWSER } from "@pi-browser/protocol";
|
|
20
|
+
/** Application-neutral host name (manifest `name`). */
|
|
21
|
+
export const NATIVE_HOST_NAME = PI_AGENT.nativeHost;
|
|
22
|
+
/** Legacy Firefox host name; removed on uninstall for cleanup. */
|
|
23
|
+
export const LEGACY_NATIVE_HOST_NAME = PI_AGENT.legacyNativeHost;
|
|
24
|
+
/** All add-on IDs the host manifest authorizes (plan §2). */
|
|
25
|
+
export const ALLOWED_EXTENSIONS = [...PI_AGENT.authorizedExtensions];
|
|
26
|
+
export const AGENT_APPS = ["firefox", "thunderbird"];
|
|
27
|
+
export function buildManifest(launcherPath) {
|
|
28
|
+
return {
|
|
29
|
+
name: NATIVE_HOST_NAME,
|
|
30
|
+
description: "Pi Agent Desktop Integration",
|
|
31
|
+
path: launcherPath,
|
|
32
|
+
type: "stdio",
|
|
33
|
+
allowed_extensions: ALLOWED_EXTENSIONS,
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Per-app, per-platform manifest file paths. On Linux and Windows both apps
|
|
38
|
+
* share one location (plan §23); on macOS the two applications use
|
|
39
|
+
* different per-user directories.
|
|
40
|
+
*/
|
|
41
|
+
export function manifestPathsForApps(apps, homeDir, platform) {
|
|
42
|
+
const firefox = platform === "darwin"
|
|
43
|
+
? `${homeDir}/Library/Application Support/Mozilla/NativeMessagingHosts/${NATIVE_HOST_NAME}.json`
|
|
44
|
+
: `${homeDir}/.mozilla/native-messaging-hosts/${NATIVE_HOST_NAME}.json`;
|
|
45
|
+
const thunderbird = platform === "darwin"
|
|
46
|
+
? `${homeDir}/Library/Mozilla/NativeMessagingHosts/${NATIVE_HOST_NAME}.json`
|
|
47
|
+
: `${homeDir}/.mozilla/native-messaging-hosts/${NATIVE_HOST_NAME}.json`;
|
|
48
|
+
const paths = { firefox, thunderbird };
|
|
49
|
+
for (const app of apps)
|
|
50
|
+
if (!(app in paths))
|
|
51
|
+
throw new Error(`unknown app: ${app}`);
|
|
52
|
+
return paths;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Distinct manifest files to write for the requested apps (on shared
|
|
56
|
+
* platforms both apps resolve to one file). Each entry names the apps it
|
|
57
|
+
* serves so status can report per-app.
|
|
58
|
+
*/
|
|
59
|
+
export function distinctManifestLocations(apps, homeDir, platform) {
|
|
60
|
+
const perApp = manifestPathsForApps(apps, homeDir, platform);
|
|
61
|
+
const byPath = new Map();
|
|
62
|
+
for (const app of apps) {
|
|
63
|
+
const p = perApp[app];
|
|
64
|
+
byPath.set(p, [...(byPath.get(p) ?? []), app]);
|
|
65
|
+
}
|
|
66
|
+
return [...byPath.entries()].map(([path, apps]) => ({ path, apps }));
|
|
67
|
+
}
|
|
68
|
+
/** Legacy (dev.pi.browser) manifest path for the given app — cleanup only. */
|
|
69
|
+
export function legacyManifestPath(app, homeDir, platform) {
|
|
70
|
+
if (platform === "win32")
|
|
71
|
+
return undefined; // legacy Windows registration used the same shared key
|
|
72
|
+
const dir = app === "firefox" ? "Library/Application Support/Mozilla/NativeMessagingHosts" : "Library/Mozilla/NativeMessagingHosts";
|
|
73
|
+
if (platform === "darwin")
|
|
74
|
+
return `${homeDir}/${dir}/${LEGACY_NATIVE_HOST_NAME}.json`;
|
|
75
|
+
return `${homeDir}/.mozilla/native-messaging-hosts/${LEGACY_NATIVE_HOST_NAME}.json`;
|
|
76
|
+
}
|
|
77
|
+
/** Windows registry key (per-user; no admin required). Both apps share it. */
|
|
78
|
+
export const WINDOWS_REGISTRY_KEY = `SOFTWARE\\Mozilla\\NativeMessagingHosts\\${NATIVE_HOST_NAME}`;
|
|
79
|
+
/** Legacy Windows registry key (removed during uninstall cleanup). */
|
|
80
|
+
export const WINDOWS_LEGACY_REGISTRY_KEY = `SOFTWARE\\Mozilla\\NativeMessagingHosts\\${LEGACY_NATIVE_HOST_NAME}`;
|
|
81
|
+
export function shLauncherContent(nodePath, mainJs) {
|
|
82
|
+
return `#!/bin/sh\nexec "${nodePath}" "${mainJs}" "$@"\n`;
|
|
83
|
+
}
|
|
84
|
+
export function cmdLauncherContent(nodePath, mainJs) {
|
|
85
|
+
return `@echo off\n"${nodePath}" "${mainJs}" %*\n`;
|
|
86
|
+
}
|
|
87
|
+
/** Friendly load-instructions for an app's built add-on (status/install output). */
|
|
88
|
+
export function addonLoadHint(app) {
|
|
89
|
+
return app === "firefox"
|
|
90
|
+
? "load the Firefox add-on (firefox/dist/manifest.json, id " + PI_BROWSER.extensionId + "): Firefox → about:debugging#aboutThisFirefoxBrowser → “Load Temporary Add-on…” → pick firefox/dist/manifest.json"
|
|
91
|
+
: "load the Thunderbird add-on (thunderbird/dist/manifest.json, id pi-agent-thunderbird@matbee.com): Thunderbird → about:debugging#aboutThisThunderbird → “Load Temporary Add-on…” → pick thunderbird/dist/manifest.json";
|
|
92
|
+
}
|
|
93
|
+
//# sourceMappingURL=common.js.map
|
|
@@ -0,0 +1,366 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Installer command entry point used by /pi-browser (install | status |
|
|
3
|
+
* doctor | uninstall) and by tests.
|
|
4
|
+
*/
|
|
5
|
+
import { spawn } from "node:child_process";
|
|
6
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
7
|
+
import os from "node:os";
|
|
8
|
+
import path from "node:path";
|
|
9
|
+
import { fileURLToPath } from "node:url";
|
|
10
|
+
import { PI_AGENT, PI_AGENT_ID, PI_BROWSER_META } from "@pi-browser/protocol";
|
|
11
|
+
import { AgentRegistry } from "../acp/agents/registry.js";
|
|
12
|
+
import { ADDON_HEARTBEAT_FRESH_MS, readClientHeartbeat } from "../client-heartbeat.js";
|
|
13
|
+
import { brokerPaths, isPidAlive, readBrokerState } from "../native-host/broker-ipc.js";
|
|
14
|
+
import { addonLoadHint } from "./common.js";
|
|
15
|
+
import { runMcpClients } from "./mcp-clients.js";
|
|
16
|
+
import { detectTargets, defaultExec, installHost, statusHost, uninstallHost, } from "./platforms.js";
|
|
17
|
+
/** Expand a CLI target (firefox | thunderbird | mozilla) to app list. */
|
|
18
|
+
export function expandAppTarget(target) {
|
|
19
|
+
if (target === undefined || target === "mozilla")
|
|
20
|
+
return ["firefox", "thunderbird"];
|
|
21
|
+
return [target];
|
|
22
|
+
}
|
|
23
|
+
export function normalizePlatform(p) {
|
|
24
|
+
if (p === "macos")
|
|
25
|
+
return "darwin";
|
|
26
|
+
if (p === "windows")
|
|
27
|
+
return "win32";
|
|
28
|
+
if (p === "darwin" || p === "win32" || p === "linux")
|
|
29
|
+
return p;
|
|
30
|
+
return "linux";
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Why `pkgRoot` is a temporary location (npx cache or the OS temp dir), or
|
|
34
|
+
* undefined when it is stable. The native-host manifest and MCP entries point
|
|
35
|
+
* into the package, so registering from a cache that npm may delete would
|
|
36
|
+
* leave both apps and every MCP client pointing at nothing.
|
|
37
|
+
*/
|
|
38
|
+
export function temporaryRootReason(pkgRoot, tmpDir = os.tmpdir()) {
|
|
39
|
+
const parts = path.resolve(pkgRoot).split(/[\\/]+/);
|
|
40
|
+
if (parts.includes("_npx"))
|
|
41
|
+
return "it runs from the npx cache (_npx)";
|
|
42
|
+
const rel = path.relative(path.resolve(tmpDir), path.resolve(pkgRoot));
|
|
43
|
+
if (rel === "" || (!rel.startsWith("..") && !path.isAbsolute(rel)))
|
|
44
|
+
return `it is under the temporary directory ${tmpDir}`;
|
|
45
|
+
return undefined;
|
|
46
|
+
}
|
|
47
|
+
/** The refusal shown for a temporary package root. */
|
|
48
|
+
export function temporaryRootRefusal(pkgRoot, reason) {
|
|
49
|
+
return {
|
|
50
|
+
ok: false,
|
|
51
|
+
lines: [
|
|
52
|
+
`refusing to register ${pkgRoot}: ${reason}, which can be deleted at any time.`,
|
|
53
|
+
"install the package globally first, then run it from there:",
|
|
54
|
+
" npm i -g trusted-user-agent",
|
|
55
|
+
" trusted-ua install",
|
|
56
|
+
],
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
export function defaultPkgRoot() {
|
|
60
|
+
// When loaded from dist, the package root is two levels up (dist/installer -> package).
|
|
61
|
+
// From src (jiti), also two levels up (src/installer -> package).
|
|
62
|
+
return path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..", "..");
|
|
63
|
+
}
|
|
64
|
+
export function buildEnv(ctx = {}) {
|
|
65
|
+
const pkgRoot = ctx.pkgRoot ?? defaultPkgRoot();
|
|
66
|
+
const platform = normalizePlatform(ctx.platform ?? process.platform);
|
|
67
|
+
const env = {
|
|
68
|
+
homeDir: ctx.homeDir ?? os.homedir(),
|
|
69
|
+
exec: ctx.exec ?? defaultExec(),
|
|
70
|
+
platform,
|
|
71
|
+
};
|
|
72
|
+
const targets = detectTargets(platform);
|
|
73
|
+
const mainJs = ctx.mainJs ?? path.join(pkgRoot, "dist", "native-host", "main.js");
|
|
74
|
+
return { env, targets, pkgRoot, mainJs };
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Add-on side of onboarding (auto-detection). The host records a heartbeat
|
|
78
|
+
* when the add-on connects and on every keepalive ping; a fresh heartbeat
|
|
79
|
+
* means the add-on is loaded and connected.
|
|
80
|
+
*/
|
|
81
|
+
function addonLines(homeDir, apps) {
|
|
82
|
+
const buildSteps = apps
|
|
83
|
+
.map((a) => (a === "firefox" ? "npm run build -w @pi-browser/firefox → firefox/dist/" : "npm run build -w @pi-browser/thunderbird → thunderbird/dist/"))
|
|
84
|
+
.join(";");
|
|
85
|
+
const loadSteps = apps.map((a, i) => ` ${i + 1}. ${a === "firefox" ? "Firefox" : "Thunderbird"}: ${a === "firefox" ? "about:debugging#aboutThisFirefoxBrowser" : "about:debugging#aboutThisThunderbird"} → “Load Temporary Add-on…” → pick ${a}/dist/manifest.json`).join("\n");
|
|
86
|
+
// Per-app heartbeats: each application reports independently so the
|
|
87
|
+
// cross-app (broker) setup is visible (plan §23).
|
|
88
|
+
const perApp = apps.map((app) => readClientHeartbeat(homeDir, app));
|
|
89
|
+
const anyDetected = perApp.some((hb) => hb !== undefined);
|
|
90
|
+
if (!anyDetected) {
|
|
91
|
+
return {
|
|
92
|
+
lines: [
|
|
93
|
+
"add-on: not detected — to finish setup:",
|
|
94
|
+
` 1. build the add-on(s) (from source): ${buildSteps}`,
|
|
95
|
+
loadSteps,
|
|
96
|
+
" 3. wait ~10s — the add-on auto-connects (no reload needed); run /pi-browser status again",
|
|
97
|
+
],
|
|
98
|
+
detected: false,
|
|
99
|
+
fresh: false,
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
const lines = apps.map((app, i) => {
|
|
103
|
+
const hb = perApp[i];
|
|
104
|
+
if (!hb)
|
|
105
|
+
return `add-on ${app}: not detected (load the add-on in ${app === "firefox" ? "Firefox" : "Thunderbird"}; see steps above)`;
|
|
106
|
+
const ageS = Math.round(hb.ageMs / 1000);
|
|
107
|
+
if (hb.ageMs <= ADDON_HEARTBEAT_FRESH_MS)
|
|
108
|
+
return `add-on ${app}: detected (${hb.client}, heartbeat ${ageS}s ago)`;
|
|
109
|
+
return `add-on ${app}: last heartbeat ${ageS}s ago (stale — the add-on may be disconnected or awaiting reload)`;
|
|
110
|
+
});
|
|
111
|
+
const allFresh = perApp.every((hb) => hb && hb.ageMs <= ADDON_HEARTBEAT_FRESH_MS);
|
|
112
|
+
return { lines, detected: true, fresh: allFresh };
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* External ACP agents for sidebar sessions (openspec sidebar-acp-agents):
|
|
116
|
+
* which are available, why the others aren't, and agents.json problems.
|
|
117
|
+
*/
|
|
118
|
+
export function agentLines(homeDir, processEnv) {
|
|
119
|
+
try {
|
|
120
|
+
const root = processEnv.PI_BROWSER_DATA_ROOT?.trim() || path.join(homeDir, ".pi", "browser");
|
|
121
|
+
const file = processEnv.PI_BROWSER_AGENTS_FILE?.trim() || path.join(root, "agents.json");
|
|
122
|
+
const res = new AgentRegistry({ env: processEnv, file }).list();
|
|
123
|
+
const lines = res.agents
|
|
124
|
+
.filter((a) => a.id !== PI_AGENT_ID)
|
|
125
|
+
.map((a) => `agent ${a.id}: ${a.available ? "available" : (a.reason ?? "not available")}`);
|
|
126
|
+
if (res.configError)
|
|
127
|
+
lines.push(`agents.json: ${res.configError}`);
|
|
128
|
+
return lines;
|
|
129
|
+
}
|
|
130
|
+
catch (err) {
|
|
131
|
+
return [`agents: unknown (${err instanceof Error ? err.message : String(err)})`];
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
/** Broker (cross-app) state, for status/doctor (plan §26–27). */
|
|
135
|
+
function brokerLines() {
|
|
136
|
+
try {
|
|
137
|
+
const paths = brokerPaths();
|
|
138
|
+
const state = readBrokerState(paths.state);
|
|
139
|
+
if (state && isPidAlive(state.pid)) {
|
|
140
|
+
const ageS = Math.round((Date.now() - state.startedAt) / 1000);
|
|
141
|
+
return [`broker: running (pid ${state.pid}, up ${ageS}s) — cross-app tool routing active`];
|
|
142
|
+
}
|
|
143
|
+
if (state)
|
|
144
|
+
return ["broker: stale state file (previous broker exited)"];
|
|
145
|
+
return ["broker: not running (single-app mode; a second app will attach automatically)"];
|
|
146
|
+
}
|
|
147
|
+
catch {
|
|
148
|
+
return ["broker: unknown state"];
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
export async function runCommand(command, ctx = {}, sub = "install") {
|
|
152
|
+
const { env, targets, pkgRoot, mainJs } = buildEnv(ctx);
|
|
153
|
+
const lines = [];
|
|
154
|
+
const note = (l) => {
|
|
155
|
+
lines.push(l);
|
|
156
|
+
ctx.log?.(l);
|
|
157
|
+
};
|
|
158
|
+
note(`platform: ${env.platform} (targets: ${targets.id})`);
|
|
159
|
+
const apps = expandAppTarget(ctx.apps);
|
|
160
|
+
note(`apps: ${apps.join(", ")}`);
|
|
161
|
+
// Registering writes absolute paths into the package: refuse a root that
|
|
162
|
+
// npm or the OS may delete (npx cache, temp dir).
|
|
163
|
+
const registers = command === "install" || (command === "mcp" && sub === "install");
|
|
164
|
+
const allowTemporary = ctx.allowTemporary || process.env.PI_BROWSER_ALLOW_TEMPORARY_ROOT === "1";
|
|
165
|
+
const tempReason = registers && !allowTemporary ? temporaryRootReason(pkgRoot) : undefined;
|
|
166
|
+
if (tempReason) {
|
|
167
|
+
const refusal = temporaryRootRefusal(pkgRoot, tempReason);
|
|
168
|
+
for (const l of refusal.lines)
|
|
169
|
+
note(l);
|
|
170
|
+
return { ok: false, lines };
|
|
171
|
+
}
|
|
172
|
+
switch (command) {
|
|
173
|
+
case "install": {
|
|
174
|
+
if (!existsSync(mainJs)) {
|
|
175
|
+
throw new Error(`host entrypoint missing: ${mainJs} (run the package build first)`);
|
|
176
|
+
}
|
|
177
|
+
const report = await installHost({ env, pkgRoot, mainJs, apps }, targets);
|
|
178
|
+
for (const l of report.lines)
|
|
179
|
+
note(l);
|
|
180
|
+
note(`installed ${PI_AGENT.nativeHost} (integration v${PI_BROWSER_META.version}, protocol v${PI_BROWSER_META.protocolVersion})`);
|
|
181
|
+
for (const app of apps)
|
|
182
|
+
note(`next: ${addonLoadHint(app)}`);
|
|
183
|
+
note("if the add-on is already loaded, no reload is needed — it auto-detects the host within ~10s");
|
|
184
|
+
return { ok: true, lines };
|
|
185
|
+
}
|
|
186
|
+
case "status": {
|
|
187
|
+
const report = await statusHost(env, pkgRoot, targets, apps);
|
|
188
|
+
for (const l of report.lines)
|
|
189
|
+
note(l);
|
|
190
|
+
if (report.installed) {
|
|
191
|
+
const addon = addonLines(env.homeDir, apps);
|
|
192
|
+
for (const l of addon.lines)
|
|
193
|
+
note(l);
|
|
194
|
+
for (const l of brokerLines())
|
|
195
|
+
note(l);
|
|
196
|
+
for (const l of agentLines(env.homeDir, ctx.processEnv ?? process.env))
|
|
197
|
+
note(l);
|
|
198
|
+
if (addon.fresh)
|
|
199
|
+
note("status: OK (host + add-on connected)");
|
|
200
|
+
else
|
|
201
|
+
note(addon.detected ? "status: HOST OK — add-on heartbeat stale (reload the add-on or check the app)" : "status: HOST OK — add-on not detected yet (see steps above)");
|
|
202
|
+
}
|
|
203
|
+
else {
|
|
204
|
+
for (const issue of report.issues)
|
|
205
|
+
note(`issue: ${issue}`);
|
|
206
|
+
note(`status: PROBLEMS FOUND (run /pi-browser install ${apps.length === 2 ? "mozilla" : apps[0]} to repair)`);
|
|
207
|
+
}
|
|
208
|
+
return { ok: report.installed, lines };
|
|
209
|
+
}
|
|
210
|
+
case "doctor": {
|
|
211
|
+
const status = await statusHost(env, pkgRoot, targets, apps);
|
|
212
|
+
for (const l of status.lines)
|
|
213
|
+
note(l);
|
|
214
|
+
if (!status.installed) {
|
|
215
|
+
for (const issue of status.issues)
|
|
216
|
+
note(`issue: ${issue}`);
|
|
217
|
+
note(`doctor: NOT INSTALLED — run /pi-browser install ${apps.length === 2 ? "mozilla" : apps[0]}`);
|
|
218
|
+
return { ok: false, lines };
|
|
219
|
+
}
|
|
220
|
+
const probe = await probeHost(env, status);
|
|
221
|
+
for (const l of probe.lines)
|
|
222
|
+
note(l);
|
|
223
|
+
if (!probe.ok) {
|
|
224
|
+
note("doctor: HOST PROBE FAILED");
|
|
225
|
+
return { ok: false, lines };
|
|
226
|
+
}
|
|
227
|
+
const addon = addonLines(env.homeDir, apps);
|
|
228
|
+
for (const l of addon.lines)
|
|
229
|
+
note(l);
|
|
230
|
+
for (const l of brokerLines())
|
|
231
|
+
note(l);
|
|
232
|
+
for (const l of agentLines(env.homeDir, ctx.processEnv ?? process.env))
|
|
233
|
+
note(l);
|
|
234
|
+
if (addon.fresh)
|
|
235
|
+
note("doctor: OK (host + add-on connected)");
|
|
236
|
+
else
|
|
237
|
+
note(addon.detected ? "doctor: HOST OK — add-on heartbeat stale (reload the add-on or check the app)" : "doctor: HOST OK — next: load the add-on (see steps above; it auto-connects within ~10s)");
|
|
238
|
+
return { ok: true, lines };
|
|
239
|
+
}
|
|
240
|
+
case "uninstall": {
|
|
241
|
+
const removed = await uninstallHost(env, pkgRoot, targets, apps);
|
|
242
|
+
for (const l of removed)
|
|
243
|
+
note(l);
|
|
244
|
+
note(`uninstalled ${PI_AGENT.nativeHost} (package files kept; pi remove to drop the package)`);
|
|
245
|
+
return { ok: true, lines };
|
|
246
|
+
}
|
|
247
|
+
case "mcp": {
|
|
248
|
+
const result = await runMcpClients(sub, ctx.mcpClient ?? "pi", {
|
|
249
|
+
homeDir: env.homeDir,
|
|
250
|
+
platform: env.platform === "darwin" || env.platform === "win32" ? env.platform : "linux",
|
|
251
|
+
exec: env.exec,
|
|
252
|
+
env: ctx.processEnv ?? process.env,
|
|
253
|
+
pkgRoot,
|
|
254
|
+
});
|
|
255
|
+
for (const l of result.lines)
|
|
256
|
+
note(l);
|
|
257
|
+
return result;
|
|
258
|
+
}
|
|
259
|
+
default:
|
|
260
|
+
throw new Error(`unknown command: ${command}`);
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
/**
|
|
264
|
+
* Spawn the installed launcher and exchange one framed x-pi-browser/ping.
|
|
265
|
+
* This verifies: manifest path is executable, the host boots, and framing
|
|
266
|
+
* + JSON-RPC work end-to-end.
|
|
267
|
+
*/
|
|
268
|
+
export async function probeHost(env, status, timeoutMs = 20_000) {
|
|
269
|
+
const lines = [];
|
|
270
|
+
const launcher = status.launcherPath;
|
|
271
|
+
if (!launcher || !existsSync(launcher)) {
|
|
272
|
+
lines.push(`probe: launcher missing (${launcher ?? "unknown"})`);
|
|
273
|
+
return { ok: false, lines };
|
|
274
|
+
}
|
|
275
|
+
return new Promise((resolve) => {
|
|
276
|
+
let settled = false;
|
|
277
|
+
const finish = (ok) => {
|
|
278
|
+
if (settled)
|
|
279
|
+
return;
|
|
280
|
+
settled = true;
|
|
281
|
+
clearTimeout(timer);
|
|
282
|
+
try {
|
|
283
|
+
child.kill("SIGTERM");
|
|
284
|
+
}
|
|
285
|
+
catch {
|
|
286
|
+
// already gone
|
|
287
|
+
}
|
|
288
|
+
resolve({ ok, lines });
|
|
289
|
+
};
|
|
290
|
+
// Node refuses to spawn .cmd/.bat directly (EINVAL since CVE-2024-27980);
|
|
291
|
+
// run it through cmd.exe exactly like Firefox launches a .cmd host.
|
|
292
|
+
const child = process.platform === "win32" && /\.(cmd|bat)$/i.test(launcher)
|
|
293
|
+
? spawn(process.env.ComSpec ?? "cmd.exe", ["/d", "/s", "/c", `"${launcher}"`], {
|
|
294
|
+
stdio: ["pipe", "pipe", "pipe"],
|
|
295
|
+
windowsVerbatimArguments: true,
|
|
296
|
+
})
|
|
297
|
+
: spawn(launcher, [], { stdio: ["pipe", "pipe", "pipe"] });
|
|
298
|
+
const timer = setTimeout(() => {
|
|
299
|
+
lines.push("probe: timed out waiting for x-pi-browser/ping");
|
|
300
|
+
finish(false);
|
|
301
|
+
}, timeoutMs);
|
|
302
|
+
let stderrTail = "";
|
|
303
|
+
let buffer = Buffer.alloc(0);
|
|
304
|
+
let gotPong = false;
|
|
305
|
+
const send = (obj) => {
|
|
306
|
+
const payload = Buffer.from(JSON.stringify(obj), "utf8");
|
|
307
|
+
const header = Buffer.alloc(4);
|
|
308
|
+
header.writeUInt32LE(payload.length, 0);
|
|
309
|
+
child.stdin.write(Buffer.concat([header, payload]));
|
|
310
|
+
};
|
|
311
|
+
child.stdout.on("data", (chunk) => {
|
|
312
|
+
buffer = Buffer.concat([buffer, chunk]);
|
|
313
|
+
while (buffer.length >= 4) {
|
|
314
|
+
const len = buffer.readUInt32LE(0);
|
|
315
|
+
if (buffer.length < 4 + len)
|
|
316
|
+
break;
|
|
317
|
+
const msgText = buffer.subarray(4, 4 + len).toString("utf8");
|
|
318
|
+
buffer = buffer.subarray(4 + len);
|
|
319
|
+
try {
|
|
320
|
+
const msg = JSON.parse(msgText);
|
|
321
|
+
if (msg.id === 1 && msg.result?.pong === true) {
|
|
322
|
+
gotPong = true;
|
|
323
|
+
const meta = msg.result.meta;
|
|
324
|
+
lines.push(`probe: pong (integration v${meta?.version ?? "?"}, protocol v${meta?.protocolVersion ?? "?"})`);
|
|
325
|
+
finish(true);
|
|
326
|
+
return;
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
catch {
|
|
330
|
+
// ignore unparseable frames during probe
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
});
|
|
334
|
+
child.stderr.on("data", (chunk) => {
|
|
335
|
+
stderrTail = (stderrTail + chunk.toString("utf8")).slice(-2000);
|
|
336
|
+
});
|
|
337
|
+
child.on("error", (err) => {
|
|
338
|
+
lines.push(`probe: failed to spawn: ${err.message}`);
|
|
339
|
+
finish(false);
|
|
340
|
+
});
|
|
341
|
+
child.on("close", (code) => {
|
|
342
|
+
if (!gotPong) {
|
|
343
|
+
lines.push(`probe: host exited early (code ${code})`);
|
|
344
|
+
if (stderrTail)
|
|
345
|
+
lines.push(`probe stderr: ${stderrTail.trim().split("\n").slice(-3).join(" | ")}`);
|
|
346
|
+
finish(false);
|
|
347
|
+
}
|
|
348
|
+
});
|
|
349
|
+
// Give the process a moment to boot, then ping.
|
|
350
|
+
setTimeout(() => {
|
|
351
|
+
send({ jsonrpc: "2.0", id: 1, method: "x-pi-browser/ping", params: { clientVersion: PI_BROWSER_META.version } });
|
|
352
|
+
}, 1500);
|
|
353
|
+
});
|
|
354
|
+
}
|
|
355
|
+
export { PI_BROWSER_META };
|
|
356
|
+
/** Read the package version (for version-mismatch diagnostics). */
|
|
357
|
+
export function packageVersion(pkgRoot) {
|
|
358
|
+
try {
|
|
359
|
+
const pkg = JSON.parse(readFileSync(path.join(pkgRoot, "package.json"), "utf8"));
|
|
360
|
+
return pkg.version ?? "0.0.0";
|
|
361
|
+
}
|
|
362
|
+
catch {
|
|
363
|
+
return "0.0.0";
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
//# sourceMappingURL=index.js.map
|