treeman_26897 (Discord) on v0.1.3127's bridge MCP auto-discovery: - Renamed (name and id), a Remote magpie's auto-added MCP server went: adoptBridges keyed it on the provider id, and after provider.Rename that id is gone, so the server was taken out as the provider's. Now RemoteHub carries the provider's Was, and adoptBridges moves the server's bridge, bridgesOn and bridgesOffered from an id it had to the id it has (one that another provider has now stays that one's). The server keeps its name. - The add dialog had only the address and key. A new Remote magpie's editor has a Name and an ID under the key; blank, they are the preset's as before (provider.Add numbers them when taken), and the ID follows the name until one is typed. - Adding its MCP servers is opt-in: a switch, "Add its MCP servers to the Library", in the add dialog (off) and in a saved one's editor. It lives in library.json (bridgesOn); /api/providers says it as libraryMCP and the save takes it (library.SetRemoteMCP). On asks the remote at once (provider.AskRemoteHub) when no model list is fetched on that save; off takes its server out. Only one switched on is asked (bridgeWanted). A server auto-added before this (no bridgesOn) is kept, and its switch shows on. A server removed in the Library turns its switch off; the provider gone takes its switch with it. Run (mote, at this tree): - go test ./internal/library ./internal/provider ./internal/gui: library ok; provider ok but TestRenewLoginsKeepsClaudeReading (#5481, red the same at the base); gui ok but TestMainWindowCorners (#3949). The first gui run had TestCoreScriptBudget red (app.js over by ~1.2KB); the new code was made compact and seven editor comment blocks cut to one line each. Rebased on09812e66bthe core was 68 bytes over again, so three more comments near the editor (decidesModel, modelAPIs, pickedOf) were cut: app.js is 161 bytes smaller than at its base, and the budget (not raised) passes there: go test -run 'TestCoreScriptBudget|Provider' ./internal/gui ok. - go vet ./..., GOOS=windows go vet ./..., GOOS=linux go vet -tags nogui ./...: ok; gofmt -l clean. - node --test remote-magpie (chromium + webkit, en + zh), i18n-keys, i18n-en-no-cjk, gui-de, gui-ja, gui-zh-tw, preset-name, name-suffix-remote, remote-self, provider-unmasked, key-row-chips-fit, ctx-head-narrow, routing-remote-windows, usage-remote-cards, old-webkit: all green except reds that are red the same at the base1ccad582dand filed: remote-magpie's "asks for the other magpie's address" fails at its decision-model chips before reaching anything this changes (#5013, #4398), and i18n-keys' "a plugin's words are its own" (#5332, #5426, #5482). With that one chips assertion bypassed in a local copy, remote-magpie's new assertions are 12 of 12 green. name-suffix-remote went red in Chromium (zh, zh-TW, de) beside 10 other files, its scrollends.js import failing (#5147); alone it is 10 of 10 green. The switch's label in the editor is the existing "MCP servers". Broken by hand: - the Was mapping off: TestRemoteMagpieMCPFollowsRename red ("renamed home, read before a sync: switch map[]"). - bridgeWanted/adoptBridge not asking bridgesOn: TestRemoteMagpieMCPIsOptIn red ("switched off, asked"). - the new editor's switch defaulting on: remote-magpie red ("off when added") in both engines. Not tried: a real rename against a real second magpie (the library tests fake RemoteHubs; the provider test renames a real providers.json in a sandbox and checks Was reaches RemoteHubs). The CLI (provider add/set remote-magpie) and the TUI have no switch: from there a new Remote magpie gets no MCP server until it is switched on in the GUI. Reported-by: treeman_26897 (Discord)
123 lines
3.7 KiB
Go
123 lines
3.7 KiB
Go
package provider
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"path/filepath"
|
|
"reflect"
|
|
"slices"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/catalog"
|
|
)
|
|
|
|
// A Remote magpie whose models are listed is asked, with its key, whether
|
|
// its router has a server to give, while the library would give it one
|
|
// (RemoteHubAsk); one that says so is noted (RemoteHubFound). One that
|
|
// doesn't say, a magpie from before it, isn't; nor one the library
|
|
// doesn't ask about, nor one found to be this magpie. RemoteHubs lists the
|
|
// Remote magpies with their address and key, and fails while
|
|
// providers.json can't be read.
|
|
func TestRemoteHubNoted(t *testing.T) {
|
|
isolate(t)
|
|
h := t.TempDir()
|
|
t.Setenv("HOME", h)
|
|
t.Setenv("USERPROFILE", h)
|
|
t.Setenv("XDG_CONFIG_HOME", filepath.Join(h, ".config"))
|
|
t.Setenv("XDG_CACHE_HOME", filepath.Join(h, ".cache"))
|
|
var mu sync.Mutex
|
|
info, lister := `{"name":"magpie","mcp_hub":true}`, "another"
|
|
var asked []string // the info's requests, by key
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
mu.Lock()
|
|
defer mu.Unlock()
|
|
w.Header().Set("Content-Type", "application/json")
|
|
if r.URL.Path == "/v1" {
|
|
asked = append(asked, r.Header.Get("Authorization"))
|
|
io.WriteString(w, info)
|
|
return
|
|
}
|
|
io.WriteString(w, `{"object":"list","data":[{"id":"own/m1","object":"model","magpie_via":["`+lister+`"]}]}`)
|
|
}))
|
|
t.Cleanup(srv.Close)
|
|
id, err := Add(Provider{ID: "rem", Name: "Rem", Preset: RemoteMagpiePreset, Key: "k1", Chat: strings.TrimPrefix(srv.URL, "http://")})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
want := true
|
|
var found []string
|
|
oldA, oldF := RemoteHubAsk, RemoteHubFound
|
|
t.Cleanup(func() { RemoteHubAsk, RemoteHubFound = oldA, oldF })
|
|
RemoteHubAsk = func(string) bool { return want }
|
|
RemoteHubFound = func(id string) { found = append(found, id) }
|
|
fetch := func() {
|
|
t.Helper()
|
|
p, err := Find(id)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := p.Fetch(context.Background()); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
reset := func() { mu.Lock(); asked = nil; mu.Unlock(); found = nil }
|
|
|
|
fetch()
|
|
if !slices.Equal(found, []string{"rem"}) || !slices.Equal(asked, []string{"Bearer k1"}) {
|
|
t.Fatalf("found %v, asked %v; want rem, with its key", found, asked)
|
|
}
|
|
hs, err := RemoteHubs()
|
|
if err != nil || len(hs) != 1 || !reflect.DeepEqual(hs[0], RemoteHub{ID: "rem", Name: "Rem", Root: srv.URL, Key: "k1"}) {
|
|
t.Fatalf("RemoteHubs: %+v %v", hs, err)
|
|
}
|
|
// renamed (treeman_26897): listed under its id now, with the one it
|
|
// had, which its Library server follows
|
|
if err := Rename("rem", "office"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
id = "office"
|
|
if hs, err := RemoteHubs(); err != nil || len(hs) != 1 || hs[0].ID != "office" || !slices.Equal(hs[0].Was, []string{"rem"}) {
|
|
t.Fatalf("RemoteHubs after a rename: %+v %v", hs, err)
|
|
}
|
|
// its switch turned on: asked at once, under its id now
|
|
reset()
|
|
AskRemoteHub(context.Background(), "office")
|
|
if !slices.Equal(found, []string{"office"}) || len(asked) != 1 {
|
|
t.Fatalf("asked at once: found %v, asked %v", found, asked)
|
|
}
|
|
|
|
reset()
|
|
want = false
|
|
fetch()
|
|
if len(found) != 0 || len(asked) != 0 {
|
|
t.Errorf("not wanted: found %v, asked %v", found, asked)
|
|
}
|
|
|
|
reset()
|
|
want = true
|
|
mu.Lock()
|
|
info = `{"name":"magpie","version":"v0.1.1"}`
|
|
mu.Unlock()
|
|
fetch()
|
|
if len(found) != 0 || len(asked) != 1 {
|
|
t.Errorf("a magpie from before: found %v, asked %v", found, asked)
|
|
}
|
|
|
|
// this magpie itself
|
|
reset()
|
|
mu.Lock()
|
|
info, lister = `{"name":"magpie","mcp_hub":true}`, catalog.Self()
|
|
mu.Unlock()
|
|
fetch()
|
|
if len(found) != 0 || len(asked) != 0 {
|
|
t.Errorf("this magpie: found %v, asked %v", found, asked)
|
|
}
|
|
if hs, err := RemoteHubs(); err != nil || len(hs) != 0 {
|
|
t.Errorf("RemoteHubs with this magpie: %+v %v", hs, err)
|
|
}
|
|
}
|