6fb858105a
Normalize provider-specific cache counters into TokenUsage, propagate call purpose and prefix fingerprints through context for logging and Langfuse, and restructure wiki page-modify prompts with warmup/singleflight to improve prefix cache hit rates during reduce batches.
59 lines
1.8 KiB
Go
59 lines
1.8 KiB
Go
package chat
|
|
|
|
import (
|
|
"reflect"
|
|
"testing"
|
|
|
|
"github.com/Tencent/WeKnora/internal/types"
|
|
)
|
|
|
|
func TestBuildLangfuseGenerationOutput(t *testing.T) {
|
|
toolCalls := []types.LLMToolCall{{ID: "call_1", Type: "function"}}
|
|
|
|
got := buildLangfuseGenerationOutput("", "", "tool_calls", toolCalls)
|
|
want := map[string]interface{}{
|
|
"content": "",
|
|
"tool_calls": toolCalls,
|
|
"finish_reason": "tool_calls",
|
|
}
|
|
if !reflect.DeepEqual(got, want) {
|
|
t.Fatalf("output without reasoning = %#v; want %#v", got, want)
|
|
}
|
|
|
|
got = buildLangfuseGenerationOutput("answer", "thinking", "stop", nil)
|
|
want = map[string]interface{}{
|
|
"content": "answer",
|
|
"tool_calls": []types.LLMToolCall(nil),
|
|
"finish_reason": "stop",
|
|
"reasoning_content": "thinking",
|
|
}
|
|
if !reflect.DeepEqual(got, want) {
|
|
t.Fatalf("output with reasoning = %#v; want %#v", got, want)
|
|
}
|
|
}
|
|
|
|
func TestBuildLangfuseMessagesReasoningContent(t *testing.T) {
|
|
msgs := buildLangfuseMessages([]Message{
|
|
{Role: "assistant", ReasoningContent: "chain of thought", ToolCalls: []ToolCall{{ID: "tc1"}}},
|
|
})
|
|
if len(msgs) != 1 {
|
|
t.Fatalf("len(messages) = %d; want 1", len(msgs))
|
|
}
|
|
if msgs[0]["reasoning_content"] != "chain of thought" {
|
|
t.Fatalf("reasoning_content = %v; want chain of thought", msgs[0]["reasoning_content"])
|
|
}
|
|
}
|
|
|
|
func TestConvertUsageIncludesPromptCacheCounters(t *testing.T) {
|
|
got := convertUsage(&types.TokenUsage{
|
|
PromptTokens: 1000, CompletionTokens: 50, TotalTokens: 1050,
|
|
CacheReadTokens: 800, CacheWriteTokens: 100, CacheMissTokens: 200,
|
|
})
|
|
if got == nil {
|
|
t.Fatal("convertUsage returned nil")
|
|
}
|
|
if got.CacheRead != 800 || got.CacheWrite != 100 || got.CacheMiss != 200 {
|
|
t.Fatalf("cache usage = read:%d write:%d miss:%d", got.CacheRead, got.CacheWrite, got.CacheMiss)
|
|
}
|
|
}
|