sea-official-plugin-assets.mjs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. // node_repl 宿主:Browser Use 与 Computer Use 共用的运行时产物,自己不是面向用户的插件
  21. // (无 skill、无市场 listing)。它必须始终随发布物嵌入,否则任一能力启用时都没有宿主可跑。
  22. marketplace: "zcode-plugins-official",
  23. name: "node-repl-host",
  24. packageName: "@zcode/node-repl-host",
  25. requiresRuntime: true,
  26. requiredRuntimePaths: ["dist/mcp/server.js"],
  27. rootPath: join("packages", "node-repl-host"),
  28. version: "0.6.0",
  29. },
  30. {
  31. marketplace: "zcode-plugins-official",
  32. name: "browser-use",
  33. packageName: "@zcode/browser-use-plugin",
  34. requiresRuntime: true,
  35. // Browser Use 的 runtime、client、API 文档和 skills 是同一发布单元;
  36. // SEA 构建必须在嵌入前拒绝任一缺失项,不能把损坏产物留到用户启动时才发现。
  37. requiredRuntimePaths: browserUseRequiredRuntimePaths,
  38. rootPath: join("packages", "browser-use-plugin"),
  39. // SEA 清单仍指向旧版时,runtime 会与官方 definition 精确匹配失败,
  40. // 导致发布产物不 seed browser-use,进而无法装配宿主 node_repl MCP。
  41. version: "0.5.1",
  42. },
  43. ];
  44. export const collectSeaOfficialPluginAssets = async ({
  45. requireRuntime = false,
  46. root,
  47. stagingDirectory,
  48. } = {}) => {
  49. const files = [];
  50. const assets = {};
  51. const plugins = [];
  52. await rm(stagingDirectory, {
  53. force: true,
  54. recursive: true,
  55. });
  56. for (const plugin of officialSeaPlugins) {
  57. const pluginRoot = resolve(root, plugin.rootPath);
  58. assertPluginRoot(pluginRoot, plugin);
  59. assertPluginRequiredSeedAssets(pluginRoot, plugin);
  60. // 只提供 skills 的内容型插件没有 MCP server,用 requiresRuntime:false 跳过校验;
  61. // 其余运行时插件仍要在此校验,避免发布缺失可执行入口的产物。
  62. if (requireRuntime && plugin.requiresRuntime !== false) assertPluginRuntime(pluginRoot, plugin);
  63. const pluginFiles = [];
  64. for await (const sourcePath of walkFiles(pluginRoot)) {
  65. const relativePath = relative(pluginRoot, sourcePath);
  66. if (!shouldIncludePluginFile(relativePath)) continue;
  67. const bytes = await readFile(sourcePath);
  68. const sourceStats = await stat(sourcePath);
  69. const assetPath = toPosixPath(
  70. join(plugin.marketplace, plugin.name, plugin.version, relativePath),
  71. );
  72. assets[`${seaOfficialPluginAssetPrefix}${assetPath}`] = sourcePath;
  73. const file = {
  74. mode: modeForSeedFile(relativePath, sourceStats.mode),
  75. path: toPosixPath(relativePath),
  76. sha256: createHash("sha256").update(bytes).digest("hex"),
  77. };
  78. pluginFiles.push(file);
  79. files.push({
  80. ...file,
  81. plugin: plugin.name,
  82. });
  83. }
  84. pluginFiles.sort((left, right) => left.path.localeCompare(right.path));
  85. plugins.push({
  86. files: pluginFiles,
  87. marketplace: plugin.marketplace,
  88. name: plugin.name,
  89. version: plugin.version,
  90. });
  91. }
  92. plugins.sort((left, right) => left.name.localeCompare(right.name));
  93. const manifestHash = createHash("sha256")
  94. .update(
  95. JSON.stringify(
  96. plugins.map((plugin) => [
  97. plugin.marketplace,
  98. plugin.name,
  99. plugin.version,
  100. plugin.files.map(({ path, sha256, mode }) => [path, sha256, modeForSeedFile(path, mode)]),
  101. ]),
  102. ),
  103. )
  104. .digest("hex");
  105. const manifest = {
  106. hash: manifestHash,
  107. plugins,
  108. version: 1,
  109. };
  110. const manifestPath = resolve(stagingDirectory, "official-plugins-manifest.json");
  111. await mkdir(stagingDirectory, {
  112. recursive: true,
  113. });
  114. await writeFile(manifestPath, JSON.stringify(manifest, null, 2));
  115. assets[seaOfficialPluginManifestAssetKey] = manifestPath;
  116. return {
  117. assets,
  118. manifest,
  119. };
  120. };
  121. function assertPluginRoot(pluginRoot, plugin) {
  122. if (!existsSync(join(pluginRoot, ".zcode-plugin", "plugin.json"))) {
  123. throw new Error(`Missing ${plugin.name} plugin manifest at ${pluginRoot}`);
  124. }
  125. }
  126. function assertPluginRequiredSeedAssets(pluginRoot, plugin) {
  127. for (const relativePath of plugin.requiredSeedPaths ?? []) {
  128. const assetPath = join(pluginRoot, ...relativePath.split("/"));
  129. if (!existsSync(assetPath)) {
  130. throw new Error(`Missing ${plugin.name} required seed asset at ${assetPath}`);
  131. }
  132. }
  133. }
  134. function assertPluginRuntime(pluginRoot, plugin) {
  135. for (const relativePath of plugin.requiredRuntimePaths ?? ["dist/mcp/server.js"]) {
  136. const runtimePath = join(pluginRoot, ...relativePath.split("/"));
  137. if (!existsSync(runtimePath)) {
  138. const assetKind = relativePath === "dist/mcp/server.js" ? "MCP runtime" : "runtime asset";
  139. throw new Error(
  140. `Missing ${plugin.name} ${assetKind} at ${runtimePath}. ` +
  141. `Run \`pnpm --filter ${plugin.packageName} build\` before \`pnpm sea\`.`,
  142. );
  143. }
  144. }
  145. }
  146. async function* walkFiles(directory) {
  147. const entries = await readdir(directory, {
  148. withFileTypes: true,
  149. });
  150. for (const entry of entries) {
  151. if (shouldSkipDirectory(entry.name)) continue;
  152. const fullPath = resolve(directory, entry.name);
  153. if (entry.isDirectory()) {
  154. yield* walkFiles(fullPath);
  155. continue;
  156. }
  157. if (entry.isFile()) {
  158. yield fullPath;
  159. }
  160. }
  161. }
  162. const shouldSkipDirectory = (name) =>
  163. name === "node_modules" ||
  164. name === ".turbo" ||
  165. name === "coverage" ||
  166. name === ".venv" ||
  167. name === "__pycache__";
  168. const includedTopLevelPaths = new Set([
  169. ".mcp.json",
  170. ".zcode-plugin",
  171. "README.md",
  172. // SEA 资源采集曾只允许 skills/commands,导致 document-skills 的 judge 子代理未进入可执行文件。
  173. "agents",
  174. "commands",
  175. "dist",
  176. "docs",
  177. "hooks",
  178. "output-styles",
  179. "package.json",
  180. "scripts",
  181. "skills",
  182. "templates",
  183. ]);
  184. const shouldIncludePluginFile = (relativePath) => {
  185. const segments = relativePath.split(sep);
  186. if (segments.includes(".DS_Store") || segments.some((segment) => segment.endsWith(".pyc"))) {
  187. return false;
  188. }
  189. const [topLevel] = relativePath.split(sep);
  190. return topLevel !== undefined && includedTopLevelPaths.has(topLevel);
  191. };
  192. const toPosixPath = (value) => value.split(sep).join("/");
  193. const modeForSeedFile = (filePath, sourceMode) => {
  194. if (sourceMode !== undefined && (sourceMode & 0o111) !== 0) return 0o755;
  195. const normalizedPath = toPosixPath(filePath);
  196. if (/(?:^|\/)dist\/mcp\/server\.js$/i.test(normalizedPath)) return 0o755;
  197. if (/^hooks\//u.test(normalizedPath) && !/\.(json|md|txt)$/iu.test(normalizedPath)) {
  198. return 0o755;
  199. }
  200. return 0o644;
  201. };