diff --git a/internal/gateway/gateway.go b/internal/gateway/gateway.go index 0c7561d69..a7494b2bf 100644 --- a/internal/gateway/gateway.go +++ b/internal/gateway/gateway.go @@ -583,6 +583,19 @@ func (s *Server) models(w http.ResponseWriter, r *http.Request) { if r.Header.Get(provider.VideomakersHeader) != "" { data = append(data, videomakerObjects()...) } + // ?format=text: the ids one a line, to paste into a client that takes + // its models typed by hand, one a line, and asks no list of its own + // (ZCode's custom provider), from a browser on another computer with + // the gateway key as ?key= + if r.URL.Query().Get("format") == "text" { + var b strings.Builder + for _, m := range data { + fmt.Fprintln(&b, m["id"]) + } + w.Header().Set("Content-Type", "text/plain; charset=utf-8") + io.WriteString(w, b.String()) + return + } out := map[string]any{"object": "list", "data": data, "has_more": false} if len(data) > 0 { out["first_id"], out["last_id"] = data[0]["id"], data[len(data)-1]["id"] diff --git a/internal/gateway/lan_test.go b/internal/gateway/lan_test.go index b75d9f2b0..59cbdc01d 100644 --- a/internal/gateway/lan_test.go +++ b/internal/gateway/lan_test.go @@ -1,10 +1,12 @@ package gateway import ( + "encoding/json" "net/http" "net/http/httptest" "os" "path/filepath" + "slices" "strings" "testing" @@ -241,3 +243,47 @@ func TestLANModelsWithKeyBesideAnotherToken(t *testing.T) { t.Fatalf("loopback: %d, counted as %q", c, who) } } + +// ZCode's custom provider asks no model list; its models are typed one a +// line (悠悠哥 on Discord: from magpie on a NAS nothing filled it, while a +// magpie on this computer writes its models into ZCode's own config). +// /v1/models?format=text gives the ids one a line to paste there, opened +// in a browser on another computer with the key as ?key=; without a key it +// is refused as the list is. +func TestLANModelsAsText(t *testing.T) { + setup(t, provider.Chat, &fake{t: t}) + t.Setenv("MAGPIE_ADDR", "") + _, secrets := newCaller(t, "PC") + h := lanGuard(New().Handler()) + get := func(path string) *httptest.ResponseRecorder { + r := httptest.NewRequest("GET", "http://192.168.0.200:3425"+path, nil) + r.RemoteAddr = "192.168.0.9:50123" + w := httptest.NewRecorder() + h.ServeHTTP(w, r) + return w + } + w := get("/v1/models?format=text&key=" + secrets[0]) + if w.Code != 200 || !strings.HasPrefix(w.Header().Get("Content-Type"), "text/plain") { + t.Fatalf("%d %s", w.Code, w.Header().Get("Content-Type")) + } + lines := strings.Split(strings.TrimSuffix(w.Body.String(), "\n"), "\n") + if !slices.Contains(lines, "fake/m1") || slices.ContainsFunc(lines, func(l string) bool { return l == "" || strings.ContainsAny(l, "{\" ") }) { + t.Fatalf("lines %q", lines) + } + // the JSON list's ids, in its order + j := get("/v1/models?key=" + secrets[0]) + var list struct{ Data []struct{ ID string } } + if err := json.Unmarshal(j.Body.Bytes(), &list); err != nil { + t.Fatal(err) + } + var ids []string + for _, m := range list.Data { + ids = append(ids, m.ID) + } + if !slices.Equal(ids, lines) { + t.Fatalf("text %q, json %q", lines, ids) + } + if w := get("/v1/models?format=text"); w.Code != http.StatusUnauthorized { + t.Fatalf("no key: %d", w.Code) + } +}