pi-web-ui 0.98.0 → 0.100.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 (69) hide show
  1. package/CHANGELOG.md +85 -5
  2. package/README.md +2 -1
  3. package/README.zh-CN.md +2 -1
  4. package/bin/pi-web-ui.mjs +174 -36
  5. package/dist/server/agent-service.js +1169 -152
  6. package/dist/server/approval-rules.js +95 -23
  7. package/dist/server/attachments.js +55 -0
  8. package/dist/server/bg-servers.js +82 -2
  9. package/dist/server/claim-files-tool.js +4 -4
  10. package/dist/server/client-state.js +41 -0
  11. package/dist/server/compact-context-tool.js +10 -9
  12. package/dist/server/conversation-read-tool.js +7 -8
  13. package/dist/server/conversation-touches.js +19 -0
  14. package/dist/server/delegate-task.js +10 -14
  15. package/dist/server/dsh/dsh-agent-service.js +116 -12
  16. package/dist/server/edit-soft-tool.js +10 -12
  17. package/dist/server/eval-tool.js +26 -6
  18. package/dist/server/goal-evidence.js +173 -0
  19. package/dist/server/goal-service.js +78 -15
  20. package/dist/server/hashline-engine.js +198 -31
  21. package/dist/server/index.js +55 -9
  22. package/dist/server/load-tools-tool.js +100 -0
  23. package/dist/server/lsp-tool.js +10 -16
  24. package/dist/server/marker-service.js +53 -12
  25. package/dist/server/markers/builtins/action.js +108 -0
  26. package/dist/server/markers/index.js +4 -1
  27. package/dist/server/model-enrich.js +2 -0
  28. package/dist/server/patch-remote-catalog.js +28 -10
  29. package/dist/server/patch-tool.js +74 -38
  30. package/dist/server/plan-manager.js +71 -30
  31. package/dist/server/plan-mode.js +7 -6
  32. package/dist/server/plugin-api-catalog.js +10 -3
  33. package/dist/server/plugin-conversation-view.js +47 -0
  34. package/dist/server/plugins.js +113 -26
  35. package/dist/server/present-files-tool.js +10 -11
  36. package/dist/server/preset-fields.js +693 -0
  37. package/dist/server/preset-share.js +1178 -0
  38. package/dist/server/prompt-composer.js +23 -9
  39. package/dist/server/protocol-version.js +1 -1
  40. package/dist/server/read-tool.js +8 -10
  41. package/dist/server/resolve-global-sdk.js +80 -18
  42. package/dist/server/schedule-agent-tool.js +147 -146
  43. package/dist/server/sdk-origin.js +125 -37
  44. package/dist/server/settings-service.js +103 -84
  45. package/dist/server/skill-tool.js +3 -4
  46. package/dist/server/subagents.js +15 -22
  47. package/dist/server/terminals.js +29 -39
  48. package/dist/server/tool-approval.js +3 -1
  49. package/dist/server/tool-manager.js +63 -27
  50. package/dist/server/tool-prompt-overrides.js +158 -0
  51. package/dist/server/tool-prompts.js +42 -0
  52. package/dist/server/tool-signature.js +104 -0
  53. package/dist/server/update-check.js +0 -15
  54. package/dist/server/workspace-snapshot.js +42 -11
  55. package/package.json +5 -3
  56. package/plugin-sdk/README.md +8 -4
  57. package/plugin-sdk/import-check.mjs +210 -0
  58. package/plugin-sdk/index.d.ts +4 -1
  59. package/plugin-sdk/index.mjs +1 -0
  60. package/plugins/catalog.json +33 -2
  61. package/themes/zhupi-dark.css +1 -1
  62. package/themes/zhupi.css +1 -1
  63. package/web/dist/assets/{TerminalPanel-C88r3r3Q.js → TerminalPanel-C3I1Mc-C.js} +2 -2
  64. package/web/dist/assets/index-B-E2EM_0.css +1 -0
  65. package/web/dist/assets/index-BYumAaGi.js +379 -0
  66. package/web/dist/index.html +2 -2
  67. package/web/dist/sw.js +1 -1
  68. package/web/dist/assets/index-2jDtjHvW.css +0 -1
  69. package/web/dist/assets/index-BjdK7s1M.js +0 -368
@@ -17,7 +17,7 @@
17
17
  * - activate 抛错只标记 error 字段并记日志,绝不影响主进程。
18
18
  */
19
19
  import { mkdir, readdir, readFile, rm, stat, writeFile } from "node:fs/promises";
20
- import { existsSync, readFileSync, renameSync, watch as fsWatch, writeFileSync } from "node:fs";
20
+ import { cpSync, existsSync, readFileSync, readdirSync, renameSync, watch as fsWatch, writeFileSync } from "node:fs";
21
21
  import { dirname, isAbsolute, join, relative, resolve, sep } from "node:path";
22
22
  import { pathToFileURL, fileURLToPath } from "node:url";
23
23
  import { pick } from "./i18n.js";
@@ -34,12 +34,37 @@ import { AGENT_TOOL_CATALOG } from "./tool-manager.js";
34
34
  import { applyPostEdit, denialText, freezeParams, isBlockingDecision, normalizePostEdit, normalizePreDecision, withGuardTimeout, } from "./plugin-tool-guard.js";
35
35
  // 工作区根的归一化与 client-state 共用一份(同一份语义:只收绝对路径 / 去重 / 上限)。
36
36
  import { normalizeWorkspaceRoots } from "./client-state.js";
37
+ import { compareVersions, ensureBackup } from "./plugin-updater.js";
37
38
  import { createProject, isInsideRoot, realPathOfNearest, } from "./plugin-project.js";
38
39
  import { createHash } from "node:crypto";
39
40
  import { execFile } from "node:child_process";
40
41
  import { promisify } from "node:util";
41
42
  /** 合法插件 id:字母/数字/下划线/连字符,防路径穿越(同 themes.ts 的做法)。 */
42
43
  const ID_RE = /^[A-Za-z0-9_-]+$/;
44
+ /** 交给插件前的防御性拷贝:provider 回的是快照管线的**活引用** —— messages 数组与
45
+ * 元素对象都被 60ms 推送管线缓存复用(lastMessagesArray / uiMessageCache),插件原地
46
+ * 改一个字段/挪一个元素,污染的就是推给真实客户端的快照。浅拷数组 + 元素对象
47
+ * structuredClone;元素是纯 JSON 结构(details 已过 JSON.stringify 闸),structuredClone
48
+ * 不会失败,JSON 往返只是万一携带不可克隆值时的兜底。
49
+ * `getActiveConversation` 与 `emitClientModelChanged` 共用(两处都要发快照给插件)。 */
50
+ function copySnapshotForPlugins(snap) {
51
+ return {
52
+ ...snap,
53
+ messages: snap.messages.map((m) => {
54
+ try {
55
+ return structuredClone(m);
56
+ }
57
+ catch {
58
+ try {
59
+ return JSON.parse(JSON.stringify(m));
60
+ }
61
+ catch {
62
+ return { ...m }; // 连 JSON 往返都失败(循环引用):至少不共享顶层对象
63
+ }
64
+ }
65
+ }),
66
+ };
67
+ }
43
68
  /** 每插件保留最近 N 条运行时日志(内存封顶,防刷爆)。 */
44
69
  export const PLUGIN_LOG_CAP = 200;
45
70
  /** 单条日志文本截断上限(字符)。 */
@@ -266,6 +291,7 @@ export const UI_SLOTS = new Set([
266
291
  "contextmenu.file",
267
292
  "contextmenu.toolcall",
268
293
  "settings.pages",
294
+ "tasks.panel",
269
295
  "leftpanel.sessions",
270
296
  "chat.header",
271
297
  "chat.empty",
@@ -1554,7 +1580,8 @@ export class PluginManager {
1554
1580
  guardDenialText(verdict, pluginId, lang = "en") {
1555
1581
  return denialText(verdict, pluginId, lang);
1556
1582
  }
1557
- /** index.ts 注入:读取当前打开对话的快照(轨迹类插件经 host.getActiveConversation 调用)。 */
1583
+ /** index.ts 注入:读取对话快照(轨迹类插件经 host.getActiveConversation 调用)。
1584
+ * #542:按 clientId 取「该标签页正在看的对话」;缺省回落「最近活跃的非子代理」。 */
1558
1585
  conversationProvider = undefined;
1559
1586
  /** index.ts 注入:插件无头调用 agent(微信通道等经 host.chat 调用)。 */
1560
1587
  chatProvider = undefined;
@@ -1674,39 +1701,37 @@ export class PluginManager {
1674
1701
  this.chatWaiters.push(entry);
1675
1702
  });
1676
1703
  }
1677
- /** 当前打开对话的快照(无提供者/暂无对话时返回 null)。 */
1678
- getActiveConversation() {
1704
+ /** 当前打开对话的快照(无提供者/暂无对话时返回 null)。
1705
+ * #542:options.clientId 取该标签页正在看的对话;缺省 = 全局回落
1706
+ * (最近活跃的**非子代理**会话,子代理不再把用户正在看的对话挤出去)。 */
1707
+ getActiveConversation(options) {
1679
1708
  let snap;
1680
1709
  try {
1681
- snap = this.conversationProvider?.() ?? null;
1710
+ snap = this.conversationProvider?.(options) ?? null;
1682
1711
  }
1683
1712
  catch (err) {
1684
1713
  console.error("[plugins] conversationProvider failed:", err);
1685
1714
  return null;
1686
1715
  }
1687
- if (!snap)
1688
- return null;
1689
- // 交给插件前做防御性拷贝:provider 回的是快照管线的**活引用** —— messages
1690
- // 数组与元素对象都被 60ms 推送管线缓存复用(lastMessagesArray / uiMessageCache),
1691
- // 插件原地改一个字段/挪一个元素,污染的就是推给真实客户端的快照。浅拷数组 +
1692
- // 元素对象 structuredClone;元素是纯 JSON 结构(details 已过 JSON.stringify 闸),
1693
- // structuredClone 不会失败,JSON 往返只是万一携带不可克隆值时的兜底。
1694
- return {
1695
- ...snap,
1696
- messages: snap.messages.map((m) => {
1716
+ return snap ? copySnapshotForPlugins(snap) : null;
1717
+ }
1718
+ /** agent-service 调:某客户端的对话模型切换成功(#542)—— 订阅
1719
+ * host.onClientModelChanged 的插件立即收到新快照(异常隔离,不会拖慢切换)。 */
1720
+ emitClientModelChanged(snap) {
1721
+ const copy = copySnapshotForPlugins(snap);
1722
+ for (const p of this.loaded.values()) {
1723
+ const hs = p.modelChangeHandlers;
1724
+ if (!hs || hs.size === 0)
1725
+ continue;
1726
+ for (const h of hs) {
1697
1727
  try {
1698
- return structuredClone(m);
1728
+ h(copy);
1699
1729
  }
1700
- catch {
1701
- try {
1702
- return JSON.parse(JSON.stringify(m));
1703
- }
1704
- catch {
1705
- return { ...m }; // 连 JSON 往返都失败(循环引用):至少不共享顶层对象
1706
- }
1730
+ catch (err) {
1731
+ console.error(`[plugin:${p.info.id}] client-model-changed handler failed:`, err);
1707
1732
  }
1708
- }),
1709
- };
1733
+ }
1734
+ }
1710
1735
  }
1711
1736
  /** agent-service 调:当前打开对话变了(切历史会话/切 running 对话/新对话)——
1712
1737
  * 轨迹类插件靠它重拉时间线(异常隔离)。 */
@@ -2154,8 +2179,64 @@ export class PluginManager {
2154
2179
  this.runtimeDiags.clear();
2155
2180
  this.pluginLogs.clear();
2156
2181
  }
2182
+ /**
2183
+ * 检查随包内置官方插件(<pkgRoot>/plugins/)与用户数据目录(<dataDir>/plugins/)的版本。
2184
+ * 若用户已安装的官方插件版本落后于当前随包版本,自动备份旧版本并增量同步更新代码(保留用户数据/设置)。
2185
+ */
2186
+ syncBuiltinPlugins() {
2187
+ if (!this.builtinCatalogPath || !existsSync(this.builtinCatalogPath))
2188
+ return;
2189
+ const builtinDir = dirname(this.builtinCatalogPath);
2190
+ if (!existsSync(builtinDir))
2191
+ return;
2192
+ let names;
2193
+ try {
2194
+ names = readdirSync(this.pluginsDir);
2195
+ }
2196
+ catch {
2197
+ return;
2198
+ }
2199
+ // 只有在官方 catalog.json 中登记的官方插件,才属于随宿主自动同步维护的对象(避免误覆盖测试或非 catalog 目录)
2200
+ let builtinCatalogIds;
2201
+ try {
2202
+ const catalogEntries = JSON.parse(readFileSync(this.builtinCatalogPath, "utf8"));
2203
+ builtinCatalogIds = new Set(catalogEntries.map((e) => e?.id).filter((id) => typeof id === "string"));
2204
+ }
2205
+ catch {
2206
+ builtinCatalogIds = new Set();
2207
+ }
2208
+ for (const name of names) {
2209
+ if (!ID_RE.test(name) || !builtinCatalogIds.has(name))
2210
+ continue;
2211
+ const targetDir = join(this.pluginsDir, name);
2212
+ const sourceDir = join(builtinDir, name);
2213
+ const targetManifest = join(targetDir, "manifest.json");
2214
+ const sourceManifest = join(sourceDir, "manifest.json");
2215
+ if (!existsSync(sourceDir) || !existsSync(sourceManifest) || !existsSync(targetManifest)) {
2216
+ continue;
2217
+ }
2218
+ try {
2219
+ const srcRaw = JSON.parse(readFileSync(sourceManifest, "utf8"));
2220
+ const tgtRaw = JSON.parse(readFileSync(targetManifest, "utf8"));
2221
+ const srcVer = typeof srcRaw.version === "string" ? srcRaw.version.trim() : "";
2222
+ const tgtVer = typeof tgtRaw.version === "string" ? tgtRaw.version.trim() : "";
2223
+ if (srcVer && tgtVer && compareVersions(srcVer, tgtVer) > 0) {
2224
+ ensureBackup(this.dataDir, name, { source: `builtin:${srcVer}` });
2225
+ cpSync(sourceDir, targetDir, {
2226
+ recursive: true,
2227
+ filter: (s) => !/(^|[\\/])(\.git|node_modules)([\\/]|$)/.test(s),
2228
+ });
2229
+ console.log(`[plugins] 内置插件 ${name} 随宿主自动同步升级:${tgtVer} → ${srcVer}`);
2230
+ }
2231
+ }
2232
+ catch (err) {
2233
+ console.warn(`[plugins] 自动同步内置插件 ${name} 失败:`, err);
2234
+ }
2235
+ }
2236
+ }
2157
2237
  /** 读 manifest 清单;坏目录(无 manifest/id 非法)直接跳过。 */
2158
2238
  async scan(lang) {
2239
+ this.syncBuiltinPlugins();
2159
2240
  const l = lang?.() ?? "en";
2160
2241
  let names;
2161
2242
  try {
@@ -2505,6 +2586,7 @@ export class PluginManager {
2505
2586
  const postGuards = new Set();
2506
2587
  const runHandlers = new Set();
2507
2588
  const convChangeHandlers = new Set();
2589
+ const modelChangeHandlers = new Set();
2508
2590
  const attachHandlers = new Set();
2509
2591
  const cwdHandlers = new Set();
2510
2592
  const httpRoutes = new Map();
@@ -2848,7 +2930,11 @@ export class PluginManager {
2848
2930
  convChangeHandlers.add(h);
2849
2931
  return () => convChangeHandlers.delete(h);
2850
2932
  },
2851
- getActiveConversation: () => self.getActiveConversation(),
2933
+ onClientModelChanged: (h) => {
2934
+ modelChangeHandlers.add(h);
2935
+ return () => modelChangeHandlers.delete(h);
2936
+ },
2937
+ getActiveConversation: (opts) => self.getActiveConversation(opts),
2852
2938
  chat: (req) => sendChat(req),
2853
2939
  chatWait: async (req, opts) => {
2854
2940
  try {
@@ -3752,6 +3838,7 @@ export class PluginManager {
3752
3838
  postGuards,
3753
3839
  runHandlers,
3754
3840
  convChangeHandlers,
3841
+ modelChangeHandlers,
3755
3842
  attachHandlers,
3756
3843
  cwdHandlers,
3757
3844
  effects,
@@ -239,27 +239,26 @@ export function makePresentFilesTool(fallbackCwd, options = {}) {
239
239
  return defineTool({
240
240
  name: PRESENT_FILES_TOOL_NAME,
241
241
  label: "Show files to the user",
242
- description: "Show files to the user as preview cards in the chat: images/videos/audio inline, text/markdown/HTML in a preview dialog; each card can open locally, reveal in file manager, download, or copy path. " +
243
- "Use whenever the user should LOOK at an artifact you produced or changed (screenshot, chart, diagram, video/audio, report, log, build output). " +
244
- "Give workspace-relative paths (max 12 items); `title`/`note` show above the cards, `caption` next to the file name, `focus: true` opens that item in the preview dialog immediately. " +
245
- "Do not use for files you merely read while reasoning, and do not repeat file contents in your reply.",
246
- promptSnippet: "show images/videos/text files to the user as preview cards",
242
+ description: "Show files to the user as preview cards in the chat: images/videos/audio inline, text/markdown/HTML in a preview dialog; " +
243
+ "each card can open locally, reveal in file manager, download, or copy path. " +
244
+ "Give workspace-relative paths (max 12); `title`/`note` show above the cards, `caption` next to the name, `focus: true` opens that item immediately.",
245
+ promptSnippet: "show a produced artifact (image/chart/report) to the user",
247
246
  promptGuidelines: [
248
- "After producing something visual or user-facing (screenshot, chart, video, report, log, build output), call present_files so the user can actually see it instead of only printing the path",
249
- "Do not call present_files for ordinary source edits the user did not ask to see, and never call it twice for the same file in one turn",
247
+ "After producing something visual or user-facing (screenshot, chart, video, report, log, build output), show it with present_files instead of only printing the path",
248
+ "Skip it for ordinary source edits the user did not ask to see; never call it twice for the same file in one turn",
250
249
  ],
251
250
  parameters: Type.Object({
252
251
  title: Type.Optional(Type.String({
253
- description: "Optional card title shown above the file cards (short, e.g. 'Q3 revenue chart').",
252
+ description: "Optional card title (short, e.g. 'Q3 revenue chart').",
254
253
  })),
255
254
  note: Type.Optional(Type.String({
256
- description: "Optional one-line note above the cards (markdown, e.g. what changed / which one to look at first).",
255
+ description: "Optional one-line note above the cards (markdown).",
257
256
  })),
258
257
  items: Type.Array(Type.Object({
259
258
  path: Type.String({ description: "File path (workspace-relative like 'docs/chart.png', or absolute)." }),
260
- caption: Type.Optional(Type.String({ description: "Optional short caption shown next to the file name." })),
259
+ caption: Type.Optional(Type.String({ description: "Optional short caption next to the file name." })),
261
260
  focus: Type.Optional(Type.Boolean({
262
- description: "true → the client opens this item in the preview dialog immediately (use for the one file that matters most).",
261
+ description: "true → open this item in the preview dialog immediately.",
263
262
  })),
264
263
  }), { description: `Files to show (1-${MAX_PRESENT_ITEMS}).` }),
265
264
  }),