refresh-container-skills.mjs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #!/usr/bin/env node
  2. /**
  3. * 按 container-skill-manifest.json 刷新 R730 上容器的技能(2026-12-03)。
  4. *
  5. * 与开通链路共用同一份清单:provision.sh 给新容器装技能,本脚本给存量容器补/替换。
  6. * 新增或替换技能只改清单,不改这里。
  7. *
  8. * 用法:
  9. * node scripts/refresh-container-skills.mjs --list # 只列清单与计划
  10. * node scripts/refresh-container-skills.mjs --dry-run # 对全部容器演算
  11. * node scripts/refresh-container-skills.mjs --containers a,b # 指定容器
  12. * node scripts/refresh-container-skills.mjs --all --apply # 真正下发(需显式 --apply)
  13. * node scripts/refresh-container-skills.mjs --containers x --apply --restart-gateway
  14. *
  15. * 约束(容器扩展铁律):只往 /opt/data/skills/<scope>/<name>/ 写文件,不碰 /opt/hermes 源码。
  16. */
  17. import { readFileSync, mkdirSync, writeFileSync } from 'node:fs';
  18. import { execFileSync } from 'node:child_process';
  19. import { resolve, basename } from 'node:path';
  20. const ROOT = resolve(new URL('..', import.meta.url).pathname.replace(/^\/([A-Za-z]:)/, '$1'));
  21. const MANIFEST = resolve(ROOT, 'cloud/skills/container-skill-manifest.json');
  22. const R730 = resolve(ROOT, '.local-backup/skill-lock-20261201/r730.py');
  23. const argv = process.argv.slice(2);
  24. const has = (f) => argv.includes(f);
  25. const val = (f) => {
  26. const i = argv.indexOf(f);
  27. return i >= 0 ? argv[i + 1] : undefined;
  28. };
  29. const manifest = JSON.parse(readFileSync(MANIFEST, 'utf8'));
  30. const enabled = (manifest.skills ?? []).filter((s) => !s.disabled);
  31. if (enabled.length === 0) {
  32. console.error('清单里没有可下发的技能');
  33. process.exit(1);
  34. }
  35. /** 读取技能文件内容(base64,避免 shell 转义问题) */
  36. function readSkillFiles(skill) {
  37. const srcDir = resolve(ROOT, skill.source);
  38. const out = {};
  39. for (const f of skill.files) {
  40. const p = resolve(srcDir, f);
  41. try {
  42. out[f] = readFileSync(p).toString('base64');
  43. } catch {
  44. throw new Error(`技能 ${skill.name} 缺少文件: ${p}`);
  45. }
  46. }
  47. return out;
  48. }
  49. /** 生成在 R730 上执行的 shell:把技能写进指定容器 */
  50. function buildScript(containers, opts) {
  51. const lines = ['set -e'];
  52. for (const c of containers) {
  53. lines.push(`echo "[CONTAINER] ${c}"`);
  54. for (const skill of enabled) {
  55. const files = readSkillFiles(skill);
  56. const dir = `/opt/data/skills/${skill.scope || 'custom'}/${skill.name}`;
  57. lines.push(`docker exec ${c} mkdir -p ${dir}`);
  58. for (const [name, b64] of Object.entries(files)) {
  59. // base64 -d 写入后 chown 回 hermes(容器内 uid 10000)
  60. lines.push(
  61. `echo "${b64}" | base64 -d | docker exec -i ${c} sh -c "cat > ${dir}/${name}"`,
  62. );
  63. }
  64. lines.push(`docker exec ${c} chmod 0755 ${dir}/*.py 2>/dev/null || true`);
  65. lines.push(`docker exec ${c} chown -R 10000:10000 ${dir} 2>/dev/null || true`);
  66. lines.push(`echo "[SKILL] ${skill.name} -> ${dir}"`);
  67. }
  68. if (opts.restartGateway) {
  69. lines.push(`docker exec ${c} /command/s6-svc -r /run/service/gateway-default || echo "[WARN] 重启网关失败"`);
  70. lines.push(`echo "[RESTART] ${c} 网关已重启"`);
  71. }
  72. }
  73. return lines.join('\n');
  74. }
  75. function listContainers() {
  76. const script = `docker ps --format '{{.Names}}' | grep '^agent-node-app-' || true`;
  77. const out = execFileSync('python', [R730, writeTmp(script), '60'], {
  78. encoding: 'utf8',
  79. cwd: ROOT,
  80. });
  81. return out.split('\n').map((s) => s.trim()).filter((s) => /^agent-node-app-/.test(s));
  82. }
  83. function writeTmp(content) {
  84. const dir = resolve(ROOT, '.local-backup/tmp-wecom');
  85. const f = resolve(dir, `refresh-${Date.now()}.sh`);
  86. // 目录在仓库内,r730.py 只接受仓库内路径
  87. mkdirSync(dir, { recursive: true });
  88. writeFileSync(f, content, 'utf8');
  89. return f;
  90. }
  91. // ---------- 入口 ----------
  92. if (has('--list')) {
  93. console.log(`清单: ${basename(MANIFEST)}(${enabled.length} 个启用)`);
  94. for (const s of enabled) {
  95. console.log(` · ${s.name} → /opt/data/skills/${s.scope || 'custom'}/${s.name}/ ${s.required ? '[必需]' : '[可选]'}`);
  96. }
  97. const off = (manifest.skills ?? []).filter((s) => s.disabled);
  98. if (off.length) {
  99. console.log('已禁用:');
  100. for (const s of off) console.log(` ✗ ${s.name} — ${s.reason ?? ''}`);
  101. }
  102. process.exit(0);
  103. }
  104. let targets = [];
  105. if (val('--containers')) {
  106. targets = val('--containers').split(',').map((s) => s.trim()).filter(Boolean);
  107. } else if (has('--all') || has('--dry-run')) {
  108. targets = listContainers();
  109. }
  110. if (targets.length === 0) {
  111. console.error('没有目标容器。用 --containers a,b 或 --all');
  112. process.exit(1);
  113. }
  114. console.log(`目标容器 ${targets.length} 个: ${targets.join(', ')}`);
  115. console.log(`技能 ${enabled.length} 个: ${enabled.map((s) => s.name).join(', ')}`);
  116. if (has('--dry-run') || !has('--apply')) {
  117. console.log('\n[DRY-RUN] 未加 --apply,不执行任何写入。');
  118. for (const c of targets) {
  119. for (const s of enabled) {
  120. console.log(` 将写入 ${c}:/opt/data/skills/${s.scope || 'custom'}/${s.name}/`);
  121. }
  122. }
  123. process.exit(0);
  124. }
  125. const script = buildScript(targets, { restartGateway: has('--restart-gateway') });
  126. const scriptPath = writeTmp(script);
  127. console.log('\n执行中...');
  128. const out = execFileSync('python', [R730, scriptPath, '600'], { encoding: 'utf8', cwd: ROOT });
  129. console.log(out);
  130. console.log('完成。若改了网关相关配置,记得加 --restart-gateway。');