provider: a Grok window the id names without a k is signed and priced as its model

grokContextModels offers each window Grok lists beside the default as
<id>@<N>k, and grokWindowLabel writes a window that is no multiple of
1000 as bare digits (131072). grokWindowed took any size without the k
for no window at all: such an id went to Grok verbatim, as
x-grok-model-override "grok-4.7@131072" with no x-grok-context-window —
a model id Grok doesn't list — and pricedNames's grokWindow gave it no
price either.

What changed
- grokWindowed reads a bare-digit size as the window itself, as the
  label writes it; a k still means thousands.
- grokWindow (pricedNames) takes the window without the k too, so the
  id is priced as its model, as grok-4.7@500k is.

What was run
- New TestGrokSignsWindowWithoutK and TestGrokWindowWithoutKListPrice
  fail on the old code (the id sent verbatim as the override, no window
  header; ListPrice and MakerPrice give nothing) and pass with the
  change.
- go test -tags nogui -count=1 ./internal/provider ok; -race -count=5
  on the Grok tests ok.
- gofmt -l internal/ clean; go vet ./..., GOOS=linux go vet -tags nogui
  ./... and GOOS=windows go vet ./... clean but the known
  trayimage_darwin.go cgo -Wenum-conversion warning; go build,
  GOOS=linux go build -tags nogui and GOOS=windows go build ok;
  go test -tags nogui -count=1 ./... green under a temp HOME
  (TestPluginListSaysMiddleware once hit its 60s bound under the full
  run and passes alone and on a second full run; unrelated to this).
This commit is contained in:
ggbdpq
2026-10-13 10:40:35 +08:00
committed by yetone
parent 4ed379f116
commit 30f7ad0462
4 changed files with 61 additions and 6 deletions
+8 -4
View File
@@ -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
+27
View File
@@ -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)
}
}
}
+23
View File
@@ -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) {
+3 -2
View File
@@ -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