Files
magpie/internal/gateway/codex_opencode_fast_test.go
T
yetone 6ea3b78d4e agent: OpenCode gets <level>-fast variants of a Codex account's GPT models, sent as Codex's /fast; the user's own variants stay (#2379)
OpenCode has no /fast (jiz4oh). A ChatGPT account's own gpt-* model (not
a routing group; catalog.Model.CodexFast, set in magpieModels from
provider.CanFast) is written into opencode.json with each level again as
low-fast … xhigh-fast, or fast alone with no levels. Each asks
service_tier "priority" twice: as an option, which OpenCode 1's
@ai-sdk/openai-compatible sends as it is (read in v1.18.34's
transform.ts and the SDK's getArgs), and in extraBody, which OpenCode 2
sends while it drops an option it doesn't know. The gateway already
sends a Chat request's priority tier to chatgpt.com on Responses and
drops OpenCode 1's stray extraBody field. Other vendors' models, an
OpenAI key's GPT model and groups get no fast variants.

The models block was written whole, so a variant the user added to one
of magpie's models was wiped on every sync. openCodeKept (OpenCode,
DevEco, Air, OpenChamber) keeps a user's variant and one written over
under a name of magpie's; a variant exactly as magpie writes it is
magpie's, so one the model no longer has goes. agent-wiring.md says so.

Run:
- go test -tags nogui -p 1 -count=1 -run 'OpenCode|OpenChamber|Air|DevEco|Deveco|Variant|Theirs|Kept|CodexTier|MagpieModels|Fast' ./internal/agent/: ok
- go test -tags nogui -p 1 -count=1 ./internal/catalog/: ok
- go test -tags nogui -p 1 -count=1 -run 'Fast|Tier|Codex|OpenCode' ./internal/gateway/: ok
- New: TestOpenCodeCodexFastVariants (written config, user variants kept,
  stale one dropped, second sync byte-identical, relay/gpt-5.5 and
  codex/codex-auto-review without fast), TestOpenCodeVariants fast cases,
  TestOpenCodeFastVariantReachesCodex (Chat as OpenCode 1 and 2 send it,
  streamed and whole, against a fake ChatGPT backend: service_tier
  priority, effort kept, no extraBody; none without the variant).
- Broken by hand, each red: CodexFast never set; theirsKept back in
  place of openCodeKept; user variants not read; stale variants kept;
  extraBody not written; gateway's Chat parse not reading priority.
- Real use: the exact opencode.json the test writes, base URL pointed at
  a fake server, in a sandbox HOME, with opencode v2.0.25:
  -m "magpie/codex/gpt-5.5#xhigh-fast" sent reasoning_effort xhigh and
  service_tier priority; #medium sent no tier.
- gofmt -l clean; go vet -tags nogui for GOOS=darwin, linux, windows on
  internal/agent, internal/catalog, internal/gateway: clean.
Not tried: an OpenCode 1.x binary (read from source only), a real
ChatGPT account's priority tier.

Full suite: SUITE_LINE
2026-10-15 23:08:04 +08:00

57 lines
2.7 KiB
Go

package gateway
import (
"net/http"
"net/url"
"testing"
"github.com/tidwall/gjson"
"github.com/yetone/magpie/internal/provider"
)
// #2379 (jiz4oh): OpenCode's fast variant of a Codex account's GPT model
// reaches the ChatGPT backend as Codex's /fast asks it, service_tier
// "priority", beside the variant's reasoning effort. OpenCode 2 sends the
// tier from the variant's extraBody, OpenCode 1 sends both of the
// variant's options as they are, extraBody a field of its own: that one
// never reaches the backend. A variant that isn't fast asks no tier.
func TestOpenCodeFastVariantReachesCodex(t *testing.T) {
codexSignedIn(t)
d := daybreak(t)
// at chatgpt.com, the one host buildResponses sends Codex's tiers to,
// reaching the fake backend
to, _ := url.Parse(provider.CodexBase)
provider.CodexBase = "https://chatgpt.com/backend-api/codex"
s := New()
s.client = &http.Client{Transport: qoderRoundTrip(func(r *http.Request) (*http.Response, error) {
r.URL.Scheme, r.URL.Host = to.Scheme, to.Host
return http.DefaultTransport.RoundTrip(r)
})}
const msgs = `"messages":[{"role":"user","content":"Respond with OK."}]`
for _, c := range []struct{ name, body string }{
{"OpenCode 2, streamed", `{"model":"codex/gpt-daybreak-blue-latest","stream":true,` + msgs + `,"reasoning_effort":"medium","service_tier":"priority"}`},
{"OpenCode 2, whole", `{"model":"codex/gpt-daybreak-blue-latest",` + msgs + `,"reasoning_effort":"medium","service_tier":"priority"}`},
{"OpenCode 1, streamed", `{"model":"codex/gpt-daybreak-blue-latest","stream":true,` + msgs + `,"reasoning_effort":"medium","service_tier":"priority","extraBody":{"service_tier":"priority"}}`},
{"OpenCode 1, whole", `{"model":"codex/gpt-daybreak-blue-latest",` + msgs + `,"reasoning_effort":"medium","service_tier":"priority","extraBody":{"service_tier":"priority"}}`},
} {
r := postFrom(t, s, "opencode", "/v1/chat/completions", c.body)
if r.Tier != "priority" {
t.Errorf("%s: the route says tier %q was asked", c.name, r.Tier)
}
b := d.last(t)
if got := gjson.GetBytes(b, "service_tier").String(); got != "priority" {
t.Errorf("%s: the backend got service_tier %q in %s", c.name, got, b)
}
if got := gjson.GetBytes(b, "reasoning.effort").String(); got != "medium" {
t.Errorf("%s: the backend got effort %q in %s", c.name, got, b)
}
if gjson.GetBytes(b, "extraBody").Exists() {
t.Errorf("%s: extraBody reached the backend: %s", c.name, b)
}
}
postFrom(t, s, "opencode", "/v1/chat/completions", `{"model":"codex/gpt-daybreak-blue-latest",`+msgs+`,"reasoning_effort":"medium"}`)
if b := d.last(t); gjson.GetBytes(b, "service_tier").Exists() {
t.Errorf("a variant that isn't fast asked a tier: %s", b)
}
}