From a90b52f1b4ffa2fcf0a7c568617b892372e856d0 Mon Sep 17 00:00:00 2001 From: yetone Date: Fri, 2 Oct 2026 23:47:10 +0800 Subject: [PATCH] =?UTF-8?q?gateway:=20the=20model=20list=20comes=20as=20id?= =?UTF-8?q?s=20one=20a=20line=20with=20=3Fformat=3Dtext,=20to=20paste=20in?= =?UTF-8?q?to=20ZCode's=20custom=20provider=20from=20a=20magpie=20on=20ano?= =?UTF-8?q?ther=20computer=20(=E6=82=A0=E6=82=A0=E5=93=A5=20on=20Discord,?= =?UTF-8?q?=20v0.1.671:=20ZCode=20pointed=20at=20magpie=20on=20a=20NAS,=20?= =?UTF-8?q?http://192.168.0.200:3425,=20Anthropic=20Messages,=20never=20go?= =?UTF-8?q?t=20the=20model=20list,=20while=20at=20127.0.0.1=20it=20did)=20?= =?UTF-8?q?=E2=80=94=20ZCode=20(3.14.3)=20asks=20a=20custom=20provider=20f?= =?UTF-8?q?or=20no=20list=20at=20all:=20its=20settings=20take=20the=20mode?= =?UTF-8?q?ls=20typed=20one=20a=20line=20("=E6=A8=A1=E5=9E=8B=E5=88=97?= =?UTF-8?q?=E8=A1=A8=20=C2=B7=20=E6=AF=8F=E8=A1=8C=E4=B8=80=E4=B8=AA?= =?UTF-8?q?=E6=A8=A1=E5=9E=8B=E5=90=8D=E7=A7=B0"),=20its=20"=E6=99=BA?= =?UTF-8?q?=E8=83=BD=E9=85=8D=E7=BD=AE"=20matches=20built-in=20metadata=20?= =?UTF-8?q?by=20id=20and=20base=20URL,=20and=20neither=20its=20desktop=20a?= =?UTF-8?q?pp=20nor=20its=20CLI=20requests=20/models=20anywhere.=20What=20?= =?UTF-8?q?filled=20it=20at=20127.0.0.1=20was=20a=20magpie=20on=20that=20c?= =?UTF-8?q?omputer,=20which=20writes=20its=20models=20into=20ZCode's=20own?= =?UTF-8?q?=20provider=5Fconfig.json=20(personalModelIds)=20and=20config.j?= =?UTF-8?q?son=20under=20the=20provider=20"magpie"=20at=20its=20own=20127.?= =?UTF-8?q?0.0.1=20gateway;=20a=20magpie=20on=20a=20NAS=20can't=20write=20?= =?UTF-8?q?that=20computer's=20files,=20so=20the=20provider=20pointed=20at?= =?UTF-8?q?=20it=20is=20left=20with=20what=20was=20typed.=20The=20gateway?= =?UTF-8?q?=20itself=20answers=20another=20computer=20as=20it=20answers=20?= =?UTF-8?q?this=20one=20(no=20Host=20check,=20the=20key=20read=20wherever?= =?UTF-8?q?=20the=20client=20puts=20it,=201c047f36=20in=20v0.1.664).=20GET?= =?UTF-8?q?=20/v1/models=3Fformat=3Dtext=20(and=20/models)=20now=20answers?= =?UTF-8?q?=20text/plain,=20the=20ids=20in=20the=20list's=20order=20one=20?= =?UTF-8?q?a=20line,=20so=20a=20browser=20on=20that=20computer=20opening?= =?UTF-8?q?=20http://:3425/v1/models=3Fformat=3Dtext&key=3D=20gets=20what=20to=20paste=20into=20ZCode's=20model=20l?= =?UTF-8?q?ist;=20without=20a=20key=20from=20the=20network=20it=20is=20ref?= =?UTF-8?q?used=20as=20the=20JSON=20list=20is.=20TestLANModelsAsText=20(fr?= =?UTF-8?q?om=20192.168.0.9=20to=20Host=20192.168.0.200:3425:=20text/plain?= =?UTF-8?q?,=20the=20JSON=20list's=20ids=20in=20its=20order,=20none=20empt?= =?UTF-8?q?y=20or=20JSON;=20no=20key=20401)=20fails=20without=20the=20chan?= =?UTF-8?q?ge.=20go=20vet=20./internal/...,=20go=20build=20-tags=20nogui?= =?UTF-8?q?=20(darwin,=20GOOS=3Dlinux),=20GOOS=3Dwindows=20go=20build,=20g?= =?UTF-8?q?o=20test=20-tags=20nogui=20-count=3D1=20./...=20under=20a=20tem?= =?UTF-8?q?p=20HOME=20pass.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/gateway/gateway.go | 13 ++++++++++ internal/gateway/lan_test.go | 46 ++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) 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) + } +}