| 1 |
package clnkrapp |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"encoding/json" |
| 6 |
"fmt" |
| 7 |
"io" |
| 8 |
"net/http" |
| 9 |
"net/http/httptest" |
| 10 |
"testing" |
| 11 |
|
| 12 |
"github.com/clnkr-ai/clnkr" |
| 13 |
"github.com/clnkr-ai/clnkr/cmd/internal/providerconfig" |
| 14 |
providerdomain "github.com/clnkr-ai/clnkr/internal/providers/providerconfig" |
| 15 |
) |
| 16 |
|
| 17 |
func anthropicWrappedDone(summary string) string { |
| 18 |
return fmt.Sprintf( |
| 19 |
`{"turn":{"type":"done","bash":null,"question":null,"summary":%q,"verification":{"status":"verified","checks":[{"command":"go test ./...","outcome":"passed","evidence":"go test ./... passed and ls output showed current directory entries for completion"}]},"known_risks":[],"reasoning":null}}`, |
| 20 |
summary, |
| 21 |
) |
| 22 |
} |
| 23 |
|
| 24 |
func TestNewModelForConfigPassesAnthropicRequestOptions(t *testing.T) { |
| 25 |
var gotBody map[string]any |
| 26 |
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 27 |
body, _ := io.ReadAll(r.Body) |
| 28 |
_ = json.Unmarshal(body, &gotBody) |
| 29 |
_ = json.NewEncoder(w).Encode(map[string]any{ |
| 30 |
"content": []map[string]string{{"type": "text", "text": anthropicWrappedDone("ok")}}, |
| 31 |
"usage": map[string]int{"input_tokens": 1, "output_tokens": 1}, |
| 32 |
}) |
| 33 |
})) |
| 34 |
defer server.Close() |
| 35 |
|
| 36 |
model := newModelForConfig(providerconfig.ResolvedProviderConfig{ |
| 37 |
Provider: providerdomain.ProviderAnthropic, |
| 38 |
Model: "claude-sonnet-4-20250514", |
| 39 |
BaseURL: server.URL, |
| 40 |
APIKey: "test-key", |
| 41 |
RequestOptions: providerdomain.ProviderRequestOptions{ |
| 42 |
AnthropicManual: providerdomain.AnthropicManualThinkingOptions{ |
| 43 |
ThinkingBudgetTokens: providerdomain.OptionalInt{Value: 2048, Set: true}, |
| 44 |
}, |
| 45 |
Output: providerdomain.ProviderOutputOptions{ |
| 46 |
MaxOutputTokens: providerdomain.OptionalInt{Value: 8000, Set: true}, |
| 47 |
}, |
| 48 |
}, |
| 49 |
}, "sys") |
| 50 |
if _, err := model.Query( |
| 51 |
context.Background(), |
| 52 |
[]clnkr.Message{{Role: "user", Content: "hi"}}, |
| 53 |
); err != nil { |
| 54 |
t.Fatalf("Query: %v", err) |
| 55 |
} |
| 56 |
if got := gotBody["max_tokens"]; got != float64(8000) { |
| 57 |
t.Fatalf("max_tokens = %#v, want 8000", got) |
| 58 |
} |
| 59 |
thinking, ok := gotBody["thinking"].(map[string]any) |
| 60 |
if !ok || thinking["type"] != "enabled" || thinking["budget_tokens"] != float64(2048) { |
| 61 |
t.Fatalf("thinking = %#v, want enabled budget 2048", gotBody["thinking"]) |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
func TestNewModelForConfigPassesBashToolCallOption(t *testing.T) { |
| 66 |
var gotBody map[string]any |
| 67 |
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 68 |
body, _ := io.ReadAll(r.Body) |
| 69 |
_ = json.Unmarshal(body, &gotBody) |
| 70 |
_ = json.NewEncoder(w).Encode(map[string]any{ |
| 71 |
"content": []map[string]any{{ |
| 72 |
"type": "tool_use", |
| 73 |
"id": "toolu_1", |
| 74 |
"name": "bash", |
| 75 |
"input": map[string]any{"command": "pwd", "workdir": nil}, |
| 76 |
}}, |
| 77 |
}) |
| 78 |
})) |
| 79 |
defer server.Close() |
| 80 |
|
| 81 |
model := newModelForConfig(providerconfig.ResolvedProviderConfig{ |
| 82 |
Provider: providerdomain.ProviderAnthropic, |
| 83 |
Model: "claude-sonnet-4-20250514", |
| 84 |
BaseURL: server.URL, |
| 85 |
APIKey: "test-key", |
| 86 |
ActProtocol: clnkr.ActProtocolToolCalls, |
| 87 |
}, "sys") |
| 88 |
resp, err := model.Query(context.Background(), []clnkr.Message{{Role: "user", Content: "hi"}}) |
| 89 |
if err != nil { |
| 90 |
t.Fatalf("Query: %v", err) |
| 91 |
} |
| 92 |
if tools, ok := gotBody["tools"].([]any); !ok || len(tools) != 1 { |
| 93 |
t.Fatalf("tools = %#v, want one bash tool", gotBody["tools"]) |
| 94 |
} |
| 95 |
if _, ok := resp.Turn.(*clnkr.ActTurn); !ok { |
| 96 |
t.Fatalf("Turn = %T, want *ActTurn", resp.Turn) |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
func TestNewModelForConfigPassesAnthropicEffortWithAdaptiveThinking(t *testing.T) { |
| 101 |
var gotBody map[string]any |
| 102 |
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 103 |
body, _ := io.ReadAll(r.Body) |
| 104 |
_ = json.Unmarshal(body, &gotBody) |
| 105 |
_ = json.NewEncoder(w).Encode(map[string]any{ |
| 106 |
"content": []map[string]string{{"type": "text", "text": anthropicWrappedDone("ok")}}, |
| 107 |
"usage": map[string]int{"input_tokens": 1, "output_tokens": 1}, |
| 108 |
}) |
| 109 |
})) |
| 110 |
defer server.Close() |
| 111 |
|
| 112 |
model := newModelForConfig(providerconfig.ResolvedProviderConfig{ |
| 113 |
Provider: providerdomain.ProviderAnthropic, |
| 114 |
Model: "claude-sonnet-4-20250514", |
| 115 |
BaseURL: server.URL, |
| 116 |
APIKey: "test-key", |
| 117 |
RequestOptions: providerdomain.ProviderRequestOptions{ |
| 118 |
Effort: providerdomain.ProviderEffortOptions{Level: "medium", Set: true}, |
| 119 |
Output: providerdomain.ProviderOutputOptions{ |
| 120 |
MaxOutputTokens: providerdomain.OptionalInt{Value: 4096, Set: true}, |
| 121 |
}, |
| 122 |
}, |
| 123 |
}, "sys") |
| 124 |
if _, err := model.Query( |
| 125 |
context.Background(), |
| 126 |
[]clnkr.Message{{Role: "user", Content: "hi"}}, |
| 127 |
); err != nil { |
| 128 |
t.Fatalf("Query: %v", err) |
| 129 |
} |
| 130 |
|
| 131 |
outputConfig, ok := gotBody["output_config"].(map[string]any) |
| 132 |
if !ok { |
| 133 |
t.Fatalf("output_config = %#v, want object", gotBody["output_config"]) |
| 134 |
} |
| 135 |
if outputConfig["effort"] != "medium" { |
| 136 |
t.Fatalf("output_config.effort = %#v, want medium", gotBody["output_config"]) |
| 137 |
} |
| 138 |
|
| 139 |
thinking, ok := gotBody["thinking"].(map[string]any) |
| 140 |
if !ok { |
| 141 |
t.Fatalf("thinking = %#v, want object", gotBody["thinking"]) |
| 142 |
} |
| 143 |
if thinking["type"] != "adaptive" { |
| 144 |
t.Fatalf("thinking.type = %#v, want adaptive", gotBody["thinking"]) |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
func TestMakeCompactorFactoryUsesOpenAIWhenProviderSelected(t *testing.T) { |
| 149 |
var gotBody map[string]any |
| 150 |
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 151 |
body, _ := io.ReadAll(r.Body) |
| 152 |
_ = json.Unmarshal(body, &gotBody) |
| 153 |
_ = json.NewEncoder(w).Encode(map[string]any{ |
| 154 |
"choices": []map[string]any{ |
| 155 |
{ |
| 156 |
"message": map[string]string{ |
| 157 |
"role": "assistant", |
| 158 |
"content": "Older work summarized.", |
| 159 |
}, |
| 160 |
}, |
| 161 |
}, |
| 162 |
"usage": map[string]int{"prompt_tokens": 1, "completion_tokens": 1}, |
| 163 |
}) |
| 164 |
})) |
| 165 |
defer server.Close() |
| 166 |
|
| 167 |
compactor := makeCompactorFactory(providerconfig.ResolvedProviderConfig{ |
| 168 |
Provider: "openai", |
| 169 |
ProviderAPI: "openai-chat-completions", |
| 170 |
BaseURL: server.URL, |
| 171 |
APIKey: "test-key", |
| 172 |
Model: "gpt-test", |
| 173 |
})() |
| 174 |
summary, err := compactor.Summarize( |
| 175 |
context.Background(), |
| 176 |
[]clnkr.Message{{Role: "user", Content: "first task"}}, |
| 177 |
) |
| 178 |
if err != nil { |
| 179 |
t.Fatalf("Summarize: %v", err) |
| 180 |
} |
| 181 |
if summary != "Older work summarized." { |
| 182 |
t.Fatalf("summary = %q, want %q", summary, "Older work summarized.") |
| 183 |
} |
| 184 |
if _, ok := gotBody["response_format"]; ok { |
| 185 |
t.Fatalf( |
| 186 |
"response_format should be omitted for compaction, got %#v", |
| 187 |
gotBody["response_format"], |
| 188 |
) |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
func TestMakeCompactorFactoryUsesOpenAIResponsesWhenConfigured(t *testing.T) { |
| 193 |
var requestPath string |
| 194 |
var gotBody map[string]any |
| 195 |
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 196 |
requestPath = r.URL.Path |
| 197 |
body, _ := io.ReadAll(r.Body) |
| 198 |
_ = json.Unmarshal(body, &gotBody) |
| 199 |
_ = json.NewEncoder(w).Encode(map[string]any{ |
| 200 |
"output": []map[string]any{ |
| 201 |
{ |
| 202 |
"type": "message", |
| 203 |
"role": "assistant", |
| 204 |
"content": []map[string]any{ |
| 205 |
{"type": "output_text", "text": "Older work summarized."}, |
| 206 |
}, |
| 207 |
}, |
| 208 |
}, |
| 209 |
"usage": map[string]int{"input_tokens": 1, "output_tokens": 1}, |
| 210 |
}) |
| 211 |
})) |
| 212 |
defer server.Close() |
| 213 |
|
| 214 |
compactor := makeCompactorFactory(providerconfig.ResolvedProviderConfig{ |
| 215 |
Provider: "openai", |
| 216 |
ProviderAPI: "openai-responses", |
| 217 |
BaseURL: server.URL, |
| 218 |
APIKey: "test-key", |
| 219 |
Model: "gpt-test", |
| 220 |
})() |
| 221 |
summary, err := compactor.Summarize( |
| 222 |
context.Background(), |
| 223 |
[]clnkr.Message{{Role: "user", Content: "first task"}}, |
| 224 |
) |
| 225 |
if err != nil { |
| 226 |
t.Fatalf("Summarize: %v", err) |
| 227 |
} |
| 228 |
if summary != "Older work summarized." { |
| 229 |
t.Fatalf("summary = %q, want %q", summary, "Older work summarized.") |
| 230 |
} |
| 231 |
if requestPath != "/responses" { |
| 232 |
t.Fatalf("request path = %q, want %q", requestPath, "/responses") |
| 233 |
} |
| 234 |
if _, ok := gotBody["text"]; ok { |
| 235 |
t.Fatalf("text should be omitted for compaction, got %#v", gotBody["text"]) |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
func TestMakeCompactorFactoryUsesAnthropicWhenProviderSelected(t *testing.T) { |
| 240 |
var requestPath string |
| 241 |
var gotBody map[string]any |
| 242 |
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 243 |
requestPath = r.URL.Path |
| 244 |
body, _ := io.ReadAll(r.Body) |
| 245 |
_ = json.Unmarshal(body, &gotBody) |
| 246 |
_ = json.NewEncoder(w).Encode(map[string]any{ |
| 247 |
"content": []map[string]string{ |
| 248 |
{"type": "text", "text": "Older work summarized."}, |
| 249 |
}, |
| 250 |
"usage": map[string]int{"input_tokens": 1, "output_tokens": 1}, |
| 251 |
}) |
| 252 |
})) |
| 253 |
defer server.Close() |
| 254 |
|
| 255 |
compactor := makeCompactorFactory(providerconfig.ResolvedProviderConfig{ |
| 256 |
Provider: "anthropic", |
| 257 |
BaseURL: server.URL, |
| 258 |
APIKey: "test-key", |
| 259 |
Model: "claude-test", |
| 260 |
})() |
| 261 |
summary, err := compactor.Summarize( |
| 262 |
context.Background(), |
| 263 |
[]clnkr.Message{{Role: "user", Content: "first task"}}, |
| 264 |
) |
| 265 |
if err != nil { |
| 266 |
t.Fatalf("Summarize: %v", err) |
| 267 |
} |
| 268 |
if summary != "Older work summarized." { |
| 269 |
t.Fatalf("summary = %q, want %q", summary, "Older work summarized.") |
| 270 |
} |
| 271 |
if requestPath != "/v1/messages" { |
| 272 |
t.Fatalf("request path = %q, want %q", requestPath, "/v1/messages") |
| 273 |
} |
| 274 |
if _, ok := gotBody["response_format"]; ok { |
| 275 |
t.Fatalf( |
| 276 |
"response_format should be omitted for compaction, got %#v", |
| 277 |
gotBody["response_format"], |
| 278 |
) |
| 279 |
} |
| 280 |
if _, ok := gotBody["max_tokens"]; !ok { |
| 281 |
t.Fatalf("anthropic request missing max_tokens: %#v", gotBody) |
| 282 |
} |
| 283 |
} |