Files
magpie/internal/plugin/resolve.go
T
yetone e7fbbaab9f plugin: an author's own listing is kept, for a search to find (plugins#479)
On magpie-community/plugins#479 the owner ruled that registry.json
entries for packages published from their author's own repository
rightly leave out "community": true, and are shown under the
publisher's name, to a search alone. parseMarket dropped every entry
without the flag, so PinK (YuchenWell, plugins#617), and WispTerm,
CiteBox and Wisp Science (hoptoper) couldn't be found in magpie at
all.

What changed:
- parseMarket keeps a listing without the flag; Listing.Community
  says which it is. The new plugin.Community(l) picks the community's.
- Every reader of the market was re-derived:
  - plugins.js already browses community listings alone, matches a
    search over every listing (name, package, summary, providers, npm
    publisher), and shows a non-community card and page under its npm
    publisher, without the magpie community badge. Unchanged.
  - plugin.resolve (a short name like "river") reads the community's
    alone: an author's "pink" stays npm's own name "pink".
  - pluginAgents (/api/plugins/agents, the Agents page's suggestions)
    reads the community's alone, as Discover's Agents section does.
  - plugin.Icon's fallback by provider id reads the community's alone;
    its exact-package match reads all, so PinK's own icon shows.
  - pluginListings and pluginMarketState pass every listing on.
- Docs (en, zh, ja plugins.html) say such a package isn't browsed and
  is found by a search, under its publisher.

Tests:
- TestMarketKeepsAuthorsOwnListings parses registry.json's River and
  PinK entries as they are at plugins 486a68a5 (#617's base, lines
  126-130, PinK without the flag; testdata/registry-pink.json). PinK is
  kept, not community, with its kind, summary and icon; resolve("pink")
  is "pink" and resolve("river") the community's package.
- TestMarketSources now expects the non-community entry kept.
- plugin-authors-listing.test.cjs: PinK isn't browsed (Agents shows
  Kilo, no PinK anywhere), a search by name or publisher shows one PinK
  card under "zhouzhenzhou" with no badge, its page reads
  "zhouzhenzhou · magpie-plugin-pink" with no badge, River keeps its
  badge. en, zh, zh-TW, ja, de; 980 and 420 wide; chromium and webkit.

Broken by hand, each red, then put back:
- parseMarket's `&& l.Community` restored: TestMarketSources and
  TestMarketKeepsAuthorsOwnListings red.
- resolve without Community(): TestMarketKeepsAuthorsOwnListings red
  (pink resolved to magpie-plugin-pink).
- pluginAgents without Community(): TestPluginAgentsNotInstalled red.
- plugins.js Agents without the l.community filter, and the badge shown
  for every agent: plugin-authors-listing 0/22 each.

Run on mote at this head (rebased on a4916aec1; i18n-keys and plugin-authors-listing ran again there, both engines):
- gofmt -l: clean. GOOS=linux go vet -tags nogui and GOOS=windows go
  vet of internal/plugin and internal/gui: ok.
- go test -timeout 45m internal/plugin, internal/provider: ok.
  internal/gui: ok but TestMainWindowCorners/macOS_26_SDK (#3949,
  mote's macOS 27, nothing here touches it).
- At 3086c650b (rebased on 541102eba; the rebase since touched
  host.js, a gateway test and a provider test only): agent, agentplug,
  backup, davsync, library, middleware, usage and plugins/...: ok.
  cli: TestEveryRouteHasACommandOrIsNamed (#4826); gateway:
  TestCodexPoolSaysTheClientsVersion (#4807). Both filed, untouched
  here.
- node --test site/*.test.mjs: 80/80.
- GUI, every file that reaches the Plugins page (agent-plugin-agents,
  i18n-keys, market-*, plugin*; 1078 tests, both engines): 1074 pass,
  plugin-authors-listing 22/22. The 4 reds are plugin-move-unserved's
  "Try again takes the models unticked" (#4410, models folded behind
  "Show the other"), not reached here. The whole GUI suite wasn't run:
  mote held two other sessions' full suites for over an hour, and this
  changes no app.js, plugins.js, layout or API call. That run was at
  f5c956b84; after the rebase on a1f56720c (i18n.js and caller keys
  moved), plugin-authors-listing, i18n-keys, plugin-market and
  plugin-registry ran again: webkit's plugin-authors-listing and
  plugin-market timed out at mote load ~250 from other sessions' suites
  (#4125), and both passed 14/14 alone right after.

Not tried: a real magpie window against the live registry.json (the
listings come from a fixture and a faked API); the Icon fallback's
community-only rule has no test that goes red without it.
2026-11-02 14:12:20 +08:00

123 lines
4.2 KiB
Go

package plugin
import (
"context"
"fmt"
"slices"
"sort"
"strings"
)
// SubscriptionPlugin is the community package that serves a retired
// built-in subscription, by its id (commandcode-plan, qoder-cn), "" for
// none: provider's movers, which plugin can't import, set it.
var SubscriptionPlugin = func(id string) string { return "" }
// npmPrefix asks for npm's own package of a name, as written, where the
// name is also one a community plugin is called by: npm:think-tags is
// npm's think-tags, not @magpie-community/middleware-think-tags.
const npmPrefix = "npm:"
// callNames are the names a listing goes by, lower-cased: its package,
// that without the scope and its short name (think-tags, workbuddy), its
// name as shown (WorkBuddy, Command Code, with and without the space),
// and the provider ids it serves or the subscription it replaces.
func callNames(l Listing) []string {
n := strings.ToLower(strings.TrimSpace(l.Name))
out := []string{l.Package, bareName(l.Package), ShortName(l.Package), n, strings.ReplaceAll(n, " ", ""), strings.ReplaceAll(n, " ", "-"), l.Replaces}
out = append(out, l.Providers...)
for i := range out {
out[i] = strings.ToLower(out[i])
}
return out
}
// resolve is the npm spec a user's name for a plugin stands for: a short
// or shown name (workbuddy, WorkBuddy, think-tags, "Qoder", qoder-cn)
// that names one of magpie's community plugins, case aside, is that
// package, at the version given. A scoped package, a path or a git spec is
// kept as it is, and so is a name no community plugin goes by, which is
// npm's own (opencode-gemini-auth) once the list was read just now: a
// short name never reaches npm's unscoped package of that name while a
// community plugin is called by it, or may be, as far as magpie knows.
// npm:name is npm's name, as written.
func resolve(ctx context.Context, spec string) (string, error) {
if after, ok := strings.CutPrefix(spec, npmPrefix); ok {
after = strings.TrimSpace(after)
if !pkgName.MatchString(Name(after)) {
return "", fmt.Errorf("%q isn't an npm package name", after)
}
return after, nil
}
if IsPath(spec) || IsGit(spec) || strings.HasPrefix(spec, "@") {
return spec, nil
}
name, version := spec, ""
if at := strings.LastIndex(spec, "@"); at > 0 {
name, version = spec[:at], spec[at:]
}
key := strings.ToLower(strings.TrimSpace(name))
if key == "" {
return spec, nil
}
// the community's alone: an author's own listing is npm's name, as
// written
list, lerr := market(ctx)
list = Community(list)
var found []string
for _, l := range list {
if slices.Contains(callNames(l), key) && !slices.Contains(found, l.Package) {
found = append(found, l.Package)
}
}
if p := SubscriptionPlugin(key); p != "" && !slices.Contains(found, p) {
found = append(found, p)
}
switch {
case len(found) == 1:
return found[0] + version, nil
case len(found) > 1:
sort.Strings(found)
return "", fmt.Errorf("%q names more than one of magpie's community plugins: %s; give the one you mean", name, strings.Join(found, ", "))
}
valid := pkgName.MatchString(name)
if valid && lerr == nil {
// npm's own package: no community plugin goes by the name
return spec, nil
}
var b strings.Builder
if lerr != nil {
fmt.Fprintf(&b, "couldn't read the community plugin list (%v), so there is no telling whether %q is one of them", lerr, name)
} else {
fmt.Fprintf(&b, "no plugin is called %q", name)
}
if names := listNames(list); names != "" {
b.WriteString(". The community plugins")
if lerr != nil {
b.WriteString(" magpie knows of")
}
b.WriteString(": " + names)
}
if valid {
fmt.Fprintf(&b, ". Try again once the list can be read, give the plugin's full package name (@magpie-community/…), or npm:%s for npm's package %s", name, name)
} else {
b.WriteString(". Or give an npm package, a GitHub repo (github:owner/repo) or a folder")
}
return "", fmt.Errorf("%s", b.String())
}
// listNames are the listings as a user writes them: the short name, and
// the name shown where it differs.
func listNames(list []Listing) string {
var out []string
for _, l := range list {
s := ShortName(l.Package)
if l.Name != "" && !strings.EqualFold(l.Name, s) {
s += " (" + l.Name + ")"
}
out = append(out, s)
}
sort.Strings(out)
return strings.Join(out, ", ")
}