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.
This commit is contained in:
@@ -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, "")
|
||||
}
|
||||
@@ -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")
|
||||
})
|
||||
+11
-29
@@ -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"}`)
|
||||
|
||||
Reference in New Issue
Block a user