#!/usr/bin/env node
import { randomUUID } from 'node:crypto';
import path from 'node:path';
import { ACodeRuntime } from './runtime.js';
function getFlagValue(args, name) {
    const index = args.indexOf(name);
    if (index >= 0) {
        return args[index + 1];
    }
    const prefixed = args.find((arg) => arg.startsWith(`${name}=`));
    return prefixed ? prefixed.slice(name.length + 1) : undefined;
}
async function main() {
    const args = process.argv.slice(2);
    const cwd = path.resolve(getFlagValue(args, '--cwd') || process.cwd());
    const configPath = getFlagValue(args, '--config');
    const skillsDir = getFlagValue(args, '--skills-dir');
    const prompt = getFlagValue(args, '--prompt') || args.filter((arg) => !arg.startsWith('--')).join(' ');
    const runtime = new ACodeRuntime({
        sessionId: getFlagValue(args, '--session-id') || randomUUID(),
        workingDirectory: cwd,
        configPath,
        skillsDir,
        initialPrompt: prompt,
        model: getFlagValue(args, '--model'),
    });
    runtime.on('event', (event) => {
        process.stdout.write(`${JSON.stringify(event)}\n`);
    });
    await runtime.start();
    await runtime.stop();
}
main().catch((error) => {
    const message = error instanceof Error ? error.message : String(error);
    process.stderr.write(`[acode] ${message}\n`);
    process.exitCode = 1;
});
