diff --git a/internal/provider/grok.go b/internal/provider/grok.go index b3c2b78fd..02808406b 100644 --- a/internal/provider/grok.go +++ b/internal/provider/grok.go @@ -759,18 +759,22 @@ func grokWindowLabel(n int) string { // grokWindowed splits a requested id into the model Grok lists and the // window the id names: grok-4.7 is grok-4.7 at its default window (0, no -// header), grok-4.7@500k is grok-4.7 at 500000 tokens. +// header), grok-4.7@500k is grok-4.7 at 500000 tokens. A size no multiple +// of 1000 is the window itself, as grokWindowLabel writes it (131072). func grokWindowed(id string) (string, int) { model, size, ok := strings.Cut(id, "@") if !ok { return id, 0 } - k, ok := strings.CutSuffix(size, "k") + k, isK := strings.CutSuffix(size, "k") n, err := strconv.Atoi(k) - if !ok || err != nil || n <= 0 || model == "" { + if err != nil || n <= 0 || model == "" { return id, 0 } - return model, n * 1000 + if isK { + n *= 1000 + } + return model, n } // grokRefreshMargin is how close to its expiry a borrowed token has the CLI diff --git a/internal/provider/grok_price_test.go b/internal/provider/grok_price_test.go index 0aaf9792e..a07f76664 100644 --- a/internal/provider/grok_price_test.go +++ b/internal/provider/grok_price_test.go @@ -115,3 +115,30 @@ func TestCursorGrokFastPrice(t *testing.T) { } } } + +// A window the id names without a k (grokWindowLabel writes a window that +// no multiple of 1000 is as bare digits) is priced as its model, as one +// spelled with a k is. +func TestGrokWindowWithoutKListPrice(t *testing.T) { + home := t.TempDir() + t.Setenv("HOME", home) + t.Setenv("USERPROFILE", home) + t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config")) + t.Setenv("XDG_CACHE_HOME", filepath.Join(home, ".cache")) + os.MkdirAll(filepath.Dir(catalog.CachePath()), 0o755) + os.WriteFile(catalog.CachePath(), []byte(`{ + "xai":{"id":"xai","models":{ + "grok-4.7":{"id":"grok-4.7","cost":{"input":2,"output":6,"cache_read":0.5}}}}}`), 0o644) + catalog.Reset() + t.Cleanup(catalog.Reset) + + grok := Provider{ID: "grok", Name: "Grok (SuperGrok)"} + for _, id := range []string{"grok-4.7@131072", "x-ai/grok-4.7@131072"} { + if pr, ok := grok.ListPrice(id); !ok || pr.Input != 2 || pr.Output != 6 || pr.CacheRead != 0.5 { + t.Errorf("ListPrice(%q) = %+v, %v; want grok-4.7's $2/$6/$0.5", id, pr, ok) + } + if pr, ok := MakerPrice(id); !ok || pr.Input != 2 { + t.Errorf("MakerPrice(%q) = %+v, %v; want grok-4.7's", id, pr, ok) + } + } +} diff --git a/internal/provider/grok_test.go b/internal/provider/grok_test.go index 5889fd182..485a447e4 100644 --- a/internal/provider/grok_test.go +++ b/internal/provider/grok_test.go @@ -84,6 +84,29 @@ func TestGrokSignsWindow(t *testing.T) { } } +// A window the id names without a k (131072: grokWindowLabel writes a +// window that no multiple of 1000 is as bare digits) is signed as that +// window at the model Grok lists, not sent as an id of its own. +func TestGrokSignsWindowWithoutK(t *testing.T) { + home := t.TempDir() + grokSignedIn(t, home, "me@x.ai") + acct := &Account{Agent: "grok"} + grokSigned(acct, home) + for _, c := range []struct{ model, sent, window string }{ + {"grok-4.7@131072", "grok-4.7", "131072"}, + {"grok-4.7@500k", "grok-4.7", "500000"}, + } { + body := []byte(`{"model":"` + c.model + `","input":"hi"}`) + req, _ := http.NewRequest("POST", "http://grok.invalid/responses", strings.NewReader(string(body))) + if err := acct.sign(context.Background(), req, body); err != nil { + t.Fatal(err) + } + if req.Header.Get("x-grok-model-override") != c.sent || req.Header.Get("x-grok-context-window") != c.window { + t.Errorf("%s: headers = %v, want %s at window %s", c.model, req.Header, c.sent, c.window) + } + } +} + // A request is signed with the sign-in of its account's home, as the CLI // signs its own. func TestGrokSigns(t *testing.T) { diff --git a/internal/provider/models.go b/internal/provider/models.go index b020d9743..33aca8012 100644 --- a/internal/provider/models.go +++ b/internal/provider/models.go @@ -1361,8 +1361,9 @@ var grokEffort = regexp.MustCompile(`^((?:.*/)?grok-[0-9][^/]*?)-(?:minimal|low| // grokWindow is a Grok id offered at a context window of its own // ("grok-4.7@500k", grokContextModels and the grok plugin's windowsOf): the -// same model, asked for at a longer window. -var grokWindow = regexp.MustCompile(`^((?:.*/)?grok-[0-9][^/@]*)@[0-9]+k$`) +// same model, asked for at a longer window. A window no multiple of 1000 +// the id names without the k (grokWindowLabel, grokWindowed). +var grokWindow = regexp.MustCompile(`^((?:.*/)?grok-[0-9][^/@]*)@[0-9]+k?$`) // pricedNames are the ids a model is priced by, in order: its own, then, // for a Grok id named at an effort, the model it is that effort of — the