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.
186 lines
6.6 KiB
Go
186 lines
6.6 KiB
Go
package provider
|
|
|
|
import (
|
|
"context"
|
|
"maps"
|
|
"slices"
|
|
"strings"
|
|
|
|
"github.com/yetone/magpie/internal/catalog"
|
|
)
|
|
|
|
// A remote magpie is another computer's magpie gateway, shared on its
|
|
// network (Settings → Network and sharing → Share on local network), as a provider: the
|
|
// computers' agents stay wired by their own magpie, while the providers,
|
|
// routing groups and usage are the one magpie's they all share. Its
|
|
// address serves every API magpie does, so the provider speaks them all
|
|
// — Chat Completions and Responses at <address>/v1, Anthropic's Messages
|
|
// at the address — and a request goes on in the API the agent spoke. Its
|
|
// model list tells, of each model, the APIs its own provider serves it on
|
|
// (native_endpoints), and a request for one goes on one of those: turned
|
|
// into another API once, here, when it must be, never on both computers.
|
|
|
|
// Its list also has the image models the other magpie draws with, which
|
|
// it gives only to a magpie that asks for them (DrawersHeader), each
|
|
// marked "kind": "image": an older magpie would have taken one whose id
|
|
// doesn't say it draws for a model to chat with. They go on to the other
|
|
// magpie's images API, which asks their vendor as it would its own.
|
|
// Each model's label is the other magpie's name for it, with its provider
|
|
// there after it ("Claude Sonnet 5 · RelayA"), as its own agents see it:
|
|
// two of its providers' models of one name are told apart here too.
|
|
// Provider in model names Off here names each by its name there alone
|
|
// (display_name, catalog.Model's Plain), whatever that magpie's own
|
|
// setting put after it (ARNO on Discord).
|
|
|
|
// Its video models come the same way (VideomakersHeader), marked "kind":
|
|
// "video", and go on to its videos API (#545).
|
|
|
|
// Its list also tells what each model costs there (PricesHeader): the
|
|
// price the other magpie counts its usage at — what its user set for the
|
|
// model by hand, else its provider's list price, at that provider's price
|
|
// rate — so its calls cost the same here (Sorghum on Discord). It is this
|
|
// provider's list price for the model: a price set here for it still comes
|
|
// first, as one set for any provider's list price does. A magpie tells it
|
|
// only to its own computer and to one sharing it with a gateway key, and
|
|
// one from before it tells nothing, which leaves the models priced as
|
|
// they were: by models.dev.
|
|
|
|
// Its decision models are requested with DecidersHeader, marked
|
|
// "kind": "decision", and asked at its System One API, including models
|
|
// whose names aren't Jev's.
|
|
|
|
// DrawersHeader asks a magpie's model list for its image models as well.
|
|
const DrawersHeader = "X-Magpie-Drawers"
|
|
|
|
// VideomakersHeader asks a magpie's model list for its video models as
|
|
// well: a magpie that doesn't send it would take grok-imagine-video, whose
|
|
// id says "imagine", for an image model.
|
|
const VideomakersHeader = "X-Magpie-Videomakers"
|
|
|
|
// PricesHeader asks a magpie's model list for what each model costs
|
|
// there ("magpie_price"): a magpie that doesn't send it is told nothing
|
|
// more than before.
|
|
const PricesHeader = "X-Magpie-Prices"
|
|
|
|
// DecidersHeader asks a magpie's model list for its decision models too.
|
|
const DecidersHeader = "X-Magpie-Deciders"
|
|
|
|
// RemoteMagpiePreset is the preset's id.
|
|
const RemoteMagpiePreset = "remote-magpie"
|
|
|
|
// IsRemoteMagpie reports whether the provider is another magpie.
|
|
func (p Provider) IsRemoteMagpie() bool { return p.Preset == RemoteMagpiePreset }
|
|
|
|
// remoteMagpieEndpoints puts every API on the address given, however it
|
|
// was typed: the bare host and port (plain http, which magpie serves),
|
|
// …/v1, or the Anthropic root.
|
|
func (p *Provider) remoteMagpieEndpoints() {
|
|
if !p.IsRemoteMagpie() {
|
|
return
|
|
}
|
|
root := ""
|
|
for _, u := range []string{p.Chat, p.Responses, p.Anthropic, p.Decide} {
|
|
if u = strings.TrimRight(strings.TrimSpace(u), "/"); u != "" {
|
|
root = u
|
|
break
|
|
}
|
|
}
|
|
if root == "" {
|
|
return
|
|
}
|
|
if !strings.Contains(root, "://") {
|
|
root = "http://" + root
|
|
}
|
|
for _, suf := range []string{
|
|
"/v1/messages/count_tokens", "/v1/messages",
|
|
"/v1/chat/completions", "/v1/responses", "/v1/systemone",
|
|
"/v1/embeddings", "/v1/rerank",
|
|
"/v1/audio/transcriptions", "/v1/audio/translations", "/v1/audio/speech",
|
|
"/v1/images/generations", "/v1/images/edits",
|
|
"/v1/videos", "/v1",
|
|
} {
|
|
if b, ok := strings.CutSuffix(root, suf); ok {
|
|
root = b
|
|
break
|
|
}
|
|
}
|
|
p.Chat, p.Responses, p.Anthropic = root+"/v1", root+"/v1", root
|
|
p.Decide = p.Chat
|
|
}
|
|
|
|
// listHeaders are the headers p's model list is asked with: a remote
|
|
// magpie is asked for its image, video and decision models too, and its prices.
|
|
// Its key goes as x-api-key whatever the user's headers say (remoteKey).
|
|
// A list is no turn of a conversation: ${sessionId} and ${threadId} in a
|
|
// value are an id of the list's own (conversation.go).
|
|
func (p Provider) listHeaders() map[string]string {
|
|
h := p.SentHeaders(context.Background())
|
|
if !p.IsRemoteMagpie() {
|
|
return h
|
|
}
|
|
h = maps.Clone(h)
|
|
if h == nil {
|
|
h = map[string]string{}
|
|
}
|
|
for k := range h {
|
|
if strings.EqualFold(k, "x-api-key") {
|
|
delete(h, k)
|
|
}
|
|
}
|
|
h[DrawersHeader] = "1"
|
|
h[VideomakersHeader] = "1"
|
|
h[PricesHeader] = "1"
|
|
h[DecidersHeader] = "1"
|
|
return h
|
|
}
|
|
|
|
// listHeadersAt is listHeaders for a list asked at u: at OpenCode's
|
|
// gateway with a session of the list's own (OpenCodeSession), unless the
|
|
// user set one on the provider.
|
|
func (p Provider) listHeadersAt(u string) map[string]string {
|
|
h := p.listHeaders()
|
|
if !OpenCodeURL(u) {
|
|
return h
|
|
}
|
|
for k := range h {
|
|
if strings.EqualFold(k, "x-opencode-session") {
|
|
return h
|
|
}
|
|
}
|
|
h = maps.Clone(h)
|
|
if h == nil {
|
|
h = map[string]string{}
|
|
}
|
|
id := openCodeSession("")
|
|
h["X-Opencode-Session"], h["X-Opencode-Session-Id"] = id, id
|
|
// as OpenCode 2 asks its gateway (#3491), unless the user set their own
|
|
if !slices.ContainsFunc(slices.Collect(maps.Keys(h)), func(k string) bool { return strings.EqualFold(k, "User-Agent") }) {
|
|
h["User-Agent"] = OpenCodeUserAgent()
|
|
}
|
|
if !slices.ContainsFunc(slices.Collect(maps.Keys(h)), func(k string) bool { return strings.EqualFold(k, "x-opencode-client") }) {
|
|
h["X-Opencode-Client"] = "cli"
|
|
}
|
|
return h
|
|
}
|
|
|
|
// remotePrice is what a remote magpie's list said the model costs there,
|
|
// if it said.
|
|
func (p Provider) remotePrice(model string) (catalog.Price, bool) {
|
|
if !p.IsRemoteMagpie() {
|
|
return catalog.Price{}, false
|
|
}
|
|
for _, m := range p.liveByID(model) {
|
|
if m.Price != nil {
|
|
return *m.Price, true
|
|
}
|
|
}
|
|
return catalog.Price{}, false
|
|
}
|
|
|
|
// RemotePriced reports whether the model's list price is the one a remote
|
|
// magpie's list gave, for the editor to say whose it is.
|
|
func (p Provider) RemotePriced(model string) bool {
|
|
_, ok := p.remotePrice(model)
|
|
return ok
|
|
}
|