refactor(ai): 助手按 AI-AGENT 开发指南对齐(Ask 模式)
对照指南逐条评估后落地 4 项真实差距(MCP/Agent Loop/白名单等重型章节不适用,明确排除): - 上下文预算(指南 07 TrimMessages):历史按 24k 字符预算从最旧裁剪,system 标注 「更早的对话已省略」;抽 trimHistory 独立函数+单测(超预算丢最旧/小历史不裁) - 配额超限诚实终态(指南 05.3/06 禁止伪装 completed):不再 400 报错,返回提示 文案;前端渲染为 warning 提示条样式,对话上下文保留 - 回复脱敏(指南 08):assistant 回复同样过 RedactTextOnly,堵住模型复述样本 带回敏感串的通道 - 退避重试(指南 05.2):429/5xx/网络错误指数退避 1s→2s 最多 2 次(计入总超时), 鉴权/参数错不重试
This commit is contained in:
1 parent
448f695c28
commit
550d8f4967
3 files changed
+106
-13
No files matched your search
@@ -0,0 +1,34 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// TestChatHistoryBudgetTrim 历史按字符预算裁剪:超预算时最旧被丢、标记注入。
|
||||
// 通过构造超长历史走 RunAIChat 会因未启用而提前返回,这里直接测裁剪语义的独立函数化验证——
|
||||
// 为可测性把裁剪抽成了 trimHistory。
|
||||
func TestChatHistoryBudgetTrim(t *testing.T) {
|
||||
long := strings.Repeat("a", 15000)
|
||||
history := []ChatMessage{
|
||||
{Role: "user", Content: long},
|
||||
{Role: "assistant", Content: long},
|
||||
{Role: "user", Content: "最后一句"},
|
||||
}
|
||||
kept, truncated := trimHistory(history, 24000)
|
||||
if !truncated {
|
||||
t.Fatal("超出预算应标记 truncated")
|
||||
}
|
||||
if len(kept) != 2 {
|
||||
t.Fatalf("应保留最近 2 条(丢弃最旧的 15000 字条目),got %d", len(kept))
|
||||
}
|
||||
if kept[0].Content != long || kept[len(kept)-1].Content != "最后一句" {
|
||||
t.Fatal("应保留最近的消息")
|
||||
}
|
||||
// 不超预算时全保留
|
||||
small := []ChatMessage{{Role: "user", Content: "hi"}, {Role: "assistant", Content: "ok"}}
|
||||
kept2, truncated2 := trimHistory(small, 24000)
|
||||
if truncated2 || len(kept2) != 2 {
|
||||
t.Fatalf("小历史不应裁剪: %v %d", truncated2, len(kept2))
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user