pi-web-ui 0.35.0 → 0.36.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 -21
- package/README.md +388 -388
- package/README.zh-CN.md +359 -359
- package/bin/pi-web-ui.mjs +1736 -1727
- package/deploy/com.xingshuyin.pi-web-ui.plist +48 -48
- package/deploy/nginx-subpath.conf +88 -88
- package/deploy/pi-web-ui-task.xml +71 -71
- package/deploy/pi-web-ui.service +31 -31
- package/dist/server/agent-service.js +34 -10
- package/dist/server/attachments.js +3 -3
- package/dist/server/index.js +4 -1
- package/dist/server/plugins.js +46 -4
- package/dist/server/protocol-version.js +1 -1
- package/dist/server/terminals.js +5 -5
- package/extensions/webui.ts +192 -192
- package/package.json +1 -1
- package/themes/md-preview.css +93 -0
- package/themes/white.css +93 -0
- package/web/dist/assets/{TerminalPanel-BIzzCNZ2.js → TerminalPanel-BxezvWth.js} +1 -1
- package/web/dist/assets/index-BP593LGC.js +19 -0
- package/web/dist/assets/index-DduTNQNx.css +10 -0
- package/web/dist/favicon.svg +8 -8
- package/web/dist/index.html +15 -15
- package/web/public/favicon.svg +8 -8
- package/web/dist/assets/index-CKIZpMV3.js +0 -19
- package/web/dist/assets/index-Diz_v1In.css +0 -10
package/dist/server/plugins.js
CHANGED
|
@@ -24,7 +24,6 @@ import { pathToFileURL } from "node:url";
|
|
|
24
24
|
const ID_RE = /^[A-Za-z0-9_-]+$/;
|
|
25
25
|
export class PluginManager {
|
|
26
26
|
dataDir;
|
|
27
|
-
cwd;
|
|
28
27
|
loaded = new Map();
|
|
29
28
|
/** 已 import 过但无入口/失败的目录——避免重复 import 与重复报错。 */
|
|
30
29
|
attempted = new Set();
|
|
@@ -36,9 +35,29 @@ export class PluginManager {
|
|
|
36
35
|
onAgentToolsChanged = undefined;
|
|
37
36
|
/** 服务端重载纪元:每次 reload() +1,前端用作 import 缓存击穿参数。 */
|
|
38
37
|
epochCounter = 0;
|
|
38
|
+
/** 当前全局工作区(host.cwd 的背后存储)——随 notifyCwd 更新。 */
|
|
39
|
+
cwdValue;
|
|
39
40
|
constructor(dataDir, cwd) {
|
|
40
41
|
this.dataDir = dataDir;
|
|
41
|
-
this.
|
|
42
|
+
this.cwdValue = resolve(cwd);
|
|
43
|
+
}
|
|
44
|
+
/** index.ts 在客户端 set_cwd 成功后调用:更新全局工作区并扇出给
|
|
45
|
+
* 所有已激活插件的 onCwdChange 钩子(异常隔离,不炸主进程)。 */
|
|
46
|
+
notifyCwd(next) {
|
|
47
|
+
const abs = resolve(next);
|
|
48
|
+
if (abs === this.cwdValue)
|
|
49
|
+
return; // 幂等:重复通知/同路径 no-op
|
|
50
|
+
this.cwdValue = abs;
|
|
51
|
+
for (const [id, p] of this.loaded) {
|
|
52
|
+
for (const h of p.cwdHandlers) {
|
|
53
|
+
try {
|
|
54
|
+
h(abs);
|
|
55
|
+
}
|
|
56
|
+
catch (err) {
|
|
57
|
+
console.error(`[plugin:${id}] cwd-change handler failed:`, err);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
42
61
|
}
|
|
43
62
|
get pluginsDir() {
|
|
44
63
|
return join(this.dataDir, "plugins");
|
|
@@ -286,6 +305,19 @@ export class PluginManager {
|
|
|
286
305
|
icon: typeof m.icon === "string" && m.icon.trim() ? m.icon.trim() : undefined,
|
|
287
306
|
hasClient: existsSync(join(dir, "client", "entry.mjs")),
|
|
288
307
|
error: this.loaded.get(name)?.info.error,
|
|
308
|
+
// 安装来源(pi-web-ui install 写入的 .pi-source.json)——
|
|
309
|
+
// 设置面板据此显示「更新」按钮;手工拷入的插件没有此文件。
|
|
310
|
+
source: await readFile(join(dir, ".pi-source.json"), "utf8")
|
|
311
|
+
.then((raw) => {
|
|
312
|
+
try {
|
|
313
|
+
const s = JSON.parse(raw);
|
|
314
|
+
return typeof s.source === "string" && s.source ? s.source : undefined;
|
|
315
|
+
}
|
|
316
|
+
catch {
|
|
317
|
+
return undefined;
|
|
318
|
+
}
|
|
319
|
+
})
|
|
320
|
+
.catch(() => undefined),
|
|
289
321
|
});
|
|
290
322
|
}
|
|
291
323
|
catch {
|
|
@@ -301,8 +333,10 @@ export class PluginManager {
|
|
|
301
333
|
this.messageHandlers.set(info.id, handlers);
|
|
302
334
|
const toolHandlers = new Set();
|
|
303
335
|
const attachHandlers = new Set();
|
|
336
|
+
const cwdHandlers = new Set();
|
|
304
337
|
const unregisterTools = [];
|
|
305
|
-
const p = { info, toolHandlers, attachHandlers };
|
|
338
|
+
const p = { info, toolHandlers, attachHandlers, cwdHandlers };
|
|
339
|
+
const self = this; // 对象字面量 getter 里不能用插件宿主的 this
|
|
306
340
|
const host = {
|
|
307
341
|
broadcast: (payload) => this.broadcast(info.id, payload),
|
|
308
342
|
notify: (level, text) => this.notifyAll(level, text),
|
|
@@ -319,6 +353,10 @@ export class PluginManager {
|
|
|
319
353
|
attachHandlers.add(h);
|
|
320
354
|
return () => attachHandlers.delete(h);
|
|
321
355
|
},
|
|
356
|
+
onCwdChange: (h) => {
|
|
357
|
+
cwdHandlers.add(h);
|
|
358
|
+
return () => cwdHandlers.delete(h);
|
|
359
|
+
},
|
|
322
360
|
// 包一层:插件反激活时自动注销它注册的全部 AI 工具,不留悬挂项。
|
|
323
361
|
registerAgentTool: (tool) => {
|
|
324
362
|
const off = this.registerAgentTool(info.id, tool);
|
|
@@ -332,7 +370,9 @@ export class PluginManager {
|
|
|
332
370
|
},
|
|
333
371
|
dir,
|
|
334
372
|
dataDir: this.dataDir,
|
|
335
|
-
cwd
|
|
373
|
+
get cwd() {
|
|
374
|
+
return self.cwdValue;
|
|
375
|
+
},
|
|
336
376
|
log: (...args) => console.log(`[plugin:${info.id}]`, ...args),
|
|
337
377
|
};
|
|
338
378
|
try {
|
|
@@ -345,6 +385,7 @@ export class PluginManager {
|
|
|
345
385
|
deactivate: typeof ret === "function" ? ret : undefined,
|
|
346
386
|
toolHandlers,
|
|
347
387
|
attachHandlers,
|
|
388
|
+
cwdHandlers,
|
|
348
389
|
agentToolUnsubscribers: unregisterTools,
|
|
349
390
|
});
|
|
350
391
|
console.log(`[plugin:${info.id}] activated (v${info.version ?? "?"})`);
|
|
@@ -354,6 +395,7 @@ export class PluginManager {
|
|
|
354
395
|
info: { ...info, error: err.message },
|
|
355
396
|
toolHandlers,
|
|
356
397
|
attachHandlers,
|
|
398
|
+
cwdHandlers,
|
|
357
399
|
});
|
|
358
400
|
console.error(`[plugin:${info.id}] activate failed:`, err);
|
|
359
401
|
}
|
package/dist/server/terminals.js
CHANGED
|
@@ -1230,11 +1230,11 @@ export const TERMINAL_TOOL_NAMES = [
|
|
|
1230
1230
|
/** System-prompt guidance teaching the model WHEN to prefer the terminal tools
|
|
1231
1231
|
* over one-shot bash. Without it models almost never pick them — bash returns
|
|
1232
1232
|
* complete output in a single call, so it always wins on convenience. */
|
|
1233
|
-
export const TERMINAL_TOOLS_GUIDANCE = `Persistent interactive terminal tools are available (terminal_create / terminal_list / terminal_close / terminal_input / terminal_key / terminal_read / terminal_wait). The one-shot bash tool stays the DEFAULT for ordinary commands - it runs once and returns the full output. Switch to the terminal tools only when:
|
|
1234
|
-
- The program is interactive or TUI-based (REPLs like python/node, vim/htop, installers asking y/n, anything waiting on stdin).
|
|
1235
|
-
- You start a long-running server or watcher and want to keep watching its output (terminal_read with waitMs), send keys to it later (e.g. interrupt via terminal_key with Ctrl+c), or block until a backgrounded command finishes without polling (terminal_wait).
|
|
1236
|
-
- The user explicitly asks you to work in the visible terminal panel.
|
|
1237
|
-
Liveness watchdog: terminals you touched (create/input/key) are monitored - if one goes silent with no new output while you are working (default 15s), an automatic system reminder is injected into the conversation. Treat it as a prompt to check that terminal (terminal_read), respond to an input prompt (terminal_input / terminal_key), or close it (terminal_close) if it is no longer needed.
|
|
1233
|
+
export const TERMINAL_TOOLS_GUIDANCE = `Persistent interactive terminal tools are available (terminal_create / terminal_list / terminal_close / terminal_input / terminal_key / terminal_read / terminal_wait). The one-shot bash tool stays the DEFAULT for ordinary commands - it runs once and returns the full output. Switch to the terminal tools only when:
|
|
1234
|
+
- The program is interactive or TUI-based (REPLs like python/node, vim/htop, installers asking y/n, anything waiting on stdin).
|
|
1235
|
+
- You start a long-running server or watcher and want to keep watching its output (terminal_read with waitMs), send keys to it later (e.g. interrupt via terminal_key with Ctrl+c), or block until a backgrounded command finishes without polling (terminal_wait).
|
|
1236
|
+
- The user explicitly asks you to work in the visible terminal panel.
|
|
1237
|
+
Liveness watchdog: terminals you touched (create/input/key) are monitored - if one goes silent with no new output while you are working (default 15s), an automatic system reminder is injected into the conversation. Treat it as a prompt to check that terminal (terminal_read), respond to an input prompt (terminal_input / terminal_key), or close it (terminal_close) if it is no longer needed.
|
|
1238
1238
|
Do NOT use them for simple one-shot commands; bash remains cheaper and simpler there.`;
|
|
1239
1239
|
/** Build the agent-facing persistent terminal tools for one conversation. */
|
|
1240
1240
|
export function makePersistentTerminalTools(terminals, cwd) {
|
package/extensions/webui.ts
CHANGED
|
@@ -1,192 +1,192 @@
|
|
|
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, ...rest] =
|
|
80
|
-
platform === "darwin"
|
|
81
|
-
? ["open", url]
|
|
82
|
-
: platform === "win32"
|
|
83
|
-
? ["cmd", "/c", "start", "", url]
|
|
84
|
-
: ["xdg-open", url];
|
|
85
|
-
// 无界面环境缺少 xdg-open 等打开器时,ENOENT 以异步 'error' 事件触发,
|
|
86
|
-
// try/catch 拦不住会崩掉整个进程 —— 必须挂 error 监听。
|
|
87
|
-
spawn(cmd, rest, { stdio: "ignore", detached: true })
|
|
88
|
-
.on("error", (err) => {
|
|
89
|
-
if ((err as NodeJS.ErrnoException)?.code === "ENOENT") {
|
|
90
|
-
console.warn(
|
|
91
|
-
`[webui] 未找到浏览器打开器 (${(err as NodeJS.ErrnoException).path || "command not found"}),请用 --no-browser 关闭自动打开`
|
|
92
|
-
);
|
|
93
|
-
} else {
|
|
94
|
-
console.warn("[webui] 打开浏览器失败:", err.message);
|
|
95
|
-
}
|
|
96
|
-
})
|
|
97
|
-
.unref();
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
export default function (pi: ExtensionAPI): void {
|
|
101
|
-
pi.registerCommand("webui", {
|
|
102
|
-
description: "启动本机 pi-web-ui Web 界面(/webui [--port N] [--cwd PATH] [--no-browser] | stop | status)",
|
|
103
|
-
handler: async (args: string, ctx: ExtensionCommandContext) => {
|
|
104
|
-
const sid = ctx.sessionManager.getSessionId();
|
|
105
|
-
const opts = parseArgs(args);
|
|
106
|
-
const action = (args.split(/\s+/)[0] || "start").toLowerCase();
|
|
107
|
-
|
|
108
|
-
// 停止
|
|
109
|
-
if (action === "stop" || action === "kill") {
|
|
110
|
-
const inst = running.get(sid);
|
|
111
|
-
if (!inst) {
|
|
112
|
-
ctx.ui.notify("没有正在运行的本机 pi-web-ui 服务器", "info");
|
|
113
|
-
return;
|
|
114
|
-
}
|
|
115
|
-
inst.proc.kill("SIGTERM");
|
|
116
|
-
running.delete(sid);
|
|
117
|
-
ctx.ui.notify(`已停止 pi-web-ui (${inst.url})`, "info");
|
|
118
|
-
return;
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
// 状态
|
|
122
|
-
if (action === "status") {
|
|
123
|
-
const inst = running.get(sid);
|
|
124
|
-
if (!inst) {
|
|
125
|
-
ctx.ui.notify("本机 pi-web-ui 未运行", "info");
|
|
126
|
-
return;
|
|
127
|
-
}
|
|
128
|
-
const alive = inst.proc.exitCode === null;
|
|
129
|
-
ctx.ui.notify(
|
|
130
|
-
alive ? `pi-web-ui 运行中 → ${inst.url}\n端口 ${inst.port} · cwd ${inst.cwd}` : `已退出(exit=${inst.proc.exitCode})`,
|
|
131
|
-
alive ? "info" : "warning",
|
|
132
|
-
);
|
|
133
|
-
return;
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
// 默认 start
|
|
137
|
-
if (action !== "start" && action !== "run") {
|
|
138
|
-
ctx.ui.notify(`未知动作 ${action}(可用 start|stop|status)`, "warning");
|
|
139
|
-
return;
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
// 已运行则提示
|
|
143
|
-
const existing = running.get(sid);
|
|
144
|
-
if (existing && existing.proc.exitCode === null) {
|
|
145
|
-
ctx.ui.notify(`pi-web-ui 已在运行 → ${existing.url}`, "info");
|
|
146
|
-
return;
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
// 检查是否已构建
|
|
150
|
-
if (!existsSync(SERVER_ENTRY)) {
|
|
151
|
-
ctx.ui.notify(
|
|
152
|
-
"缺少 dist/ 产物(当前安装未包含已构建前端)。请运行 `npm run build` 后重试,或用 pi-web-ui 官方 npm 包。",
|
|
153
|
-
"warning",
|
|
154
|
-
);
|
|
155
|
-
return;
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
const port = opts.port ?? (await findFreePort());
|
|
159
|
-
const cwd = opts.cwd ?? ctx.cwd;
|
|
160
|
-
const url = `http://localhost:${port}`;
|
|
161
|
-
|
|
162
|
-
const env = {
|
|
163
|
-
...process.env,
|
|
164
|
-
PORT: String(port),
|
|
165
|
-
PI_WEB_CWD: cwd,
|
|
166
|
-
...(process.env.PI_WEB_DATA_DIR ? {} : { PI_WEB_DATA_DIR: join(cwd, ".pi-web") }),
|
|
167
|
-
};
|
|
168
|
-
const proc = spawn(NODE, [SERVER_ENTRY], { cwd, env, stdio: "ignore", detached: true });
|
|
169
|
-
proc.unref();
|
|
170
|
-
running.set(sid, { proc, port, cwd, url });
|
|
171
|
-
|
|
172
|
-
ctx.ui.notify(`pi-web-ui 启动中 → ${url}\n端口 ${port} · cwd ${cwd}\n(几秒后可用,/webui status 查看)`);
|
|
173
|
-
|
|
174
|
-
if (!opts.noBrowser) await openBrowser(url);
|
|
175
|
-
|
|
176
|
-
// 进程退出时清理
|
|
177
|
-
proc.on("exit", () => {
|
|
178
|
-
if (running.get(sid)?.proc === proc) running.delete(sid);
|
|
179
|
-
});
|
|
180
|
-
},
|
|
181
|
-
});
|
|
182
|
-
|
|
183
|
-
// 会话结束清理子进程,避免孤儿
|
|
184
|
-
pi.on("session_shutdown", async (_event, ctx) => {
|
|
185
|
-
const sid = ctx.sessionManager.getSessionId();
|
|
186
|
-
const inst = running.get(sid);
|
|
187
|
-
if (inst && inst.proc.exitCode === null) {
|
|
188
|
-
inst.proc.kill("SIGTERM");
|
|
189
|
-
running.delete(sid);
|
|
190
|
-
}
|
|
191
|
-
});
|
|
192
|
-
}
|
|
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, ...rest] =
|
|
80
|
+
platform === "darwin"
|
|
81
|
+
? ["open", url]
|
|
82
|
+
: platform === "win32"
|
|
83
|
+
? ["cmd", "/c", "start", "", url]
|
|
84
|
+
: ["xdg-open", url];
|
|
85
|
+
// 无界面环境缺少 xdg-open 等打开器时,ENOENT 以异步 'error' 事件触发,
|
|
86
|
+
// try/catch 拦不住会崩掉整个进程 —— 必须挂 error 监听。
|
|
87
|
+
spawn(cmd, rest, { stdio: "ignore", detached: true })
|
|
88
|
+
.on("error", (err) => {
|
|
89
|
+
if ((err as NodeJS.ErrnoException)?.code === "ENOENT") {
|
|
90
|
+
console.warn(
|
|
91
|
+
`[webui] 未找到浏览器打开器 (${(err as NodeJS.ErrnoException).path || "command not found"}),请用 --no-browser 关闭自动打开`
|
|
92
|
+
);
|
|
93
|
+
} else {
|
|
94
|
+
console.warn("[webui] 打开浏览器失败:", err.message);
|
|
95
|
+
}
|
|
96
|
+
})
|
|
97
|
+
.unref();
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export default function (pi: ExtensionAPI): void {
|
|
101
|
+
pi.registerCommand("webui", {
|
|
102
|
+
description: "启动本机 pi-web-ui Web 界面(/webui [--port N] [--cwd PATH] [--no-browser] | stop | status)",
|
|
103
|
+
handler: async (args: string, ctx: ExtensionCommandContext) => {
|
|
104
|
+
const sid = ctx.sessionManager.getSessionId();
|
|
105
|
+
const opts = parseArgs(args);
|
|
106
|
+
const action = (args.split(/\s+/)[0] || "start").toLowerCase();
|
|
107
|
+
|
|
108
|
+
// 停止
|
|
109
|
+
if (action === "stop" || action === "kill") {
|
|
110
|
+
const inst = running.get(sid);
|
|
111
|
+
if (!inst) {
|
|
112
|
+
ctx.ui.notify("没有正在运行的本机 pi-web-ui 服务器", "info");
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
inst.proc.kill("SIGTERM");
|
|
116
|
+
running.delete(sid);
|
|
117
|
+
ctx.ui.notify(`已停止 pi-web-ui (${inst.url})`, "info");
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// 状态
|
|
122
|
+
if (action === "status") {
|
|
123
|
+
const inst = running.get(sid);
|
|
124
|
+
if (!inst) {
|
|
125
|
+
ctx.ui.notify("本机 pi-web-ui 未运行", "info");
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
const alive = inst.proc.exitCode === null;
|
|
129
|
+
ctx.ui.notify(
|
|
130
|
+
alive ? `pi-web-ui 运行中 → ${inst.url}\n端口 ${inst.port} · cwd ${inst.cwd}` : `已退出(exit=${inst.proc.exitCode})`,
|
|
131
|
+
alive ? "info" : "warning",
|
|
132
|
+
);
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// 默认 start
|
|
137
|
+
if (action !== "start" && action !== "run") {
|
|
138
|
+
ctx.ui.notify(`未知动作 ${action}(可用 start|stop|status)`, "warning");
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// 已运行则提示
|
|
143
|
+
const existing = running.get(sid);
|
|
144
|
+
if (existing && existing.proc.exitCode === null) {
|
|
145
|
+
ctx.ui.notify(`pi-web-ui 已在运行 → ${existing.url}`, "info");
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// 检查是否已构建
|
|
150
|
+
if (!existsSync(SERVER_ENTRY)) {
|
|
151
|
+
ctx.ui.notify(
|
|
152
|
+
"缺少 dist/ 产物(当前安装未包含已构建前端)。请运行 `npm run build` 后重试,或用 pi-web-ui 官方 npm 包。",
|
|
153
|
+
"warning",
|
|
154
|
+
);
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
const port = opts.port ?? (await findFreePort());
|
|
159
|
+
const cwd = opts.cwd ?? ctx.cwd;
|
|
160
|
+
const url = `http://localhost:${port}`;
|
|
161
|
+
|
|
162
|
+
const env = {
|
|
163
|
+
...process.env,
|
|
164
|
+
PORT: String(port),
|
|
165
|
+
PI_WEB_CWD: cwd,
|
|
166
|
+
...(process.env.PI_WEB_DATA_DIR ? {} : { PI_WEB_DATA_DIR: join(cwd, ".pi-web") }),
|
|
167
|
+
};
|
|
168
|
+
const proc = spawn(NODE, [SERVER_ENTRY], { cwd, env, stdio: "ignore", detached: true });
|
|
169
|
+
proc.unref();
|
|
170
|
+
running.set(sid, { proc, port, cwd, url });
|
|
171
|
+
|
|
172
|
+
ctx.ui.notify(`pi-web-ui 启动中 → ${url}\n端口 ${port} · cwd ${cwd}\n(几秒后可用,/webui status 查看)`);
|
|
173
|
+
|
|
174
|
+
if (!opts.noBrowser) await openBrowser(url);
|
|
175
|
+
|
|
176
|
+
// 进程退出时清理
|
|
177
|
+
proc.on("exit", () => {
|
|
178
|
+
if (running.get(sid)?.proc === proc) running.delete(sid);
|
|
179
|
+
});
|
|
180
|
+
},
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
// 会话结束清理子进程,避免孤儿
|
|
184
|
+
pi.on("session_shutdown", async (_event, ctx) => {
|
|
185
|
+
const sid = ctx.sessionManager.getSessionId();
|
|
186
|
+
const inst = running.get(sid);
|
|
187
|
+
if (inst && inst.proc.exitCode === null) {
|
|
188
|
+
inst.proc.kill("SIGTERM");
|
|
189
|
+
running.delete(sid);
|
|
190
|
+
}
|
|
191
|
+
});
|
|
192
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pi-web-ui",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.36.0",
|
|
4
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",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
package/themes/md-preview.css
CHANGED
|
@@ -961,6 +961,29 @@ body.panel-resizing {
|
|
|
961
961
|
font-size: 14px;
|
|
962
962
|
}
|
|
963
963
|
|
|
964
|
+
/* 全窗口拖放提示(issue #19):拖入文件时覆盖整个视口,drop 任意位置即附加。
|
|
965
|
+
pointer-events:none 保证 drop 落在下面的元素上再冒泡到 .app 的 onDrop。 */
|
|
966
|
+
.app-drop-overlay {
|
|
967
|
+
position: fixed;
|
|
968
|
+
inset: 0;
|
|
969
|
+
z-index: 300;
|
|
970
|
+
display: flex;
|
|
971
|
+
align-items: center;
|
|
972
|
+
justify-content: center;
|
|
973
|
+
background: color-mix(in srgb, var(--bg-elev) 72%, transparent);
|
|
974
|
+
border: 2px dashed var(--accent);
|
|
975
|
+
pointer-events: none;
|
|
976
|
+
}
|
|
977
|
+
.app-drop-overlay span {
|
|
978
|
+
padding: 14px 26px;
|
|
979
|
+
border: 1px dashed var(--accent);
|
|
980
|
+
border-radius: 12px;
|
|
981
|
+
background: var(--bg-elev2);
|
|
982
|
+
color: var(--text);
|
|
983
|
+
font-size: 16px;
|
|
984
|
+
box-shadow: 0 8px 32px rgba(0, 0, 0, 0.35);
|
|
985
|
+
}
|
|
986
|
+
|
|
964
987
|
.attach-range {
|
|
965
988
|
font-family: var(--mono);
|
|
966
989
|
font-size: 10.5px;
|
|
@@ -3340,11 +3363,81 @@ body.panel-resizing {
|
|
|
3340
3363
|
}
|
|
3341
3364
|
|
|
3342
3365
|
.msg-editor {
|
|
3366
|
+
position: relative;
|
|
3343
3367
|
display: flex;
|
|
3344
3368
|
flex-direction: column;
|
|
3345
3369
|
gap: 8px;
|
|
3346
3370
|
}
|
|
3347
3371
|
|
|
3372
|
+
/* 编辑重问的图片附件条:原图缩略图 + 新粘贴/拖入的图 */
|
|
3373
|
+
.msg-editor-images {
|
|
3374
|
+
display: flex;
|
|
3375
|
+
flex-wrap: wrap;
|
|
3376
|
+
gap: 6px;
|
|
3377
|
+
}
|
|
3378
|
+
.msg-editor-img {
|
|
3379
|
+
position: relative;
|
|
3380
|
+
display: inline-flex;
|
|
3381
|
+
width: 64px;
|
|
3382
|
+
height: 64px;
|
|
3383
|
+
border-radius: 8px;
|
|
3384
|
+
overflow: hidden;
|
|
3385
|
+
border: 1px solid var(--border);
|
|
3386
|
+
background: var(--bg-elev1);
|
|
3387
|
+
}
|
|
3388
|
+
.msg-editor-img img {
|
|
3389
|
+
width: 100%;
|
|
3390
|
+
height: 100%;
|
|
3391
|
+
object-fit: cover;
|
|
3392
|
+
display: block;
|
|
3393
|
+
}
|
|
3394
|
+
.msg-editor-img-remove {
|
|
3395
|
+
position: absolute;
|
|
3396
|
+
top: 2px;
|
|
3397
|
+
right: 2px;
|
|
3398
|
+
width: 18px;
|
|
3399
|
+
height: 18px;
|
|
3400
|
+
padding: 0;
|
|
3401
|
+
display: flex;
|
|
3402
|
+
align-items: center;
|
|
3403
|
+
justify-content: center;
|
|
3404
|
+
border: none;
|
|
3405
|
+
border-radius: 50%;
|
|
3406
|
+
cursor: pointer;
|
|
3407
|
+
font-size: 10px;
|
|
3408
|
+
line-height: 1;
|
|
3409
|
+
color: #fff;
|
|
3410
|
+
background: rgba(0, 0, 0, 0.55);
|
|
3411
|
+
}
|
|
3412
|
+
.msg-editor-img-remove:hover {
|
|
3413
|
+
background: rgba(220, 38, 38, 0.85);
|
|
3414
|
+
}
|
|
3415
|
+
.msg-editor-img-remove svg {
|
|
3416
|
+
width: 12px;
|
|
3417
|
+
height: 12px;
|
|
3418
|
+
}
|
|
3419
|
+
/* 拖拽悬停时整个编辑器高亮 */
|
|
3420
|
+
.msg-editor.drag-over .msg-editor-input {
|
|
3421
|
+
border-color: var(--accent);
|
|
3422
|
+
box-shadow: 0 0 0 2px rgba(139, 92, 246, 0.25);
|
|
3423
|
+
}
|
|
3424
|
+
.msg-editor.drag-over::after {
|
|
3425
|
+
content: "📎";
|
|
3426
|
+
position: absolute;
|
|
3427
|
+
inset: 0;
|
|
3428
|
+
display: flex;
|
|
3429
|
+
align-items: center;
|
|
3430
|
+
justify-content: center;
|
|
3431
|
+
font-size: 28px;
|
|
3432
|
+
pointer-events: none;
|
|
3433
|
+
z-index: 2;
|
|
3434
|
+
}
|
|
3435
|
+
/* 提示文案里的内联图标与文字对齐 */
|
|
3436
|
+
span.msg-editor-hint svg {
|
|
3437
|
+
vertical-align: -2px;
|
|
3438
|
+
margin-right: 4px;
|
|
3439
|
+
}
|
|
3440
|
+
|
|
3348
3441
|
.msg-editor-input {
|
|
3349
3442
|
width: 100%;
|
|
3350
3443
|
min-height: 48px;
|