generate-child.ts 1.94 KB
/**
 * Child process entry point for service-mode generation.
 *
 * Why a separate process: generation (especially VALIDATION's Playwright work
 * and the multi-agent INITIAL node) is heavy and can crash the whole event
 * loop. By running it in a detached child, the `serve` UI process stays alive
 * no matter what happens during a run. Logs are forwarded to the parent by
 * simply printing to stdout (the parent captures and re-broadcasts them via
 * SSE), and a sentinel line marks completion so the parent can flip job state.
 */
import { design2code } from '../graph';
import { ValidationMode } from '../types/graph-types';
import { patchEvoltagentStreamingForceOff } from '../utils/patch-evoltagent';

function mapMode(mode: string): ValidationMode {
    if (mode === 'code') return ValidationMode.CodeOnly;
    if (mode === 'with-report') return ValidationMode.ReportOnly;
    return ValidationMode.Full;
}

async function main(): Promise<void> {
    const argv = process.argv.slice(2);
    const get = (name: string): string | undefined => {
        const i = argv.indexOf(`--${name}`);
        return i >= 0 ? argv[i + 1] : undefined;
    };
    const figmaUrl = get('figmaUrl');
    const mode = mapMode(get('mode') ?? 'full');
    const outputDir = get('outputDir');
    const appName = get('appName');

    if (!figmaUrl) {
        process.stdout.write('__D2C_ERROR__:missing figmaUrl\n');
        process.exit(1);
    }

    // Force non-streaming agent calls (see patch module).
    patchEvoltagentStreamingForceOff();

    try {
        await design2code(figmaUrl, mode, {
            outputDir: outputDir || undefined,
            appName: appName || undefined,
            resume: false,
        });
        process.stdout.write('__D2C_DONE__\n');
        process.exit(0);
    } catch (err) {
        const message = err instanceof Error ? err.message : String(err);
        process.stdout.write(`__D2C_ERROR__:${message}\n`);
        process.exit(1);
    }
}

main();