pi-web-ui 0.113.1 → 0.114.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 (222) hide show
  1. package/CHANGELOG.md +43 -1
  2. package/README.md +117 -246
  3. package/README.zh-CN.md +145 -217
  4. package/README.zh-TW.md +145 -217
  5. package/bin/device-hub-cli.mjs +577 -0
  6. package/bin/pi-device-hub.mjs +9 -0
  7. package/bin/pi-web-ui.mjs +47 -15
  8. package/dist/server/agent-service-daemon.js +102 -24
  9. package/dist/server/agent-service.js +2289 -1120
  10. package/dist/server/approval-rules.js +29 -3
  11. package/dist/server/ask-user-question-tool.js +8 -8
  12. package/dist/server/atomic-file.js +35 -14
  13. package/dist/server/attachment-store.js +71 -35
  14. package/dist/server/attachments.js +18 -8
  15. package/dist/server/canvas/canvas-agent-tool.js +65 -15
  16. package/dist/server/canvas/canvas-dispatch.js +61 -20
  17. package/dist/server/canvas/canvas-orchestrator.js +516 -199
  18. package/dist/server/canvas/canvas-scheduler.js +99 -0
  19. package/dist/server/canvas/canvas-store.js +107 -54
  20. package/dist/server/canvas/canvas-validation.js +131 -0
  21. package/dist/server/canvas/canvas-workspace.js +284 -0
  22. package/dist/server/client-id-utils.js +8 -0
  23. package/dist/server/client-state.js +104 -22
  24. package/dist/server/communicate-tool.js +76 -0
  25. package/dist/server/compact-context-tool.js +2 -3
  26. package/dist/server/compaction-instructions.js +75 -0
  27. package/dist/server/compaction-markers.js +7 -2
  28. package/dist/server/compaction-state.js +84 -0
  29. package/dist/server/composer-drafts.js +43 -40
  30. package/dist/server/context-evidence.js +346 -0
  31. package/dist/server/conversation-communication.js +368 -0
  32. package/dist/server/conversation-read-tool.js +3 -4
  33. package/dist/server/conversation-tool-names.js +14 -0
  34. package/dist/server/conversation-tool.js +57 -0
  35. package/dist/server/delegate-mode.js +17 -4
  36. package/dist/server/device-center/bot-catalog.js +88 -0
  37. package/dist/server/device-center/bot-execution.js +106 -0
  38. package/dist/server/device-center/bots.js +170 -0
  39. package/dist/server/device-center/collab-service.js +153 -0
  40. package/dist/server/device-center/collab-tool.js +184 -0
  41. package/dist/server/device-center/connector.js +332 -0
  42. package/dist/server/device-center/desktop-linux.js +372 -0
  43. package/dist/server/device-center/desktop-native.js +413 -0
  44. package/dist/server/device-center/desktop-page.js +232 -0
  45. package/dist/server/device-center/desktop-setup.js +30 -0
  46. package/dist/server/device-center/desktop.js +387 -0
  47. package/dist/server/device-center/device-portals.js +241 -0
  48. package/dist/server/device-center/execution-failure.js +70 -0
  49. package/dist/server/device-center/executor.js +371 -0
  50. package/dist/server/device-center/file-sync.js +493 -0
  51. package/dist/server/device-center/hub-auth.js +69 -0
  52. package/dist/server/device-center/hub-page.js +68 -0
  53. package/dist/server/device-center/hub-setup.js +214 -0
  54. package/dist/server/device-center/hub.js +829 -0
  55. package/dist/server/device-center/local-page.js +17 -0
  56. package/dist/server/device-center/local.js +476 -0
  57. package/dist/server/device-center/main.js +118 -0
  58. package/dist/server/device-center/native-desktop/macos.js +192 -0
  59. package/dist/server/device-center/native-desktop/windows.js +118 -0
  60. package/dist/server/device-center/path-portal.js +120 -0
  61. package/dist/server/device-center/relay.js +959 -0
  62. package/dist/server/device-center/runtime.js +253 -0
  63. package/dist/server/device-center/smoke-preview.js +3 -0
  64. package/dist/server/device-center/store.js +122 -0
  65. package/dist/server/device-center/task-error.js +14 -0
  66. package/dist/server/device-center/transfers.js +293 -0
  67. package/dist/server/device-center/workspaces.js +45 -0
  68. package/dist/server/dispatch-domain-handlers.js +83 -27
  69. package/dist/server/dsh/dsh-agent-service.js +277 -158
  70. package/dist/server/dsh/dsh-client.js +12 -2
  71. package/dist/server/durable/durable-agent-service.js +1 -1
  72. package/dist/server/evidence-tool-hooks.js +49 -0
  73. package/dist/server/file-save.js +95 -0
  74. package/dist/server/file-transfer-routes.js +97 -47
  75. package/dist/server/files-service.js +38 -26
  76. package/dist/server/goal-review-gate.js +16 -6
  77. package/dist/server/goal-service.js +54 -30
  78. package/dist/server/http-proxy.js +120 -0
  79. package/dist/server/index.js +229 -223
  80. package/dist/server/load-timing.js +31 -0
  81. package/dist/server/mcp-bridge.js +219 -94
  82. package/dist/server/mcp-hot-reload.js +31 -5
  83. package/dist/server/mcp-manager.js +192 -93
  84. package/dist/server/mcp-oauth-login.js +169 -0
  85. package/dist/server/mcp-oauth-ui.js +137 -0
  86. package/dist/server/mcp-oauth.js +395 -0
  87. package/dist/server/mcp-project-runtime.js +207 -0
  88. package/dist/server/message-window.js +74 -21
  89. package/dist/server/net-guard.js +122 -20
  90. package/dist/server/payload-limits.js +17 -0
  91. package/dist/server/pending-question.js +82 -0
  92. package/dist/server/plan-mode.js +338 -137
  93. package/dist/server/plugin-assets.js +8 -4
  94. package/dist/server/plugin-chat-attachments.js +48 -0
  95. package/dist/server/plugin-facilities.js +67 -220
  96. package/dist/server/plugin-installer.js +2 -1
  97. package/dist/server/plugin-removal.js +49 -0
  98. package/dist/server/plugin-secrets.js +179 -0
  99. package/dist/server/plugins.js +262 -92
  100. package/dist/server/preset-fields.js +49 -0
  101. package/dist/server/preset-plugin-dependencies.js +57 -0
  102. package/dist/server/preset-share.js +112 -72
  103. package/dist/server/protocol-version.js +1 -1
  104. package/dist/server/queue-modes.js +10 -0
  105. package/dist/server/remote-ssh-service.js +160 -67
  106. package/dist/server/request-context.js +164 -0
  107. package/dist/server/sandbox-data.js +655 -0
  108. package/dist/server/sandbox-engines.js +7 -0
  109. package/dist/server/sandbox-runtime-gate.js +41 -0
  110. package/dist/server/sandbox-service.js +139 -698
  111. package/dist/server/sandbox-tool.js +21 -4
  112. package/dist/server/scheduler-tasks.js +63 -48
  113. package/dist/server/serialize.js +48 -19
  114. package/dist/server/session-branch.js +25 -3
  115. package/dist/server/session-parser.js +28 -18
  116. package/dist/server/session-scan.js +130 -0
  117. package/dist/server/session-search.js +51 -37
  118. package/dist/server/settings-service.js +267 -111
  119. package/dist/server/setup-config.js +58 -0
  120. package/dist/server/skill-maintenance.js +112 -24
  121. package/dist/server/subagent-helpers.js +4 -1
  122. package/dist/server/subagent-notify.js +4 -1
  123. package/dist/server/subagent-security-gate.js +52 -11
  124. package/dist/server/subagent-task.js +27 -0
  125. package/dist/server/subagents.js +217 -46
  126. package/dist/server/tabs.js +9 -1
  127. package/dist/server/terminals.js +467 -363
  128. package/dist/server/tool-guards.js +42 -6
  129. package/dist/server/tool-manager.js +47 -20
  130. package/dist/server/tool-overrides.js +13 -11
  131. package/dist/server/tool-prompt-overrides.js +32 -2
  132. package/dist/server/tool-prompts.js +1 -1
  133. package/dist/server/turn-timing.js +11 -0
  134. package/dist/server/universal/session-store.js +48 -6
  135. package/dist/server/universal/universal-service.js +1 -1
  136. package/dist/server/universal/universal-session.js +334 -231
  137. package/dist/server/uploads.js +64 -5
  138. package/dist/server/worktree-service.js +32 -24
  139. package/dist/server/ws-pending-queue.js +73 -27
  140. package/docs/device-center.md +142 -0
  141. package/docs/device-desktop.md +48 -0
  142. package/package.json +4 -1
  143. package/plugin-sdk/index.d.ts +28 -6
  144. package/plugin-sdk/ssh-host-trust.mjs +225 -0
  145. package/plugins/catalog.json +1 -1
  146. package/themes/aetheris.css +1 -107
  147. package/themes/ayu-light.css +5 -29
  148. package/themes/bases/flow-base.css +0 -6
  149. package/themes/bases/statusbar-flat-base.css +112 -0
  150. package/themes/bases/zhujian-base.css +1 -0
  151. package/themes/bases/zhupi-base.css +1 -0
  152. package/themes/catppuccin-latte.css +4 -27
  153. package/themes/catppuccin.css +4 -35
  154. package/themes/claude-code-dark.css +1 -0
  155. package/themes/codex.css +4 -28
  156. package/themes/cyberpunk.css +3 -61
  157. package/themes/dark-teal.css +0 -1
  158. package/themes/dazzle.css +3 -73
  159. package/themes/everforest-light.css +4 -27
  160. package/themes/geist.css +4 -28
  161. package/themes/gruvbox-light.css +4 -27
  162. package/themes/kanagawa-lotus.css +4 -27
  163. package/themes/md-preview.css +3 -213
  164. package/themes/mist.css +4 -24
  165. package/themes/nord.css +4 -35
  166. package/themes/one-dark.css +4 -37
  167. package/themes/paper.css +4 -16
  168. package/themes/rose-pine-dawn.css +4 -28
  169. package/themes/sakura.css +4 -24
  170. package/themes/solarized-light.css +4 -27
  171. package/themes/tokyo-night.css +3 -34
  172. package/themes/white.css +4 -26
  173. package/themes/zhujian-dark.css +1 -0
  174. package/themes/zhupi-dark.css +1 -0
  175. package/web/dist/.build-assets.json +1 -0
  176. package/web/dist/assets/BgTasksModal-CS7UNzcw.js +1 -0
  177. package/web/dist/assets/BgTasksModal-Dk-tp78W.js +1 -0
  178. package/web/dist/assets/CanvasStudio-BEJZE5BI.js +1579 -0
  179. package/web/dist/assets/CanvasStudio-CgUNHqos.js +1579 -0
  180. package/web/dist/assets/DeviceCenter-C8_CbxQ3.js +3 -0
  181. package/web/dist/assets/DeviceCenter-CVblvhNX.js +3 -0
  182. package/web/dist/assets/Dropdown-Oug0DDte.js +1 -0
  183. package/web/dist/assets/FilePreview-CvdX6nr6.js +2 -0
  184. package/web/dist/assets/FilePreview-kCZhfBAF.js +2 -0
  185. package/web/dist/assets/GlobalSearchModal-BLH63yYP.js +1 -0
  186. package/web/dist/assets/GlobalSearchModal-DAkvlvp6.js +1 -0
  187. package/web/dist/assets/GroupConversation-Cgo4xbpa.js +1 -0
  188. package/web/dist/assets/GroupConversation-DZ71jEi8.js +1 -0
  189. package/web/dist/assets/ModelConfigModal-BpWdFdQC.js +2 -0
  190. package/web/dist/assets/PiSetupModal-mkWPSxMj.js +1 -0
  191. package/web/dist/assets/ProviderOAuthControls-VK_jqXYS.js +1 -0
  192. package/web/dist/assets/SCMPanel-BP1TUT-V.js +2 -0
  193. package/web/dist/assets/SCMPanel-BscPm9kf.js +2 -0
  194. package/web/dist/assets/SettingsModal-C8UgMjVM.js +20 -0
  195. package/web/dist/assets/SettingsModal-VynB7jNb.js +20 -0
  196. package/web/dist/assets/TerminalPanel-D6cPRFei.js +3 -0
  197. package/web/dist/assets/TerminalPanel-DL20uKgU.js +3 -0
  198. package/web/dist/assets/TuiOverlayModal-Dst3BmT8.js +1 -0
  199. package/web/dist/assets/TuiOverlayModal-NCkpNtTp.js +1 -0
  200. package/web/dist/assets/UniversalPathPicker-CfQFc_c3.js +1 -0
  201. package/web/dist/assets/UniversalPathPicker-DwxwNcFw.js +1 -0
  202. package/web/dist/assets/app-globals-CnO6KlEM.js +325 -0
  203. package/web/dist/assets/desktop-DPyTy4M3.js +1 -0
  204. package/web/dist/assets/es-DWMIuDU2.js +2 -0
  205. package/web/dist/assets/index-CPgAH5q7.js +83 -0
  206. package/web/dist/assets/index-Cje8iugL.js +83 -0
  207. package/web/dist/assets/index-DnKeiDbj.css +1 -0
  208. package/web/dist/assets/markdown-BggnuQ4H.js +40 -0
  209. package/web/dist/assets/markdown-C8LL_u4z.css +1 -0
  210. package/web/dist/assets/math-1uRJCsA9.js +257 -0
  211. package/web/dist/assets/math-BorAY7qD.css +1 -0
  212. package/web/dist/assets/plugin-host-CqA4yW6w.js +1 -0
  213. package/web/dist/assets/shortcut-stack-hhrDyRBn.js +1 -0
  214. package/web/dist/assets/theme-BqdAjEjF.js +1 -0
  215. package/web/dist/assets/uuid-BzDIGCwP.js +1 -0
  216. package/web/dist/index.html +15 -9
  217. package/web/dist/sw.js +1 -1
  218. package/web/dist/assets/TerminalPanel-Ci55JtJL.js +0 -3
  219. package/web/dist/assets/index-ByiO9f-N.js +0 -2008
  220. package/web/dist/assets/index-ycGLh4a4.css +0 -1
  221. package/web/dist/assets/markdown-BJZlfvdP.js +0 -296
  222. package/web/dist/assets/markdown-DSNUiDzp.css +0 -1
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * Settings service — 从 agent-service.ts 抽出(系统提示词 / 技能插件开关 /
3
- * 目标审查提示词 / 预设 / 视觉桥偏好)。设置持久化在 client-state.json 按客户端隔离。
3
+ * 目标审查提示词 / 预设 / 视觉桥偏好)。设置持久化在 client-state.json 全局共享。
4
4
  *
5
5
  * 经 SettingsHost 回调与 ClientSession 解耦:本模块只管「设置状态 + 面板推送 +
6
6
  * 预设存取 + 何时需要 reload」,真正动 runtime 的 session.reload() 走宿主回调
@@ -30,6 +30,11 @@ export class SettingsService {
30
30
  knownExtensions = new Map();
31
31
  /** 流式中改了需要 reload 的设置 → agent_end 后延迟应用(防拆毁运行中 run)。 */
32
32
  pendingReload = false;
33
+ unsubscribeSettings;
34
+ sharedVersion = 0;
35
+ sharedQueue = Promise.resolve();
36
+ committingSettings = false;
37
+ disposed = false;
33
38
  constructor(host,
34
39
  /** 全局子代理模板库(所有客户端共享;模板改动无需 reload runtime)。 */
35
40
  templates,
@@ -41,8 +46,94 @@ export class SettingsService {
41
46
  this.settings = host.stateStore.getSettings(host.clientId);
42
47
  this.presets = host.stateStore.getPresets(host.clientId);
43
48
  }
49
+ /** Immutable snapshot: repeated hot-path reads share the same detached object. */
44
50
  get current() {
45
- return this.settings;
51
+ return (this.host.stateStore.getSettingsSnapshot?.(this.host.clientId) ??
52
+ this.host.stateStore.getSettings(this.host.clientId));
53
+ }
54
+ /** Call once after the host has assigned settingsSvc and finished initialization. */
55
+ subscribeSharedSettings() {
56
+ if (this.unsubscribeSettings || this.disposed)
57
+ return;
58
+ this.sharedVersion = this.host.stateStore.getSettingsVersion();
59
+ this.unsubscribeSettings = this.host.stateStore.subscribeSettings((change) => {
60
+ if (this.committingSettings) {
61
+ this.sharedVersion = Math.max(this.sharedVersion, change.version);
62
+ return;
63
+ }
64
+ return this.receiveSharedSettings(change);
65
+ });
66
+ }
67
+ dispose() {
68
+ this.disposed = true;
69
+ this.unsubscribeSettings?.();
70
+ this.unsubscribeSettings = undefined;
71
+ this.pendingReload = false;
72
+ }
73
+ /** Apply a durable commit without saving it again. Serialize reloads; ignore stale deliveries. */
74
+ receiveSharedSettings(change) {
75
+ if (this.disposed || this.host.isDisposed() || change.version <= this.sharedVersion)
76
+ return this.sharedQueue;
77
+ this.sharedVersion = change.version;
78
+ this.sharedQueue = this.sharedQueue
79
+ .then(async () => {
80
+ if (this.disposed || this.host.isDisposed())
81
+ return;
82
+ await this.applySharedSettings(change.before, change.settings);
83
+ })
84
+ .catch((error) => {
85
+ if (this.disposed || this.host.isDisposed())
86
+ return;
87
+ console.error("[settings] shared settings apply failed:", error);
88
+ this.host.emit({
89
+ type: "notice",
90
+ level: "error",
91
+ text: "共享设置已保存,但当前会话应用失败,请重试或重新加载会话。",
92
+ textEn: "Shared settings were saved, but could not be applied to this session. Retry or reload the session.",
93
+ });
94
+ });
95
+ return this.sharedQueue;
96
+ }
97
+ async applySharedSettings(before, after) {
98
+ const changed = (...keys) => keys.some((key) => JSON.stringify(before[key]) !== JSON.stringify(after[key]));
99
+ if (changed("retryMaxAttempts"))
100
+ this.host.applyRetryOverrides();
101
+ if (changed("steeringMode", "followUpMode"))
102
+ this.host.applyQueueModes?.();
103
+ if (changed("softCapTokens", "softCapByModel"))
104
+ this.host.applyCompactionOverrides();
105
+ if (changed("codemodeMode", "codemodeInlineBudget"))
106
+ this.host.applyCodemodeOverrides?.();
107
+ if (changed("disabledAgentTools", "disabledPluginTools", "disabledPlugins", "toolPromptOverrides", "toolLazyLoading", "terminalToolsEnabled", "editSoftEnabled", "questionnaireEnabled"))
108
+ this.host.applyToolGating();
109
+ if (before.goalModeEnabled !== false && after.goalModeEnabled === false)
110
+ this.host.onGoalModeDisabled?.();
111
+ if (changed("showForkedParentSessions"))
112
+ this.host.onSessionsFilterChanged?.();
113
+ this.push(false);
114
+ this.host.flushSnapshot();
115
+ if (changed("promptMode", "customSystemPrompt", "promptTemplate", "promptOverrides", "promptDisabled", "disabledSkills", "disabledExtensions"))
116
+ await this.applyRuntime();
117
+ }
118
+ commitSettings(patch) {
119
+ this.committingSettings = true;
120
+ try {
121
+ this.host.stateStore.saveSettings(this.host.clientId, patch);
122
+ this.settings = this.host.stateStore.getSettings(this.host.clientId);
123
+ }
124
+ catch (error) {
125
+ this.settings = this.host.stateStore.getSettings(this.host.clientId);
126
+ this.host.emit({
127
+ type: "notice",
128
+ level: "error",
129
+ text: "设置保存失败,修改未生效。请检查磁盘空间和文件权限后重试。",
130
+ textEn: "Settings were not saved or applied. Check disk space and file permissions, then retry.",
131
+ });
132
+ throw error;
133
+ }
134
+ finally {
135
+ this.committingSettings = false;
136
+ }
46
137
  }
47
138
  /** Effective dev-no-cache: explicit setting wins, else source-tree default
48
139
  * (ON from source, OFF for installs) — same rule as the index.html route.
@@ -128,7 +219,11 @@ export class SettingsService {
128
219
  }
129
220
  return false;
130
221
  }
131
- push() {
222
+ push(maintain = true) {
223
+ if (this.disposed || this.host.isDisposed())
224
+ return;
225
+ this.settings = this.host.stateStore.getSettings(this.host.clientId);
226
+ this.presets = this.host.stateStore.getPresets(this.host.clientId);
132
227
  const disabledSkills = new Set(this.settings.disabledSkills);
133
228
  const reviewDisabledSkills = new Set(this.settings.reviewDisabledSkills);
134
229
  const disabledExts = new Set(this.settings.disabledExtensions);
@@ -141,10 +236,11 @@ export class SettingsService {
141
236
  loadedSkillNames = new Set(loadedSkills.map((s) => s.name));
142
237
  loadedExtNames = new Set(loadedExts.map((e) => extensionKey(e)));
143
238
  // 实时全源磁盘扫描:动态发现外部新安装/更新的各类 Agent 全局与本地技能
144
- const diskSkills = discoverDiskSkills({
239
+ const discoveredSkills = discoverDiskSkills({
145
240
  cwd: this.host.cwd(),
146
241
  agentDir: this.host.agentDir(),
147
242
  });
243
+ const diskSkills = this.host.filterDiskSkills?.(discoveredSkills) ?? discoveredSkills;
148
244
  // Prune entries that no longer exist on disk AND aren't disabled
149
245
  // (e.g. a skill/extension file was deleted). Disabled entries are
150
246
  // kept so they can be re-enabled even when filtered out of the loader.
@@ -198,8 +294,8 @@ export class SettingsService {
198
294
  }
199
295
  // 若发现磁盘新增了 loader 尚未加载的技能,触发会话后台异步 reload 以刷新提示词和技能工具
200
296
  const hasUnloadedDiskSkills = diskSkills.some((ds) => !loadedSkillNames.has(ds.name));
201
- if (hasUnloadedDiskSkills && !this.host.isStreaming()) {
202
- void this.host.reloadSession();
297
+ if (maintain && hasUnloadedDiskSkills && !this.host.isStreaming()) {
298
+ void this.applyRuntime();
203
299
  }
204
300
  }
205
301
  catch {
@@ -209,7 +305,7 @@ export class SettingsService {
209
305
  // disabledSkills / reviewDisabledSkills 持久化记录中移除——否则每次
210
306
  // 推送都会把已删除的 skill 以灰条形式永恒地补回面板(“关闭过的
211
307
  // skill 被一直记录”)。session 未就绪时保守跳过。
212
- if (loadedSkillNames !== null) {
308
+ if (maintain && loadedSkillNames !== null) {
213
309
  const stale = [
214
310
  ...new Set([
215
311
  ...this.settings.disabledSkills,
@@ -221,7 +317,7 @@ export class SettingsService {
221
317
  this.settings.disabledSkills = this.settings.disabledSkills.filter((n) => !stale.includes(n));
222
318
  this.settings.reviewDisabledSkills = this.settings.reviewDisabledSkills.filter((n) => !stale.includes(n));
223
319
  this.settings.skillsFullText = normalizeSkillList(this.settings.skillsFullText).filter((n) => !stale.includes(n));
224
- this.host.stateStore.saveSettings(this.host.clientId, {
320
+ this.commitSettings({
225
321
  disabledSkills: this.settings.disabledSkills,
226
322
  reviewDisabledSkills: this.settings.reviewDisabledSkills,
227
323
  skillsFullText: this.settings.skillsFullText,
@@ -234,11 +330,11 @@ export class SettingsService {
234
330
  // disabledExtensions 同理:已卸载(loader 里没有、磁盘/配置里也没有)的扩展
235
331
  // 从禁用记录里剔除并持久化 —— 否则卸载过的扩展以“已禁用”灰条永生(issue #192)。
236
332
  // loadedExtNames 非 null 即 session 就绪;取不到(catch 分支)时保守跳过。
237
- if (loadedExtNames !== null) {
333
+ if (maintain && loadedExtNames !== null) {
238
334
  const staleExts = this.settings.disabledExtensions.filter((id) => isInternalExtension({ path: id }) || (!loadedExtNames.has(id) && !this.extensionStillOnDisk(id)));
239
335
  if (staleExts.length > 0) {
240
336
  this.settings.disabledExtensions = this.settings.disabledExtensions.filter((n) => !staleExts.includes(n));
241
- this.host.stateStore.saveSettings(this.host.clientId, {
337
+ this.commitSettings({
242
338
  disabledExtensions: this.settings.disabledExtensions,
243
339
  });
244
340
  }
@@ -323,6 +419,9 @@ export class SettingsService {
323
419
  showForkedParentSessions: this.settings.showForkedParentSessions ?? false,
324
420
  retainFinishedConversations: this.settings.retainFinishedConversations ?? false,
325
421
  openRecentSessionOnProjectSwitch: this.settings.openRecentSessionOnProjectSwitch ?? true,
422
+ chatUploadMaxMb: this.settings.chatUploadMaxMb ?? 20,
423
+ workspaceUploadMaxMb: this.settings.workspaceUploadMaxMb ?? 100,
424
+ folderUploadMaxMb: this.settings.folderUploadMaxMb ?? 32,
326
425
  visionBridgeEnabled: this.settings.visionBridgeEnabled,
327
426
  visionBridgeModel: this.settings.visionBridgeModel,
328
427
  visionBridgePromptMode: this.settings.visionBridgePromptMode,
@@ -366,7 +465,11 @@ export class SettingsService {
366
465
  subagentDefaultTemplates: DEFAULT_TEMPLATES.map((t) => t.name),
367
466
  subagentDefaultModel: this.settings.subagentDefaultModel ?? null,
368
467
  subagentMaxDepth: this.settings.subagentMaxDepth ?? 0,
468
+ questionPresentation: this.settings.questionPresentation ?? "queue",
469
+ questionTimeoutSeconds: this.settings.questionTimeoutSeconds ?? 240,
369
470
  subagentAutoWakeMaxTurns: this.settings.subagentAutoWakeMaxTurns ?? defaultSubagentAutoWakeMaxTurns(),
471
+ steeringMode: this.settings.steeringMode,
472
+ followUpMode: this.settings.followUpMode,
370
473
  retryMaxAttempts: this.settings.retryMaxAttempts,
371
474
  softCapTokens: this.settings.softCapTokens,
372
475
  softCapByModel: { ...this.settings.softCapByModel },
@@ -423,6 +526,8 @@ export class SettingsService {
423
526
  /** Persist + apply a partial settings update (compose template / per-source
424
527
  * overrides, skill/extension toggles). */
425
528
  async set(partial) {
529
+ const before = this.host.stateStore.getSettings(this.host.clientId);
530
+ const next = structuredClone(before);
426
531
  const needsReload = partial.promptMode !== undefined ||
427
532
  partial.customSystemPrompt !== undefined ||
428
533
  partial.promptTemplate !== undefined ||
@@ -440,262 +545,298 @@ export class SettingsService {
440
545
  partial.toolPromptOverrides !== undefined ||
441
546
  partial.toolLazyLoading !== undefined;
442
547
  if (partial.promptMode !== undefined)
443
- this.settings.promptMode = partial.promptMode;
548
+ next.promptMode = partial.promptMode;
444
549
  if (partial.customSystemPrompt !== undefined) {
445
- this.settings.customSystemPrompt = partial.customSystemPrompt;
550
+ next.customSystemPrompt = partial.customSystemPrompt;
446
551
  }
447
552
  if (partial.promptTemplate !== undefined) {
448
- this.settings.promptTemplate = partial.promptTemplate;
553
+ next.promptTemplate = partial.promptTemplate;
449
554
  }
450
555
  if (partial.promptOverrides !== undefined) {
451
556
  // 只合并给出的 key;空串 = 清除该来源覆盖。
452
- const next = { ...this.settings.promptOverrides };
557
+ const overrides = { ...next.promptOverrides };
453
558
  for (const [k, v] of Object.entries(partial.promptOverrides)) {
454
559
  if (v && v.trim())
455
- next[k] = v;
560
+ overrides[k] = v;
456
561
  else
457
- delete next[k];
562
+ delete overrides[k];
458
563
  }
459
- this.settings.promptOverrides = next;
564
+ next.promptOverrides = overrides;
460
565
  }
461
566
  if (partial.promptDisabled !== undefined)
462
- this.settings.promptDisabled = [...partial.promptDisabled];
567
+ next.promptDisabled = [...partial.promptDisabled];
463
568
  if (partial.toolPromptOverrides !== undefined) {
464
569
  // 逐工具合并:给出的键整体替换(归一化后为空 = 清除该工具覆盖)。
465
- const next = { ...this.settings.toolPromptOverrides };
570
+ const overrides = { ...next.toolPromptOverrides };
466
571
  const normalized = normalizeToolPromptOverrides(partial.toolPromptOverrides);
467
572
  for (const key of Object.keys(partial.toolPromptOverrides)) {
468
573
  if (normalized[key])
469
- next[key] = normalized[key];
574
+ overrides[key] = normalized[key];
470
575
  else
471
- delete next[key];
576
+ delete overrides[key];
472
577
  }
473
- this.settings.toolPromptOverrides = normalizeToolPromptOverrides(next);
578
+ next.toolPromptOverrides = normalizeToolPromptOverrides(overrides);
474
579
  }
475
580
  if (partial.disabledSkills !== undefined) {
476
- this.settings.disabledSkills = partial.disabledSkills;
581
+ next.disabledSkills = partial.disabledSkills;
477
582
  }
478
583
  if (partial.disabledExtensions !== undefined) {
479
- this.settings.disabledExtensions = partial.disabledExtensions;
584
+ next.disabledExtensions = partial.disabledExtensions;
480
585
  }
481
586
  // 插件开关是纯 UI 隐藏(不进 needsReload——运行时无需重载)。
482
587
  if (partial.disabledPlugins !== undefined) {
483
- this.settings.disabledPlugins = partial.disabledPlugins;
588
+ next.disabledPlugins = partial.disabledPlugins;
484
589
  }
485
590
  // UI 布局偏好只是渲染层的事(顶栏/底栏/右键菜单由前端拼),同样不需 reload。
486
591
  if (partial.uiLayout !== undefined) {
487
- this.settings.uiLayout = normalizeUiLayout(partial.uiLayout);
592
+ next.uiLayout = normalizeUiLayout(partial.uiLayout);
488
593
  }
489
594
  // 统一工具开关:新字段优先;只给遗留单开关时折回新字段。两边写完再由
490
595
  // deriveLegacy 回填遗留别名,保证内存/推送/落盘三处一致。
491
596
  if (partial.disabledAgentTools !== undefined) {
492
- this.settings.disabledAgentTools = normalizeDisabledAgentTools(partial.disabledAgentTools);
597
+ next.disabledAgentTools = normalizeDisabledAgentTools(partial.disabledAgentTools);
493
598
  }
494
599
  if (partial.disabledPluginTools !== undefined) {
495
- this.settings.disabledPluginTools = normalizeDisabledPluginTools(partial.disabledPluginTools);
600
+ next.disabledPluginTools = normalizeDisabledPluginTools(partial.disabledPluginTools);
496
601
  }
497
602
  if (partial.terminalToolsEnabled !== undefined ||
498
603
  partial.editSoftEnabled !== undefined ||
499
604
  partial.questionnaireEnabled !== undefined) {
500
- this.settings.disabledAgentTools = foldLegacyIntoDisabled(this.settings.disabledAgentTools ?? [], {
605
+ next.disabledAgentTools = foldLegacyIntoDisabled(next.disabledAgentTools ?? [], {
501
606
  terminalToolsEnabled: partial.terminalToolsEnabled,
502
607
  editSoftEnabled: partial.editSoftEnabled,
503
608
  questionnaireEnabled: partial.questionnaireEnabled,
504
609
  });
505
610
  }
506
611
  {
507
- const legacy = deriveLegacy(this.settings.disabledAgentTools ?? []);
508
- this.settings.terminalToolsEnabled = legacy.terminalToolsEnabled;
509
- this.settings.editSoftEnabled = legacy.editSoftEnabled;
510
- this.settings.questionnaireEnabled = legacy.questionnaireEnabled;
612
+ const legacy = deriveLegacy(next.disabledAgentTools ?? []);
613
+ next.terminalToolsEnabled = legacy.terminalToolsEnabled;
614
+ next.editSoftEnabled = legacy.editSoftEnabled;
615
+ next.questionnaireEnabled = legacy.questionnaireEnabled;
511
616
  }
512
617
  if (partial.terminalBash !== undefined) {
513
- this.settings.terminalBash = partial.terminalBash;
618
+ next.terminalBash = partial.terminalBash;
514
619
  }
515
620
  if (partial.terminalBashIdleMs !== undefined) {
516
- this.settings.terminalBashIdleMs = Math.max(0, Math.floor(partial.terminalBashIdleMs) || 0);
621
+ next.terminalBashIdleMs = Math.max(0, Math.floor(partial.terminalBashIdleMs) || 0);
517
622
  }
518
623
  if (partial.terminalBashMaxForegroundMs !== undefined) {
519
- this.settings.terminalBashMaxForegroundMs = Math.max(0, Math.floor(partial.terminalBashMaxForegroundMs) || 0);
624
+ next.terminalBashMaxForegroundMs = Math.max(0, Math.floor(partial.terminalBashMaxForegroundMs) || 0);
520
625
  }
521
626
  if (partial.toolWatchdogTimeoutMs !== undefined) {
522
- this.settings.toolWatchdogTimeoutMs = normalizeToolWatchdogTimeoutMs(partial.toolWatchdogTimeoutMs);
627
+ next.toolWatchdogTimeoutMs = normalizeToolWatchdogTimeoutMs(partial.toolWatchdogTimeoutMs);
523
628
  }
524
629
  // read 读目录开关:覆盖定义每次调用实时读取,改动即时生效,无需 reload。
525
630
  if (partial.readDirEnabled !== undefined) {
526
- this.settings.readDirEnabled = partial.readDirEnabled;
631
+ next.readDirEnabled = partial.readDirEnabled;
527
632
  }
528
633
  // 「后台任务」自动清理阈值(分钟;0 = 关):bg-servers 的定时器每轮实时读,即时生效。
529
634
  if (partial.bgAutoCleanupMin !== undefined) {
530
- this.settings.bgAutoCleanupMin = normalizeBgCleanupMinutes(partial.bgAutoCleanupMin);
635
+ next.bgAutoCleanupMin = normalizeBgCleanupMinutes(partial.bgAutoCleanupMin);
531
636
  }
532
637
  // 工具延迟加载开关(默认关,设置页可开):门控每次实时读取,改动经 applyToolGating 重放生效
533
638
  // (见 toolGatingChanged)——**只影响新会话与之后的门控重放**,已有对话的已加载
534
639
  // 集合不会被反向清空(不想让在跑的对话凭空丢掉工具)。
535
640
  if (partial.toolLazyLoading !== undefined) {
536
- this.settings.toolLazyLoading = partial.toolLazyLoading === true;
641
+ next.toolLazyLoading = partial.toolLazyLoading === true;
537
642
  }
538
643
  let codemodeChanged = false;
539
- if (partial.codemodeMode !== undefined && partial.codemodeMode !== this.settings.codemodeMode) {
540
- this.settings.codemodeMode = partial.codemodeMode;
644
+ if (partial.codemodeMode !== undefined && partial.codemodeMode !== next.codemodeMode) {
645
+ next.codemodeMode = partial.codemodeMode;
541
646
  codemodeChanged = true;
542
647
  }
543
- if (partial.codemodeInlineBudget !== undefined &&
544
- partial.codemodeInlineBudget !== this.settings.codemodeInlineBudget) {
545
- this.settings.codemodeInlineBudget = partial.codemodeInlineBudget;
648
+ if (partial.codemodeInlineBudget !== undefined && partial.codemodeInlineBudget !== next.codemodeInlineBudget) {
649
+ next.codemodeInlineBudget = partial.codemodeInlineBudget;
546
650
  codemodeChanged = true;
547
651
  }
548
- if (codemodeChanged) {
549
- this.host.applyCodemodeOverrides?.();
550
- }
551
652
  // 工具执行审批总开关:审批入口每次实时读取(askApproval 顶部门禁),无需 reload。
552
653
  if (partial.toolApprovalEnabled !== undefined) {
553
- this.settings.toolApprovalEnabled = partial.toolApprovalEnabled;
654
+ next.toolApprovalEnabled = partial.toolApprovalEnabled;
554
655
  }
555
656
  // 目标模式总开关:运行时无需重载(goal bar / 服务端入口实时读取)。
556
657
  // on→off 时立即停掉在飞的目标/调研 —— 不停的话循环照跑照派,开关名存实亡。
557
658
  if (partial.goalModeEnabled !== undefined) {
558
- const wasOn = this.settings.goalModeEnabled !== false;
559
- this.settings.goalModeEnabled = partial.goalModeEnabled;
560
- if (wasOn && partial.goalModeEnabled === false)
561
- this.host.onGoalModeDisabled?.();
659
+ next.goalModeEnabled = partial.goalModeEnabled;
562
660
  }
563
661
  // 同项目并行提醒开关:同上,发送入口逐轮实时读取,无需 reload。
564
662
  if (partial.parallelReminderEnabled !== undefined) {
565
- this.settings.parallelReminderEnabled = partial.parallelReminderEnabled;
663
+ next.parallelReminderEnabled = partial.parallelReminderEnabled;
566
664
  }
567
665
  if (partial.devNoCache !== undefined) {
568
- this.settings.devNoCache = partial.devNoCache;
666
+ next.devNoCache = partial.devNoCache;
569
667
  }
570
668
  if (partial.autoReload !== undefined) {
571
- this.settings.autoReload = partial.autoReload;
669
+ next.autoReload = partial.autoReload;
572
670
  }
573
671
  if (partial.thinkingWrap !== undefined) {
574
- this.settings.thinkingWrap = partial.thinkingWrap;
672
+ next.thinkingWrap = partial.thinkingWrap;
575
673
  }
576
674
  if (partial.toolsWrap !== undefined) {
577
- this.settings.toolsWrap = partial.toolsWrap;
675
+ next.toolsWrap = partial.toolsWrap;
578
676
  }
579
677
  if (partial.toolImagesEnabled !== undefined) {
580
- this.settings.toolImagesEnabled = partial.toolImagesEnabled;
678
+ next.toolImagesEnabled = partial.toolImagesEnabled;
581
679
  }
582
680
  if (partial.keepRecentMessages !== undefined) {
583
- this.settings.keepRecentMessages = normalizeKeepRecentMessages(partial.keepRecentMessages);
681
+ next.keepRecentMessages = normalizeKeepRecentMessages(partial.keepRecentMessages);
584
682
  }
585
683
  // 向上分页加载:前端 MessageList 实时读它(纯渲染策略),无需 reload runtime。
586
684
  if (partial.reversePagination !== undefined) {
587
- this.settings.reversePagination = Boolean(partial.reversePagination);
685
+ next.reversePagination = Boolean(partial.reversePagination);
588
686
  }
589
687
  if (partial.retainFinishedConversations !== undefined) {
590
- this.settings.retainFinishedConversations = Boolean(partial.retainFinishedConversations);
688
+ next.retainFinishedConversations = Boolean(partial.retainFinishedConversations);
591
689
  }
592
690
  if (partial.openRecentSessionOnProjectSwitch !== undefined) {
593
- this.settings.openRecentSessionOnProjectSwitch = Boolean(partial.openRecentSessionOnProjectSwitch);
691
+ next.openRecentSessionOnProjectSwitch = Boolean(partial.openRecentSessionOnProjectSwitch);
594
692
  }
595
693
  let sessionsFilterChanged = false;
596
694
  if (partial.showForkedParentSessions !== undefined) {
597
695
  const nextVal = Boolean(partial.showForkedParentSessions);
598
- if (nextVal !== (this.settings.showForkedParentSessions ?? false)) {
696
+ if (nextVal !== (next.showForkedParentSessions ?? false)) {
599
697
  sessionsFilterChanged = true;
600
698
  }
601
- this.settings.showForkedParentSessions = nextVal;
699
+ next.showForkedParentSessions = nextVal;
602
700
  }
603
701
  // 编排模式 / skill 全文注入:before_agent_start 逐 run 实时读取(agent-service
604
702
  // composeInputs + 指导块追加),开关下一轮即生效,无需 reload runtime。
605
703
  if (partial.skillsFullText !== undefined) {
606
- this.settings.skillsFullText = normalizeSkillList(partial.skillsFullText);
704
+ next.skillsFullText = normalizeSkillList(partial.skillsFullText);
607
705
  }
608
706
  if (partial.skillsCatalogMaxDesc !== undefined) {
609
- this.settings.skillsCatalogMaxDesc = normalizeSkillsCatalogMaxDesc(partial.skillsCatalogMaxDesc);
707
+ next.skillsCatalogMaxDesc = normalizeSkillsCatalogMaxDesc(partial.skillsCatalogMaxDesc);
610
708
  }
611
709
  if (partial.skillsCatalogShowLocation !== undefined) {
612
- this.settings.skillsCatalogShowLocation = Boolean(partial.skillsCatalogShowLocation);
710
+ next.skillsCatalogShowLocation = Boolean(partial.skillsCatalogShowLocation);
613
711
  }
614
712
  if (partial.skillsCatalogBudget !== undefined) {
615
- this.settings.skillsCatalogBudget = normalizeSkillsCatalogBudget(partial.skillsCatalogBudget);
713
+ next.skillsCatalogBudget = normalizeSkillsCatalogBudget(partial.skillsCatalogBudget);
714
+ }
715
+ for (const key of ["chatUploadMaxMb", "workspaceUploadMaxMb", "folderUploadMaxMb"]) {
716
+ if (partial[key] !== undefined)
717
+ next[key] = partial[key];
616
718
  }
617
719
  if (partial.visionBridgeEnabled !== undefined) {
618
- this.settings.visionBridgeEnabled = partial.visionBridgeEnabled;
720
+ next.visionBridgeEnabled = partial.visionBridgeEnabled;
619
721
  }
620
722
  if (partial.visionBridgeModel !== undefined) {
621
- this.settings.visionBridgeModel = partial.visionBridgeModel ?? null;
723
+ next.visionBridgeModel = partial.visionBridgeModel ?? null;
622
724
  }
623
725
  if (partial.visionBridgePromptMode !== undefined) {
624
- this.settings.visionBridgePromptMode = partial.visionBridgePromptMode;
726
+ next.visionBridgePromptMode = partial.visionBridgePromptMode;
625
727
  }
626
728
  if (partial.visionBridgePrompt !== undefined) {
627
- this.settings.visionBridgePrompt = partial.visionBridgePrompt;
729
+ next.visionBridgePrompt = partial.visionBridgePrompt;
628
730
  }
629
731
  if (partial.scmCommitMsgPromptMode !== undefined) {
630
- this.settings.scmCommitMsgPromptMode = partial.scmCommitMsgPromptMode;
732
+ next.scmCommitMsgPromptMode = partial.scmCommitMsgPromptMode;
631
733
  }
632
734
  if (partial.scmCommitMsgPrompt !== undefined) {
633
- this.settings.scmCommitMsgPrompt = partial.scmCommitMsgPrompt;
735
+ next.scmCommitMsgPrompt = partial.scmCommitMsgPrompt;
634
736
  }
635
737
  if (partial.planModePromptMode !== undefined) {
636
- this.settings.planModePromptMode = partial.planModePromptMode;
738
+ next.planModePromptMode = partial.planModePromptMode;
637
739
  }
638
740
  if (partial.planModePrompt !== undefined) {
639
- this.settings.planModePrompt = partial.planModePrompt;
741
+ next.planModePrompt = partial.planModePrompt;
640
742
  }
641
743
  if (partial.reviewPrompt !== undefined) {
642
- this.settings.reviewPrompt = partial.reviewPrompt;
744
+ next.reviewPrompt = partial.reviewPrompt;
643
745
  }
644
746
  if (partial.reviewDisabledSkills !== undefined) {
645
- this.settings.reviewDisabledSkills = partial.reviewDisabledSkills;
747
+ next.reviewDisabledSkills = partial.reviewDisabledSkills;
646
748
  }
647
749
  if (partial.subagentDefaultModel !== undefined) {
648
750
  // 空串归一为 null(跟随主对话);其余剥空白后存格式 provider/id。
649
751
  const m = partial.subagentDefaultModel?.trim() ?? "";
650
- this.settings.subagentDefaultModel = m ? m : null;
752
+ next.subagentDefaultModel = m ? m : null;
753
+ }
754
+ if (partial.questionPresentation !== undefined) {
755
+ if (!["queue", "all", "manual"].includes(partial.questionPresentation))
756
+ throw new Error("Invalid questionnaire presentation");
757
+ next.questionPresentation = partial.questionPresentation;
758
+ }
759
+ if (partial.questionTimeoutSeconds !== undefined) {
760
+ const seconds = partial.questionTimeoutSeconds;
761
+ if (!Number.isSafeInteger(seconds) || seconds < 0 || seconds > 86400)
762
+ throw new Error("Question timeout must be 0–86400 seconds");
763
+ next.questionTimeoutSeconds = seconds;
651
764
  }
652
765
  if (partial.subagentMaxDepth !== undefined) {
653
766
  const n = Math.floor(Number(partial.subagentMaxDepth));
654
- this.settings.subagentMaxDepth = Number.isFinite(n) && n > 0 ? n : 0;
767
+ next.subagentMaxDepth = Number.isFinite(n) && n > 0 ? n : 0;
655
768
  }
656
769
  if (partial.subagentAutoWakeMaxTurns !== undefined) {
657
- this.settings.subagentAutoWakeMaxTurns = normalizeSubagentAutoWakeMaxTurns(partial.subagentAutoWakeMaxTurns);
770
+ next.subagentAutoWakeMaxTurns = normalizeSubagentAutoWakeMaxTurns(partial.subagentAutoWakeMaxTurns);
771
+ }
772
+ for (const key of ["steeringMode", "followUpMode"]) {
773
+ const mode = partial[key];
774
+ if (mode === undefined)
775
+ continue;
776
+ if (mode !== "all" && mode !== "one-at-a-time")
777
+ throw new Error(`Invalid ${key}`);
778
+ next[key] = mode;
658
779
  }
659
780
  if (partial.retryMaxAttempts !== undefined) {
660
781
  // 出错重试次数:持久化 + 即时注入各会话(SDK 在每次退避前都重读
661
782
  // getRetrySettings,无需 reload runtime;见宿主 applyRetryOverrides)。
662
- this.settings.retryMaxAttempts = normalizeRetryMaxAttempts(partial.retryMaxAttempts);
663
- this.host.applyRetryOverrides();
783
+ next.retryMaxAttempts = normalizeRetryMaxAttempts(partial.retryMaxAttempts);
664
784
  }
665
785
  if (partial.softCapTokens !== undefined || partial.softCapByModel !== undefined) {
666
786
  // 压缩软上限:持久化 + 即时重算各会话的 compaction reserveTokens
667
787
  // 覆盖(SDK 每次自动压缩检查前都重读 getCompactionSettings,无需
668
788
  // reload;见宿主 applyCompactionOverrides,issue #229)。
669
789
  if (partial.softCapTokens !== undefined)
670
- this.settings.softCapTokens = normalizeSoftCapTokens(partial.softCapTokens);
790
+ next.softCapTokens = normalizeSoftCapTokens(partial.softCapTokens);
671
791
  if (partial.softCapByModel !== undefined)
672
- this.settings.softCapByModel = normalizeSoftCapByModel(partial.softCapByModel);
673
- this.host.applyCompactionOverrides();
792
+ next.softCapByModel = normalizeSoftCapByModel(partial.softCapByModel);
674
793
  // 底栏标记线读快照的 contextUsage.softCap——推一次快照让在线页即时看到。
675
- this.host.flushSnapshot();
676
794
  }
677
795
  if (partial.quickPhrases !== undefined) {
678
796
  // 归一化:去空白/空项/重名,单条 ≤200 字,最多 30 条。纯 UI 偏好,不 reload。
679
797
  const seen = new Set();
680
- this.settings.quickPhrases = (Array.isArray(partial.quickPhrases) ? partial.quickPhrases : [])
798
+ next.quickPhrases = (Array.isArray(partial.quickPhrases) ? partial.quickPhrases : [])
681
799
  .map((p) => String(p).trim().slice(0, 200))
682
800
  .filter((p) => p && !seen.has(p) && (seen.add(p), true))
683
801
  .slice(0, 30);
684
802
  }
685
803
  if (partial.quickPhrasesEnabled !== undefined) {
686
- this.settings.quickPhrasesEnabled = partial.quickPhrasesEnabled;
804
+ next.quickPhrasesEnabled = partial.quickPhrasesEnabled;
687
805
  }
688
806
  if (partial.minimalNewChat !== undefined) {
689
- this.settings.minimalNewChat = partial.minimalNewChat;
807
+ next.minimalNewChat = partial.minimalNewChat;
690
808
  }
691
809
  if (partial.preserveDraftAcrossConversations !== undefined) {
692
- this.settings.preserveDraftAcrossConversations = Boolean(partial.preserveDraftAcrossConversations);
810
+ next.preserveDraftAcrossConversations = Boolean(partial.preserveDraftAcrossConversations);
693
811
  }
694
812
  // 统一工具开关 live 生效(ActiveSet 加减;失败静默,下次创建/reload 重放)。
695
813
  // 必须在 this.push() 前生效,保证 promptSnapshot 收集到的是最新的活跃工具集与完整提示词!
814
+ const patch = {};
815
+ for (const key of Object.keys(partial)) {
816
+ if (key in next && partial[key] !== undefined) {
817
+ Object.assign(patch, { [key]: next[key] });
818
+ }
819
+ }
820
+ if (partial.disabledAgentTools !== undefined ||
821
+ partial.terminalToolsEnabled !== undefined ||
822
+ partial.editSoftEnabled !== undefined ||
823
+ partial.questionnaireEnabled !== undefined) {
824
+ patch.disabledAgentTools = next.disabledAgentTools;
825
+ Object.assign(patch, deriveLegacy(next.disabledAgentTools ?? []));
826
+ }
827
+ this.commitSettings(patch);
828
+ if (codemodeChanged)
829
+ this.host.applyCodemodeOverrides?.();
830
+ if (before.goalModeEnabled !== false && next.goalModeEnabled === false)
831
+ this.host.onGoalModeDisabled?.();
832
+ if (partial.retryMaxAttempts !== undefined)
833
+ this.host.applyRetryOverrides();
834
+ if (before.steeringMode !== next.steeringMode || before.followUpMode !== next.followUpMode)
835
+ this.host.applyQueueModes?.();
836
+ if (partial.softCapTokens !== undefined || partial.softCapByModel !== undefined)
837
+ this.host.applyCompactionOverrides();
696
838
  if (toolGatingChanged)
697
839
  this.host.applyToolGating();
698
- this.host.stateStore.saveSettings(this.host.clientId, this.settings);
699
840
  this.push();
700
841
  if (sessionsFilterChanged)
701
842
  this.host.onSessionsFilterChanged?.();
@@ -715,6 +856,7 @@ export class SettingsService {
715
856
  });
716
857
  return;
717
858
  }
859
+ this.presets = this.host.stateStore.getPresets(this.host.clientId);
718
860
  const preset = this.snapshotPreset(n);
719
861
  const existing = this.presets.findIndex((p) => p.name === n);
720
862
  if (existing >= 0)
@@ -728,11 +870,13 @@ export class SettingsService {
728
870
  * **全量**:`ClientSettings` 的每个字段都进预设(字段清单/分组见 `server/preset-fields.ts`),
729
871
  * 导出/分享/导入共用同一张白名单;深层对象做一份拷贝,避免与当前设置共享引用。 */
730
872
  snapshotPreset(name) {
731
- const s = structuredClone(this.settings);
873
+ const s = structuredClone(this.current);
732
874
  return { ...s, name };
733
875
  }
734
876
  /** Replace the current settings with the named preset and apply it. */
735
877
  async applyPreset(name) {
878
+ this.settings = this.current;
879
+ this.presets = this.host.stateStore.getPresets(this.host.clientId);
736
880
  const p = this.presets.find((x) => x.name === name);
737
881
  if (!p) {
738
882
  this.host.emit({
@@ -744,6 +888,7 @@ export class SettingsService {
744
888
  return;
745
889
  }
746
890
  // 统一工具开关:新预设直接用它;旧预设(只有遗留两开关)按当前禁用名单折算。
891
+ const hasToolSettings = ["disabledAgentTools", "terminalToolsEnabled", "editSoftEnabled"].some((key) => Object.prototype.hasOwnProperty.call(p, key));
747
892
  const presetDisabled = normalizeDisabledAgentTools(p.disabledAgentTools ??
748
893
  foldLegacyIntoDisabled(this.settings.disabledAgentTools ?? [], {
749
894
  terminalToolsEnabled: p.terminalToolsEnabled,
@@ -754,25 +899,30 @@ export class SettingsService {
754
899
  // 旧预设(19 字段)、以及导入时只勾了一部分的预设,都靠这个 merge 落地。
755
900
  const patch = { ...p };
756
901
  delete patch.name;
902
+ delete patch.plugins;
757
903
  for (const k of Object.keys(patch))
758
904
  if (patch[k] === undefined)
759
905
  delete patch[k];
760
906
  const prevShowForked = this.settings.showForkedParentSessions ?? false;
761
- this.settings = {
762
- ...this.settings,
907
+ this.commitSettings({
763
908
  ...patch,
764
- // 工具开关与两个遗留别名以派生结果为准(单源:disabledAgentTools)。
765
- disabledAgentTools: presetDisabled,
766
- terminalToolsEnabled: presetLegacy.terminalToolsEnabled,
767
- editSoftEnabled: presetLegacy.editSoftEnabled,
768
- };
769
- this.host.stateStore.saveSettings(this.host.clientId, this.settings);
909
+ // 部分预设未携带工具字段时,保持当前设置;遗留别名仍统一派生。
910
+ ...(hasToolSettings
911
+ ? {
912
+ disabledAgentTools: presetDisabled,
913
+ terminalToolsEnabled: presetLegacy.terminalToolsEnabled,
914
+ editSoftEnabled: presetLegacy.editSoftEnabled,
915
+ }
916
+ : {}),
917
+ });
770
918
  // 统一收口重放配置覆盖与工具门控(包含 retry/compaction/codemode 与 toolGating)
771
919
  if (this.host.applySessionOverrides) {
772
920
  this.host.applySessionOverrides();
773
921
  }
774
922
  else {
775
923
  this.host.applyRetryOverrides();
924
+ this.host.applyQueueModes?.();
925
+ this.host.applyCompactionOverrides();
776
926
  this.host.applyCodemodeOverrides?.();
777
927
  this.host.applyToolGating();
778
928
  }
@@ -780,11 +930,11 @@ export class SettingsService {
780
930
  if ((this.settings.showForkedParentSessions ?? false) !== prevShowForked) {
781
931
  this.host.onSessionsFilterChanged?.();
782
932
  }
783
- await this.applyRuntime();
933
+ await this.applyRuntime(true);
784
934
  }
785
935
  /** Remove a named preset. */
786
936
  async deletePreset(name) {
787
- this.presets = this.presets.filter((p) => p.name !== name);
937
+ this.presets = this.host.stateStore.getPresets(this.host.clientId).filter((p) => p.name !== name);
788
938
  this.host.stateStore.savePresets(this.host.clientId, this.presets);
789
939
  this.push();
790
940
  }
@@ -800,12 +950,13 @@ export class SettingsService {
800
950
  return {
801
951
  lang: () => this.lang,
802
952
  appVersion: () => packageVersion(),
803
- presets: () => this.presets,
953
+ presets: () => this.host.stateStore.getPresets(this.host.clientId),
804
954
  currentSettings: () => {
805
955
  const { name: _drop, ...rest } = this.snapshotPreset("");
806
956
  return { ...rest };
807
957
  },
808
958
  upsertPreset: (preset) => {
959
+ this.presets = this.host.stateStore.getPresets(this.host.clientId);
809
960
  const i = this.presets.findIndex((p) => p.name === preset.name);
810
961
  if (i >= 0)
811
962
  this.presets[i] = preset;
@@ -814,6 +965,7 @@ export class SettingsService {
814
965
  this.host.stateStore.savePresets(this.host.clientId, this.presets);
815
966
  },
816
967
  applyPreset: (name) => this.applyPreset(name),
968
+ isApplyPending: () => this.hasPendingReload(),
817
969
  pushSettings: () => this.push(),
818
970
  emit: (msg) => this.host.emit(msg),
819
971
  };
@@ -964,8 +1116,8 @@ export class SettingsService {
964
1116
  * overrides read this.settings at call time, so a reload re-applies them.
965
1117
  * Reloading mid-stream would tear down the in-flight run — defer instead.
966
1118
  */
967
- async applyRuntime() {
968
- if (this.host.isDisposed())
1119
+ async applyRuntime(throwOnError = false) {
1120
+ if (this.disposed || this.host.isDisposed())
969
1121
  return;
970
1122
  if (this.host.isStreaming()) {
971
1123
  this.pendingReload = true;
@@ -977,11 +1129,11 @@ export class SettingsService {
977
1129
  });
978
1130
  return;
979
1131
  }
980
- await this.applyReload();
1132
+ await this.applyReload(throwOnError);
981
1133
  }
982
1134
  /** session.reload() + refresh the slash-command catalog + push state. */
983
- async applyReload() {
984
- if (this.host.isDisposed())
1135
+ async applyReload(throwOnError = false) {
1136
+ if (this.disposed || this.host.isDisposed())
985
1137
  return;
986
1138
  if (this.host.isStreaming()) {
987
1139
  this.pendingReload = true;
@@ -995,7 +1147,9 @@ export class SettingsService {
995
1147
  }
996
1148
  try {
997
1149
  await this.host.reloadSession();
998
- this.push();
1150
+ if (this.disposed || this.host.isDisposed())
1151
+ return;
1152
+ this.push(false);
999
1153
  this.host.flushSnapshot();
1000
1154
  this.host.emit({ type: "notice", level: "info", text: "设置已应用", textEn: "Settings applied" });
1001
1155
  }
@@ -1007,6 +1161,8 @@ export class SettingsService {
1007
1161
  text: `设置应用失败:${err.message}`,
1008
1162
  textEn: `Failed to apply settings: ${err.message}`,
1009
1163
  });
1164
+ if (throwOnError)
1165
+ throw err;
1010
1166
  }
1011
1167
  }
1012
1168
  }