Files
magpie/internal/gateway/agentpath.go
T
yetone 93a33d8174 agent: OpenBot's icon, its requests named its own, its skills in the Library (SSNQT on X)
SSNQT on X, magpie 0.1.3503 on macOS, OpenBot (611ce90d1): no icon in
the Agents list, its requests listed as OpenCode's, and no way to pick
it for skills or MCP servers in the Library.

- Icon: OpenBot's production icon (build/icon-production.png in
  nightly-labs/openbot, 128px) as icons/openbot.png, in pngIcons and in
  app.css's tile rule (a solid tile, icon-tile.test.cjs); openbot is off
  client_icons_test's generic list.
- Requests: OpenBot seals an endpoint's key and headers in safeStorage,
  which magpie can't write, and hands the endpoint to the OpenCode it
  runs, so every request came with OpenCode's User-Agent and nothing of
  OpenBot's. magpie's entry is now at <gateway>/agent/openbot/v1
  (gateway.AgentURL). lanGuard takes /agent/<id> off the path when <id>
  is an agent magpie knows (takeAgentPath), agentOf names that agent
  first, and startedByAgent never takes such a request for a script. An
  unknown id is left in the path (404 as before). An entry written at
  /v1 is rewritten at the next sync; OpenBot reads it after a restart,
  which its notice already says. Requests recorded before stay
  OpenCode's.
- Library: an openbot target with skills in ~/.agents/skills, the only
  folder OpenBot reads (home-skills.ts, HOME_SKILL_FOLDER). Its MCP
  servers are rows in its SQLite database (openbot.db,
  projection_mcp_servers), which magpie doesn't write, so it stays out
  of the MCP picker: #5896.
- Guard: TestEveryAgentInLibrary fails for a built-in agent that has no
  Library target and isn't in noLibrary with a reason. 34 agents had
  none when it landed; they are listed as "not looked at (#5897)". The
  icon guard (TestEveryClientHasNameAndIcon) and the agent-name guard
  (TestIsAgentByUserAgent) were there already.

Run (mote unless said): gofmt -l internal (clean); go vet darwin, GOOS=linux
-tags nogui, GOOS=windows on agent, gateway, library, gui: ok;
go test -count=1 ./internal/agent ./internal/gateway ./internal/library
./internal/gui ./internal/usage: all ok but internal/gui's
TestUpdateSlowDownloadOutlastsTheCheckBound (its check request hit its
deadline with four other suites on the box; passes 3 of 3 alone; the
updater isn't touched here; #5905); TestAgentInPath and
TestEveryAgentInLibrary on Windows (win-run): ok; GUI i18n-keys,
icon-tile, icon-one-tone, icon-builtin-over-address,
routing-client-icons, library-pick-remove: chromium 101/101, webkit
54/54. Real gateway (nogui build, sandbox HOME, port 3591): a POST to
/agent/openbot/v1/chat/completions with OpenCode's User-Agent was
recorded as agent openbot, the same at /v1 as opencode, and
/agent/openbot/v1/models answered the list.

Broken by hand, each red: the takeAgentPath call in lanGuard taken out
(TestAgentInPath: path kept, agent opencode); the pathAgent check in
agentOf taken out (agent opencode); the openbot case in targetOf renamed
(TestEveryAgentInLibrary: openbot has no place); Icon back to generic
(TestEveryClientHasNameAndIcon).

Not tried: real OpenBot or a real OpenCode sending to the new address
(the URL is built as `${baseURL}/chat/completions` by
@ai-sdk/openai-compatible, read in its source); the full suite (runs
after the tag).
2026-11-10 15:56:21 +08:00

58 lines
1.9 KiB
Go

package gateway
import (
"context"
"net/http"
"strings"
"github.com/yetone/magpie/internal/usage"
)
// AgentPath is the prefix of a gateway address that names the agent
// calling it, for an agent that can be given neither a key of its own
// (TokenFor) nor a User-Agent: OpenBot seals the key of an endpoint it
// lists in safeStorage, which magpie doesn't open, and hands the endpoint
// to the OpenCode it runs, whose requests say "opencode/…". So its
// requests were counted and listed as OpenCode's (SSNQT on X). Given
// <gateway>/agent/openbot/v1, the gateway reads /agent/openbot off the
// path and serves the rest as it is.
const AgentPath = "/agent/"
// AgentURL is the gateway at gw, as the agent id reaches it (AgentPath).
func AgentURL(gw, id string) string { return strings.TrimRight(gw, "/") + AgentPath + id }
// agentPathCtx holds the agent a request's path named (takeAgentPath).
type agentPathCtx struct{}
// takeAgentPath is r without the AgentPath its path begins with, the agent
// it names kept for agentOf: /agent/openbot/v1/chat/completions is
// /v1/chat/completions from OpenBot. A path naming no agent magpie knows is
// left as it is.
func takeAgentPath(r *http.Request) *http.Request {
rest, ok := strings.CutPrefix(r.URL.Path, AgentPath)
if !ok {
return r
}
id, rest, _ := strings.Cut(rest, "/")
if id == "" || !usage.IsAgent(id) {
return r
}
r2 := r.Clone(context.WithValue(r.Context(), agentPathCtx{}, usage.AgentOf(id)))
r2.URL.Path = "/" + rest
if raw := r2.URL.RawPath; raw != "" {
if rawRest, ok := strings.CutPrefix(raw, AgentPath+id); ok {
r2.URL.RawPath = "/" + strings.TrimPrefix(rawRest, "/")
} else {
r2.URL.RawPath = ""
}
}
r2.RequestURI = r2.URL.RequestURI()
return r2
}
// pathAgent is the agent r's path named (takeAgentPath), or "".
func pathAgent(r *http.Request) string {
id, _ := r.Context().Value(agentPathCtx{}).(string)
return id
}