efe8b6f41d
Introduce display_type-based output stripping for SSE/DB storage and add knowledge_chunks_list display type for list_knowledge_chunks.
90 lines
2.7 KiB
Go
90 lines
2.7 KiB
Go
package tools
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/Tencent/WeKnora/internal/types"
|
|
)
|
|
|
|
func TestShouldOmitRawToolOutput(t *testing.T) {
|
|
if !ShouldOmitRawToolOutput(ToolListKnowledgeChunks, map[string]interface{}{"display_type": "knowledge_chunks_list"}) {
|
|
t.Fatal("structured list_knowledge_chunks output should be omitted")
|
|
}
|
|
if !ShouldOmitRawToolOutput(ToolGrepChunks, map[string]interface{}{"display_type": "grep_results"}) {
|
|
t.Fatal("structured grep output should be omitted")
|
|
}
|
|
if ShouldOmitRawToolOutput("custom_tool", nil) {
|
|
t.Fatal("unknown tools should keep raw output by default")
|
|
}
|
|
}
|
|
|
|
func TestSanitizeToolDataForPersist_knowledgeChunksList(t *testing.T) {
|
|
data := map[string]interface{}{
|
|
"display_type": "knowledge_chunks_list",
|
|
"knowledge_title": "sample.pdf",
|
|
"fetched_chunks": 50,
|
|
"total_chunks": 282,
|
|
"chunks": []map[string]interface{}{{"content": "secret"}},
|
|
}
|
|
out := SanitizeToolDataForPersist(data)
|
|
if _, ok := out["chunks"]; ok {
|
|
t.Fatal("chunk bodies should be stripped from persisted tool data")
|
|
}
|
|
if out["fetched_chunks"] != 50 {
|
|
t.Fatalf("summary fields should be kept, got %#v", out["fetched_chunks"])
|
|
}
|
|
}
|
|
|
|
func TestSanitizeAgentStepsForStorage_stripsLargeOutput(t *testing.T) {
|
|
steps := []types.AgentStep{{
|
|
Iteration: 1,
|
|
ToolCalls: []types.ToolCall{{
|
|
ID: "call-1",
|
|
Name: ToolListKnowledgeChunks,
|
|
Result: &types.ToolResult{
|
|
Success: true,
|
|
Output: strings.Repeat("x", 10000),
|
|
Data: map[string]interface{}{
|
|
"display_type": "knowledge_chunks_list",
|
|
"knowledge_title": "sample.pdf",
|
|
"fetched_chunks": 50,
|
|
"total_chunks": 282,
|
|
"chunks": []map[string]interface{}{{"content": "body"}},
|
|
},
|
|
},
|
|
}},
|
|
}}
|
|
|
|
sanitized := SanitizeAgentStepsForStorage(steps)
|
|
result := sanitized[0].ToolCalls[0].Result
|
|
if len(result.Output) >= 10000 {
|
|
t.Fatal("persisted output should be compacted")
|
|
}
|
|
if !strings.Contains(result.Output, "content omitted from history") {
|
|
t.Fatalf("unexpected compact output: %q", result.Output)
|
|
}
|
|
if _, ok := result.Data["chunks"]; ok {
|
|
t.Fatal("chunk bodies should be removed from persisted data")
|
|
}
|
|
}
|
|
|
|
func TestSanitizeToolResultForClient_omitsOutput(t *testing.T) {
|
|
meta := SanitizeToolResultForClient(ToolListKnowledgeChunks, &types.ToolResult{
|
|
Success: true,
|
|
Output: "<knowledge_chunks>very large</knowledge_chunks>",
|
|
Data: map[string]interface{}{
|
|
"display_type": "knowledge_chunks_list",
|
|
"knowledge_title": "sample.pdf",
|
|
"fetched_chunks": 1,
|
|
"total_chunks": 1,
|
|
},
|
|
})
|
|
if _, ok := meta["output"]; ok {
|
|
t.Fatal("raw output should not be sent to client metadata")
|
|
}
|
|
if meta["fetched_chunks"] != 1 {
|
|
t.Fatalf("summary metadata should remain, got %#v", meta["fetched_chunks"])
|
|
}
|
|
}
|