diff --git a/scripts/tagged-kind.mjs b/scripts/tagged-kind.mjs new file mode 100644 index 0000000..dcc4046 --- /dev/null +++ b/scripts/tagged-kind.mjs @@ -0,0 +1,58 @@ +// 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, "") +} diff --git a/scripts/tagged-kind.test.mjs b/scripts/tagged-kind.test.mjs new file mode 100644 index 0000000..df3e3a5 --- /dev/null +++ b/scripts/tagged-kind.test.mjs @@ -0,0 +1,33 @@ +import { expect, test } from "bun:test" +import { entryOf, kindOf } from "./tagged-kind.mjs" + +// The manifests #1200 lists from dist/tagged.json, cut to what decides. +test("an agent plugin that keeps a module of its own is an agent (#1200)", () => { + const enhanced = { main: "./control/host.js", magpie: { agent: "./agent.js" } } // cursor-bridge-enhanced 0.2.17 + const deva = { main: "./index.mjs", magpie: { agent: "deva.agent.js" } } + const kiki = { main: "./kiki.host.js", magpie: { agent: "./kiki.agent.js" } } + const plain = { magpie: { agent: "./agent.js" } } // cursor-bridge 0.2.7 + for (const pj of [enhanced, deva, kiki, plain]) expect(kindOf(pj)).toBe("agent") + // the file it loads is its module, as magpie's readTagged checks it + expect(entryOf(kiki, "agent", true)).toBe("./kiki.host.js") + expect(entryOf(plain, "agent", true)).toBe("./agent.js") +}) + +test("hooks alone are a hooks plugin, and their file is the one to find", () => { + const pj = { magpie: { hooks: "./model-watch.hooks.js" } } + expect(kindOf(pj)).toBe("hooks") + expect(entryOf(pj, "hooks", true)).toBe("./model-watch.hooks.js") +}) + +test("a module of its own keeps middleware, a page and hooks a provider, as before", () => { + for (const mg of [{ middleware: "m.js" }, { page: { file: "p.html" } }, { hooks: "h.js" }]) { + expect(kindOf({ main: "index.mjs", magpie: mg })).toBe("") + expect(kindOf({ exports: { ".": "./i.mjs" }, magpie: mg })).toBe("") + } + expect(kindOf({ main: "index.mjs" })).toBe("") + expect(kindOf({ magpie: { middleware: "m.js" } })).toBe("middleware") + expect(kindOf({ magpie: { page: { file: "p.html" } } })).toBe("page") + // installed from npm, a provider loads npm's index file + expect(entryOf({ main: "index.mjs" }, "", false)).toBe("index.js") + expect(entryOf({ exports: { ".": { bun: "./b.mjs" } } }, "", true)).toBe("./b.mjs") +}) diff --git a/scripts/tagged.mjs b/scripts/tagged.mjs index 2be351b..41062be 100644 --- a/scripts/tagged.mjs +++ b/scripts/tagged.mjs @@ -8,6 +8,7 @@ // stays as the last file had it; a search that fails writes nothing. import { existsSync, mkdirSync, readFileSync, readdirSync, rmSync, writeFileSync } from "node:fs" import { dirname, join, resolve, posix } from "node:path" +import { READ, entryOf, kindOf, named } from "./tagged-kind.mjs" const TOPIC = "magpie-plugin" const pkgName = /^(@[a-z0-9][a-z0-9._~-]*\/)?[a-z0-9][a-z0-9._~-]*$/ @@ -18,8 +19,12 @@ if (!process.argv[2] || !existsSync(out)) { } const file = join(out, "tagged.json") let last = [] +// a file read another way (READ) is read again whole +let lastRead = 0 try { - last = JSON.parse(readFileSync(file, "utf8")).plugins ?? [] + const was = JSON.parse(readFileSync(file, "utf8")) + last = was.plugins ?? [] + lastRead = was.read ?? 1 } catch {} const before = new Map(last.map((t) => [t.repo.toLowerCase(), t])) @@ -75,22 +80,6 @@ const repoOf = (v) => { const m = /^https:\/\/(?:www\.)?github\.com\/([^/]+)\/([^/#]+)/.exec(s) return m ? `${m[1]}/${m[2]}` : "" } -const named = (e) => !!e && (typeof e !== "object" || Object.keys(e).length > 0) -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" -} - // the repository as magpie lists it, or null when it isn't a plugin magpie // could install; throws when GitHub or npm didn't answer async function read(it) { @@ -113,12 +102,8 @@ async function read(it) { t.version = pj.version ?? "" const icon = ownIcon(pj.magpie?.icon) if (icon) t.icon = icon - const mg = pj.magpie ?? {} - if (!pj.main && !named(pj.exports)) { - if (String(mg.middleware ?? "").trim()) t.kind = "middleware" - else if (String(mg.agent ?? "").trim()) t.kind = "agent" - else if (String(mg.page?.file ?? "").trim()) t.kind = "page" - } + const kind = kindOf(pj) + if (kind) t.kind = kind // published to npm from this repository: npm's copy is installed let npmNames = false try { @@ -134,10 +119,7 @@ async function read(it) { // installed from the repository, or from an npm copy that names no file, // the file it loads must be in the repository if (t.spec.startsWith("github:") || !npmNames) { - let entry = mg.middleware - if (t.kind === "agent") entry = mg.agent - else if (t.kind === "page") entry = mg.page?.file - else if (t.kind !== "middleware") entry = t.spec.startsWith("github:") ? pkgEntry(pj.exports, pj.main) : pkgEntry(null, "") + const entry = entryOf(pj, kind, t.spec.startsWith("github:")) if (posix.extname(entry ?? "") !== "") { try { await get(raw(posix.normalize("/" + entry).replace(/^\//, ""))) @@ -157,7 +139,7 @@ async function worker() { for (let it; (it = queue.shift()); ) { const was = before.get(it.full_name.toLowerCase()) // read again only when pushed to; stars and the rest are the search's - if (was && was.pushed === it.pushed_at) { + if (was && was.pushed === it.pushed_at && lastRead === READ) { plugins.push({ ...was, stars: it.stargazers_count, description: it.description ?? "", ownerAvatar: it.owner.avatar_url }) continue } @@ -227,5 +209,5 @@ const sweep = (dir, rel) => { sweep(readmes, "") const same = JSON.stringify(last.map((t) => t.repo).sort()) === JSON.stringify(plugins.map((t) => t.repo).sort()) -writeFileSync(file, JSON.stringify({ version: 1, generated: new Date().toISOString(), plugins })) +writeFileSync(file, JSON.stringify({ version: 1, read: READ, generated: new Date().toISOString(), plugins })) console.log(`${plugins.length} plugins, ${failed} unread${same ? "" : ", list changed"}`)