sea-official-plugin-assets.mjs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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.1",
  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.0",
  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.1",
  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.2",
  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.1",
  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.1",
  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. "skills/site-deploy/SKILL.md",
  222. ],
  223. rootPath: join("packages", "zcode-site-plugin"),
  224. version: "0.4.2",
  225. },
  226. // superpowers 已从内置插件下线,改走 UI 推荐区按需安装;这里不再打入 SEA 资源清单。
  227. ];
  228. export const collectSeaOfficialPluginAssets = async ({
  229. requireRuntime = false,
  230. root,
  231. stagingDirectory,
  232. } = {}) => {
  233. const files = [];
  234. const assets = {};
  235. const plugins = [];
  236. await rm(stagingDirectory, {
  237. force: true,
  238. recursive: true,
  239. });
  240. for (const plugin of officialSeaPlugins) {
  241. const pluginRoot = resolve(root, plugin.rootPath);
  242. assertPluginRoot(pluginRoot, plugin);
  243. assertPluginRequiredSeedAssets(pluginRoot, plugin);
  244. // 只提供 skills 的内容型插件没有 MCP server,用 requiresRuntime:false 跳过校验;
  245. // 其余运行时插件仍要在此校验,避免发布缺失可执行入口的产物。
  246. if (requireRuntime && plugin.requiresRuntime !== false) assertPluginRuntime(pluginRoot, plugin);
  247. const pluginFiles = [];
  248. for await (const sourcePath of walkFiles(pluginRoot)) {
  249. const relativePath = relative(pluginRoot, sourcePath);
  250. if (!shouldIncludePluginFile(relativePath)) continue;
  251. const bytes = await readFile(sourcePath);
  252. const sourceStats = await stat(sourcePath);
  253. const assetPath = toPosixPath(
  254. join(plugin.marketplace, plugin.name, plugin.version, relativePath),
  255. );
  256. assets[`${seaOfficialPluginAssetPrefix}${assetPath}`] = sourcePath;
  257. const file = {
  258. mode: modeForSeedFile(relativePath, sourceStats.mode),
  259. path: toPosixPath(relativePath),
  260. sha256: createHash("sha256").update(bytes).digest("hex"),
  261. };
  262. pluginFiles.push(file);
  263. files.push({
  264. ...file,
  265. plugin: plugin.name,
  266. });
  267. }
  268. pluginFiles.sort((left, right) => left.path.localeCompare(right.path));
  269. plugins.push({
  270. files: pluginFiles,
  271. marketplace: plugin.marketplace,
  272. name: plugin.name,
  273. version: plugin.version,
  274. });
  275. }
  276. plugins.sort((left, right) => left.name.localeCompare(right.name));
  277. const manifestHash = createHash("sha256")
  278. .update(
  279. JSON.stringify(
  280. plugins.map((plugin) => [
  281. plugin.marketplace,
  282. plugin.name,
  283. plugin.version,
  284. plugin.files.map(({ path, sha256, mode }) => [path, sha256, modeForSeedFile(path, mode)]),
  285. ]),
  286. ),
  287. )
  288. .digest("hex");
  289. const manifest = {
  290. hash: manifestHash,
  291. plugins,
  292. version: 1,
  293. };
  294. const manifestPath = resolve(stagingDirectory, "official-plugins-manifest.json");
  295. await mkdir(stagingDirectory, {
  296. recursive: true,
  297. });
  298. await writeFile(manifestPath, JSON.stringify(manifest, null, 2));
  299. assets[seaOfficialPluginManifestAssetKey] = manifestPath;
  300. return {
  301. assets,
  302. manifest,
  303. };
  304. };
  305. function assertPluginRoot(pluginRoot, plugin) {
  306. if (!existsSync(join(pluginRoot, ".zcode-plugin", "plugin.json"))) {
  307. throw new Error(`Missing ${plugin.name} plugin manifest at ${pluginRoot}`);
  308. }
  309. }
  310. function assertPluginRequiredSeedAssets(pluginRoot, plugin) {
  311. for (const relativePath of plugin.requiredSeedPaths ?? []) {
  312. const assetPath = join(pluginRoot, ...relativePath.split("/"));
  313. if (!existsSync(assetPath)) {
  314. throw new Error(`Missing ${plugin.name} required seed asset at ${assetPath}`);
  315. }
  316. }
  317. }
  318. function assertPluginRuntime(pluginRoot, plugin) {
  319. for (const relativePath of plugin.requiredRuntimePaths ?? ["dist/mcp/server.js"]) {
  320. const runtimePath = join(pluginRoot, ...relativePath.split("/"));
  321. if (!existsSync(runtimePath)) {
  322. const assetKind = relativePath === "dist/mcp/server.js" ? "MCP runtime" : "runtime asset";
  323. throw new Error(
  324. `Missing ${plugin.name} ${assetKind} at ${runtimePath}. ` +
  325. `Run \`pnpm --filter ${plugin.packageName} build\` before \`pnpm sea\`.`,
  326. );
  327. }
  328. }
  329. }
  330. async function* walkFiles(directory) {
  331. const entries = await readdir(directory, {
  332. withFileTypes: true,
  333. });
  334. for (const entry of entries) {
  335. if (shouldSkipDirectory(entry.name)) continue;
  336. const fullPath = resolve(directory, entry.name);
  337. if (entry.isDirectory()) {
  338. yield* walkFiles(fullPath);
  339. continue;
  340. }
  341. if (entry.isFile()) {
  342. yield fullPath;
  343. }
  344. }
  345. }
  346. const shouldSkipDirectory = (name) =>
  347. name === "node_modules" ||
  348. name === ".turbo" ||
  349. name === "coverage" ||
  350. name === ".venv" ||
  351. name === "__pycache__";
  352. const includedTopLevelPaths = new Set([
  353. ".mcp.json",
  354. ".zcode-plugin",
  355. "README.md",
  356. // SEA 资源采集曾只允许 skills/commands,导致 document-skills 的 judge 子代理未进入可执行文件。
  357. "agents",
  358. "commands",
  359. "dist",
  360. "docs",
  361. "hooks",
  362. "output-styles",
  363. "package.json",
  364. "scripts",
  365. "skills",
  366. "templates",
  367. ]);
  368. const shouldIncludePluginFile = (relativePath) => {
  369. const segments = relativePath.split(sep);
  370. if (segments.includes(".DS_Store") || segments.some((segment) => segment.endsWith(".pyc"))) {
  371. return false;
  372. }
  373. const [topLevel] = relativePath.split(sep);
  374. return topLevel !== undefined && includedTopLevelPaths.has(topLevel);
  375. };
  376. const toPosixPath = (value) => value.split(sep).join("/");
  377. const modeForSeedFile = (filePath, sourceMode) => {
  378. if (sourceMode !== undefined && (sourceMode & 0o111) !== 0) return 0o755;
  379. const normalizedPath = toPosixPath(filePath);
  380. if (/(?:^|\/)dist\/mcp\/server\.js$/i.test(normalizedPath)) return 0o755;
  381. if (/^hooks\//u.test(normalizedPath) && !/\.(json|md|txt)$/iu.test(normalizedPath)) {
  382. return 0o755;
  383. }
  384. return 0o644;
  385. };