pi-web-ui 0.30.0 → 0.32.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 +295 -295
- package/README.zh-CN.md +279 -279
- package/bin/pi-web-ui.mjs +0 -0
- 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 +67 -144
- package/dist/server/attachments.js +3 -3
- package/dist/server/client-state.js +2 -0
- package/dist/server/index.js +39 -48
- package/dist/server/model-admin.js +94 -0
- package/dist/server/protocol-version.js +1 -1
- package/dist/server/settings-service.js +9 -1
- package/dist/server/slash-commands.js +2 -0
- package/dist/server/terminals.js +19 -2
- package/dist/server/themes.js +20 -2
- package/extensions/webui.ts +192 -192
- package/package.json +1 -1
- package/themes/light.css +65 -0
- package/themes/md-preview.css +6911 -0
- package/themes/white.css +6972 -0
- package/web/dist/assets/{TerminalPanel-CMBWruhs.js → TerminalPanel-D9N7NOQl.js} +1 -1
- package/web/dist/assets/index-C9LigvHH.css +10 -0
- package/web/dist/assets/index-vcrKbPaa.js +19 -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-C8YkKrP3.css +0 -10
- package/web/dist/assets/index-DpzPCOcG.js +0 -19
|
@@ -99,6 +99,7 @@ export class SettingsService {
|
|
|
99
99
|
settings: {
|
|
100
100
|
promptMode: this.settings.promptMode,
|
|
101
101
|
customSystemPrompt: this.settings.customSystemPrompt,
|
|
102
|
+
terminalToolsEnabled: this.settings.terminalToolsEnabled,
|
|
102
103
|
visionBridgeEnabled: this.settings.visionBridgeEnabled,
|
|
103
104
|
visionBridgeModel: this.settings.visionBridgeModel,
|
|
104
105
|
visionBridgePromptMode: this.settings.visionBridgePromptMode,
|
|
@@ -139,7 +140,8 @@ export class SettingsService {
|
|
|
139
140
|
const needsReload = partial.promptMode !== undefined ||
|
|
140
141
|
partial.customSystemPrompt !== undefined ||
|
|
141
142
|
partial.disabledSkills !== undefined ||
|
|
142
|
-
partial.disabledExtensions !== undefined
|
|
143
|
+
partial.disabledExtensions !== undefined ||
|
|
144
|
+
partial.terminalToolsEnabled !== undefined;
|
|
143
145
|
if (partial.promptMode !== undefined)
|
|
144
146
|
this.settings.promptMode = partial.promptMode;
|
|
145
147
|
if (partial.customSystemPrompt !== undefined) {
|
|
@@ -151,6 +153,9 @@ export class SettingsService {
|
|
|
151
153
|
if (partial.disabledExtensions !== undefined) {
|
|
152
154
|
this.settings.disabledExtensions = partial.disabledExtensions;
|
|
153
155
|
}
|
|
156
|
+
if (partial.terminalToolsEnabled !== undefined) {
|
|
157
|
+
this.settings.terminalToolsEnabled = partial.terminalToolsEnabled;
|
|
158
|
+
}
|
|
154
159
|
if (partial.visionBridgeEnabled !== undefined) {
|
|
155
160
|
this.settings.visionBridgeEnabled = partial.visionBridgeEnabled;
|
|
156
161
|
}
|
|
@@ -187,6 +192,7 @@ export class SettingsService {
|
|
|
187
192
|
customSystemPrompt: this.settings.customSystemPrompt,
|
|
188
193
|
disabledSkills: [...this.settings.disabledSkills],
|
|
189
194
|
disabledExtensions: [...this.settings.disabledExtensions],
|
|
195
|
+
terminalToolsEnabled: this.settings.terminalToolsEnabled,
|
|
190
196
|
reviewPrompt: this.settings.reviewPrompt,
|
|
191
197
|
reviewDisabledSkills: [...this.settings.reviewDisabledSkills],
|
|
192
198
|
};
|
|
@@ -210,6 +216,8 @@ export class SettingsService {
|
|
|
210
216
|
customSystemPrompt: p.customSystemPrompt,
|
|
211
217
|
disabledSkills: [...p.disabledSkills],
|
|
212
218
|
disabledExtensions: [...p.disabledExtensions],
|
|
219
|
+
// 旧版持久化的预设可能没有该字段——保留当前值。
|
|
220
|
+
terminalToolsEnabled: p.terminalToolsEnabled ?? this.settings.terminalToolsEnabled,
|
|
213
221
|
reviewPrompt: p.reviewPrompt ?? this.settings.reviewPrompt,
|
|
214
222
|
reviewDisabledSkills: [
|
|
215
223
|
...(p.reviewDisabledSkills ?? this.settings.reviewDisabledSkills),
|
|
@@ -204,6 +204,8 @@ export class SlashCommandsService {
|
|
|
204
204
|
// Re-discovers extensions / skills / prompt templates from disk and
|
|
205
205
|
// re-pushes the picker catalog (the CLI's /reload semantics).
|
|
206
206
|
await this.host.getSession().reload();
|
|
207
|
+
// reload() 会把 custom 工具加回活跃集——重放设置门控(终端开关等)。
|
|
208
|
+
this.host.afterReload?.();
|
|
207
209
|
await this.push();
|
|
208
210
|
this.host.emit({
|
|
209
211
|
type: "notice",
|
package/dist/server/terminals.js
CHANGED
|
@@ -778,6 +778,23 @@ export class TerminalManager {
|
|
|
778
778
|
this.emitList();
|
|
779
779
|
}
|
|
780
780
|
}
|
|
781
|
+
/** Names of the agent-facing persistent-terminal tools(设置开关门控用)。 */
|
|
782
|
+
export const TERMINAL_TOOL_NAMES = [
|
|
783
|
+
"terminal_create",
|
|
784
|
+
"terminal_list",
|
|
785
|
+
"terminal_close",
|
|
786
|
+
"terminal_input",
|
|
787
|
+
"terminal_key",
|
|
788
|
+
"terminal_read",
|
|
789
|
+
];
|
|
790
|
+
/** System-prompt guidance teaching the model WHEN to prefer the terminal tools
|
|
791
|
+
* over one-shot bash. Without it models almost never pick them — bash returns
|
|
792
|
+
* complete output in a single call, so it always wins on convenience. */
|
|
793
|
+
export const TERMINAL_TOOLS_GUIDANCE = `Persistent interactive terminal tools are available (terminal_create / terminal_list / terminal_close / terminal_input / terminal_key / terminal_read). 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:
|
|
794
|
+
- The program is interactive or TUI-based (REPLs like python/node, vim/htop, installers asking y/n, anything waiting on stdin).
|
|
795
|
+
- You start a long-running server or watcher and want to keep watching its output (terminal_read with waitMs) or send keys to it later (e.g. interrupt via terminal_key with Ctrl+c).
|
|
796
|
+
- The user explicitly asks you to work in the visible terminal panel.
|
|
797
|
+
Do NOT use them for simple one-shot commands; bash remains cheaper and simpler there.`;
|
|
781
798
|
/** Build the agent-facing persistent terminal tools for one conversation. */
|
|
782
799
|
export function makePersistentTerminalTools(terminals, cwd) {
|
|
783
800
|
const result = (text, details = {}) => ({ content: [{ type: "text", text }], details });
|
|
@@ -789,8 +806,8 @@ export function makePersistentTerminalTools(terminals, cwd) {
|
|
|
789
806
|
defineTool({
|
|
790
807
|
name: "terminal_create",
|
|
791
808
|
label: "Create terminal",
|
|
792
|
-
description: "Create a named persistent interactive PTY in the current workspace. Use terminal_input or terminal_key to interact with it and terminal_read to inspect incremental output.",
|
|
793
|
-
promptSnippet: "
|
|
809
|
+
description: "Create a named persistent interactive PTY in the current workspace. Use terminal_input or terminal_key to interact with it and terminal_read to inspect incremental output. Prefer this over bash when the program is interactive/TUI-based (REPLs, vim/htop, y/n prompts), when starting a long-running server you want to keep observing or interrupt, or when the user asks to work in the visible terminal. For simple one-shot commands use bash instead.",
|
|
810
|
+
promptSnippet: "run interactive programs or long-running servers in a persistent visible PTY (multi-step: create → input/key → read)",
|
|
794
811
|
parameters: Type.Object({
|
|
795
812
|
terminalId: Type.String({ description: "Stable terminal name" }),
|
|
796
813
|
cwd: Type.Optional(Type.String({ description: "Workspace-relative directory" })),
|
package/dist/server/themes.js
CHANGED
|
@@ -7,10 +7,24 @@
|
|
|
7
7
|
* - builtin: <pkgRoot>/themes/*.css (ships with the npm package)
|
|
8
8
|
* - user : <dataDir>/themes/*.css (drop a css file here to add a theme)
|
|
9
9
|
*/
|
|
10
|
-
import { existsSync, readdirSync, statSync } from "node:fs";
|
|
10
|
+
import { existsSync, readdirSync, readFileSync, statSync } from "node:fs";
|
|
11
11
|
import { join } from "node:path";
|
|
12
12
|
/** Only simple file ids — no path traversal. */
|
|
13
13
|
const ID_RE = /^[A-Za-z0-9_-]+$/;
|
|
14
|
+
/** Display-name marker inside a theme css file (first lines):
|
|
15
|
+
* `/* theme-name: 中文名 *∕` — falls back to the file id when absent.
|
|
16
|
+
* Lets built-in AND user themes carry a human-readable label while the
|
|
17
|
+
* filename stays ASCII (id must match ID_RE). */
|
|
18
|
+
const THEME_NAME_RE = /\/\*\s*theme-name:\s*(.+?)\s*\*\//;
|
|
19
|
+
function readDisplayName(path, fallback) {
|
|
20
|
+
try {
|
|
21
|
+
const head = readFileSync(path, "utf8").slice(0, 300);
|
|
22
|
+
return head.match(THEME_NAME_RE)?.[1]?.trim() || fallback;
|
|
23
|
+
}
|
|
24
|
+
catch {
|
|
25
|
+
return fallback;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
14
28
|
export function listThemes(builtinDir, userDir) {
|
|
15
29
|
const scan = (dir, builtin) => {
|
|
16
30
|
if (!existsSync(dir))
|
|
@@ -19,7 +33,11 @@ export function listThemes(builtinDir, userDir) {
|
|
|
19
33
|
.filter((f) => f.endsWith(".css"))
|
|
20
34
|
.filter((f) => ID_RE.test(f.slice(0, -4)))
|
|
21
35
|
.sort()
|
|
22
|
-
.map((f) => ({
|
|
36
|
+
.map((f) => ({
|
|
37
|
+
id: f.slice(0, -4),
|
|
38
|
+
name: readDisplayName(join(dir, f), f.slice(0, -4)),
|
|
39
|
+
builtin,
|
|
40
|
+
}));
|
|
23
41
|
};
|
|
24
42
|
const builtin = scan(builtinDir, true);
|
|
25
43
|
const user = scan(userDir, false);
|
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.32.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/light.css
CHANGED
|
@@ -3926,6 +3926,46 @@ body.panel-resizing {
|
|
|
3926
3926
|
|
|
3927
3927
|
/* ----------------------------------------------------------- message roles */
|
|
3928
3928
|
|
|
3929
|
+
/* Queued prompts (steer = 插队 / followUp = 排队) — pending user bubbles at
|
|
3930
|
+
* the bottom of the real message list, styled like user messages but dimmed
|
|
3931
|
+
* with a tag until the server actually delivers them. */
|
|
3932
|
+
.queued-msg {
|
|
3933
|
+
display: flex;
|
|
3934
|
+
justify-content: flex-end;
|
|
3935
|
+
padding: 6px 16px;
|
|
3936
|
+
}
|
|
3937
|
+
.queued-bubble {
|
|
3938
|
+
max-width: min(85%, 720px);
|
|
3939
|
+
background: var(--accent-soft);
|
|
3940
|
+
border: 1px dashed rgba(139, 92, 246, 0.35);
|
|
3941
|
+
border-radius: 12px;
|
|
3942
|
+
padding: 8px 12px;
|
|
3943
|
+
opacity: 0.75;
|
|
3944
|
+
}
|
|
3945
|
+
.queued-tag {
|
|
3946
|
+
display: inline-block;
|
|
3947
|
+
font-size: 10px;
|
|
3948
|
+
line-height: 1.4;
|
|
3949
|
+
padding: 1px 7px;
|
|
3950
|
+
border-radius: 999px;
|
|
3951
|
+
margin-bottom: 4px;
|
|
3952
|
+
font-weight: 600;
|
|
3953
|
+
}
|
|
3954
|
+
.queued-tag.steer {
|
|
3955
|
+
color: var(--amber);
|
|
3956
|
+
background: rgba(251, 191, 36, 0.14);
|
|
3957
|
+
}
|
|
3958
|
+
.queued-tag.follow {
|
|
3959
|
+
color: var(--text-dim);
|
|
3960
|
+
background: var(--bg-elev2);
|
|
3961
|
+
}
|
|
3962
|
+
.queued-text {
|
|
3963
|
+
white-space: pre-wrap;
|
|
3964
|
+
word-break: break-word;
|
|
3965
|
+
font-size: 13px;
|
|
3966
|
+
color: var(--text);
|
|
3967
|
+
}
|
|
3968
|
+
|
|
3929
3969
|
.msg-user .msg-body {
|
|
3930
3970
|
background: var(--accent-soft);
|
|
3931
3971
|
border: 1px solid rgba(139, 92, 246, 0.25);
|
|
@@ -5360,6 +5400,31 @@ body.panel-resizing {
|
|
|
5360
5400
|
text-overflow: ellipsis;
|
|
5361
5401
|
}
|
|
5362
5402
|
|
|
5403
|
+
/* Switch */
|
|
5404
|
+
.set-uninstall {
|
|
5405
|
+
flex: none;
|
|
5406
|
+
display: inline-flex;
|
|
5407
|
+
align-items: center;
|
|
5408
|
+
gap: 4px;
|
|
5409
|
+
padding: 3px 8px;
|
|
5410
|
+
font-size: 11px;
|
|
5411
|
+
border-radius: 6px;
|
|
5412
|
+
border: 1px solid var(--border);
|
|
5413
|
+
background: transparent;
|
|
5414
|
+
color: var(--text-dim);
|
|
5415
|
+
cursor: pointer;
|
|
5416
|
+
transition: color 0.15s, border-color 0.15s;
|
|
5417
|
+
}
|
|
5418
|
+
.set-uninstall:hover {
|
|
5419
|
+
color: var(--red);
|
|
5420
|
+
border-color: var(--red);
|
|
5421
|
+
}
|
|
5422
|
+
.set-uninstall.confirm {
|
|
5423
|
+
color: var(--red);
|
|
5424
|
+
border-color: var(--red);
|
|
5425
|
+
font-weight: 600;
|
|
5426
|
+
}
|
|
5427
|
+
|
|
5363
5428
|
/* Switch */
|
|
5364
5429
|
.set-switch {
|
|
5365
5430
|
flex: none;
|