Repository navigation
[1405] 修复 message_usage 事件缺窗口时上下文占比回退为 0.0% - #23
Merged
Merged
Conversation
当 message_usage 事件未携带 context window(window=#f)时,pi-context-percent 返回 #f,导致 footer 格式化时显示成 "0.0%",表现为对话进行中占比突然归零。修复为回退到 footer 已知的 context-window 来计算百分比。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
问题
TUI 左下角上下文占比 `x.x%/1.0M (auto)` 在对话进行中会慢慢增加,但后面突然回退显示为 `0.0%`。
根因
`message_usage` 事件的第三项为当前模型的上下文窗口(`window`),在某些情况下为 `#f`(例如模型 metadata 缺少 `contextWindow` 时,`%cfg-context-window` 返回 `#f`)。
`app-events.scm` 原逻辑直接用事件携带的 `window` 计算占比:
```scheme
(window (if (> (length ev) 2) (caddr ev) #f))
(context (make-pi-context-usage (pi-context-percent context-tokens window) window #t))
```
当 `window=#f` 时:
这就表现为占比突然归零。
修复
当事件未携带有效窗口时,回退到 footer 已知的 `context-window` 来计算百分比:
```scheme
(footer (%pi-app-footer app))
(ev-window (if (> (length ev) 2) (caddr ev) #f))
(window (if (and (number? ev-window) (> ev-window 0))
ev-window
(pi-footer-context-window footer)))
```
footer 的 `context-window` 在初始化时默认 1000000,且会随 `active_node` / `set-model!` 等更新,是可靠的回退来源。
验证
Closes #1405