agent + usage: each agent says what its User-Agent begins with, beside its name and icon, and the gateway tells requests apart by that — usage kept a list of its own, which had to be kept in step with the agents by hand and wasn't (DeepSeek Harness, omp and Command Code were missing from it, so they were drawn with no logo); a new agent is now known by its requests as soon as it is described

This commit is contained in:
yetone
2026-09-25 05:51:32 +08:00
parent 658d264e14
commit a9e1a6769a
11 changed files with 78 additions and 37 deletions
+3
View File
@@ -52,6 +52,9 @@ type Agent struct {
// Notice, if set, is advice worth showing after a change: agents that
// read their config once at start-up need a restart to see it.
Notice func() string
// UA is what the agent's User-Agent begins with, lower-case: how the
// gateway tells its requests from others'
UA []string
}
// Running reports whether a process whose command line matches any pattern
+18
View File
@@ -13,8 +13,20 @@ import (
"github.com/yetone/magpie/internal/edit"
"github.com/yetone/magpie/internal/gateway"
"github.com/yetone/magpie/internal/provider"
"github.com/yetone/magpie/internal/usage"
)
// The gateway knows each agent's requests by what this package says of it.
func init() {
usage.Agents = func() []usage.Known {
var out []usage.Known
for _, a := range All() {
out = append(out, usage.Known{ID: a.ID, Names: append([]string{a.ID}, a.Aliases...), UA: a.UA})
}
return out
}
}
// All returns every agent magpie knows about, detected or not.
func All() []*Agent {
home, _ := os.UserHomeDir()
@@ -240,6 +252,7 @@ func opencode(home, cfg string) *Agent {
}
return &Agent{
ID: "opencode", Name: "OpenCode", Icon: "opencode", Aliases: []string{"oc"},
UA: []string{"opencode"},
Bin: "opencode", Dir: dir, Path: path,
Fields: []Field{
{Key: "model", Label: "model", Get: jsonGet(path, "model"), Set: set("model"), Options: opts("model")},
@@ -261,6 +274,7 @@ func pi(home string) *Agent {
}
return &Agent{
ID: "pi", Name: "Pi", Icon: "pi", Bin: "pi", Dir: dir, Path: path,
UA: []string{"pi-"},
Fields: []Field{
{
Key: "model", Label: "model",
@@ -320,6 +334,7 @@ func goose(home, cfg string) *Agent {
set := func(kvs ...edit.KV) error { return edit.SetYAMLTop(path, kvs...) }
return &Agent{
ID: "goose", Name: "Goose", Icon: "goose", Bin: "goose", Dir: filepath.Dir(path), Path: path,
UA: []string{"goose"},
Fields: []Field{{
Key: "model", Label: "model",
Get: pairGet(get, "GOOSE_PROVIDER", "GOOSE_MODEL"),
@@ -340,6 +355,7 @@ func cursor(home string) *Agent {
path := filepath.Join(home, ".cursor", "cli-config.json")
return &Agent{
ID: "cursor", Name: "Cursor", Icon: "cursor", Aliases: []string{"cursor-agent"},
UA: []string{"cursor"},
Bin: "cursor-agent", Dir: filepath.Dir(path), Path: path,
Fields: []Field{{
Key: "model", Label: "model",
@@ -367,6 +383,7 @@ func copilot(home string) *Agent {
path := filepath.Join(dir, "settings.json")
return &Agent{
ID: "copilot", Name: "Copilot CLI", Icon: "githubcopilot", Aliases: []string{"gh-copilot"},
UA: []string{"copilot", "github-copilot"},
Bin: "copilot", Dir: dir, Path: path,
Fields: []Field{{
Key: "model", Label: "model",
@@ -436,6 +453,7 @@ func crush(home, cfg string) *Agent {
}
return &Agent{
ID: "crush", Name: "Crush", Icon: "crush", Bin: "crush", Dir: filepath.Dir(path), Path: path,
UA: []string{"crush"},
Fields: []Field{
{Key: "model", Label: "large", Get: pairGet(get, "models.large.provider", "models.large.model"), Set: setter("models.large.provider", "models.large.model"), Options: opts("model")},
{Key: "small", Label: "small", Get: pairGet(get, "models.small.provider", "models.small.model"), Set: setter("models.small.provider", "models.small.model"), Options: opts("small")},
+1
View File
@@ -192,6 +192,7 @@ func claude(home string) *Agent {
return &Agent{
ID: "claude", Name: "Claude Code", Icon: "claudecode-color", Aliases: []string{"cc", "claude-code"},
UA: []string{"claude-cli", "claude-code"},
Bin: "claude", Dir: filepath.Dir(path), Path: path,
Fields: fields,
}
+1
View File
@@ -163,6 +163,7 @@ func codex(home string) *Agent {
return &Agent{
ID: "codex", Name: "Codex", Icon: "codex-color", Bin: "codex", Dir: dir, Path: path,
UA: []string{"codex"},
// the app-server behind the Codex app (and every codex TUI) builds
// its model list once, at start-up.
Notice: func() string {
+1
View File
@@ -32,6 +32,7 @@ func commandCode(home string) *Agent {
}
return &Agent{
ID: "commandcode", Name: "Command Code", Icon: "commandcode", Aliases: []string{"command-code", "cmd"},
UA: []string{"command-code", "commandcode"},
Bin: "command-code", Dir: dir, Path: path,
Notice: func() string {
notes := []string{"Command Code wants its own sign-in (cmd login) even for models through magpie."}
+1
View File
@@ -49,6 +49,7 @@ func dsh(home string) *Agent {
}
return &Agent{
ID: "dsh", Name: "DeepSeek Harness", Icon: "deepseek-color", Aliases: []string{"deepseek-harness"},
UA: []string{"deepseek-harness"},
Bin: "dsh", Dir: dir, Path: path,
Notice: func() string {
var notes []string
+1
View File
@@ -156,6 +156,7 @@ func gemini(home string) *Agent {
return &Agent{
ID: "gemini", Name: "Gemini CLI", Icon: "geminicli-color", Aliases: []string{"gemini-cli"},
UA: []string{"geminicli", "gemini-cli"},
Bin: "gemini", Dir: dir, Path: path,
Notice: func() string {
if Running(`(^|/)gemini( |$)`) {
+1
View File
@@ -55,6 +55,7 @@ func omp(home string) *Agent {
}
return &Agent{
ID: "omp", Name: "omp", Icon: "omp", Aliases: []string{"oh-my-pi"},
UA: []string{"oh-my-pi"},
Bin: "omp", Dir: dir, Path: path,
Notice: func() string {
if Running(`(^|/)omp( |$)`, `@oh-my-pi/pi-coding-agent`) {
+29
View File
@@ -0,0 +1,29 @@
package agent
import (
"testing"
"github.com/yetone/magpie/internal/usage"
)
// Every agent's requests are told by what its own description says.
func TestAgentOf(t *testing.T) {
cases := map[string]string{
"claude-cli/2.1.0 (external, cli)": "claude",
"codex_cli_rs/0.40.0 (Mac OS 26.0; arm64)": "codex",
"GeminiCLI/0.9.0 (darwin; arm64)": "gemini",
"opencode/1.2.3": "opencode",
"curl/8.4.0": "curl",
"oh-my-pi/1.0": "omp",
"command-code/2.0": "commandcode",
"deepseek-harness/0.3.1": "dsh",
"dsh": "dsh", // a record kept by an id already
"pi": "pi",
"": "other",
}
for ua, want := range cases {
if got := usage.AgentOf(ua); got != want {
t.Errorf("%q: got %q want %q", ua, got, want)
}
}
}
+22 -18
View File
@@ -10,6 +10,7 @@ import (
"encoding/json"
"os"
"path/filepath"
"slices"
"sort"
"strings"
"sync"
@@ -91,6 +92,24 @@ func Load(since time.Time) []Record {
return out
}
// Known is an agent as its requests name it.
type Known struct {
ID string
Names []string // its id and aliases, which a record may already carry
UA []string // what its User-Agent begins with
}
// Agents lists the agents magpie knows. Package agent sets it from each
// agent's own description, so they are listed in one place only.
var Agents func() []Known
var knownAgents = sync.OnceValue(func() []Known {
if Agents == nil {
return nil
}
return Agents()
})
// AgentOf names the agent behind a client User-Agent. Known agents map to
// their magpie id; anything else keeps its product name.
func AgentOf(ua string) string {
@@ -98,9 +117,9 @@ func AgentOf(ua string) string {
name, _, _ := strings.Cut(ua, "/")
name, _, _ = strings.Cut(name, " ")
l := strings.ToLower(name)
for _, k := range known {
if strings.HasPrefix(l, k.prefix) {
return k.id
for _, k := range knownAgents() {
if slices.Contains(k.Names, l) || slices.ContainsFunc(k.UA, func(p string) bool { return strings.HasPrefix(l, p) }) {
return k.ID
}
}
if name == "" {
@@ -109,21 +128,6 @@ func AgentOf(ua string) string {
return name
}
var known = []struct{ prefix, id string }{
{"claude-cli", "claude"}, {"claude-code", "claude"},
{"codex", "codex"},
{"geminicli", "gemini"}, {"gemini-cli", "gemini"},
{"opencode", "opencode"},
{"pi-", "pi"}, {"pi/", "pi"},
{"goose", "goose"},
{"cursor", "cursor"},
{"copilot", "copilot"}, {"github-copilot", "copilot"},
{"crush", "crush"},
{"deepseek-harness", "dsh"}, {"dsh", "dsh"},
{"oh-my-pi", "omp"}, {"omp", "omp"},
{"command-code", "commandcode"}, {"commandcode", "commandcode"},
}
// ---- summaries --------------------------------------------------------------
// Period is a window of time to sum over.
-19
View File
@@ -5,25 +5,6 @@ import (
"time"
)
func TestAgentOf(t *testing.T) {
cases := map[string]string{
"claude-cli/2.1.0 (external, cli)": "claude",
"codex_cli_rs/0.40.0 (Mac OS 26.0; arm64)": "codex",
"GeminiCLI/0.9.0 (darwin; arm64)": "gemini",
"opencode/1.2.3": "opencode",
"curl/8.4.0": "curl",
"deepseek-harness/0.3.1": "dsh",
"dsh": "dsh", // a record kept by an id already
"pi": "pi",
"": "other",
}
for ua, want := range cases {
if got := AgentOf(ua); got != want {
t.Errorf("%q: got %q want %q", ua, got, want)
}
}
}
func TestSummarize(t *testing.T) {
now := time.Date(2026, 9, 23, 15, 30, 0, 0, time.UTC)
recs := []Record{