gongxiangp2/apps/detail-agent-pi/scripts/build-skill-runtime.mjs

40 lines
2.0 KiB
JavaScript

import { spawnSync } from 'node:child_process';
import { copyFileSync, mkdirSync, mkdtempSync, rmSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { basename, join, resolve } from 'node:path';
import { fileURLToPath, pathToFileURL } from 'node:url';
import { parseArgs } from 'node:util';
const source = fileURLToPath(new URL('../skill-runtime/', import.meta.url));
const images = ['bash:5.2', 'python:3.13-alpine'];
const pillow = '12.3.0';
const docker = ['--context', 'desktop-linux'];
function run(command, args) {
const result = spawnSync(command, args, { stdio: 'inherit' });
if (result.error) throw result.error;
if (result.status !== 0) throw Error(command + ' failed (' + (result.status ?? result.signal) + ')');
}
export function buildSkillRuntime({ tempDir = tmpdir(), wheel } = {}, execute = run) {
execute('docker', [...docker, 'image', 'inspect', '--format', '{{.Id}}', ...images]);
mkdirSync(tempDir, { recursive: true });
const context = mkdtempSync(join(resolve(tempDir), 'xiaoyi-skill-'));
try {
for (const file of ['Dockerfile', '.dockerignore']) copyFileSync(join(source, file), join(context, file));
if (wheel) copyFileSync(wheel, join(context, basename(wheel)));
else execute('python', ['-m', 'pip', 'download', '--only-binary=:all:', '--no-deps',
'--platform', 'musllinux_1_2_x86_64', '--implementation', 'cp', '--python-version', '3.13',
'--abi', 'cp313', '--dest', context, 'Pillow==' + pillow]);
execute('docker', [...docker, 'build', '--builder', 'desktop-linux', '--pull=false', '--network=none',
'-t', 'gongxiangp2-skill-runtime:python3.13-pillow' + pillow, context]);
} finally {
rmSync(context, { recursive: true, force: true });
}
}
if (process.argv[1] && pathToFileURL(resolve(process.argv[1])).href === import.meta.url) {
const { values } = parseArgs({ options: { 'temp-dir': { type: 'string' }, wheel: { type: 'string' } } });
buildSkillRuntime({ tempDir: values['temp-dir'], wheel: values.wheel });
}