diff --git a/internal/plugin/github.go b/internal/plugin/github.go index a41d2eaf8..8b71939f8 100644 --- a/internal/plugin/github.go +++ b/internal/plugin/github.go @@ -34,10 +34,10 @@ type Tagged struct { License string `json:"license,omitempty"` // SPDX id Pushed time.Time `json:"pushed"` // from its package.json on the default branch: what bun will install - // it as, its version, and whether it is gateway middleware + // it as, its version, and whether it is gateway middleware or an agent Package string `json:"package,omitempty"` Version string `json:"version,omitempty"` - Kind string `json:"kind,omitempty"` // "middleware" when that is all it is + Kind string `json:"kind,omitempty"` // "middleware" or "agent" when that is all it is // its package.json's magpie.icon, as an installed plugin gives its // provider one: an https URL or a data:image URI, as it is written Icon string `json:"icon,omitempty"` @@ -211,6 +211,7 @@ func askTagged(ctx context.Context) ([]Tagged, error) { Exports json.RawMessage `json:"exports"` Magpie struct { Middleware string `json:"middleware"` + Agent string `json:"agent"` Icon string `json:"icon"` } `json:"magpie"` } @@ -224,8 +225,13 @@ func askTagged(ctx context.Context) ([]Tagged, error) { if ic := strings.TrimSpace(pj.Magpie.Icon); len(ic) <= 3<<19 && (strings.HasPrefix(strings.ToLower(ic), "https://") || strings.HasPrefix(strings.ToLower(ic), "data:image/")) { t.Icon = ic } - if strings.TrimSpace(pj.Magpie.Middleware) != "" && pj.Main == "" && len(pj.Exports) == 0 { - t.Kind = "middleware" + if pj.Main == "" && len(pj.Exports) == 0 { + switch { + case strings.TrimSpace(pj.Magpie.Middleware) != "": + t.Kind = "middleware" + case strings.TrimSpace(pj.Magpie.Agent) != "": + t.Kind = "agent" + } } // published to npm from this repository: npm's copy is the one // built to be installed (a repository often leaves its dist out, @@ -252,7 +258,9 @@ func askTagged(ctx context.Context) ([]Tagged, error) { // would never load (#1327) if IsGit(t.Spec) || !npmNames { entry := pj.Magpie.Middleware - if t.Kind != "middleware" { + if t.Kind == "agent" { + entry = pj.Magpie.Agent + } else if t.Kind != "middleware" { entry = pkgEntry(pj.Exports, pj.Main) if !IsGit(t.Spec) { entry = pkgEntry(nil, "") diff --git a/internal/plugin/github_test.go b/internal/plugin/github_test.go index accddea3c..990cb195b 100644 --- a/internal/plugin/github_test.go +++ b/internal/plugin/github_test.go @@ -17,7 +17,8 @@ import ( // do, from testdata/github (npm's real answers for the packages it has): search.json is a real answer for a topic (five // repositories), with three more items made from its first — a fork, an // archived one and magpie-community's — and someone/word-guard, whose -// package.json is word-guard's own, a middleware alone; alfaoz's +// package.json is word-guard's own, a middleware alone; someone/agent-aider, +// magpie-community's agent-aider package.json and module, an agent alone; alfaoz's // opencode-see-image (real), whose main is a dist the repository hasn't; // and someone/flaky-entry, whose main GitHub doesn't answer for. // reads counts what fakeGitHub answered other than searches: package.json, @@ -50,6 +51,7 @@ func fakeGitHub(t *testing.T, up *atomic.Bool, asked *atomic.Int32) { } items = append(items, like("someone/word-guard", map[string]any{"description": "word guard", "stargazers_count": 3, "license": nil}), + like("someone/agent-aider", nil), like("alfaoz/opencode-see-image", nil), like("someone/flaky-entry", map[string]any{"license": map[string]any{"key": "other", "spdx_id": "NOASSERTION"}}), like("cyberElar/magpie-x-search", nil), @@ -126,7 +128,7 @@ func TestTaggedRepos(t *testing.T) { // none would load. magpie-x-search (real, #1327) is on npm from its // repository, but a command alone, an MCP server: npm's copy names no // file to load, and it has no index.js - want := "rynfar/meridian griffinmartin/opencode-claude-auth slkiser/opencode-quota someone/word-guard someone/flaky-entry" + want := "rynfar/meridian griffinmartin/opencode-claude-auth slkiser/opencode-quota someone/word-guard someone/agent-aider someone/flaky-entry" if strings.Join(repos, " ") != want { t.Fatalf("repos %v\nwant %s", repos, want) } @@ -150,6 +152,9 @@ func TestTaggedRepos(t *testing.T) { if c.Icon != "" { t.Fatalf("no magpie.icon, yet %q", c.Icon) } + if x := byRepo["someone/agent-aider"]; x.Kind != "agent" || x.Spec != "github:someone/agent-aider" { + t.Fatalf("an agent alone: %+v", x) + } if x := byRepo["someone/word-guard"]; x.Kind != "middleware" || x.Package != "@magpie-community/middleware-word-guard" || x.License != "" { t.Fatalf("a middleware alone: %+v", x) } @@ -179,7 +184,7 @@ func TestTaggedRepos(t *testing.T) { taggedAt = time.Now().Add(-taggedTTL - time.Second) taggedMu.Unlock() before := reads.Load() - if l := TaggedRepos(ctx); len(l) != 5 || asked.Load() != 2 { + if l := TaggedRepos(ctx); len(l) != 6 || asked.Load() != 2 { t.Fatalf("ten minutes on: asked %d times, %d repos", asked.Load(), len(l)) } if n := reads.Load() - before; n != 3 { @@ -194,7 +199,7 @@ func TestTaggedRepos(t *testing.T) { if err := os.Chtimes(taggedCache(), time.Now(), time.Now()); err != nil { t.Fatal(err) } - if l := TaggedRepos(ctx); len(l) != 5 || asked.Load() != 1 { + if l := TaggedRepos(ctx); len(l) != 6 || asked.Load() != 1 { t.Fatalf("restarted: asked %d times, %d repos", asked.Load(), len(l)) } @@ -205,7 +210,7 @@ func TestTaggedRepos(t *testing.T) { } up.Store(false) forget() - if l := TaggedRepos(ctx); len(l) != 5 || l[1].Package != "opencode-claude-auth" || asked.Load() != 1 { + if l := TaggedRepos(ctx); len(l) != 6 || l[1].Package != "opencode-claude-auth" || asked.Load() != 1 { t.Fatalf("down: %+v", l) } diff --git a/internal/plugin/market.go b/internal/plugin/market.go index f9c9b954c..df34cb24e 100644 --- a/internal/plugin/market.go +++ b/internal/plugin/market.go @@ -40,7 +40,7 @@ type Listing struct { Providers []string `json:"providers,omitempty"` // OpenCode's ids of those it signs in to Community bool `json:"community,omitempty"` // written by magpie's community Replaces string `json:"replaces,omitempty"` // the built-in subscription it does the work of - Kind string `json:"kind,omitempty"` // "middleware" for gateway middleware; none for a provider + Kind string `json:"kind,omitempty"` // "middleware" for gateway middleware, "agent" for an agent; none for a provider Summary map[string]string `json:"summary,omitempty"` // by language: en, zh } diff --git a/internal/plugin/testdata/github/raw/someone/agent-aider/main/aider.agent.js b/internal/plugin/testdata/github/raw/someone/agent-aider/main/aider.agent.js new file mode 100644 index 000000000..29113e2b5 --- /dev/null +++ b/internal/plugin/testdata/github/raw/someone/agent-aider/main/aider.agent.js @@ -0,0 +1,23 @@ +// Aider (aider.chat) keeps its settings in ~/.aider.conf.yml and reaches +// any OpenAI-compatible endpoint through litellm's openai/ prefix, so +// magpie's model goes in as openai//, beside magpie's +// address and the key magpie keeps for Aider. +export const agent = { + id: "aider", + name: "Aider", + bin: "aider", + config: "~/.aider.conf.yml", + model: "model", + prefix: "openai/", + ua: ["aider", "litellm"], + notice: "Aider reads its settings when it starts: restart it to use the new model.", +} + +export function connect({ gateway }) { + return { + "openai-api-base": gateway.v1, + "openai-api-key": gateway.key, + // magpie's model ids aren't in litellm's list, so aider would warn on each + "show-model-warnings": false, + } +} diff --git a/internal/plugin/testdata/github/raw/someone/agent-aider/main/package.json b/internal/plugin/testdata/github/raw/someone/agent-aider/main/package.json new file mode 100644 index 000000000..49c376544 --- /dev/null +++ b/internal/plugin/testdata/github/raw/someone/agent-aider/main/package.json @@ -0,0 +1,11 @@ +{ + "name": "@magpie-community/agent-aider", + "version": "0.1.0", + "description": "Aider (aider.chat) as a magpie agent: pick one of magpie's models for it on the Agents page", + "type": "module", + "files": ["aider.agent.js", "README.md"], + "keywords": ["magpie", "magpie-agent", "aider", "llm-gateway"], + "license": "MIT", + "repository": { "type": "git", "url": "git+https://github.com/magpie-community/plugins.git", "directory": "packages/aider" }, + "magpie": { "agent": "./aider.agent.js" } +}