pi-web-ui 0.9.3 → 0.10.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/extensions/webui.ts +184 -0
- package/package.json +8 -2
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* pi-web-ui 的 pi 扩展 —— 提供命令行集成。
|
|
3
|
+
*
|
|
4
|
+
* 能力:
|
|
5
|
+
* /webui 启动本机 pi-web-ui 服务器,打开浏览器访问
|
|
6
|
+
* /webui --port 9000 指定端口启动
|
|
7
|
+
* /webui --no-browser 启动但不开浏览器
|
|
8
|
+
* /webui stop 停止已启动的服务器
|
|
9
|
+
* /webui status 查看运行状态 / URL
|
|
10
|
+
*
|
|
11
|
+
* 实现说明:
|
|
12
|
+
* - 不依赖全局 bin(pi install 后 pi-web-ui 命令不一定在 PATH),直接用
|
|
13
|
+
* node 调包内 dist/server/index.js,通过环境变量 PORT / PI_WEB_CWD /
|
|
14
|
+
* PI_WEB_DATA_DIR 控制。
|
|
15
|
+
* - 工作目录默认用当前 pi 会话的 ctx.cwd;可用 --cwd / path 覆盖。
|
|
16
|
+
* - 服务器作为子进程后台运行,/webui 不阻塞 pi。
|
|
17
|
+
* - 每个 pi 会话管理一个子进程;session_shutdown 时清理,避免孤儿进程。
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
import { spawn } from "node:child_process";
|
|
21
|
+
import { existsSync } from "node:fs";
|
|
22
|
+
import { dirname, join, resolve } from "node:path";
|
|
23
|
+
import { fileURLToPath } from "node:url";
|
|
24
|
+
import net from "node:net";
|
|
25
|
+
import type { ExtensionAPI, ExtensionCommandContext } from "@earendil-works/pi-coding-agent";
|
|
26
|
+
|
|
27
|
+
// 本文件位于 <pkg>/extensions/webui.ts → 包根在上一级
|
|
28
|
+
const PKG_ROOT = resolve(dirname(fileURLToPath(import.meta.url)), "..");
|
|
29
|
+
const SERVER_ENTRY = join(PKG_ROOT, "dist", "server", "index.js");
|
|
30
|
+
const NODE = process.execPath;
|
|
31
|
+
|
|
32
|
+
/** 每个会话的服务器子进程 + 元数据 */
|
|
33
|
+
interface RunningServer {
|
|
34
|
+
proc: ReturnType<typeof spawn>;
|
|
35
|
+
port: number;
|
|
36
|
+
cwd: string;
|
|
37
|
+
url: string;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// 会话 → 运行实例(模块级 Map;每会话一个会话对象,无需清理全局)
|
|
41
|
+
const running = new Map<string, RunningServer>();
|
|
42
|
+
|
|
43
|
+
/** 找一个空闲端口 */
|
|
44
|
+
function findFreePort(from = 8787): Promise<number> {
|
|
45
|
+
return new Promise((resolve_, reject) => {
|
|
46
|
+
const srv = net.createServer();
|
|
47
|
+
srv.listen(from, () => {
|
|
48
|
+
const port = (srv.address() as net.AddressInfo).port;
|
|
49
|
+
srv.close(() => resolve_(port));
|
|
50
|
+
});
|
|
51
|
+
srv.on("error", () => {
|
|
52
|
+
// 端口被占则顺延
|
|
53
|
+
findFreePort(from + 1).then(resolve_, reject);
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** 解析 --key value / --flag 参数 */
|
|
59
|
+
function parseArgs(args: string): { port?: number; cwd?: string; noBrowser: boolean } {
|
|
60
|
+
const out: { port?: number; cwd?: string; noBrowser: boolean } = { noBrowser: false };
|
|
61
|
+
const toks = args.split(/\s+/).filter(Boolean);
|
|
62
|
+
for (let i = 0; i < toks.length; i++) {
|
|
63
|
+
const t = toks[i];
|
|
64
|
+
if ((t === "--port" || t === "-p") && toks[i + 1]) {
|
|
65
|
+
const n = Number(toks[++i]);
|
|
66
|
+
if (Number.isInteger(n) && n > 0 && n < 65536) out.port = n;
|
|
67
|
+
} else if ((t === "--cwd") && toks[i + 1]) {
|
|
68
|
+
out.cwd = resolve(toks[++i]);
|
|
69
|
+
} else if (t === "--no-browser") {
|
|
70
|
+
out.noBrowser = true;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return out;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** 打开浏览器 */
|
|
77
|
+
async function openBrowser(url: string): Promise<void> {
|
|
78
|
+
const { platform } = process;
|
|
79
|
+
const cmd =
|
|
80
|
+
platform === "darwin"
|
|
81
|
+
? ["open", url]
|
|
82
|
+
: platform === "win32"
|
|
83
|
+
? ["cmd", "/c", "start", "", url]
|
|
84
|
+
: ["xdg-open", url];
|
|
85
|
+
try {
|
|
86
|
+
spawn(cmd[0], cmd.slice(1), { stdio: "ignore", detached: true }).unref();
|
|
87
|
+
} catch {
|
|
88
|
+
/* 忽略打开失败(headless 等场景) */
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export default function (pi: ExtensionAPI): void {
|
|
93
|
+
pi.registerCommand("webui", {
|
|
94
|
+
description: "启动本机 pi-web-ui Web 界面(/webui [--port N] [--cwd PATH] [--no-browser] | stop | status)",
|
|
95
|
+
handler: async (args: string, ctx: ExtensionCommandContext) => {
|
|
96
|
+
const sid = ctx.sessionManager.getSessionId();
|
|
97
|
+
const opts = parseArgs(args);
|
|
98
|
+
const action = (args.split(/\s+/)[0] || "start").toLowerCase();
|
|
99
|
+
|
|
100
|
+
// 停止
|
|
101
|
+
if (action === "stop" || action === "kill") {
|
|
102
|
+
const inst = running.get(sid);
|
|
103
|
+
if (!inst) {
|
|
104
|
+
ctx.ui.notify("没有正在运行的本机 pi-web-ui 服务器", "info");
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
inst.proc.kill("SIGTERM");
|
|
108
|
+
running.delete(sid);
|
|
109
|
+
ctx.ui.notify(`已停止 pi-web-ui (${inst.url})`, "info");
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// 状态
|
|
114
|
+
if (action === "status") {
|
|
115
|
+
const inst = running.get(sid);
|
|
116
|
+
if (!inst) {
|
|
117
|
+
ctx.ui.notify("本机 pi-web-ui 未运行", "info");
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
const alive = inst.proc.exitCode === null;
|
|
121
|
+
ctx.ui.notify(
|
|
122
|
+
alive ? `pi-web-ui 运行中 → ${inst.url}\n端口 ${inst.port} · cwd ${inst.cwd}` : `已退出(exit=${inst.proc.exitCode})`,
|
|
123
|
+
alive ? "info" : "warning",
|
|
124
|
+
);
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// 默认 start
|
|
129
|
+
if (action !== "start" && action !== "run") {
|
|
130
|
+
ctx.ui.notify(`未知动作 ${action}(可用 start|stop|status)`, "warning");
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// 已运行则提示
|
|
135
|
+
const existing = running.get(sid);
|
|
136
|
+
if (existing && existing.proc.exitCode === null) {
|
|
137
|
+
ctx.ui.notify(`pi-web-ui 已在运行 → ${existing.url}`, "info");
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// 检查是否已构建
|
|
142
|
+
if (!existsSync(SERVER_ENTRY)) {
|
|
143
|
+
ctx.ui.notify(
|
|
144
|
+
"缺少 dist/ 产物(当前安装未包含已构建前端)。请运行 `npm run build` 后重试,或用 pi-web-ui 官方 npm 包。",
|
|
145
|
+
"warning",
|
|
146
|
+
);
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
const port = opts.port ?? (await findFreePort());
|
|
151
|
+
const cwd = opts.cwd ?? ctx.cwd;
|
|
152
|
+
const url = `http://localhost:${port}`;
|
|
153
|
+
|
|
154
|
+
const env = {
|
|
155
|
+
...process.env,
|
|
156
|
+
PORT: String(port),
|
|
157
|
+
PI_WEB_CWD: cwd,
|
|
158
|
+
...(process.env.PI_WEB_DATA_DIR ? {} : { PI_WEB_DATA_DIR: join(cwd, ".pi-web") }),
|
|
159
|
+
};
|
|
160
|
+
const proc = spawn(NODE, [SERVER_ENTRY], { cwd, env, stdio: "ignore", detached: true });
|
|
161
|
+
proc.unref();
|
|
162
|
+
running.set(sid, { proc, port, cwd, url });
|
|
163
|
+
|
|
164
|
+
ctx.ui.notify(`pi-web-ui 启动中 → ${url}\n端口 ${port} · cwd ${cwd}\n(几秒后可用,/webui status 查看)`);
|
|
165
|
+
|
|
166
|
+
if (!opts.noBrowser) await openBrowser(url);
|
|
167
|
+
|
|
168
|
+
// 进程退出时清理
|
|
169
|
+
proc.on("exit", () => {
|
|
170
|
+
if (running.get(sid)?.proc === proc) running.delete(sid);
|
|
171
|
+
});
|
|
172
|
+
},
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
// 会话结束清理子进程,避免孤儿
|
|
176
|
+
pi.on("session_shutdown", async (_event, ctx) => {
|
|
177
|
+
const sid = ctx.sessionManager.getSessionId();
|
|
178
|
+
const inst = running.get(sid);
|
|
179
|
+
if (inst && inst.proc.exitCode === null) {
|
|
180
|
+
inst.proc.kill("SIGTERM");
|
|
181
|
+
running.delete(sid);
|
|
182
|
+
}
|
|
183
|
+
});
|
|
184
|
+
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pi-web-ui",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"version": "0.9.3",
|
|
3
|
+
"version": "0.10.0",
|
|
5
4
|
"description": "Web chat interface for the pi coding agent, powered by the pi SDK (@earendil-works/pi-coding-agent) — one-command run, Docker/systemd/launchd deployable",
|
|
6
5
|
"license": "MIT",
|
|
7
6
|
"type": "module",
|
|
@@ -14,11 +13,13 @@
|
|
|
14
13
|
"dist/",
|
|
15
14
|
"web/dist/",
|
|
16
15
|
"deploy/",
|
|
16
|
+
"extensions/",
|
|
17
17
|
"README.md",
|
|
18
18
|
"LICENSE"
|
|
19
19
|
],
|
|
20
20
|
"keywords": [
|
|
21
21
|
"pi",
|
|
22
|
+
"pi-package",
|
|
22
23
|
"coding-agent",
|
|
23
24
|
"ai",
|
|
24
25
|
"chat",
|
|
@@ -30,6 +31,11 @@
|
|
|
30
31
|
"type": "git",
|
|
31
32
|
"url": "git+https://github.com/xing-shuyin/pi-web-ui.git"
|
|
32
33
|
},
|
|
34
|
+
"pi": {
|
|
35
|
+
"extensions": [
|
|
36
|
+
"./extensions"
|
|
37
|
+
]
|
|
38
|
+
},
|
|
33
39
|
"engines": {
|
|
34
40
|
"node": ">=22.19.0"
|
|
35
41
|
},
|