pi-webapp 0.1.27 → 0.1.29
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/bin/pi-webapp.mjs +25 -10
- package/desktop/check-pty.cjs +35 -0
- package/desktop/managed-release.mjs +82 -0
- package/dist/extension.js +30 -30
- package/docs/desktop.md +55 -8
- package/package.json +5 -2
- package/web/dist/.vite/manifest.json +23 -23
- package/web/dist/assets/{app-DGAHiflJ.js → app-B6KN89oD.js} +31 -31
- package/web/dist/assets/app.js +2 -2
- package/web/dist/assets/{graphql-CM8br3eP.js → graphql-y8zqvm7I.js} +1 -1
- package/web/dist/assets/{http-Dl6CKe_K.js → http-RvN4shOP.js} +1 -1
- package/web/dist/assets/{php-Bk_hkz6U.js → php-DYEaTBiJ.js} +1 -1
- package/web/dist/assets/{plugin-BPPaJeXM.js → plugin-CWZjNBTB.js} +1 -1
- package/web/dist/assets/{plugin-C7GyvZv1.js → plugin-Ct_K82sD.js} +1 -1
- package/web/dist/assets/{plugin-BBQh2TKb.js → plugin-DPe686Yq.js} +1 -1
- package/web/dist/assets/{rst-L1NMAHRS.js → rst-GflBTkRB.js} +1 -1
- package/web/dist/assets/{ruby-CT2Et3Md.js → ruby-h9QEI4Un.js} +1 -1
- package/web/dist/assets/{svelte-gqLIvd6P.js → svelte-Dnn1bF1c.js} +1 -1
- package/web/dist/assets/{vue-9dlEQDyH.js → vue-VJMQoCXf.js} +1 -1
package/bin/pi-webapp.mjs
CHANGED
|
@@ -2,25 +2,27 @@
|
|
|
2
2
|
import { spawn as spawnNode } from "node:child_process";
|
|
3
3
|
import { randomUUID } from "node:crypto";
|
|
4
4
|
import { closeSync, existsSync, openSync, watch } from "node:fs";
|
|
5
|
-
import { mkdir, open, readFile, rename, stat, unlink, writeFile } from "node:fs/promises";
|
|
5
|
+
import { mkdir, open, readFile, realpath, rename, stat, unlink, writeFile } from "node:fs/promises";
|
|
6
6
|
import { homedir } from "node:os";
|
|
7
7
|
import { delimiter, dirname, join, resolve } from "node:path";
|
|
8
8
|
import { fileURLToPath } from "node:url";
|
|
9
9
|
import spawn from "cross-spawn";
|
|
10
10
|
import { openWebPage } from "./open-web-page.mjs";
|
|
11
11
|
import { npmCommand } from "./npm-command.mjs";
|
|
12
|
+
import { desktopProtocolVersion, desktopUpdatePath, readDesktopRelease, rollbackDesktopRelease } from "../desktop/managed-release.mjs";
|
|
12
13
|
|
|
13
14
|
const minimum = [0, 87, 1];
|
|
14
15
|
const packageVersion = JSON.parse(await readFile(new URL("../package.json", import.meta.url), "utf8")).version;
|
|
15
16
|
const runtimeKind = process.env.PI_WEBAPP_RUNTIME === "desktop" ? "desktop" : "npm";
|
|
16
|
-
const
|
|
17
|
+
const desktopPackages = runtimeKind === "desktop" && process.env.PI_WEBAPP_DESKTOP_PACKAGE_UPDATES === "1";
|
|
18
|
+
const runtimeInfo = { version: packageVersion, bridgeVersion: 1, runtimeKind, desktopProtocolVersion };
|
|
17
19
|
const launcher = fileURLToPath(import.meta.url);
|
|
18
20
|
const extension = fileURLToPath(new URL("../dist/extension.js", import.meta.url));
|
|
19
21
|
const agentDir = resolve((process.env.PI_CODING_AGENT_DIR ?? join(homedir(), ".pi", "agent")).replace(/^~(?=$|[\\/])/, homedir()));
|
|
20
22
|
const statePath = join(agentDir, "pi-web", "launcher.json");
|
|
21
23
|
const stopPath = join(agentDir, "pi-web", "launcher.stop");
|
|
22
24
|
const restartPath = join(agentDir, "pi-web", "launcher.restart");
|
|
23
|
-
const updatePath = join(agentDir, "pi-web", "update.json");
|
|
25
|
+
const updatePath = desktopPackages ? desktopUpdatePath(agentDir) : join(agentDir, "pi-web", "update.json");
|
|
24
26
|
const logPath = join(agentDir, "pi-web", "launcher.log");
|
|
25
27
|
|
|
26
28
|
function versionParts(value) {
|
|
@@ -180,7 +182,8 @@ async function serve(pi, args) {
|
|
|
180
182
|
try {
|
|
181
183
|
const request = JSON.parse(await readFile(restartPath, "utf8"));
|
|
182
184
|
const prepared = JSON.parse(await readFile(updatePath, "utf8"));
|
|
183
|
-
|
|
185
|
+
const selected = desktopPackages ? await readDesktopRelease(agentDir, packageVersion) : prepared;
|
|
186
|
+
if ((runtimeKind !== "desktop" || desktopPackages) && request.nonce === nonce && request.launcher === selected?.launcher && existsSync(request.launcher)) {
|
|
184
187
|
restartRequest = request;
|
|
185
188
|
shutdown("restart_request");
|
|
186
189
|
}
|
|
@@ -289,11 +292,15 @@ async function serve(pi, args) {
|
|
|
289
292
|
const fd = openSync(logPath, "a", 0o600);
|
|
290
293
|
const restartEnv = { ...process.env, PI_WEBAPP_AUTO_OPEN: "0", PI_WEBAPP_RESTART_PORT: oldUrl.port, PI_WEBAPP_RESTART_TOKEN: oldUrl.hash.slice(1) };
|
|
291
294
|
try {
|
|
292
|
-
const runLauncher = (path) =>
|
|
293
|
-
const
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
295
|
+
const runLauncher = async (path) => {
|
|
296
|
+
const selected = desktopPackages ? await readDesktopRelease(agentDir, packageVersion) : null;
|
|
297
|
+
const env = selected?.launcher === path ? { ...restartEnv, PI_WEBAPP_PI_CLI: selected.piCli } : restartEnv;
|
|
298
|
+
return new Promise((resolveExit) => {
|
|
299
|
+
const next = spawnNode(process.execPath, [path, "start", ...args], { cwd: process.cwd(), env, stdio: ["ignore", fd, fd] });
|
|
300
|
+
next.once("error", () => resolveExit(1));
|
|
301
|
+
next.once("exit", (code) => resolveExit(code ?? 1));
|
|
302
|
+
});
|
|
303
|
+
};
|
|
297
304
|
if (await runLauncher(restartRequest.launcher) !== 0) {
|
|
298
305
|
await clearManagedLauncher(restartRequest.launcher);
|
|
299
306
|
console.error("pi-webapp: 新版启动失败,正在恢复旧版服务");
|
|
@@ -304,12 +311,18 @@ async function serve(pi, args) {
|
|
|
304
311
|
}
|
|
305
312
|
|
|
306
313
|
async function clearManagedLauncher(path) {
|
|
314
|
+
if (desktopPackages) { await rollbackDesktopRelease(agentDir, path); return; }
|
|
307
315
|
try {
|
|
308
316
|
if (JSON.parse(await readFile(updatePath, "utf8")).launcher === path) await unlink(updatePath);
|
|
309
317
|
} catch { /* No matching managed release. */ }
|
|
310
318
|
}
|
|
311
319
|
|
|
312
320
|
async function managedLauncher() {
|
|
321
|
+
if (runtimeKind === "desktop") {
|
|
322
|
+
if (!desktopPackages) return null;
|
|
323
|
+
const selected = await readDesktopRelease(agentDir, packageVersion);
|
|
324
|
+
return selected && await realpath(selected.launcher) !== await realpath(launcher) ? selected.launcher : null;
|
|
325
|
+
}
|
|
313
326
|
try {
|
|
314
327
|
if (existsSync(fileURLToPath(new URL("../.git", import.meta.url)))) return null;
|
|
315
328
|
const update = JSON.parse(await readFile(updatePath, "utf8"));
|
|
@@ -330,7 +343,9 @@ async function start(args) {
|
|
|
330
343
|
}
|
|
331
344
|
const managed = await managedLauncher();
|
|
332
345
|
if (managed) {
|
|
333
|
-
const
|
|
346
|
+
const selected = desktopPackages ? await readDesktopRelease(agentDir, packageVersion) : null;
|
|
347
|
+
const env = selected?.launcher === managed ? { ...process.env, PI_WEBAPP_PI_CLI: selected.piCli } : process.env;
|
|
348
|
+
const next = spawnNode(process.execPath, [managed, "start", ...args], { cwd: process.cwd(), env, stdio: "inherit" });
|
|
334
349
|
const code = await new Promise((resolveExit, rejectExit) => { next.once("error", rejectExit); next.once("exit", (status) => resolveExit(status)); });
|
|
335
350
|
if (code !== 0) {
|
|
336
351
|
await clearManagedLauncher(managed);
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
const { createRequire } = require("node:module");
|
|
2
|
+
const { join } = require("node:path");
|
|
3
|
+
|
|
4
|
+
// A real terminal must answer cursor/device queries. Windows ConPTY can wait
|
|
5
|
+
// for these answers before running cmd; a raw onData collector is not a client.
|
|
6
|
+
function checkPty({ pty, Terminal, platform = process.platform, env = process.env, timeoutMs = 12000 }) {
|
|
7
|
+
return new Promise((resolve, reject) => {
|
|
8
|
+
const screen = new Terminal({ cols: 80, rows: 24, allowProposedApi: true });
|
|
9
|
+
const windows = platform === "win32";
|
|
10
|
+
const terminal = pty.spawn(windows ? "cmd.exe" : "/bin/sh", windows ? ["/d", "/q"] : ["-i"], {
|
|
11
|
+
cols: 80, rows: 24, env: { ...env, PI_DESKTOP_PROBE_SUFFIX: "PTY_OK" },
|
|
12
|
+
});
|
|
13
|
+
let text = "", finished = false;
|
|
14
|
+
const timer = setTimeout(() => finish(new Error("PTY command/exit timed out")), timeoutMs);
|
|
15
|
+
function finish(error) {
|
|
16
|
+
if (finished) return;
|
|
17
|
+
finished = true; clearTimeout(timer); screen.dispose();
|
|
18
|
+
if (error) { try { terminal.kill(); } catch {} reject(error); }
|
|
19
|
+
else resolve();
|
|
20
|
+
}
|
|
21
|
+
screen.onData(data => terminal.write(data));
|
|
22
|
+
terminal.onData(data => { text += data; screen.write(data); });
|
|
23
|
+
terminal.onExit(({ exitCode }) => finish(exitCode === 0 && text.includes("PI_DESKTOP_PTY_OK")
|
|
24
|
+
? null : new Error("PTY command did not complete with expected output and exit status")));
|
|
25
|
+
terminal.write(windows ? "echo PI_DESKTOP_%PI_DESKTOP_PROBE_SUFFIX%\rexit /b 0\r" : "printf 'PI_DESKTOP_%s\\n' PTY_OK\rexit 0\r");
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
module.exports = { checkPty };
|
|
29
|
+
if (require.main === module) {
|
|
30
|
+
// Resolve inside the staged backend, never the host build's node_modules.
|
|
31
|
+
const runtimeRequire = createRequire(join(process.cwd(), "package.json"));
|
|
32
|
+
checkPty({ pty: runtimeRequire("node-pty"), Terminal: runtimeRequire("@xterm/headless").Terminal })
|
|
33
|
+
.then(() => { console.log("PI_DESKTOP_PTY_OK (interactive terminal and exit verified)"); process.exit(0); })
|
|
34
|
+
.catch(error => { console.error(error.message); process.exit(1); });
|
|
35
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { access, readFile, realpath, rename, rm, writeFile } from "node:fs/promises";
|
|
2
|
+
import { randomUUID } from "node:crypto";
|
|
3
|
+
import { dirname, join, resolve, sep } from "node:path";
|
|
4
|
+
import { createRequire } from "node:module";
|
|
5
|
+
|
|
6
|
+
// Version of the shell/launcher contract, independent of the npm package version.
|
|
7
|
+
export const desktopProtocolVersion = 1;
|
|
8
|
+
export const desktopUpdatePath = agentDir => join(agentDir, "pi-web", "desktop-update.json");
|
|
9
|
+
|
|
10
|
+
function compare(a, b) {
|
|
11
|
+
const parts = value => /^\d+\.\d+\.\d+$/.test(value ?? "") ? value.split(".").map(Number) : null;
|
|
12
|
+
const left = parts(a); const right = parts(b);
|
|
13
|
+
if (!left || !right) throw new Error("无效的桌面业务包版本");
|
|
14
|
+
for (let i = 0; i < 3; i++) if (left[i] !== right[i]) return left[i] - right[i];
|
|
15
|
+
return 0;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// Resolve metadata without executing dependencies or relying on package.json exports.
|
|
19
|
+
export async function dependencyRoot(packageRoot, name) {
|
|
20
|
+
const require = createRequire(join(packageRoot, "package.json"));
|
|
21
|
+
for (const directory of require.resolve.paths(name) ?? []) {
|
|
22
|
+
const candidate = join(directory, name);
|
|
23
|
+
try {
|
|
24
|
+
if (JSON.parse(await readFile(join(candidate, "package.json"), "utf8")).name === name) return await realpath(candidate);
|
|
25
|
+
} catch { /* Try the next Node resolution directory. */ }
|
|
26
|
+
}
|
|
27
|
+
throw new Error(`缺少运行依赖:${name}`);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export async function validateDesktopRelease(agentDir, release) {
|
|
31
|
+
compare(release?.version, release?.version);
|
|
32
|
+
if (release.desktopProtocolVersion !== desktopProtocolVersion || typeof release.launcher !== "string") throw new Error("桌面业务包协议不兼容");
|
|
33
|
+
const root = await realpath(resolve(dirname(release.launcher), ".."));
|
|
34
|
+
const releases = await realpath(join(agentDir, "pi-web", "releases"));
|
|
35
|
+
const directory = dirname(dirname(root));
|
|
36
|
+
if (await realpath(release.launcher) !== join(root, "bin", "pi-webapp.mjs")
|
|
37
|
+
|| dirname(directory) !== releases || !directory.startsWith(releases + sep)
|
|
38
|
+
|| !/^\d+\.\d+\.\d+-[\w-]+$/.test(directory.slice(releases.length + 1))) throw new Error("拒绝用户更新目录之外的桌面业务包");
|
|
39
|
+
const pkg = JSON.parse(await readFile(join(root, "package.json"), "utf8"));
|
|
40
|
+
if (pkg.name !== "pi-webapp" || pkg.version !== release.version) throw new Error("桌面业务包版本与记录不一致");
|
|
41
|
+
if (pkg.piWebappDesktopProtocol !== desktopProtocolVersion) throw new Error("新版业务包需要更新桌面外壳,尚未切换运行版本");
|
|
42
|
+
const piRoot = await dependencyRoot(root, "@earendil-works/pi-coding-agent");
|
|
43
|
+
if (!piRoot.startsWith(directory + sep)) throw new Error("桌面业务包缺少独立的 Pi 运行依赖");
|
|
44
|
+
const piCli = join(piRoot, "dist", "bundle", "cli.js");
|
|
45
|
+
await Promise.all([release.launcher, piCli, join(root, "dist", "extension.js"), join(root, "web", "dist", "index.html")].map(path => access(path)));
|
|
46
|
+
return { version: release.version, launcher: release.launcher, piCli, desktopProtocolVersion };
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** Invalid, older or incomplete selections never replace the bundled fallback. */
|
|
50
|
+
export async function readDesktopRelease(agentDir, minimumVersion) {
|
|
51
|
+
try {
|
|
52
|
+
const release = JSON.parse(await readFile(desktopUpdatePath(agentDir), "utf8"));
|
|
53
|
+
if (compare(release.version, minimumVersion) < 0) return null;
|
|
54
|
+
return await validateDesktopRelease(agentDir, release);
|
|
55
|
+
} catch { return null; }
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export async function writeDesktopRelease(agentDir, release) {
|
|
59
|
+
const path = desktopUpdatePath(agentDir);
|
|
60
|
+
const temporary = `${path}.${randomUUID()}.tmp`;
|
|
61
|
+
try {
|
|
62
|
+
await writeFile(temporary, JSON.stringify(release), { mode: 0o600 });
|
|
63
|
+
await rename(temporary, path);
|
|
64
|
+
} finally { await rm(temporary, { force: true }); }
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** Only roll back our own failed selection, never a replacement from another client. */
|
|
68
|
+
export async function rollbackDesktopRelease(agentDir, failedLauncher) {
|
|
69
|
+
try {
|
|
70
|
+
const path = desktopUpdatePath(agentDir);
|
|
71
|
+
const release = JSON.parse(await readFile(path, "utf8"));
|
|
72
|
+
if (release.launcher !== failedLauncher) return;
|
|
73
|
+
if (release.previous) {
|
|
74
|
+
try {
|
|
75
|
+
const previous = await validateDesktopRelease(agentDir, release.previous);
|
|
76
|
+
await writeDesktopRelease(agentDir, previous);
|
|
77
|
+
return;
|
|
78
|
+
} catch { /* Fall back to the application's bundled package. */ }
|
|
79
|
+
}
|
|
80
|
+
await rm(path, { force: true });
|
|
81
|
+
} catch (error) { if (error.code !== "ENOENT") throw error; }
|
|
82
|
+
}
|