sea-official-plugin-assets.mjs 18 KB

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