Files
magpie/internal/provider/opencode_client.go
T
yetone 3515d99a9c gateway: a request to OpenCode's gateway (Zen or Go) says it is OpenCode's, as OpenCode itself sends it (the user: free Zen models 403 "OpenCode's free tier can only be used from within OpenCode"; mimicking OpenCode is allowed on this route) — magpie sent User-Agent magpie/<version> and its own conversation id (magpie-…) in x-opencode-session. OpenCode (packages/opencode/src/session/llm/request.ts, for a provider whose id starts with "opencode") sends User-Agent opencode/<version>, x-opencode-session (ses_…), x-opencode-request (msg_…), x-opencode-client and x-opencode-project; every request to opencode.ai now carries them: opencode/1.18.34, the conversation as an id of OpenCode's form (one already ses_… goes as it is, any other is made from it, the same for the same conversation), a fresh msg_… id, cli and global. Prompts and body go as the caller sent them; other providers get none of it. Zen's free tier also asks for OpenCode 1.18.0 or newer (426 otherwise), a streamed request, and tools named bash and read: a live request through the gateway with the anonymous key "public" to mimo-v2.6-flash-free went from 403 to 200 for a streamed one offering bash and read (OpenCode's and Pi's), and stays 403 for Claude Code's Bash/Read or no tools, so the free models stay off the list. TestOpenCodeClientHeaders fails without the change; TestOpenCodeGetsConversationSession now expects OpenCode's form. go vet, go build -tags nogui (darwin, GOOS=linux), GOOS=windows go build, go test -tags nogui ./... pass.
2026-10-02 08:28:35 +08:00

58 lines
2.1 KiB
Go

package provider
import (
"crypto/rand"
"crypto/sha256"
"encoding/hex"
"net/http"
"regexp"
"strings"
)
// OpenCodeVersion is the OpenCode release magpie says it is to OpenCode's
// gateway. Zen's free tier turns away anything older than 1.18.0 (426
// "OpenCode 1.18.0 or newer is required to use the free tier").
const OpenCodeVersion = "1.18.34"
// OpenCodeClient makes a request to OpenCode's gateway (Zen or Go) carry
// what OpenCode itself sends there (packages/opencode/src/session/llm/
// request.ts, for a provider whose id starts with "opencode"): its
// User-Agent, the session in x-opencode-session, the turn in
// x-opencode-request, and x-opencode-client and x-opencode-project. Zen's
// free models are served to no one else ("OpenCode's free tier can only be
// used from within OpenCode"). session names the conversation; one already
// in OpenCode's form (ses_…) goes as it is, any other becomes one, the same
// for the same conversation, which Zen routes and caches by.
func OpenCodeClient(h http.Header, session string) {
h.Set("User-Agent", "opencode/"+OpenCodeVersion)
h.Set("x-opencode-session", openCodeID("ses", session))
h.Set("x-opencode-request", openCodeID("msg", ""))
h.Set("x-opencode-client", "cli")
// OpenCode's project outside a git repository
h.Set("x-opencode-project", "global")
}
var openCodeIDRe = regexp.MustCompile(`^[a-z]{3}_[0-9a-f]{12}[0-9A-Za-z]{14}$`)
const base62 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
// openCodeID is an id as OpenCode makes them (prefix_, 12 hex digits and 14
// base62 ones): seed's own when it is one with this prefix, else made from
// seed, or at random when there is none.
func openCodeID(prefix, seed string) string {
if strings.HasPrefix(seed, prefix+"_") && openCodeIDRe.MatchString(seed) {
return seed
}
var b [32]byte
if seed != "" {
b = sha256.Sum256([]byte(seed))
} else if _, err := rand.Read(b[:]); err != nil {
b = sha256.Sum256([]byte(randomUUID()))
}
id := []byte(prefix + "_" + hex.EncodeToString(b[:6]))
for _, c := range b[6:20] {
id = append(id, base62[int(c)%len(base62)])
}
return string(id)
}