sea-official-plugin-assets.mjs 13 KB

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