pi-web-ui 0.88.0 → 0.90.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 +80 -1
- package/dist/server/agent-service.js +464 -68
- package/dist/server/attachments.js +39 -0
- package/dist/server/auth-cookie.js +23 -0
- package/dist/server/client-state.js +35 -0
- package/dist/server/composer-drafts.js +20 -2
- package/dist/server/conversation-read-tool.js +325 -0
- package/dist/server/dsh/dsh-agent-service.js +46 -11
- package/dist/server/file-archives.js +258 -0
- package/dist/server/file-transfer-routes.js +113 -0
- package/dist/server/files-service.js +123 -1
- package/dist/server/index.js +361 -14
- package/dist/server/marker-service.js +3 -1
- package/dist/server/model-admin.js +285 -36
- package/dist/server/plugins.js +152 -5
- package/dist/server/queue-utils.js +16 -0
- package/dist/server/schedule-agent-tool.js +240 -0
- package/dist/server/scheduler-tasks.js +414 -0
- package/dist/server/settings-service.js +76 -2
- package/dist/server/terminals.js +14 -1
- package/dist/server/tool-manager.js +16 -1
- package/dist/server/update-check.js +253 -19
- package/dist/server/webui-context.js +4 -0
- package/dist/server/ws-unknown-types.js +44 -0
- package/extensions/webui.ts +9 -2
- package/package.json +7 -3
- package/plugin-sdk/index.d.ts +4 -0
- package/plugin-sdk/index.mjs +9 -0
- package/plugins/catalog.json +11 -2
- package/web/dist/assets/TerminalPanel-BDbdLhpT.js +6 -0
- package/web/dist/assets/index-1f_DP8S3.js +380 -0
- package/web/dist/assets/index-DeUwDA-C.css +10 -0
- package/web/dist/index.html +2 -2
- package/web/dist/assets/TerminalPanel-CDH27dNP.js +0 -6
- package/web/dist/assets/index-CQg5A74_.js +0 -375
- package/web/dist/assets/index-DTEcxHWs.css +0 -10
|
@@ -6,10 +6,10 @@
|
|
|
6
6
|
* 预设存取 + 何时需要 reload」,真正动 runtime 的 session.reload() 走宿主回调
|
|
7
7
|
* (reloadSession 里还会刷新斜杠命令目录)。
|
|
8
8
|
*/
|
|
9
|
-
import { existsSync, readdirSync } from "node:fs";
|
|
9
|
+
import { existsSync, readdirSync, readFileSync } from "node:fs";
|
|
10
10
|
import { basename, dirname, join } from "node:path";
|
|
11
11
|
import { fileURLToPath } from "node:url";
|
|
12
|
-
import { extensionKey, normalizeRetryMaxAttempts, normalizeSkillList, normalizeUiLayout, } from "./client-state.js";
|
|
12
|
+
import { extensionKey, normalizeDisabledPluginTools, normalizeRetryMaxAttempts, normalizeSkillList, normalizeUiLayout, } from "./client-state.js";
|
|
13
13
|
import { findVisionModels, SYSTEM_PROMPT } from "./vision-bridge.js";
|
|
14
14
|
import { DEFAULT_TEMPLATES } from "./subagent-templates.js";
|
|
15
15
|
import { deriveLegacy, foldLegacyIntoDisabled, normalizeDisabledAgentTools } from "./tool-manager.js";
|
|
@@ -105,15 +105,65 @@ export class SettingsService {
|
|
|
105
105
|
}
|
|
106
106
|
return false;
|
|
107
107
|
}
|
|
108
|
+
/** 判断某个扩展 id 是否仍存在(loader 之外的兜底,供禁用残留清理用):
|
|
109
|
+
* npm: 包 → 任一 npm 根还有目录,或还列在任一 settings.json packages 里
|
|
110
|
+
* (只删了 node_modules 但配置还在 = 保守保留,等下次 loader 同步);
|
|
111
|
+
* 路径型 → 文件/目录任一存在。空串一律不存在。 */
|
|
112
|
+
extensionStillOnDisk(id) {
|
|
113
|
+
if (!id)
|
|
114
|
+
return false;
|
|
115
|
+
if (id.startsWith("npm:")) {
|
|
116
|
+
const pkg = id.slice(4);
|
|
117
|
+
if (!pkg)
|
|
118
|
+
return false;
|
|
119
|
+
const cwd = this.host.cwd();
|
|
120
|
+
const agentDir = this.host.agentDir();
|
|
121
|
+
for (const npmRoot of [join(agentDir, "npm", "node_modules"), join(cwd, ".pi", "npm", "node_modules")]) {
|
|
122
|
+
try {
|
|
123
|
+
if (existsSync(join(npmRoot, pkg)))
|
|
124
|
+
return true;
|
|
125
|
+
}
|
|
126
|
+
catch { }
|
|
127
|
+
}
|
|
128
|
+
return this.packageStillListed(pkg);
|
|
129
|
+
}
|
|
130
|
+
try {
|
|
131
|
+
return existsSync(id);
|
|
132
|
+
}
|
|
133
|
+
catch {
|
|
134
|
+
return false;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
/** 包名是否还列在全局/项目 settings.json 的 packages 里(文件缺失/坏 JSON 当没列)。 */
|
|
138
|
+
packageStillListed(pkg) {
|
|
139
|
+
const wanted = `npm:${pkg}`;
|
|
140
|
+
for (const file of [join(this.host.agentDir(), "settings.json"), join(this.host.cwd(), ".pi", "settings.json")]) {
|
|
141
|
+
try {
|
|
142
|
+
const parsed = JSON.parse(readFileSync(file, "utf8"));
|
|
143
|
+
if (!Array.isArray(parsed?.packages))
|
|
144
|
+
continue;
|
|
145
|
+
for (const entry of parsed.packages) {
|
|
146
|
+
if (typeof entry !== "string")
|
|
147
|
+
continue;
|
|
148
|
+
if (entry === wanted || entry === pkg)
|
|
149
|
+
return true;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
catch { }
|
|
153
|
+
}
|
|
154
|
+
return false;
|
|
155
|
+
}
|
|
108
156
|
push() {
|
|
109
157
|
const disabledSkills = new Set(this.settings.disabledSkills);
|
|
110
158
|
const reviewDisabledSkills = new Set(this.settings.reviewDisabledSkills);
|
|
111
159
|
const disabledExts = new Set(this.settings.disabledExtensions);
|
|
112
160
|
let loadedSkillNames = null;
|
|
161
|
+
let loadedExtNames = null;
|
|
113
162
|
try {
|
|
114
163
|
const loadedSkills = this.host.getSession().resourceLoader.getSkills().skills;
|
|
115
164
|
const loadedExts = this.host.getSession().resourceLoader.getExtensions().extensions;
|
|
116
165
|
loadedSkillNames = new Set(loadedSkills.map((s) => s.name));
|
|
166
|
+
loadedExtNames = new Set(loadedExts.map((e) => extensionKey(e)));
|
|
117
167
|
// Prune entries that no longer exist on disk AND aren't disabled
|
|
118
168
|
// (e.g. a skill/extension file was deleted). Disabled entries are
|
|
119
169
|
// kept so they can be re-enabled even when filtered out of the loader.
|
|
@@ -179,6 +229,18 @@ export class SettingsService {
|
|
|
179
229
|
// Disabled entries that still exist on disk are re-added (with the
|
|
180
230
|
// last-known description) so they can be re-enabled; entries whose
|
|
181
231
|
// source file was deleted are dropped instead of being resurrected.
|
|
232
|
+
// disabledExtensions 同理:已卸载(loader 里没有、磁盘/配置里也没有)的扩展
|
|
233
|
+
// 从禁用记录里剔除并持久化 —— 否则卸载过的扩展以“已禁用”灰条永生(issue #192)。
|
|
234
|
+
// loadedExtNames 非 null 即 session 就绪;取不到(catch 分支)时保守跳过。
|
|
235
|
+
if (loadedExtNames !== null) {
|
|
236
|
+
const staleExts = this.settings.disabledExtensions.filter((id) => !loadedExtNames.has(id) && !this.extensionStillOnDisk(id));
|
|
237
|
+
if (staleExts.length > 0) {
|
|
238
|
+
this.settings.disabledExtensions = this.settings.disabledExtensions.filter((n) => !staleExts.includes(n));
|
|
239
|
+
this.host.stateStore.saveSettings(this.host.clientId, {
|
|
240
|
+
disabledExtensions: this.settings.disabledExtensions,
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
}
|
|
182
244
|
for (const name of this.settings.disabledSkills) {
|
|
183
245
|
if (this.knownSkills.has(name))
|
|
184
246
|
continue;
|
|
@@ -186,8 +248,12 @@ export class SettingsService {
|
|
|
186
248
|
continue;
|
|
187
249
|
this.knownSkills.set(name, { name, description: "", enabled: false });
|
|
188
250
|
}
|
|
251
|
+
// (上面的 stale 清理已把卸载项移出禁用记录;这里再按磁盘挡一次,
|
|
252
|
+
// session 未就绪跳过清理时也不复活幽灵。)
|
|
189
253
|
for (const id of this.settings.disabledExtensions) {
|
|
190
254
|
if (!this.knownExtensions.has(id)) {
|
|
255
|
+
if (!this.extensionStillOnDisk(id))
|
|
256
|
+
continue;
|
|
191
257
|
this.knownExtensions.set(id, {
|
|
192
258
|
id,
|
|
193
259
|
name: id.startsWith("npm:") ? id : basename(id),
|
|
@@ -217,6 +283,7 @@ export class SettingsService {
|
|
|
217
283
|
promptTemplate: this.settings.promptTemplate ?? "",
|
|
218
284
|
promptOverrides: { ...this.settings.promptOverrides },
|
|
219
285
|
disabledAgentTools: [...normalizeDisabledAgentTools(this.settings.disabledAgentTools)],
|
|
286
|
+
disabledPluginTools: [...normalizeDisabledPluginTools(this.settings.disabledPluginTools)],
|
|
220
287
|
terminalToolsEnabled: legacyTools.terminalToolsEnabled,
|
|
221
288
|
terminalBash: this.settings.terminalBash,
|
|
222
289
|
terminalBashIdleMs: this.settings.terminalBashIdleMs,
|
|
@@ -317,6 +384,7 @@ export class SettingsService {
|
|
|
317
384
|
partial.disabledExtensions !== undefined;
|
|
318
385
|
// 统一工具开关 live 生效(ActiveSet 加减,无需 reload),见末尾 applyToolGating。
|
|
319
386
|
const toolGatingChanged = partial.disabledAgentTools !== undefined ||
|
|
387
|
+
partial.disabledPluginTools !== undefined ||
|
|
320
388
|
partial.terminalToolsEnabled !== undefined ||
|
|
321
389
|
partial.editSoftEnabled !== undefined ||
|
|
322
390
|
partial.questionnaireEnabled !== undefined;
|
|
@@ -358,6 +426,9 @@ export class SettingsService {
|
|
|
358
426
|
if (partial.disabledAgentTools !== undefined) {
|
|
359
427
|
this.settings.disabledAgentTools = normalizeDisabledAgentTools(partial.disabledAgentTools);
|
|
360
428
|
}
|
|
429
|
+
if (partial.disabledPluginTools !== undefined) {
|
|
430
|
+
this.settings.disabledPluginTools = normalizeDisabledPluginTools(partial.disabledPluginTools);
|
|
431
|
+
}
|
|
361
432
|
if (partial.terminalToolsEnabled !== undefined ||
|
|
362
433
|
partial.editSoftEnabled !== undefined ||
|
|
363
434
|
partial.questionnaireEnabled !== undefined) {
|
|
@@ -469,6 +540,7 @@ export class SettingsService {
|
|
|
469
540
|
disabledSkills: [...this.settings.disabledSkills],
|
|
470
541
|
disabledExtensions: [...this.settings.disabledExtensions],
|
|
471
542
|
disabledAgentTools: [...normalizeDisabledAgentTools(this.settings.disabledAgentTools)],
|
|
543
|
+
disabledPluginTools: [...normalizeDisabledPluginTools(this.settings.disabledPluginTools)],
|
|
472
544
|
terminalToolsEnabled: this.settings.terminalToolsEnabled,
|
|
473
545
|
terminalBash: this.settings.terminalBash,
|
|
474
546
|
terminalBashIdleMs: this.settings.terminalBashIdleMs,
|
|
@@ -514,6 +586,7 @@ export class SettingsService {
|
|
|
514
586
|
disabledSkills: [...p.disabledSkills],
|
|
515
587
|
disabledExtensions: [...p.disabledExtensions],
|
|
516
588
|
disabledAgentTools: presetDisabled,
|
|
589
|
+
disabledPluginTools: normalizeDisabledPluginTools(p.disabledPluginTools ?? this.settings.disabledPluginTools),
|
|
517
590
|
terminalToolsEnabled: presetLegacy.terminalToolsEnabled,
|
|
518
591
|
// 终端接管偏好随预设走;旧预设缺字段时保留当前值。
|
|
519
592
|
terminalBash: p.terminalBash ?? this.settings.terminalBash,
|
|
@@ -623,6 +696,7 @@ export class SettingsService {
|
|
|
623
696
|
this.host.emit({ type: "notice", level: "info", text: "设置已应用", textEn: "Settings applied" });
|
|
624
697
|
}
|
|
625
698
|
catch (err) {
|
|
699
|
+
console.error(`[settings] apply reload failed (client ${this.host.clientId}):`, err);
|
|
626
700
|
this.host.emit({
|
|
627
701
|
type: "notice",
|
|
628
702
|
level: "error",
|
package/dist/server/terminals.js
CHANGED
|
@@ -1077,7 +1077,7 @@ export class TerminalManager {
|
|
|
1077
1077
|
* state worth protecting, so they neither retain the conversation nor
|
|
1078
1078
|
* block its dismissal (they are killed together with the conversation).
|
|
1079
1079
|
* Retained-output history never counts either (same as countLive).
|
|
1080
|
-
*
|
|
1080
|
+
* 存活且“用过”的终端数——对话保留只看这个口径(含 AI bash:切走不杀后台活)。 */
|
|
1081
1081
|
countBlockingLive() {
|
|
1082
1082
|
let n = 0;
|
|
1083
1083
|
for (const entry of this.terms.values())
|
|
@@ -1085,6 +1085,19 @@ export class TerminalManager {
|
|
|
1085
1085
|
n++;
|
|
1086
1086
|
return n;
|
|
1087
1087
|
}
|
|
1088
|
+
/** Count of LIVE *user* terminals that were actually used. AI terminals
|
|
1089
|
+
* (agentBash — 终端接管 bash 的一次性/持久终端) are EXCLUDED: they are
|
|
1090
|
+
* internal execution records owned by the agent, killed together with the
|
|
1091
|
+
* conversation on dismissal (removeConversation 里 killAll), so they must
|
|
1092
|
+
* never block it (issue #181:残留 ai-bash-98/99 把会话永久钉在列表里)。
|
|
1093
|
+
* 只保护用户在交互式终端里的现场:用户亲手开且用过的存活终端才阻断移出。 */
|
|
1094
|
+
countUserBlockingLive() {
|
|
1095
|
+
let n = 0;
|
|
1096
|
+
for (const entry of this.terms.values())
|
|
1097
|
+
if (!entry.exited && entry.used && !entry.agentBash)
|
|
1098
|
+
n++;
|
|
1099
|
+
return n;
|
|
1100
|
+
}
|
|
1088
1101
|
emitList() {
|
|
1089
1102
|
this.emit({ type: "terminal_list", terminals: this.list() });
|
|
1090
1103
|
}
|
|
@@ -47,9 +47,17 @@ export const MARKERS_LIST_TOOL_NAME = "todo_list";
|
|
|
47
47
|
/** 浏览器页面操作工具(定义见 agent-service.ts makeBrowserPageTool):模型经
|
|
48
48
|
* page-picker 浏览器扩展读/操作用户已授权的页面。 */
|
|
49
49
|
export const BROWSER_PAGE_TOOL_NAME = "browser_page";
|
|
50
|
+
/** 别的对话读取工具(定义见 conversation-read-tool.ts):运行中对话(含子代理)+
|
|
51
|
+
* 历史会话转录,只读。 */
|
|
52
|
+
export const CONVERSATION_READ_TOOL_NAME = "conversation_read";
|
|
53
|
+
/** 定时任务工具(定义见 schedule-agent-tool.ts):创建/查看/取消内置调度任务,
|
|
54
|
+
* 到期自动唤醒发起对话执行 prompt 并汇报。 */
|
|
55
|
+
export const SCHEDULE_TASK_TOOL_NAME = "schedule_task";
|
|
56
|
+
export const SCHEDULE_LIST_TOOL_NAME = "schedule_list";
|
|
57
|
+
export const SCHEDULE_CANCEL_TOOL_NAME = "schedule_cancel";
|
|
50
58
|
/** 旧工具名(持久化迁移用;新代码一律用 MARKERS_LIST_TOOL_NAME)。 */
|
|
51
59
|
export const LEGACY_MARKERS_LIST_TOOL_NAME = "markers_list";
|
|
52
|
-
/** 可开关的 Agent 工具总目录(共
|
|
60
|
+
/** 可开关的 Agent 工具总目录(共 23 个;bash 本体与 SDK 内置 edit/read
|
|
53
61
|
* 不进目录——关了 agent 就残了,不给关)。 */
|
|
54
62
|
export const AGENT_TOOL_CATALOG = [
|
|
55
63
|
...TERMINAL_TOOL_NAMES.map((name) => ({
|
|
@@ -71,6 +79,13 @@ export const AGENT_TOOL_CATALOG = [
|
|
|
71
79
|
// 默认关(AI 动用户浏览器,opt-in 才开)且 dshVisible=false:DSH 引擎没有页面桥
|
|
72
80
|
// (page_request 由 pi 引擎的 customTool 发出),列在那里只会让用户关一个不存在的工具。
|
|
73
81
|
{ name: BROWSER_PAGE_TOOL_NAME, group: "other", defaultOn: false, dshVisible: false },
|
|
82
|
+
// 只读别的对话(含子代理实时消息与历史转录),默认开;DSH 引擎没有该 customTool。
|
|
83
|
+
{ name: CONVERSATION_READ_TOOL_NAME, group: "other", defaultOn: true, dshVisible: false },
|
|
84
|
+
// 定时/延时唤醒:默认开(不打开 AI 根本不知道能定时;60s 间隔底线+面板可随时取消),
|
|
85
|
+
// DSH 引擎没有该 customTool(走 goal-rpc,无 customTool 注册面)。
|
|
86
|
+
{ name: SCHEDULE_TASK_TOOL_NAME, group: "other", defaultOn: true, dshVisible: false },
|
|
87
|
+
{ name: SCHEDULE_LIST_TOOL_NAME, group: "other", defaultOn: true, dshVisible: false },
|
|
88
|
+
{ name: SCHEDULE_CANCEL_TOOL_NAME, group: "other", defaultOn: true, dshVisible: false },
|
|
74
89
|
];
|
|
75
90
|
const KNOWN_NAMES = new Set(AGENT_TOOL_CATALOG.map((t) => t.name));
|
|
76
91
|
/** 是否为本表登记的可开关工具(未知名一律 false,不抛错)。 */
|
|
@@ -1,14 +1,17 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* All-source update check: pi-web-ui itself, the installed pi core
|
|
3
3
|
* (@earendil-works/pi-coding-agent — probed via `pi --version`, with a
|
|
4
|
-
* vendored-copy fallback),
|
|
5
|
-
* <agentDir>/npm/package.json (fallback: raw node_modules walk)
|
|
4
|
+
* vendored-copy fallback), the DIRECT pi extensions declared in
|
|
5
|
+
* <agentDir>/npm/package.json (fallback: raw node_modules walk), plus the
|
|
6
|
+
* git-source extensions declared in global/project settings.json (issue #178).
|
|
6
7
|
* Pure logic lives here so it can be unit-tested with an injected fetcher
|
|
7
8
|
* (and an injected pi-core probe); ClientSession only wires it to the wire
|
|
8
9
|
* protocol.
|
|
9
10
|
*/
|
|
11
|
+
import { execFile } from "node:child_process";
|
|
10
12
|
import { readdirSync, readFileSync, realpathSync, existsSync } from "node:fs";
|
|
11
13
|
import { delimiter, dirname, join } from "node:path";
|
|
14
|
+
import { promisify } from "node:util";
|
|
12
15
|
import { pick } from "./i18n.js";
|
|
13
16
|
const PI_CORE_PACKAGE = "@earendil-works/pi-coding-agent";
|
|
14
17
|
/** npm 官方源;用户在 <agentDir>/npm/.npmrc 里配了镜像/私有源时会被覆盖(issue #151)。 */
|
|
@@ -155,6 +158,207 @@ function readLocalPackage(dir) {
|
|
|
155
158
|
return null;
|
|
156
159
|
}
|
|
157
160
|
}
|
|
161
|
+
// -- git-source extensions (issue #178) --------------------------------------
|
|
162
|
+
// The panel historically enumerated only npm direct deps, so `git:` entries in
|
|
163
|
+
// settings.json never appeared. pi core updates both (`updateConfiguredSources`
|
|
164
|
+
// buckets npmCandidates/gitCandidates), keyed by normalized `git:host/path`.
|
|
165
|
+
const execFileAsync = promisify(execFile);
|
|
166
|
+
/** Matches pi core NETWORK_TIMEOUT_MS so slow forges degrade the same way. */
|
|
167
|
+
const GIT_TIMEOUT_MS = 10_000;
|
|
168
|
+
function hasUnsafeGitPart(value, allowSlash) {
|
|
169
|
+
if (value.includes("\0") || value.includes("\\") || value.startsWith("/"))
|
|
170
|
+
return true;
|
|
171
|
+
if (!allowSlash && value.includes("/"))
|
|
172
|
+
return true;
|
|
173
|
+
if (value.split("/").includes(".."))
|
|
174
|
+
return true;
|
|
175
|
+
return false;
|
|
176
|
+
}
|
|
177
|
+
function normalizeGitRepo(host, path, ref) {
|
|
178
|
+
const cleanPath = path.replace(/\.git$/, "").replace(/^\/+|\/+$/g, "");
|
|
179
|
+
if (!host || !cleanPath || cleanPath.split("/").length < 2)
|
|
180
|
+
return null;
|
|
181
|
+
if (hasUnsafeGitPart(host, false) || hasUnsafeGitPart(cleanPath, true))
|
|
182
|
+
return null;
|
|
183
|
+
if (ref !== null && (ref === "" || ref.includes("\0")))
|
|
184
|
+
return null;
|
|
185
|
+
return {
|
|
186
|
+
host,
|
|
187
|
+
path: cleanPath,
|
|
188
|
+
ref,
|
|
189
|
+
shorthand: `${host}/${cleanPath}`,
|
|
190
|
+
identity: `git:${host}/${cleanPath}`,
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
/** Split `rest` (the part after the host) on the first `@` into path + ref. */
|
|
194
|
+
function splitGitRef(rest) {
|
|
195
|
+
const at = rest.indexOf("@");
|
|
196
|
+
if (at < 0)
|
|
197
|
+
return { path: rest, ref: null };
|
|
198
|
+
return { path: rest.slice(0, at), ref: rest.slice(at + 1) || null };
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* Parse a settings `packages` entry into a git source, or null when it is not
|
|
202
|
+
* one. Accepts the same shapes pi core does (`parseGitUrl`): `git:`-prefixed
|
|
203
|
+
* shorthand/URLs plus bare explicit protocol URLs. `npm:`/local entries yield
|
|
204
|
+
* null — they are handled by the npm enumeration, not here.
|
|
205
|
+
*/
|
|
206
|
+
export function parseGitExtensionSource(entry) {
|
|
207
|
+
const trimmed = entry.trim();
|
|
208
|
+
if (!trimmed || trimmed.startsWith("npm:"))
|
|
209
|
+
return null;
|
|
210
|
+
const url = trimmed.startsWith("git:") ? trimmed.slice(4).trim() : trimmed;
|
|
211
|
+
if (!url)
|
|
212
|
+
return null;
|
|
213
|
+
// scp-like: git@host:owner/repo[.git][@ref]
|
|
214
|
+
const scp = url.match(/^git@([^:]+):(.+)$/);
|
|
215
|
+
if (scp) {
|
|
216
|
+
const { path, ref } = splitGitRef(scp[2]);
|
|
217
|
+
return normalizeGitRepo(scp[1], path, ref);
|
|
218
|
+
}
|
|
219
|
+
// Explicit protocol URLs (bare or git:-prefixed).
|
|
220
|
+
if (/^(https?|ssh|git):\/\//i.test(url)) {
|
|
221
|
+
let host;
|
|
222
|
+
let rest;
|
|
223
|
+
try {
|
|
224
|
+
const parsed = new URL(url);
|
|
225
|
+
host = parsed.hostname;
|
|
226
|
+
rest = parsed.pathname.replace(/^\/+/, "");
|
|
227
|
+
}
|
|
228
|
+
catch {
|
|
229
|
+
return null;
|
|
230
|
+
}
|
|
231
|
+
const { path, ref } = splitGitRef(rest);
|
|
232
|
+
return normalizeGitRepo(host, path, ref);
|
|
233
|
+
}
|
|
234
|
+
// Shorthand only with an explicit git: prefix (bare `a/b` is a local path).
|
|
235
|
+
if (!trimmed.startsWith("git:"))
|
|
236
|
+
return null;
|
|
237
|
+
const slash = url.indexOf("/");
|
|
238
|
+
if (slash < 0)
|
|
239
|
+
return null;
|
|
240
|
+
const host = url.slice(0, slash);
|
|
241
|
+
if (!host.includes(".") && host !== "localhost")
|
|
242
|
+
return null;
|
|
243
|
+
const { path, ref } = splitGitRef(url.slice(slash + 1));
|
|
244
|
+
return normalizeGitRepo(host, path, ref);
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* Total switch for the git-source check (issue #178 step 8). `0/false/no/off`
|
|
248
|
+
* disables it (large-monorepo escape hatch); unset or anything else enables.
|
|
249
|
+
*/
|
|
250
|
+
export function isGitExtensionCheckEnabled(env = process.env) {
|
|
251
|
+
const v = (env.PI_WEB_GIT_EXTENSION_CHECK ?? "").trim().toLowerCase();
|
|
252
|
+
return v !== "0" && v !== "false" && v !== "no" && v !== "off";
|
|
253
|
+
}
|
|
254
|
+
function readSettingsPackagesFile(file) {
|
|
255
|
+
try {
|
|
256
|
+
const data = JSON.parse(readFileSync(file, "utf8"));
|
|
257
|
+
if (!data || typeof data !== "object" || !Array.isArray(data.packages))
|
|
258
|
+
return [];
|
|
259
|
+
return data.packages.filter((p) => typeof p === "string" || (p !== null && typeof p === "object"));
|
|
260
|
+
}
|
|
261
|
+
catch {
|
|
262
|
+
return [];
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
function readGitCloneNameVersion(installDir) {
|
|
266
|
+
try {
|
|
267
|
+
const pkg = JSON.parse(readFileSync(join(installDir, "package.json"), "utf8"));
|
|
268
|
+
// Name and version fall back independently: a clone with a version but
|
|
269
|
+
// no usable name still reports its version (name falls back to the
|
|
270
|
+
// source shorthand at the call site), and vice versa.
|
|
271
|
+
return {
|
|
272
|
+
...(typeof pkg.name === "string" && pkg.name ? { name: pkg.name } : {}),
|
|
273
|
+
...(typeof pkg.version === "string" && pkg.version ? { version: pkg.version } : {}),
|
|
274
|
+
};
|
|
275
|
+
}
|
|
276
|
+
catch {
|
|
277
|
+
return {};
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
/**
|
|
281
|
+
* Enumerate `git:` entries from global (<agentDir>/settings.json) plus project
|
|
282
|
+
* (<projectCwd>/.pi/settings.json) settings — the same two scopes pi core
|
|
283
|
+
* updates. Project wins on identity collision (mirrors core dedupePackages).
|
|
284
|
+
* Entries whose clone is missing are still listed (version `?`): checkAll
|
|
285
|
+
* reports them as per-item errors instead of silently dropping the row.
|
|
286
|
+
*/
|
|
287
|
+
export function listGitExtensions(agentDir, projectCwd, env = process.env) {
|
|
288
|
+
if (!isGitExtensionCheckEnabled(env))
|
|
289
|
+
return [];
|
|
290
|
+
const scopes = [];
|
|
291
|
+
if (projectCwd)
|
|
292
|
+
scopes.push({ file: join(projectCwd, ".pi", "settings.json"), gitRoot: join(projectCwd, ".pi", "git") });
|
|
293
|
+
scopes.push({ file: join(agentDir, "settings.json"), gitRoot: join(agentDir, "git") });
|
|
294
|
+
const seen = new Set();
|
|
295
|
+
const out = [];
|
|
296
|
+
for (const { file, gitRoot } of scopes) {
|
|
297
|
+
for (const pkg of readSettingsPackagesFile(file)) {
|
|
298
|
+
const raw = typeof pkg === "string" ? pkg : typeof pkg.source === "string" ? pkg.source : null;
|
|
299
|
+
if (!raw)
|
|
300
|
+
continue;
|
|
301
|
+
const parsed = parseGitExtensionSource(raw);
|
|
302
|
+
if (!parsed || seen.has(parsed.identity))
|
|
303
|
+
continue;
|
|
304
|
+
seen.add(parsed.identity);
|
|
305
|
+
const installDir = join(gitRoot, parsed.host, ...parsed.path.split("/"));
|
|
306
|
+
const nv = readGitCloneNameVersion(installDir);
|
|
307
|
+
out.push({
|
|
308
|
+
name: nv.name ?? parsed.shorthand,
|
|
309
|
+
version: nv.version ?? "?",
|
|
310
|
+
kind: "git-extension",
|
|
311
|
+
source: parsed.shorthand,
|
|
312
|
+
installDir,
|
|
313
|
+
ref: parsed.ref,
|
|
314
|
+
});
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
return out.sort((a, b) => (a.name < b.name ? -1 : a.name > b.name ? 1 : 0));
|
|
318
|
+
}
|
|
319
|
+
/** Display form for git rows: `<version> (<short sha>)`, sha-only when the
|
|
320
|
+
* clone has no readable package.json version (issue #178 step 5). */
|
|
321
|
+
export function formatGitVersion(version, sha) {
|
|
322
|
+
const short = sha.slice(0, 7);
|
|
323
|
+
return version && version !== "?" ? `${version} (${short})` : short;
|
|
324
|
+
}
|
|
325
|
+
async function runGit(args, cwd) {
|
|
326
|
+
const { stdout } = await execFileAsync("git", args, {
|
|
327
|
+
cwd,
|
|
328
|
+
timeout: GIT_TIMEOUT_MS,
|
|
329
|
+
env: { ...process.env, GIT_TERMINAL_PROMPT: "0" },
|
|
330
|
+
});
|
|
331
|
+
return stdout.trim();
|
|
332
|
+
}
|
|
333
|
+
/**
|
|
334
|
+
* Compare a clone's HEAD against its remote (lightweight: `ls-remote` fetches
|
|
335
|
+
* refs only, no objects). Upstream branch wins when configured, else origin
|
|
336
|
+
* HEAD — the same order pi core `getRemoteGitHead` uses.
|
|
337
|
+
*/
|
|
338
|
+
export async function defaultCheckGitExtension(installDir) {
|
|
339
|
+
const localSha = await runGit(["rev-parse", "HEAD"], installDir);
|
|
340
|
+
if (!/^[0-9a-f]{40}$/i.test(localSha))
|
|
341
|
+
throw new Error(`Bad local HEAD: ${localSha.slice(0, 20)}`);
|
|
342
|
+
let remoteSha = null;
|
|
343
|
+
try {
|
|
344
|
+
const upstream = await runGit(["rev-parse", "--abbrev-ref", "@{upstream}"], installDir);
|
|
345
|
+
const branch = upstream.match(/^origin\/(.+)$/)?.[1];
|
|
346
|
+
if (branch) {
|
|
347
|
+
const out = await runGit(["ls-remote", "origin", `refs/heads/${branch}`], installDir);
|
|
348
|
+
remoteSha = out.match(/^([0-9a-f]{40})\s+/m)?.[1] ?? null;
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
catch {
|
|
352
|
+
/* no usable upstream — fall through to origin HEAD */
|
|
353
|
+
}
|
|
354
|
+
if (!remoteSha) {
|
|
355
|
+
const out = await runGit(["ls-remote", "origin", "HEAD"], installDir);
|
|
356
|
+
remoteSha = out.match(/^([0-9a-f]{40})\s+HEAD$/m)?.[1] ?? null;
|
|
357
|
+
}
|
|
358
|
+
if (!remoteSha)
|
|
359
|
+
throw new Error("Failed to determine remote HEAD");
|
|
360
|
+
return { localSha, remoteSha };
|
|
361
|
+
}
|
|
158
362
|
/** How long a pi probe result stays hot (mirrors ClientSession.piCliProbe). */
|
|
159
363
|
const PI_PROBE_TTL_MS = 10_000;
|
|
160
364
|
let piCoreProbe = null;
|
|
@@ -236,13 +440,14 @@ function readVendoredPiCore(agentDir) {
|
|
|
236
440
|
}
|
|
237
441
|
}
|
|
238
442
|
/**
|
|
239
|
-
* Build the full local target list: webui + the pi core + installed packages
|
|
443
|
+
* Build the full local target list: webui + the pi core + installed packages
|
|
444
|
+
* + git-source extensions (issue #178) from global and project settings.
|
|
240
445
|
* The pi core version comes from the CLI probe (injectable for tests), falling
|
|
241
446
|
* back to the vendored copy under <agentDir>/npm/node_modules. Packages
|
|
242
447
|
* listing the core directly are filtered out so the pi-core row wins — never
|
|
243
448
|
* two rows for the same package.
|
|
244
449
|
*/
|
|
245
|
-
export function collectTargets(agentDir, webuiVersion, probePiCore = defaultProbePiCore) {
|
|
450
|
+
export function collectTargets(agentDir, webuiVersion, probePiCore = defaultProbePiCore, opts) {
|
|
246
451
|
const targets = [{ name: "pi-web-ui", version: webuiVersion, kind: "webui" }];
|
|
247
452
|
const coreVersion = probePiCore() ?? readVendoredPiCore(agentDir);
|
|
248
453
|
if (coreVersion) {
|
|
@@ -253,6 +458,7 @@ export function collectTargets(agentDir, webuiVersion, probePiCore = defaultProb
|
|
|
253
458
|
});
|
|
254
459
|
}
|
|
255
460
|
targets.push(...listInstalledPackages(agentDir).filter((pkg) => pkg.name !== PI_CORE_PACKAGE));
|
|
461
|
+
targets.push(...listGitExtensions(agentDir, opts?.projectCwd));
|
|
256
462
|
return targets;
|
|
257
463
|
}
|
|
258
464
|
/** Default fetcher (real network). Tests inject a fake. */
|
|
@@ -361,24 +567,61 @@ export async function fetchLatest(fetcher, name, registry = NPM_DEFAULT_REGISTRY
|
|
|
361
567
|
};
|
|
362
568
|
}
|
|
363
569
|
/**
|
|
364
|
-
* Check every target against the registry
|
|
365
|
-
*
|
|
366
|
-
*
|
|
570
|
+
* Check every target: npm kinds against the registry, git-extension kinds
|
|
571
|
+
* against their clone's remote (`ls-remote`, refs only). One failed lookup
|
|
572
|
+
* degrades to an error item (upToDate: false) without failing the rest.
|
|
573
|
+
* Results keep the input order. Bounded concurrency (CONCURRENCY) keeps
|
|
574
|
+
* registry/remote load polite. The git checker is injectable for tests.
|
|
367
575
|
*/
|
|
368
576
|
export async function checkAll(targets, fetcher = defaultFetcher,
|
|
369
|
-
/**
|
|
577
|
+
/** 单项查询失败时的 error 文案语言(默认英文)。 */
|
|
370
578
|
lang,
|
|
371
579
|
/** 镜像/私有源配置(默认官方源;调用方经 resolveNpmRegistry 传入 .npmrc,issue #151)。 */
|
|
372
|
-
registryConfig
|
|
580
|
+
registryConfig,
|
|
581
|
+
/** git 源远端比较(默认真 git;单测注入 fake,issue #178)。 */
|
|
582
|
+
gitCheck = defaultCheckGitExtension) {
|
|
373
583
|
const l = lang?.() ?? "en";
|
|
374
584
|
const registry = registryConfig?.registry ?? NPM_DEFAULT_REGISTRY;
|
|
375
585
|
const authHeader = registryConfig?.authHeader ?? null;
|
|
586
|
+
const fail = (t, errMessage) => ({
|
|
587
|
+
name: t.name,
|
|
588
|
+
kind: t.kind,
|
|
589
|
+
current: t.version,
|
|
590
|
+
latest: null,
|
|
591
|
+
latestPublishedAt: null,
|
|
592
|
+
upToDate: false,
|
|
593
|
+
...(t.source ? { source: t.source } : {}),
|
|
594
|
+
error: pick(l, `检查更新失败:${errMessage}`, `Failed to check for updates: ${errMessage}`, "updatecheck.check.failed", {
|
|
595
|
+
errMessage,
|
|
596
|
+
}),
|
|
597
|
+
});
|
|
376
598
|
const results = Array.from({ length: targets.length });
|
|
377
599
|
let cursor = 0;
|
|
378
600
|
async function worker() {
|
|
379
601
|
while (cursor < targets.length) {
|
|
380
602
|
const i = cursor++;
|
|
381
603
|
const t = targets[i];
|
|
604
|
+
if (t.kind === "git-extension") {
|
|
605
|
+
try {
|
|
606
|
+
if (!t.installDir)
|
|
607
|
+
throw new Error("missing install dir");
|
|
608
|
+
const { localSha, remoteSha } = await gitCheck(t.installDir);
|
|
609
|
+
const upToDate = localSha.trim() === remoteSha.trim();
|
|
610
|
+
results[i] = {
|
|
611
|
+
name: t.name,
|
|
612
|
+
kind: t.kind,
|
|
613
|
+
current: formatGitVersion(t.version, localSha),
|
|
614
|
+
latest: formatGitVersion(t.version, remoteSha),
|
|
615
|
+
latestPublishedAt: null,
|
|
616
|
+
upToDate,
|
|
617
|
+
...(t.source ? { source: t.source } : {}),
|
|
618
|
+
};
|
|
619
|
+
}
|
|
620
|
+
catch (err) {
|
|
621
|
+
results[i] = fail(t, err.message);
|
|
622
|
+
}
|
|
623
|
+
continue;
|
|
624
|
+
}
|
|
382
625
|
try {
|
|
383
626
|
const { latest, latestPublishedAt } = await fetchLatest(fetcher, t.name, registry, authHeader);
|
|
384
627
|
results[i] = {
|
|
@@ -391,16 +634,7 @@ registryConfig) {
|
|
|
391
634
|
};
|
|
392
635
|
}
|
|
393
636
|
catch (err) {
|
|
394
|
-
|
|
395
|
-
results[i] = {
|
|
396
|
-
name: t.name,
|
|
397
|
-
kind: t.kind,
|
|
398
|
-
current: t.version,
|
|
399
|
-
latest: null,
|
|
400
|
-
latestPublishedAt: null,
|
|
401
|
-
upToDate: false,
|
|
402
|
-
error: pick(l, `检查更新失败:${errMessage}`, `Failed to check for updates: ${errMessage}`, "updatecheck.check.failed", { errMessage }),
|
|
403
|
-
};
|
|
637
|
+
results[i] = fail(t, err.message);
|
|
404
638
|
}
|
|
405
639
|
}
|
|
406
640
|
}
|
|
@@ -104,6 +104,10 @@ export class WebUIContext {
|
|
|
104
104
|
}
|
|
105
105
|
this.push();
|
|
106
106
|
};
|
|
107
|
+
/** Whether any widgets are mounted — the 2s poll skips refresh when false. */
|
|
108
|
+
hasWidgets() {
|
|
109
|
+
return this.widgets.size > 0;
|
|
110
|
+
}
|
|
107
111
|
/** Re-render all widgets and push when content changed (polled + on demand). */
|
|
108
112
|
refresh() {
|
|
109
113
|
let changed = false;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Unknown inbound WS message-type counter —— 独立成模块是为了可单测
|
|
3
|
+
* (`server/index.ts` 起 WS + SDK,不适合在单测里 import)。
|
|
4
|
+
*
|
|
5
|
+
* 背景:`dispatch` 的 `switch (msg.type)` 的 `default:` 分支原来是空的,
|
|
6
|
+
* 版本错位(旧前端/新服务端)或插件发错 type 时静默丢弃,只能抓包排查。
|
|
7
|
+
* 这里只做计数 + 节流 warn,不改变任何已知类型的行为。
|
|
8
|
+
*/
|
|
9
|
+
/** 同一 type 两次 warn 之间最小间隔(防刷屏),可单测覆盖。 */
|
|
10
|
+
export const UNKNOWN_WS_WARN_INTERVAL_MS = 60_000;
|
|
11
|
+
/** 未知 type 种类上限:type 字符串远端可控,无上限则内存无界增长。
|
|
12
|
+
* 超限后的新 type 不再建 key,只进溢出计数。 */
|
|
13
|
+
export const MAX_UNKNOWN_WS_TYPES = 500;
|
|
14
|
+
const counts = new Map();
|
|
15
|
+
const lastWarnAt = new Map();
|
|
16
|
+
let overflowed = 0;
|
|
17
|
+
/**
|
|
18
|
+
* 记录一次未知 type,计数永远 +1;同一 type 在 `UNKNOWN_WS_WARN_INTERVAL_MS`
|
|
19
|
+
* 内只返回一次 `warn: true`(调用方决定是否 `console.warn`)。
|
|
20
|
+
*/
|
|
21
|
+
export function recordUnknownWsType(type, now = Date.now()) {
|
|
22
|
+
if (!counts.has(type) && counts.size >= MAX_UNKNOWN_WS_TYPES) {
|
|
23
|
+
overflowed++;
|
|
24
|
+
return { count: overflowed, warn: false };
|
|
25
|
+
}
|
|
26
|
+
const count = (counts.get(type) ?? 0) + 1;
|
|
27
|
+
counts.set(type, count);
|
|
28
|
+
const last = lastWarnAt.get(type) ?? -Infinity;
|
|
29
|
+
if (now - last >= UNKNOWN_WS_WARN_INTERVAL_MS) {
|
|
30
|
+
lastWarnAt.set(type, now);
|
|
31
|
+
return { count, warn: true };
|
|
32
|
+
}
|
|
33
|
+
return { count, warn: false };
|
|
34
|
+
}
|
|
35
|
+
/** 只读快照(排障用,返回拷贝,调用方改不动内部表)。 */
|
|
36
|
+
export function unknownWsCounts() {
|
|
37
|
+
return new Map(counts);
|
|
38
|
+
}
|
|
39
|
+
/** 单测隔离用。 */
|
|
40
|
+
export function resetUnknownWsTypes() {
|
|
41
|
+
counts.clear();
|
|
42
|
+
lastWarnAt.clear();
|
|
43
|
+
overflowed = 0;
|
|
44
|
+
}
|
package/extensions/webui.ts
CHANGED
|
@@ -80,7 +80,8 @@ async function openBrowser(url: string): Promise<void> {
|
|
|
80
80
|
platform === "darwin" ? ["open", url] : platform === "win32" ? ["cmd", "/c", "start", "", url] : ["xdg-open", url];
|
|
81
81
|
// 无界面环境缺少 xdg-open 等打开器时,ENOENT 以异步 'error' 事件触发,
|
|
82
82
|
// try/catch 拦不住会崩掉整个进程 —— 必须挂 error 监听。
|
|
83
|
-
|
|
83
|
+
// Windows 下 detached 子进程会新建控制台闪窗;改为不 detached 并 windowsHide。
|
|
84
|
+
spawn(cmd, rest, { stdio: "ignore", detached: process.platform !== "win32", windowsHide: true })
|
|
84
85
|
.on("error", (err) => {
|
|
85
86
|
if ((err as NodeJS.ErrnoException)?.code === "ENOENT") {
|
|
86
87
|
console.warn(
|
|
@@ -163,7 +164,13 @@ export default function (pi: ExtensionAPI): void {
|
|
|
163
164
|
PI_WEB_CWD: cwd,
|
|
164
165
|
...(process.env.PI_WEB_DATA_DIR ? {} : { PI_WEB_DATA_DIR: join(cwd, ".pi-web") }),
|
|
165
166
|
};
|
|
166
|
-
const proc = spawn(NODE, [SERVER_ENTRY], {
|
|
167
|
+
const proc = spawn(NODE, [SERVER_ENTRY], {
|
|
168
|
+
cwd,
|
|
169
|
+
env,
|
|
170
|
+
stdio: "ignore",
|
|
171
|
+
detached: process.platform !== "win32",
|
|
172
|
+
windowsHide: true,
|
|
173
|
+
});
|
|
167
174
|
proc.unref();
|
|
168
175
|
running.set(sid, { proc, port, cwd, url });
|
|
169
176
|
|