sea-official-plugin-assets.mjs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. import { createHash } from "node:crypto";
  2. import { mkdir, readdir, readFile, rm, stat, writeFile } from "node:fs/promises";
  3. import { existsSync } from "node:fs";
  4. import { join, relative, resolve, sep } from "node:path";
  5. export const seaOfficialPluginAssetPrefix = "zcode-official-plugins/";
  6. export const seaOfficialPluginManifestAssetKey = `${seaOfficialPluginAssetPrefix}manifest.json`;
  7. const browserUseRequiredRuntimePaths = [
  8. "scripts/browser-client.mjs",
  9. "docs/api.json",
  10. "docs/documents.json",
  11. "docs/overview.md",
  12. // recording lookup 是录屏 API 的模型入口,SEA 不得接受缺失正文的插件资产。
  13. "docs/recording.md",
  14. "docs/workflow.md",
  15. "skills/control-browser/SKILL.md",
  16. "skills/web-gui-tester/SKILL.md",
  17. ];
  18. // 文档插件查看面板的构建产物(office-runtime 输出到各插件 dist/)。
  19. const officeViewerRuntimePaths = ["dist/server.mjs", "dist/ui/assets.json", "dist/ui/panel.html"];
  20. export const officialSeaPlugins = [
  21. {
  22. marketplace: "zcode-plugins-official",
  23. name: "visualize",
  24. packageName: "@zcode/visualize-plugin",
  25. requiresRuntime: false,
  26. requiredSeedPaths: [
  27. "skills/visualize/SKILL.md",
  28. "skills/visualize/references/api.md",
  29. "skills/visualize/references/styles.md",
  30. "skills/visualize/tweak.md",
  31. "skills/visualize/LICENSE.md",
  32. "skills/visualize/scripts/render.py",
  33. "skills/visualize/assets/visualize.css",
  34. "skills/visualize/assets/visualize.html",
  35. "skills/visualize/assets/calendar.js",
  36. "skills/visualize/assets/runtime-manifest.json",
  37. "skills/visualize/scripts/vendor.py",
  38. "skills/visualize/assets/vendor/manifest.json",
  39. "skills/visualize/assets/vendor/floating-ui-core-1.7.3.min.js",
  40. "skills/visualize/assets/vendor/floating-ui-core-1.7.3.min.js.LICENSE",
  41. "skills/visualize/assets/vendor/floating-ui-dom-1.7.4.min.js",
  42. "skills/visualize/assets/vendor/floating-ui-dom-1.7.4.min.js.LICENSE",
  43. "skills/visualize/assets/vendor/lucide-1.17.0.js",
  44. "skills/visualize/assets/vendor/lucide-1.17.0.js.LICENSE",
  45. "skills/visualize/assets/vendor/d3-7.9.0.min.js",
  46. "skills/visualize/assets/vendor/d3-7.9.0.min.js.LICENSE",
  47. "skills/visualize/widgets/calendar.md",
  48. "skills/visualize/examples/calendar.html",
  49. "skills/visualize/assets/standalone-host-bridge.js",
  50. "skills/visualize/assets/standalone-shell.js",
  51. ],
  52. rootPath: join("packages", "visualize-plugin"),
  53. version: "0.1.2",
  54. },
  55. {
  56. // node_repl 宿主:Browser Use 与 Computer Use 共用的运行时产物,自己不是面向用户的插件
  57. // (无 skill、无市场 listing)。它必须始终随发布物嵌入,否则任一能力启用时都没有宿主可跑。
  58. marketplace: "zcode-plugins-official",
  59. name: "node-repl-host",
  60. packageName: "@zcode/node-repl-host",
  61. requiresRuntime: true,
  62. requiredRuntimePaths: ["dist/mcp/server.js"],
  63. rootPath: join("packages", "node-repl-host"),
  64. version: "0.7.0",
  65. },
  66. {
  67. marketplace: "zcode-plugins-official",
  68. name: "android-emulator",
  69. packageName: "@zcode/android-emulator-plugin",
  70. requiresRuntime: true,
  71. requiredRuntimePaths: ["dist/mcp/server.js"],
  72. rootPath: join("packages", "android-emulator-plugin"),
  73. version: "0.1.1",
  74. },
  75. {
  76. marketplace: "zcode-plugins-official",
  77. name: "documents",
  78. packageName: "@zcode/documents-plugin",
  79. // 0.2.0 起带查看面板:SEA 必须在嵌入前校验 office-runtime 构建出的 server 与面板资源。
  80. requiresRuntime: true,
  81. requiredRuntimePaths: officeViewerRuntimePaths,
  82. requiredSeedPaths: [
  83. "agents/visual-judge.md",
  84. "skills/docx/SKILL.md",
  85. "skills/docx-viewer/SKILL.md",
  86. ],
  87. rootPath: join("packages", "documents-plugin"),
  88. // 修复原因:SEA manifest 必须与 package、plugin manifest 和官方 definition 同步,避免发布产物嵌入旧版本。
  89. version: "0.2.2",
  90. },
  91. {
  92. marketplace: "zcode-plugins-official",
  93. name: "image-search",
  94. packageName: "@zcode/image-search-plugin",
  95. requiresRuntime: false,
  96. requiredSeedPaths: [".mcp.json"],
  97. rootPath: join("packages", "image-search-plugin"),
  98. // 修复原因:SEA manifest 必须与 package、plugin manifest 和官方 definition 同步,避免发布产物嵌入旧版本。
  99. version: "0.1.1",
  100. },
  101. {
  102. marketplace: "zcode-plugins-official",
  103. name: "pdf",
  104. packageName: "@zcode/pdf-plugin",
  105. // 0.2.0 起带查看面板:SEA 必须在嵌入前校验 office-runtime 构建出的 server 与面板资源。
  106. requiresRuntime: true,
  107. requiredRuntimePaths: officeViewerRuntimePaths,
  108. requiredSeedPaths: [
  109. "agents/visual-judge.md",
  110. "skills/pdf/SKILL.md",
  111. "skills/pdf-viewer/SKILL.md",
  112. ],
  113. rootPath: join("packages", "pdf-plugin"),
  114. // 修复原因:SEA manifest 必须与 package、plugin manifest 和官方 definition 同步,避免发布产物嵌入旧版本。
  115. version: "0.2.3",
  116. },
  117. {
  118. marketplace: "zcode-plugins-official",
  119. name: "presentations",
  120. packageName: "@zcode/presentations-plugin",
  121. // 0.2.0 起带查看面板:SEA 必须在嵌入前校验 office-runtime 构建出的 server 与面板资源。
  122. requiresRuntime: true,
  123. requiredRuntimePaths: officeViewerRuntimePaths,
  124. requiredSeedPaths: [
  125. "agents/visual-judge.md",
  126. "skills/pptx/SKILL.md",
  127. "skills/pptx-viewer/SKILL.md",
  128. ],
  129. rootPath: join("packages", "presentations-plugin"),
  130. // 修复原因:SEA manifest 必须与 package、plugin manifest 和官方 definition 同步,避免发布产物嵌入旧版本。
  131. version: "0.2.2",
  132. },
  133. {
  134. marketplace: "zcode-plugins-official",
  135. name: "spreadsheets",
  136. packageName: "@zcode/spreadsheets-plugin",
  137. // 0.2.0 起带查看面板:SEA 必须在嵌入前校验 office-runtime 构建出的 server 与面板资源。
  138. requiresRuntime: true,
  139. requiredRuntimePaths: officeViewerRuntimePaths,
  140. requiredSeedPaths: [
  141. "agents/visual-judge.md",
  142. "skills/xlsx/SKILL.md",
  143. "skills/xlsx-viewer/SKILL.md",
  144. ],
  145. rootPath: join("packages", "spreadsheets-plugin"),
  146. // 修复原因:SEA manifest 必须与 package、plugin manifest 和官方 definition 同步,避免发布产物嵌入旧版本。
  147. version: "0.2.2",
  148. },
  149. {
  150. // browser-use 使用真实 node_repl MCP runtime,SEA 必须校验并嵌入 server.js。
  151. marketplace: "zcode-plugins-official",
  152. name: "browser-use",
  153. packageName: "@zcode/browser-use-plugin",
  154. requiresRuntime: true,
  155. // Browser Use 的 runtime、client、API 文档和 skills 是同一发布单元;
  156. // SEA 构建必须在嵌入前拒绝任一缺失项,不能把损坏产物留到用户启动时才发现。
  157. requiredRuntimePaths: browserUseRequiredRuntimePaths,
  158. rootPath: join("packages", "browser-use-plugin"),
  159. // SEA 清单仍指向旧版时,runtime 会与官方 definition 精确匹配失败,
  160. // 导致发布产物不 seed browser-use,进而无法装配宿主 node_repl MCP。
  161. version: "0.5.1",
  162. },
  163. {
  164. marketplace: "zcode-plugins-official",
  165. name: "ios-simulator",
  166. packageName: "@zcode/ios-simulator-plugin",
  167. requiresRuntime: true,
  168. requiredRuntimePaths: ["dist/mcp/server.js"],
  169. rootPath: join("packages", "ios-simulator-plugin"),
  170. version: "0.1.0",
  171. },
  172. {
  173. marketplace: "zcode-plugins-official",
  174. name: "restore-legacy-sessions",
  175. packageName: "@zcode/restore-legacy-sessions-plugin",
  176. requiresRuntime: false,
  177. rootPath: join("packages", "restore-legacy-sessions-plugin"),
  178. version: "0.1.0",
  179. },
  180. {
  181. marketplace: "zcode-plugins-official",
  182. name: "skill-creator",
  183. packageName: "@zcode/skill-creator-plugin",
  184. requiresRuntime: false,
  185. rootPath: join("packages", "skill-creator-plugin"),
  186. version: "0.1.0",
  187. },
  188. {
  189. marketplace: "zcode-plugins-official",
  190. name: "plugin-creator",
  191. packageName: "@zcode/plugin-creator-plugin",
  192. requiresRuntime: false,
  193. rootPath: join("packages", "plugin-creator-plugin"),
  194. version: "0.1.1",
  195. },
  196. {
  197. // 纯内容型(只提供 skills):requiresRuntime:false,SEA 构建不校验 dist/mcp/server.js。
  198. // 版本必须与 package.json / plugin.json / official-plugin-definitions.ts 同步 bump——
  199. // runtime 按 (name, version) 精确匹配 SEA 资产,这里落后会让整个插件在 SEA 下不 seed
  200. // (browser-use 0.3.1 的既有教训,build-sea.test.mjs:564 钉住四处对齐)。
  201. // 0.3.0:`/workflow` 与 dynamic-workflows 技能移出本插件(见 sea-bundled-skill-assets.mjs)。
  202. marketplace: "zcode-plugins-official",
  203. name: "zcode-guide",
  204. packageName: "@zcode/zcode-guide-plugin",
  205. requiresRuntime: false,
  206. requiredSeedPaths: ["skills/zcode-configuration-guide/SKILL.md"],
  207. rootPath: join("packages", "zcode-guide-plugin"),
  208. version: "0.3.0",
  209. },
  210. {
  211. // skill + start.sh 模板 + 零依赖的 zcode-site CLI,加上 Sites 面板(dist/ 下的 MCP server 与页面,
  212. // 由插件自己的 build 脚本构建)。版本与 package.json / plugin.json / official-plugin-definitions.ts 同步 bump。
  213. marketplace: "zcode-plugins-official",
  214. name: "zcode-site",
  215. packageName: "@zcode/zcode-site-plugin",
  216. requiresRuntime: true,
  217. requiredRuntimePaths: ["dist/server.mjs", "dist/ui/panel.html"],
  218. requiredSeedPaths: [
  219. "scripts/zcode-site.mjs",
  220. "scripts/stage.mjs",
  221. "scripts/errors.mjs",
  222. "skills/site-deploy/SKILL.md",
  223. ],
  224. rootPath: join("packages", "zcode-site-plugin"),
  225. version: "0.5.0",
  226. },
  227. {
  228. // design:纯内容型(技能 + 设计资源 + SessionStart hook),requiresRuntime:false。
  229. // requiredSeedPaths 与 official-plugin-definitions.ts 的 OFFICIAL_DESIGN_REQUIRED_SEED_PATHS 一致;
  230. // 版本与 package.json / plugin.json / definitions 同步 bump。
  231. marketplace: "zcode-plugins-official",
  232. name: "design",
  233. packageName: "@zcode/design-plugin",
  234. requiresRuntime: false,
  235. requiredSeedPaths: [
  236. "skills/design/SKILL.md",
  237. "skills/design/references/catalog.md",
  238. "skills/design/references/check.md",
  239. "skills/design/references/create.md",
  240. "skills/design/references/design-system.md",
  241. "skills/design/references/interact.md",
  242. "skills/design/references/output.md",
  243. "skills/design/references/redesign.md",
  244. "skills/design/scripts/adapt-canvas.mjs",
  245. "skills/design/scripts/design-lint.mjs",
  246. "skills/design/scripts/design-tokens.mjs",
  247. "skills/design/scripts/import-design-system.mjs",
  248. "hooks/hooks.json",
  249. "hooks/run-hook.cmd",
  250. "hooks/design-rules",
  251. ],
  252. rootPath: join("packages", "design-plugin"),
  253. version: "0.7.2",
  254. },
  255. // superpowers 已从内置插件下线,改走 UI 推荐区按需安装;这里不再打入 SEA 资源清单。
  256. ];
  257. export const collectSeaOfficialPluginAssets = async ({
  258. requireRuntime = false,
  259. root,
  260. stagingDirectory,
  261. } = {}) => {
  262. const files = [];
  263. const assets = {};
  264. const plugins = [];
  265. await rm(stagingDirectory, {
  266. force: true,
  267. recursive: true,
  268. });
  269. for (const plugin of officialSeaPlugins) {
  270. const pluginRoot = resolve(root, plugin.rootPath);
  271. assertPluginRoot(pluginRoot, plugin);
  272. assertPluginRequiredSeedAssets(pluginRoot, plugin);
  273. // 只提供 skills 的内容型插件没有 MCP server,用 requiresRuntime:false 跳过校验;
  274. // 其余运行时插件仍要在此校验,避免发布缺失可执行入口的产物。
  275. if (requireRuntime && plugin.requiresRuntime !== false) assertPluginRuntime(pluginRoot, plugin);
  276. const pluginFiles = [];
  277. for await (const sourcePath of walkFiles(pluginRoot)) {
  278. const relativePath = relative(pluginRoot, sourcePath);
  279. if (!shouldIncludePluginFile(relativePath)) continue;
  280. const bytes = await readFile(sourcePath);
  281. const sourceStats = await stat(sourcePath);
  282. const assetPath = toPosixPath(
  283. join(plugin.marketplace, plugin.name, plugin.version, relativePath),
  284. );
  285. assets[`${seaOfficialPluginAssetPrefix}${assetPath}`] = sourcePath;
  286. const file = {
  287. mode: modeForSeedFile(relativePath, sourceStats.mode),
  288. path: toPosixPath(relativePath),
  289. sha256: createHash("sha256").update(bytes).digest("hex"),
  290. };
  291. pluginFiles.push(file);
  292. files.push({
  293. ...file,
  294. plugin: plugin.name,
  295. });
  296. }
  297. pluginFiles.sort((left, right) => left.path.localeCompare(right.path));
  298. plugins.push({
  299. files: pluginFiles,
  300. marketplace: plugin.marketplace,
  301. name: plugin.name,
  302. version: plugin.version,
  303. });
  304. }
  305. plugins.sort((left, right) => left.name.localeCompare(right.name));
  306. const manifestHash = createHash("sha256")
  307. .update(
  308. JSON.stringify(
  309. plugins.map((plugin) => [
  310. plugin.marketplace,
  311. plugin.name,
  312. plugin.version,
  313. plugin.files.map(({ path, sha256, mode }) => [path, sha256, modeForSeedFile(path, mode)]),
  314. ]),
  315. ),
  316. )
  317. .digest("hex");
  318. const manifest = {
  319. hash: manifestHash,
  320. plugins,
  321. version: 1,
  322. };
  323. const manifestPath = resolve(stagingDirectory, "official-plugins-manifest.json");
  324. await mkdir(stagingDirectory, {
  325. recursive: true,
  326. });
  327. await writeFile(manifestPath, JSON.stringify(manifest, null, 2));
  328. assets[seaOfficialPluginManifestAssetKey] = manifestPath;
  329. return {
  330. assets,
  331. manifest,
  332. };
  333. };
  334. function assertPluginRoot(pluginRoot, plugin) {
  335. if (!existsSync(join(pluginRoot, ".zcode-plugin", "plugin.json"))) {
  336. throw new Error(`Missing ${plugin.name} plugin manifest at ${pluginRoot}`);
  337. }
  338. }
  339. function assertPluginRequiredSeedAssets(pluginRoot, plugin) {
  340. for (const relativePath of plugin.requiredSeedPaths ?? []) {
  341. const assetPath = join(pluginRoot, ...relativePath.split("/"));
  342. if (!existsSync(assetPath)) {
  343. throw new Error(`Missing ${plugin.name} required seed asset at ${assetPath}`);
  344. }
  345. }
  346. }
  347. function assertPluginRuntime(pluginRoot, plugin) {
  348. for (const relativePath of plugin.requiredRuntimePaths ?? ["dist/mcp/server.js"]) {
  349. const runtimePath = join(pluginRoot, ...relativePath.split("/"));
  350. if (!existsSync(runtimePath)) {
  351. const assetKind = relativePath === "dist/mcp/server.js" ? "MCP runtime" : "runtime asset";
  352. throw new Error(
  353. `Missing ${plugin.name} ${assetKind} at ${runtimePath}. ` +
  354. `Run \`pnpm --filter ${plugin.packageName} build\` before \`pnpm sea\`.`,
  355. );
  356. }
  357. }
  358. }
  359. async function* walkFiles(directory) {
  360. const entries = await readdir(directory, {
  361. withFileTypes: true,
  362. });
  363. for (const entry of entries) {
  364. if (shouldSkipDirectory(entry.name)) continue;
  365. const fullPath = resolve(directory, entry.name);
  366. if (entry.isDirectory()) {
  367. yield* walkFiles(fullPath);
  368. continue;
  369. }
  370. if (entry.isFile()) {
  371. yield fullPath;
  372. }
  373. }
  374. }
  375. const shouldSkipDirectory = (name) =>
  376. name === "node_modules" ||
  377. name === ".turbo" ||
  378. name === "coverage" ||
  379. name === ".venv" ||
  380. name === "__pycache__";
  381. const includedTopLevelPaths = new Set([
  382. ".mcp.json",
  383. ".zcode-plugin",
  384. "README.md",
  385. // SEA 资源采集曾只允许 skills/commands,导致 document-skills 的 judge 子代理未进入可执行文件。
  386. "agents",
  387. "commands",
  388. "dist",
  389. "docs",
  390. "hooks",
  391. "output-styles",
  392. "package.json",
  393. "scripts",
  394. "skills",
  395. "templates",
  396. ]);
  397. const shouldIncludePluginFile = (relativePath) => {
  398. const segments = relativePath.split(sep);
  399. if (segments.includes(".DS_Store") || segments.some((segment) => segment.endsWith(".pyc"))) {
  400. return false;
  401. }
  402. const [topLevel] = relativePath.split(sep);
  403. return topLevel !== undefined && includedTopLevelPaths.has(topLevel);
  404. };
  405. const toPosixPath = (value) => value.split(sep).join("/");
  406. const modeForSeedFile = (filePath, sourceMode) => {
  407. if (sourceMode !== undefined && (sourceMode & 0o111) !== 0) return 0o755;
  408. const normalizedPath = toPosixPath(filePath);
  409. if (/(?:^|\/)dist\/mcp\/server\.js$/i.test(normalizedPath)) return 0o755;
  410. if (/^hooks\//u.test(normalizedPath) && !/\.(json|md|txt)$/iu.test(normalizedPath)) {
  411. return 0o755;
  412. }
  413. return 0o644;
  414. };