diff --git a/internal/agent/agent.go b/internal/agent/agent.go index 80064d7b1..637a7b06f 100644 --- a/internal/agent/agent.go +++ b/internal/agent/agent.go @@ -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 diff --git a/internal/agent/agents.go b/internal/agent/agents.go index 5c2192b9a..4d77b416d 100644 --- a/internal/agent/agents.go +++ b/internal/agent/agents.go @@ -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")}, diff --git a/internal/agent/claude.go b/internal/agent/claude.go index cf4c076e7..96b9dd733 100644 --- a/internal/agent/claude.go +++ b/internal/agent/claude.go @@ -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, } diff --git a/internal/agent/codex.go b/internal/agent/codex.go index 1d61159d1..88c2e62d1 100644 --- a/internal/agent/codex.go +++ b/internal/agent/codex.go @@ -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 { diff --git a/internal/agent/commandcode.go b/internal/agent/commandcode.go index d8cea1b21..6a3223a8e 100644 --- a/internal/agent/commandcode.go +++ b/internal/agent/commandcode.go @@ -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."} diff --git a/internal/agent/dsh.go b/internal/agent/dsh.go index de6379ce9..6c9de2f5b 100644 --- a/internal/agent/dsh.go +++ b/internal/agent/dsh.go @@ -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 diff --git a/internal/agent/gemini.go b/internal/agent/gemini.go index 61a33c3ad..308396347 100644 --- a/internal/agent/gemini.go +++ b/internal/agent/gemini.go @@ -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( |$)`) { diff --git a/internal/agent/omp.go b/internal/agent/omp.go index a568ac6dd..c554aa58c 100644 --- a/internal/agent/omp.go +++ b/internal/agent/omp.go @@ -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`) { diff --git a/internal/agent/useragent_test.go b/internal/agent/useragent_test.go new file mode 100644 index 000000000..aa917fdc2 --- /dev/null +++ b/internal/agent/useragent_test.go @@ -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) + } + } +} diff --git a/internal/usage/usage.go b/internal/usage/usage.go index fcdc99db4..e23d818e0 100644 --- a/internal/usage/usage.go +++ b/internal/usage/usage.go @@ -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. diff --git a/internal/usage/usage_test.go b/internal/usage/usage_test.go index a993ef0e1..e672bf184 100644 --- a/internal/usage/usage_test.go +++ b/internal/usage/usage_test.go @@ -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{