v0.21.0 cd9706d

provider_test.go RawHistoryFolder

1package provider
2
3import (
4	"context"
5	"errors"
6	"io"
7	"math"
8	"net/http"
9	"net/http/httptest"
10	"strings"
11	"testing"
12)
13
14func TestUsageCacheHitRatioIncludesAllPromptTokens(t *testing.T) {
15	u := Usage{InputTokens: 84_000, CacheReadTokens: 123_000, CacheWriteTokens: 3_000, CacheMeasuredPromptTokens: 210_000, CacheMeasuredReadTokens: 123_000}
16	if got := u.PromptTokens(); got != 210_000 {
17		t.Fatalf("prompt tokens = %d, want 210000", got)
18	}
19	ratio, ok := u.CacheHitRatio()
20	if !ok || math.Abs(ratio-123.0/210.0) > 1e-9 {
21		t.Fatalf("cache hit ratio = %v, %v, want %v, true", ratio, ok, 123.0/210.0)
22	}
23}
24
25func TestUsageCacheHitRatioDistinguishesKnownZeroFromHistoricalUnknown(t *testing.T) {
26	knownMiss := Usage{InputTokens: 84_000, CacheMeasuredPromptTokens: 84_000}
27	if ratio, ok := knownMiss.CacheHitRatio(); !ok || ratio != 0 {
28		t.Fatalf("known cache miss ratio = %v, %v, want 0, true", ratio, ok)
29	}
30
31	historicalUnknown := Usage{InputTokens: 84_000}
32	if ratio, ok := historicalUnknown.CacheHitRatio(); ok || ratio != 0 {
33		t.Fatalf("historical cache ratio = %v, %v, want 0, false", ratio, ok)
34	}
35}
36
37func TestUsageAddPreservesOnlyMeasuredCacheDenominators(t *testing.T) {
38	measured := Usage{InputTokens: 60, CacheReadTokens: 40, CacheMeasuredPromptTokens: 100, CacheMeasuredReadTokens: 40}
39	unknown := Usage{InputTokens: 200, CacheReadTokens: 100}
40
41	got := measured.Add(unknown)
42	if got.CacheMeasuredPromptTokens != 100 {
43		t.Fatalf("measured cache denominator = %d, want 100", got.CacheMeasuredPromptTokens)
44	}
45	if ratio, ok := got.CacheHitRatio(); !ok || math.Abs(ratio-0.4) > 1e-9 {
46		t.Fatalf("cache hit ratio = %v, %v, want 0.4, true", ratio, ok)
47	}
48}
49
50func TestSSEParse(t *testing.T) {
51	r := strings.NewReader("event: foo\ndata: {\"a\":1}\n\ndata: hello\ndata: world\n\n")
52	ch := make(chan sseEvent, 4)
53	go readSSE(r, ch)
54
55	e := <-ch
56	if e.Event != "foo" || e.Data != `{"a":1}` {
57		t.Fatalf("event 1: %+v", e)
58	}
59	e = <-ch
60	if e.Event != "" || e.Data != "hello\nworld" {
61		t.Fatalf("event 2: %+v", e)
62	}
63	if _, ok := <-ch; ok {
64		t.Fatalf("channel not closed")
65	}
66}
67
68type errorAfterReader struct {
69	data *strings.Reader
70	err  error
71}
72
73func (r *errorAfterReader) Read(p []byte) (int, error) {
74	if r.data.Len() > 0 {
75		return r.data.Read(p)
76	}
77	return 0, r.err
78}
79
80func TestSSEReadErrorIsDeliveredBeforeChannelClose(t *testing.T) {
81	want := io.ErrUnexpectedEOF
82	ch := make(chan sseEvent, 2)
83	go readSSE(&errorAfterReader{
84		data: strings.NewReader("data: hello\n\n"),
85		err:  want,
86	}, ch)
87
88	if event := <-ch; event.Data != "hello" {
89		t.Fatalf("event = %+v, want payload before read error", event)
90	}
91	terminal, ok := <-ch
92	if !ok {
93		t.Fatal("channel closed without scanner error")
94	}
95	if !errors.Is(terminal.Err, want) {
96		t.Fatalf("scanner error = %v, want %v", terminal.Err, want)
97	}
98	if _, ok := <-ch; ok {
99		t.Fatal("channel remained open after scanner error")
100	}
101}
102
103func TestStreamsReportSSEReadFailures(t *testing.T) {
104	want := io.ErrUnexpectedEOF
105	response := func() *http.Response {
106		return &http.Response{Body: io.NopCloser(&errorAfterReader{
107			data: strings.NewReader(""),
108			err:  want,
109		})}
110	}
111	cases := []struct {
112		name string
113		run  func(chan<- Event)
114	}{
115		{
116			name: "anthropic",
117			run: func(out chan<- Event) {
118				(&anthropicClient{}).runStream(context.Background(), response(), Request{Model: "test"}, out)
119			},
120		},
121		{
122			name: "openai",
123			run: func(out chan<- Event) {
124				(&openaiClient{}).runStream(context.Background(), response(), Request{Model: "test"}, out)
125			},
126		},
127		{
128			name: "gemini",
129			run: func(out chan<- Event) {
130				(&geminiClient{}).runStream(context.Background(), response(), Request{Model: "test"}, out)
131			},
132		},
133		{
134			name: "openai codex",
135			run: func(out chan<- Event) {
136				(&codexClient{}).runStream(context.Background(), response(), Request{Model: "test"}, out)
137			},
138		},
139	}
140	for _, tc := range cases {
141		t.Run(tc.name, func(t *testing.T) {
142			out := make(chan Event, 4)
143			go tc.run(out)
144
145			var done EventDone
146			foundDone := false
147			for event := range out {
148				if e, ok := event.(EventDone); ok {
149					done = e
150					foundDone = true
151				}
152			}
153			if !foundDone {
154				t.Fatal("stream ended without EventDone")
155			}
156			if done.Stop != StopError {
157				t.Fatalf("stop = %s, want %s", done.Stop, StopError)
158			}
159			if !errors.Is(done.Err, want) {
160				t.Fatalf("stream error = %v, want %v", done.Err, want)
161			}
162		})
163	}
164}
165
166func TestModelCatalog(t *testing.T) {
167	if len(Catalog) == 0 {
168		t.Fatal("empty catalog")
169	}
170	if _, err := FindModel("anthropic", "claude-sonnet-4-5"); err != nil {
171		t.Fatal(err)
172	}
173	if _, err := FindModel("openai", "gpt-5"); err != nil {
174		t.Fatal(err)
175	}
176	if _, err := FindModel("", "nope"); err == nil {
177		t.Fatal("expected error")
178	}
179}
180
181func TestVertexGeminiFlashCatalogLimits(t *testing.T) {
182	for _, id := range []string{"gemini-3.1-flash-lite", "gemini-3.5-flash"} {
183		m, err := FindModel("google-vertex", id)
184		if err != nil {
185			t.Fatal(err)
186		}
187		if m.ContextWindow != 1048576 || m.MaxOutput != 65535 || !m.Reasoning {
188			t.Errorf("%s metadata = %+v", id, m)
189		}
190	}
191}
192
193func TestComputeCost(t *testing.T) {
194	m, _ := FindModel("anthropic", "claude-sonnet-4-5")
195	cost := ComputeCost(m, Usage{InputTokens: 1_000_000, OutputTokens: 1_000_000})
196	want := m.PriceInput + m.PriceOutput
197	if cost != want {
198		t.Fatalf("cost=%v want=%v", cost, want)
199	}
200}
201
202func TestComputeCostInputTier(t *testing.T) {
203	m := Model{
204		PriceInput: 1, PriceOutput: 2, PriceCacheRead: 0.1,
205		PriceTierInputTokens: 100,
206		PriceInputAbove:      3, PriceOutputAbove: 4, PriceCacheReadAbove: 0.2,
207	}
208	below := ComputeCost(m, Usage{InputTokens: 50, OutputTokens: 10, CacheReadTokens: 50})
209	wantBelow := (50.0*1 + 10*2 + 50*0.1) / 1_000_000
210	if math.Abs(below-wantBelow) > 1e-12 {
211		t.Fatalf("below-tier cost=%v want=%v", below, wantBelow)
212	}
213
214	above := ComputeCost(m, Usage{InputTokens: 51, OutputTokens: 10, CacheReadTokens: 50})
215	wantAbove := (51.0*3 + 10*4 + 50*0.2) / 1_000_000
216	if math.Abs(above-wantAbove) > 1e-12 {
217		t.Fatalf("above-tier cost=%v want=%v", above, wantAbove)
218	}
219}
220
221func TestAnthropicErrorStatus(t *testing.T) {
222	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
223		w.WriteHeader(http.StatusUnauthorized)
224		_, _ = w.Write([]byte(`{"error":{"type":"auth","message":"bad key"}}`))
225	}))
226	defer srv.Close()
227
228	c := NewAnthropic("x", srv.URL)
229	_, err := c.Stream(context.Background(), Request{Model: "claude-sonnet-4-5"})
230	if err == nil || !strings.Contains(err.Error(), "401") {
231		t.Fatalf("want 401 err, got %v", err)
232	}
233}
234
235func TestOpenAIErrorStatus(t *testing.T) {
236	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
237		w.WriteHeader(http.StatusBadRequest)
238		_, _ = w.Write([]byte(`{"error":{"message":"nope"}}`))
239	}))
240	defer srv.Close()
241
242	c := NewOpenAI("x", srv.URL)
243	_, err := c.Stream(context.Background(), Request{Model: "gpt-5"})
244	if err == nil || !strings.Contains(err.Error(), "400") {
245		t.Fatalf("want 400 err, got %v", err)
246	}
247}
248
249func TestOpenAIUserContentPreservesProjectedTextBlocks(t *testing.T) {
250	c := NewOpenAI("x", "").(*openaiClient)
251	wire, err := c.buildRequest(Request{
252		Model: "gpt-5",
253		Messages: []Message{{
254			Role: RoleUser,
255			Content: []Content{
256				TextBlock{Text: "[message time: 2026-08-08T12:34:56-07:00]"},
257				TextBlock{Text: "fail"},
258			},
259		}},
260	})
261	if err != nil {
262		t.Fatal(err)
263	}
264	blocks, ok := wire.Messages[0].Content.([]interface{})
265	if !ok {
266		t.Fatalf("user content = %T, want structured text blocks", wire.Messages[0].Content)
267	}
268	if len(blocks) != 2 {
269		t.Fatalf("user content blocks = %d, want 2", len(blocks))
270	}
271	if got := blocks[0].(oaiContentText).Text; got != "[message time: 2026-08-08T12:34:56-07:00]" {
272		t.Fatalf("timestamp block = %q", got)
273	}
274	if got := blocks[1].(oaiContentText).Text; got != "fail" {
275		t.Fatalf("user text block = %q", got)
276	}
277}
278
279func TestMiniMaxM3CatalogAndRequest(t *testing.T) {
280	for _, provider := range []string{"minimax", "minimax-cn"} {
281		m, err := FindModel(provider, "MiniMax-M3")
282		if err != nil {
283			t.Fatal(err)
284		}
285		if m.ContextWindow != 1000000 || m.MaxOutput != 131072 || !m.Reasoning || !m.AdaptiveThinkingCompat {
286			t.Errorf("%s metadata = %+v", provider, m)
287		}
288		if m.PriceInput != 0.3 || m.PriceOutput != 1.2 || m.PriceCacheRead != 0.06 || m.PriceTierInputTokens != 512000 || m.PriceInputAbove != 0.6 || m.PriceOutputAbove != 2.4 || m.PriceCacheReadAbove != 0.12 {
289			t.Errorf("%s pricing = %+v", provider, m)
290		}
291	}
292
293	c := NewAnthropicCompat("minimax", "x", "https://api.minimax.io/anthropic").(*anthropicClient)
294	temp := float32(0.7)
295	wire, err := c.buildRequest(Request{
296		Model:       "MiniMax-M3",
297		Reasoning:   "high",
298		Temperature: &temp,
299		Messages:    []Message{{Role: RoleUser, Content: []Content{TextBlock{Text: "hi"}}}},
300	})
301	if err != nil {
302		t.Fatal(err)
303	}
304	if wire.Thinking == nil || wire.Thinking.Type != "adaptive" || wire.Thinking.BudgetTokens != 0 {
305		t.Fatalf("want adaptive thinking without a budget, got %+v", wire.Thinking)
306	}
307	if wire.OutputConfig != nil {
308		t.Fatalf("MiniMax-M3 must not send output_config, got %+v", wire.OutputConfig)
309	}
310	if wire.Temperature == nil || *wire.Temperature != temp {
311		t.Fatalf("MiniMax-M3 should keep temperature, got %v", wire.Temperature)
312	}
313}
314
315func TestAnthropicAdaptiveThinking(t *testing.T) {
316	c := NewAnthropic("x", "").(*anthropicClient)
317	temp := float32(0.7)
318
319	// Opus 5 -> adaptive thinking, effort set, no budget, no temperature.
320	wire, err := c.buildRequest(Request{
321		Model:       "claude-opus-5",
322		Reasoning:   "high",
323		Temperature: &temp,
324		Messages:    []Message{{Role: RoleUser, Content: []Content{TextBlock{Text: "hi"}}}},
325	})
326	if err != nil {
327		t.Fatal(err)
328	}
329	if wire.Thinking == nil || wire.Thinking.Type != "adaptive" {
330		t.Fatalf("want adaptive thinking, got %+v", wire.Thinking)
331	}
332	if wire.Thinking.BudgetTokens != 0 {
333		t.Fatalf("adaptive must not send budget_tokens, got %d", wire.Thinking.BudgetTokens)
334	}
335	if wire.OutputConfig == nil || wire.OutputConfig.Effort != "high" {
336		t.Fatalf("want effort=high, got %+v", wire.OutputConfig)
337	}
338	if wire.Temperature != nil {
339		t.Fatalf("adaptive must drop temperature, got %v", *wire.Temperature)
340	}
341
342	// maximum -> xhigh effort.
343	wire, err = c.buildRequest(Request{
344		Model:     "claude-opus-5",
345		Reasoning: "maximum",
346		Messages:  []Message{{Role: RoleUser, Content: []Content{TextBlock{Text: "hi"}}}},
347	})
348	if err != nil {
349		t.Fatal(err)
350	}
351	if wire.OutputConfig == nil || wire.OutputConfig.Effort != "xhigh" {
352		t.Fatalf("want effort=xhigh, got %+v", wire.OutputConfig)
353	}
354
355	// max is a separate native tier above xhigh on adaptive models.
356	wire, err = c.buildRequest(Request{
357		Model:     "claude-opus-5",
358		Reasoning: "max",
359		Messages:  []Message{{Role: RoleUser, Content: []Content{TextBlock{Text: "hi"}}}},
360	})
361	if err != nil {
362		t.Fatal(err)
363	}
364	if wire.OutputConfig == nil || wire.OutputConfig.Effort != "max" {
365		t.Fatalf("want effort=max, got %+v", wire.OutputConfig)
366	}
367
368	// Sonnet 5 -> adaptive thinking, effort set, no budget, no temperature.
369	wire, err = c.buildRequest(Request{
370		Model:       "claude-sonnet-5",
371		Reasoning:   "high",
372		Temperature: &temp,
373		Messages:    []Message{{Role: RoleUser, Content: []Content{TextBlock{Text: "hi"}}}},
374	})
375	if err != nil {
376		t.Fatal(err)
377	}
378	if wire.Thinking == nil || wire.Thinking.Type != "adaptive" {
379		t.Fatalf("want adaptive thinking for Sonnet 5, got %+v", wire.Thinking)
380	}
381	if wire.Thinking.BudgetTokens != 0 {
382		t.Fatalf("Sonnet 5 adaptive must not send budget_tokens, got %d", wire.Thinking.BudgetTokens)
383	}
384	if wire.OutputConfig == nil || wire.OutputConfig.Effort != "high" {
385		t.Fatalf("want Sonnet 5 effort=high, got %+v", wire.OutputConfig)
386	}
387	if wire.Temperature != nil {
388		t.Fatalf("Sonnet 5 adaptive must drop temperature, got %v", *wire.Temperature)
389	}
390
391	// Opus 4.5 -> budget-based thinking, no output_config, temperature kept.
392	wire, err = c.buildRequest(Request{
393		Model:       "claude-opus-4-5",
394		Reasoning:   "high",
395		Temperature: &temp,
396		Messages:    []Message{{Role: RoleUser, Content: []Content{TextBlock{Text: "hi"}}}},
397	})
398	if err != nil {
399		t.Fatal(err)
400	}
401	if wire.Thinking == nil || wire.Thinking.Type != "enabled" || wire.Thinking.BudgetTokens <= 0 {
402		t.Fatalf("want budget thinking, got %+v", wire.Thinking)
403	}
404	if wire.OutputConfig != nil {
405		t.Fatalf("budget models must not send output_config, got %+v", wire.OutputConfig)
406	}
407	if wire.Temperature == nil || *wire.Temperature != temp {
408		t.Fatalf("budget model should keep temperature, got %v", wire.Temperature)
409	}
410}
411
412func TestAnthropicBuildRequestStripsAssistantImages(t *testing.T) {
413	c := NewAnthropic("x", "").(*anthropicClient)
414	wire, err := c.buildRequest(Request{
415		Model: "claude-sonnet-4-5",
416		Messages: []Message{
417			{Role: RoleUser, Content: []Content{TextBlock{Text: "make an image"}}},
418			{Role: RoleAssistant, Content: []Content{
419				TextBlock{Text: "done"},
420				ImageBlock{MimeType: "image/png", Data: []byte("png")},
421				TextBlock{Text: "Saved image: `zut-gemini-image-x.png`"},
422			}},
423			{Role: RoleUser, Content: []Content{TextBlock{Text: "hello"}}},
424		},
425	})
426	if err != nil {
427		t.Fatal(err)
428	}
429	if len(wire.Messages) != 3 {
430		t.Fatalf("messages=%d", len(wire.Messages))
431	}
432	assistant := wire.Messages[1]
433	if assistant.Role != "assistant" {
434		t.Fatalf("role=%q", assistant.Role)
435	}
436	if len(assistant.Content) != 2 {
437		t.Fatalf("assistant content=%+v", assistant.Content)
438	}
439	for _, b := range assistant.Content {
440		if _, ok := b.(anthImageBlock); ok {
441			t.Fatalf("assistant image block was not stripped: %+v", assistant.Content)
442		}
443	}
444}
445
446func TestAnthropicStreamHappyPath(t *testing.T) {
447	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
448		w.Header().Set("content-type", "text/event-stream")
449		w.WriteHeader(http.StatusOK)
450		fl, _ := w.(http.Flusher)
451		write := func(s string) {
452			_, _ = w.Write([]byte(s))
453			if fl != nil {
454				fl.Flush()
455			}
456		}
457		write("event: message_start\ndata: {\"message\":{\"usage\":{\"input_tokens\":10,\"output_tokens\":0}}}\n\n")
458		write("event: content_block_start\ndata: {\"index\":0,\"content_block\":{\"type\":\"text\",\"text\":\"\"}}\n\n")
459		write("event: content_block_delta\ndata: {\"index\":0,\"delta\":{\"type\":\"text_delta\",\"text\":\"hi\"}}\n\n")
460		write("event: content_block_stop\ndata: {\"index\":0}\n\n")
461		write("event: message_delta\ndata: {\"delta\":{\"stop_reason\":\"end_turn\"},\"usage\":{\"output_tokens\":5}}\n\n")
462		write("event: message_stop\ndata: {}\n\n")
463	}))
464	defer srv.Close()
465
466	c := NewAnthropic("x", srv.URL)
467	evs, err := c.Stream(context.Background(), Request{Model: "claude-sonnet-4-5"})
468	if err != nil {
469		t.Fatal(err)
470	}
471	var gotText string
472	var done EventDone
473	for ev := range evs {
474		switch e := ev.(type) {
475		case EventTextDelta:
476			gotText += e.Delta
477		case EventDone:
478			done = e
479		}
480	}
481	if gotText != "hi" {
482		t.Fatalf("text=%q", gotText)
483	}
484	if done.Stop != StopEnd {
485		t.Fatalf("stop=%v", done.Stop)
486	}
487}
488
489func TestClaudeOpus5Catalog(t *testing.T) {
490	m, err := FindModel("anthropic", "claude-opus-5")
491	if err != nil {
492		t.Fatal(err)
493	}
494	if m.DisplayName != "Claude Opus 5" || m.ContextWindow != 1000000 || m.MaxOutput != 128000 || !m.Reasoning || !m.AdaptiveThinking || m.Speculative {
495		t.Fatalf("unexpected Opus 5 model: %+v", m)
496	}
497	if m.PriceInput != 5 || m.PriceOutput != 25 || m.PriceCacheRead != 0.5 || m.PriceCacheWrite != 6.25 {
498		t.Fatalf("unexpected Opus 5 pricing: %+v", m)
499	}
500}
501
502func TestClaudeSonnet5Catalog(t *testing.T) {
503	m, err := FindModel("anthropic", "claude-sonnet-5")
504	if err != nil {
505		t.Fatal(err)
506	}
507	if m.DisplayName != "Claude Sonnet 5" || m.ContextWindow != 1000000 || m.MaxOutput != 128000 || !m.Reasoning || !m.AdaptiveThinking {
508		t.Fatalf("unexpected Sonnet 5 model: %+v", m)
509	}
510	if m.PriceInput != 2 || m.PriceOutput != 10 || m.PriceCacheRead != 0.2 || m.PriceCacheWrite != 2.5 {
511		t.Fatalf("unexpected Sonnet 5 pricing: %+v", m)
512	}
513}
514
515func TestOpenAICompatAnthropicReasoningEffort(t *testing.T) {
516	c := NewOpenRouter("token", "").(*openaiClient)
517	wire, err := c.buildRequest(Request{
518		Model:     "anthropic/claude-opus-4.8",
519		Reasoning: "maximum",
520		Messages:  []Message{{Role: RoleUser, Content: []Content{TextBlock{Text: "hi"}}}},
521	})
522	if err != nil {
523		t.Fatal(err)
524	}
525	if wire.ReasoningEffort != "xhigh" {
526		t.Fatalf("want xhigh for adaptive Anthropic model over OpenAI-compatible wire, got %q", wire.ReasoningEffort)
527	}
528
529	wire, err = c.buildRequest(Request{
530		Model:     "anthropic/claude-sonnet-5",
531		Reasoning: "maximum",
532		Messages:  []Message{{Role: RoleUser, Content: []Content{TextBlock{Text: "hi"}}}},
533	})
534	if err != nil {
535		t.Fatal(err)
536	}
537	if wire.ReasoningEffort != "xhigh" {
538		t.Fatalf("want xhigh for Claude Sonnet 5 over OpenAI-compatible wire, got %q", wire.ReasoningEffort)
539	}
540
541	wire, err = c.buildRequest(Request{
542		Model:     "gpt-5.1",
543		Reasoning: "maximum",
544		Messages:  []Message{{Role: RoleUser, Content: []Content{TextBlock{Text: "hi"}}}},
545	})
546	if err != nil {
547		t.Fatal(err)
548	}
549	if wire.ReasoningEffort != "high" {
550		t.Fatalf("want generic OpenAI-compatible maximum clamped to high, got %q", wire.ReasoningEffort)
551	}
552}
553
554func TestOpenAIBuildRequestSkipsReasoningOnlyAssistantMessages(t *testing.T) {
555	c := NewKimi("token", "").(*openaiClient)
556	wire, err := c.buildRequest(Request{
557		Model: "kimi-for-coding",
558		Messages: []Message{
559			{Role: RoleUser, Content: []Content{TextBlock{Text: "first"}}},
560			{Role: RoleAssistant, Content: []Content{ReasoningBlock{Summary: "thinking only"}}},
561			{Role: RoleUser, Content: []Content{TextBlock{Text: "second"}}},
562		},
563	})
564	if err != nil {
565		t.Fatal(err)
566	}
567	for i, msg := range wire.Messages {
568		if msg.Role == "assistant" && msg.Content == nil && len(msg.ToolCalls) == 0 {
569			t.Fatalf("message %d is empty assistant: %+v", i, msg)
570		}
571	}
572	if got := len(wire.Messages); got != 2 {
573		t.Fatalf("messages=%d want 2 after skipping reasoning-only assistant", got)
574	}
575}
576
577func TestOpenAIStreamHappyPath(t *testing.T) {
578	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
579		w.Header().Set("content-type", "text/event-stream")
580		w.WriteHeader(http.StatusOK)
581		fl, _ := w.(http.Flusher)
582		write := func(s string) {
583			_, _ = w.Write([]byte(s))
584			if fl != nil {
585				fl.Flush()
586			}
587		}
588		write("data: {\"choices\":[{\"index\":0,\"delta\":{\"content\":\"hel\"},\"finish_reason\":null}]}\n\n")
589		write("data: {\"choices\":[{\"index\":0,\"delta\":{\"content\":\"lo\"},\"finish_reason\":\"stop\"}]}\n\n")
590		write("data: {\"choices\":[],\"usage\":{\"prompt_tokens\":8,\"completion_tokens\":2}}\n\n")
591		write("data: [DONE]\n\n")
592	}))
593	defer srv.Close()
594
595	c := NewOpenAI("x", srv.URL)
596	evs, err := c.Stream(context.Background(), Request{Model: "gpt-5"})
597	if err != nil {
598		t.Fatal(err)
599	}
600	var gotText string
601	var done EventDone
602	for ev := range evs {
603		switch e := ev.(type) {
604		case EventTextDelta:
605			gotText += e.Delta
606		case EventDone:
607			done = e
608		}
609	}
610	if gotText != "hello" {
611		t.Fatalf("text=%q", gotText)
612	}
613	if done.Stop != StopEnd {
614		t.Fatalf("stop=%v", done.Stop)
615	}
616}
617
618func TestDiscoverOpenRouter(t *testing.T) {
619	const body = `{"data":[
620		{"id":"x/full","pricing":{"prompt":"0.000003","completion":"0.000015"},
621		 "context_length":200000,"top_provider":{"max_completion_tokens":64000},
622		 "supported_parameters":["reasoning"]},
623		{"id":"x/fallback","top_provider":{"context_length":128000}},
624		{"id":""}]}`
625	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
626		_, _ = w.Write([]byte(body))
627	}))
628	defer srv.Close()
629
630	models, err := DiscoverOpenRouter(context.Background(), srv.URL)
631	if err != nil {
632		t.Fatal(err)
633	}
634	if len(models) != 2 { // empty id dropped
635		t.Fatalf("want 2 models, got %d", len(models))
636	}
637	// per-token USD -> per-1M; reasoning from supported_parameters.
638	if m := models[0]; m.Provider != "openrouter" || m.ContextWindow != 200000 ||
639		m.MaxOutput != 64000 || !m.Reasoning || m.PriceInput != 3 || m.PriceOutput != 15 {
640		t.Errorf("model[0]: %+v", m)
641	}
642	// context falls back to top_provider; no reasoning.
643	if m := models[1]; m.ContextWindow != 128000 || m.MaxOutput != 0 || m.Reasoning {
644		t.Errorf("model[1]: %+v", m)
645	}
646}
647
648func TestGPT56CatalogEntries(t *testing.T) {
649	cases := []struct {
650		provider   string
651		id         string
652		context    int
653		priceInput float64
654		priceOut   float64
655		cacheRead  float64
656		cacheWrite float64
657	}{
658		{"openai", "gpt-5.6-luna", 500000, 1, 6, 0.1, 1.25},
659		{"openai", "gpt-5.6-sol", 500000, 5, 30, 0.5, 6.25},
660		{"openai", "gpt-5.6-terra", 500000, 2.5, 15, 0.25, 3.125},
661		{"openai-codex", "gpt-5.6-luna", 500000, 1, 6, 0.1, 1.25},
662		{"openai-codex", "gpt-5.6-sol", 500000, 5, 30, 0.5, 6.25},
663		{"openai-codex", "gpt-5.6-terra", 500000, 2.5, 15, 0.25, 3.125},
664		{"azure-openai-responses", "gpt-5.6-luna", 1050000, 1, 6, 0.1, 1.25},
665		{"azure-openai-responses", "gpt-5.6-sol", 1050000, 5, 30, 0.5, 6.25},
666		{"azure-openai-responses", "gpt-5.6-terra", 1050000, 2.5, 15, 0.25, 3.125},
667		{"vercel-ai-gateway", "openai/gpt-5.6-luna", 1050000, 1, 6, 0.1, 1.25},
668		{"vercel-ai-gateway", "openai/gpt-5.6-sol", 1050000, 5, 30, 0.5, 6.25},
669		{"vercel-ai-gateway", "openai/gpt-5.6-terra", 1050000, 2.5, 15, 0.25, 3.125},
670	}
671	for _, tc := range cases {
672		m, err := FindModel(tc.provider, tc.id)
673		if err != nil {
674			t.Fatal(err)
675		}
676		if m.ContextWindow != tc.context || m.MaxOutput != 128000 || !m.Reasoning {
677			t.Fatalf("%s/%s limits: %+v", tc.provider, tc.id, m)
678		}
679		if m.PriceInput != tc.priceInput || m.PriceOutput != tc.priceOut || m.PriceCacheRead != tc.cacheRead || m.PriceCacheWrite != tc.cacheWrite {
680			t.Fatalf("%s/%s prices: %+v", tc.provider, tc.id, m)
681		}
682		if tc.provider == "openai-codex" && m.Speculative {
683			t.Fatalf("%s/%s is speculative", tc.provider, tc.id)
684		}
685	}
686}
687
688// TestOpenCodeGoCatalog pins entries added from the vendor's current Go
689// model lineup (https://opencode.ai/docs/go).
690func TestOpenCodeGoCatalog(t *testing.T) {
691	cases := []struct {
692		id        string
693		context   int
694		maxOut    int
695		priceIn   float64
696		priceOut  float64
697		cacheRead float64
698	}{
699		{"gpt-5.6-luna", 1050000, 128000, 0.2, 1.2, 0.02},
700		{"grok-4.5", 500000, 500000, 2, 6, 0.3},
701		{"kimi-k3", 1048576, 131072, 3, 15, 0.3},
702	}
703	for _, tc := range cases {
704		m, err := FindModel("opencode-go", tc.id)
705		if err != nil {
706			t.Fatal(err)
707		}
708		if m.ContextWindow != tc.context || m.MaxOutput != tc.maxOut || !m.Reasoning {
709			t.Fatalf("opencode-go/%s limits: %+v", tc.id, m)
710		}
711		if m.PriceInput != tc.priceIn || m.PriceOutput != tc.priceOut || m.PriceCacheRead != tc.cacheRead {
712			t.Fatalf("opencode-go/%s prices: %+v", tc.id, m)
713		}
714		if m.BaseURL != "https://opencode.ai/zen/go/v1" {
715			t.Fatalf("opencode-go/%s baseURL: %s", tc.id, m.BaseURL)
716		}
717	}
718
719	luna, err := FindModel("opencode-go", "gpt-5.6-luna")
720	if err != nil {
721		t.Fatal(err)
722	}
723	if luna.API != APIResponses || luna.PriceCacheWrite != 0.25 {
724		t.Fatalf("opencode-go/gpt-5.6-luna API or cache-write price: %+v", luna)
725	}
726	if luna.PriceTierInputTokens != 272000 || luna.PriceInputAbove != 0.4 || luna.PriceOutputAbove != 1.8 || luna.PriceCacheReadAbove != 0.04 || luna.PriceCacheWriteAbove != 0.5 {
727		t.Fatalf("opencode-go/gpt-5.6-luna long-context prices: %+v", luna)
728	}
729}
730
731// TestOpenAIOmitsZeroMaxTokens guards against sending max_tokens: 0 for
732// discovered models that don't advertise an output cap (MaxOutput == 0).
733func TestOpenAIOmitsZeroMaxTokens(t *testing.T) {
734	SetLiveModels([]Model{
735		{Provider: "openrouter", ID: "vendor/no-cap", DisplayName: "No Cap"},
736		{Provider: "openrouter", ID: "vendor/reason-no-cap", DisplayName: "Reason No Cap", Reasoning: true},
737		{Provider: "openrouter", ID: "vendor/capped", DisplayName: "Capped", MaxOutput: 4096},
738	})
739	defer SetLiveModels(nil)
740
741	c := NewOpenAI("x", "").(*openaiClient)
742
743	got, err := c.buildRequest(Request{Model: "vendor/no-cap"})
744	if err != nil {
745		t.Fatal(err)
746	}
747	if got.MaxTokens != nil {
748		t.Errorf("expected max_tokens omitted, got %d", *got.MaxTokens)
749	}
750
751	got, err = c.buildRequest(Request{Model: "vendor/reason-no-cap"})
752	if err != nil {
753		t.Fatal(err)
754	}
755	if got.MaxCompletionTok != nil {
756		t.Errorf("expected max_completion_tokens omitted, got %d", *got.MaxCompletionTok)
757	}
758
759	got, err = c.buildRequest(Request{Model: "vendor/capped"})
760	if err != nil {
761		t.Fatal(err)
762	}
763	if got.MaxTokens == nil || *got.MaxTokens != 4096 {
764		t.Errorf("expected max_tokens 4096, got %v", got.MaxTokens)
765	}
766}

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 1

Tags 476

↑↓ moveenter openesc close