u-foo 4.0.3 → 4.0.10
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/README.md +239 -19
- package/README.zh-CN.md +3 -3
- package/bin/ucode.js +10 -2
- package/bin/ufoo.js +3 -0
- package/dist/tui/darwin-arm64/ufoo-tui +0 -0
- package/dist/tui/darwin-x64/ufoo-tui +0 -0
- package/dist/tui/linux-arm64/ufoo-tui +0 -0
- package/dist/tui/linux-x64/ufoo-tui +0 -0
- package/{OBSERVABILITY.md → docs/OBSERVABILITY.md} +3 -3
- package/docs/README.md +27 -0
- package/extensions/ufoo-browser/LICENSE +152 -0
- package/extensions/ufoo-browser/NOTICE +10 -0
- package/extensions/ufoo-browser/README.md +30 -0
- package/extensions/ufoo-browser/background.js +409 -0
- package/extensions/ufoo-browser/icons/icon-128.png +0 -0
- package/extensions/ufoo-browser/icons/icon-16.png +0 -0
- package/extensions/ufoo-browser/icons/icon-256.png +0 -0
- package/extensions/ufoo-browser/icons/icon-32.png +0 -0
- package/extensions/ufoo-browser/icons/icon-48.png +0 -0
- package/extensions/ufoo-browser/icons/logo.png +0 -0
- package/extensions/ufoo-browser/icons/toolbar-16.png +0 -0
- package/extensions/ufoo-browser/icons/toolbar-32.png +0 -0
- package/extensions/ufoo-browser/icons/toolbar-logo.png +0 -0
- package/extensions/ufoo-browser/manifest.json +30 -0
- package/extensions/ufoo-browser/popup.css +71 -0
- package/extensions/ufoo-browser/popup.html +44 -0
- package/extensions/ufoo-browser/popup.js +153 -0
- package/package.json +7 -5
- package/scripts/postinstall.js +6 -0
- package/src/agents/capabilities/browser/index.js +49 -0
- package/src/agents/capabilities/coding/backgroundHooks.js +94 -0
- package/src/agents/capabilities/coding/index.js +30 -1
- package/src/agents/capabilities/index.js +5 -2
- package/src/agents/capabilities/planning/index.js +44 -5
- package/src/agents/capabilities/turnRecall/index.js +341 -0
- package/src/agents/controller/controllerToolExecutor.js +2 -2
- package/src/agents/internal/internalRunner.js +30 -8
- package/src/agents/internal/taskControl.js +107 -0
- package/src/agents/profiles/coding.js +1 -1
- package/src/agents/profiles/main.js +1 -1
- package/src/agents/prompts/mainAgent.js +1 -0
- package/src/agents/prompts/native/toolDescriptions/bash.js +7 -1
- package/src/agents/providers/claudeSessionFiles.js +1 -1
- package/src/agents/providers/codexThreadProvider.js +6 -1
- package/src/agents/providers/credentials/agy.js +171 -0
- package/src/agents/providers/directAuthStatus.js +20 -2
- package/src/agents/providers/modelsCatalog.js +26 -5
- package/src/agents/providers/nativeTransport.js +145 -73
- package/src/agents/providers/providerId.js +17 -0
- package/src/agents/providers/runtimeConfig.js +67 -20
- package/src/agents/providers/transports/antigravityTransport.js +242 -0
- package/src/agents/providers/transports/index.js +1 -0
- package/src/agents/providers/transports/openaiChatTransport.js +3 -2
- package/src/agents/providers/transports/openaiResponsesTransport.js +2 -1
- package/src/agents/providers/transports/responsesProtocol.js +119 -18
- package/src/agents/providers/transports/transportContract.js +1 -0
- package/src/agents/providers/upstreamRuntime.js +3 -1
- package/src/agents/runtime/context/contextBudget.js +1 -19
- package/src/agents/runtime/context/contextWindow.js +5 -0
- package/src/agents/runtime/context/replayHistory.js +16 -10
- package/src/agents/runtime/context/runtimeStore.js +0 -1
- package/src/agents/runtime/context/sessionJournal.js +1 -1
- package/src/agents/runtime/context/slidingWindowManager.js +792 -0
- package/src/agents/runtime/context/topicSelector.js +338 -0
- package/src/agents/runtime/context/turnAnalyzer.js +767 -0
- package/src/agents/runtime/context/turnIndexStore.js +515 -0
- package/src/agents/runtime/core/agentLoop.js +140 -35
- package/src/agents/runtime/core/executionBudget.js +2 -1
- package/src/agents/runtime/core/toolErrorGuard.js +39 -0
- package/src/agents/runtime/createAgentRuntime.js +80 -7
- package/src/agents/runtime/index.js +5 -0
- package/src/agents/runtime/protocol/materialize.js +1 -1
- package/src/agents/runtime/tasks/hooks.js +137 -0
- package/src/app/chat/ChatController.js +8 -4
- package/src/app/chat/daemonMessageRouter.js +3 -1
- package/src/app/chat/inputSubmitHandler.js +2 -2
- package/src/app/chat/transport.js +1 -1
- package/src/app/cli/browserCommands.js +340 -0
- package/src/app/cli/busCoreCommands.js +63 -3
- package/src/app/cli/features/doctor.js +21 -0
- package/src/app/cli/metricsCommands.js +1 -1
- package/src/app/cli/run.js +81 -1
- package/src/app/cli/setupCommands.js +200 -0
- package/src/code/UcodeController.js +31 -1
- package/src/code/agent.js +76 -25
- package/src/code/busConsumer.js +13 -12
- package/src/code/commands.js +0 -1
- package/src/code/context/assembler.js +11 -88
- package/src/code/context/planGraphService.js +121 -6
- package/src/code/context/planProjection.js +10 -2
- package/src/code/context/reducers.js +172 -4
- package/src/code/context/stateCommit.js +0 -32
- package/src/code/context/transcript.js +7 -4
- package/src/code/internalThread.js +9 -1
- package/src/code/launcher/ucode.js +4 -2
- package/src/code/modelCommand.js +6 -5
- package/src/code/nativeRunner.js +232 -62
- package/src/code/repl.js +27 -1
- package/src/code/runtime/agentWakeup.js +20 -3
- package/src/code/runtime/backgroundWakeHost.js +45 -0
- package/src/code/runtime/taskFocusContext.js +7 -0
- package/src/code/runtime/taskLoop.js +24 -1
- package/src/code/sessionStore.js +14 -10
- package/src/code/taskDecomposer.js +16 -7
- package/src/code/tools/backgroundProcess.js +23 -0
- package/src/code/tools/bash.js +764 -12
- package/src/code/tools/executor.js +13 -4
- package/src/code/tools/planGraph.js +1 -0
- package/src/code/tools/specs.js +62 -4
- package/src/code/tools/taskRun.js +38 -0
- package/src/config.js +92 -6
- package/src/coordination/bus/index.js +2 -2
- package/src/coordination/bus/message.js +10 -1
- package/src/coordination/bus/nickname.js +3 -1
- package/src/coordination/bus/subscriber.js +25 -10
- package/src/coordination/bus/utils.js +4 -0
- package/src/coordination/bus/wakeEndpoint.js +198 -0
- package/src/coordination/history/agentPerformance.js +18 -18
- package/src/coordination/history/agentTrace.js +7 -3
- package/src/coordination/state/paths.js +4 -0
- package/src/coordination/state/workspaceAccess.js +3 -1
- package/src/orchestration/groups/modelConfig.js +66 -0
- package/src/orchestration/groups/validateTemplate.js +2 -0
- package/src/runtime/browser/broker.js +1027 -0
- package/src/runtime/browser/brokerClient.js +108 -0
- package/src/runtime/browser/componentInstaller.js +275 -0
- package/src/runtime/browser/installState.js +329 -0
- package/src/runtime/browser/manifest.js +145 -0
- package/src/runtime/browser/native-host-runner.js +198 -0
- package/src/runtime/browser/nativeHost.js +213 -0
- package/src/runtime/browser/nativeMessagingProtocol.js +50 -0
- package/src/runtime/browser/paths.js +134 -0
- package/src/runtime/browser/playwrightSession.js +614 -0
- package/src/runtime/daemon/agentHost.js +175 -28
- package/src/runtime/daemon/busBridge.js +2 -1
- package/src/runtime/daemon/deliveryScheduler.js +70 -2
- package/src/runtime/daemon/globalDaemon.js +20 -0
- package/src/runtime/daemon/groupOrchestrator.js +20 -1
- package/src/runtime/daemon/index.js +6 -4
- package/src/runtime/daemon/ops.js +14 -8
- package/src/tools/handlers/listAgents.js +1 -1
- package/src/tools/handlers/readBusSummary.js +1 -1
- package/src/tools/handlers/routeAgent.js +122 -5
- package/src/tools/schemaFixtures.js +1 -1
- package/src/ui/agentSurface.js +60 -6
- package/src/ui/format/index.js +34 -2
- package/src/ui/format/markdownRenderer.js +4 -2
- package/src/ui/rustChatHost.js +22 -7
- package/src/ui/rustMultiSession.js +14 -2
- package/src/ui/rustUcodeHost.js +249 -80
- package/src/ui/ucodeAgentSession.js +88 -0
- package/templates/groups/build-lane.json +57 -3
- package/templates/groups/build-ultra.json +114 -6
- package/templates/groups/design-system.json +57 -3
- package/templates/groups/product-discovery.json +57 -3
- package/templates/groups/ui-plan-review.json +57 -3
- package/templates/groups/ui-polish.json +57 -3
- package/templates/groups/verify-ship.json +57 -3
- package/src/agents/runtime/context/checkpointStore.js +0 -38
- package/src/agents/runtime/context/compaction.js +0 -151
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const { spawn } = require("child_process");
|
|
4
|
+
const fs = require("fs");
|
|
5
|
+
const net = require("net");
|
|
6
|
+
const path = require("path");
|
|
7
|
+
const lockfile = require("proper-lockfile");
|
|
8
|
+
const { resolveNodeExecutable } = require("../process/nodeExecutable");
|
|
9
|
+
const {
|
|
10
|
+
brokerSocketPath,
|
|
11
|
+
brokerLockPath,
|
|
12
|
+
privateDir,
|
|
13
|
+
} = require("./paths");
|
|
14
|
+
|
|
15
|
+
function requestBroker(op, params = {}, { env = process.env, timeoutMs = 10000 } = {}) {
|
|
16
|
+
const sockPath = brokerSocketPath(env);
|
|
17
|
+
if (!fs.existsSync(sockPath)) {
|
|
18
|
+
return Promise.resolve({ ok: false, code: "broker_not_running", error: "Browser broker is not running" });
|
|
19
|
+
}
|
|
20
|
+
return new Promise((resolve) => {
|
|
21
|
+
const socket = net.createConnection(sockPath);
|
|
22
|
+
let buffer = "";
|
|
23
|
+
let settled = false;
|
|
24
|
+
const finish = (value) => {
|
|
25
|
+
if (settled) return;
|
|
26
|
+
settled = true;
|
|
27
|
+
clearTimeout(timer);
|
|
28
|
+
try { socket.destroy(); } catch { /* ignore */ }
|
|
29
|
+
resolve(value);
|
|
30
|
+
};
|
|
31
|
+
const timer = setTimeout(() => {
|
|
32
|
+
finish({ ok: false, code: "broker_timeout", error: `Browser broker request timed out (${op})` });
|
|
33
|
+
}, timeoutMs);
|
|
34
|
+
timer.unref?.();
|
|
35
|
+
socket.on("connect", () => {
|
|
36
|
+
socket.write(`${JSON.stringify({ op, params })}\n`);
|
|
37
|
+
});
|
|
38
|
+
socket.on("data", (chunk) => {
|
|
39
|
+
buffer += chunk.toString("utf8");
|
|
40
|
+
const idx = buffer.indexOf("\n");
|
|
41
|
+
if (idx === -1) return;
|
|
42
|
+
const line = buffer.slice(0, idx).trim();
|
|
43
|
+
try {
|
|
44
|
+
finish(JSON.parse(line));
|
|
45
|
+
} catch {
|
|
46
|
+
finish({ ok: false, code: "broker_invalid_response", error: "Malformed broker response" });
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
socket.on("error", (err) => {
|
|
50
|
+
finish({ ok: false, code: "broker_unreachable", error: err?.message || "Browser broker unreachable" });
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
async function pingBroker({ env = process.env } = {}) {
|
|
56
|
+
const res = await requestBroker("ping", {}, { env, timeoutMs: 1500 });
|
|
57
|
+
return Boolean(res && res.ok);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Ensure the user-scoped browser broker is running, starting a detached user-level
|
|
62
|
+
* broker process on demand if neither GlobalDaemon nor a standalone broker is up.
|
|
63
|
+
*/
|
|
64
|
+
async function ensureBrokerRunning({ env = process.env, waitTimeoutMs = 6000 } = {}) {
|
|
65
|
+
if (await pingBroker({ env })) return { ok: true, started: false };
|
|
66
|
+
fs.mkdirSync(privateDir(env), { recursive: true, mode: 0o700 });
|
|
67
|
+
const lockPath = brokerLockPath(env);
|
|
68
|
+
let release;
|
|
69
|
+
try {
|
|
70
|
+
release = await lockfile.lock(lockPath, { realpath: false, stale: 15000, retries: { retries: 5, minTimeout: 100, maxTimeout: 300 } });
|
|
71
|
+
} catch {
|
|
72
|
+
// Another process may be starting it; fall through to wait loop.
|
|
73
|
+
}
|
|
74
|
+
try {
|
|
75
|
+
if (await pingBroker({ env })) return { ok: true, started: false };
|
|
76
|
+
const cliEntry = path.resolve(__dirname, "..", "..", "..", "bin", "ufoo.js");
|
|
77
|
+
const child = spawn(resolveNodeExecutable(), [cliEntry, "browser", "broker-serve"], {
|
|
78
|
+
detached: true,
|
|
79
|
+
stdio: "ignore",
|
|
80
|
+
env: { ...process.env, ...env, UFOO_BROWSER_BROKER_CHILD: "1" },
|
|
81
|
+
});
|
|
82
|
+
child.unref();
|
|
83
|
+
|
|
84
|
+
const deadline = Date.now() + waitTimeoutMs;
|
|
85
|
+
while (Date.now() < deadline) {
|
|
86
|
+
// eslint-disable-next-line no-await-in-loop
|
|
87
|
+
if (await pingBroker({ env })) return { ok: true, started: true };
|
|
88
|
+
// eslint-disable-next-line no-await-in-loop
|
|
89
|
+
await new Promise((r) => setTimeout(r, 120));
|
|
90
|
+
}
|
|
91
|
+
return { ok: false, code: "broker_start_failed", error: "Timed out waiting for user browser broker to start" };
|
|
92
|
+
} finally {
|
|
93
|
+
if (release) await release().catch(() => {});
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
async function notifyBrokerConfigChanged({ env = process.env } = {}) {
|
|
98
|
+
if (!(await pingBroker({ env }))) return { ok: false, notified: false };
|
|
99
|
+
const res = await requestBroker("config_changed", {}, { env });
|
|
100
|
+
return { ok: Boolean(res && res.ok), notified: true, ...res };
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
module.exports = {
|
|
104
|
+
requestBroker,
|
|
105
|
+
pingBroker,
|
|
106
|
+
ensureBrokerRunning,
|
|
107
|
+
notifyBrokerConfigChanged,
|
|
108
|
+
};
|
|
@@ -0,0 +1,275 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const crypto = require("crypto");
|
|
4
|
+
const fs = require("fs");
|
|
5
|
+
const path = require("path");
|
|
6
|
+
const { spawnSync } = require("child_process");
|
|
7
|
+
const { randomUUID } = require("crypto");
|
|
8
|
+
const { resolveNodeExecutable } = require("../process/nodeExecutable");
|
|
9
|
+
const { getReleaseManifest } = require("./manifest");
|
|
10
|
+
const {
|
|
11
|
+
browserHomeDir,
|
|
12
|
+
runtimeVersionsDir,
|
|
13
|
+
activeRuntimePath,
|
|
14
|
+
setupLockPath,
|
|
15
|
+
} = require("./paths");
|
|
16
|
+
const {
|
|
17
|
+
readActiveRuntime,
|
|
18
|
+
writeActiveRuntimeAtomic,
|
|
19
|
+
readInstallState,
|
|
20
|
+
writeInstallState,
|
|
21
|
+
listLeasedRuntimeVersions,
|
|
22
|
+
} = require("./installState");
|
|
23
|
+
const lockfile = require("proper-lockfile");
|
|
24
|
+
|
|
25
|
+
function computeFileSha256(filePath) {
|
|
26
|
+
const hash = crypto.createHash("sha256");
|
|
27
|
+
const data = fs.readFileSync(filePath);
|
|
28
|
+
hash.update(data);
|
|
29
|
+
return hash.digest("hex").toLowerCase();
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function resolveNpmCommand(nodeExec = resolveNodeExecutable()) {
|
|
33
|
+
const npmBesideNode = path.join(path.dirname(nodeExec), process.platform === "win32" ? "npm.cmd" : "npm");
|
|
34
|
+
if (fs.existsSync(npmBesideNode)) return npmBesideNode;
|
|
35
|
+
return process.platform === "win32" ? "npm.cmd" : "npm";
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function copyDirectorySync(src, dest) {
|
|
39
|
+
fs.mkdirSync(dest, { recursive: true });
|
|
40
|
+
for (const entry of fs.readdirSync(src, { withFileTypes: true })) {
|
|
41
|
+
const srcPath = path.join(src, entry.name);
|
|
42
|
+
const destPath = path.join(dest, entry.name);
|
|
43
|
+
if (entry.isSymbolicLink()) {
|
|
44
|
+
const target = fs.readlinkSync(srcPath);
|
|
45
|
+
fs.symlinkSync(target, destPath);
|
|
46
|
+
} else if (entry.isDirectory()) {
|
|
47
|
+
copyDirectorySync(srcPath, destPath);
|
|
48
|
+
} else {
|
|
49
|
+
fs.copyFileSync(srcPath, destPath);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function runStagedSelfCheck(cliPath, nodeExec = resolveNodeExecutable()) {
|
|
55
|
+
if (!cliPath || !fs.existsSync(cliPath)) {
|
|
56
|
+
throw new Error(`Staged Playwright MCP entrypoint missing: ${cliPath}`);
|
|
57
|
+
}
|
|
58
|
+
const res = spawnSync(nodeExec, [cliPath, "--help"], {
|
|
59
|
+
encoding: "utf8",
|
|
60
|
+
timeout: 20000,
|
|
61
|
+
env: { ...process.env, NO_COLOR: "1" },
|
|
62
|
+
});
|
|
63
|
+
if (res.error) throw res.error;
|
|
64
|
+
if (res.status !== 0) {
|
|
65
|
+
const detail = String(res.stderr || res.stdout || "").trim().slice(0, 400);
|
|
66
|
+
throw new Error(`Staged Playwright MCP self-check failed (exit ${res.status})${detail ? `: ${detail}` : ""}`);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function pruneUnusedRuntimes({ env = process.env } = {}) {
|
|
71
|
+
const dir = runtimeVersionsDir(env);
|
|
72
|
+
if (!fs.existsSync(dir)) return [];
|
|
73
|
+
const active = readActiveRuntime(env);
|
|
74
|
+
const activeDir = active?.runtimeDir ? path.resolve(active.runtimeDir) : "";
|
|
75
|
+
const leased = listLeasedRuntimeVersions(env);
|
|
76
|
+
const removed = [];
|
|
77
|
+
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
|
|
78
|
+
if (!entry.isDirectory()) continue;
|
|
79
|
+
if (entry.name.startsWith(".staging-")) continue;
|
|
80
|
+
const full = path.resolve(path.join(dir, entry.name));
|
|
81
|
+
if (full === activeDir) continue;
|
|
82
|
+
const versionPrefix = entry.name.split("+")[0];
|
|
83
|
+
if (leased.has(entry.name) || leased.has(versionPrefix)) continue;
|
|
84
|
+
try {
|
|
85
|
+
fs.rmSync(full, { recursive: true, force: true });
|
|
86
|
+
removed.push(entry.name);
|
|
87
|
+
} catch {
|
|
88
|
+
// Ignore busy directories; next prune will collect them.
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
return removed;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
async function downloadToFile(url, destFile) {
|
|
95
|
+
const response = await fetch(url);
|
|
96
|
+
if (!response.ok) {
|
|
97
|
+
throw new Error(`Failed to download component (${response.status} ${response.statusText})`);
|
|
98
|
+
}
|
|
99
|
+
const buffer = Buffer.from(await response.arrayBuffer());
|
|
100
|
+
fs.writeFileSync(destFile, buffer, { mode: 0o600 });
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Install or update the pinned optional Playwright MCP runtime into ~/.ufoo/browser/runtime/<version>/.
|
|
105
|
+
* Never mutates the active ufoo package directory; rolls back cleanly on failure.
|
|
106
|
+
*/
|
|
107
|
+
async function installBrowserRuntime({
|
|
108
|
+
env = process.env,
|
|
109
|
+
version = "",
|
|
110
|
+
sourceDir = "",
|
|
111
|
+
tarball = "",
|
|
112
|
+
sha256 = "",
|
|
113
|
+
mode = "release",
|
|
114
|
+
runSelfCheck = runStagedSelfCheck,
|
|
115
|
+
installFromNpm = null,
|
|
116
|
+
} = {}) {
|
|
117
|
+
const manifest = getReleaseManifest(env);
|
|
118
|
+
const targetVersion = String(version || manifest.runtimeComponentVersion).trim();
|
|
119
|
+
const homeDir = browserHomeDir(env);
|
|
120
|
+
const versionsDir = runtimeVersionsDir(env);
|
|
121
|
+
fs.mkdirSync(homeDir, { recursive: true, mode: 0o700 });
|
|
122
|
+
fs.mkdirSync(versionsDir, { recursive: true, mode: 0o700 });
|
|
123
|
+
|
|
124
|
+
const lockTarget = setupLockPath(env);
|
|
125
|
+
const previousActive = readActiveRuntime(env);
|
|
126
|
+
const previousInstall = readInstallState(env);
|
|
127
|
+
writeInstallState({ state: "installing", mode, lastError: "" }, env);
|
|
128
|
+
|
|
129
|
+
const stagingDir = path.join(versionsDir, `.staging-${targetVersion}-${randomUUID().slice(0, 8)}`);
|
|
130
|
+
let releaseLock = null;
|
|
131
|
+
try {
|
|
132
|
+
releaseLock = await lockfile.lock(lockTarget, {
|
|
133
|
+
realpath: false,
|
|
134
|
+
stale: 120000,
|
|
135
|
+
update: 10000,
|
|
136
|
+
retries: { retries: 10, minTimeout: 100, maxTimeout: 500 },
|
|
137
|
+
});
|
|
138
|
+
fs.mkdirSync(stagingDir, { recursive: true, mode: 0o700 });
|
|
139
|
+
const nodeExec = resolveNodeExecutable();
|
|
140
|
+
const effectiveSource = String(sourceDir || env.UFOO_BROWSER_COMPONENT_SOURCE || "").trim();
|
|
141
|
+
const effectiveTarball = String(tarball || manifest.componentTarballUrl || "").trim();
|
|
142
|
+
const expectedSha = String(sha256 || manifest.componentTarballSha256 || "").trim().toLowerCase();
|
|
143
|
+
let computedSha = "";
|
|
144
|
+
|
|
145
|
+
if (effectiveSource) {
|
|
146
|
+
if (!fs.existsSync(effectiveSource)) {
|
|
147
|
+
throw new Error(`Browser component source directory not found: ${effectiveSource}`);
|
|
148
|
+
}
|
|
149
|
+
copyDirectorySync(effectiveSource, stagingDir);
|
|
150
|
+
} else {
|
|
151
|
+
let packageSpec = `${manifest.runtimeComponentName}@${targetVersion}`;
|
|
152
|
+
if (effectiveTarball) {
|
|
153
|
+
let localTarball = effectiveTarball;
|
|
154
|
+
if (/^https?:\/\//i.test(effectiveTarball)) {
|
|
155
|
+
localTarball = path.join(stagingDir, "component.tgz");
|
|
156
|
+
await downloadToFile(effectiveTarball, localTarball);
|
|
157
|
+
} else if (!fs.existsSync(localTarball)) {
|
|
158
|
+
throw new Error(`Browser component tarball not found: ${localTarball}`);
|
|
159
|
+
}
|
|
160
|
+
computedSha = computeFileSha256(localTarball);
|
|
161
|
+
if (expectedSha && computedSha !== expectedSha) {
|
|
162
|
+
throw new Error(`Browser component sha256 mismatch (expected ${expectedSha}, got ${computedSha})`);
|
|
163
|
+
}
|
|
164
|
+
packageSpec = localTarball;
|
|
165
|
+
}
|
|
166
|
+
if (typeof installFromNpm === "function") {
|
|
167
|
+
await installFromNpm({ stagingDir, packageSpec, nodeExec });
|
|
168
|
+
} else {
|
|
169
|
+
fs.writeFileSync(path.join(stagingDir, "package.json"), JSON.stringify({ name: "ufoo-browser-runtime", private: true }, null, 2));
|
|
170
|
+
const npmCmd = resolveNpmCommand(nodeExec);
|
|
171
|
+
const res = spawnSync(
|
|
172
|
+
npmCmd,
|
|
173
|
+
["install", "--prefix", stagingDir, "--no-save", "--no-audit", "--no-fund", "--omit=dev", packageSpec],
|
|
174
|
+
{ encoding: "utf8", timeout: 180000, cwd: stagingDir }
|
|
175
|
+
);
|
|
176
|
+
if (res.error) throw res.error;
|
|
177
|
+
if (res.status !== 0) {
|
|
178
|
+
const detail = String(res.stderr || res.stdout || "").trim().slice(0, 600);
|
|
179
|
+
throw new Error(`npm install of browser component failed (exit ${res.status})${detail ? `: ${detail}` : ""}`);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
const cliCandidates = [
|
|
185
|
+
path.join(stagingDir, "node_modules", "@playwright", "mcp", "cli.js"),
|
|
186
|
+
path.join(stagingDir, "cli.js"),
|
|
187
|
+
];
|
|
188
|
+
const stagedCli = cliCandidates.find((candidate) => fs.existsSync(candidate));
|
|
189
|
+
if (!stagedCli) {
|
|
190
|
+
throw new Error("Playwright MCP cli.js not found in staged component");
|
|
191
|
+
}
|
|
192
|
+
runSelfCheck(stagedCli, nodeExec);
|
|
193
|
+
|
|
194
|
+
const pkgJsonPath = path.join(path.dirname(stagedCli), "package.json");
|
|
195
|
+
let resolvedVersion = targetVersion;
|
|
196
|
+
try {
|
|
197
|
+
const parsedPkg = JSON.parse(fs.readFileSync(pkgJsonPath, "utf8"));
|
|
198
|
+
if (parsedPkg && parsedPkg.version) resolvedVersion = String(parsedPkg.version);
|
|
199
|
+
} catch {
|
|
200
|
+
// Keep requested version
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
const leased = listLeasedRuntimeVersions(env);
|
|
204
|
+
const baseTarget = path.join(versionsDir, resolvedVersion);
|
|
205
|
+
const finalRuntimeDir = fs.existsSync(baseTarget) && (leased.has(resolvedVersion) || previousActive?.runtimeDir === path.resolve(baseTarget))
|
|
206
|
+
? path.join(versionsDir, `${resolvedVersion}+${randomUUID().slice(0, 6)}`)
|
|
207
|
+
: baseTarget;
|
|
208
|
+
if (fs.existsSync(finalRuntimeDir)) {
|
|
209
|
+
fs.rmSync(finalRuntimeDir, { recursive: true, force: true });
|
|
210
|
+
}
|
|
211
|
+
fs.renameSync(stagingDir, finalRuntimeDir);
|
|
212
|
+
const finalCli = path.relative(stagingDir, stagedCli);
|
|
213
|
+
const entryPath = path.join(finalRuntimeDir, finalCli);
|
|
214
|
+
const activeRecord = writeActiveRuntimeAtomic({
|
|
215
|
+
schemaVersion: 1,
|
|
216
|
+
component: manifest.runtimeComponentName,
|
|
217
|
+
version: resolvedVersion,
|
|
218
|
+
runtimeDir: finalRuntimeDir,
|
|
219
|
+
entryPath,
|
|
220
|
+
sha256: computedSha || expectedSha || "",
|
|
221
|
+
installedAt: new Date().toISOString(),
|
|
222
|
+
}, env);
|
|
223
|
+
writeInstallState({
|
|
224
|
+
state: "installed",
|
|
225
|
+
mode,
|
|
226
|
+
runtimeInstalled: true,
|
|
227
|
+
runtimeVersion: resolvedVersion,
|
|
228
|
+
lastError: "",
|
|
229
|
+
}, env);
|
|
230
|
+
pruneUnusedRuntimes({ env });
|
|
231
|
+
return { ok: true, active: activeRecord };
|
|
232
|
+
} catch (error) {
|
|
233
|
+
try { fs.rmSync(stagingDir, { recursive: true, force: true }); } catch { /* ignore */ }
|
|
234
|
+
if (previousActive && !readActiveRuntime(env)) {
|
|
235
|
+
writeActiveRuntimeAtomic(previousActive, env);
|
|
236
|
+
}
|
|
237
|
+
writeInstallState({
|
|
238
|
+
state: previousActive ? previousInstall.state : "not_installed",
|
|
239
|
+
runtimeInstalled: Boolean(previousActive),
|
|
240
|
+
runtimeVersion: previousActive?.version || previousInstall.runtimeVersion || "",
|
|
241
|
+
lastError: error && error.message ? error.message : String(error),
|
|
242
|
+
}, env);
|
|
243
|
+
throw error;
|
|
244
|
+
} finally {
|
|
245
|
+
if (releaseLock) await releaseLock().catch(() => {});
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
function uninstallBrowserRuntime({ env = process.env } = {}) {
|
|
250
|
+
const homeDir = browserHomeDir(env);
|
|
251
|
+
if (!fs.existsSync(homeDir)) return { removed: false };
|
|
252
|
+
try {
|
|
253
|
+
fs.rmSync(activeRuntimePath(env), { force: true });
|
|
254
|
+
} catch { /* ignore */ }
|
|
255
|
+
try {
|
|
256
|
+
fs.rmSync(runtimeVersionsDir(env), { recursive: true, force: true });
|
|
257
|
+
} catch { /* ignore */ }
|
|
258
|
+
writeInstallState({
|
|
259
|
+
state: "not_installed",
|
|
260
|
+
runtimeInstalled: false,
|
|
261
|
+
runtimeVersion: "",
|
|
262
|
+
nativeHostRegistered: false,
|
|
263
|
+
devHostRegistered: false,
|
|
264
|
+
lastError: "",
|
|
265
|
+
}, env);
|
|
266
|
+
return { removed: true };
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
module.exports = {
|
|
270
|
+
computeFileSha256,
|
|
271
|
+
runStagedSelfCheck,
|
|
272
|
+
pruneUnusedRuntimes,
|
|
273
|
+
installBrowserRuntime,
|
|
274
|
+
uninstallBrowserRuntime,
|
|
275
|
+
};
|