sea-official-plugin-assets.mjs 14 KB

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