Files
magpie/internal/gui/gatewaymode.go
T
yetone 1c0ecec0f6 library: a magpie in Docker keeps skills and prompts for its clients
fenglian980510 on X: in the Docker gateway the Library had MCP and no
Skills. Since f285426ac a page that shows no agent (no agents found, as
in a container, or gateway mode) drew the MCP tab alone. 5a9b5720d then
let another computer's magpie take a remote's skills and prompts (Use its
skills and prompts, GET /v1/magpie/library), but a Docker magpie had no
way to hold any, and anything it held was ticked for no agent, so Borrow
gave it to none on the client. The borrow change didn't re-derive the
agentless page.

- client.js: with no agent shown, the Library has Instructions, MCP
  servers and Skills (AGENTLESS_TABS), with nothing of agents (no chips,
  agents' own skills, projects or Agents list) and a line saying a
  magpie that adds this one as a provider takes them. It opens on MCP
  unless the reader picked one of the three. Strings in zh, zh-TW, ja, de.
- library: Shared says NoAgents when the page shows none (NoAgentsShown,
  set by gui.gatewayMode). Borrow gives such a new skill or set ticked
  for none to the agents here that take skills or instructions and
  aren't hidden (givenHere); one ticked for agents keeps them.
- docs: docker.html en/zh/ja (zh and ja still said there is no Library
  at all), reference.md, library, gui-shell and mcp-router pages; the
  search index rebuilt.

Rebased on origin/main 5cf1d3846 (runs below at 589e4ba6d; the five
commits since touch none of these files but gui-shell.md, which
rebased clean, and docs-index.mjs writeAll left the index unchanged).
The search index was rebuilt through
docs-index.mjs's writeAll after the rebase). What was run:
- At this head, on Linux (WSL magpie-desktop): gofmt -l (0 files),
  GOOS=linux go vet -tags nogui ./..., GOOS=windows go vet ./...: ok
  (mote and mica were over their load bars).
- At this head, on WSL: go test -tags nogui ./internal/library
  ./internal/gui ./internal/gateway ./internal/provider
  ./internal/testenv -count=1 -timeout 45m: all ok.
- At this head, on WSL: library-agentless-skills, library-mcp-agentless,
  i18n-keys, gateway-mode*, library-* and i18n-* (both engines, every
  language the files run): 1975 of 2010 ok. The 35 reds are in 11
  tests, all red on main too: library-all, library-commands-import,
  library-group-steady, library-mcp-copy-config, library-mcp-off-fold,
  library-open-again (#7097), library-bili (#6015: 82/2 alone, with and
  without this change), gateway-model-chip-height (#7108: red 2 of 3
  alone at main). library-bili-settings and library-mcp-json pass alone
  at both (22/22, 32/32).
- At 998e40c85 (the same change on origin/main 48fffb60c), on mica
  (load 25-35 on 12 cores): go test ./... -timeout 45m, 100 packages ok
  and 3 red, each also red or passing alone at the base 534b85bf9:
  internal/dirent (4 tests, red alone at both, #7031), internal/gui
  TestGatewayModeListsNoOwnSessions (passes alone 3/3, #7064),
  internal/sessions TestWSLStoppedDistroKept (passes alone, #7071).
  gofmt and darwin/linux/windows vet ok there too.
- At 998e40c85, on WSL: 239 GUI files that reach the Library (library-*,
  i18n-*, gateway-mode*, web-page*, any file naming the Library):
  4530 of 4688 ok. The 24 red files rerun at concurrency 2: 151 red at
  998e40c85, 155 at 48fffb60c, the same files (#7097, with #6990,
  #4566, #3922, #6570, #4716 for segs-press-redraw, old-webkit and
  title-bar-tools).
- Not on macOS this round: mote and mica each had another session's full
  GUI suite running all the while.

Broken by hand:
- Borrow's two NoAgents branches set to false:
  TestBorrowFromAMagpieWithNoAgents red ("a skill ticked for none there
  went to [] here, not [claude gemini]").
- origin/main's client.js and i18n.js: library-agentless-skills and
  library-mcp-agentless red at "the tabs", 44 of 44.

Not tried: a real Docker magpie with a second magpie borrowing from it
across two machines. site/search-index.mjs run as a script hangs on its
circular import of docs-index.mjs (as on main); the index was written
through docs-index.mjs's writeAll.
2026-11-18 21:29:39 +08:00

166 lines
5.2 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package gui
import (
"encoding/json"
"fmt"
"net/http"
"os"
"slices"
"sync"
"sync/atomic"
"time"
"github.com/yetone/magpie/internal/agent"
"github.com/yetone/magpie/internal/library"
"github.com/yetone/magpie/internal/sessions"
"github.com/yetone/magpie/internal/settings"
)
// Gateway mode (Player on Discord): `magpie web` on a server that is only
// the gateway for other machines' agents shows what such a gateway needs —
// Providers, Gateway, Routing, Usage, Sessions, Plugins, Settings and the
// Library's MCP servers with the router (treeman_26897) — and leaves out
// what is this machine's agents' (Agents, the Library's other tabs, the settings
// written into agents' files) and its desktop's (the tray, the Dock, open
// at login, notifications). It is the browser page's alone: the app's
// windows show everything whatever it says.
//
// Settings' choice (settings.GatewayMode, "on" or "off") wins; with none,
// `magpie web --gateway` turns it on, and so does a magpie web with no
// agent on its machine (a container, a NAS). Settings › General turns it
// off again, which brings the pages back.
// WebGateway is `magpie web --gateway`'s.
var WebGateway atomic.Bool
// webPage is set once the page is served by `magpie web`: settingsState,
// which has no Windows, tells the page its gateway mode by it.
var webPage atomic.Bool
// gatewayMode says whether the page is in gateway mode, and why: "on" or
// "off" (Settings' choice), "flag" (magpie web --gateway), "no-agents", or
// "" (the app's windows, or magpie web on a machine with agents).
func gatewayMode(web bool) (bool, string) {
if !web {
return false, ""
}
switch m := settings.Shared().GatewayMode; m {
case "on", "off":
return m == "on", m
}
if WebGateway.Load() {
return true, "flag"
}
if !agentsHere() {
return true, "no-agents"
}
return false, ""
}
// agentsHere says whether an agent is found on this computer, stopping at
// the first: each agent's check can read app bundles and ask Spotlight.
// WSL's agents are those last listed: a hung wsl.exe doesn't hold the
// Sessions page (sessions.Dirs asks this). A test's own.
var agentsHere = func() bool { return slices.ContainsFunc(agent.AllKnown(), (*agent.Agent).Detected) }
// gatewayModeRoutes: POST /api/settings/gateway-mode {"mode": "on" | "off"
// | ""} sets Settings' choice, "" leaving it to magpie again.
func gatewayModeRoutes(mux *http.ServeMux) {
mux.HandleFunc("POST /api/settings/gateway-mode", func(rw http.ResponseWriter, r *http.Request) {
var in struct {
Mode string `json:"mode"`
}
if err := json.NewDecoder(r.Body).Decode(&in); err != nil {
fail(rw, err)
return
}
if in.Mode != "" && in.Mode != "on" && in.Mode != "off" {
fail(rw, fmt.Errorf("gateway mode is on, off or \"\" (automatic), not %q", in.Mode))
return
}
s := settings.Load()
s.GatewayMode = in.Mode
if err := settings.Save(s); err != nil {
fail(rw, err)
return
}
// the Sessions page lists this computer's agents' sessions, or
// stops, at once (ownSessionsOff)
ownSessionsOff()
forgetOwnSessions()
writeJSON(rw, settingsState())
})
}
// ownSessionsOff is gatewayMode as the sessions' gate asks it
// (sessions.OwnOff; paultian on Discord): in gateway mode the Sessions page
// and Usage › Sessions list what came through this gateway, never this
// computer's own agents' session files, which a machine put in gateway
// mode with agents installed still listed. It is asked on every read of a
// session, so it reads the shared settings and an agents look kept a while
// (agentsHereKept) rather than gatewayMode's own. When its answer turns,
// what was read under the other answer is dropped (ownTurned).
func ownSessionsOff() bool {
on := false
if webPage.Load() {
switch settings.Shared().GatewayMode {
case "on":
on = true
case "off":
default:
on = WebGateway.Load() || !agentsHereKept()
}
}
ownTurned(on)
return on
}
// ownWas is ownSessionsOff's last answer: 0 none yet, 1 off, 2 on.
var ownWas atomic.Int32
// ownTurned drops the sessions read, and the stats made, under the other
// answer, off the caller's goroutine: the gate is asked under the
// sessions' own lock, which Reset takes.
func ownTurned(on bool) {
v := int32(1)
if on {
v = 2
}
if was := ownWas.Swap(v); was != 0 && was != v {
go forgetOwnSessions()
}
}
func forgetOwnSessions() {
sessions.Reset()
forgetStats()
}
// agentsHereKept is agentsHere looked at most every half minute: it looks
// for every agent's files and commands. Another HOME or PATH (a test's
// sandbox) is looked at afresh.
var agentsSeen struct {
sync.Mutex
at time.Time
on string // the HOME and PATH it was looked for in
here bool
}
func agentsHereKept() bool {
agentsSeen.Lock()
defer agentsSeen.Unlock()
on := os.Getenv("HOME") + "\x00" + os.Getenv("PATH")
if agentsSeen.at.IsZero() || agentsSeen.on != on || time.Since(agentsSeen.at) > 30*time.Second {
agentsSeen.here, agentsSeen.at, agentsSeen.on = agentsHere(), time.Now(), on
}
return agentsSeen.here
}
func init() {
f := ownSessionsOff
sessions.OwnOff.Store(&f)
// the browser page in gateway mode ticks a skill or the prompts for
// no agent: a magpie that uses this one's Library gives them to its own
library.NoAgentsShown = func() bool { on, _ := gatewayMode(true); return on }
}