import type { Command } from "commander"; import { getSessionFromCommand, syncToTOS } from "../services/cerevox"; import { createProgressSpinner } from "../utils/progress"; import fs from "node:fs"; import path from "node:path"; export const name = "music"; export const description = "Music command: create music"; export function register(program: Command): void { const parent = program.command("music").description("Create a new music; requires --prompt"); async function musicCreateAction( this: Command, opts: { prompt?: string; output?: string; } ) { const session = getSessionFromCommand(this as unknown as Record); if (!session) { process.stderr.write("No active session\n"); return; } const prompt = typeof opts.prompt === "string" ? opts.prompt : undefined; if (!prompt || prompt.trim().length === 0) { process.stderr.write("Missing required option: --prompt\n"); process.exitCode = 1; return; } const res = await session.ai.generateMusic({ prompt, onProgress: createProgressSpinner("inferencing"), }); process.stdout.write("\n"); try { if (res?.url) { const tosUrl = await syncToTOS(res.url as string); if (tosUrl) { res.url = tosUrl; } } } catch (ex: unknown) { console.log((ex as Error).message); } const output = typeof opts.output === "string" ? opts.output : undefined; if (output) { const dir = process.cwd(); const url = res.url; const response = await fetch(url); const buffer = Buffer.from(await response.arrayBuffer()); const filePath = path.resolve(dir, output); if (!fs.existsSync(path.dirname(filePath))) { fs.mkdirSync(path.dirname(filePath), { recursive: true }); } fs.writeFileSync(filePath, buffer); res.output = filePath; } console.log({ url: res.url, duration: res.duration, usage: res.usage, output: res.output, }); } parent .option("--prompt ", "Text prompt for music generation (required)") .option("--output ", "Output file path") .action(musicCreateAction); parent .command("create") .description("Create a new music; requires --prompt") .option("--prompt ", "Text prompt for music generation (required)") .option("--output ", "Output file path") .action(musicCreateAction); }