laomao04_65777 on Discord: Zen's free models answered 429 "Free usage limit reached. Free models work in OpenCode" to the built-in OpenCode Zen provider, while real OpenCode 2 on the same machine was served. magpie sent OpenCode 1.x's shape. It now sends what OpenCode 2.2.5 sent in a captured request (internal/provider/testdata/opencode-2.2.5-chat.json, from a recording proxy), as the zen-free plugin does since plugins d62193c and 8501988: - User-Agent opencode/latest/<v>/cli. <v> is the version OpenCode's updater (opencode.ai/update/api/latest/cli/npm) names, never below 2.2.5 and never anything but a plain x.y.z. It is read in the background at most every 6h, with a 3s timeout and no redirects. - x-opencode-project is OpenCode's project id for the folder the agent says it works in: sha1 of git-remote:host/path (origin, else the first remote; worktree .git files followed), else of directory:<root or folder>. It is no longer "global". - b3 and traceparent: one trace a conversation, a new span a request. - The session id goes in x-opencode-session, x-opencode-session-id, x-session-affinity and x-session-id. Accept is */*, and x-opencode-request is gone. - On a free request the agent's own headers (Claude Code's x-stainless-*, Codex's originator) are dropped. Only Content-Type and Anthropic-Version and Anthropic-Beta are kept. - The core tools are edit, glob, grep, read and shell with 2.2.5's schemas, strict false on Chat. Claude Code's Read, Edit, Glob and Grep are offered under those names. Its Bash is no longer bash: shell is a stub. - OpenCode's <env> block, word for word, for the agent's folder, is added to the system prompt unless it has one already (OpenCode's own). - Paths are the v2 catalog's: /inference/openai/v1 for Chat and Responses, and /inference/anthropic/v1 for Messages. The models list is unchanged. Every route takes this: chat, responses and messages through forwardOnce, the Test button's probe (tiny/zenFreeProbe), and the models list (listHeadersAt: UA and x-opencode-client). Sibling: paid Zen and OpenCode Go share OpenCodeClient, so they get 2.2.5's headers. Their /zen/v1 and /zen/go/v1 paths are left as they are: #4740. Run on mote (mote-run) at this tree: - gofmt -l . is clean. - GOOS=darwin and windows go vet ./... pass. GOOS=linux go vet passes for internal/provider and internal/gateway. The whole-tree Linux vet stops in Wails' cgo when run from macOS, and in WSL for want of gtk4, before it reaches this change. - go test ./internal/provider ./internal/gateway -count=1 -timeout 45m: ok provider (154s) and ok gateway (715s) on base788057340. After the rebase ontoa5a9cdf7b, which touched gateway.go and ir.go, the same tree passed again: ok provider (138s) and ok gateway (816s), with GOOS darwin, linux and windows go vet of both packages. - Rebased again onto4c815ac2a, which touched search.go and provider.go. At that head gofmt and the three-OS vet of both packages are clean, and -run 'OpenCode|Zen|Search|Searcher' passes in both packages. The full suites were not rerun after this last rebase. Broken by hand, each turning a test red (provider and gateway, -run 'OpenCode|Zen', on mote): - version 1.19.0: TestOpenCodeClientIs225 and TestOpenCodeVersionFromUpdater. - the old /inference/v1 path: TestOpenCodeFreeURL, TestZenFreeAsOpenCode225, TestZenFreeAtInferenceURL and TestZenPublicTestAsksFreeModels. - caller headers kept: TestZenFreeAsOpenCode225. - no env block: TestZenFreeAsOpenCode225. - no strict: TestZenFreeAtInferenceURL. - project "global": TestOpenCodeClientIs225, TestOpenCodeClientHeaders and TestZenFreeAsOpenCode225. - the same span on every request: TestOpenCodeClientIs225 and TestOpenCodeClientHeaders. - no 2.2.5 floor: TestOpenCodeVersionFromUpdater. - a changed read schema: TestOpenCodeToolsAre225s. Not tried: - A real 200 from a free model: this egress IP's free allowance is spent. The captured 2.2.5 request itself got 429. - Header order and case: Go's HTTP/1 writes headers sorted, and HTTP/2 lowercases them, so OpenCode's order cannot be copied. - Windows' project id against real OpenCode on Windows.
110 lines
4.7 KiB
Go
110 lines
4.7 KiB
Go
package gateway
|
|
|
|
import (
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"regexp"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// OpenCode Zen's free models answer 403 "OpenCode's free tier can only be
|
|
// used from within OpenCode", and since late October 2026 429 "Free models
|
|
// work in OpenCode" to 1.x's shape (#3491), unless the request says it is
|
|
// OpenCode 2's: its User-Agent and a session id of its form, as OpenCode
|
|
// sends them to a provider whose id starts with "opencode". Requests to
|
|
// OpenCode's gateway carry them, those to anyone else don't, and the body
|
|
// goes as the caller sent it either way.
|
|
func TestOpenCodeClientHeaders(t *testing.T) {
|
|
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
|
|
t.Setenv("XDG_CACHE_HOME", t.TempDir())
|
|
for _, p := range []provider.Provider{
|
|
{ID: "zen", Name: "Zen", Key: "public", Chat: "https://opencode.ai/zen/v1", Models: []string{"kimi-k2.6"}},
|
|
{ID: "other", Name: "Other", Key: "k", Chat: "https://other.test/v1", Models: []string{"kimi-k2.6"}},
|
|
} {
|
|
if err := provider.Save(p); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
type seen struct {
|
|
h http.Header
|
|
body string
|
|
}
|
|
got := map[string][]seen{}
|
|
s := New()
|
|
s.client = &http.Client{Transport: countTransport(func(r *http.Request) (*http.Response, error) {
|
|
b, _ := io.ReadAll(r.Body)
|
|
r.Body.Close()
|
|
got[r.URL.Host] = append(got[r.URL.Host], seen{r.Header.Clone(), string(b)})
|
|
return &http.Response{StatusCode: 200, Header: http.Header{"Content-Type": {"application/json"}},
|
|
Body: io.NopCloser(strings.NewReader(`{"id":"c","object":"chat.completion","model":"m","choices":[{"index":0,"message":{"role":"assistant","content":"pong"},"finish_reason":"stop"}]}`))}, nil
|
|
})}
|
|
send := func(model, affinity string) {
|
|
t.Helper()
|
|
req := httptest.NewRequest("POST", "/v1/chat/completions", strings.NewReader(`{"model":"`+model+`","messages":[{"role":"user","content":"Say pong"}],`+
|
|
`"tools":[{"type":"function","function":{"name":"bash","parameters":{"type":"object"}}},{"type":"function","function":{"name":"read","parameters":{"type":"object"}}}]}`))
|
|
req.Header.Set("User-Agent", "some-agent/1.0")
|
|
if affinity != "" {
|
|
req.Header.Set("x-session-affinity", affinity)
|
|
}
|
|
rec := httptest.NewRecorder()
|
|
s.Handler().ServeHTTP(rec, req)
|
|
if rec.Code != 200 {
|
|
t.Fatalf("%s: %d %s", model, rec.Code, rec.Body)
|
|
}
|
|
}
|
|
send("zen/kimi-k2.6", "")
|
|
send("zen/kimi-k2.6", "")
|
|
send("zen/kimi-k2.6", "ses_ecc5d8f28ffe9Cy14V8Em7lI2u") // the agent's own session id names the session, and the header itself stays behind
|
|
send("other/kimi-k2.6", "")
|
|
|
|
zen, other := got["opencode.ai"], got["other.test"]
|
|
if len(zen) != 3 || len(other) != 1 {
|
|
t.Fatalf("requests: %v", got)
|
|
}
|
|
id := regexp.MustCompile(`^ses_[0-9a-f]{12}[0-9A-Za-z]{14}$`)
|
|
for i, r := range zen {
|
|
if ua := r.h.Get("User-Agent"); ua != provider.OpenCodeUserAgent() {
|
|
t.Errorf("zen %d: User-Agent %q", i, ua)
|
|
}
|
|
if !id.MatchString(r.h.Get("x-opencode-session")) || r.h.Get("x-opencode-request") != "" {
|
|
t.Errorf("zen %d: session %q request %q", i, r.h.Get("x-opencode-session"), r.h.Get("x-opencode-request"))
|
|
}
|
|
// OpenCode 2.2.5 sends the session in all four
|
|
for _, h := range []string{"x-opencode-session-id", "x-session-affinity", "X-Session-Id"} {
|
|
if v := r.h.Get(h); v != r.h.Get("x-opencode-session") {
|
|
t.Errorf("zen %d: %s %q, x-opencode-session %q", i, h, v, r.h.Get("x-opencode-session"))
|
|
}
|
|
}
|
|
if r.h.Get("x-opencode-client") != "cli" || len(r.h.Get("x-opencode-project")) != 40 || r.h.Get("b3") == "" || r.h.Get("traceparent") == "" {
|
|
t.Errorf("zen %d: client %q project %q", i, r.h.Get("x-opencode-client"), r.h.Get("x-opencode-project"))
|
|
}
|
|
if r.h.Get("Authorization") != "Bearer public" {
|
|
t.Errorf("zen %d: Authorization %q", i, r.h.Get("Authorization"))
|
|
}
|
|
if r.body != other[0].body {
|
|
t.Errorf("zen %d: body changed:\n%s\nvs\n%s", i, r.body, other[0].body)
|
|
}
|
|
}
|
|
// one conversation, one session and trace; each request its own span
|
|
if zen[0].h.Get("x-opencode-session") != zen[1].h.Get("x-opencode-session") || zen[0].h.Get("traceparent")[:35] != zen[1].h.Get("traceparent")[:35] ||
|
|
zen[0].h.Get("traceparent") == zen[1].h.Get("traceparent") {
|
|
t.Errorf("session %q %q, traceparent %q %q", zen[0].h.Get("x-opencode-session"), zen[1].h.Get("x-opencode-session"),
|
|
zen[0].h.Get("traceparent"), zen[1].h.Get("traceparent"))
|
|
}
|
|
if s := zen[2].h.Get("x-opencode-session"); s != "ses_ecc5d8f28ffe9Cy14V8Em7lI2u" {
|
|
t.Errorf("OpenCode's own session became %q", s)
|
|
}
|
|
for k := range other[0].h {
|
|
if strings.HasPrefix(strings.ToLower(k), "x-opencode-") {
|
|
t.Errorf("other provider got %s", k)
|
|
}
|
|
}
|
|
if ua := other[0].h.Get("User-Agent"); strings.HasPrefix(ua, "opencode/") {
|
|
t.Errorf("other provider got User-Agent %q", ua)
|
|
}
|
|
}
|