/**
 * Headless ACP runner.
 *
 * Glue between the `agents run <agent> "prompt" --acp` command and the ACP
 * client. Emits either a human-readable stream of agent messages or newline-
 * delimited JSON events, depending on `--json`.
 */
import { runAcp } from './client.js';
/** Runs a prompt turn over ACP, streaming output to stdout per `json` mode. */
export async function runAcpHeadless(opts) {
    const onUpdate = opts.json ? emitJsonLine : emitTextChunk;
    const result = await runAcp({
        agent: opts.agent,
        prompt: opts.prompt,
        cwd: opts.cwd,
        mode: opts.mode,
        onUpdate,
    });
    if (opts.json) {
        emitJsonLine({
            sessionId: result.sessionId,
            update: { sessionUpdate: 'agent_message_chunk', content: { type: 'text', text: '' } },
            _meta: { stopReason: result.stopReason },
        });
    }
    else {
        process.stdout.write('\n');
    }
    return result.stopReason === 'end_turn' ? 0 : 1;
}
function emitJsonLine(n) {
    process.stdout.write(JSON.stringify(n) + '\n');
}
function emitTextChunk(n) {
    const upd = n.update;
    if (upd.sessionUpdate === 'agent_message_chunk' && upd.content.type === 'text') {
        process.stdout.write(upd.content.text);
    }
}
