lcc580 asked for a pass-through on the Remote magpie provider: show the
upstream magpie's own provider beside each model, so an agent can tell the
same model from two of its providers apart. Before, the names followed
both magpies' Provider in model names: with either one Off, both relays'
DeepSeek Chat reached the agents as "DeepSeek Chat · Office" (or plain
"DeepSeek Chat").
- The list a magpie gives another magpie (X-Magpie-Drawers) now names,
as magpie_by, the provider serving each model there (Entry.ServedBy):
"routing group" for a group, and for a model of its own Remote magpie
the provider at the start of that chain. The fetch keeps it as the
model's By (catalog/live.go).
- A Remote magpie provider gets upstreamProviders: the editor's "Model
names" tick ("With the provider serving it there"), or
`magpie provider set <id> upstream-providers=on`. With it on, each of
its models is labelled "DeepSeek Chat · Office › Relay A" in every
agent's list, whatever either magpie's Provider in model names says,
unless the user named the model themselves under Own
(Entry.Label/upstream, LabelsFor). Model settings' "Agents' lists show"
line follows the tick at once.
- Ids are unchanged, so a request still goes by office/relay-b/model.
Off (the default) names everything as before. A remote older than
magpie_by has nothing to add, so its names stay as before.
- The core's scripts stay in budget: four long comments in app.js were
cut to pay for it, no raise.
Run:
- Tests that fail on the old code: TestRemoteMagpieUpstreamNames
(gateway: an httptest remote with two relays' DeepSeek Chat and a
group, every Provider in model names mode, on and off, the third
magpie's magpie_by, ids kept), TestServedByAndUpstreamLabel (provider),
and name-suffix-remote.test.cjs's new case (tick, hint, save body).
- Broken by hand: upstream() returning "" and the magpie_by line removed;
both Go tests went red with the plain labels, then restored.
- Two real gateways, built -tags nogui on the Linux box (WSL) under
sandbox HOMEs: B with Relay A and Relay B both serving deepseek-chat,
A with B as Remote magpie "Office". A's /v1/models before:
"DeepSeek Chat · Relay A · Office"; after
`provider set office upstream-providers=on`:
"DeepSeek Chat · Office › Relay A" / "› Relay B" / "› routing group".
A request for office/relay-b/deepseek-chat reached relay-b on B.
- gofmt -l clean; go vet darwin, linux (nogui), windows on the touched
packages; go test catalog, provider, gateway, cli, gui on mote
(macOS) at the rebased head: ok (catalog 2.6s, provider 165s, gateway 948s, cli 56s, gui 149s).
- GUI on mote, chromium and webkit: name-suffix-remote, remote-magpie,
remote-self, i18n-keys, i18n-en-no-cjk, gui-ja, gui-de, gui-zh-tw,
provider-detect-draft, provider-unmasked, routing-remote-windows,
usage-remote-cards: all green except reds that fail the same on origin/main 388ca9ad4 (run there on the Linux box): gui-ja "ja has every zh string" (#5943) and remote-magpie "asks for the other magpie's address" in both engines (#4398); name-suffix-remote's old "named alone under Off" once in chromium de (#5147, scrollends.js import under load). The new case first failed in webkit (route lost after the save, the page's host went to DNS); it now uses one route whose server is swapped, 20 of 20 on the Linux box (WSL), both engines.
Not tried: the full suite (starts after the tag); the real desktop app's
editor by hand; a chain of three real magpies (the third magpie's
magpie_by is covered by the gateway test only).
116 lines
4.6 KiB
Go
116 lines
4.6 KiB
Go
package gateway
|
||
|
||
import (
|
||
"context"
|
||
"encoding/json"
|
||
"io"
|
||
"net/http"
|
||
"net/http/httptest"
|
||
"strings"
|
||
"testing"
|
||
|
||
"github.com/yetone/magpie/internal/provider"
|
||
)
|
||
|
||
// upstreamList is what another magpie, its own Provider in model names
|
||
// Off, lists for this one: DeepSeek Chat from two of its relays, each
|
||
// labelled by its name alone, so the agents here saw two "DeepSeek Chat ·
|
||
// Remote magpie" (lcc580 on X). magpie_by names the provider serving each
|
||
// there.
|
||
const upstreamList = `{"object":"list","data":[` +
|
||
`{"id":"relaya/deepseek-chat","object":"model","owned_by":"relaya","display_name":"DeepSeek Chat","magpie_label":"DeepSeek Chat","magpie_by":"Relay A","native_endpoints":["/v1/chat/completions"],"context_length":128000},` +
|
||
`{"id":"relayb/deepseek-chat","object":"model","owned_by":"relayb","display_name":"DeepSeek Chat","magpie_label":"DeepSeek Chat","magpie_by":"Relay B","native_endpoints":["/v1/chat/completions"],"context_length":128000},` +
|
||
`{"id":"group/fast","object":"model","owned_by":"relaya","display_name":"Fast","magpie_label":"Fast","magpie_by":"routing group","context_length":128000}]}`
|
||
|
||
type listed struct {
|
||
ID string `json:"id"`
|
||
Label string `json:"magpie_label"`
|
||
By string `json:"magpie_by"`
|
||
}
|
||
|
||
func listFor(t *testing.T, asMagpie bool) map[string]listed {
|
||
t.Helper()
|
||
rec := httptest.NewRecorder()
|
||
req := httptest.NewRequest("GET", "/v1/models", nil)
|
||
if asMagpie {
|
||
req.Header.Set(provider.DrawersHeader, "1")
|
||
}
|
||
New().Handler().ServeHTTP(rec, req)
|
||
var out struct{ Data []listed }
|
||
if err := json.Unmarshal(rec.Body.Bytes(), &out); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
got := map[string]listed{}
|
||
for _, m := range out.Data {
|
||
got[m.ID] = m
|
||
}
|
||
return got
|
||
}
|
||
|
||
// A Remote magpie's upstream names name each of its models by the
|
||
// provider serving it there, whatever either magpie's Provider in model
|
||
// names is, and its ids stay as they were. Off, the default, they are
|
||
// named as before. Listed on to a third magpie, a model names the provider
|
||
// at the start of the chain, not the Remote magpie it came through.
|
||
func TestRemoteMagpieUpstreamNames(t *testing.T) {
|
||
fresh(t)
|
||
remote := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||
w.Header().Set("Content-Type", "application/json")
|
||
io.WriteString(w, upstreamList)
|
||
}))
|
||
t.Cleanup(remote.Close)
|
||
id, err := provider.Add(provider.Provider{ID: "office", Name: "Office", Key: "magpie", Preset: provider.RemoteMagpiePreset, Chat: remote.URL})
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
p, _ := provider.Find(id)
|
||
if _, err := p.Fetch(context.Background()); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
t.Cleanup(func() { provider.SetSuffixMode(provider.SuffixOn) })
|
||
set := func(on bool) {
|
||
t.Helper()
|
||
p, _ := provider.Find(id)
|
||
p.UpstreamProviders = on
|
||
if err := provider.Save(*p); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
}
|
||
for _, c := range []struct {
|
||
on bool
|
||
mode string
|
||
a, b, group string
|
||
}{
|
||
{false, provider.SuffixOn, "DeepSeek Chat · Office", "DeepSeek Chat · Office", "Fast · Office"},
|
||
{false, provider.SuffixOff, "DeepSeek Chat · Office", "DeepSeek Chat · Office", "Fast"},
|
||
{true, provider.SuffixOn, "DeepSeek Chat · Office › Relay A", "DeepSeek Chat · Office › Relay B", "Fast · Office › routing group"},
|
||
{true, provider.SuffixOwn, "DeepSeek Chat · Office › Relay A", "DeepSeek Chat · Office › Relay B", "Fast · Office › routing group"},
|
||
{true, provider.SuffixOff, "DeepSeek Chat · Office › Relay A", "DeepSeek Chat · Office › Relay B", "Fast · Office › routing group"},
|
||
} {
|
||
set(c.on)
|
||
if err := provider.SetSuffixMode(c.mode); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
got := listFor(t, false)
|
||
a, b, g := got["office/relaya/deepseek-chat"], got["office/relayb/deepseek-chat"], got["office/group/fast"]
|
||
if a.Label != c.a || b.Label != c.b || g.Label != c.group {
|
||
t.Errorf("upstream names %v, %s: %q, %q, %q; want %q, %q, %q", c.on, c.mode, a.Label, b.Label, g.Label, c.a, c.b, c.group)
|
||
}
|
||
// an agent's list is told no magpie_by; another magpie's is
|
||
if a.By != "" {
|
||
t.Errorf("an agent's list has magpie_by %q", a.By)
|
||
}
|
||
}
|
||
// a third magpie is told the provider at the start of the chain
|
||
got := listFor(t, true)
|
||
for id, want := range map[string]string{"office/relaya/deepseek-chat": "Relay A", "office/relayb/deepseek-chat": "Relay B", "office/group/fast": "routing group"} {
|
||
if got[id].By != want {
|
||
t.Errorf("%s: magpie_by %q, want %q", id, got[id].By, want)
|
||
}
|
||
}
|
||
// a request still names the model by its id, which goes to the remote
|
||
if _, ok := got["office/relaya/deepseek-chat"]; !ok || strings.Contains(got["office/relaya/deepseek-chat"].ID, "›") {
|
||
t.Errorf("ids changed: %v", got)
|
||
}
|
||
}
|