Files
vibemake/scripts/package-new-simulations.mjs

70 lines
6.7 KiB
JavaScript

// Pack data-only Robot Packages for the admin folder uploader. LFS placeholders
// are hydrated only when the fetched bytes match the supplied source's hash.
import { readFile, writeFile, readdir, mkdir } from 'node:fs/promises';
import { resolve, join, dirname, relative } from 'node:path';
import { createHash } from 'node:crypto';
import { execFileSync } from 'node:child_process';
import assert from 'node:assert/strict';
import { validatePackage } from '../sandbox/src/platform/loader/SchemaValidator.js';
import { readOnnxMetadata } from '../sandbox/src/platform/loader/metadata.js';
import { resourceIndex, validateRobotResources } from '../js/project-resources.js';
const [sourceArg, hydratedArg, outputArg] = process.argv.slice(2);
if (!outputArg) throw new Error('Usage: node scripts/package-new-simulations.mjs SOURCE_ROBOTS HYDRATED_ROBOTS NEW_OUTPUT_FOLDER');
const source = resolve(sourceArg), hydrated = resolve(hydratedArg), output = resolve(outputArg);
const sha = (bytes) => createHash('sha256').update(bytes).digest('hex');
const json = (path, data) => writeFile(path, JSON.stringify(data, null, 2) + '\n');
const walk = async (dir) => (await Promise.all((await readdir(dir, { withFileTypes: true }))
.filter(e => !e.name.startsWith('.')).map(e => e.isDirectory() ? walk(join(dir, e.name)) : [join(dir, e.name)]))).flat().sort();
await mkdir(output); // Never overwrite an earlier delivery.
const report = { checkedAt: new Date().toISOString(), browserPlaybackTested: false, robots: [] };
for (const [id, folder] of [['t1', '01-Booster-T1'], ['apollo', '02-Apptronik-Apollo']]) {
const original = join(source, id), destination = join(output, folder, '仿真资源');
const inventory = [];
let hydratedFiles = 0;
for (const path of await walk(original)) {
const name = relative(original, path);
let bytes = await readFile(path);
const pointer = /^version https:\/\/git-lfs.github.com\/spec\/v1\noid sha256:([a-f0-9]{64})\nsize (\d+)\s*$/.exec(bytes.toString('utf8'));
if (pointer) {
bytes = await readFile(join(hydrated, id, name));
assert.equal(bytes.length, Number(pointer[2]), `${id}/${name}: LFS size`);
assert.equal(sha(bytes), pointer[1], `${id}/${name}: LFS hash`);
hydratedFiles++;
}
assert.ok(!bytes.subarray(0, 100).toString().includes('git-lfs.github.com/spec'), `${name}: placeholder`);
const target = join(destination, name);
await mkdir(dirname(target), { recursive: true });
await writeFile(target, bytes);
inventory.push({ path: name, bytes: bytes.length, sha256: sha(bytes) });
}
const config = validatePackage(JSON.parse(await readFile(join(destination, 'robot.json'))));
const manifest = JSON.parse(await readFile(join(destination, 'source-manifest.json')));
for (const expected of manifest.files) {
const actual = inventory.find(f => f.path === expected.file);
assert.ok(actual, `${id}: missing ${expected.file}`);
assert.equal(actual.bytes, expected.bytes, `${id}/${expected.file}: source size`);
assert.equal(actual.sha256, expected.sha256, `${id}/${expected.file}: source hash`);
}
const entries = inventory.map(f => ({ relativePath: f.path, sizeBytes: f.bytes, url: `https://assets.example/${id}/${f.path}` }));
const packet = validateRobotResources(config, 'robot.json', resourceIndex(entries));
assert.equal(packet.warnings.length, 0);
assert.deepEqual(packet.capabilities, [{ id: 'walk', skillId: 'walk' }]);
const primary = config.skills.find(s => s.kind === 'primary');
const metadata = readOnnxMetadata(await readFile(join(destination, primary.controller.policy)));
assert.deepEqual(metadata.joint_names.split(','), config.joints.groups[primary.owns]);
const jointCount = config.joints.groups[primary.owns].length;
const totalBytes = inventory.reduce((n, f) => n + f.bytes, 0);
const zip = `${id}-仿真上传.zip`;
execFileSync('/usr/bin/zip', ['-q', '-r', join(output, folder, zip), '.'], { cwd: destination });
execFileSync('/usr/bin/unzip', ['-tq', join(output, folder, zip)], { stdio: 'pipe' });
const zipBytes = await readFile(join(output, folder, zip));
await json(join(output, folder, '文件校验清单.json'), { id, totalBytes, files: inventory, zip: { path: zip, bytes: zipBytes.length, sha256: sha(zipBytes) } });
await writeFile(join(output, folder, '上传说明.txt'), `${config.name} · 仿真上传\n\n后台“机器人运动演示 / 仿真”→ 选择整个“仿真资源”文件夹。\n如果后台使用 ZIP 上传,则选择同目录的“${zip}”。两种方式选一种,不要重复上传。\n\n文件夹内 ${inventory.length} 个文件,约 ${(totalBytes / 1048576).toFixed(1)} MiB。\n入口:robot.json;物理模型:${config.model};策略:${primary.controller.policy};模型网格 ${config.assets.length} 个。\n保留全部子目录、文件名和大小写。不要只选择 meshes/assets/policies,也不要选择包含 ZIP 的外层文件夹。\n\n本包只提供仿真:行走、方向控制、暂停、重置。没有声明踢球或跳舞,不会强制添加这些动作。\n本包没有整机 GLB、装配零件 GLB 和拼装步骤,不要上传到这些栏目。仿真使用原工程的 MJCF + STL + ONNX,未修改材质、训练策略或控制参数。\n\n社区播放器需包含本次 linear_velocity 观测及地面碰撞兼容修改;仅上传文件不会更新线上播放器。\n`);
report.robots.push({ id, name: config.name, fileCount: inventory.length, hydratedFiles, totalBytes, jointCount, meshCount: config.assets.length, skills: config.skills.map(s => s.id), sourceManifestVerified: true });
console.log(`${config.name}: ${inventory.length} files, ${(totalBytes / 1048576).toFixed(1)} MiB, ${hydratedFiles} LFS objects verified`);
}
await json(join(output, '校验报告.json'), report);
await writeFile(join(output, '先看这里-上传说明.txt'), `新增两款机器人 · 后台仿真上传\n\n01-Booster-T1\n02-Apptronik-Apollo\n\n两个机器人分别对应两个后台项目。每个项目在“机器人运动演示 / 仿真”栏目上传其“仿真资源”文件夹;若后台提供 ZIP 导入,上传同目录 ZIP。\n不要把两个机器人合成一个资源包,不要把最外层总文件夹作为一个机器人上传。\n\n原始模型文件、控制配置、策略、来源和许可证已保留。真实 STL/ONNX 已从原仓库获取并逐文件核对原始 SHA256,不是源码压缩包中的 LFS 占位文件。\n\n上传完成后刷新社区详情,点击“仿真”。打开默认不自动走;按方向或自动行走开始。\n线上播放器必须更新本次兼容代码,否则旧版本会拒绝 linear_velocity 观测。\n\n此处只有仿真资料;整机展示和 AI 装配需要另外提供对应 GLB 与步骤文件。\n`);
console.log(output);