Files
plugins/scripts/tagged-kind.mjs
T
yetone df52820f18 tagged: an agent plugin with a module of its own is an agent, and hooks are read (#1200)
kingchen95272025 (#1200): scripts/tagged.mjs only read magpie.agent
when a package had no main or exports, so an agent plugin that keeps a
host module (cursor-bridge-enhanced 0.2.17, magpie-agent-deva,
magpie-kiki) had no kind in dist/tagged.json and Discover showed it
under Providers, while magpie's own readTagged (internal/plugin/
github.go, since plugins#737) calls it an agent. tagged.mjs also had no
hooks branch.

- scripts/tagged-kind.mjs holds kindOf and entryOf, mirroring
  readTagged: middleware, page and hooks only without a module of their
  own; magpie.agent always. The file checked is the package's module
  when it has one, else the file its kind declares, as readTagged does.
- tagged.json carries read: 2. A file read the old way is read again
  whole on the next run, so entries not pushed to since are corrected
  too, not only new pushes. version stays 1, which the site's worker
  checks.

Run (mote):
- bun test scripts/: 30 pass, 0 fail.
- node scripts/tagged.mjs on a copy of dist's tagged.json, against
  GitHub (no token): 442 plugins, 0 unread, read 2. The three repos
  #1200 names, and 30 more, went from no kind to agent; 8 hooks
  repositories got hooks. No entry changed kind any other way. Which
  repositories the topic search returns differed from dist's file
  (16 gone, 10 new); that comes from the search, not this change.

Broken by hand: kindOf with the old nesting: 2 of 3 tests fail.

Not tried: the tagged workflow itself; it runs on its half-hourly
schedule after this lands.
2026-11-16 12:08:25 +08:00

59 lines
2.1 KiB
JavaScript

// What a tagged repository is, and the file it loads, as magpie reads them
// (internal/plugin/github.go readTagged). Kept apart from tagged.mjs, which
// reaches GitHub when loaded, so they can be tested.
// READ names this way of reading: tagged.json carries it, and a file read
// another way is read again whole, not only the repositories pushed to.
// 2: an agent plugin with a module of its own is an agent, and hooks are
// read (#1200).
export const READ = 2
export const named = (e) => !!e && (typeof e !== "object" || Object.keys(e).length > 0)
export const pkgEntry = (exports, main) => {
const pick = (v) => {
if (typeof v === "string") return v
if (v && typeof v === "object") {
if ("." in v) return pick(v["."])
for (const k of ["bun", "import", "default", "node", "require"]) {
const s = pick(v[k])
if (s) return s
}
}
return ""
}
return pick(exports) || main || "index.js"
}
// runs is whether Bun loads a module of the package's own (main, exports)
export const runs = (pj) => !!pj.main || named(pj.exports)
const said = (v) => String(v ?? "").trim() !== ""
// kindOf is the kind magpie shows, or "" for a provider
export function kindOf(pj) {
const mg = pj.magpie ?? {}
const r = runs(pj)
if (!r && said(mg.middleware)) return "middleware"
// a package that adds an agent is an agent plugin, its module (main)
// only what keeps the agent working, not a provider (#737, #1200)
if (said(mg.agent)) return "agent"
if (!r && said(mg.page?.file)) return "page"
if (!r && said(mg.hooks)) return "hooks"
return ""
}
// entryOf is the file the installed plugin loads, which the repository must
// have: its own module when it has one, else the file its kind declares.
// git is whether it is installed from the repository rather than from npm.
export function entryOf(pj, kind, git) {
const mg = pj.magpie ?? {}
if (!runs(pj)) {
if (kind === "middleware") return mg.middleware
if (kind === "agent") return mg.agent
if (kind === "page") return mg.page?.file
if (kind === "hooks") return mg.hooks
}
return git ? pkgEntry(pj.exports, pj.main) : pkgEntry(null, "")
}