Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:sparkles: AI Agent https://github.com/siyuan-note/siyuan/issues/17797

Daniel8f19a8962fcf
diff --git a/app/src/layout/dock/AgentChat.ts b/app/src/layout/dock/AgentChat.ts
index 77b672530c..a375c3f877 100644
--- a/app/src/layout/dock/AgentChat.ts
+++ b/app/src/layout/dock/AgentChat.ts
@@ -225,8 +225,8 @@ export class AgentChat extends Model {
'<span class="fn__flex-1"></span>' +
'<span class="agent-chat__tokens fn__none" aria-label="' + (L.tokenUsage || "Context Usage") + '">' +
'<svg viewBox="0 0 24 24">' +
- '<circle class="agent-chat__tokens-track" cx="12" cy="12" r="9" stroke-width="2.5"></circle>' +
- '<circle class="agent-chat__tokens-arc" cx="12" cy="12" r="9" stroke-width="2.5" stroke-dasharray="0 56.55"></circle>' +
+ '<circle class="agent-chat__tokens-track" cx="12" cy="12" r="9" stroke-width="3"></circle>' +
+ '<circle class="agent-chat__tokens-arc" cx="12" cy="12" r="9" stroke-width="3" stroke-dasharray="0 56.55"></circle>' +
"</svg>" +
"</span>" +
'<button class="agent-chat__send b3-button b3-button--text b3-tooltips b3-tooltips__n" aria-label="' + (L.agentSend || "Send") + '"><svg><use xlink:href="#iconSend"></use></svg></button>' +
diff --git a/kernel/agent/tokens.go b/kernel/agent/tokens.go
index b0fbf78ea3..72c19a0510 100644
--- a/kernel/agent/tokens.go
+++ b/kernel/agent/tokens.go
@@ -202,7 +202,10 @@ func computeTokenBreakdown(counter *tokenCounter, messages []openai.ChatCompleti
breakdown["messages"] += 3
}
- // "其他" = 真实总数 - 前 9 类估算之和(clamp≥0)。吸收剩余的估算误差。
+ // 估算之和与真实 prompt tokens 对齐,保证各类百分比相加 = 100%。
+ // 估算 < 真实:差额计入 other(吸收低估残差)。
+ // 估算 > 真实:按比例等比压缩前 9 类(吸收高估残差),other 保持 0。
+ // 不归一化会导致前端各类百分比之和 > 100%(tiktoken 估算/overhead 补偿可能高估)。
estimated := 0
for k, v := range breakdown {
if k == "other" {
@@ -212,6 +215,25 @@ func computeTokenBreakdown(counter *tokenCounter, messages []openai.ChatCompleti
}
if realPromptTokens > estimated {
breakdown["other"] = realPromptTokens - estimated
+ } else if estimated > realPromptTokens && realPromptTokens > 0 {
+ scale := float64(realPromptTokens) / float64(estimated)
+ allocated := 0
+ // 按固定顺序缩放前 9 类(跳过 other),最后一类承接舍入残差以精确对齐。
+ keys := []string{"system", "skills", "messages",
+ "nativeToolsDef", "pluginToolsDef", "mcpToolsDef",
+ "nativeTool", "pluginTool", "mcpTool"}
+ for i, k := range keys {
+ if i == len(keys)-1 {
+ breakdown[k] = realPromptTokens - allocated
+ if breakdown[k] < 0 {
+ breakdown[k] = 0
+ }
+ break
+ }
+ scaled := int(float64(breakdown[k]) * scale)
+ breakdown[k] = scaled
+ allocated += scaled
+ }
}
return breakdown
}