Files
magpie/internal/gateway/remote_magpie.go
T
yetone 248a68598c gateway, provider, gui, cli: a key+URL provider may be sent the agent's own User-Agent in place of magpie's (#1851)
Every forward went with User-Agent magpie/<version>. A coding plan that
serves only the agents it names (pi, OpenCode, Claude Code…) knows them by
their User-Agent, so it turned magpie away.

What changed
- provider: PassUserAgent (passUserAgent in providers.json), off by
  default. PassesUserAgent takes it only for a key+URL provider: never a
  signed-in account, a plugin's, or a vendor whose own client magpie goes
  as (OwnClient: OpenCode, Cline, Kilo, another magpie). A copy keeps it.
- gateway: userAgentFor in forwardOnce (chat, Anthropic, Responses,
  Gemini, count_tokens) sends the inbound User-Agent unchanged where the
  provider passes it on. It sends magpie's where it doesn't, where the
  agent sent none, and on magpie's own requests (magpiesOwn: magpie/…,
  magpie-router/vision/search, the image tool's "<client> magpie-image/1",
  which is built from MCP clientInfo, not a User-Agent). magpie never
  invents another client's. The claude-cli/Codex header copies, the
  OpenCode/Cline/Kilo overrides and account signing run after it as
  before, and the user's header.User-Agent is put on at signing, so it
  wins.
- gateway: embeddings, images and videos (retrieveFrom, sendWith,
  remoteVideoContent) now stamp the same for a key+URL provider through
  stampUA, from the caller's User-Agent kept on the request ctx
  (caller.ua, withCaller). Before, they went with Go's default
  (Go-http-client/1.1). Account and remote-magpie paths are untouched.
- cli: `magpie provider set <id> user-agent=agent|magpie`, refused for a
  signed-in account; `magpie provider <id>` shows it.
- gui: the provider editor's User-Agent row, "Send the agent's own
  User-Agent", with an explanation, for a provider with a key only (the
  JSON says ownClient for presets and providers); en/zh/zh-TW/ja/de.
  The TUI only adds presets, so it has no field for this.
- docs: reference.md (provider set) and subsystems/gateway-routing.md.

What was run
- gofmt -l clean; go vet ./... on darwin, GOOS=linux -tags nogui and
  GOOS=windows.
- go test -count=1 ./internal/gateway ./internal/provider ./internal/cli
  ./internal/gui: all ok (sandbox HOME, private GOCACHE).
- TestUserAgentPassedOn covers 7 routes (messages, count_tokens, chat,
  responses, gemini, embeddings, images) x off / on / on with no UA / on
  with magpie-router/1 / on with the image tool's UA / the user's header
  with it on and off. TestUserAgentAccountUntouched: a Factory account
  with passUserAgent in providers.json is still asked as factory-cli/.
  Also TestUserAgentClaudeCodeAsBefore, TestUserAgentFor,
  TestPassesUserAgent, TestAddCopyPassUserAgent, TestProviderUserAgentPair.
- Broken by hand, each went red: forwardOnce back to magpie's UA (5
  routes red); stampUA removed from embeddings/draw (both red, seen as
  Go-http-client/1.1); PassesUserAgent ignoring accounts/OwnClient;
  magpiesOwn dropped (the internal UAs leaked on all 7 routes); the GUI
  row shown for OpenCode and Save not posting it.
- provider-user-agent.test.cjs: 60/60 in chromium and webkit, en, zh,
  zh-TW, ja, de, at 900 and 440 wide. The provider/editor/add/i18n GUI
  tests in chromium: 244/244.

Not run
- Not tried against a real coding plan that checks User-Agent, nor with a
  real agent through a live gateway. Videos have no test of their own
  (they go through sendWith, which the images test covers).
- The GUI suite was not run in full; the Windows box was not used.
2026-10-13 02:56:34 +08:00

142 lines
4.6 KiB
Go

package gateway
import (
"context"
"net"
"net/http"
"os"
"strings"
"sync"
"unicode"
"github.com/yetone/magpie/internal/usage"
)
// A request passed on to another computer's magpie (a Remote magpie
// provider) goes as magpie's own, with magpie's User-Agent and the other
// magpie's key, so that magpie knew it only as "magpie" (Jorben on Discord).
// It now names the agent it came from (AgentHeader) and the computer
// (ViaHeader), and the other magpie keeps them in its usage, as "Claude
// Code · via <computer>". They are a label and nothing more: what magpie
// does with the request — the models an agent is shown, its stand-ins, a
// routing group's rules — still goes by who sent it, and they are taken
// only from a magpie's request (its User-Agent), one a client on the
// network could send only with the key anyway, and could name itself any
// agent by its User-Agent as well. No vendor is ever sent them: they come
// off every request as it arrives, and go on only to a remote magpie.
const (
AgentHeader = "X-Magpie-Agent"
ViaHeader = "X-Magpie-Via"
)
// caller is who a request is recorded as: its agent, and the computer it
// was passed on from, "" for this one. session and native are the session
// the caller named (sessionOf's X-Magpie-Session, and the agent's own
// header nativeSessionOf takes, by its name), passed on to a remote magpie
// with the request (#672).
type caller struct {
agent, via string
session string
native [2]string // header, value
// ua is the User-Agent the request came with, as it came: the one a
// provider that passes the agent's on is sent (userAgentFor)
ua string
}
type callerCtx struct{}
// withCaller notes on each request who it is recorded as, and takes the
// headers that said so off it.
func withCaller(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
c := caller{agent: agentOf(r), session: boundedSession(r.Header.Get(SessionHeader)), ua: r.Header.Get("User-Agent")}
for _, h := range sessionHeaders {
if v := boundedSession(r.Header.Get(h)); v != "" {
c.native = [2]string{h, v}
break
}
}
if strings.HasPrefix(r.Header.Get("User-Agent"), "magpie/") {
if a := label(r.Header.Get(AgentHeader)); a != "" {
c.agent = usage.AgentOf(a)
c.via = label(r.Header.Get(ViaHeader))
if c.via == "" {
c.via, _, _ = net.SplitHostPort(r.RemoteAddr)
}
}
}
ctx := context.WithValue(r.Context(), callerCtx{}, c)
if searchingFrom(r) {
// another magpie's search: only the model itself searches
ctx = context.WithValue(ctx, searchingKey{}, true)
}
r.Header.Del(AgentHeader)
r.Header.Del(ViaHeader)
r.Header.Del(SearchingHeader)
next.ServeHTTP(w, r.WithContext(ctx))
})
}
// callerOf is who the request is recorded as.
func callerOf(r *http.Request) caller {
if c, ok := r.Context().Value(callerCtx{}).(caller); ok {
return c
}
return caller{agent: agentOf(r)}
}
// label is a header's name for an agent or a computer, if it is a fit one
// to show: short, one word of printable characters.
func label(v string) string {
v = strings.TrimSpace(v)
if v == "" || len(v) > 64 || strings.IndexFunc(v, func(r rune) bool { return !unicode.IsPrint(r) || unicode.IsSpace(r) }) >= 0 {
return ""
}
return v
}
// boundedSession is a session header's value as magpie keeps it: trimmed,
// at most 128 bytes, as sessionOf takes it.
func boundedSession(v string) string {
v = strings.TrimSpace(v)
return v[:min(len(v), 128)]
}
// passOnCaller names, on a request to a remote magpie, the agent it is for
// and the computer it comes from: the one it was passed on from, else this.
// The session the caller named goes on too, as it named it — magpie's
// X-Magpie-Session and the agent's own header (session_id, …) — so the
// other magpie records the call under it and keeps a conversation on its
// account by it rather than by its first message (#672). Only a remote
// magpie is sent them; a vendor never is.
func passOnCaller(ctx context.Context, req *http.Request) {
c, ok := ctx.Value(callerCtx{}).(caller)
if !ok {
return
}
if c.session != "" {
req.Header.Set(SessionHeader, c.session)
}
if c.native[0] != "" {
req.Header.Set(c.native[0], c.native[1])
}
if c.agent == "" {
return
}
req.Header.Set(AgentHeader, c.agent)
if c.via == "" {
c.via = hostName()
}
if c.via != "" {
req.Header.Set(ViaHeader, c.via)
}
}
// hostName is this computer's name as people know it (MacBook-Pro, not
// MacBook-Pro.local).
var hostName = sync.OnceValue(func() string {
h, _ := os.Hostname()
h, _, _ = strings.Cut(h, ".")
return label(h)
})