A Discord user's POST /v1/decisions was answered 404 ("magpie serves
/v1/chat/completions, ..."). It is OpenAI's Decisions API (gpt-6-luna):
one input, up to 400 questions, typed answers, input tokens billed only.
The shape was taken from openai-python (resources/decisions.py,
types/decision_create_params.py, types/decision.py, added in #4033 on
10-06) and the Decisions guide, not guessed: POST /v1/decisions, a JSON
body of model, input (a string or messages/items with input_text,
input_image as a data or http(s) URL, JSON parts; function and custom
tool calls and their outputs), questions (predicate / choice / score),
and optional prompt_cache_key, prompt_cache_options, safety_identifier.
The answer is {answers, model, usage}, not streamed; usage.input_tokens
includes input_tokens_details.cached_tokens and cache_write_tokens. The
Azure client sends it on the v1 API (/openai/v1/decisions), the model in
the body.
- decide (decisions.go) goes through serveRetrieval as embeddings and
rerank do, body as sent with only model changed. So a disabled model,
a key's models, accounts and spent limits, pause rules, keys/accounts
of the provider, masking and the Routing page hold here as on every
route.
- Only a provider canDecide takes is sent it: an OpenAI API key on
api.openai.com, Azure OpenAI (IsAzure, at its OpenAIBase, so
/openai/v1/decisions with api-key), or another magpie. Anyone else (a
relay, a plugin, a ChatGPT sign-in) gets a 400 that says who serves
it, checked in serveRetrieval's try loop beside the no-OpenAI-API
check (refusingRetrieval), never the vendor's 404; a group moves on.
- A bare gpt-6-luna is a kind group "decision" (servesDecision), so a
relay that lists the same id is never its member.
- Usage is recorded as "decisions" (countedRetrieval): input less the
cached and cache-written tokens, those as CacheRead/CacheWrite, and
no output, so gpt-6-luna's own models.dev rates price it as OpenAI
bills ($0.10/1M input, cache read/write at their rates).
- /decisions joins the one-port client prefixes, another magpie's URL
suffixes, the 404's route list and info's apis. The readonly, LAN
and CORS guard tests list the route.
- native_endpoints is left alone: it names the chat APIs a model is
relayed on (Cursor's names, a remote's API choice).
- docs: gateway-routing.md has a Decisions row; reference.md a section.
Run on mote at this tree (mote-run): gofmt -l clean; go vet of gateway
and provider; GOOS=linux go vet -tags nogui ./...; GOOS=windows go vet
./...; go build ./..., linux nogui and windows builds ok;
go test -count=1 -timeout 45m ./internal/gateway ./internal/provider
./internal/usage: ok (gateway 413s, provider 88s, usage 18s).
Red on the old code (e0930b1de, the new tests copied in): every
TestDecisions* is red with magpie's 404.
Broken by hand, each red: refuses always "" (the relay is sent it:
TestDecisionsRefusedWhereNotServed); the cache split not read
(Input 1200, CacheRead 0: TestDecisionsReachOpenAIAndAzure); canDecide
without the api.openai.com check (TestCanDecide, the relay sent it);
/v1/decisions out of the remote suffixes (TestRemoteMagpie).
Not tried: a real OpenAI Decisions request, or a real Azure resource.
Tests reach only httptest servers (OpenAI's host through the provider's
proxy).
201 lines
7.0 KiB
Go
201 lines
7.0 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/decisions",
|
|
"/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
|
|
}
|
|
|
|
// RemoteVia is the magpies a remote magpie's model passes through to its
|
|
// vendor, as its list said, that magpie last (catalog's magpie_via.go);
|
|
// nil for another provider's, or a list from a magpie from before.
|
|
func (p Provider) RemoteVia(model string) []string {
|
|
if !p.IsRemoteMagpie() {
|
|
return nil
|
|
}
|
|
for _, m := range p.liveByID(model) {
|
|
if len(m.Via) > 0 {
|
|
return m.Via
|
|
}
|
|
}
|
|
return nil
|
|
}
|