模块介绍 / 今日规划 / 下班日报 / 团队摘要 生成成功后没有上报,也几乎不写「AI」日志。

流结束时的 usage 用了非阻塞发送,通道一满就被丢掉;很多服务商默认还不在流里带 usage。
现在每次真正打到模型的调用都会:

在本地日志(分类 AI,消息 AI 调用完成)写下 scene / prompt / completion / cached
登录后同步上报到后台日汇总
This commit is contained in:
李琦
2026-08-17 14:00:47 +08:00
parent 83e69317d4
commit e71ce451ff
23 changed files with 1363 additions and 93 deletions

View File

@@ -56,6 +56,51 @@ func TestChatStreamSSE(t *testing.T) {
}
}
func TestParseUsageCachedTokens(t *testing.T) {
u := parseUsage(&chatUsage{
PromptTokens: 100,
CompletionTokens: 20,
PromptCacheHitTokens: 80,
})
if u == nil || u.CachedTokens != 80 || u.PromptTokens != 100 {
t.Fatalf("deepseek cache field: %+v", u)
}
u = parseUsage(&chatUsage{
PromptTokens: 90,
CompletionTokens: 10,
PromptTokensDetails: &struct {
CachedTokens int64 `json:"cached_tokens"`
}{CachedTokens: 64},
})
if u == nil || u.CachedTokens != 64 {
t.Fatalf("openai cached_tokens: %+v", u)
}
}
func TestChatStreamSendsUsage(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/event-stream")
w.Write([]byte("data: {\"choices\":[{\"delta\":{\"content\":\"ok\"}}]}\n\n"))
w.Write([]byte("data: {\"usage\":{\"prompt_tokens\":12,\"completion_tokens\":3,\"prompt_cache_hit_tokens\":8}}\n\n"))
w.Write([]byte("data: [DONE]\n\n"))
}))
defer srv.Close()
p := &openAICompatible{name: "test", baseURL: srv.URL, model: "m", apiKey: "k", client: srv.Client()}
stream, e := p.ChatStream(context.Background(), []Message{{Role: "user", Content: "hi"}})
if e != nil {
t.Fatal(e)
}
var usage *Usage
for c := range stream {
if c.Usage != nil {
usage = c.Usage
}
}
if usage == nil || usage.PromptTokens != 12 || usage.CachedTokens != 8 {
t.Fatalf("usage not delivered: %+v", usage)
}
}
func TestChatStreamAuthError(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusUnauthorized)