sea-bundled-skill-assets.mjs 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. // 随 CLI 内置的技能包(apps/zcode-cli/packages/bundled-skills)。它不是官方插件:不进市场目录、
  6. // 没有版本身份,运行时按内容 hash 解压到 `<cli storage>/bundled-skills/<hash>/`
  7. // (bootstrap/src/app/bundled-skills.ts)。这里的 manifest 形状与那边的读取逐字对应。
  8. export const seaBundledSkillAssetPrefix = "zcode-bundled-skills/";
  9. export const seaBundledSkillManifestAssetKey = `${seaBundledSkillAssetPrefix}manifest.json`;
  10. export const bundledSkillPackRootPath = join("packages", "bundled-skills");
  11. export const bundledSkillPackSkillsDirectory = "skills";
  12. // 与 bootstrap 的 BUNDLED_SKILL_PACK_REQUIRED_PATHS 对齐:缺任一项即中止 SEA 构建,
  13. // 不把一个引用文件残缺的技能包发进正式二进制。
  14. export const bundledSkillPackRequiredPaths = [
  15. "skills/dynamic-workflows/SKILL.md",
  16. "skills/dynamic-workflows/patterns.md",
  17. "skills/dynamic-workflows/examples.md",
  18. ];
  19. export const collectSeaBundledSkillAssets = async ({ root, stagingDirectory }) => {
  20. const packRoot = resolve(root, bundledSkillPackRootPath);
  21. assertBundledSkillPack(packRoot);
  22. await rm(stagingDirectory, { force: true, recursive: true });
  23. const files = [];
  24. const assets = {};
  25. for await (const sourcePath of walkFiles(join(packRoot, bundledSkillPackSkillsDirectory))) {
  26. const relativePath = relative(packRoot, sourcePath);
  27. if (!shouldIncludeFile(relativePath)) continue;
  28. const bytes = await readFile(sourcePath);
  29. const sourceStats = await stat(sourcePath);
  30. const posixPath = toPosixPath(relativePath);
  31. assets[`${seaBundledSkillAssetPrefix}${posixPath}`] = sourcePath;
  32. files.push({
  33. mode: modeForFile(sourceStats.mode),
  34. path: posixPath,
  35. sha256: createHash("sha256").update(bytes).digest("hex"),
  36. });
  37. }
  38. files.sort((left, right) => left.path.localeCompare(right.path));
  39. const manifest = {
  40. hash: createHash("sha256")
  41. .update(JSON.stringify(files.map(({ path, sha256, mode }) => [path, sha256, mode])))
  42. .digest("hex"),
  43. files,
  44. version: 1,
  45. };
  46. const manifestPath = resolve(stagingDirectory, "bundled-skills-manifest.json");
  47. await mkdir(stagingDirectory, { recursive: true });
  48. await writeFile(manifestPath, JSON.stringify(manifest, null, 2));
  49. assets[seaBundledSkillManifestAssetKey] = manifestPath;
  50. return { assets, manifest };
  51. };
  52. function assertBundledSkillPack(packRoot) {
  53. if (!existsSync(join(packRoot, bundledSkillPackSkillsDirectory))) {
  54. throw new Error(`Missing bundled skill pack at ${packRoot}`);
  55. }
  56. for (const relativePath of bundledSkillPackRequiredPaths) {
  57. const assetPath = join(packRoot, ...relativePath.split("/"));
  58. if (!existsSync(assetPath)) {
  59. throw new Error(`Missing bundled skill pack required asset at ${assetPath}`);
  60. }
  61. }
  62. }
  63. async function* walkFiles(directory) {
  64. const entries = await readdir(directory, { withFileTypes: true });
  65. for (const entry of entries) {
  66. if (entry.name === "node_modules" || entry.name === ".turbo") continue;
  67. const fullPath = resolve(directory, entry.name);
  68. if (entry.isDirectory()) {
  69. yield* walkFiles(fullPath);
  70. continue;
  71. }
  72. if (entry.isFile()) yield fullPath;
  73. }
  74. }
  75. const shouldIncludeFile = (relativePath) => !relativePath.split(sep).includes(".DS_Store");
  76. const toPosixPath = (value) => value.split(sep).join("/");
  77. const modeForFile = (sourceMode) => ((sourceMode & 0o111) !== 0 ? 0o755 : 0o644);