This commit is contained in:
lpf
2026-02-19 15:40:27 +08:00
parent 75e678061a
commit 15bd337c49
3 changed files with 282 additions and 16 deletions

View File

@@ -1,6 +1,7 @@
package agent
import (
"context"
"fmt"
"strings"
"testing"
@@ -8,6 +9,35 @@ import (
"clawgo/pkg/providers"
)
type compactionTestProvider struct {
errByModel map[string]error
summaryByModel map[string]string
calledModels []string
}
func (p *compactionTestProvider) Chat(ctx context.Context, messages []providers.Message, tools []providers.ToolDefinition, model string, options map[string]interface{}) (*providers.LLMResponse, error) {
return &providers.LLMResponse{Content: "summary-fallback"}, nil
}
func (p *compactionTestProvider) GetDefaultModel() string {
return ""
}
func (p *compactionTestProvider) SupportsResponsesCompact() bool {
return true
}
func (p *compactionTestProvider) BuildSummaryViaResponsesCompact(ctx context.Context, model string, existingSummary string, messages []providers.Message, maxSummaryChars int) (string, error) {
p.calledModels = append(p.calledModels, model)
if err := p.errByModel[model]; err != nil {
return "", err
}
if out := strings.TrimSpace(p.summaryByModel[model]); out != "" {
return out, nil
}
return "", fmt.Errorf(`responses compact request failed (status 400): {"error":{"message":"model not found"}}`)
}
func TestShouldCompactBySize(t *testing.T) {
history := []providers.Message{
{Role: "user", Content: strings.Repeat("a", 80)},
@@ -58,3 +88,45 @@ func TestFormatCompactionTranscript_TrimsToolPayloadMoreAggressively(t *testing.
t.Fatalf("expected tool content to be trimmed aggressively, got length %d", len(out))
}
}
func TestBuildCompactedSummary_ResponsesCompactFallsBackToBackupProxyOnTimeout(t *testing.T) {
primary := &compactionTestProvider{
errByModel: map[string]error{
"gpt-4o-mini": fmt.Errorf("failed to send request: Post \"https://primary/v1/chat/completions\": context deadline exceeded"),
},
}
backup := &compactionTestProvider{
summaryByModel: map[string]string{
"deepseek-chat": "compacted summary",
},
}
al := &AgentLoop{
provider: primary,
proxy: "primary",
proxyFallbacks: []string{"backup"},
model: "gpt-4o-mini",
providersByProxy: map[string]providers.LLMProvider{
"primary": primary,
"backup": backup,
},
modelsByProxy: map[string][]string{
"primary": []string{"gpt-4o-mini"},
"backup": []string{"deepseek-chat"},
},
}
out, err := al.buildCompactedSummary(context.Background(), "", []providers.Message{{Role: "user", Content: "a"}}, 2000, 1200, "responses_compact")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if strings.TrimSpace(out) != "compacted summary" {
t.Fatalf("unexpected summary: %q", out)
}
if al.proxy != "backup" {
t.Fatalf("expected proxy switched to backup, got %q", al.proxy)
}
if al.model != "deepseek-chat" {
t.Fatalf("expected model switched to deepseek-chat, got %q", al.model)
}
}