Atlas on Discord (#help, 10-25, Linux): `magpie plugin add WorkBuddy`
said "isn't an npm package name". Worse, a bare `workbuddy` passed the
npm-name check and installed npm's unscoped workbuddy@latest, not
@magpie-community/opencode-workbuddy-auth.
plugin.Add, which the CLI, the Plugins page's field (/api/plugins/add)
and the movers all go through, now resolves a bare name first
(internal/plugin/resolve.go). Case aside, a name one community listing
goes by is its package, at the version given: the package or its bare
name, the short name (workbuddy, think-tags), the shown name (WorkBuddy,
Command Code), a provider id it serves or the subscription it replaces,
and a retired subscription's id through its mover (qoder-cn; provider
sets plugin.SubscriptionPlugin = MovePackage). The CLI says which
package it picked. A name two listings go by, or none that isn't an npm
name, is refused with the community plugins listed. A name no listing
goes by is npm's own package (opencode-gemini-auth keeps working), but
only when the list was fetched within the six hours: when the fetch
fails, the copy kept or built in may not name a newer plugin, so the
name is refused with why, and npm:<name> installs npm's package as
written. market() now says whether its list is a fresh fetch (marketErr).
Scoped names, paths and git specs are unchanged. A backup's or a sync's
plugins go in by their own spec (plugin.AddExact via addPluginExact),
never resolved, so a restore isn't held by GitHub being unreachable.
Update, Upgrade and InstallMissing use add with stored specs, unchanged.
Tests (fake registry over httptest, fake bun; no network):
TestResolveCommunityNames, TestResolveRefusesWhatItCantName,
TestResolveListUnread, TestAddByCommunityName. TestUpdateAllUpdatesAPinnedPlugin
adds an unscoped third-party name, so it now serves a registry.
Broken by hand: Add without resolve -> TestAddByCommunityName red with
the reporter's own error ("WorkBuddy" isn't an npm package name) and
workbuddy -> workbuddy@latest. The unread-list rule turned off (valid
name goes to npm regardless) -> TestResolveListUnread red.
Real use, sandbox HOME, built binary, real npm and GitHub:
`plugin add WorkBuddy` -> installed @magpie-community/opencode-workbuddy-auth@latest,
listed workbuddy and workbuddy-ai; `think-tags` -> middleware-think-tags;
`opencode-gemini-auth` -> npm's, google-plugin listed; `qoder-cn` with
MAGPIE_PLUGIN_MARKET=off -> opencode-qoder-auth; "Work Buddy Pro" ->
refused with the 38 community plugins listed; `twinz` with the list off
or at a dead URL -> refused, saying why and npm:twinz.
Run at this head (rebased on origin/main 42976f9a0): gofmt -l clean;
go vet (darwin) and GOOS=linux / GOOS=windows -tags nogui vet of
plugin, provider, cli, backup, gui clean; go test -timeout 45m
./internal/plugin ok (120s, after the pinned-update fix),
./internal/provider ./internal/cli ./internal/backup ./internal/gui ok.
Not tried: the full ./... suite; Windows and Linux runs of the tests.
No GUI JS changed: the Plugins page's field goes through the same Add,
and its status still says "<typed name> is installed" rather than the
package picked; the installed list shows the package.
120 lines
4.1 KiB
Go
120 lines
4.1 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
|
|
}
|
|
list, lerr := market(ctx)
|
|
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, ", ")
|
|
}
|