pi-web-ui 0.99.0 → 0.101.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/CHANGELOG.md +90 -1
- package/README.md +48 -45
- package/README.zh-CN.md +43 -40
- package/bin/pi-web-ui.mjs +172 -40
- package/dist/server/agent-formatters.js +191 -0
- package/dist/server/agent-service-daemon.js +1266 -0
- package/dist/server/agent-service.js +9851 -11892
- package/dist/server/approval-default-rules.js +158 -0
- package/dist/server/approval-rules.js +130 -184
- package/dist/server/ask-user-question-tool.js +94 -0
- package/dist/server/attachment-store.js +38 -1
- package/dist/server/attachments.js +55 -0
- package/dist/server/bash-tool.js +83 -0
- package/dist/server/bg-servers.js +82 -2
- package/dist/server/browser-page-tool.js +207 -0
- package/dist/server/claim-files-tool.js +4 -4
- package/dist/server/client-id-utils.js +51 -0
- package/dist/server/client-state.js +63 -1
- package/dist/server/compact-context-tool.js +10 -9
- package/dist/server/compaction-markers.js +34 -1
- package/dist/server/context-budget-runner.js +43 -0
- package/dist/server/conversation-read-tool.js +7 -8
- package/dist/server/conversation-touches.js +19 -0
- package/dist/server/dangling-tools.js +18 -5
- package/dist/server/delegate-mode.js +1 -0
- package/dist/server/delegate-task.js +10 -14
- package/dist/server/dispatch-domain-handlers.js +631 -0
- package/dist/server/dispatch-plugin-handlers.js +300 -0
- package/dist/server/dsh/dsh-agent-service.js +125 -13
- package/dist/server/edit-soft-tool.js +10 -12
- package/dist/server/eval-tool.js +4 -5
- package/dist/server/goal-evidence.js +180 -0
- package/dist/server/goal-service.js +49 -9
- package/dist/server/hashline-engine.js +199 -32
- package/dist/server/index.js +212 -944
- package/dist/server/left-panel-running.js +115 -0
- package/dist/server/load-tools-tool.js +100 -0
- package/dist/server/lsp-tool.js +50 -28
- package/dist/server/marker-service.js +53 -12
- package/dist/server/markers/builtins/action.js +108 -0
- package/dist/server/markers/index.js +4 -1
- package/dist/server/markers-list-tool.js +34 -0
- package/dist/server/market-service.js +277 -0
- package/dist/server/mcp-bridge.js +137 -30
- package/dist/server/mcp-hot-reload.js +84 -27
- package/dist/server/mcp-manager.js +215 -0
- package/dist/server/message-entry-resolver.js +32 -0
- package/dist/server/orphan-manager.js +44 -0
- package/dist/server/patch-remote-catalog.js +28 -10
- package/dist/server/patch-tool.js +9 -8
- package/dist/server/persona-chain.js +63 -0
- package/dist/server/plan-mode.js +9 -6
- package/dist/server/plan-update-tool.js +96 -0
- package/dist/server/plugin-api-catalog.js +30 -3
- package/dist/server/plugin-conversation-view.js +47 -0
- package/dist/server/plugin-facilities.js +13 -3
- package/dist/server/plugin-ui-slots.js +39 -0
- package/dist/server/plugins.js +183 -63
- package/dist/server/present-files-tool.js +10 -11
- package/dist/server/preset-fields.js +700 -0
- package/dist/server/preset-share.js +1179 -0
- package/dist/server/process-utils.js +30 -0
- package/dist/server/prompt-assembler.js +80 -0
- package/dist/server/prompt-composer.js +23 -9
- package/dist/server/protocol-version.js +1 -1
- package/dist/server/read-tool.js +8 -10
- package/dist/server/resolve-global-sdk.js +80 -18
- package/dist/server/schedule-agent-tool.js +147 -146
- package/dist/server/scm-commitmsg.js +74 -0
- package/dist/server/sdk-origin.js +125 -37
- package/dist/server/serialize.js +97 -7
- package/dist/server/session-branch.js +367 -0
- package/dist/server/session-parser.js +235 -0
- package/dist/server/session-search.js +158 -0
- package/dist/server/settings-service.js +115 -84
- package/dist/server/skill-content-filler.js +33 -0
- package/dist/server/skill-tool.js +3 -4
- package/dist/server/subagent-handoff.js +18 -0
- package/dist/server/subagent-helpers.js +88 -0
- package/dist/server/subagents.js +15 -22
- package/dist/server/terminals.js +171 -91
- package/dist/server/tool-approval.js +3 -1
- package/dist/server/tool-guards.js +527 -0
- package/dist/server/tool-host-factories.js +217 -0
- package/dist/server/tool-manager.js +84 -23
- package/dist/server/tool-prompt-overrides.js +158 -0
- package/dist/server/tool-prompts.js +42 -0
- package/dist/server/tool-signature.js +104 -0
- package/dist/server/wait-subscription-scan.js +46 -1
- package/dist/server/workspace-snapshot.js +60 -11
- package/extensions/webui.ts +90 -24
- package/package.json +5 -3
- package/plugin-sdk/README.md +25 -4
- package/plugin-sdk/import-check.mjs +210 -0
- package/plugin-sdk/index.d.ts +21 -1
- package/plugin-sdk/index.mjs +6 -0
- package/plugins/catalog.json +53 -2
- package/themes/aetheris.css +3 -1
- package/themes/translucent.css +8 -1
- package/themes/transparent.css +9 -1
- package/themes/zhupi-dark.css +1 -1
- package/themes/zhupi.css +1 -1
- package/web/dist/assets/{TerminalPanel-NerrJlTS.js → TerminalPanel-DD7I5_aC.js} +2 -2
- package/web/dist/assets/index-CpjA1YdT.css +1 -0
- package/web/dist/assets/index-nKn87VuM.js +678 -0
- package/web/dist/index.html +2 -2
- package/web/dist/sw.js +1 -1
- package/web/dist/assets/index-BdMaGCcs.css +0 -1
- package/web/dist/assets/index-ByCBN4Kt.js +0 -368
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* server/tool-signature.ts
|
|
3
|
+
*
|
|
4
|
+
* Generates compact parameter signatures with concise English hints (1-2 words / <= 20 chars)
|
|
5
|
+
* and produces lightweight shallow schemas (skeleton schemas) from tool JSON Schema / TypeBox definitions.
|
|
6
|
+
*
|
|
7
|
+
* Strictly pure English to keep model prompts concise and conform to hygiene guards.
|
|
8
|
+
*/
|
|
9
|
+
/** Clean and extract a concise English hint from a parameter's schema. */
|
|
10
|
+
export function extractConciseParamHint(prop) {
|
|
11
|
+
if (!prop || typeof prop !== "object")
|
|
12
|
+
return "";
|
|
13
|
+
const p = prop;
|
|
14
|
+
const desc = (p.description || "").trim();
|
|
15
|
+
if (desc) {
|
|
16
|
+
// Strip parenthetical notes like "(default 30)", "(1-indexed)"
|
|
17
|
+
const cleaned = desc.replace(/\([^)]*\)/g, "").trim();
|
|
18
|
+
// Take first clause before punctuation or newline
|
|
19
|
+
const clause = cleaned.split(/[,.;:—\n]/)[0]?.trim() || "";
|
|
20
|
+
// Strip leading articles/adjectives: "the", "optional", "a", "an"
|
|
21
|
+
const stripped = clause.replace(/^(the|optional|a|an)\s+/i, "").trim();
|
|
22
|
+
// Extract up to 2 words, max 20 chars, without truncating words
|
|
23
|
+
const words = stripped.split(/\s+/).filter(Boolean);
|
|
24
|
+
const picked = [];
|
|
25
|
+
let len = 0;
|
|
26
|
+
for (const w of words) {
|
|
27
|
+
if (picked.length >= 2)
|
|
28
|
+
break;
|
|
29
|
+
const nextLen = len === 0 ? w.length : len + 1 + w.length;
|
|
30
|
+
if (nextLen > 20) {
|
|
31
|
+
if (picked.length === 0)
|
|
32
|
+
picked.push(w.slice(0, 20));
|
|
33
|
+
break;
|
|
34
|
+
}
|
|
35
|
+
picked.push(w);
|
|
36
|
+
len = nextLen;
|
|
37
|
+
}
|
|
38
|
+
if (picked.length > 0)
|
|
39
|
+
return picked.join(" ");
|
|
40
|
+
}
|
|
41
|
+
if (p.type && typeof p.type === "string") {
|
|
42
|
+
return p.type;
|
|
43
|
+
}
|
|
44
|
+
return "";
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Format a compact parameter signature for a tool definition's parameters:
|
|
48
|
+
* e.g. `(steps: step list, activeStepId?: step ID)`
|
|
49
|
+
*/
|
|
50
|
+
export function formatCompactSignature(parameters) {
|
|
51
|
+
if (!parameters || typeof parameters !== "object")
|
|
52
|
+
return "";
|
|
53
|
+
const schema = parameters;
|
|
54
|
+
const props = schema.properties;
|
|
55
|
+
if (!props || typeof props !== "object" || Object.keys(props).length === 0) {
|
|
56
|
+
return "";
|
|
57
|
+
}
|
|
58
|
+
const required = new Set(Array.isArray(schema.required) ? schema.required : []);
|
|
59
|
+
const entries = Object.entries(props);
|
|
60
|
+
// Limit to first 6 properties to keep lines compact
|
|
61
|
+
const visibleEntries = entries.slice(0, 6);
|
|
62
|
+
const formatted = visibleEntries.map(([key, val]) => {
|
|
63
|
+
const isReq = required.has(key);
|
|
64
|
+
const hint = extractConciseParamHint(val);
|
|
65
|
+
return `${key}${isReq ? "" : "?"}${hint ? `: ${hint}` : ""}`;
|
|
66
|
+
});
|
|
67
|
+
if (entries.length > visibleEntries.length) {
|
|
68
|
+
formatted.push("...");
|
|
69
|
+
}
|
|
70
|
+
return `(${formatted.join(", ")})`;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Creates a lightweight "shallow schema" (skeleton schema) from a full TypeBox / JSON Schema.
|
|
74
|
+
* Retains only top-level property types and concise hints while stripping heavy nested
|
|
75
|
+
* enum descriptions, complex sub-schemas, and markdown examples.
|
|
76
|
+
*
|
|
77
|
+
* Reduces token footprint by ~80-90% while keeping native function calling fully compatible.
|
|
78
|
+
*/
|
|
79
|
+
export function createShallowSchema(parameters) {
|
|
80
|
+
if (!parameters || typeof parameters !== "object")
|
|
81
|
+
return undefined;
|
|
82
|
+
const schema = parameters;
|
|
83
|
+
const props = schema.properties;
|
|
84
|
+
if (!props || typeof props !== "object") {
|
|
85
|
+
return { type: schema.type || "object" };
|
|
86
|
+
}
|
|
87
|
+
const shallowProps = {};
|
|
88
|
+
for (const [key, val] of Object.entries(props)) {
|
|
89
|
+
if (!val || typeof val !== "object")
|
|
90
|
+
continue;
|
|
91
|
+
const p = val;
|
|
92
|
+
const hint = extractConciseParamHint(val);
|
|
93
|
+
shallowProps[key] = {
|
|
94
|
+
type: p.type || "string",
|
|
95
|
+
...(hint ? { description: hint } : {}),
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
const req = Array.isArray(schema.required) ? schema.required : undefined;
|
|
99
|
+
return {
|
|
100
|
+
type: "object",
|
|
101
|
+
properties: shallowProps,
|
|
102
|
+
...(req && req.length > 0 ? { required: req } : {}),
|
|
103
|
+
};
|
|
104
|
+
}
|
|
@@ -236,11 +236,51 @@ export function hasPendingWaitSubscription(options) {
|
|
|
236
236
|
}
|
|
237
237
|
return false;
|
|
238
238
|
}
|
|
239
|
+
/**
|
|
240
|
+
* 磁盘扫描:该会话在本进程里是否还有 running 的第三方后台任务(pi-background-tasks 的 bg_run)。
|
|
241
|
+
* 任务元数据位于 <cwd>/.pi/tasks/<会话UUID>-<pid>/<taskId>.json;本进程起的任务目录 pid
|
|
242
|
+
* 必为 process.pid,因此只看这一个目录——上次进程崩溃遗留的 status:"running" 不算活跃证据。
|
|
243
|
+
* 只要有 running 任务,移出/置换/切目录都会 dispose 运行时、把它 SIGTERM 掉且永不补通知(#576),
|
|
244
|
+
* 故与 pending wake 同级保留。任何 I/O / 解析错误按「无证据」处理(fail-open)。
|
|
245
|
+
*/
|
|
246
|
+
export function hasRunningBackgroundTask(options) {
|
|
247
|
+
const { cwd, sessionFile } = options;
|
|
248
|
+
if (!cwd || !sessionFile)
|
|
249
|
+
return false;
|
|
250
|
+
const sessionUuid = path
|
|
251
|
+
.basename(sessionFile)
|
|
252
|
+
.replace(/\.jsonl$/i, "")
|
|
253
|
+
.split("_")
|
|
254
|
+
.pop();
|
|
255
|
+
if (!sessionUuid)
|
|
256
|
+
return false;
|
|
257
|
+
const ownDir = path.join(cwd, ".pi", "tasks", `${sessionUuid}-${options.pid ?? process.pid}`);
|
|
258
|
+
let files;
|
|
259
|
+
try {
|
|
260
|
+
files = readdirSync(ownDir);
|
|
261
|
+
}
|
|
262
|
+
catch {
|
|
263
|
+
return false; // 目录不存在 / 不可读 → 无证据
|
|
264
|
+
}
|
|
265
|
+
for (const f of files) {
|
|
266
|
+
if (!f.endsWith(".json"))
|
|
267
|
+
continue;
|
|
268
|
+
try {
|
|
269
|
+
const task = JSON.parse(readFileSync(path.join(ownDir, f), "utf-8"));
|
|
270
|
+
if (task && task.status === "running")
|
|
271
|
+
return true;
|
|
272
|
+
}
|
|
273
|
+
catch {
|
|
274
|
+
// 缺失 / 损坏 → 无证据(fail-open)
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
return false;
|
|
278
|
+
}
|
|
239
279
|
/**
|
|
240
280
|
* 纯函数版置换决策:true = 保留(不得 dispose),false = 调用方可释放。
|
|
241
281
|
* Pure decision core of displaceActive(): true = retain, false = may dispose.
|
|
242
282
|
* 顺序与 displaceActive 保持一致:pin → review/wizard → streaming → terminals →
|
|
243
|
-
* active subagent run → pending wake → listed+continued(「打开后继续过」的会话也保留)。
|
|
283
|
+
* active subagent run → pending wake → running background task → listed+continued(「打开后继续过」的会话也保留)。
|
|
244
284
|
*/
|
|
245
285
|
export function shouldRetainActive(input) {
|
|
246
286
|
if (input.pinned)
|
|
@@ -259,6 +299,11 @@ export function shouldRetainActive(input) {
|
|
|
259
299
|
const hasPendingWake = typeof input.hasPendingWake === "function" ? input.hasPendingWake() : input.hasPendingWake;
|
|
260
300
|
if (hasPendingWake)
|
|
261
301
|
return true;
|
|
302
|
+
const hasBgTask = typeof input.hasRunningBackgroundTask === "function"
|
|
303
|
+
? input.hasRunningBackgroundTask()
|
|
304
|
+
: (input.hasRunningBackgroundTask ?? false);
|
|
305
|
+
if (hasBgTask)
|
|
306
|
+
return true;
|
|
262
307
|
if (input.listed && input.promptedSinceActive)
|
|
263
308
|
return true;
|
|
264
309
|
return false;
|
|
@@ -5,9 +5,11 @@
|
|
|
5
5
|
*
|
|
6
6
|
* 每次用户发起新任务或运行关键操作前,为当前工作区记录轻量快照引用(Git commit SHA)。
|
|
7
7
|
* 机制:
|
|
8
|
-
* - 利用独立的临时 GIT_INDEX_FILE,通过 `git add -A
|
|
8
|
+
* - 利用独立的临时 GIT_INDEX_FILE,通过 `git add -A -- .` + `git write-tree` + `git commit-tree` 生成独立 commit;
|
|
9
|
+
* - 快照只覆盖对话 cwd 的**子树**(而非整个工作区),commit-tree 的顶层因此只有该子目录一个条目;
|
|
9
10
|
* - 绝不改变用户现有的 HEAD、branch 指针、工作区文件或 .git/index;
|
|
10
|
-
* - 回滚时若启用 restoreWorkspace,通过 `git checkout <snapshotRef> --
|
|
11
|
+
* - 回滚时若启用 restoreWorkspace,通过 `git checkout <snapshotRef> -- <prefix>` 与 `git clean -fd -- <prefix>`
|
|
12
|
+
* 只还原/清理 cwd 子树,绝不触碰同仓库的兄弟目录。
|
|
11
13
|
*/
|
|
12
14
|
import { execFile } from "node:child_process";
|
|
13
15
|
import { randomUUID } from "node:crypto";
|
|
@@ -46,8 +48,9 @@ export async function createWorkspaceSnapshot(cwd) {
|
|
|
46
48
|
const tempIndex = join(gitDir, `temp-index-snapshot-${randomUUID()}`);
|
|
47
49
|
const env = { GIT_INDEX_FILE: tempIndex };
|
|
48
50
|
try {
|
|
49
|
-
// 1.
|
|
50
|
-
|
|
51
|
+
// 1. 将 cwd 子树(而非整个工作区)的当前改动(含未跟踪与新增)记录到临时 index;
|
|
52
|
+
// 暂存路径相对仓库根,write-tree 顶层因此只有该子目录一个条目
|
|
53
|
+
await runGit(cwd, ["add", "-A", "--", "."], env);
|
|
51
54
|
// 2. 写入 tree 对象
|
|
52
55
|
const tree = await runGit(cwd, ["write-tree"], env);
|
|
53
56
|
if (!tree)
|
|
@@ -85,11 +88,16 @@ export async function createWorkspaceSnapshot(cwd) {
|
|
|
85
88
|
}
|
|
86
89
|
}
|
|
87
90
|
/**
|
|
88
|
-
*
|
|
91
|
+
* 将工作区物理文件还原到指定的快照状态,作用范围严格限定在 cwd 子树内。
|
|
89
92
|
*
|
|
90
93
|
* 执行步骤:
|
|
91
|
-
*
|
|
92
|
-
*
|
|
94
|
+
* - 仓库根 cwd:使用原子 `git read-tree -u --reset` + `git clean -fd` 整仓还原(天然支持空仓库快照);
|
|
95
|
+
* - 子目录 cwd(pathspec 以 `:(top)` 锚定到仓库根的 cwd 前缀):
|
|
96
|
+
* 1. `git rm -r --cached -- <pathspec>` 把 cwd 子树移出 index(工作区文件不动),使快照中
|
|
97
|
+
* 不存在的文件退回未跟踪状态——这一步让 checkout 之外的「快照后新增且已 add」的文件也能被回滚;
|
|
98
|
+
* 2. `git checkout <snapshotRef> -- <pathspec>` 把快照内容写回 index 与工作区(恢复被修改/删除的跟踪文件,快照无该子树文件时容错跳过);
|
|
99
|
+
* 3. `git clean -fd -- <pathspec>` 清理快照之后新增的未跟踪文件与空目录。
|
|
100
|
+
* 同仓库的兄弟目录在以上任何一步都不受影响。
|
|
93
101
|
*/
|
|
94
102
|
export async function restoreWorkspaceSnapshot(cwd, snapshotRef) {
|
|
95
103
|
const gitDir = await gitDirOf(cwd);
|
|
@@ -99,10 +107,33 @@ export async function restoreWorkspaceSnapshot(cwd, snapshotRef) {
|
|
|
99
107
|
try {
|
|
100
108
|
// 验证 snapshotRef 合法性
|
|
101
109
|
await runGit(cwd, ["rev-parse", "--verify", snapshotRef]);
|
|
102
|
-
//
|
|
103
|
-
await runGit(cwd, ["
|
|
104
|
-
|
|
105
|
-
|
|
110
|
+
// cwd 相对仓库根的前缀(子目录形如 `proj/`,仓库根为空串)。
|
|
111
|
+
const prefix = await runGit(cwd, ["rev-parse", "--show-prefix"]);
|
|
112
|
+
if (!prefix) {
|
|
113
|
+
// 仓库根 cwd:整仓还原,使用原子 read-tree --reset,旧实现经过长期验证且天然支持空仓库快照
|
|
114
|
+
await runGit(cwd, ["read-tree", "-u", "--reset", snapshotRef]);
|
|
115
|
+
await runGit(cwd, ["clean", "-fd"]);
|
|
116
|
+
return { success: true };
|
|
117
|
+
}
|
|
118
|
+
// 子目录 cwd:作用域收敛到该子树,不误伤同仓兄弟目录与仓库根
|
|
119
|
+
const pathspec = `:(top)${prefix}`;
|
|
120
|
+
// 1. 把 cwd 子树移出 index(不删除工作区文件),快照里不存在的文件由此退回未跟踪状态
|
|
121
|
+
await runGit(cwd, ["rm", "-r", "--cached", "--quiet", "--ignore-unmatch", "--", pathspec]);
|
|
122
|
+
// 2. 从快照树把 cwd 子树写回 index 与工作区(恢复被修改与被删除的跟踪文件)
|
|
123
|
+
// 注:若快照时刻该子树为空(快照树内无匹配文件),checkout 会报
|
|
124
|
+
// "did not match any file(s) known to git";此时快照树内本无文件,捕获忽略即可,
|
|
125
|
+
// 第 3 步 clean 会清理掉新增文件完成空状态还原。
|
|
126
|
+
try {
|
|
127
|
+
await runGit(cwd, ["checkout", snapshotRef, "--", pathspec]);
|
|
128
|
+
}
|
|
129
|
+
catch (checkoutErr) {
|
|
130
|
+
const errMsg = String(checkoutErr);
|
|
131
|
+
if (!errMsg.includes("did not match any file(s) known to git")) {
|
|
132
|
+
throw checkoutErr;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
// 3. 清理快照之后新增的未跟踪文件与空目录(含第 1 步退回未跟踪的那些)
|
|
136
|
+
await runGit(cwd, ["clean", "-fd", "--", pathspec]);
|
|
106
137
|
return { success: true };
|
|
107
138
|
}
|
|
108
139
|
catch (err) {
|
|
@@ -111,3 +142,21 @@ export async function restoreWorkspaceSnapshot(cwd, snapshotRef) {
|
|
|
111
142
|
return { success: false, error: msg };
|
|
112
143
|
}
|
|
113
144
|
}
|
|
145
|
+
/**
|
|
146
|
+
* 查找与指定消息 entryId/时间戳最匹配的工作区物理快照:
|
|
147
|
+
* 优先匹配 exact entryId,其次查找该 entry 时间戳之前(或 +2000ms 内)的最近一份快照。
|
|
148
|
+
*/
|
|
149
|
+
export function findMatchingWorkspaceSnapshot(snapshots, entryId, entryTs = 0) {
|
|
150
|
+
if (!snapshots || snapshots.length === 0)
|
|
151
|
+
return undefined;
|
|
152
|
+
if (entryId) {
|
|
153
|
+
const exact = snapshots.find((s) => s.entryId === entryId);
|
|
154
|
+
if (exact)
|
|
155
|
+
return exact;
|
|
156
|
+
}
|
|
157
|
+
const eligible = snapshots.filter((s) => entryTs === 0 || s.timestamp <= entryTs + 2000);
|
|
158
|
+
if (eligible.length > 0) {
|
|
159
|
+
return eligible[eligible.length - 1];
|
|
160
|
+
}
|
|
161
|
+
return snapshots[0];
|
|
162
|
+
}
|
package/extensions/webui.ts
CHANGED
|
@@ -18,10 +18,10 @@
|
|
|
18
18
|
*/
|
|
19
19
|
|
|
20
20
|
import { spawn, execSync } from "node:child_process";
|
|
21
|
-
import { existsSync } from "node:fs";
|
|
21
|
+
import { closeSync, existsSync, mkdirSync, openSync } from "node:fs";
|
|
22
22
|
import { homedir } from "node:os";
|
|
23
23
|
import { dirname, join, resolve } from "node:path";
|
|
24
|
-
import { fileURLToPath } from "node:url";
|
|
24
|
+
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
25
25
|
import net from "node:net";
|
|
26
26
|
import type { ExtensionAPI, ExtensionCommandContext } from "@earendil-works/pi-coding-agent";
|
|
27
27
|
|
|
@@ -92,11 +92,28 @@ interface RunningServer {
|
|
|
92
92
|
port: number;
|
|
93
93
|
cwd: string;
|
|
94
94
|
url: string;
|
|
95
|
+
logFile?: string;
|
|
96
|
+
stop: () => void;
|
|
95
97
|
}
|
|
96
98
|
|
|
97
99
|
// 会话 → 运行实例(模块级 Map;每会话一个会话对象,无需清理全局)
|
|
98
100
|
const running = new Map<string, RunningServer>();
|
|
99
101
|
|
|
102
|
+
/**
|
|
103
|
+
* 组装子进程参数。
|
|
104
|
+
* hookPath(resolve-global-sdk.js)必须以 file:// URL 形式传给 --import,
|
|
105
|
+
* 否则在 Windows 上传裸盘符路径(如 C:\...)会触发 Node ESM 的 ERR_UNSUPPORTED_ESM_URL_SCHEME 异常(issue #420 / #580)。
|
|
106
|
+
*/
|
|
107
|
+
export function buildNodeArgs(
|
|
108
|
+
entry: string,
|
|
109
|
+
hookPath?: string,
|
|
110
|
+
hookExists = hookPath ? existsSync(hookPath) : false,
|
|
111
|
+
): string[] {
|
|
112
|
+
return hookPath && hookExists ? ["--import", pathToFileURL(hookPath).href, entry] : [entry];
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export { running };
|
|
116
|
+
|
|
100
117
|
/** 找一个空闲端口 */
|
|
101
118
|
function findFreePort(from = 8787): Promise<number> {
|
|
102
119
|
return new Promise((resolve_, reject) => {
|
|
@@ -162,11 +179,13 @@ export default function (pi: ExtensionAPI): void {
|
|
|
162
179
|
// 停止
|
|
163
180
|
if (action === "stop" || action === "kill") {
|
|
164
181
|
const inst = running.get(sid);
|
|
165
|
-
|
|
182
|
+
const alive = inst && inst.proc.exitCode === null && inst.proc.signalCode === null;
|
|
183
|
+
if (!inst || !alive) {
|
|
184
|
+
running.delete(sid);
|
|
166
185
|
ctx.ui.notify("没有正在运行的本机 pi-web-ui 服务器", "info");
|
|
167
186
|
return;
|
|
168
187
|
}
|
|
169
|
-
inst.
|
|
188
|
+
inst.stop();
|
|
170
189
|
running.delete(sid);
|
|
171
190
|
ctx.ui.notify(`已停止 pi-web-ui (${inst.url})`, "info");
|
|
172
191
|
return;
|
|
@@ -179,13 +198,21 @@ export default function (pi: ExtensionAPI): void {
|
|
|
179
198
|
ctx.ui.notify("本机 pi-web-ui 未运行", "info");
|
|
180
199
|
return;
|
|
181
200
|
}
|
|
182
|
-
const alive = inst.proc.exitCode === null;
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
201
|
+
const alive = inst.proc.exitCode === null && inst.proc.signalCode === null;
|
|
202
|
+
if (alive) {
|
|
203
|
+
ctx.ui.notify(
|
|
204
|
+
`pi-web-ui 运行中 → ${inst.url}\n端口 ${inst.port} · cwd ${inst.cwd}${inst.logFile ? `\n日志: ${inst.logFile}` : ""}`,
|
|
205
|
+
"info",
|
|
206
|
+
);
|
|
207
|
+
} else {
|
|
208
|
+
const exit =
|
|
209
|
+
inst.proc.exitCode !== null
|
|
210
|
+
? `exit=${inst.proc.exitCode}`
|
|
211
|
+
: inst.proc.signalCode
|
|
212
|
+
? `signal=${inst.proc.signalCode}`
|
|
213
|
+
: "unknown";
|
|
214
|
+
ctx.ui.notify(`pi-web-ui 已退出 (${exit})${inst.logFile ? ` · 日志: ${inst.logFile}` : ""}`, "warning");
|
|
215
|
+
}
|
|
189
216
|
return;
|
|
190
217
|
}
|
|
191
218
|
|
|
@@ -197,7 +224,7 @@ export default function (pi: ExtensionAPI): void {
|
|
|
197
224
|
|
|
198
225
|
// 已运行则提示
|
|
199
226
|
const existing = running.get(sid);
|
|
200
|
-
if (existing && existing.proc.exitCode === null) {
|
|
227
|
+
if (existing && existing.proc.exitCode === null && existing.proc.signalCode === null) {
|
|
201
228
|
ctx.ui.notify(`pi-web-ui 已在运行 → ${existing.url}`, "info");
|
|
202
229
|
return;
|
|
203
230
|
}
|
|
@@ -226,34 +253,73 @@ export default function (pi: ExtensionAPI): void {
|
|
|
226
253
|
}
|
|
227
254
|
} catch {}
|
|
228
255
|
|
|
256
|
+
const dataDir = process.env.PI_WEB_DATA_DIR ? resolve(process.env.PI_WEB_DATA_DIR) : join(cwd, ".pi-web");
|
|
229
257
|
const env = {
|
|
230
258
|
...process.env,
|
|
231
259
|
PORT: String(port),
|
|
232
260
|
PI_WEB_PORT: String(port), // server/index.js 读取 PI_WEB_PORT
|
|
233
261
|
PI_WEB_CWD: cwd,
|
|
234
262
|
...(hostSdkDir ? { PI_WEB_SDK_DIR: hostSdkDir } : {}),
|
|
235
|
-
|
|
263
|
+
PI_WEB_DATA_DIR: dataDir,
|
|
236
264
|
};
|
|
237
265
|
const hookPath = join(PKG_ROOT, "dist", "server", "resolve-global-sdk.js");
|
|
238
|
-
const nodeArgs =
|
|
266
|
+
const nodeArgs = buildNodeArgs(SERVER_ENTRY, hookPath);
|
|
267
|
+
|
|
268
|
+
let logFile: string | undefined;
|
|
269
|
+
let logFd: number | "ignore" = "ignore";
|
|
270
|
+
try {
|
|
271
|
+
mkdirSync(dataDir, { recursive: true });
|
|
272
|
+
logFile = join(dataDir, "webui.log");
|
|
273
|
+
logFd = openSync(logFile, "a");
|
|
274
|
+
} catch {
|
|
275
|
+
/* 无法创建日志目录或文件时静默回退 ignore */
|
|
276
|
+
}
|
|
277
|
+
|
|
239
278
|
const proc = spawn(NODE, nodeArgs, {
|
|
240
279
|
cwd,
|
|
241
280
|
env,
|
|
242
|
-
stdio: "ignore",
|
|
281
|
+
stdio: ["ignore", logFd, logFd],
|
|
243
282
|
detached: process.platform !== "win32",
|
|
244
283
|
windowsHide: true,
|
|
245
284
|
});
|
|
285
|
+
if (typeof logFd === "number") {
|
|
286
|
+
try {
|
|
287
|
+
closeSync(logFd);
|
|
288
|
+
} catch {}
|
|
289
|
+
}
|
|
246
290
|
proc.unref();
|
|
247
|
-
running.set(sid, { proc, port, cwd, url });
|
|
248
291
|
|
|
249
|
-
|
|
292
|
+
let stoppedManually = false;
|
|
293
|
+
const startTime = Date.now();
|
|
294
|
+
const inst: RunningServer = {
|
|
295
|
+
proc,
|
|
296
|
+
port,
|
|
297
|
+
cwd,
|
|
298
|
+
url,
|
|
299
|
+
logFile,
|
|
300
|
+
stop: () => {
|
|
301
|
+
stoppedManually = true;
|
|
302
|
+
proc.kill("SIGTERM");
|
|
303
|
+
},
|
|
304
|
+
};
|
|
305
|
+
running.set(sid, inst);
|
|
250
306
|
|
|
251
|
-
|
|
307
|
+
proc.on("error", (err) => {
|
|
308
|
+
ctx.ui.notify(`pi-web-ui 启动失败: ${err.message}${logFile ? `\n日志: ${logFile}` : ""}`, "error");
|
|
309
|
+
});
|
|
252
310
|
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
311
|
+
proc.on("exit", (code, signal) => {
|
|
312
|
+
const duration = Date.now() - startTime;
|
|
313
|
+
// 启动后 15 秒内非手动停止的异常退出,向用户告警
|
|
314
|
+
if (!stoppedManually && (code !== 0 || signal !== null) && duration < 15000) {
|
|
315
|
+
const reason = code !== null ? `exit=${code}` : `signal=${signal}`;
|
|
316
|
+
ctx.ui.notify(`pi-web-ui 启动异常退出 (${reason})。\n${logFile ? `查看日志: ${logFile}` : ""}`, "error");
|
|
317
|
+
}
|
|
256
318
|
});
|
|
319
|
+
|
|
320
|
+
ctx.ui.notify(`pi-web-ui 启动中 → ${url}\n端口 ${port} · cwd ${cwd}\n(几秒后可用,/webui status 查看)`);
|
|
321
|
+
|
|
322
|
+
if (!opts.noBrowser) await openBrowser(url);
|
|
257
323
|
},
|
|
258
324
|
});
|
|
259
325
|
|
|
@@ -261,9 +327,9 @@ export default function (pi: ExtensionAPI): void {
|
|
|
261
327
|
pi.on("session_shutdown", async (_event, ctx) => {
|
|
262
328
|
const sid = ctx.sessionManager.getSessionId();
|
|
263
329
|
const inst = running.get(sid);
|
|
264
|
-
if (inst && inst.proc.exitCode === null) {
|
|
265
|
-
inst.
|
|
266
|
-
running.delete(sid);
|
|
330
|
+
if (inst && inst.proc.exitCode === null && inst.proc.signalCode === null) {
|
|
331
|
+
inst.stop();
|
|
267
332
|
}
|
|
333
|
+
running.delete(sid);
|
|
268
334
|
});
|
|
269
335
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pi-web-ui",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.101.0",
|
|
4
4
|
"description": "Just open your browser to get all your work done — browser cockpit for AI coding agents (chat, code, files, terminal, Git in one tab)",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": {
|
|
@@ -85,17 +85,19 @@
|
|
|
85
85
|
"dependencies": {
|
|
86
86
|
"@deepseek-ai/dsh-sdk-jsonrpc-server": "^0.1.5-rc.3",
|
|
87
87
|
"@deepseek-ai/dsh-sdk-protocol": "^0.1.5-rc.3",
|
|
88
|
-
"@earendil-works/pi-coding-agent": ">=0.85.1",
|
|
89
88
|
"compression": "^1.8.2",
|
|
90
89
|
"electron-updater": "^6.8.9",
|
|
91
90
|
"express": "^5.2.1",
|
|
92
91
|
"node-pty": "^1.1.0",
|
|
93
92
|
"tar": "^7.5.22",
|
|
94
|
-
"typebox": "^1.3.34",
|
|
95
93
|
"undici": "^8.9.0",
|
|
96
94
|
"ws": "^8.21.3",
|
|
97
95
|
"yauzl": "^3.4.0"
|
|
98
96
|
},
|
|
97
|
+
"optionalDependencies": {
|
|
98
|
+
"@earendil-works/pi-coding-agent": ">=1.1.0",
|
|
99
|
+
"typebox": "^1.3.34"
|
|
100
|
+
},
|
|
99
101
|
"peerDependencies": {
|
|
100
102
|
"@earendil-works/pi-coding-agent": "*",
|
|
101
103
|
"typebox": "*"
|
package/plugin-sdk/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
## 脚手架:`pi-web-ui plugin create`
|
|
4
4
|
|
|
5
|
-
不用手拷文件,一条命令生成最小可跑骨架(`manifest.json` + `index.mjs` + `client/entry.mjs` + `README.md`,SDK 自动拷进 `sdk/`):
|
|
5
|
+
不用手拷文件,一条命令生成最小可跑骨架(`manifest.json` + `index.mjs` + `client/entry.mjs` + `README.md`,SDK 自动拷进 `sdk/` 与 `client/sdk/`):
|
|
6
6
|
|
|
7
7
|
```bash
|
|
8
8
|
pi-web-ui plugin create my-plugin --template minimal # 零权限,可直接激活
|
|
@@ -11,7 +11,9 @@ pi-web-ui plugin create my-plugin --template agent-tool # registerAgentTool 示
|
|
|
11
11
|
pi-web-ui plugin create my-plugin --template renderer # view:false + renderers 示例
|
|
12
12
|
```
|
|
13
13
|
|
|
14
|
-
常用选项:`--dir <插件父目录>`(默认 `<dataDir>/plugins`,也可用 `--data-dir` 指定数据目录)、`--force`(目标已存在时覆盖)。生成后会自动做一次 manifest 基础校验 + 生成文件的 `node --check
|
|
14
|
+
常用选项:`--dir <插件父目录>`(默认 `<dataDir>/plugins`,也可用 `--data-dir` 指定数据目录)、`--force`(目标已存在时覆盖)。生成后会自动做一次 manifest 基础校验 + 生成文件的 `node --check` + **静态 import 自检**,有 warning 会直接打印;生效只需刷新浏览器(或发 `plugins_reload`),未运行则下次启动生效。
|
|
15
|
+
|
|
16
|
+
> **为什么 SDK 拷两份**:宿主**只暴露 `/plugins/<id>/client/*`**(`server/plugins.ts` 的 `resolvePluginClientFile` 把根钉在 `client/`)—— 客户端 bundle 与它的依赖必须都在 `client/` 内,越界的相对 import 会让浏览器报 `Failed to fetch dynamically imported module` 且**整个 bundle 都不执行**(插件页空白,而服务端一切正常、`active:true`)。服务端入口则是同进程 `import()`,不受暴露面限制。所以:服务端用 `<plugin>/sdk/`、客户端用 `<plugin>/client/sdk/`,`client/entry.mjs` 里只能写 `import … from "./sdk/index.mjs"`。两份都拷 `index.mjs` + `client-utils.mjs`(前者末尾 `export * from "./client-utils.mjs"`,少一份连服务端都加载不了)。
|
|
15
17
|
|
|
16
18
|
---
|
|
17
19
|
|
|
@@ -21,7 +23,7 @@ pi-web-ui plugin create my-plugin --template renderer # view:false + renderers
|
|
|
21
23
|
|
|
22
24
|
## 怎么用
|
|
23
25
|
|
|
24
|
-
1. 把 `index.mjs`(和要补全就把 `index.d.ts`)拷进你的插件目录(如 `my-plugin/sdk
|
|
26
|
+
1. 把 `index.mjs` + `client-utils.mjs`(和要补全就把 `index.d.ts`)拷进你的插件目录(如 `my-plugin/sdk/`);**客户端要用到的那份再拷一份到 `my-plugin/client/sdk/`**(宿主只服务 `client/*`,见上文「为什么 SDK 拷两份」);
|
|
25
27
|
2. 服务端入口:
|
|
26
28
|
|
|
27
29
|
```js
|
|
@@ -109,7 +111,9 @@ describe("my-plugin", () => {
|
|
|
109
111
|
- 注册/订阅类返回注销函数,传进来的 handler 存进 `host.mock.handlers[method]`,用 `host.mock.emit(method, ...args)` 同步触发(异步 handler 用 `await host.mock.emitAsync(...)` 按序 await,返回各返回值)。工具/命令/路由/定时任务的定义体另存进 `host.mock.agentTools/commands/routes/schedules`(活数组,注销即摘除;工具的 `execute` 可直接调);`schedule()` 不设真定时器,用 `await host.mock.fireSchedules()` 手动触发。
|
|
110
112
|
- `ui` 是内存注册表(同 id 覆盖、单次上限 32 条):`list()` 真反映 register/update/remove/arrange。
|
|
111
113
|
- 脚手架可直接生成这个最小单测:`pi-web-ui plugin create my-plugin --with-test`(生成 `index.test.mjs`,现有模板默认不变)。
|
|
112
|
-
- `sdk
|
|
114
|
+
- `sdk/` 是静态拷贝(`index.mjs` + `client-utils.mjs`):`SDK_VERSION` 与 `plugin-sdk/package.json` 的 version 保持一致;已装插件用 `pi-web-ui plugin upgrade-sdk [id]` 一键刷新拷贝(**服务端 `sdk/` 与客户端 `client/sdk/` 一起刷**;版本号或文件对不上才拷,客户端代码确实 `import "./sdk/"` 但目录缺失会补建,零 SDK 拷贝的插件跳过;同时会对每个插件跑一遍 import 自检并报出越界/缺失)。
|
|
115
|
+
- 自检工具:`plugin-sdk/import-check.mjs`(纯 ESM、零依赖)扫一个插件目录里的相对 import,报「客户端越界」与「目标缺失」——`plugin create` 生成后自动跑(随包装在 CLI 旁边);插件作者也可以自己对已有插件跑一遍:
|
|
116
|
+
`node --input-type=module -e "const m = await import('./sdk/import-check.mjs'); console.log(m.checkPluginImports(process.cwd()))"`(从插件目录跑)。
|
|
113
117
|
- mock 只在 SDK 层:不断言、不碰 `server/plugins.ts` 运行时。类型见 `index.d.ts` 的 `MockHost` / `MockHostOverrides` / `createMockHost`。
|
|
114
118
|
|
|
115
119
|
## 本次 P0 新增速览
|
|
@@ -125,6 +129,23 @@ describe("my-plugin", () => {
|
|
|
125
129
|
| `schedule` 持久版(`{id, persistent, catchUp, label}`,落盘+补跑+进后台面板) | ✅ | — |
|
|
126
130
|
| `host.requestPermission`(动态授权:net 补主机 / llm 限模型,用户逐条确认可记住) | ✅(基础族须已声明) | 确认框 + 设置面板「已授权能力」 |
|
|
127
131
|
|
|
132
|
+
## 左栏运行条目:`host.leftPanel`(需要能力 `ui`)
|
|
133
|
+
|
|
134
|
+
把「正在跑的事」登记到左栏「运行的对话」里,按插件分组显示(例如第三方子代理:谁在跑、进度多少)。
|
|
135
|
+
**只展示**:不是对话,没有停止按钮;点击条目时把 `action` 交给你的浏览器 bundle(`host.onUiAction`)。
|
|
136
|
+
|
|
137
|
+
```js
|
|
138
|
+
// 整体替换本插件的条目;同时作为心跳(ttlMs 内不再调用 → 整组自动清空,缺省 60s,夹取 5s–1h)
|
|
139
|
+
host.leftPanel.setRunning(
|
|
140
|
+
[{ id: "sub-1", title: "代码审查", hint: "第 2/5 步", status: "running", icon: "🔍", action: "review:open" }],
|
|
141
|
+
{ ttlMs: 30_000 },
|
|
142
|
+
);
|
|
143
|
+
host.leftPanel.clear(); // 立即清空
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
`status`:`running`(进行中)/ `done` / `error`;缺省 `running`。`id` 在插件内唯一(重复的后者丢弃)。
|
|
147
|
+
上限 50 条;超出、缺 id、非法字段都会丢弃并进入运行时诊断,不会抛错。
|
|
148
|
+
|
|
128
149
|
## 反激活与清理:`host.effect(label, dispose)`
|
|
129
150
|
|
|
130
151
|
宿主的每个注册面(`ui.register` / `registerAgentTool` / `registerCommand` / `route` /
|