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).
294 lines
12 KiB
Go
294 lines
12 KiB
Go
package gateway
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/access"
|
|
"github.com/yetone/magpie/internal/budget"
|
|
"github.com/yetone/magpie/internal/provider"
|
|
"github.com/yetone/magpie/internal/usage"
|
|
)
|
|
|
|
// decisionAsked is the Decisions guide's request
|
|
// (developers.openai.com/api/docs/guides/decisions), as openai-python's
|
|
// client.decisions.create sends it: a model, the input every question
|
|
// shares (a user message of text and an image) and the questions.
|
|
func decisionAsked(model string) string {
|
|
return `{"model":"` + model + `","input":[{"role":"user","content":[{"type":"input_text","text":"Inspect the product in this photo."},{"type":"input_image","image_url":"data:image/png;base64,iVBORw0KGgo="}]}],"questions":[{"type":"predicate","name":"visible_damage","instructions":"Is there visible damage?"},{"type":"choice","name":"condition","instructions":"Its condition","choices":[{"value":"new"},{"value":"used"}]}]}`
|
|
}
|
|
|
|
// decisionAnswer is a Decisions answer, the shape openai-python's
|
|
// Decision reads: typed answers and a usage whose input_tokens has its
|
|
// cached and cache-written tokens in it.
|
|
const decisionAnswer = `{"answers":[{"type":"predicate","name":"visible_damage","probability":0.92},{"type":"choice","name":"condition","choice":"used","confidence":0.8,"probabilities":[{"value":"new","probability":0.2},{"value":"used","probability":0.8}]}],"model":"gpt-6-luna","usage":{"input_tokens":1200,"input_tokens_details":{"cached_tokens":1000,"cache_write_tokens":50},"output_tokens":3,"output_tokens_details":{"reasoning_tokens":0},"total_tokens":1203}}`
|
|
|
|
type decisionHit struct {
|
|
host, path, auth, apiKey string
|
|
body map[string]any
|
|
}
|
|
|
|
// decider is a vendor that serves the Decisions API at /v1/decisions and
|
|
// Azure's /openai/v1/decisions, and keeps each request.
|
|
type decider struct {
|
|
mu sync.Mutex
|
|
hits []decisionHit
|
|
}
|
|
|
|
func (d *decider) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
raw, _ := io.ReadAll(r.Body)
|
|
var body map[string]any
|
|
json.Unmarshal(raw, &body)
|
|
d.mu.Lock()
|
|
d.hits = append(d.hits, decisionHit{host: r.Host, path: r.URL.Path, auth: r.Header.Get("Authorization"), apiKey: r.Header.Get("api-key"), body: body})
|
|
d.mu.Unlock()
|
|
if r.URL.Path != "/v1/decisions" && r.URL.Path != "/openai/v1/decisions" {
|
|
w.WriteHeader(404)
|
|
return
|
|
}
|
|
w.Header().Set("Content-Type", "application/json")
|
|
io.WriteString(w, decisionAnswer)
|
|
}
|
|
|
|
func (d *decider) all() []decisionHit {
|
|
d.mu.Lock()
|
|
defer d.mu.Unlock()
|
|
return append([]decisionHit(nil), d.hits...)
|
|
}
|
|
|
|
// decidingProviders saves an OpenAI API key (reached through the
|
|
// provider's proxy, so its host is OpenAI's own), an Azure OpenAI
|
|
// resource and a relay that lists gpt-6-luna among its chat models.
|
|
func decidingProviders(t *testing.T) (*decider, *decider) {
|
|
t.Helper()
|
|
fresh(t)
|
|
for _, k := range []string{"HTTPS_PROXY", "https_proxy", "HTTP_PROXY", "http_proxy", "ALL_PROXY", "all_proxy", "NO_PROXY", "no_proxy"} {
|
|
t.Setenv(k, "")
|
|
}
|
|
openai := &decider{}
|
|
ou := httptest.NewServer(openai)
|
|
t.Cleanup(ou.Close)
|
|
if err := provider.Save(provider.Provider{ID: "openai", Name: "OpenAI", Key: "sk-openai", Chat: "http://api.openai.com/v1", Proxy: ou.URL, Models: []string{"gpt-6-sol"}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
other := &decider{}
|
|
au := httptest.NewServer(other)
|
|
t.Cleanup(au.Close)
|
|
az, err := provider.FromPreset(provider.AzurePreset)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
az.Key, az.Chat, az.Models = "azure-key", au.URL+"/openai/v1", []string{"luna-deploy"}
|
|
az.Responses = az.Chat
|
|
if err := provider.Save(az); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := provider.Save(provider.Provider{ID: "relay", Name: "Relay", Key: "relay-key", Chat: au.URL + "/v1", Models: []string{"gpt-6-luna"}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return openai, other
|
|
}
|
|
|
|
func decisionRecords(t *testing.T, n int) []usage.Record {
|
|
t.Helper()
|
|
var recs []usage.Record
|
|
for deadline := time.Now().Add(10 * time.Second); time.Now().Before(deadline); time.Sleep(10 * time.Millisecond) {
|
|
recs = nil
|
|
for _, r := range usage.Load(time.Time{}) {
|
|
if r.Operation == "decisions" {
|
|
recs = append(recs, r)
|
|
}
|
|
}
|
|
if len(recs) >= n {
|
|
return recs
|
|
}
|
|
}
|
|
t.Fatalf("%d decisions recorded, want %d: %+v", len(recs), n, recs)
|
|
return nil
|
|
}
|
|
|
|
// POST /v1/decisions (a Discord user had a 404) goes as the agent sent it
|
|
// to OpenAI with its key, to Azure OpenAI at its v1 API's
|
|
// /openai/v1/decisions with the key in api-key, and on the bare
|
|
// /decisions an SDK given magpie's root posts to. The answer comes back
|
|
// as OpenAI said it, and the call is recorded as decisions: its input
|
|
// apart from the cached and cache-written tokens, each priced at its
|
|
// rate, and no output.
|
|
func TestDecisionsReachOpenAIAndAzure(t *testing.T) {
|
|
openai, other := decidingProviders(t)
|
|
s := New()
|
|
for _, tc := range []struct {
|
|
name, path, model, sentModel, host, upPath string
|
|
up *decider
|
|
}{
|
|
{"openai by its ref", "/v1/decisions", "openai/gpt-6-luna", "gpt-6-luna", "api.openai.com", "/v1/decisions", openai},
|
|
// a relay lists the same id, but only OpenAI can serve the API
|
|
{"the bare id", "/v1/decisions", "gpt-6-luna", "gpt-6-luna", "api.openai.com", "/v1/decisions", openai},
|
|
{"the bare path", "/decisions", "openai/gpt-6-luna", "gpt-6-luna", "api.openai.com", "/v1/decisions", openai},
|
|
{"azure", "/v1/decisions", "azure/luna-deploy", "luna-deploy", "", "/openai/v1/decisions", other},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
before := len(tc.up.all())
|
|
code, raw := postJSON(t, s, tc.path, decisionAsked(tc.model))
|
|
if code != 200 || raw != decisionAnswer {
|
|
t.Fatalf("%d %s", code, raw)
|
|
}
|
|
hits := tc.up.all()
|
|
if len(hits) != before+1 {
|
|
t.Fatalf("vendor asked %d times", len(hits)-before)
|
|
}
|
|
h := hits[len(hits)-1]
|
|
if h.path != tc.upPath || tc.host != "" && h.host != tc.host || h.body["model"] != tc.sentModel {
|
|
t.Fatalf("sent to %s%s as %v", h.host, h.path, h.body["model"])
|
|
}
|
|
if qs, _ := h.body["questions"].([]any); len(qs) != 2 {
|
|
t.Fatalf("questions sent: %v", h.body["questions"])
|
|
}
|
|
in, _ := h.body["input"].([]any)
|
|
if b, _ := json.Marshal(in); !strings.Contains(string(b), `"image_url":"data:image/png;base64,iVBORw0KGgo="`) {
|
|
t.Fatalf("input sent: %s", b)
|
|
}
|
|
if tc.up == other {
|
|
if h.apiKey != "azure-key" || h.auth != "" {
|
|
t.Fatalf("azure signed with %q %q", h.apiKey, h.auth)
|
|
}
|
|
} else if h.auth != "Bearer sk-openai" {
|
|
t.Fatalf("openai signed with %q", h.auth)
|
|
}
|
|
})
|
|
}
|
|
for _, h := range other.all() {
|
|
if h.path == "/v1/decisions" {
|
|
t.Fatal("the relay was sent a decision")
|
|
}
|
|
}
|
|
recs := decisionRecords(t, 4)
|
|
for _, r := range recs {
|
|
if r.Status != 200 || r.Input != 150 || r.CacheRead != 1000 || r.CacheWrite != 50 || r.Output != 0 {
|
|
t.Fatalf("recorded %+v", r)
|
|
}
|
|
}
|
|
if r := recs[0]; r.Provider != "openai" || r.Model != "gpt-6-luna" || r.Requested != "openai/gpt-6-luna" {
|
|
t.Fatalf("recorded %+v", r)
|
|
}
|
|
}
|
|
|
|
// A provider that can't serve the Decisions API is never sent the
|
|
// request: a relay, whatever its list says, gets a 400 that says who
|
|
// can, not the vendor's 404.
|
|
func TestDecisionsRefusedWhereNotServed(t *testing.T) {
|
|
_, other := decidingProviders(t)
|
|
code, raw := postJSON(t, New(), "/v1/decisions", decisionAsked("relay/gpt-6-luna"))
|
|
if code != 400 || !strings.Contains(raw, "Relay can't serve OpenAI's Decisions API") || !strings.Contains(raw, "Azure OpenAI") {
|
|
t.Fatalf("%d %s", code, raw)
|
|
}
|
|
if hits := other.all(); len(hits) != 0 {
|
|
t.Fatalf("the relay was asked: %+v", hits)
|
|
}
|
|
// no model, no JSON
|
|
if code, raw := postJSON(t, New(), "/v1/decisions", `{"input":"hi","questions":[]}`); code != 400 || !strings.Contains(raw, "name the model") {
|
|
t.Fatalf("no model: %d %s", code, raw)
|
|
}
|
|
}
|
|
|
|
// canDecide: OpenAI's API with a key, Azure OpenAI and another magpie;
|
|
// never a ChatGPT sign-in on OpenAI's host, a plugin, or a relay.
|
|
func TestCanDecide(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
name string
|
|
p provider.Provider
|
|
want bool
|
|
}{
|
|
{"openai key", provider.Provider{ID: "openai", Key: "sk", Chat: "https://api.openai.com/v1"}, true},
|
|
{"azure", provider.Provider{ID: "az", Preset: provider.AzurePreset, Key: "k", Chat: "https://res.openai.azure.com/openai/v1"}, true},
|
|
{"azure by its host", provider.Provider{ID: "az2", Key: "k", Chat: "https://res.openai.azure.com/openai/v1"}, true},
|
|
{"another magpie", provider.Provider{ID: "office", Preset: provider.RemoteMagpiePreset, Key: "k", Chat: "http://192.168.1.20:3425/v1"}, true},
|
|
{"a chatgpt sign-in", provider.Provider{ID: "codex", Account: &provider.Account{Agent: "codex", User: "me@example.com"}, Chat: "https://api.openai.com/v1"}, false},
|
|
{"a plugin", provider.PluginForTest("grok", "grok"), false},
|
|
{"a relay", provider.Provider{ID: "relay", Key: "k", Chat: "https://relay.example/v1"}, false},
|
|
{"openai with no key", provider.Provider{ID: "openai", Chat: "https://api.openai.com/v1"}, false},
|
|
} {
|
|
if got := canDecide(tc.p); got != tc.want {
|
|
t.Errorf("%s: %v", tc.name, got)
|
|
}
|
|
}
|
|
}
|
|
|
|
// Another magpie serves the Decisions API by routing it on: the request
|
|
// reaches its /v1/decisions and, through it, OpenAI.
|
|
func TestDecisionsThroughAnotherMagpie(t *testing.T) {
|
|
openai, _ := decidingProviders(t)
|
|
remote := httptest.NewServer(New().Handler())
|
|
t.Cleanup(remote.Close)
|
|
if err := provider.Save(provider.Provider{ID: "office", Preset: provider.RemoteMagpiePreset, Chat: remote.URL, Key: "remote-key", Models: []string{"openai/gpt-6-luna"}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
p, _ := provider.Find("office")
|
|
if _, err := p.Fetch(context.Background()); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
code, raw := postJSON(t, New(), "/v1/decisions", decisionAsked("office/openai/gpt-6-luna"))
|
|
if code != 200 || raw != decisionAnswer {
|
|
t.Fatalf("%d %s", code, raw)
|
|
}
|
|
if hits := openai.all(); len(hits) != 1 || hits[0].body["model"] != "gpt-6-luna" {
|
|
t.Fatalf("openai asked %+v", hits)
|
|
}
|
|
recs := decisionRecords(t, 2)
|
|
seen := map[string]bool{}
|
|
for _, r := range recs {
|
|
seen[r.Provider] = true
|
|
}
|
|
if !seen["office"] || !seen["openai"] {
|
|
t.Fatalf("recorded %+v", recs)
|
|
}
|
|
}
|
|
|
|
// A gateway key's rules hold on /v1/decisions as on every route: a key
|
|
// held to other models is refused before any vendor is asked, a disabled
|
|
// model is refused, and a key's spent limit of the model refuses it.
|
|
func TestDecisionsHeldByKeysAndRules(t *testing.T) {
|
|
openai, _ := decidingProviders(t)
|
|
budget.Forget()
|
|
pinLimitClock(t)
|
|
keys, secrets := newCaller(t, "Held", "Capped")
|
|
if _, err := access.Update("models-key", access.Change{Key: keys[0].ID, Models: []string{"relay/*"}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
setLimit(t, keys[1].ID, &access.Limit{Period: "day", Tokens: 100, Models: []string{"openai/*"}})
|
|
h := New().Handler()
|
|
if w := keyCall(h, secrets[0], "/v1/decisions", decisionAsked("openai/gpt-6-luna")); w.Code != http.StatusForbidden {
|
|
t.Fatalf("a key held to other models: %d %s", w.Code, w.Body.String())
|
|
}
|
|
if w := keyCall(h, "sk-magpie-not-a-key", "/v1/decisions", decisionAsked("openai/gpt-6-luna")); w.Code != http.StatusUnauthorized && w.Code != http.StatusForbidden {
|
|
t.Fatalf("a wrong key: %d %s", w.Code, w.Body.String())
|
|
}
|
|
if n := len(openai.all()); n != 0 {
|
|
t.Fatalf("openai asked %d times", n)
|
|
}
|
|
if w := keyCall(h, secrets[1], "/v1/decisions", decisionAsked("openai/gpt-6-luna")); w.Code != 200 {
|
|
t.Fatalf("the capped key's first call: %d %s", w.Code, w.Body.String())
|
|
}
|
|
decisionRecords(t, 1)
|
|
spentSaid(t, keyCall(h, secrets[1], "/v1/decisions", decisionAsked("openai/gpt-6-luna")), "openai/*")
|
|
if n := len(openai.all()); n != 1 {
|
|
t.Fatalf("openai asked %d times", n)
|
|
}
|
|
if err := provider.DisableModels("openai/gpt-6-luna"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if code, raw := postJSON(t, New(), "/v1/decisions", decisionAsked("openai/gpt-6-luna")); code != http.StatusForbidden {
|
|
t.Fatalf("a disabled model: %d %s", code, raw)
|
|
}
|
|
if n := len(openai.all()); n != 1 {
|
|
t.Fatalf("openai asked %d times", n)
|
|
}
|
|
}
|