修复: namespace引用按真实体量入账估算
ref 尾段携带原文字节数, 估算器解析入账(原 ~15 token 低估致 预算/裁剪/压缩三门失效); 旧格式向后兼容, 淘汰内容不虚占预算
This commit is contained in:
@@ -61,6 +61,15 @@ impl TokenEstimator {
|
||||
/// 这里把 parts 的文本/base64 按同一 chars_ratio 粗估累加(base64 视为密集字符,0.35 偏保守)。
|
||||
pub fn estimate_message(&self, msg: &ChatMessage) -> u32 {
|
||||
let mut char_count = msg.content.chars().count();
|
||||
// 939① 估算入账修正:Tool 消息 content 为 namespace 引用时,按 ref 尾段携带的
|
||||
// 原文字节数入账(字节保守视作字符,中文会 3x 高估——方向安全:宁可高估触发
|
||||
// 裁剪/压缩,不可漏估致预算门失效,后者曾致 provider 超限 400)。旧格式/已淘汰
|
||||
// 内容(无 ?len= 尾段)按字面字符保守计,行为同前。
|
||||
if matches!(msg.role, crate::provider::MessageRole::Tool) {
|
||||
if let Some(n) = crate::namespace_store::parse_ref_content_len(&msg.content) {
|
||||
char_count = n;
|
||||
}
|
||||
}
|
||||
// url 图地板单独按 token 累加:地板语义是"每图至少 ~1600 token",必须生效于
|
||||
// ×chars_ratio 折扣之后——若折进 char_count 会被 0.35 稀释成 560,1600 保底失效,
|
||||
// 含 url 图消息的 history_tokens 低估 → 预算裁剪漏触发 → provider 超限 400。
|
||||
|
||||
Reference in New Issue
Block a user