What: Provider.Serves now reads a new catalog.LiveAll (the fetched list
with image and video models kept) instead of live(), which drops Draws and
Films models. The draw key loop and the custom video path move on to the
next key when a key answers 404 whose body says the model isn't served
(keyLacksModel = 404 + modelRefused's wording); any other 404 still stops.
Why: wangru8080's analysis on #1657. A relay whose per-key lists name
gpt-image-2 under one key only still had drawing try a key that lacks it:
Serves read a list with the image model filtered out, so every key looked
unlisted, and that key's 404 "Model ... is not supported by any configured
account in this group" ended the loop instead of asking the next key.
Embeddings already move on any error short of the last key.
Run (sandbox HOME, private GOCACHE, worktree at origin/main 97dfed0f3):
- TestDrawGoesPastAKeyTheRelayListsForText, TestCustomVideoGoesPastAKeyThatLacksTheModel.
Break checks: old Serves -> subtest 1 asks key-a first; draw loop without
keyLacksModel -> subtest 2 gets the 404; video loop off -> 404 on start.
A bare 404 still never reaches key-b.
- gofmt -l; go vet ./... (darwin), GOOS=linux -tags nogui, GOOS=windows;
go build for all three.
- go test -tags nogui ./internal/gateway ./internal/provider
./internal/catalog ./internal/codexcat: all ok.
Not tried against a real relay.
Co-authored-by: wangru8080 <22983744+wangru8080@users.noreply.github.com>
328 lines
14 KiB
Go
328 lines
14 KiB
Go
package gateway
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"slices"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/access"
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// openAIVideos is an upstream serving OpenAI's Videos API under /v1, as a
|
|
// provider set up with a base URL does (#1446): its model list marks
|
|
// vid-1 "kind": "video" and lists sora-like-2 with no kind, a video takes
|
|
// two polls to be made, and what each request was sent and signed with is
|
|
// kept.
|
|
type openAIVideos struct {
|
|
*httptest.Server
|
|
mu sync.Mutex
|
|
asked []string // "METHOD path key"
|
|
bodies []string // bodies of POST /v1/videos
|
|
heads []http.Header
|
|
polls int
|
|
lacks string // a key answered 404 that it serves no such model
|
|
}
|
|
|
|
func newOpenAIVideos(t *testing.T) *openAIVideos {
|
|
t.Helper()
|
|
u := &openAIVideos{}
|
|
u.Server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
b, _ := io.ReadAll(r.Body)
|
|
u.mu.Lock()
|
|
defer u.mu.Unlock()
|
|
key := strings.TrimPrefix(r.Header.Get("Authorization"), "Bearer ")
|
|
u.asked = append(u.asked, r.Method+" "+r.URL.Path+" "+key)
|
|
u.heads = append(u.heads, r.Header.Clone())
|
|
w.Header().Set("Content-Type", "application/json")
|
|
// the vendor's id has a dot, which magpie's own ids can't carry bare
|
|
const vid = "video_68d7.abc"
|
|
switch {
|
|
case r.Method == "GET" && r.URL.Path == "/v1/models":
|
|
io.WriteString(w, `{"object":"list","data":[{"id":"gpt-5.5","object":"model"},{"id":"vid-1","object":"model","kind":"video"},{"id":"sora-like-2","object":"model"}]}`)
|
|
case r.Method == "POST" && r.URL.Path == "/v1/videos" && key == u.lacks:
|
|
w.WriteHeader(404)
|
|
io.WriteString(w, `{"error":{"message":"Model \"vid-1\" is not supported by any configured account in this group","type":"invalid_request_error"}}`)
|
|
case r.Method == "POST" && r.URL.Path == "/v1/videos":
|
|
u.bodies = append(u.bodies, string(b))
|
|
io.WriteString(w, `{"id":"`+vid+`","object":"video","created_at":1790000000,"status":"queued","progress":0,"model":"vid-1","seconds":"4","size":"1280x720"}`)
|
|
case r.Method == "GET" && r.URL.Path == "/v1/videos/"+vid:
|
|
u.polls++
|
|
switch u.polls {
|
|
case 1:
|
|
io.WriteString(w, `{"id":"`+vid+`","object":"video","status":"in_progress","progress":40,"model":"vid-1"}`)
|
|
default:
|
|
io.WriteString(w, `{"id":"`+vid+`","object":"video","status":"completed","progress":100,"model":"vid-1","seconds":"4"}`)
|
|
}
|
|
case r.Method == "GET" && r.URL.Path == "/v1/videos/"+vid+"/content":
|
|
w.Header().Set("Content-Type", "video/mp4")
|
|
io.WriteString(w, "\x00\x00\x00\x18ftypmp42fake-mp4")
|
|
default:
|
|
w.WriteHeader(404)
|
|
io.WriteString(w, `{"error":{"message":"no route `+r.Method+" "+r.URL.Path+`","type":"invalid_request_error"}}`)
|
|
}
|
|
}))
|
|
t.Cleanup(u.Close)
|
|
return u
|
|
}
|
|
|
|
func (u *openAIVideos) log() []string {
|
|
u.mu.Lock()
|
|
defer u.mu.Unlock()
|
|
return slices.Clone(u.asked)
|
|
}
|
|
|
|
// saveFetched saves p and fetches its model list, as adding it does.
|
|
func saveFetched(t *testing.T, p provider.Provider) provider.Provider {
|
|
t.Helper()
|
|
if err := provider.Save(p); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
found, err := provider.Find(p.ID)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := found.Fetch(context.Background()); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return *found
|
|
}
|
|
|
|
func asKey(t *testing.T, h http.Handler, secret, method, path, body string) (int, map[string]any, []byte) {
|
|
t.Helper()
|
|
r := httptest.NewRequest(method, path, strings.NewReader(body))
|
|
if body != "" {
|
|
r.Header.Set("Content-Type", "application/json")
|
|
}
|
|
if secret != "" {
|
|
r.Header.Set("Authorization", "Bearer "+secret)
|
|
}
|
|
w := httptest.NewRecorder()
|
|
h.ServeHTTP(w, r)
|
|
var obj map[string]any
|
|
json.Unmarshal(w.Body.Bytes(), &obj)
|
|
return w.Code, obj, w.Body.Bytes()
|
|
}
|
|
|
|
// A provider set up with a base URL whose model list marks a model "kind":
|
|
// "video" makes videos with it: started, polled and fetched at its
|
|
// OpenAI-shaped videos API (#1446). The old gateway turned it away 400 as
|
|
// not a Grok or Volcengine model.
|
|
func TestCustomProviderMakesVideos(t *testing.T) {
|
|
fresh(t)
|
|
up := newOpenAIVideos(t)
|
|
p := saveFetched(t, provider.Provider{ID: "studio", Name: "Studio", Chat: up.URL + "/v1", Key: "sk-studio"})
|
|
ms := Videomakers(p)
|
|
if len(ms) != 1 || ms[0].ID != "vid-1" || ms[0].Provider != "studio" {
|
|
t.Fatalf("videomakers %v: the list marks vid-1 alone a video model", ms)
|
|
}
|
|
h := New().Handler()
|
|
code, obj, raw := asKey(t, h, "", "POST", "/v1/videos", `{"model":"studio/vid-1","prompt":"a cat on a fence","seconds":4,"size":"1280x720"}`)
|
|
id, _ := obj["id"].(string)
|
|
if code != 200 || !strings.HasPrefix(id, "video_studio.") || obj["model"] != "studio/vid-1" || obj["status"] != "queued" {
|
|
t.Fatalf("start: %d %s", code, raw)
|
|
}
|
|
var sent map[string]any
|
|
json.Unmarshal([]byte(up.bodies[0]), &sent)
|
|
if sent["model"] != "vid-1" || sent["prompt"] != "a cat on a fence" || sent["seconds"] != "4" || sent["size"] != "1280x720" {
|
|
t.Fatalf("sent %v", sent)
|
|
}
|
|
for i, want := range []string{"in_progress", "completed"} {
|
|
code, obj, raw = asKey(t, h, "", "GET", "/v1/videos/"+id, "")
|
|
if code != 200 || obj["id"] != id || obj["status"] != want || obj["model"] != "studio/vid-1" {
|
|
t.Fatalf("poll %d: %d %s", i, code, raw)
|
|
}
|
|
}
|
|
rec := httptest.NewRecorder()
|
|
h.ServeHTTP(rec, httptest.NewRequest("GET", "/v1/videos/"+id+"/content", nil))
|
|
if rec.Code != 200 || rec.Body.String() != "\x00\x00\x00\x18ftypmp42fake-mp4" || rec.Header().Get("Content-Type") != "video/mp4" {
|
|
t.Fatalf("content: %d %q %v", rec.Code, rec.Body.String(), rec.Header())
|
|
}
|
|
want := []string{"GET /v1/models sk-studio", "POST /v1/videos sk-studio", "GET /v1/videos/video_68d7.abc sk-studio", "GET /v1/videos/video_68d7.abc sk-studio", "GET /v1/videos/video_68d7.abc/content sk-studio"}
|
|
if got := up.log(); !slices.Equal(got, want) {
|
|
t.Fatalf("asked %v\nwant %v", got, want)
|
|
}
|
|
// a vendor is told nothing of who is calling
|
|
for _, hd := range up.heads {
|
|
if hd.Get(AgentHeader) != "" || hd.Get(SessionHeader) != "" {
|
|
t.Fatalf("caller headers went to the vendor: %v", hd)
|
|
}
|
|
}
|
|
// by its bare name too, and with none named when it is the only maker
|
|
if code, obj, raw = asKey(t, h, "", "POST", "/v1/videos", `{"model":"vid-1","prompt":"a dog"}`); code != 200 || obj["model"] != "studio/vid-1" {
|
|
t.Fatalf("bare name: %d %s", code, raw)
|
|
}
|
|
if code, obj, raw = asKey(t, h, "", "POST", "/v1/videos", `{"prompt":"a dog"}`); code != 200 || obj["model"] != "studio/vid-1" {
|
|
t.Fatalf("no model named: %d %s", code, raw)
|
|
}
|
|
// another magpie asking this one for its video models is told of it
|
|
if objs := videomakerObjects(); len(objs) != 1 || objs[0]["id"] != "studio/vid-1" {
|
|
t.Fatalf("videomakerObjects %v", objs)
|
|
}
|
|
}
|
|
|
|
// A model's name alone makes no video model: one the provider's list
|
|
// doesn't mark "kind": "video" is still turned away before any request,
|
|
// and so is every model of a provider whose list marks none.
|
|
func TestCustomProviderVideoNeedsTheListsWord(t *testing.T) {
|
|
fresh(t)
|
|
up := newOpenAIVideos(t)
|
|
saveFetched(t, provider.Provider{ID: "studio", Name: "Studio", Chat: up.URL + "/v1", Key: "sk-studio"})
|
|
h := New().Handler()
|
|
for _, model := range []string{"studio/sora-like-2", "studio/gpt-5.5", "studio/grok-imagine-video"} {
|
|
code, obj, raw := asKey(t, h, "", "POST", "/v1/videos", `{"model":"`+model+`","prompt":"a cat"}`)
|
|
if code != 400 || !strings.Contains(errorOf(obj), "can't make videos") {
|
|
t.Fatalf("%s: %d %s", model, code, raw)
|
|
}
|
|
}
|
|
for _, a := range up.log() {
|
|
if strings.Contains(a, "/videos") {
|
|
t.Fatalf("a video was asked for: %v", up.log())
|
|
}
|
|
}
|
|
if ms := Videomakers(provider.Provider{ID: "unlisted", Name: "Unlisted", Chat: up.URL + "/v1", Key: "k"}); len(ms) != 0 {
|
|
t.Fatalf("a provider with no fetched list makes videos: %v", ms)
|
|
}
|
|
}
|
|
|
|
// A Responses-only relay has no Chat base: its video is started, polled
|
|
// and fetched under its Responses base all the same, as its images are.
|
|
func TestCustomProviderVideoOnAResponsesBase(t *testing.T) {
|
|
fresh(t)
|
|
up := newOpenAIVideos(t)
|
|
saveFetched(t, provider.Provider{ID: "relay", Name: "Relay", Responses: up.URL + "/v1", Key: "sk-relay"})
|
|
h := New().Handler()
|
|
code, obj, raw := asKey(t, h, "", "POST", "/v1/videos", `{"model":"relay/vid-1","prompt":"a kite"}`)
|
|
id, _ := obj["id"].(string)
|
|
if code != 200 || id == "" {
|
|
t.Fatalf("start: %d %s", code, raw)
|
|
}
|
|
asKey(t, h, "", "GET", "/v1/videos/"+id, "")
|
|
if code, obj, raw = asKey(t, h, "", "GET", "/v1/videos/"+id, ""); code != 200 || obj["status"] != "completed" {
|
|
t.Fatalf("poll: %d %s", code, raw)
|
|
}
|
|
rec := httptest.NewRecorder()
|
|
h.ServeHTTP(rec, httptest.NewRequest("GET", "/v1/videos/"+id+"/content", nil))
|
|
if rec.Code != 200 || !strings.HasSuffix(rec.Body.String(), "fake-mp4") {
|
|
t.Fatalf("content: %d %s", rec.Code, rec.Body.String())
|
|
}
|
|
}
|
|
|
|
// A video started on a provider's later key is asked after on that key,
|
|
// whichever is first: the vendor keeps it for the key that started it.
|
|
// A gateway key held to some accounts (#905) asks after it only on one it
|
|
// may use, and one held to some models (#882) only after a model it may
|
|
// use, on the poll and the content as on the start.
|
|
func TestCustomProviderVideoKeys(t *testing.T) {
|
|
fresh(t)
|
|
up := newOpenAIVideos(t)
|
|
saveFetched(t, provider.Provider{ID: "studio", Name: "Studio", Chat: up.URL + "/v1", Key: "kA", Keys: []provider.KeyAccount{{Key: "kB"}}})
|
|
keys, secrets := newCaller(t, "OnB", "OnA", "OtherModels", "ThisModel")
|
|
for i, c := range []access.Change{
|
|
{Key: keys[0].ID, Accounts: []string{"studio/" + provider.KeyID("kB")}},
|
|
{Key: keys[1].ID, Accounts: []string{"studio/" + provider.KeyID("kA")}},
|
|
{Key: keys[2].ID, Models: []string{"elsewhere/*"}},
|
|
{Key: keys[3].ID, Models: []string{"studio/vid-1"}},
|
|
} {
|
|
if _, err := access.Update([]string{"accounts-key", "accounts-key", "models-key", "models-key"}[i], c); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
h := New().Handler()
|
|
code, obj, raw := asKey(t, h, secrets[0], "POST", "/v1/videos", `{"model":"studio/vid-1","prompt":"a cat"}`)
|
|
id, _ := obj["id"].(string)
|
|
if code != 200 || id == "" {
|
|
t.Fatalf("start: %d %s", code, raw)
|
|
}
|
|
if code, _, raw = asKey(t, h, secrets[0], "GET", "/v1/videos/"+id, ""); code != 200 {
|
|
t.Fatalf("poll on kB: %d %s", code, raw)
|
|
}
|
|
// a caller with no restriction asks on kB too, not on the first key
|
|
if code, _, raw = asKey(t, h, "", "GET", "/v1/videos/"+id, ""); code != 200 {
|
|
t.Fatalf("poll: %d %s", code, raw)
|
|
}
|
|
if code, _, raw = asKey(t, h, secrets[0], "GET", "/v1/videos/"+id+"/content", ""); code != 200 || !strings.HasSuffix(string(raw), "fake-mp4") {
|
|
t.Fatalf("content on kB: %d %s", code, raw)
|
|
}
|
|
for _, a := range up.log()[1:] {
|
|
if !strings.HasSuffix(a, " kB") {
|
|
t.Fatalf("asked on another key than kB: %v", up.log())
|
|
}
|
|
}
|
|
before := len(up.log())
|
|
// held to kA: kB's video is not its to ask after
|
|
for _, path := range []string{"/v1/videos/" + id, "/v1/videos/" + id + "/content"} {
|
|
if code, obj, raw = asKey(t, h, secrets[1], "GET", path, ""); code != 403 || !strings.Contains(errorOf(obj), "not allowed to use the accounts") {
|
|
t.Fatalf("held to kA, %s: %d %s", path, code, raw)
|
|
}
|
|
}
|
|
if len(up.log()) != before {
|
|
t.Fatalf("a key held from the account had the vendor asked: %v", up.log()[before:])
|
|
}
|
|
// held to other models: neither the poll nor the bytes
|
|
for _, path := range []string{"/v1/videos/" + id, "/v1/videos/" + id + "/content"} {
|
|
if code, obj, raw = asKey(t, h, secrets[2], "GET", path, ""); code != 403 || !strings.Contains(errorOf(obj), "may not use studio/vid-1") {
|
|
t.Fatalf("held to other models, %s: %d %s", path, code, raw)
|
|
}
|
|
}
|
|
for _, a := range up.log()[before:] {
|
|
if strings.HasSuffix(strings.Fields(a)[1], "/content") {
|
|
t.Fatalf("a key held from the model was sent the video: %v", up.log()[before:])
|
|
}
|
|
}
|
|
// held to this model: both
|
|
if code, _, raw = asKey(t, h, secrets[3], "GET", "/v1/videos/"+id, ""); code != 200 {
|
|
t.Fatalf("held to vid-1, poll: %d %s", code, raw)
|
|
}
|
|
if code, _, raw = asKey(t, h, secrets[3], "GET", "/v1/videos/"+id+"/content", ""); code != 200 || !strings.HasSuffix(string(raw), "fake-mp4") {
|
|
t.Fatalf("held to vid-1, content: %d %s", code, raw)
|
|
}
|
|
// and a key taken off, its videos say so rather than ask on another
|
|
if err := provider.SetKeyOn("studio", provider.KeyID("kB"), false); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if code, obj, raw = asKey(t, h, "", "GET", "/v1/videos/"+id, ""); code != 404 || !strings.Contains(errorOf(obj), "no longer on") {
|
|
t.Fatalf("kB off: %d %s", code, raw)
|
|
}
|
|
}
|
|
|
|
// A Grok subscription's video is held by a gateway key's models on its
|
|
// poll and content as on its start; one the key may use is answered as
|
|
// before.
|
|
func TestGrokVideoPollHeldByKeyModels(t *testing.T) {
|
|
grokSignedIn(t)
|
|
newGrokMedia(t)
|
|
keys, secrets := newCaller(t, "OtherModels", "Grok")
|
|
if _, err := access.Update("models-key", access.Change{Key: keys[0].ID, Models: []string{"elsewhere/*"}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := access.Update("models-key", access.Change{Key: keys[1].ID, Models: []string{"grok/*"}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
h := New().Handler()
|
|
code, obj, raw := asKey(t, h, secrets[1], "POST", "/v1/videos", `{"model":"grok/grok-imagine-video","prompt":"a kite"}`)
|
|
id, _ := obj["id"].(string)
|
|
if code != 200 || id == "" {
|
|
t.Fatalf("start: %d %s", code, raw)
|
|
}
|
|
for _, path := range []string{"/v1/videos/" + id, "/v1/videos/" + id + "/content"} {
|
|
if code, obj, raw = asKey(t, h, secrets[0], "GET", path, ""); code != 403 || !strings.Contains(errorOf(obj), "may not use grok/") {
|
|
t.Fatalf("held, %s: %d %s", path, code, raw)
|
|
}
|
|
}
|
|
// the two held asks read the vendor's state, as a poll does: the
|
|
// third ask finds it made
|
|
if code, obj, raw = asKey(t, h, secrets[1], "GET", "/v1/videos/"+id, ""); code != 200 || obj["status"] != "completed" || obj["model"] != "grok/grok-imagine-video" {
|
|
t.Fatalf("poll: %d %s", code, raw)
|
|
}
|
|
if code, _, raw = asKey(t, h, secrets[1], "GET", "/v1/videos/"+id+"/content", ""); code != 200 || string(raw) != "fake-mp4-bytes" {
|
|
t.Fatalf("content: %d %s", code, raw)
|
|
}
|
|
}
|