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.
Files changed (109) hide show
  1. package/CHANGELOG.md +90 -1
  2. package/README.md +48 -45
  3. package/README.zh-CN.md +43 -40
  4. package/bin/pi-web-ui.mjs +172 -40
  5. package/dist/server/agent-formatters.js +191 -0
  6. package/dist/server/agent-service-daemon.js +1266 -0
  7. package/dist/server/agent-service.js +9851 -11892
  8. package/dist/server/approval-default-rules.js +158 -0
  9. package/dist/server/approval-rules.js +130 -184
  10. package/dist/server/ask-user-question-tool.js +94 -0
  11. package/dist/server/attachment-store.js +38 -1
  12. package/dist/server/attachments.js +55 -0
  13. package/dist/server/bash-tool.js +83 -0
  14. package/dist/server/bg-servers.js +82 -2
  15. package/dist/server/browser-page-tool.js +207 -0
  16. package/dist/server/claim-files-tool.js +4 -4
  17. package/dist/server/client-id-utils.js +51 -0
  18. package/dist/server/client-state.js +63 -1
  19. package/dist/server/compact-context-tool.js +10 -9
  20. package/dist/server/compaction-markers.js +34 -1
  21. package/dist/server/context-budget-runner.js +43 -0
  22. package/dist/server/conversation-read-tool.js +7 -8
  23. package/dist/server/conversation-touches.js +19 -0
  24. package/dist/server/dangling-tools.js +18 -5
  25. package/dist/server/delegate-mode.js +1 -0
  26. package/dist/server/delegate-task.js +10 -14
  27. package/dist/server/dispatch-domain-handlers.js +631 -0
  28. package/dist/server/dispatch-plugin-handlers.js +300 -0
  29. package/dist/server/dsh/dsh-agent-service.js +125 -13
  30. package/dist/server/edit-soft-tool.js +10 -12
  31. package/dist/server/eval-tool.js +4 -5
  32. package/dist/server/goal-evidence.js +180 -0
  33. package/dist/server/goal-service.js +49 -9
  34. package/dist/server/hashline-engine.js +199 -32
  35. package/dist/server/index.js +212 -944
  36. package/dist/server/left-panel-running.js +115 -0
  37. package/dist/server/load-tools-tool.js +100 -0
  38. package/dist/server/lsp-tool.js +50 -28
  39. package/dist/server/marker-service.js +53 -12
  40. package/dist/server/markers/builtins/action.js +108 -0
  41. package/dist/server/markers/index.js +4 -1
  42. package/dist/server/markers-list-tool.js +34 -0
  43. package/dist/server/market-service.js +277 -0
  44. package/dist/server/mcp-bridge.js +137 -30
  45. package/dist/server/mcp-hot-reload.js +84 -27
  46. package/dist/server/mcp-manager.js +215 -0
  47. package/dist/server/message-entry-resolver.js +32 -0
  48. package/dist/server/orphan-manager.js +44 -0
  49. package/dist/server/patch-remote-catalog.js +28 -10
  50. package/dist/server/patch-tool.js +9 -8
  51. package/dist/server/persona-chain.js +63 -0
  52. package/dist/server/plan-mode.js +9 -6
  53. package/dist/server/plan-update-tool.js +96 -0
  54. package/dist/server/plugin-api-catalog.js +30 -3
  55. package/dist/server/plugin-conversation-view.js +47 -0
  56. package/dist/server/plugin-facilities.js +13 -3
  57. package/dist/server/plugin-ui-slots.js +39 -0
  58. package/dist/server/plugins.js +183 -63
  59. package/dist/server/present-files-tool.js +10 -11
  60. package/dist/server/preset-fields.js +700 -0
  61. package/dist/server/preset-share.js +1179 -0
  62. package/dist/server/process-utils.js +30 -0
  63. package/dist/server/prompt-assembler.js +80 -0
  64. package/dist/server/prompt-composer.js +23 -9
  65. package/dist/server/protocol-version.js +1 -1
  66. package/dist/server/read-tool.js +8 -10
  67. package/dist/server/resolve-global-sdk.js +80 -18
  68. package/dist/server/schedule-agent-tool.js +147 -146
  69. package/dist/server/scm-commitmsg.js +74 -0
  70. package/dist/server/sdk-origin.js +125 -37
  71. package/dist/server/serialize.js +97 -7
  72. package/dist/server/session-branch.js +367 -0
  73. package/dist/server/session-parser.js +235 -0
  74. package/dist/server/session-search.js +158 -0
  75. package/dist/server/settings-service.js +115 -84
  76. package/dist/server/skill-content-filler.js +33 -0
  77. package/dist/server/skill-tool.js +3 -4
  78. package/dist/server/subagent-handoff.js +18 -0
  79. package/dist/server/subagent-helpers.js +88 -0
  80. package/dist/server/subagents.js +15 -22
  81. package/dist/server/terminals.js +171 -91
  82. package/dist/server/tool-approval.js +3 -1
  83. package/dist/server/tool-guards.js +527 -0
  84. package/dist/server/tool-host-factories.js +217 -0
  85. package/dist/server/tool-manager.js +84 -23
  86. package/dist/server/tool-prompt-overrides.js +158 -0
  87. package/dist/server/tool-prompts.js +42 -0
  88. package/dist/server/tool-signature.js +104 -0
  89. package/dist/server/wait-subscription-scan.js +46 -1
  90. package/dist/server/workspace-snapshot.js +60 -11
  91. package/extensions/webui.ts +90 -24
  92. package/package.json +5 -3
  93. package/plugin-sdk/README.md +25 -4
  94. package/plugin-sdk/import-check.mjs +210 -0
  95. package/plugin-sdk/index.d.ts +21 -1
  96. package/plugin-sdk/index.mjs +6 -0
  97. package/plugins/catalog.json +53 -2
  98. package/themes/aetheris.css +3 -1
  99. package/themes/translucent.css +8 -1
  100. package/themes/transparent.css +9 -1
  101. package/themes/zhupi-dark.css +1 -1
  102. package/themes/zhupi.css +1 -1
  103. package/web/dist/assets/{TerminalPanel-NerrJlTS.js → TerminalPanel-DD7I5_aC.js} +2 -2
  104. package/web/dist/assets/index-CpjA1YdT.css +1 -0
  105. package/web/dist/assets/index-nKn87VuM.js +678 -0
  106. package/web/dist/index.html +2 -2
  107. package/web/dist/sw.js +1 -1
  108. package/web/dist/assets/index-BdMaGCcs.css +0 -1
  109. package/web/dist/assets/index-ByCBN4Kt.js +0 -368
@@ -1,3 +1,4 @@
1
+ import { resolve } from "node:path";
1
2
  /**
2
3
  * process-utils — 跨平台进程工具:监听端口快照、进程树查杀、进程名查询。
3
4
  * 后台任务面板(bgServers)用它们检测/停止 agent 在后台拉起的服务。
@@ -161,3 +162,32 @@ export async function lookupProcessName(pid) {
161
162
  return undefined;
162
163
  }
163
164
  }
165
+ /**
166
+ * 超时杀进程树的决策(纯函数,可单测)。runAsync 的超时兜底用:
167
+ * - win32:shell:true 实际起的是 cmd.exe,p.kill() 只杀 cmd 本身,npm/git 的
168
+ * 孙进程照活 —— 必须走 `taskkill /PID <pid> /T /F` 才能整树带走;
169
+ * - posix:配合 spawn 的 detached:true,向负 pid(整组)发 SIGTERM。
170
+ * 返回决策而不是直接执行,执行留在调用方(便于注入/测试)。
171
+ */
172
+ export function processTreeKillPlan(platform, pid) {
173
+ if (!pid || pid <= 0)
174
+ return { kind: "none" };
175
+ if (platform === "win32")
176
+ return { kind: "taskkill", cmd: "taskkill", args: ["/PID", String(pid), "/T", "/F"] };
177
+ return { kind: "group-signal", signal: "SIGTERM" };
178
+ }
179
+ /**
180
+ * /cwd 目标解析(纯函数,可单测):
181
+ * - 相对路径以**当前会话 cwd** 为基准(/cwd src = 当前项目的 src),不按 server
182
+ * 进程 cwd 解析 —— 两者经常不同,按进程 cwd 切必错位。绝对路径不受基准影响
183
+ * (resolve 遇到绝对段会丢弃前面的基准)。
184
+ * - win32 裸盘符("C:"):resolve 会按该盘当前目录解析,必须显式指到盘根;仅
185
+ * win32 生效——posix 下 "C:" 仍是普通相对路径,避免误伤同名目录。
186
+ */
187
+ export function resolveCwdTarget(raw, sessionCwd, platform = process.platform) {
188
+ const trimmed = String(raw ?? "").trim();
189
+ if (platform === "win32" && /^[A-Za-z]:$/.test(trimmed)) {
190
+ return `${trimmed.toUpperCase()}\\`;
191
+ }
192
+ return resolve(sessionCwd, trimmed);
193
+ }
@@ -0,0 +1,80 @@
1
+ /**
2
+ * prompt-assembler — 提示词渲染判定、上下文 Token 估算与定稿 Usage 探测纯函数。
3
+ *
4
+ * 负责:
5
+ * - estimateNonSystemTokens: 估算非系统消息与正在流式的助手内容的 token 占用
6
+ * - hasSettledAssistantUsage: 判定会话中是否已有定稿的助手 usage(input/output/totalTokens)
7
+ * - shouldRenderMainCompose: 判定当前预设/覆盖/工具开关是否需要渲染自定义系统提示词
8
+ *
9
+ * 从 agent-service.ts 抽出为独立模块。
10
+ */
11
+ import { estimateTokens } from "@earendil-works/pi-coding-agent";
12
+ import { DEFAULT_PROMPT_TEMPLATE, renderPromptTemplate, resolveSectionTexts, } from "./prompt-composer.js";
13
+ /**
14
+ * 会话是否已获得定稿的助手模型 usage(决定是使用真实的 usage 还是估算值)。
15
+ */
16
+ export function hasSettledAssistantUsage(messages) {
17
+ if (!Array.isArray(messages))
18
+ return false;
19
+ try {
20
+ for (let i = messages.length - 1; i >= 0; i--) {
21
+ const m = messages[i];
22
+ if (m && m.role === "assistant" && m.usage) {
23
+ const u = m.usage;
24
+ if ((u.input ?? 0) > 0 || (u.totalTokens ?? 0) > 0 || (u.output ?? 0) > 0) {
25
+ return true;
26
+ }
27
+ }
28
+ }
29
+ }
30
+ catch {
31
+ // fallback
32
+ }
33
+ return false;
34
+ }
35
+ /**
36
+ * 估算会话中所有非系统消息(用户消息、工具调用/结果、自定义消息以及正在流式的助手内容)的 Token 开销。
37
+ * 用于在尚未获得定稿 LLM usage 时,配合精准的 baseTokens 给出平滑、真实的上下文预估,
38
+ * 杜绝 SDK 内部全量遍历粗估系统提示词与 baseTokens 双重叠加导致首轮突增翻倍。
39
+ */
40
+ export function estimateNonSystemTokens(messages, streamingMessage) {
41
+ let total = 0;
42
+ if (!Array.isArray(messages))
43
+ return total;
44
+ try {
45
+ for (const m of messages) {
46
+ if (m?.role !== "system") {
47
+ total += estimateTokens(m);
48
+ }
49
+ }
50
+ if (streamingMessage) {
51
+ total += estimateTokens(streamingMessage);
52
+ }
53
+ }
54
+ catch {
55
+ // best-effort
56
+ }
57
+ return total;
58
+ }
59
+ /**
60
+ * 判定当前设置与预设是否属于定制模式,需要渲染独立系统提示词而非依赖 SDK 默认装配。
61
+ */
62
+ export function shouldRenderMainCompose(opts) {
63
+ const tpl = (opts.promptTemplate ?? "").trim();
64
+ const ovs = opts.promptOverrides ?? {};
65
+ const hasOverride = Object.values(ovs).some((v) => typeof v === "string" && v.trim());
66
+ const preset = opts.preset ?? "standard";
67
+ return !!tpl || hasOverride || preset !== "standard" || opts.disabledToolCount > 0 || opts.isLazy === true;
68
+ }
69
+ /**
70
+ * 渲染组合模板。非定制状态返回 undefined 让 SDK 内部装配。
71
+ */
72
+ export function renderMainCompose(inputs, opts) {
73
+ if (!shouldRenderMainCompose(opts))
74
+ return undefined;
75
+ const texts = resolveSectionTexts(inputs);
76
+ const tpl = (opts.promptTemplate ?? "").trim();
77
+ const ovs = opts.promptOverrides ?? {};
78
+ const hasOverride = Object.values(ovs).some((v) => typeof v === "string" && v.trim());
79
+ return renderPromptTemplate(tpl || DEFAULT_PROMPT_TEMPLATE, texts, hasOverride ? ovs : undefined);
80
+ }
@@ -184,14 +184,28 @@ function buildContextText(files, lang = "en") {
184
184
  "</project_context>",
185
185
  ].join("\n");
186
186
  }
187
- /** 工具列表块:工具列表 + "In addition…" 句。 */
188
- function buildToolsText(inputs) {
189
- const visible = inputs.selectedTools.filter((n) => !!inputs.toolSnippets[n]);
190
- const toolsList = visible.length > 0 ? visible.map((n) => `- ${n}: ${inputs.toolSnippets[n]}`).join("\n") : "(none)";
191
- return [
192
- `Available tools:\n${toolsList}`,
193
- "In addition to the tools above, you may have access to other custom tools depending on the project.",
194
- ].join("\n\n");
187
+ /** 工具列表块:工具列表(延迟加载时列完整目录)+ 恒定提示 + "In addition…" 句。
188
+ *
189
+ * 延迟加载下这里**只依赖目录本身**(不依赖「哪些已加载」):系统提示词是供应商
190
+ * 前缀缓存的第一段,只要它不变,加载工具造成的只是 tools 数组的追加(前缀不变的
191
+ * 增量),缓存才不会被我们自已打坏。 */
192
+ function buildToolsText(inputs, lang) {
193
+ const listed = inputs.lazy ? (inputs.catalogTools ?? inputs.selectedTools) : inputs.selectedTools;
194
+ const visible = listed.filter((n) => !!inputs.toolSnippets[n]);
195
+ const toolsList = visible.length > 0
196
+ ? visible
197
+ .map((n) => {
198
+ const sig = inputs.toolSignatures?.[n] ? ` ${inputs.toolSignatures[n]}` : "";
199
+ return `- ${n}:${sig} ${inputs.toolSnippets[n]}`;
200
+ })
201
+ .join("\n")
202
+ : "(none)";
203
+ const parts = [`Available tools:\n${toolsList}`];
204
+ if (inputs.lazy) {
205
+ parts.push(pick(lang, "上表里有一部分工具的**参数 schema 尚未附加**;调用前先用 `load_tools` 把它们的名字传进来,即可拿到完整参数(本对话后续轮次都能继续用)。尚不在你工具列表里的,一律先 load_tools。", "Some tools above do NOT have their parameter schema attached yet. Before calling one, pass its name to `load_tools` to attach the full schema for the rest of this conversation. Anything not in your function list must be loaded first.", "prompt.tools.lazy"));
206
+ }
207
+ parts.push("In addition to the tools above, you may have access to other custom tools depending on the project.");
208
+ return parts.join("\n\n");
195
209
  }
196
210
  /** 工具 schema 只读文本(设置面板「查看当前完整提示词」里展示发给模型的完整
197
211
  * 工具定义:name + description + parameters JSON Schema)。 */
@@ -220,7 +234,7 @@ export function resolveSectionTexts(inputs) {
220
234
  soul = getServerBlock(lang, "prompt.soul", BUILTIN_SOUL_ZH.split("\n"), BUILTIN_SOUL.split("\n")).join("\n");
221
235
  return {
222
236
  soul,
223
- tools: buildToolsText(inputs),
237
+ tools: buildToolsText(inputs, lang),
224
238
  guidelines: buildGuidelinesText(inputs),
225
239
  pi_docs: buildPiDocsText(inputs.piReadme, inputs.piDocs, inputs.piExamples, lang),
226
240
  append: inputs.appendFiles.join("\n\n"),
@@ -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 = 22;
11
+ export const PROTOCOL_VERSION = 26;
@@ -29,7 +29,7 @@
29
29
  import { stat } from "node:fs/promises";
30
30
  import { homedir } from "node:os";
31
31
  import { isAbsolute, join, resolve as nodeResolve } from "node:path";
32
- import { DEFAULT_MAX_BYTES, DEFAULT_MAX_LINES, createLsToolDefinition, createReadToolDefinition, defineTool, } from "@earendil-works/pi-coding-agent";
32
+ import { createLsToolDefinition, createReadToolDefinition, defineTool, } from "@earendil-works/pi-coding-agent";
33
33
  import { Type } from "typebox";
34
34
  import { pick } from "./i18n.js";
35
35
  const UNICODE_SPACES = /[\u00A0\u2000-\u200A\u202F\u205F\u3000]/g;
@@ -87,8 +87,10 @@ export function prepareReadArguments(raw) {
87
87
  return args;
88
88
  }
89
89
  /** Directory support note appended to any read definition (definitions are English-only). */
90
- const DIR_DESCRIPTION_NOTE = "Also accepts a directory path: its entries are then listed instead of file contents (one entry per line, directories suffixed with '/'); in that case `limit` caps the number of entries and `offset` is ignored.";
91
- const DIR_GUIDELINE = "Use read on a directory to list its entries — no need to shell out to `ls`";
90
+ const DIR_DESCRIPTION_NOTE = "Also accepts a directory path: its entries are listed instead of contents (one per line, directories suffixed with '/'); `limit` then caps entries and `offset` is ignored.";
91
+ /** 内置基底专用:`file_path` 别名只在没有扩展同名工具时由本模块提供。 */
92
+ const FILE_PATH_ALIAS_NOTE = "Also accepts `file_path` as an alias of `path`.";
93
+ const DIR_GUIDELINE = "No need to shell out to `ls`: read lists a directory's entries";
92
94
  /**
93
95
  * 两条路(内置基底 / 扩展基底)共用的执行体:先判「路径是不是目录」—— 是就复用 SDK 的
94
96
  * ls 列条目(排序/`/` 后缀/截断提示口径一致),否则把请求转发给基底实现。
@@ -147,13 +149,9 @@ export function makeReadDirTool(fallbackCwd, options = {}) {
147
149
  const getLang = options.getLang ?? (() => "en");
148
150
  return defineTool({
149
151
  ...base,
150
- description: `${base.description} Also accepts \`file_path\` as an alias of \`path\`. ` +
151
- "If the path is a directory, its entries are listed instead (one per line, directories suffixed with '/'); `limit` caps the entries and `offset` is ignored.",
152
- promptSnippet: "Read file contents (a directory path lists its entries)",
153
- promptGuidelines: [
154
- ...(base.promptGuidelines ?? []),
155
- "Use read on a directory to list its entries — no need to shell out to `ls`",
156
- ],
152
+ description: `${base.description} ${FILE_PATH_ALIAS_NOTE} ${DIR_DESCRIPTION_NOTE}`,
153
+ promptSnippet: "file contents by path (a directory lists its entries)",
154
+ promptGuidelines: [...(base.promptGuidelines ?? []), DIR_GUIDELINE],
157
155
  parameters: readDirSchema,
158
156
  prepareArguments: prepareReadArguments,
159
157
  async execute(toolCallId, params, signal, onUpdate, ctx) {
@@ -25,33 +25,79 @@
25
25
  * 让服务起不来。它必须在任何 SDK 静态 import **之前**被加载(`--import <本文件>`,
26
26
  * 或前台启动时在 import 服务入口之前先 import 它)。
27
27
  */
28
+ import { existsSync, readFileSync } from "node:fs";
28
29
  import { registerHooks } from "node:module";
29
- import { dirname } from "node:path";
30
- import { pathToFileURL } from "node:url";
30
+ import { dirname, join } from "node:path";
31
+ import { fileURLToPath, pathToFileURL } from "node:url";
31
32
  import { compareVersions, sdkCopies } from "./sdk-origin.js";
32
33
  const PKG = "@earendil-works/pi-coding-agent";
33
34
  /** 显式强制自带副本的取值(旧默认;可复现、CI 覆盖的那份)。 */
34
35
  const BUNDLED_MODES = new Set(["bundled", "0", "off", "false", "no"]);
36
+ /**
37
+ * 找出传入副本列表中的随包自带副本(如果有的话)。
38
+ * 自带副本必须位于 pi-web-ui 自身包根下的 node_modules/@earendil-works/pi-coding-agent/package.json。
39
+ */
40
+ export function findBundledCopy(copies, fromFile = import.meta.url) {
41
+ const filePath = fromFile.startsWith("file:") ? fileURLToPath(fromFile) : fromFile;
42
+ let cur = dirname(filePath);
43
+ let pkgRoot = null;
44
+ for (let i = 0; i < 5; i++) {
45
+ const pj = join(cur, "package.json");
46
+ if (existsSync(pj)) {
47
+ try {
48
+ const info = JSON.parse(readFileSync(pj, "utf8"));
49
+ if (info?.name === "pi-web-ui") {
50
+ pkgRoot = cur;
51
+ break;
52
+ }
53
+ }
54
+ catch { }
55
+ }
56
+ const p = dirname(cur);
57
+ if (p === cur)
58
+ break;
59
+ cur = p;
60
+ }
61
+ if (!pkgRoot) {
62
+ return copies[0] ?? null;
63
+ }
64
+ const expectedBundledPath = join(pkgRoot, "node_modules", PKG, "package.json").replace(/\\/g, "/");
65
+ return copies.find((c) => c.path.replace(/\\/g, "/") === expectedBundledPath) ?? null;
66
+ }
35
67
  /**
36
68
  * 该用哪一份对外解析(纯函数,便于单测)。
37
- * `copies` 按 Node 的解析顺序([0] = 自带的、无钩子时实际会被用到的),
69
+ * `copies` 按 Node 的解析顺序,`bundledCopy` 为识别到的随包自带副本(null = 无自带副本)。
38
70
  * 返回 null = 保持默认(用自带副本兜底)。
39
71
  */
40
- export function pickGlobalSdk(copies, mode) {
72
+ export function pickGlobalSdk(copies, mode, bundledCopy = copies[0] ?? null) {
41
73
  if (BUNDLED_MODES.has((mode ?? "global").trim().toLowerCase()))
42
74
  return null;
43
- const bundled = copies[0];
44
75
  // 显式透传的宿主 SDK 路径(如 extensions/webui.ts 宿主传递,issue #482):若不降级优先选用
45
76
  if (process.env.PI_WEB_SDK_DIR) {
46
77
  const hostCopy = copies.find((c) => dirname(c.path) === process.env.PI_WEB_SDK_DIR);
47
- if (hostCopy && (!bundled || compareVersions(hostCopy.version, bundled.version) >= 0)) {
78
+ if (hostCopy && (!bundledCopy || compareVersions(hostCopy.version, bundledCopy.version) >= 0)) {
48
79
  return hostCopy;
49
80
  }
50
81
  }
51
- // 只考虑祖先链上**严格更新**的副本,多份取版本最高的(旧的/同版本的都不折腾)。
82
+ // 如果没有随包自带副本(例如 pnpm 隔离安装未包含可选依赖,或轻量部署,issue #562):
83
+ // 机器上只要发现可用副本就必须选用,否则 Node 无法导入 SDK 会报错崩溃。
84
+ if (!bundledCopy) {
85
+ if (copies.length === 0)
86
+ return null;
87
+ let best = copies[0];
88
+ for (const copy of copies.slice(1)) {
89
+ if (compareVersions(copy.version, best.version) > 0) {
90
+ best = copy;
91
+ }
92
+ }
93
+ return best;
94
+ }
95
+ // 存在自带副本:只考虑比自带副本**严格更新**的外部副本,多份取版本最高的(旧的/同版本的都不折腾)。
52
96
  let best = null;
53
- for (const copy of copies.slice(1)) {
54
- if ((!bundled || compareVersions(copy.version, bundled.version) > 0) &&
97
+ for (const copy of copies) {
98
+ if (copy.path === bundledCopy.path)
99
+ continue;
100
+ if (compareVersions(copy.version, bundledCopy.version) > 0 &&
55
101
  (!best || compareVersions(copy.version, best.version) > 0)) {
56
102
  best = copy;
57
103
  }
@@ -62,21 +108,37 @@ export function pickGlobalSdk(copies, mode) {
62
108
  export function registerGlobalSdkPreference(mode = process.env.PI_WEB_SDK, fromFile = import.meta.url) {
63
109
  try {
64
110
  const copies = sdkCopies(fromFile);
65
- const chosen = pickGlobalSdk(copies, mode);
111
+ const bundled = findBundledCopy(copies, fromFile);
112
+ const chosen = pickGlobalSdk(copies, mode, bundled);
66
113
  if (!chosen)
67
114
  return null;
68
115
  registerHooks({
69
116
  resolve(specifier, context, nextResolve) {
70
- if (specifier !== PKG)
71
- return nextResolve(specifier, context);
72
- // 以目标副本的 package.json 为父重新解析:Node 从那里往上找,
73
- // 第一个命中的就是它自己 —— 等价于「从那个包内部 import 自己」。
74
- try {
75
- return nextResolve(specifier, { ...context, parentURL: pathToFileURL(chosen.path).href });
117
+ if (specifier === PKG) {
118
+ // 以目标副本的 package.json 为父重新解析:Node 从那里往上找,
119
+ // 第一个命中的就是它自己 —— 等价于「从那个包内部 import 自己」。
120
+ try {
121
+ return nextResolve(specifier, { ...context, parentURL: pathToFileURL(chosen.path).href });
122
+ }
123
+ catch {
124
+ return nextResolve(specifier, context);
125
+ }
76
126
  }
77
- catch {
78
- return nextResolve(specifier, context);
127
+ if (specifier === "typebox") {
128
+ try {
129
+ return nextResolve(specifier, context);
130
+ }
131
+ catch {
132
+ // 本地缺少 typebox(如 pnpm 隔离安装)时,从已选中的 pi SDK 目录解析(issue #562)
133
+ try {
134
+ return nextResolve(specifier, { ...context, parentURL: pathToFileURL(chosen.path).href });
135
+ }
136
+ catch {
137
+ return nextResolve(specifier, context);
138
+ }
139
+ }
79
140
  }
141
+ return nextResolve(specifier, context);
80
142
  },
81
143
  });
82
144
  return chosen;