pi-web-ui 0.90.0 → 0.91.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.
Files changed (49) hide show
  1. package/CHANGELOG.md +74 -1
  2. package/README.md +7 -4
  3. package/README.zh-CN.md +6 -3
  4. package/bin/pi-web-ui.mjs +37 -10
  5. package/deploy/pi-web-ui.service +36 -31
  6. package/dist/server/agent-service.js +1309 -151
  7. package/dist/server/client-state.js +70 -5
  8. package/dist/server/compaction-markers.js +287 -0
  9. package/dist/server/delegate-task.js +12 -1
  10. package/dist/server/dsh/dsh-agent-service.js +21 -1
  11. package/dist/server/file-transfer-routes.js +4 -1
  12. package/dist/server/icon-svg.js +35 -0
  13. package/dist/server/index.js +175 -38
  14. package/dist/server/plugin-catalog.js +5 -0
  15. package/dist/server/plugin-facilities.js +42 -11
  16. package/dist/server/plugins.js +47 -8
  17. package/dist/server/prompt-composer.js +1 -1
  18. package/dist/server/protocol-version.js +1 -1
  19. package/dist/server/schedule-agent-tool.js +23 -5
  20. package/dist/server/scheduler-tasks.js +60 -1
  21. package/dist/server/scm-commitmsg.js +140 -0
  22. package/dist/server/scm.js +42 -0
  23. package/dist/server/settings-service.js +40 -0
  24. package/dist/server/skill-tool.js +120 -0
  25. package/dist/server/soft-cap.js +68 -0
  26. package/dist/server/subagent-templates.js +6 -0
  27. package/dist/server/subagents.js +157 -16
  28. package/dist/server/terminals.js +50 -20
  29. package/dist/server/tool-manager.js +7 -1
  30. package/package.json +33 -32
  31. package/plugins/catalog.json +10 -0
  32. package/web/dist/assets/TerminalPanel-GtDQ1h85.js +3 -0
  33. package/web/dist/assets/es-DIDZppIF.js +2 -0
  34. package/web/dist/assets/index-CdsKltOS.css +1 -0
  35. package/web/dist/assets/index-OoULNF70.js +363 -0
  36. package/web/dist/assets/markdown-CCeVCbci.css +1 -0
  37. package/web/dist/assets/markdown-eUQn_o9D.js +296 -0
  38. package/web/dist/assets/react-CR5VJ85Q.js +9 -0
  39. package/web/dist/assets/rolldown-runtime-hePW80VL.js +1 -0
  40. package/web/dist/assets/xterm-B1njb2Cr.css +1 -0
  41. package/web/dist/assets/xterm-Cf5xxe4g.js +16 -0
  42. package/web/dist/index.html +6 -4
  43. package/web/dist/assets/TerminalPanel-BDbdLhpT.js +0 -6
  44. package/web/dist/assets/TerminalPanel-DOrYoP_4.css +0 -32
  45. package/web/dist/assets/index-1f_DP8S3.js +0 -380
  46. package/web/dist/assets/index-DeUwDA-C.css +0 -10
  47. package/web/dist/assets/markdown-D3PKeHAZ.js +0 -311
  48. package/web/dist/assets/react-w24rH0km.js +0 -24
  49. package/web/dist/assets/xterm-B96xOxS9.js +0 -38
@@ -212,6 +212,16 @@ function cookieToken(req) {
212
212
  }
213
213
  return "";
214
214
  }
215
+ /** Express 5 命名通配 `*splat` 的取值:单段是字符串,多段是字符串数组
216
+ * (issue #225:直接 String() 会把多段用逗号拼成 "a,b.mjs",插件 vendor 子
217
+ * 目录、嵌套文件预览、插件子路径 API 全 404)。统一拼回 "/" 即得 Express 4
218
+ * 语义;下游既有的越界/包含校验(workspacePath / resolvePluginClientFile)不变。 */
219
+ function splatParam(req) {
220
+ const v = req.params.splat;
221
+ if (Array.isArray(v))
222
+ return v.join("/");
223
+ return String(v ?? "");
224
+ }
215
225
  if (AUTH_TOKEN) {
216
226
  // /api/health 保持开放:无敏感信息,容器/监控探针需要它。
217
227
  // 但绝不能因命中 /api/health 就反射下发真实 token cookie(安全漏洞:issue #45)。
@@ -318,7 +328,10 @@ app.get("/api/file", async (req, res) => {
318
328
  if (isDownload) {
319
329
  // res.download sets Content-Disposition: attachment and RFC 5987
320
330
  // filename* encoding for non-ASCII names.
321
- res.download(abs, name);
331
+ // dotfiles: allow — issue #223:Express 5 的 send 默认 dotfiles=ignore,
332
+ // 工作区/数据目录常位于隐藏目录下(如 ~/.pi-web),绝对路径含点号段会被判 404。
333
+ // 路径已由上方的 workspacePath/isAbsoluteWirePath 做工作区 containment 校验,放行安全。
334
+ res.download(abs, name, { dotfiles: "allow" });
322
335
  }
323
336
  else {
324
337
  if (isHtmlPreview) {
@@ -335,7 +348,7 @@ app.get("/api/file", async (req, res) => {
335
348
  res.setHeader("Content-Security-Policy", allowJs ? "sandbox allow-scripts" : "sandbox");
336
349
  res.setHeader("X-Content-Type-Options", "nosniff");
337
350
  }
338
- res.sendFile(abs);
351
+ res.sendFile(abs, { dotfiles: "allow" });
339
352
  }
340
353
  }
341
354
  catch {
@@ -358,10 +371,10 @@ app.get("/api/file", async (req, res) => {
358
371
  * get a sandboxed CSP (?allowJs=1 relaxes scripts only — never same-origin),
359
372
  * everything else streams with its real content type.
360
373
  */
361
- app.get("/api/preview/*", async (req, res) => {
374
+ app.get("/api/preview/*splat", async (req, res) => {
362
375
  try {
363
- // SAFETY: Express wildcard captures are indexed string route parameters.
364
- const captured = String(req.params[0] ?? "");
376
+ // 多段路径的 splat 是数组(见 splatParam),拼回 "/" 后才是线形路径。
377
+ const captured = splatParam(req);
365
378
  const ABS_MARKER = "__abs__/";
366
379
  const cid = typeof req.query.clientId === "string" ? req.query.clientId : "";
367
380
  const cs = cid ? service.get(cid) : undefined;
@@ -399,7 +412,7 @@ app.get("/api/preview/*", async (req, res) => {
399
412
  const allowJs = req.query.allowJs === "1";
400
413
  res.setHeader("Content-Security-Policy", allowJs ? "sandbox allow-scripts" : "sandbox");
401
414
  }
402
- res.sendFile(abs);
415
+ res.sendFile(abs, { dotfiles: "allow" });
403
416
  }
404
417
  catch {
405
418
  res.status(404).end("not found");
@@ -498,7 +511,10 @@ app.get("/themes/:id.css", (req, res) => {
498
511
  }
499
512
  res.setHeader("Content-Type", "text/css; charset=utf-8");
500
513
  res.setHeader("Cache-Control", "no-cache");
501
- res.sendFile(file);
514
+ // dotfiles: allow — issue #223:Express 5 的 send 默认 dotfiles=ignore,主题文件位于
515
+ // 隐藏目录下(如 ~/.pi-web/themes、npm 全局目录 ~/.local/…)时会被判 404。路径已由
516
+ // resolveThemeFile 校验(id 白名单 + 仅已知目录 + isFile),放行安全。
517
+ res.sendFile(file, { dotfiles: "allow" });
502
518
  });
503
519
  // Plugin client bundles: <dataDir>/plugins/<id>/client/* served at
504
520
  // /plugins/<id>/client/* so the frontend can import() plugin views. Only the
@@ -509,9 +525,9 @@ const PLUGINS_DIR = join(DATA_DIR, "plugins");
509
525
  // 插件 HTTP 路由挂载点:host.route("GET", "/inbox") 实际暴露为
510
526
  // /plugins-api/<id>/inbox。PI_WEB_TOKEN 鉴权(上方 app.use)自动覆盖;
511
527
  // 响应已在前面过了 express.json。注意不要在此 catch-all 里消费 body。
512
- app.all(["/plugins-api/:id/*", "/plugins-api/:id"], (req, res) => {
513
- // SAFETY: Express wildcard captures are strings indexed by 0.
514
- const rest = String(req.params[0] ?? "");
528
+ app.all(["/plugins-api/:id/*splat", "/plugins-api/:id"], (req, res) => {
529
+ // 多段子路径的 splat 是数组(见 splatParam),拼回 "/" 后再交插件路由。
530
+ const rest = splatParam(req);
515
531
  pluginMgr.handleHttp(String(req.params.id ?? ""), req.method, rest, req, res);
516
532
  });
517
533
  function proxyForwardPath(hit, url) {
@@ -583,9 +599,9 @@ app.use((req, res, next) => {
583
599
  }
584
600
  proxyHttp(hit, req, res);
585
601
  });
586
- app.get("/plugins/:id/client/*", (req, res) => {
587
- // SAFETY: express 4 的通配参数在运行时落在 params[0],但类型声明里没有 -- 显式取
588
- const rest = String(req.params[0] ?? "");
602
+ app.get("/plugins/:id/client/*splat", (req, res) => {
603
+ // 多段子路径(插件 vendor/分包)的 splat 是数组(见 splatParam),拼回 "/"。
604
+ const rest = splatParam(req);
589
605
  // 特权 DOM 门禁:声明了 dom 能力的插件,其 bundle 需用户逐个授权后才下发
590
606
  // (同源 bundle 技术上拦不住 DOM 访问,门只能放在这里;见 server/plugin-dom.ts)。
591
607
  if (pluginMgr.isDomBundleBlocked(String(req.params.id ?? ""))) {
@@ -602,7 +618,8 @@ app.get("/plugins/:id/client/*", (req, res) => {
602
618
  res.setHeader("Content-Type", "text/javascript; charset=utf-8");
603
619
  }
604
620
  res.setHeader("Cache-Control", "no-cache"); // 开发期改文件即生效
605
- res.sendFile(abs, (err) => {
621
+ // dotfiles: allow — issue #223:插件目录默认在 ~/.pi-web/plugins(隐藏目录段),同上需放行。
622
+ res.sendFile(abs, { dotfiles: "allow" }, (err) => {
606
623
  if (err && !res.headersSent)
607
624
  res
608
625
  .status(err.statusCode === 404 ? 404 : 500)
@@ -659,7 +676,8 @@ if (existsSync(webDist)) {
659
676
  const stored = readDevNoCacheSetting();
660
677
  const noStore = stored ?? envDefault;
661
678
  res.setHeader("Cache-Control", noStore ? "no-store" : "public, max-age=0");
662
- res.sendFile(join(webDist, "index.html"), (err) => {
679
+ // dotfiles: allow — issue #223:nvm 等安装路径本身在隐藏目录下(如 ~/.nvm/…),同上需放行。
680
+ res.sendFile(join(webDist, "index.html"), { dotfiles: "allow" }, (err) => {
663
681
  if (err && !res.headersSent) {
664
682
  res.status(503).send("正在更新 pi-web-ui,请稍后刷新…");
665
683
  }
@@ -967,7 +985,8 @@ pluginMgr.permissionRequester = (pluginId, req) => new Promise((resolve) => {
967
985
  pluginMgr.onPermGrantsChanged = () => pushPluginPermissions();
968
986
  // 内置定时任务(issue #184):全局 <dataDir>/scheduler-tasks.json,TTL 与
969
987
  // client-state 同级;Agent 工具建的任务优先唤醒发起对话(issue #193:
970
- // wakeConversation steer 投递,不切用户当前对话),对话不在了再回落无头伪
988
+ // wakeConversation steer 投递,不切用户当前对话),原对话不在先回落同项目
989
+ // 活跃对话(issue #231:视口兜底+自动重绑定+明确降级提示),都没有才无头伪
971
990
  // 客户端(chatFromScheduler);单次任务触发后自动删除。DSH 引擎无这俩方法时
972
991
  // executor 回 not-supported(历史里记失败,不炸进程)。
973
992
  const scheduler = new SchedulerStore(DATA_DIR, {
@@ -979,31 +998,83 @@ const scheduler = new SchedulerStore(DATA_DIR, {
979
998
  result = { ok: false, error: "当前引擎不支持定时任务(仅标准 pi 引擎)" };
980
999
  }
981
1000
  else {
982
- // 发起对话还在 → steer 唤醒它(报告直接落原对话);不在了 → 无头执行。
1001
+ const runHeadless = () => svc.chatFromScheduler({
1002
+ id: task.id,
1003
+ cwd: task.cwd,
1004
+ prompt: task.prompt,
1005
+ model: task.model,
1006
+ thinkingLevel: task.thinkingLevel,
1007
+ });
983
1008
  const target = String(task.conversationId ?? "").trim();
984
- if (target && typeof svc.wakeConversation === "function") {
985
- const w = await svc.wakeConversation(target, `[定时任务 ${task.name}] ${task.prompt}`);
1009
+ const taskFile = String(task.sessionFile ?? "").trim();
1010
+ const text = `[定时任务 ${task.name}] ${task.prompt}`;
1011
+ if (!target && !taskFile) {
1012
+ // 面板建的任务:创建时就没绑对话,保持无头语义(不抢占用户视口)。
1013
+ result = await runHeadless();
1014
+ }
1015
+ else if (typeof svc.wakeConversation === "function") {
1016
+ // 原绑定对话还在(含压缩/重启后按会话文件重认)→ steer 唤醒,报告落原对话。
1017
+ let w;
1018
+ try {
1019
+ w = await svc.wakeConversation(target, text, { sessionFile: taskFile, cwd: task.cwd });
1020
+ }
1021
+ catch (err) {
1022
+ w = { ok: false, error: err.message };
1023
+ }
986
1024
  if (w.ok) {
1025
+ // 会话继承(issue #231):压缩/重启后同文件对话换了新 id → 任务跟过去,
1026
+ // 下次触发直达,不再误判 closed/gone。单次任务随后自删,免一次写盘。
1027
+ if (!task.oneShot) {
1028
+ try {
1029
+ if (w.conversationId && w.conversationId !== target)
1030
+ scheduler.rebind(task.id, { conversationId: w.conversationId });
1031
+ if (w.sessionFile && w.sessionFile !== taskFile)
1032
+ scheduler.rebind(task.id, { sessionFile: w.sessionFile });
1033
+ }
1034
+ catch {
1035
+ // 重绑失败不影响本次已投递的唤醒
1036
+ }
1037
+ }
987
1038
  result = { ok: true, conversationId: w.conversationId };
988
1039
  }
1040
+ else if (typeof svc.wakeViewportInCwd === "function") {
1041
+ // 活跃视口兜底(issue #231):原句柄断开(切走释放/过户改名/重启),
1042
+ // 只要同项目还有用户正看着的对话,报告落那里 —— 不静默吞结果。
1043
+ const fallbackText = `[定时任务 ${task.name}|原对话不在,已转到本窗口继续] ${task.prompt}`;
1044
+ let f;
1045
+ try {
1046
+ f = await svc.wakeViewportInCwd(task.cwd, fallbackText);
1047
+ }
1048
+ catch (err) {
1049
+ f = { ok: false, error: err.message };
1050
+ }
1051
+ if (f.ok) {
1052
+ if (!task.oneShot) {
1053
+ try {
1054
+ scheduler.rebind(task.id, {
1055
+ conversationId: f.conversationId ?? "",
1056
+ sessionFile: f.sessionFile ?? "",
1057
+ });
1058
+ }
1059
+ catch {
1060
+ // 重绑失败不影响本次已投递的唤醒
1061
+ }
1062
+ }
1063
+ pushNoticeToAll("info", `定时任务「${task.name}」原对话不在,已转到同项目的活跃对话继续(原绑定 ${target || "(未知)"})。报告直接落在当前对话。`, `Scheduled task "${task.name}" moved to the project's active conversation (was ${target || "unknown"}). The report lands in the current chat.`);
1064
+ result = { ok: true, conversationId: f.conversationId };
1065
+ }
1066
+ else {
1067
+ // 降级可见性(issue #231):必须无头时明确广播去向,不静默。
1068
+ pushNoticeToAll("warning", `定时任务「${task.name}」原对话不在、同项目也无存活对话,已转后台执行(无头)。报告在后台任务面板的调度历史中查看。`, `Scheduled task "${task.name}" found no live conversation and runs headless; see its report in the background-tasks panel history.`);
1069
+ result = await runHeadless();
1070
+ }
1071
+ }
989
1072
  else {
990
- result = await svc.chatFromScheduler({
991
- id: task.id,
992
- cwd: task.cwd,
993
- prompt: task.prompt,
994
- model: task.model,
995
- thinkingLevel: task.thinkingLevel,
996
- });
1073
+ result = await runHeadless();
997
1074
  }
998
1075
  }
999
1076
  else {
1000
- result = await svc.chatFromScheduler({
1001
- id: task.id,
1002
- cwd: task.cwd,
1003
- prompt: task.prompt,
1004
- model: task.model,
1005
- thinkingLevel: task.thinkingLevel,
1006
- });
1077
+ result = await runHeadless();
1007
1078
  }
1008
1079
  }
1009
1080
  }
@@ -1442,6 +1513,32 @@ wss.on("connection", (ws) => {
1442
1513
  case "switch_conversation":
1443
1514
  void cs.switchConversation(msg.id);
1444
1515
  break;
1516
+ case "take_over_conversation":
1517
+ if (typeof service.takeOverConversation === "function") {
1518
+ void service.takeOverConversation(clientId, msg.owner, msg.id);
1519
+ }
1520
+ else {
1521
+ send({
1522
+ type: "notice",
1523
+ level: "error",
1524
+ text: "当前引擎不支持过户(take over),请用 pi 引擎",
1525
+ textEn: "Takeover is not supported by the current engine; use the pi engine.",
1526
+ });
1527
+ }
1528
+ break;
1529
+ case "peek_elsewhere_question":
1530
+ if (typeof service.peekElsewhereQuestion === "function") {
1531
+ void service.peekElsewhereQuestion(clientId, msg.owner, msg.id);
1532
+ }
1533
+ else {
1534
+ send({
1535
+ type: "notice",
1536
+ level: "error",
1537
+ text: "当前引擎不支持跨页作答,请用 pi 引擎",
1538
+ textEn: "Cross-page answering is not supported by the current engine; use the pi engine.",
1539
+ });
1540
+ }
1541
+ break;
1445
1542
  case "list_files":
1446
1543
  void cs.listFiles(msg.path);
1447
1544
  break;
@@ -1463,6 +1560,22 @@ wss.on("connection", (ws) => {
1463
1560
  case "scm_commit":
1464
1561
  void cs.scmQuery("commit", msg.reqId, { hash: msg.hash });
1465
1562
  break;
1563
+ case "scm_commitmsg":
1564
+ if (typeof cs.scmGenCommitMessage === "function") {
1565
+ void cs.scmGenCommitMessage(msg.reqId);
1566
+ }
1567
+ else {
1568
+ // DSH 等引擎没有 pi 的 ModelRuntime——照样应答一次(ok:false),
1569
+ // 前端按钮不能因引擎差异卡在转圈。
1570
+ send({
1571
+ type: "scm_data",
1572
+ reqId: msg.reqId,
1573
+ kind: "commitmsg",
1574
+ ok: false,
1575
+ error: "当前引擎不支持 AI 生成提交信息(请用 pi 引擎)/ AI commit messages need the pi engine",
1576
+ });
1577
+ }
1578
+ break;
1466
1579
  case "read_file":
1467
1580
  void cs.readFile(msg.path);
1468
1581
  break;
@@ -1703,6 +1816,7 @@ wss.on("connection", (ws) => {
1703
1816
  terminalBashIdleMs: msg.terminalBashIdleMs,
1704
1817
  editSoftEnabled: msg.editSoftEnabled,
1705
1818
  questionnaireEnabled: msg.questionnaireEnabled,
1819
+ parallelReminderEnabled: msg.parallelReminderEnabled,
1706
1820
  goalModeEnabled: msg.goalModeEnabled,
1707
1821
  thinkingWrap: msg.thinkingWrap,
1708
1822
  toolsWrap: msg.toolsWrap,
@@ -1715,6 +1829,8 @@ wss.on("connection", (ws) => {
1715
1829
  visionBridgePrompt: msg.visionBridgePrompt,
1716
1830
  subagentDefaultModel: msg.subagentDefaultModel,
1717
1831
  retryMaxAttempts: msg.retryMaxAttempts,
1832
+ softCapTokens: msg.softCapTokens,
1833
+ softCapByModel: msg.softCapByModel,
1718
1834
  reviewPrompt: msg.reviewPrompt,
1719
1835
  reviewDisabledSkills: msg.reviewDisabledSkills,
1720
1836
  markersEnabled: msg.markersEnabled,
@@ -1919,7 +2035,23 @@ wss.on("connection", (ws) => {
1919
2035
  void cs.rescanDshPatches?.();
1920
2036
  break;
1921
2037
  case "question_answer":
1922
- void cs.answerQuestion?.(msg.id, msg.answers, msg.cancelled);
2038
+ if (msg.owner) {
2039
+ // 跨页作答:答案转交持有方会话(本页不持有该问卷)。
2040
+ if (typeof service.answerElsewhereQuestion === "function") {
2041
+ void service.answerElsewhereQuestion(clientId, msg.owner, msg.id, msg.answers, msg.cancelled);
2042
+ }
2043
+ else {
2044
+ send({
2045
+ type: "notice",
2046
+ level: "error",
2047
+ text: "当前引擎不支持跨页作答,请用 pi 引擎",
2048
+ textEn: "Cross-page answering is not supported by the current engine; use the pi engine.",
2049
+ });
2050
+ }
2051
+ }
2052
+ else {
2053
+ void cs.answerQuestion?.(msg.id, msg.answers, msg.cancelled);
2054
+ }
1923
2055
  break;
1924
2056
  case "page_response":
1925
2057
  // 浏览器(page-picker 扩展经前端)对 browser_page 的回包:恢复挂起的
@@ -2212,10 +2344,15 @@ async function shutdown(signal = "SIGINT") {
2212
2344
  }
2213
2345
  shuttingDown = true;
2214
2346
  console.log("\nshutting down…");
2215
- // Windows ConPTY 兜底看门狗(PR #204 带入):主线程若死锁在 native
2216
- // ClosePseudoConsole 里,事件循环冻结,上面的 forceExitTimer 永远触发不了 ——
2347
+ // Windows ConPTY 兜底看门狗(issue #215):主线程若死锁在 native
2348
+ // ClosePseudoConsole 里,事件循环冻结,进程内的 forceExitTimer 永远触发不了 ——
2217
2349
  // 只能靠外部进程收尾。超时取 forceExit + 余量(只在进程内兜底失效时才开火);
2218
2350
  // 正常退出在 finally 里取消,避免误杀 + PID 复用竞态。
2351
+ // taskkill 必须带 /T(树杀):只杀单个 node.exe 会留下 node-pty 派生的
2352
+ // conhost --headless / bash,控制台永远不回提示符;/T 连它们一起收走。
2353
+ // 不用 detached:Windows 会给 detached 子进程分配可见控制台窗口(黑框一闪),
2354
+ // 而看门狗根本不需要脱离——父进程死锁时它照样能跑,父进程正常退出时 finally
2355
+ // 里会取消它;父进程崩掉时看门狗也没了,但那时本来就不需要收尾。
2219
2356
  // cmd.exe 是无 GUI 的控制台宿主,windowsHide 藏的是它一闪而过的黑窗口。
2220
2357
  let disarmKiller = null;
2221
2358
  if (process.platform === "win32") {
@@ -2223,8 +2360,8 @@ async function shutdown(signal = "SIGINT") {
2223
2360
  const { spawn } = await import("node:child_process");
2224
2361
  const killer = spawn("cmd.exe", [
2225
2362
  "/c",
2226
- `timeout /t ${Math.ceil((SHUTDOWN_FORCE_EXIT_MS + 5000) / 1000)} /nobreak >nul && taskkill /F /PID ${process.pid}`,
2227
- ], { detached: true, stdio: "ignore", windowsHide: true });
2363
+ `timeout /t ${Math.ceil((SHUTDOWN_FORCE_EXIT_MS + 5000) / 1000)} /nobreak >nul && taskkill /F /T /PID ${process.pid}`,
2364
+ ], { stdio: "ignore", windowsHide: true });
2228
2365
  killer.unref();
2229
2366
  disarmKiller = () => {
2230
2367
  try {
@@ -16,6 +16,7 @@
16
16
  */
17
17
  import { existsSync, mkdirSync, readFileSync, renameSync, writeFileSync } from "node:fs";
18
18
  import { dirname } from "node:path";
19
+ import { normalizeIconSvg } from "./icon-svg.js";
19
20
  import { pick } from "./i18n.js";
20
21
  /** 合法插件 id(与 server/plugins.ts 的 ID_RE 一致,防路径穿越)。 */
21
22
  const ID_RE = /^[A-Za-z0-9_-]+$/;
@@ -77,6 +78,7 @@ function toEntry(raw, builtin) {
77
78
  const description = typeof raw.description === "string" && raw.description.trim() ? raw.description.trim() : undefined;
78
79
  const descriptionEn = typeof raw.descriptionEn === "string" && raw.descriptionEn.trim() ? raw.descriptionEn.trim() : undefined;
79
80
  const icon = typeof raw.icon === "string" && raw.icon.trim() ? raw.icon.trim() : undefined;
81
+ const iconSvg = normalizeIconSvg(raw.iconSvg);
80
82
  const homepage = typeof raw.homepage === "string" && raw.homepage.trim() ? raw.homepage.trim() : undefined;
81
83
  return {
82
84
  id,
@@ -86,6 +88,7 @@ function toEntry(raw, builtin) {
86
88
  ...(description ? { description } : {}),
87
89
  ...(descriptionEn ? { descriptionEn } : {}),
88
90
  ...(icon ? { icon } : {}),
91
+ ...(iconSvg ? { iconSvg } : {}),
89
92
  ...(homepage ? { homepage } : {}),
90
93
  };
91
94
  }
@@ -146,6 +149,7 @@ lang) {
146
149
  ? { description: input.description.trim() }
147
150
  : {}),
148
151
  ...(typeof input?.icon === "string" && input.icon.trim() ? { icon: input.icon.trim() } : {}),
152
+ ...(normalizeIconSvg(input?.iconSvg) ? { iconSvg: normalizeIconSvg(input?.iconSvg) } : {}),
149
153
  });
150
154
  atomicWrite(customPath, { entries: next });
151
155
  return toEntry(next[next.length - 1], false);
@@ -223,6 +227,7 @@ export function writeCustomCatalog(customPath, incoming, replace) {
223
227
  ...(e.description ? { description: e.description } : {}),
224
228
  ...(e.descriptionEn ? { descriptionEn: e.descriptionEn } : {}),
225
229
  ...(e.icon ? { icon: e.icon } : {}),
230
+ ...(e.iconSvg ? { iconSvg: e.iconSvg } : {}),
226
231
  ...(e.homepage ? { homepage: e.homepage } : {}),
227
232
  }));
228
233
  atomicWrite(customPath, { entries });
@@ -16,7 +16,7 @@
16
16
  * 不能防同一用户账号下的完整进程妥协——本地个人工具的合理折衷。
17
17
  */
18
18
  import { createCipheriv, createDecipheriv, randomBytes } from "node:crypto";
19
- import { chmodSync, existsSync, mkdirSync, readFileSync, renameSync, writeFileSync } from "node:fs";
19
+ import { chmodSync, existsSync, mkdirSync, readFileSync, renameSync, statSync, writeFileSync } from "node:fs";
20
20
  import { readFile as fspReadFile, readdir as fspReaddir, rm as fspRm, mkdir as fspMkdir, writeFile as fspWriteFile, appendFile as fspAppendFile, stat as fspStat, } from "node:fs/promises";
21
21
  import { dirname, join, relative, resolve } from "node:path";
22
22
  import { createRequire } from "node:module";
@@ -31,24 +31,52 @@ function atomicWrite(file, data) {
31
31
  // ---------------------------------------------------------------------------
32
32
  // storage
33
33
  // ---------------------------------------------------------------------------
34
- /** 每插件的 JSON 文件 KV。所有方法同步(数据量小,避免并发写乱序)。 */
34
+ /** 每插件的 JSON 文件 KV。所有方法同步(数据量小,避免并发写乱序)。
35
+ *
36
+ * 缓存按文件 mtimeMs 失效(而非「首次读入后永不失效」):storage.json 有两个
37
+ * 写入者 —— 本类,以及宿主 settings 面板的 saveSettingsValues(直写磁盘的
38
+ * settings 键,不经过本缓存)。插件长轮询里每隔几秒就 store.set("cursor", …)
39
+ * 一次,若缓存永不失效,set() 会把整份旧快照回写,把面板刚保存的 settings
40
+ * 抹掉 —— 表现为「设置重启即丢」。mtime 失效让下一次 load() 重读,两个写入者
41
+ * 各自保留自己的键。 */
35
42
  export class PluginStorage {
36
43
  file;
37
44
  cache;
45
+ /** 缓存对应的文件 mtimeMs(undefined = 文件尚不存在或不可 stat)。 */
46
+ cacheMtimeMs;
38
47
  constructor(file) {
39
48
  this.file = file;
40
49
  }
41
- load() {
42
- if (this.cache)
43
- return this.cache;
50
+ mtime() {
44
51
  try {
45
- const parsed = JSON.parse(readFileSync(this.file, "utf8"));
46
- this.cache = parsed && typeof parsed === "object" ? parsed : {};
52
+ return statSync(this.file).mtimeMs;
47
53
  }
48
54
  catch {
49
- this.cache = {}; // 不存在/损坏 = 空表(损坏不致命,重新积累)
55
+ return undefined; // 不存在 / 不可 stat
50
56
  }
51
- return this.cache;
57
+ }
58
+ /** 从磁盘重读(不存在 / 损坏 = 空表)并同步缓存与 mtime。 */
59
+ readFromDisk(mtimeMs = this.mtime()) {
60
+ let parsed = {};
61
+ if (mtimeMs !== undefined) {
62
+ try {
63
+ const raw = JSON.parse(readFileSync(this.file, "utf8"));
64
+ if (raw && typeof raw === "object")
65
+ parsed = raw;
66
+ }
67
+ catch {
68
+ parsed = {}; // 损坏 = 空表(不致命,重新积累)
69
+ }
70
+ }
71
+ this.cache = parsed;
72
+ this.cacheMtimeMs = mtimeMs;
73
+ return parsed;
74
+ }
75
+ load() {
76
+ const mtimeMs = this.mtime();
77
+ if (this.cache && this.cacheMtimeMs === mtimeMs)
78
+ return this.cache;
79
+ return this.readFromDisk(mtimeMs);
52
80
  }
53
81
  get(key, fallback) {
54
82
  const v = this.load()[key];
@@ -60,7 +88,10 @@ export class PluginStorage {
60
88
  set(key, value) {
61
89
  if (!key)
62
90
  throw new Error("storage.set: key 不能为空");
63
- const store = this.load();
91
+ // 写前必重读磁盘:宿主 settings 面板的 saveSettingsValues 直写同一文件的
92
+ // settings 键(不经过本缓存),拿旧快照整份回写会把它抹掉。写是低频路径,
93
+ // 重读的代价可忽略;mtime 粒度即使同毫秒也抹不掉(这里是无条件重读)。
94
+ const store = this.readFromDisk();
64
95
  store[key] = value;
65
96
  try {
66
97
  atomicWrite(this.file, JSON.stringify(store));
@@ -70,7 +101,7 @@ export class PluginStorage {
70
101
  }
71
102
  }
72
103
  delete(key) {
73
- const store = this.load();
104
+ const store = this.readFromDisk(); // 同 set:写前重读,不拿旧快照回写
74
105
  if (!(key in store))
75
106
  return;
76
107
  delete store[key];
@@ -26,6 +26,7 @@ import { parseCronSpec, nextCronFire, loadScheduleRecords, saveScheduleRecords,
26
26
  import { PluginPermissionStore } from "./plugin-permissions.js";
27
27
  import { readCatalog, addCustomEntry, removeCustomEntry } from "./plugin-catalog.js";
28
28
  import { PluginGrantsStore, normalizeGrantPath } from "./plugin-grants.js";
29
+ import { normalizeIconSvg } from "./icon-svg.js";
29
30
  import { PluginDomConsent, declarationWantsDom } from "./plugin-dom.js";
30
31
  // 工作区根的归一化与 client-state 共用一份(同一份语义:只收绝对路径 / 去重 / 上限)。
31
32
  import { normalizeWorkspaceRoots } from "./client-state.js";
@@ -371,6 +372,9 @@ export function parseUiItem(raw, slot, diagnostics) {
371
372
  ...(align ? { align } : {}),
372
373
  ...(trimStr(o.labelEn, 60) ? { labelEn: trimStr(o.labelEn, 60) } : {}),
373
374
  ...(trimStr(o.icon, 16) ? { icon: trimStr(o.icon, 16) } : {}),
375
+ ...(normalizeIconSvg(o.iconSvg)
376
+ ? { iconSvg: normalizeIconSvg(o.iconSvg) }
377
+ : {}),
374
378
  ...(trimStr(o.hint, 200) ? { hint: trimStr(o.hint, 200) } : {}),
375
379
  ...(trimStr(o.hintEn, 200) ? { hintEn: trimStr(o.hintEn, 200) } : {}),
376
380
  kind,
@@ -538,6 +542,9 @@ export function parseUiArrange(raw, diagnostics) {
538
542
  ...(trimStr(o.label, 60) ? { label: trimStr(o.label, 60) } : {}),
539
543
  ...(trimStr(o.hint, 200) ? { hint: trimStr(o.hint, 200) } : {}),
540
544
  ...(trimStr(o.icon, 16) ? { icon: trimStr(o.icon, 16) } : {}),
545
+ ...(normalizeIconSvg(o.iconSvg)
546
+ ? { iconSvg: normalizeIconSvg(o.iconSvg) }
547
+ : {}),
541
548
  });
542
549
  }
543
550
  return out;
@@ -1582,15 +1589,15 @@ export class PluginManager {
1582
1589
  }
1583
1590
  }
1584
1591
  /** 当前目录清单(重扫 manifest,不重新 import)。 */
1585
- async list() {
1586
- return this.scan();
1592
+ async list(lang) {
1593
+ return this.scan(lang);
1587
1594
  }
1588
1595
  /**
1589
1596
  * attach 时调用:重扫目录 + 激活尚未加载的新插件。
1590
1597
  * 返回给浏览器的目录(含激活失败的条目,前端显示为不可用)。
1591
1598
  */
1592
1599
  async ensureLoaded(lang) {
1593
- const found = await this.scan();
1600
+ const found = await this.scan(lang);
1594
1601
  for (const info of found) {
1595
1602
  if (this.loaded.has(info.id) || this.attempted.has(info.id))
1596
1603
  continue;
@@ -1607,10 +1614,13 @@ export class PluginManager {
1607
1614
  }
1608
1615
  return found.map((f) => {
1609
1616
  const base = this.loaded.get(f.id)?.info ?? f;
1617
+ // 运行相位(设置面板清单用):宿主持有实例即 active(含激活失败的占位行;
1618
+ // 纯前端插件无 index.mjs、从未进 loaded 表,保持非 active)。
1619
+ const withPhase = { ...base, active: this.loaded.has(f.id) };
1610
1620
  const agentTools = this.agentToolsSnapshot(f.id);
1611
1621
  if (agentTools.length)
1612
- return { ...base, agentTools };
1613
- const { agentTools: _drop, ...rest } = base;
1622
+ return { ...withPhase, agentTools };
1623
+ const { agentTools: _drop, ...rest } = withPhase;
1614
1624
  return rest;
1615
1625
  });
1616
1626
  }
@@ -1702,7 +1712,8 @@ export class PluginManager {
1702
1712
  this.pluginLogs.clear();
1703
1713
  }
1704
1714
  /** 读 manifest 清单;坏目录(无 manifest/id 非法)直接跳过。 */
1705
- async scan() {
1715
+ async scan(lang) {
1716
+ const l = lang?.() ?? "en";
1706
1717
  let names;
1707
1718
  try {
1708
1719
  names = await readdir(this.pluginsDir);
@@ -1731,6 +1742,7 @@ export class PluginManager {
1731
1742
  version: typeof m.version === "string" ? m.version : undefined,
1732
1743
  description: typeof m.description === "string" ? m.description : undefined,
1733
1744
  icon: typeof m.icon === "string" && m.icon.trim() ? m.icon.trim() : undefined,
1745
+ iconSvg: normalizeIconSvg(m.iconSvg),
1734
1746
  hasClient: existsSync(join(dir, "client", "entry.mjs")),
1735
1747
  error: this.loaded.get(name)?.info.error,
1736
1748
  // manifest 声明的能力清单(fs/net/tools…)——设置面板展示用
@@ -1840,7 +1852,19 @@ export class PluginManager {
1840
1852
  }
1841
1853
  }
1842
1854
  catch {
1843
- continue; // 无 manifest / JSON 坏 —— 不是插件
1855
+ // 无 manifest.json / JSON 解析失败 —— 占位行展示(坏插件不跳过,
1856
+ // 设置面板清单标红 + 给原因;ensureLoaded 照例跳过激活)。
1857
+ const msg = pick(l, `manifest.json 缺失或解析失败(${name}/),不是有效插件 —— 修复或移走该目录后点“重新扫描”`, `manifest.json missing or unparsable (${name}/), not a valid plugin — fix or remove the directory, then hit Rescan`, "plugins.manifest.broken", { name });
1858
+ console.error(`[plugin:${name}] ${msg}`);
1859
+ const brokenDiags = this.diagnosticsOf(name);
1860
+ out.push({
1861
+ id: name,
1862
+ name,
1863
+ hasClient: existsSync(join(dir, "client", "entry.mjs")),
1864
+ error: msg,
1865
+ view: false,
1866
+ ...(brokenDiags ? { diagnostics: brokenDiags } : {}),
1867
+ });
1844
1868
  }
1845
1869
  }
1846
1870
  // 删掉的目录不同时清快照:门禁会把不存在的 id 当 403,正确的应该是 404。
@@ -2073,7 +2097,22 @@ export class PluginManager {
2073
2097
  if (text.length > 8000)
2074
2098
  return Promise.reject(new Error("chat: text 超长(>8000 字),请裁剪后重发"));
2075
2099
  const accountId = String(req?.accountId ?? "default").slice(0, 64);
2076
- return self.chatProvider(info.id, { text, accountId });
2100
+ // issue #226:透传定时任务对齐的四件套(各按长度封顶,语义校验归宿主 chatFromPlugin)。
2101
+ const r = (req ?? {});
2102
+ const passthrough = { text, accountId };
2103
+ const cwd = String(r.cwd ?? "").trim();
2104
+ if (cwd)
2105
+ passthrough.cwd = cwd.slice(0, 1024);
2106
+ const conversationId = String(r.conversationId ?? "").trim();
2107
+ if (conversationId)
2108
+ passthrough.conversationId = conversationId.slice(0, 128);
2109
+ const model = String(r.model ?? "").trim();
2110
+ if (model)
2111
+ passthrough.model = model.slice(0, 256);
2112
+ const thinkingLevel = String(r.thinkingLevel ?? "").trim();
2113
+ if (thinkingLevel)
2114
+ passthrough.thinkingLevel = thinkingLevel.slice(0, 64);
2115
+ return self.chatProvider(info.id, passthrough);
2077
2116
  };
2078
2117
  const host = {
2079
2118
  broadcast: (payload) => this.broadcast(info.id, payload),
@@ -149,7 +149,7 @@ export function buildSkillsText(skills, lang = "en", fullText = false) {
149
149
  return "";
150
150
  const lines = [
151
151
  pick(lang, "以下技能为特定任务提供专门的指令。", "The following skills provide specialized instructions for specific tasks.", "prompt.skills.intro.specialized"),
152
- pick(lang, "当任务与某技能的描述相符时,用 read 工具加载该技能文件。", "Use the read tool to load a skill's file when the task matches its description.", "prompt.skills.intro.use.read"),
152
+ pick(lang, "当任务与某技能的描述相符时,用 skill 工具按名称加载该技能全文。", "Use the skill tool to load a skill's full text by name when the task matches its description.", "prompt.skills.intro.use.skill"),
153
153
  pick(lang, "当技能文件引用相对路径时,以技能目录(SKILL.md 的父目录 / 该路径的 dirname)为基准解析,并在工具命令中使用解析后的绝对路径。", "When a skill file references a relative path, resolve it against the skill directory (parent of SKILL.md / dirname of the path) and use that absolute path in tool commands.", "prompt.skills.intro.resolve.path"),
154
154
  "",
155
155
  "<available_skills>",
@@ -8,4 +8,4 @@
8
8
  * its own copy in web/src/protocol-version.ts; scripts/check-protocol-sync.mjs
9
9
  * verifies the two never drift.
10
10
  */
11
- export const PROTOCOL_VERSION = 17;
11
+ export const PROTOCOL_VERSION = 18;