Skip to content

feat(desktop): 输入框上下文用量胶囊 + 逐对话压缩阈值面板 / context usage chip and per-chat compaction threshold - #62

Merged
sqfcyily merged 1 commit into
sqfcyily:merge/context-usage-chipfrom
moxuyio1024:feat/context-usage-chip
Nov 17, 2026
Merged

sqfcyily merged 1 commit into
sqfcyily:merge/context-usage-chipfrom
moxuyio1024:feat/context-usage-chip

Conversation

@moxuyio1024

Copy link
Copy Markdown
Contributor

在输入框工具条模型贴片左侧加一枚胶囊,显示本对话的窗口占用与上一轮提示缓存命中率;点开可拖动自动压缩的触发比例并查看压缩记录。用量走压缩判定的同一套口径,故显示的百分比就是压缩的实际依据。

  • 主进程新增 chat:context-usage / chat:set-compact-ratio 两个 IPC,与压缩判定共用 contextWindowFor / triggerTokens,避免界面与引擎两套口径各算各的。
  • 会话新增 compactRatio / compactLog / lastPromptParts 三个字段(纯观测,不参与判定;旧版读到未知键会忽略,不影响兼容)。
  • 压缩发生的那一刻就推一次用量快照(轮前与轮中两处),否则长回合要跑十几分钟,胶囊和面板会一直停在压缩前的读数上,表现为「明明已压缩却显示还没压过」。
  • 两个百分比分母不同,故胶囊只显示窗口占用(分母=生效窗口),压缩线进度放进悬停提示并决定转红:越过压缩线就红,避免和滑块读数重复。
  • 提示词构成(系统提示词 / 工具定义 / 对话消息)取本地字符估算,再按「真实总量 / 估算总量」等比缩放展示,三项之和恒等于真实总量。

参考来源(取舍依据,非代码照搬):

  • DSH 插件 dsh-compaction-threshold(MIT,npm):阈值滑块写 thresholdRatio、滑块范围 20%–95%、压缩记录「压后」读数延后到下一次 usage 回报才定稿。
  • 触发线算式(窗口 − 输出上限 − 余量)对齐 Claude Code;保留尾部上限参照 Codex 与 OpenCode(见 compaction.ts 内注释)。

验证

  • corepack pnpm typecheck、corepack pnpm build(与 CI 相同的两步)通过。
  • Windows 实际构建(Electron 安装版)上实测:胶囊随回合更新;滑块改动立刻生效——该会话在 562K 时按 54% 触发压缩,折入 1129 条消息;压缩记录能读到 before/after 读数。

说明

记忆系统的改动(遗忘区归档 + memory_merge / memory_check / memory_tidy)另开一帖 #61 附补丁,与本提交无关,可分别评审。若上游另有更合适的呈现方式,欢迎直接改。

…at compaction threshold

在输入框工具条模型贴片左侧加一枚胶囊,显示本对话的窗口占用与上一轮提示缓存
命中率;点开可拖动自动压缩的触发比例并查看压缩记录。用量走压缩判定的同一套
口径,故显示的百分比就是压缩的实际依据。

- 主进程新增 chat:context-usage / chat:set-compact-ratio 两个 IPC,与压缩判定共用
  contextWindowFor / triggerTokens,避免界面与引擎两套口径各算各的。
- 会话新增 compactRatio / compactLog / lastPromptParts 三个字段(纯观测,不参与判定;
  旧版读到未知键会忽略,不影响兼容)。
- 压缩发生的那一刻就推一次用量快照(轮前与轮中两处),否则长回合要跑十几分钟,
  胶囊和面板会一直停在压缩前的读数上,表现为「明明已压缩却显示还没压过」。
- 两个百分比分母不同,故胶囊只显示窗口占用(分母=生效窗口),压缩线进度放进悬停
  提示并决定转红:越过压缩线就红,避免和滑块读数重复。
- 提示词构成(系统提示词 / 工具定义 / 对话消息)取本地字符估算,再按「真实总量 /
  估算总量」等比缩放展示,三项之和恒等于真实总量。

参考来源(取舍依据,非代码照搬):
- DSH 插件 dsh-compaction-threshold(MIT,npm):阈值滑块写 thresholdRatio、滑块范围
  20%–95%、压缩记录「压后」读数延后到下一次 usage 回报才定稿。
- 触发线算式(窗口 − 输出上限 − 余量)对齐 Claude Code;保留尾部上限参照 Codex 与
  OpenCode(见 compaction.ts 内注释)。

---

Add a context-usage chip to the left of the model picker in the composer toolbar,
showing window usage and the last turn's prompt-cache hit rate. Clicking it opens a
panel with a drag-to-set auto-compaction threshold slider and the chat's compaction
log. Usage shares the exact same calculation as the compaction trigger, so the
displayed percentage is what the engine actually acts on.

- New IPC channels chat:context-usage / chat:set-compact-ratio reusing
  contextWindowFor / triggerTokens from compaction.ts, so the UI and the engine
  cannot drift apart.
- Session gains compactRatio / compactLog / lastPromptParts (observation only, never
  used for decisions; older builds ignore unknown keys).
- Emit a usage snapshot the moment compaction happens (both before a turn and mid-turn),
  otherwise a long turn keeps showing pre-compaction numbers for minutes.
- The two percentages have different denominators, so the chip shows window usage only;
  the compaction-line progress lives in the tooltip and drives the red state.
- Prompt composition (system prompt / tool definitions / messages) is estimated locally,
  then scaled so the three parts always sum to the real total.

References (rationale only, no code copied):
- DSH plugin dsh-compaction-threshold (MIT, npm): thresholdRatio slider, the 20%–95%
  range, and deferring the post-compaction reading until the next usage report.
- Trigger formula (window − output budget − headroom) follows Claude Code; tail retention
  limits follow Codex and OpenCode (see comments in compaction.ts).
@moxuyio1024
moxuyio1024 force-pushed the feat/context-usage-chip branch from 1e90cb1 to 2d6ee65 Compare November 16, 2026 11:37
@sqfcyily
sqfcyily changed the base branch from main to merge/context-usage-chip November 17, 2026 07:55
@sqfcyily
sqfcyily merged commit 2d6ee65 into sqfcyily:merge/context-usage-chip Nov 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants