dsh-tool-manager 1.5.2 → 1.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/client.js CHANGED
@@ -17,7 +17,7 @@ window.__ModuleLoader__.load({
17
17
  ".tm_model_block{display:flex;flex-direction:column;gap:6px;min-width:0}.tm_model_row{display:flex;align-items:center;gap:8px;flex-wrap:wrap;min-width:0}.tm_model_row .tm_model_label{flex:0 0 auto;font-size:12px;color:var(--dsw-alias-label-secondary,#9097a4);cursor:help;white-space:nowrap}.tm_model_row .tm_select_wrap{min-width:0}.tm_model_row .tm_model_pick{flex:1 1 auto}.tm_model_row .tm_preset_select{min-width:0;width:100%}.tm_model_row .tm_effort_pick{flex:0 0 auto}.tm_model_row .tm_effort_pick .tm_preset_select{width:auto;min-width:96px}.tm_model_hints{display:flex;align-items:center;gap:8px;flex-wrap:wrap}",
18
18
  ".tm_panel{border:1px solid var(--dsw-alias-border-l2,rgba(128,128,128,.24));border-radius:12px;background:var(--dsw-alias-bg-layer-1,rgba(128,128,128,.06));overflow:hidden}",
19
19
  ".tm_panel_head{padding:13px 14px;border-bottom:1px solid var(--dsw-alias-border-l2,rgba(128,128,128,.2));display:flex;justify-content:space-between;gap:12px;align-items:center;flex-wrap:wrap;position:relative}.tm_panel_head b{font-size:14px}",
20
- ".tm_tools{display:grid;grid-template-columns:repeat(auto-fit,minmax(280px,1fr));gap:0}.tm_tool{display:flex;gap:10px;align-items:flex-start;padding:11px 14px;border-bottom:1px solid var(--dsw-alias-border-l2,rgba(128,128,128,.13))}.tm_tool_name{font:600 12px ui-monospace,SFMono-Regular,Consolas,monospace}.tm_tool_desc{font-size:11px;color:var(--dsw-alias-label-secondary,#9097a4);line-height:1.4;margin-top:3px;display:-webkit-box;-webkit-line-clamp:2;-webkit-box-orient:vertical;overflow:hidden}",
20
+ ".tm_tools{display:grid;grid-template-columns:repeat(auto-fit,minmax(280px,1fr));gap:0}.tm_tool{display:flex;gap:10px;align-items:flex-start;padding:11px 14px;border-bottom:1px solid var(--dsw-alias-border-l2,rgba(128,128,128,.13))}.tm_tool_name{font:600 12px ui-monospace,SFMono-Regular,Consolas,monospace}.tm_group_ref{display:inline-block;margin-left:6px;padding:1px 6px;border-radius:6px;background:rgba(128,128,128,.12);color:var(--dsw-alias-label-secondary,#9097a4);font:400 10px/1.5 ui-sans-serif,system-ui,sans-serif;vertical-align:middle}.tm_group_name{display:inline-block;margin-left:4px;color:var(--dsw-alias-label-secondary,#9097a4);font:400 10px/1.5 ui-sans-serif,system-ui,sans-serif;vertical-align:middle}.tm_tool_desc{font-size:11px;color:var(--dsw-alias-label-secondary,#9097a4);line-height:1.4;margin-top:3px;display:-webkit-box;-webkit-line-clamp:2;-webkit-box-orient:vertical;overflow:hidden}",
21
21
  ".tm_row{display:flex;gap:8px;align-items:center;flex-wrap:wrap}.tm_input{box-sizing:border-box;min-width:130px;flex:1;border:1px solid var(--dsw-alias-border-l2,rgba(128,128,128,.3));border-radius:7px;background:var(--dsw-alias-bg-base,#17191d);color:inherit;padding:7px 9px;font:inherit;font-size:12px}.tm_code{font:11px ui-monospace,SFMono-Regular,Consolas,monospace;color:#9da8ff}.tm_notice{padding:12px;border-radius:9px;background:color-mix(in srgb,#f59e0b 12%,transparent);font-size:12px}.tm_error{padding:12px;border-radius:9px;background:color-mix(in srgb,#ef4444 12%,transparent);color:#fca5a5;font-size:12px}",
22
22
  ".tm_filters{display:flex;gap:6px;flex-wrap:wrap;align-items:center}.tm_chip{appearance:none;border:1px solid var(--dsw-alias-border-l2,rgba(128,128,128,.28));border-radius:999px;background:transparent;color:inherit;padding:4px 9px;cursor:pointer;font:inherit;font-size:11px}.tm_chip.active{border-color:#7c8cff;background:color-mix(in srgb,#7c8cff 16%,transparent)}",
23
23
  ".tm_stats{display:flex;flex-wrap:nowrap;gap:6px;padding:8px 12px;border-bottom:1px solid var(--dsw-alias-border-l2,rgba(128,128,128,.13))}.tm_stat{flex:1 1 0;min-width:0;padding:6px 7px;border-radius:7px;background:var(--dsw-alias-bg-layer-1,rgba(128,128,128,.07));border:1px solid var(--dsw-alias-border-l2,rgba(128,128,128,.15))}.tm_stat_value{font-size:15px;font-weight:700;line-height:1.15;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}.tm_stat_label{margin-top:2px;font-size:10px;line-height:1.3;color:var(--dsw-alias-label-secondary,#9097a4);white-space:nowrap;overflow:hidden;text-overflow:ellipsis}@media(max-width:560px){.tm_stats{flex-wrap:wrap}.tm_stat{flex:1 1 30%}}",
@@ -25,13 +25,13 @@ window.__ModuleLoader__.load({
25
25
  ".tm_help{display:inline-flex;align-items:center;justify-content:center;width:15px;height:15px;margin-left:6px;border:1px solid var(--dsw-alias-border-l2,rgba(128,128,128,.28));border-radius:50%;background:var(--dsw-alias-bg-layer-1,rgba(128,128,128,.08));color:var(--dsw-alias-label-secondary,#9097a4);font:700 10px/1 ui-sans-serif,system-ui,sans-serif;cursor:help;vertical-align:middle;outline:none}.tm_help:hover,.tm_help:focus-visible{border-color:#7c8cff;color:#9da8ff}.tm_help::after{content:attr(data-tip);position:absolute;left:0;top:calc(100% + 6px);width:min(520px,calc(100vw - 48px));max-width:none;padding:10px 12px;border:1px solid var(--dsw-alias-border-l2,rgba(128,128,128,.28));border-radius:9px;background:var(--dsw-alias-bg-base,#17191d);box-shadow:0 14px 40px rgba(0,0,0,.35);color:var(--dsw-alias-label-primary,#e7e9ed);font:400 12px/1.65 ui-sans-serif,system-ui,sans-serif;white-space:pre-line;text-align:left;opacity:0;visibility:hidden;pointer-events:none;z-index:30;transition:opacity .12s ease}.tm_help:hover::after,.tm_help:focus-visible::after{opacity:1;visibility:visible}",
26
26
  ".tm_badge{display:inline-block;margin-left:6px;padding:2px 7px;border:1px solid transparent;border-radius:999px;font-size:10px;font-weight:700;line-height:1.35;letter-spacing:.02em;vertical-align:middle;color:#fff}.tm_badge.off{background:#b42318;border-color:#d92d20;color:#fff}.tm_badge.cold{background:#4338ca;border-color:#6366f1;color:#fff}.tm_badge.live{background:#15803d;border-color:#22c55e;color:#fff}.tm_badge.ptc{background:#6d28d9;border-color:#a78bfa;color:#fff}.tm_badge.native{background:#334155;border-color:#64748b;color:#fff}",
27
27
  ".tm_group{border-bottom:1px solid var(--dsw-alias-border-l2,rgba(128,128,128,.13))}.tm_group:last-child{border-bottom:0}",
28
- ".tm_group_bar{display:flex;align-items:center;gap:8px;padding:11px 14px}",
28
+ ".tm_group_bar{display:flex;align-items:center;gap:8px;padding:11px 14px}.tm_group_mode{border:1px solid var(--dsw-alias-border-l2,rgba(128,128,128,.28));border-radius:999px;background:var(--dsw-alias-bg-layer-1,rgba(128,128,128,.08));color:inherit;padding:3px 8px;font:inherit;font-size:11px;cursor:pointer}.tm_group_mode.always{color:#22c55e;border-color:#22c55e;background:rgba(34,197,94,.12)}.tm_group_mode.ondemand{color:#818cf8;border-color:#6366f1;background:rgba(99,102,241,.12)}.tm_group_mode.off{color:#f87171;border-color:#ef4444;background:rgba(239,68,68,.12)}.tm_group_mode option{background:var(--dsw-alias-bg-base,#17191d);color:var(--dsw-alias-label-primary,#e7e9ed)}.tm_group_mode option[value=always]{color:#22c55e}.tm_group_mode option[value=ondemand]{color:#818cf8}.tm_group_mode option[value=off]{color:#f87171}",
29
29
  ".tm_chevron{appearance:none;border:0;background:transparent;color:inherit;width:28px;height:28px;border-radius:7px;cursor:pointer;font:16px/1 inherit;flex:0 0 auto}.tm_chevron:hover{background:var(--dsw-alias-bg-layer-1,rgba(128,128,128,.1))}",
30
30
  ".tm_group_main{appearance:none;border:0;background:transparent;color:inherit;text-align:left;flex:1;min-width:0;cursor:pointer;padding:0}",
31
31
  ".tm_group_title{font-size:13px;font-weight:600;line-height:1.3}.tm_group_sub{font-size:11px;color:var(--dsw-alias-label-secondary,#9097a4);margin-top:2px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap}",
32
32
  ".tm_group_count{flex:0 0 auto;font-size:12px;color:var(--dsw-alias-label-secondary,#9097a4)}",
33
33
  ".tm_group_body{padding:0 14px 12px 50px}",
34
- ".tm_member{padding:7px 0;border-bottom:1px solid var(--dsw-alias-border-l2,rgba(128,128,128,.08))}.tm_member:last-child{border-bottom:0}",
34
+ ".tm_member{display:flex;gap:8px;align-items:flex-start;padding:7px 0;border-bottom:1px solid var(--dsw-alias-border-l2,rgba(128,128,128,.08))}.tm_member:last-child{border-bottom:0}",
35
35
  ".tm_modal{position:fixed;inset:0;z-index:10000;display:flex;align-items:center;justify-content:center;padding:24px}",
36
36
  ".tm_modal_mask{position:absolute;inset:0;background:rgba(0,0,0,.55)}",
37
37
  ".tm_modal_card{position:relative;z-index:1;width:min(720px,100%);max-height:min(84vh,760px);display:flex;flex-direction:column;border:1px solid var(--dsw-alias-border-l2,rgba(128,128,128,.28));border-radius:14px;background:var(--dsw-alias-bg-base,#17191d);box-shadow:0 24px 80px rgba(0,0,0,.45)}",
@@ -89,17 +89,27 @@ window.__ModuleLoader__.load({
89
89
  function policyPayload(policy) {
90
90
  const payload = {
91
91
  disabled: [...(policy.disabled || [])],
92
- groups: (policy.groups || []).map((group) => ({
93
- name: group.name,
94
- ...(group.description ? { description: group.description } : {}),
95
- patterns: [...(group.patterns || [])],
96
- })),
92
+ groups: (policy.groups || []).map((group) => {
93
+ const loading = group.loading === "always" || group.loading === "off" ? group.loading : undefined;
94
+ const groupDisabled = Array.isArray(group.disabled) ? [...group.disabled] : [];
95
+ return {
96
+ name: group.name,
97
+ ...(group.description ? { description: group.description } : {}),
98
+ patterns: [...(group.patterns || [])],
99
+ ...(loading === undefined ? {} : { loading }),
100
+ ...(groupDisabled.length === 0 ? {} : { disabled: groupDisabled }),
101
+ };
102
+ }),
97
103
  };
98
104
  if (policy.mode === "mcp") payload.mode = "mcp";
105
+ if (typeof policy.keepAfterCompaction === "number") {
106
+ payload.keepAfterCompaction = policy.keepAfterCompaction;
107
+ }
99
108
  return payload;
100
109
  }
101
110
  /**
102
- * Copy one Preset's on-demand groups and disabled names onto another.
111
+ * Copy one Preset's groups (including each group's loading mode and
112
+ * group-level disables) and disabled names onto another.
103
113
  *
104
114
  * The target is meant to be overwritten, so its current groups and
105
115
  * disabled names are ignored. Copy only what the target can actually
@@ -112,7 +122,8 @@ window.__ModuleLoader__.load({
112
122
  * groups it owns (tool_list in default mode, tool_find in mcp mode), so
113
123
  * it belongs to the target. Rebuilding the policy used to omit `mode`,
114
124
  * which silently turned an MCP 按需 target back into default mode after
115
- * a copy.
125
+ * a copy. The compaction keep limit is policy rather than loading
126
+ * semantics, so it follows the groups: the target is overwritten whole.
116
127
  */
117
128
  function copyPresetPolicy(sourcePreset, targetPreset) {
118
129
  const sourceTools = sourcePreset.tools || [];
@@ -125,36 +136,63 @@ window.__ModuleLoader__.load({
125
136
  const groups = [];
126
137
  const skippedGroups = [];
127
138
  for (const group of sourcePolicy.groups || []) {
139
+ const sourceMembers = new Set(matchingTools(sourceTools, group.patterns || []).map((tool) => tool.name));
140
+ const groupDisabled = new Set(
141
+ (group.disabled || []).filter((name) => sourceMembers.has(name) && targetNames.has(name)),
142
+ );
128
143
  const members = [];
129
144
  for (const tool of matchingTools(sourceTools, group.patterns || [])) {
130
145
  if (!targetNames.has(tool.name) || claimed.has(tool.name)) continue;
131
146
  claimed.add(tool.name);
132
147
  members.push(tool.name);
133
148
  }
134
- if (members.length === 0) {
149
+ if (!members.some((name) => !groupDisabled.has(name))) {
135
150
  skippedGroups.push(group.name || "未命名分组");
136
151
  continue;
137
152
  }
153
+ const loading = group.loading === "always" || group.loading === "off" ? group.loading : undefined;
154
+ const disabledMembers = [...groupDisabled];
138
155
  groups.push({
139
156
  name: group.name,
140
157
  ...(group.description ? { description: group.description } : {}),
141
158
  patterns: members,
159
+ ...(loading === undefined ? {} : { loading }),
160
+ ...(disabledMembers.length === 0 ? {} : { disabled: disabledMembers }),
142
161
  });
143
162
  }
144
163
  const mode = targetPreset.policy && targetPreset.policy.mode === "mcp" ? "mcp" : undefined;
145
- return { disabled, groups, ...(mode ? { mode } : {}), skippedGroups };
164
+ const keep = typeof sourcePolicy.keepAfterCompaction === "number"
165
+ ? sourcePolicy.keepAfterCompaction
166
+ : undefined;
167
+ return {
168
+ disabled,
169
+ groups,
170
+ ...(mode ? { mode } : {}),
171
+ ...(keep === undefined ? {} : { keepAfterCompaction: keep }),
172
+ skippedGroups,
173
+ };
146
174
  }
147
175
  /**
148
176
  * The empty policy a Preset resets to.
149
177
  *
150
178
  * Loading mode survives a reset for the same reason it survives a copy:
151
179
  * it is a Preset-level choice, not one of the groups being cleared.
180
+ * The compaction keep limit is the same kind of choice — it says how
181
+ * opened groups age, not which groups exist — so reset keeps it too.
152
182
  * Dropping it flipped an MCP 按需 Preset back to default mode and
153
183
  * re-registered the discovery tool the Preset had switched away from.
154
184
  */
155
185
  function resetPresetPolicy(policy) {
156
186
  const mode = policy && policy.mode === "mcp" ? "mcp" : undefined;
157
- return { disabled: [], groups: [], ...(mode ? { mode } : {}) };
187
+ const keep = policy && typeof policy.keepAfterCompaction === "number"
188
+ ? policy.keepAfterCompaction
189
+ : undefined;
190
+ return {
191
+ disabled: [],
192
+ groups: [],
193
+ ...(mode ? { mode } : {}),
194
+ ...(keep === undefined ? {} : { keepAfterCompaction: keep }),
195
+ };
158
196
  }
159
197
  function settingsPayload(value) {
160
198
  const settings = { presets: {} };
@@ -337,8 +375,11 @@ window.__ModuleLoader__.load({
337
375
  const empty = [];
338
376
  const disabledMembers = [];
339
377
  for (const group of preset.policy.groups || []) {
378
+ const groupDisabled = new Set(group.disabled || []);
340
379
  const members = matchingTools(preset.tools, group.patterns);
341
- if (!members.some((tool) => !disabled.has(tool.name))) empty.push(group.name || "未命名分组");
380
+ if (!members.some((tool) => !disabled.has(tool.name) && !groupDisabled.has(tool.name))) {
381
+ empty.push(group.name || "未命名分组");
382
+ }
342
383
  for (const tool of members) {
343
384
  if (disabled.has(tool.name)) disabledMembers.push({ tool: tool.name, group: group.name || "未命名分组" });
344
385
  const groups = owners.get(tool.name) || [];
@@ -351,16 +392,27 @@ window.__ModuleLoader__.load({
351
392
  }
352
393
  function catalogStats(preset) {
353
394
  const disabled = new Set(preset.policy.disabled || []);
354
- const grouped = groupedNames(preset);
395
+ const groups = preset.policy.groups || [];
355
396
  let ungroupedTools = 0;
356
397
  let onDemandTools = 0;
357
398
  let disabledTools = 0;
358
399
  for (const tool of preset.tools || []) {
359
- if (disabled.has(tool.name)) disabledTools += 1;
360
- else if (grouped.has(tool.name)) onDemandTools += 1;
400
+ if (disabled.has(tool.name)) {
401
+ disabledTools += 1;
402
+ continue;
403
+ }
404
+ const owner = groups.find((group) => matchingTools([tool], group.patterns).length > 0);
405
+ if (!owner) {
406
+ ungroupedTools += 1;
407
+ continue;
408
+ }
409
+ const groupDisabled = Array.isArray(owner.disabled) ? owner.disabled : [];
410
+ const loading = owner.loading === "always" || owner.loading === "off" ? owner.loading : "ondemand";
411
+ if (loading === "off" || groupDisabled.includes(tool.name)) disabledTools += 1;
412
+ else if (loading === "ondemand") onDemandTools += 1;
361
413
  else ungroupedTools += 1;
362
414
  }
363
- return { totalTools: (preset.tools || []).length, groupCount: (preset.policy.groups || []).length, ungroupedTools, onDemandTools, disabledTools };
415
+ return { totalTools: (preset.tools || []).length, groupCount: groups.length, ungroupedTools, onDemandTools, disabledTools };
364
416
  }
365
417
  // Token estimates mirror the Host's `@deepseek-ai/dsh-token-meter/estimate`
366
418
  // (4 characters per token) so the editor updates live while groups are
@@ -404,38 +456,82 @@ window.__ModuleLoader__.load({
404
456
  return estimateToolTokens(tool);
405
457
  }
406
458
  function estimateGroupTokens(group, members) {
407
- if (members.length === 0) return { schema: 0, catalog: 0, net: 0 };
459
+ if (members.length === 0 || groupLoading(group) === "off") return { schema: 0, catalog: 0, net: 0 };
408
460
  let schema = 0;
409
461
  for (const tool of members) {
410
462
  const tokens = toolTokens(tool);
411
463
  if (tokens === null) return null;
412
464
  schema += tokens;
413
465
  }
466
+ if (groupLoading(group) === "always") return { schema, catalog: 0, net: schema };
414
467
  const catalog = Math.ceil(renderCatalogLine(group, members).length / 4);
415
468
  return { schema, catalog, net: schema - catalog };
416
469
  }
470
+ function groupLoading(group) {
471
+ return group.loading === "always" || group.loading === "off" ? group.loading : "ondemand";
472
+ }
473
+ function groupDisabled(group) {
474
+ return Array.isArray(group.disabled) ? group.disabled : [];
475
+ }
476
+ function groupOwners(preset) {
477
+ const owners = new Map();
478
+ for (const group of preset.policy.groups || []) {
479
+ for (const tool of matchingTools(preset.tools, group.patterns)) {
480
+ if (!owners.has(tool.name)) owners.set(tool.name, group);
481
+ }
482
+ }
483
+ return owners;
484
+ }
485
+ function groupOfTool(preset, toolName) {
486
+ const tool = (preset.tools || []).find((item) => item.name === toolName);
487
+ if (!tool) return undefined;
488
+ return (preset.policy.groups || []).find((group) => matchingTools([tool], group.patterns).length > 0);
489
+ }
490
+ function toolDisabled(preset, toolName) {
491
+ if ((preset.policy.disabled || []).includes(toolName)) return true;
492
+ const group = groupOfTool(preset, toolName);
493
+ return group !== undefined
494
+ && (groupLoading(group) === "off" || groupDisabled(group).includes(toolName));
495
+ }
496
+ function activeGroupMembers(group, preset) {
497
+ const disabled = new Set(preset.policy.disabled || []);
498
+ const groupDisabledSet = new Set(groupDisabled(group));
499
+ return matchingTools(preset.tools, group.patterns)
500
+ .filter((tool) => !disabled.has(tool.name) && !groupDisabledSet.has(tool.name));
501
+ }
417
502
  function activeGroupViews(preset) {
418
503
  const disabled = new Set(preset.policy.disabled || []);
419
504
  const claimed = new Set();
420
505
  const active = [];
421
506
  for (const group of preset.policy.groups || []) {
422
- const members = matchingTools(preset.tools, group.patterns)
423
- .filter((tool) => !disabled.has(tool.name) && !claimed.has(tool.name));
507
+ const allMembers = matchingTools(preset.tools, group.patterns);
508
+ const members = activeGroupMembers(group, preset).filter((tool) => !claimed.has(tool.name));
509
+ // Claim disabled members too: first-match ownership must not let a
510
+ // later group resurrect a group-disabled tool in the draft view.
511
+ for (const tool of allMembers) claimed.add(tool.name);
424
512
  if (members.length === 0) continue;
425
- for (const tool of members) claimed.add(tool.name);
426
513
  active.push({ group, members });
427
514
  }
428
515
  return { active, claimed, disabled };
429
516
  }
430
517
  function presetTokenStats(preset) {
431
- const { active, claimed, disabled } = activeGroupViews(preset);
518
+ const { active, disabled } = activeGroupViews(preset);
519
+ const onDemandActive = active.filter((item) => groupLoading(item.group) === "ondemand");
520
+ const owners = groupOwners(preset);
432
521
  const allTools = preset.tools || [];
433
- const alwaysOn = allTools.filter((tool) => !disabled.has(tool.name) && !claimed.has(tool.name));
434
- const onDemand = allTools.filter((tool) => claimed.has(tool.name));
435
- const disabledTools = allTools.filter((tool) => disabled.has(tool.name));
522
+ const alwaysOn = [];
523
+ const onDemand = [];
524
+ const disabledTools = [];
525
+ for (const tool of allTools) {
526
+ const owner = owners.get(tool.name);
527
+ const groupOff = owner !== undefined && (groupLoading(owner) === "off" || groupDisabled(owner).includes(tool.name));
528
+ if (disabled.has(tool.name) || groupOff) disabledTools.push(tool);
529
+ else if (owner !== undefined && groupLoading(owner) === "ondemand") onDemand.push(tool);
530
+ else alwaysOn.push(tool);
531
+ }
436
532
  let onDemandTokens = 0;
437
533
  let onDemandSavings = 0;
438
- for (const item of active) {
534
+ for (const item of onDemandActive) {
439
535
  const tokens = estimateGroupTokens(item.group, item.members);
440
536
  if (tokens) {
441
537
  onDemandTokens += tokens.schema;
@@ -456,7 +552,7 @@ window.__ModuleLoader__.load({
456
552
  alwaysOnTokens: additive ? resident(alwaysOn) : estimateToolsTokens(alwaysOn),
457
553
  onDemandTools: onDemand.length,
458
554
  onDemandTokens,
459
- onDemandGroups: active.length,
555
+ onDemandGroups: onDemandActive.length,
460
556
  onDemandSavings,
461
557
  disabledTools: disabledTools.length,
462
558
  disabledTokens: additive ? resident(disabledTools) : estimateToolsTokens(disabledTools),
@@ -464,8 +560,14 @@ window.__ModuleLoader__.load({
464
560
  }
465
561
  function groupTokenEstimates(preset) {
466
562
  const map = new Map();
467
- for (const item of activeGroupViews(preset).active) {
468
- map.set(item.group._key || item.group.name, estimateGroupTokens(item.group, item.members));
563
+ for (const group of preset.policy.groups || []) {
564
+ if (groupLoading(group) === "off") {
565
+ map.set(group._key || group.name, null);
566
+ continue;
567
+ }
568
+ const members = activeGroupMembers(group, preset);
569
+ const tokens = estimateGroupTokens(group, members);
570
+ map.set(group._key || group.name, tokens);
469
571
  }
470
572
  return map;
471
573
  }
@@ -579,7 +681,7 @@ window.__ModuleLoader__.load({
579
681
  }
580
682
 
581
683
  const TOOL_CATALOG_HELP = [
582
- "关掉后立刻对模型隐藏。按需组默认隐藏,模型要先调用 tool_list 打开所在组才能使用。",
684
+ "关掉后立刻对模型隐藏。分组可设为常驻、按需或关闭:常驻组直接声明,按需组默认隐藏、模型要先调用 tool_list 打开所在组才能使用,关闭组整组不声明;组内成员还可以单独禁用。",
583
685
  "",
584
686
  "tool_list 只在当前 Preset 有有效按需组时才出现(保存后从下一个模型步骤生效),没有有效按需组时模型看不到它;它由插件自动管理,不出现在本页工具目录中,也不能在这里开关或分组。",
585
687
  ].join("\n");
@@ -588,7 +690,7 @@ window.__ModuleLoader__.load({
588
690
  // paragraphs have to name the other tool. The header keeps one help
589
691
  // entry point and picks the wording from the preset loading mode.
590
692
  const TOOL_CATALOG_HELP_MCP = [
591
- "关掉后立刻对模型隐藏。按需组只声明组名与描述,组内工具不单独声明;模型用 tool_find 读取整组用法后可直接按名调用,不需要开组。",
693
+ "关掉后立刻对模型隐藏。分组可设为常驻、按需或关闭:常驻组直接声明,按需组只声明组名与描述、组内工具不单独声明,模型用 tool_find 读取整组用法后可直接按名调用;关闭组整组不声明,组内成员还可以单独禁用。",
592
694
  "",
593
695
  "tool_find 由插件自动注册,不出现在本页目录中,也不能在这里开关或分组;它只在当前 Preset 有有效按需组时出现。",
594
696
  "",
@@ -610,6 +712,22 @@ window.__ModuleLoader__.load({
610
712
  "注意:MCP 按需依赖模型能够直接发出未声明的工具名。部分供应商会限制工具调用名(例如 OpenCode Go),模型只能反复调用 tool_find,无法真正调用组内工具;遇到这种情况请改用「默认」加载模式。",
611
713
  ].join("\n");
612
714
 
715
+ // How a completed compaction treats the groups this Session opened. The
716
+ // default is deliberately "keep everything": compaction must not surprise
717
+ // a reader who never configured this, and only the default mode has an
718
+ // open state to begin with.
719
+ const KEEP_AFTER_COMPACTION_HELP = [
720
+ "默认全部保留:会话压缩不会改变已经打开的按需组。",
721
+ "",
722
+ "填一个数字后,每次会话压缩成功结束(自动压缩或 /compact)时,只保留最近使用的 N 个已打开的按需组,其余重新收起;模型可以再用 tool_list 打开。",
723
+ "",
724
+ "「最近使用」只看上一次压缩之后到这一次压缩之间的区间:组内工具有过成功调用,或组在此期间被打开/重新打开;只是闲置跨过压缩的组会被关闭,若这一区间没有任何组被打开/使用,压缩后可以一个组都不保留。压缩之后才打开的组不受这一次裁剪影响。",
725
+ "",
726
+ "0 表示压缩后全部关闭;留空表示不限制。",
727
+ "",
728
+ "只在「默认」加载模式下生效:MCP 按需模式没有打开状态。随页面「保存」写入。",
729
+ ].join("\n");
730
+
613
731
  // The generation model is a global choice, not a Preset one: auto-naming
614
732
  // and auto-grouping classify tools and print JSON, so a fast model is
615
733
  // usually enough even when the conversation runs a slow reasoning one.
@@ -731,6 +849,31 @@ window.__ModuleLoader__.load({
731
849
  });
732
850
  }
733
851
 
852
+ /**
853
+ * Set how many opened groups survive a completed compaction.
854
+ *
855
+ * A controlled text input rather than `type=number`: the latter
856
+ * reports an empty value for half-typed text ("1e"), which would
857
+ * silently clear the setting while the reader is still typing. Only
858
+ * digits are accepted, and an empty field means the unlimited
859
+ * default.
860
+ */
861
+ function setCompactionKeep(value) {
862
+ const text = String(value || "").trim();
863
+ if (text === "") {
864
+ mutatePreset((target) => {
865
+ delete target.policy.keepAfterCompaction;
866
+ });
867
+ return;
868
+ }
869
+ if (!/^\d+$/.test(text)) return;
870
+ const count = Number(text);
871
+ if (!Number.isSafeInteger(count) || count > 999) return;
872
+ mutatePreset((target) => {
873
+ target.policy.keepAfterCompaction = count;
874
+ });
875
+ }
876
+
734
877
  /**
735
878
  * Choose the shared generation model, or `null` to let the plugin pick.
736
879
  *
@@ -835,6 +978,8 @@ window.__ModuleLoader__.load({
835
978
  name: nextGroupName(preset.policy.groups),
836
979
  description: "",
837
980
  patterns: [],
981
+ loading: "ondemand",
982
+ disabled: [],
838
983
  query: "",
839
984
  generating: false,
840
985
  generateError: "",
@@ -843,7 +988,7 @@ window.__ModuleLoader__.load({
843
988
 
844
989
  function resetPresetConfig() {
845
990
  if (!hasPresetConfig) return;
846
- if (!window.confirm("确定重置当前 Preset 的配置吗?这会清空它的全部按需组和已关闭工具,加载模式保持不变;只修改页面草稿,保存后生效。")) return;
991
+ if (!window.confirm("确定重置当前 Preset 的配置吗?这会清空它的全部组和已关闭工具,加载模式与「压缩后保留」保持不变;只修改页面草稿,保存后生效。")) return;
847
992
  mutatePreset((target) => {
848
993
  target.policy = resetPresetPolicy(target.policy);
849
994
  });
@@ -926,12 +1071,35 @@ window.__ModuleLoader__.load({
926
1071
  name: group.name || "",
927
1072
  description: group.description || "",
928
1073
  patterns: members,
1074
+ loading: groupLoading(group),
1075
+ disabled: [...groupDisabled(group)],
929
1076
  query: "",
930
1077
  generating: false,
931
1078
  generateError: "",
932
1079
  });
933
1080
  }
934
1081
 
1082
+ function setGroupLoading(key, value) {
1083
+ mutatePreset((target) => {
1084
+ const group = (target.policy.groups || []).find((item) => item._key === key);
1085
+ if (!group) return;
1086
+ if (value === "always" || value === "off") group.loading = value;
1087
+ else delete group.loading;
1088
+ });
1089
+ }
1090
+
1091
+ function setGroupMemberDisabled(key, toolName, disabled) {
1092
+ mutatePreset((target) => {
1093
+ const group = (target.policy.groups || []).find((item) => item._key === key);
1094
+ if (!group) return;
1095
+ const next = new Set(group.disabled || []);
1096
+ if (disabled) next.add(toolName);
1097
+ else next.delete(toolName);
1098
+ if (next.size === 0) delete group.disabled;
1099
+ else group.disabled = [...next];
1100
+ });
1101
+ }
1102
+
935
1103
  function deleteGroup(key) {
936
1104
  mutatePreset((target) => {
937
1105
  target.policy.groups = (target.policy.groups || []).filter((group) => group._key !== key);
@@ -944,6 +1112,16 @@ window.__ModuleLoader__.load({
944
1112
  setModal((current) => current ? { ...current, ...patch } : current);
945
1113
  }
946
1114
 
1115
+ function toggleModalDisabled(toolName) {
1116
+ setModal((current) => {
1117
+ if (!current) return current;
1118
+ const next = new Set(current.disabled || []);
1119
+ if (next.has(toolName)) next.delete(toolName);
1120
+ else next.add(toolName);
1121
+ return { ...current, disabled: [...next], generateError: "" };
1122
+ });
1123
+ }
1124
+
947
1125
  function patchAiConfirm(patch) {
948
1126
  setAiConfirm((current) => current ? { ...current, ...patch } : current);
949
1127
  }
@@ -1000,7 +1178,11 @@ window.__ModuleLoader__.load({
1000
1178
  const description = String(modal.description || "").trim();
1001
1179
  const eligible = new Set(eligibleGroupTools(preset, modal.key).map((tool) => tool.name));
1002
1180
  const patterns = [...new Set(modal.patterns || [])].filter((toolName) => eligible.has(toolName));
1181
+ const selected = new Set(patterns);
1182
+ const groupDisabled = [...new Set(modal.disabled || [])].filter((toolName) => selected.has(toolName));
1183
+ const loading = modal.loading === "always" || modal.loading === "off" ? modal.loading : undefined;
1003
1184
  if (!name || patterns.length === 0) return;
1185
+ if (patterns.every((toolName) => groupDisabled.includes(toolName))) return;
1004
1186
  const created = modal.mode === "create";
1005
1187
  const key = modal.key;
1006
1188
  mutatePreset((target) => {
@@ -1008,7 +1190,14 @@ window.__ModuleLoader__.load({
1008
1190
  const clash = target.policy.groups.some((item) => item._key !== key && String(item.name || "").toLowerCase() === name.toLowerCase());
1009
1191
  if (clash) return;
1010
1192
  if (created) {
1011
- target.policy.groups.push({ _key: key, name, description, patterns });
1193
+ target.policy.groups.push({
1194
+ _key: key,
1195
+ name,
1196
+ description,
1197
+ patterns,
1198
+ ...(loading === undefined ? {} : { loading }),
1199
+ ...(groupDisabled.length === 0 ? {} : { disabled: groupDisabled }),
1200
+ });
1012
1201
  return;
1013
1202
  }
1014
1203
  const group = target.policy.groups.find((item) => item._key === key);
@@ -1016,6 +1205,10 @@ window.__ModuleLoader__.load({
1016
1205
  group.name = name;
1017
1206
  group.description = description;
1018
1207
  group.patterns = patterns;
1208
+ if (loading === undefined) delete group.loading;
1209
+ else group.loading = loading;
1210
+ if (groupDisabled.length === 0) delete group.disabled;
1211
+ else group.disabled = groupDisabled;
1019
1212
  });
1020
1213
  setModal(null);
1021
1214
  if (created) setExpanded((current) => ({ ...current, [key]: true }));
@@ -1124,13 +1317,19 @@ window.__ModuleLoader__.load({
1124
1317
  function save() {
1125
1318
  for (const item of draft.presets || []) {
1126
1319
  if (item.broken) continue;
1127
- // Groups are inert in mcp mode, so a stale/empty group must not
1128
- // block saving the mode switch.
1129
- if (item.policy.mode === "mcp") continue;
1130
- const issues = groupDraftIssues(item);
1320
+ // MCP mode keeps groups for tool_find; stale default/ondemand
1321
+ // groups should not block saving, but explicit always/off groups
1322
+ // still carry loading semantics and must be valid.
1323
+ let checkItem = item;
1324
+ if (item.policy.mode === "mcp") {
1325
+ const groups = (item.policy.groups || []).filter((group) => group.loading === "always" || group.loading === "off");
1326
+ if (groups.length === 0) continue;
1327
+ checkItem = { ...item, policy: { ...item.policy, groups } };
1328
+ }
1329
+ const issues = groupDraftIssues(checkItem);
1131
1330
  if (issues.disabledMembers.length) {
1132
1331
  const issue = issues.disabledMembers[0];
1133
- setError("Preset “" + item.id + "”中的已关闭工具“" + issue.tool + "”不能加入分组“" + issue.group + "”。请从分组移除或重新开启该工具。");
1332
+ setError("Preset “" + item.id + "”中的已关闭工具“" + issue.tool + "”落在分组“" + issue.group + "”的匹配范围内。Preset 级已关闭工具不能与分组重叠,请从分组移除该工具或取消它的关闭状态。");
1134
1333
  setMessage("");
1135
1334
  return;
1136
1335
  }
@@ -1203,28 +1402,48 @@ window.__ModuleLoader__.load({
1203
1402
  const title = String(group.name || "").toLowerCase();
1204
1403
  const description = String(group.description || "").toLowerCase();
1205
1404
  const members = matchingTools(preset.tools, group.patterns);
1405
+ const loading = groupLoading(group);
1406
+ if (filter === "cold" && loading !== "ondemand") return false;
1407
+ if (filter === "live" && loading !== "always") return false;
1408
+ if (filter === "disabled" && loading !== "off") return false;
1206
1409
  if (!needle) return true;
1207
1410
  return title.includes(needle) || description.includes(needle) || members.some((tool) => matchesQuery(tool, needle));
1208
1411
  });
1209
- const showGroups = filter === "all" || filter === "cold";
1412
+ const showGroups = filter === "all" || filter === "cold" || filter === "live" || filter === "disabled";
1210
1413
  const visibleTools = (preset.tools || []).filter((tool) => {
1211
1414
  if (!matchesQuery(tool, needle)) return false;
1212
1415
  if (filter === "cold") return false;
1213
- if (filter === "disabled") return disabled.has(tool.name);
1214
- if (filter === "live") return !disabled.has(tool.name) && !grouped.has(tool.name);
1416
+ if (filter === "disabled") {
1417
+ const owner = groupOfTool(preset, tool.name);
1418
+ // A closed group is already represented by its group card and
1419
+ // its “已关闭 N 个工具” summary; don't repeat every member here.
1420
+ if (owner && groupLoading(owner) === "off") return false;
1421
+ return toolDisabled(preset, tool.name);
1422
+ }
1423
+ if (filter === "live") return !toolDisabled(preset, tool.name) && !grouped.has(tool.name);
1215
1424
  return !grouped.has(tool.name);
1216
1425
  });
1217
1426
  const orphans = (preset.policy.disabled || []).filter((name) => !(preset.tools || []).some((tool) => tool.name === name));
1218
1427
  const modalNeedle = String(modal && modal.query || "").trim().toLowerCase();
1219
1428
  const modalSelected = new Set(modal ? modal.patterns || [] : []);
1429
+ const modalGroupDisabled = new Set(modal ? modal.disabled || [] : []);
1430
+ const modalLoading = modal && (modal.loading === "always" || modal.loading === "off") ? modal.loading : "ondemand";
1431
+ const modalHint = modalLoading === "always"
1432
+ ? "勾选要常驻的工具;取消勾选或点「禁用」都不会向模型声明。"
1433
+ : modalLoading === "off"
1434
+ ? "整组关闭时不声明;切回「常驻 / 按需」后按这里的勾选恢复。"
1435
+ : "筛选后勾选要收进该组的工具。确定后会显示在按需里。";
1220
1436
  const modalEligibleTools = modal ? eligibleGroupTools(preset, modal.key) : [];
1221
1437
  const modalEligibleNames = new Set(modalEligibleTools.map((tool) => tool.name));
1222
1438
  const pickerTools = modal ? (preset.tools || []).filter((tool) =>
1223
1439
  (modalEligibleNames.has(tool.name) || modalSelected.has(tool.name)) && matchesQuery(tool, modalNeedle)) : [];
1224
1440
  const visibleSelected = pickerTools.filter((tool) => modalSelected.has(tool.name)).map((tool) => tool.name);
1225
1441
  const modalMatchedTools = modal ? matchingTools(preset.tools, modal.patterns || []).filter((tool) => modalEligibleNames.has(tool.name)) : [];
1442
+ const modalEnabledCount = modalMatchedTools.filter((tool) => (
1443
+ !disabled.has(tool.name) && !modalGroupDisabled.has(tool.name)
1444
+ )).length;
1226
1445
  const modalInvalidSelected = modal ? [...modalSelected].filter((name) => !modalEligibleNames.has(name)) : [];
1227
- const canConfirm = !!(modal && String(modal.name || "").trim() && modalMatchedTools.length > 0 && modalInvalidSelected.length === 0);
1446
+ const canConfirm = !!(modal && String(modal.name || "").trim() && modalEnabledCount > 0 && modalInvalidSelected.length === 0);
1228
1447
  const listedGroups = showGroups ? visibleGroups : [];
1229
1448
  const aiIsAutoGroup = !!(aiConfirm && aiConfirm.kind === "auto-group");
1230
1449
  const aiNeedle = String(aiConfirm && aiConfirm.query || "").trim().toLowerCase();
@@ -1256,31 +1475,69 @@ window.__ModuleLoader__.load({
1256
1475
  patchAiConfirm(patch);
1257
1476
  }
1258
1477
 
1478
+ function groupModeOptions() {
1479
+ return [
1480
+ h("option", { value: "always", style: { color: "#22c55e" } }, "常驻"),
1481
+ h("option", { value: "ondemand", style: { color: "#818cf8" } }, "按需"),
1482
+ h("option", { value: "off", style: { color: "#f87171" } }, "关闭"),
1483
+ ];
1484
+ }
1485
+
1259
1486
  function renderGroup(group) {
1260
1487
  const key = group._key;
1261
1488
  const open = !!expanded[key];
1262
1489
  const members = matchingTools(preset.tools, group.patterns);
1263
1490
  const estimate = groupTokens.get(key) || null;
1264
1491
  const title = (group.name || "").trim() || "未命名分组";
1492
+ const loading = groupLoading(group);
1493
+ const groupDisabledSet = new Set(groupDisabled(group));
1494
+ const memberStatus = (tool) => {
1495
+ if (disabled.has(tool.name) || loading === "off" || groupDisabledSet.has(tool.name)) return { label: "关闭", className: "off" };
1496
+ return loading === "always" ? { label: "常驻", className: "live" } : { label: "按需", className: "cold" };
1497
+ };
1265
1498
  return h("div", { className: "tm_group", key: key },
1266
1499
  h("div", { className: "tm_group_bar" },
1267
1500
  h("button", { className: "tm_chevron", title: open ? "收起" : "展开", onClick: () => setExpanded((current) => ({ ...current, [key]: !current[key] })) }, open ? "▾" : "▸"),
1268
1501
  h("button", { className: "tm_group_main", onClick: () => setExpanded((current) => ({ ...current, [key]: !current[key] })) },
1269
- h("div", { className: "tm_group_title" }, title, h("span", { className: "tm_badge cold" }, "按需")),
1502
+ h("div", { className: "tm_group_title" }, title),
1270
1503
  h("div", { className: "tm_group_sub" }, group.description || "没有描述"),
1271
1504
  ),
1272
- estimate && estimate.schema > 0 ? tokenBadge(estimate.net, "估算(" + unitNote + "):组内工具 " + formatTokenAmount(estimate.schema) + " tok · 关闭时目录行 " + formatTokenAmount(estimate.catalog) + " tok · 收起净省 " + formatTokenAmount(estimate.net) + " tok/轮") : null,
1273
- h("span", { className: "tm_group_count" }, members.length + " 个工具"),
1505
+ h("select", {
1506
+ className: "tm_group_mode " + loading,
1507
+ value: loading,
1508
+ title: preset.policy.mode === "mcp"
1509
+ ? "常驻=直接声明;按需=tool_find 读取后按名调用;关闭=整组不声明"
1510
+ : "常驻=直接声明;按需=tool_list 打开后使用;关闭=整组不声明",
1511
+ onChange: (event) => setGroupLoading(key, event.target.value),
1512
+ }, groupModeOptions()),
1513
+ loading === "off"
1514
+ ? h("span", { className: "tm_muted" }, "已关闭 " + members.length + " 个工具")
1515
+ : estimate && estimate.schema > 0
1516
+ ? tokenBadge(estimate.net, loading === "always"
1517
+ ? "估算(" + unitNote + "):组内工具常驻 " + formatTokenAmount(estimate.schema) + " tok/轮"
1518
+ : "估算(" + unitNote + "):组内工具 " + formatTokenAmount(estimate.schema) + " tok · 关闭时目录行 " + formatTokenAmount(estimate.catalog) + " tok · 收起净省 " + formatTokenAmount(estimate.net) + " tok/轮")
1519
+ : null,
1520
+ loading === "off" ? null : h("span", { className: "tm_group_count" }, members.length + " 个工具"),
1274
1521
  h("button", { className: "tm_btn", onClick: () => openEdit(group) }, "编辑"),
1275
1522
  h("button", { className: "tm_btn", onClick: () => deleteGroup(key) }, "删除"),
1276
1523
  ),
1277
1524
  open ? h("div", { className: "tm_group_body" }, members.length
1278
1525
  ? members.map((tool) => {
1279
1526
  const tokens = toolTokens(tool);
1280
- const isDisabled = disabled.has(tool.name);
1281
- return h("div", { className: "tm_member", key: tool.name },
1282
- h("div", { className: "tm_tool_name" }, tool.name, isDisabled ? h("span", { className: "tm_badge off" }, "关闭") : null, tokenBadge(tokens, "估算(" + unitNote + "):" + (isDisabled ? "已关闭,开启后常驻 " : "单工具常驻 ") + formatTokenAmount(tokens) + " tok/轮")),
1283
- h("div", { className: "tm_tool_desc", title: tool.description }, tool.description),
1527
+ const status = memberStatus(tool);
1528
+ const memberTag = loading === "off" ? "div" : "label";
1529
+ return h(memberTag, { className: "tm_member", key: tool.name },
1530
+ loading === "off" ? null : h("input", {
1531
+ type: "checkbox",
1532
+ checked: !groupDisabledSet.has(tool.name) && !disabled.has(tool.name),
1533
+ disabled: disabled.has(tool.name),
1534
+ title: disabled.has(tool.name) ? "该工具由 Preset 级设置关闭" : "取消勾选=在本组内禁用此工具",
1535
+ onChange: (event) => setGroupMemberDisabled(key, tool.name, !event.target.checked),
1536
+ }),
1537
+ h("span", null,
1538
+ h("div", { className: "tm_tool_name" }, tool.name, h("span", { className: "tm_badge " + status.className }, status.label), tokenBadge(tokens, "估算(" + unitNote + "):" + formatTokenAmount(tokens) + " tok/轮")),
1539
+ h("div", { className: "tm_tool_desc", title: tool.description }, tool.description),
1540
+ ),
1284
1541
  );
1285
1542
  })
1286
1543
  : h("div", { className: "tm_error" }, "无效空组:请编辑并选择至少一个可用工具,或删除该组。"),
@@ -1289,13 +1546,29 @@ window.__ModuleLoader__.load({
1289
1546
  }
1290
1547
 
1291
1548
  function renderTool(tool) {
1292
- const enabled = !disabled.has(tool.name);
1549
+ const owner = groupOfTool(preset, tool.name);
1550
+ const enabled = !toolDisabled(preset, tool.name);
1293
1551
  const tokens = toolTokens(tool);
1552
+ const groupName = owner ? (String(owner.name || "").trim() || "未命名分组") : "";
1553
+ const ownerLoading = owner ? groupLoading(owner) : "ondemand";
1294
1554
  return h("label", { className: "tm_tool", key: tool.name },
1295
- h("input", { type: "checkbox", checked: enabled, onChange: () => toggleTool(tool.name) }),
1555
+ h("input", {
1556
+ type: "checkbox",
1557
+ checked: enabled,
1558
+ disabled: !!owner,
1559
+ title: owner ? "该工具由分组「" + groupName + "」控制,请在分组中调整" : undefined,
1560
+ onChange: () => { if (!owner) toggleTool(tool.name); },
1561
+ }),
1296
1562
  h("span", null,
1297
1563
  h("div", { className: "tm_tool_name" }, tool.name,
1298
- !enabled ? h("span", { className: "tm_badge off" }, "关闭") : h("span", { className: "tm_badge live" }, "常开"),
1564
+ enabled ? h("span", { className: "tm_badge live" }, "常驻") : h("span", { className: "tm_badge off" }, "关闭"),
1565
+ owner && filter === "disabled"
1566
+ ? h("span", null,
1567
+ h("span", { className: "tm_group_ref" }, "所属组:"),
1568
+ h("span", { className: "tm_badge " + (ownerLoading === "always" ? "live" : "cold") }, ownerLoading === "always" ? "常驻" : "按需"),
1569
+ h("span", { className: "tm_group_name" }, groupName),
1570
+ )
1571
+ : null,
1299
1572
  ),
1300
1573
  h("div", { className: "tm_tool_desc", title: tool.description }, tool.description),
1301
1574
  ),
@@ -1579,13 +1852,13 @@ window.__ModuleLoader__.load({
1579
1852
  ) : null;
1580
1853
 
1581
1854
  const dialog = modal && typeof document !== "undefined" ? reactDom.createPortal(
1582
- h("div", { className: "tm_modal", role: "dialog", "aria-modal": "true", "aria-label": modal.mode === "create" ? "新建按需组" : "编辑按需组" },
1855
+ h("div", { className: "tm_modal", role: "dialog", "aria-modal": "true", "aria-label": modal.mode === "create" ? "新建分组" : "编辑分组" },
1583
1856
  h("div", { className: "tm_modal_mask", onMouseDown: () => setModal(null) }),
1584
1857
  h("div", { className: "tm_modal_card" },
1585
1858
  h("div", { className: "tm_modal_head" },
1586
1859
  h("div", null,
1587
- h("h3", null, modal.mode === "create" ? "新建按需组" : "编辑按需组"),
1588
- h("div", { className: "tm_muted" }, "筛选后勾选要收进该组的工具。确定后会显示在按需里。"),
1860
+ h("h3", null, modal.mode === "create" ? "新建分组" : "编辑分组"),
1861
+ h("div", { className: "tm_muted" }, modalHint),
1589
1862
  ),
1590
1863
  h("button", { className: "tm_btn", onClick: () => setModal(null) }, "关闭"),
1591
1864
  ),
@@ -1594,6 +1867,14 @@ window.__ModuleLoader__.load({
1594
1867
  h("input", { className: "tm_input", value: modal.name, placeholder: "名称(模型用这个打开)", onChange: (e) => patchModal({ name: e.target.value, generateError: "" }) }),
1595
1868
  h("button", { className: "tm_btn", disabled: modal.generating || modalMatchedTools.length === 0 || modalInvalidSelected.length > 0, onClick: generateGroupDetails }, modal.generating ? "生成中…" : "自动生成名称和描述"),
1596
1869
  ),
1870
+ h("div", { className: "tm_row" },
1871
+ h("span", { className: "tm_muted" }, "加载"),
1872
+ h("select", {
1873
+ className: "tm_group_mode " + modalLoading,
1874
+ value: modalLoading,
1875
+ onChange: (e) => patchModal({ loading: e.target.value }),
1876
+ }, groupModeOptions()),
1877
+ ),
1597
1878
  renderGenerationPicker(),
1598
1879
  modal.generateError ? h("div", { className: "tm_error" }, modal.generateError) : null,
1599
1880
  modal.generateNotice ? h("div", { className: "tm_notice" }, modal.generateNotice) : null,
@@ -1605,9 +1886,11 @@ window.__ModuleLoader__.load({
1605
1886
  ),
1606
1887
  modalInvalidSelected.length
1607
1888
  ? h("div", { className: "tm_error" }, "以下工具已关闭或属于其他分组,不能加入当前组:" + modalInvalidSelected.join(", ") + "。请取消选择。")
1608
- : h("div", { className: modalMatchedTools.length ? "tm_muted" : "tm_error" }, modalMatchedTools.length
1609
- ? "已选 " + modalMatchedTools.length + " 个工具。自动生成会调用" + generationLabel + ",结果仍可手动修改。"
1610
- : "每个按需组至少需要一个未关闭且未被其他组占用的工具。"),
1889
+ : h("div", { className: modalEnabledCount > 0 ? "tm_muted" : "tm_error" }, modalMatchedTools.length
1890
+ ? modalLoading === "off"
1891
+ ? "已选 " + modalMatchedTools.length + " 个工具。自动生成会调用" + generationLabel + ",结果仍可手动修改。"
1892
+ : "已选 " + modalMatchedTools.length + " 个工具,其中 " + (modalMatchedTools.length - modalEnabledCount) + " 个禁用。自动生成会调用" + generationLabel + ",结果仍可手动修改。"
1893
+ : "每组至少需要一个未禁用的工具。"),
1611
1894
  pickerTools.length
1612
1895
  ? h("div", { className: "tm_picker" }, pickerTools.map((tool) => h("label", { className: "tm_pick", key: tool.name },
1613
1896
  h("input", {
@@ -1616,16 +1899,33 @@ window.__ModuleLoader__.load({
1616
1899
  disabled: !modalEligibleNames.has(tool.name) && !modalSelected.has(tool.name),
1617
1900
  onChange: (e) => {
1618
1901
  const next = new Set(modal.patterns || []);
1619
- if (e.target.checked && modalEligibleNames.has(tool.name)) next.add(tool.name); else next.delete(tool.name);
1902
+ if (e.target.checked && modalEligibleNames.has(tool.name)) next.add(tool.name);
1903
+ else {
1904
+ next.delete(tool.name);
1905
+ const disabledNext = new Set(modal.disabled || []);
1906
+ disabledNext.delete(tool.name);
1907
+ patchModal({ patterns: [...next], disabled: [...disabledNext], generateError: "" });
1908
+ return;
1909
+ }
1620
1910
  patchModal({ patterns: [...next], generateError: "" });
1621
1911
  },
1622
1912
  }),
1623
1913
  h("span", null,
1624
1914
  h("div", { className: "tm_tool_name" }, tool.name,
1625
1915
  disabled.has(tool.name) ? h("span", { className: "tm_badge off" }, "已关闭")
1916
+ : modalGroupDisabled.has(tool.name) ? h("span", { className: "tm_badge off" }, "禁用")
1626
1917
  : (!modalEligibleNames.has(tool.name) ? h("span", { className: "tm_badge off" }, "其他组") : null)),
1627
1918
  h("div", { className: "tm_tool_desc", title: tool.description }, tool.description),
1628
1919
  ),
1920
+ modalSelected.has(tool.name) && modalLoading !== "off"
1921
+ ? h("button", {
1922
+ type: "button",
1923
+ className: "tm_btn",
1924
+ style: { marginLeft: "auto", flex: "0 0 auto", fontSize: "11px", padding: "3px 7px" },
1925
+ disabled: disabled.has(tool.name),
1926
+ onClick: (e) => { e.preventDefault(); toggleModalDisabled(tool.name); },
1927
+ }, modalGroupDisabled.has(tool.name) ? "取消禁用" : "禁用")
1928
+ : null,
1629
1929
  )))
1630
1930
  : h("div", { className: "tm_muted" }, preset.tools.length ? "没有符合筛选的工具。" : "这个 Preset 当前没有可见工具。"),
1631
1931
  ),
@@ -1652,7 +1952,7 @@ window.__ModuleLoader__.load({
1652
1952
  h("div", { style: { padding: "10px 14px" } }, group.tools.map((name) => h("div", { className: "tm_member tm_tool_name", key: name }, name))),
1653
1953
  )),
1654
1954
  autoPreview.notice ? h("div", { className: "tm_notice" }, autoPreview.notice) : null,
1655
- autoPreview.ungrouped.length ? h("div", { className: "tm_notice" }, "保持常开、未归组:", h("span", { className: "tm_code" }, autoPreview.ungrouped.join(", "))) : null,
1955
+ autoPreview.ungrouped.length ? h("div", { className: "tm_notice" }, "保持常驻、未归组:", h("span", { className: "tm_code" }, autoPreview.ungrouped.join(", "))) : null,
1656
1956
  ),
1657
1957
  h("div", { className: "tm_modal_foot" },
1658
1958
  h("button", { className: "tm_btn", onClick: () => setAutoPreview(null) }, "取消"),
@@ -1670,7 +1970,7 @@ window.__ModuleLoader__.load({
1670
1970
  h("div", { className: "tm_modal_head" },
1671
1971
  h("div", null,
1672
1972
  h("h3", null, "复制当前 Preset 配置"),
1673
- h("div", { className: "tm_muted" }, "将当前 Preset 的全部按需组和已关闭工具复制到勾选的 Preset;加载模式不跟随复制,各 Preset 保持自己的「默认 / MCP 按需」设置;只改页面草稿,保存后生效。"),
1973
+ h("div", { className: "tm_muted" }, "将当前 Preset 的全部组和已关闭工具复制到勾选的 Preset;每组加载方式与组内禁用会随组一起复制,加载模式不跟随复制,「压缩后保留最近使用的按需组」会随分组一起复制;只改页面草稿,保存后生效。"),
1674
1974
  ),
1675
1975
  h("button", { className: "tm_btn", onClick: () => setCopyDialog(null) }, "关闭"),
1676
1976
  ),
@@ -1678,9 +1978,9 @@ window.__ModuleLoader__.load({
1678
1978
  h("div", { className: "tm_notice" },
1679
1979
  "源 Preset「" + (copySource.name || copySource.id) + "」(加载模式:"
1680
1980
  + (copySource.policy.mode === "mcp" ? "MCP 按需" : "默认") + "):"
1681
- + (copySource.policy.groups || []).length + " 个按需组、"
1981
+ + (copySource.policy.groups || []).length + " 个组、"
1682
1982
  + (copySource.policy.disabled || []).length + " 个已关闭工具。"
1683
- + "勾选的目标 Preset 现有分组和关闭项将被整份覆盖,加载模式不变。",
1983
+ + "勾选的目标 Preset 现有分组和关闭项将被整份覆盖,加载模式不变,「压缩后保留」跟随源 Preset。",
1684
1984
  ),
1685
1985
  h("div", { className: "tm_row" },
1686
1986
  h("button", {
@@ -1704,6 +2004,9 @@ window.__ModuleLoader__.load({
1704
2004
  ? ";跳过 " + preview.skippedGroups.length + " 个组:" + preview.skippedGroups.join("、")
1705
2005
  : "")
1706
2006
  + ";加载模式保持「" + targetMode + "」"
2007
+ + ";压缩后保留 " + (typeof preview.keepAfterCompaction === "number"
2008
+ ? preview.keepAfterCompaction + " 个"
2009
+ : "全部")
1707
2010
  + ((target.tools || []).length === 0 ? ";目标没有可见工具,复制后配置为空" : "");
1708
2011
  return h("label", { className: "tm_pick", key: target.id },
1709
2012
  h("input", {
@@ -1741,7 +2044,7 @@ window.__ModuleLoader__.load({
1741
2044
 
1742
2045
  return h("div", { className: "tm_root" },
1743
2046
  h("div", { className: "tm_head" },
1744
- h("div", null, h("h2", null, "Agent 工具管理"), h("div", { className: "tm_muted" }, "按每个 Agent 预设开关工具。点「手动分组」把不常用的收进按需组:平时模型看不到,需要时先查目录再打开。")),
2047
+ h("div", null, h("h2", null, "Agent 工具管理"), h("div", { className: "tm_muted" }, "按每个 Agent 预设开关工具。点「手动分组」把工具整理成常驻、按需或关闭组:常驻组直接可用,按需组平时模型看不到、需要时先查目录再打开,关闭组整组不声明。")),
1745
2048
  h("div", { className: "tm_row" },
1746
2049
  h("button", { className: "tm_btn", onClick: reload, disabled: busy || autoGrouping }, "刷新"),
1747
2050
  h("button", {
@@ -1780,11 +2083,32 @@ window.__ModuleLoader__.load({
1780
2083
  ? h("span", { className: "tm_badge ptc", title: "本 Preset 走 PTC:工具以一份生成的 TypeScript SDK 注入系统提示词,token 按该文本 ÷4 估算(含 run_code 调用声明、PTC 指令段与 SDK 脚手架)" }, "PTC")
1781
2084
  : h("span", { className: "tm_badge native", title: "本 Preset 走原生 tool-calling:工具以 JSON schema 随请求发送,token 按 JSON ÷4 估算" }, "原生"),
1782
2085
  ),
1783
- h("div", { className: "tm_row", style: { padding: "0 14px 10px" } },
2086
+ h("div", { className: "tm_row", style: { padding: "0 14px" } },
1784
2087
  h("span", { className: "tm_muted", style: { position: "relative" } }, "加载模式", h("span", { className: "tm_help", tabIndex: 0, "aria-label": "加载模式说明", "data-tip": LOADING_MODE_HELP }, "?")),
1785
2088
  h("button", { className: "tm_chip" + (mcpMode ? "" : " active"), onClick: () => setMode("groups") }, "默认"),
1786
2089
  h("button", { className: "tm_chip" + (mcpMode ? " active" : ""), onClick: () => setMode("mcp") }, "MCP 按需"),
1787
2090
  ),
2091
+ mcpMode ? null : h("div", { className: "tm_row", style: { padding: "0 14px" } },
2092
+ h("span", { className: "tm_muted", style: { position: "relative" } },
2093
+ "压缩后保留最近使用的按需组",
2094
+ h("span", { className: "tm_help", tabIndex: 0, "aria-label": "压缩后保留说明", "data-tip": KEEP_AFTER_COMPACTION_HELP }, "?"),
2095
+ ),
2096
+ h("input", {
2097
+ className: "tm_input",
2098
+ style: { maxWidth: "120px", flex: "0 0 120px" },
2099
+ type: "text",
2100
+ inputMode: "numeric",
2101
+ "aria-label": "压缩后保留最近使用的按需组数量",
2102
+ value: typeof preset.policy.keepAfterCompaction === "number"
2103
+ ? String(preset.policy.keepAfterCompaction)
2104
+ : "",
2105
+ placeholder: "全部",
2106
+ disabled: autoGrouping,
2107
+ title: "留空=全部保留;0=压缩后全部关闭;N=只保留最近使用的 N 个",
2108
+ onChange: (event) => setCompactionKeep(event.target.value),
2109
+ }),
2110
+ h("span", { className: "tm_muted" }, "留空=全部保留"),
2111
+ ),
1788
2112
  preset.broken ? h("div", { className: "tm_error" }, preset.broken) : null,
1789
2113
  autoGroupError ? h("div", { className: "tm_error" }, autoGroupError) : null,
1790
2114
  aiNotice ? h("div", { className: "tm_notice" }, aiNotice) : null,
@@ -1801,7 +2125,7 @@ window.__ModuleLoader__.load({
1801
2125
  onClick: resetPresetConfig,
1802
2126
  disabled: autoGrouping || busy || !draft.writable || !hasPresetConfig,
1803
2127
  title: hasPresetConfig
1804
- ? "清空当前 Preset 的全部按需组和已关闭工具(加载模式保持不变)"
2128
+ ? "清空当前 Preset 的全部组和已关闭工具(加载模式与压缩后保留保持不变)"
1805
2129
  : "当前 Preset 没有可重置的配置",
1806
2130
  }, "重置配置"),
1807
2131
  h("button", {
@@ -1809,7 +2133,7 @@ window.__ModuleLoader__.load({
1809
2133
  onClick: openCopyDialog,
1810
2134
  disabled: autoGrouping || busy || !draft.writable || !hasPresetConfig || copyTargetCount === 0,
1811
2135
  title: hasPresetConfig
1812
- ? "把当前 Preset 的分组和已关闭工具复制到其他 Preset(加载模式不复制)"
2136
+ ? "把当前 Preset 的分组和已关闭工具复制到其他 Preset(加载模式不复制;压缩后保留会复制)"
1813
2137
  : "当前 Preset 没有可复制的分组或已关闭工具",
1814
2138
  }, "复制配置"),
1815
2139
  ),
@@ -1817,8 +2141,8 @@ window.__ModuleLoader__.load({
1817
2141
  h("div", { className: "tm_stats" },
1818
2142
  [
1819
2143
  ["工具总量", stats.totalTools, formatTokenAmount(tokenStats.totalTokens) + " tok", "全部 " + stats.totalTools + " 个工具合计 " + formatTokenAmount(tokenStats.totalTokens) + " tok/轮(" + unitNote + (isPtc ? ",含平台常数 " + formatTokenAmount(constantTokens) + " tok" : "") + ";全部常驻时)"],
1820
- ["分组数量", stats.groupCount, "省 " + formatTokenAmount(tokenStats.onDemandSavings) + " tok", "全部 " + tokenStats.onDemandGroups + " 个按需组收起:组内工具合计 " + formatTokenAmount(tokenStats.onDemandTokens) + " tok − 关闭态目录行 " + formatTokenAmount(onDemandCatalogTokens) + " tok = 净省 " + formatTokenAmount(tokenStats.onDemandSavings) + " tok/轮"],
1821
- ["未分组工具", stats.ungroupedTools, formatTokenAmount(tokenStats.alwaysOnTokens) + " tok", "未分组且未关闭的 " + tokenStats.alwaysOnTools + " 个工具每轮常驻 " + formatTokenAmount(tokenStats.alwaysOnTokens) + " tok"],
2144
+ ["分组数量", stats.groupCount, "省 " + formatTokenAmount(tokenStats.onDemandSavings) + " tok", "其中 " + tokenStats.onDemandGroups + " 个按需组收起:组内工具合计 " + formatTokenAmount(tokenStats.onDemandTokens) + " tok − 关闭态目录行 " + formatTokenAmount(onDemandCatalogTokens) + " tok = 净省 " + formatTokenAmount(tokenStats.onDemandSavings) + " tok/轮"],
2145
+ ["常驻工具", stats.ungroupedTools, formatTokenAmount(tokenStats.alwaysOnTokens) + " tok", "未分组或常驻组内且未关闭的 " + tokenStats.alwaysOnTools + " 个工具每轮常驻 " + formatTokenAmount(tokenStats.alwaysOnTokens) + " tok"],
1822
2146
  ["按需工具", stats.onDemandTools, formatTokenAmount(tokenStats.onDemandTokens) + " tok", "按需组内 " + tokenStats.onDemandTools + " 个工具合计 " + formatTokenAmount(tokenStats.onDemandTokens) + " tok;收起后仍会注入目录行 " + formatTokenAmount(onDemandCatalogTokens) + " tok,实际净省见「分组数量」卡"],
1823
2147
  ["已关闭工具", stats.disabledTools, formatTokenAmount(tokenStats.disabledTokens) + " tok", "已关闭的 " + tokenStats.disabledTools + " 个工具关闭可省 " + formatTokenAmount(tokenStats.disabledTokens) + " tok/轮"],
1824
2148
  ...(isPtc
@@ -1831,7 +2155,7 @@ window.__ModuleLoader__.load({
1831
2155
  h("div", { className: "tm_row", style: { padding: "10px 14px" } },
1832
2156
  h("input", { className: "tm_input", value: query, placeholder: "搜索工具名、描述或组名", onChange: (e) => setQuery(e.target.value) }),
1833
2157
  h("div", { className: "tm_filters" },
1834
- [["all", "全部"], ["live", "常开"], ["cold", "按需"], ["disabled", "已关闭"]].map(([id, label]) =>
2158
+ [["all", "全部"], ["live", "常驻"], ["cold", "按需"], ["disabled", "已关闭"]].map(([id, label]) =>
1835
2159
  h("button", { key: id, className: "tm_chip" + (filter === id ? " active" : ""), onClick: () => setFilter(id) }, label),
1836
2160
  ),
1837
2161
  ),