Files
magpie/internal/provider/remote_agent_models.go
T
yetone b1dd96ab14 agents: a client of a Remote magpie starts its agents on the remote's models and follows them (KoiLx0 on X)
KoiLx0 runs several computers on one remote magpie and had to set every
agent's models again on each of them, and again whenever the remote
changed. Routing groups already reach a client (a group is a model of the
remote's list, TestRemoteMagpieNewGroupServedBesidePicks); the agents'
picks did not.

- The remote tells, per connected agent, each model field's catalog ref
  at GET /v1/magpie/agent-models (gateway.SharedAgentModels,
  agent.SharedModels). Guarded as the Library is: this computer, or a
  full-access key of a magpie shared on the network; a held key is
  refused (readonly_test), and another machine without a key gets 401
  (TestNetworkGatewayGuardsEveryRoute).
- An agent connected here starts on those picks where this magpie lists
  them as <remote id>/<ref> (How "remote", after "again" and "joined";
  the Agents page says "as on the remote magpie", in all five languages).
- A field taken from the remote, or found on its pick, is recorded in the
  stash and follows the remote's change every 5 minutes
  (FollowRemotePicks from library.KeepLinked). A model picked here is the
  user's: the record goes, and the field is left alone until it is on the
  remote's pick again. A pick this magpie doesn't list is skipped.
- app.js budget: no raise. The new line's 76 bytes come from same and
  alike sharing one string (CONN_WAS) and the native joined case
  returning plain instead of the same t() again.

Run at this head (rebased on c38753a7e, i18n-keys ok there; before that on b010fba05, whose budget raise left 2 bytes;
CONN_WAS's short name gives them):
- mote: go test -count=1 ./internal/agent ./internal/gui ./internal/library
  ./internal/provider: all ok.
- mica: go test ./internal/gateway (whole package, at the pre-rebase tree,
  ok 920s); at this head go build ./... and the gateway's guard, read-only
  and Library tests: ok.
- mica: GOOS=linux and GOOS=windows go vet of agent, provider, gateway,
  library: ok. gofmt -l: clean.
- mica at this head: go test ./internal/gui (budget ok), i18n-keys,
  agent-leave-magpie, agent-connect in chromium + webkit: ok.
- mica, chromium + webkit, before the last rebase: i18n-keys, i18n-en-no-cjk, agent-leave-magpie,
  agent-connect, agent-connect-no-models, agent-connected-unfolded: 0 fail.

Broken by hand: with connectRemote cut out of connect(),
TestConnectFollowsTheRemoteMagpiesPicks fails (connected on
aaa/claude-opus-5-5 (alike)); with the follow's "still the recorded value"
check made always true, it fails on the user's pick written over.

Not tried: two real computers; only a fake remote in the tests. Left for
#6824: a headless `magpie serve` client doesn't follow later changes
(the loop runs in the GUI), an empty field the remote fills later isn't
filled, and WSL / claude#name agent ids don't match across machines.
2026-11-16 14:18:47 +08:00

80 lines
2.6 KiB
Go

package provider
import (
"context"
"encoding/json"
"net/http"
"strings"
"time"
)
// RemoteAgentModelsPath is where a remote magpie tells the models its
// agents are on (KoiLx0 on X: several computers on one remote magpie, each
// setting every agent's models again by hand, and again after each change
// there). It is answered as the Library is: to the computer itself and to
// a full-access key of a magpie shared on the local network.
const RemoteAgentModelsPath = "/v1/magpie/agent-models"
// RemoteAgentModelsWait is how long a remote magpie has to tell its picks.
const RemoteAgentModelsWait = 8 * time.Second
const errRemoteNoAgentModels = "remote magpie doesn't tell its agents' models; update magpie on that computer"
// AgentModels is what a magpie tells of its agents' models: by agent id,
// each model field's key and the catalog ref it is on there (group/smart,
// deepseek/deepseek-chat).
type AgentModels struct {
Agents map[string]map[string]string `json:"agents"`
}
// RemotePicks is one Remote magpie's AgentModels, under its provider id
// here: a ref there is Provider+"/"+ref in this magpie's catalog.
type RemotePicks struct {
Provider string
Agents map[string]map[string]string
}
// pickedMagpies are the Remote magpies whose agents' picks are followed:
// on, with an address and a key, and not this magpie itself.
func pickedMagpies() []Provider {
var out []Provider
for _, p := range storedFile().Providers {
if strings.TrimSpace(p.Anthropic) == "" {
continue
}
if p = normalize(p); !hasEndpoint(p) {
continue
}
if p.IsRemoteMagpie() && !p.Off && p.Anthropic != "" && p.Key != "" && !p.SelfLeftOut() {
out = append(out, p)
}
}
return out
}
// HasPickedMagpie reports whether a Remote magpie's picks could be asked:
// a caller with nothing to follow asks no one.
func HasPickedMagpie() bool { return len(pickedMagpies()) > 0 }
// RemoteAgentPicks asks every Remote magpie, in the order they were added,
// the models its agents are on. One that can't tell (unreachable, older,
// a held key) is left out, its error in errs by provider id.
func RemoteAgentPicks(ctx context.Context) (out []RemotePicks, errs map[string]error) {
for _, p := range pickedMagpies() {
b, err := remoteBodyAs(ctx, p, http.MethodGet, RemoteAgentModelsPath, RemoteAgentModelsWait, 1<<20, errRemoteNoAgentModels)
var m AgentModels
if err == nil {
err = json.Unmarshal(b, &m)
}
if err != nil {
if errs == nil {
errs = map[string]error{}
}
errs[p.ID] = err
continue
}
out = append(out, RemotePicks{Provider: p.ID, Agents: m.Agents})
}
return out, errs
}