import { runCommand } from "@poe-code/agent-spawn";
import { UserError } from "toolcraft";
import { workflowSubprocessTimeoutMs } from "./subprocess-timeout.js";
function formatCommand(command, args) {
    return [command, ...args].join(" ");
}
function trimOutput(value) {
    return value.trim();
}
export function resolveWorkflowAgent(automation) {
    const configured = automation.agent?.trim();
    return configured && configured.length > 0 ? configured : "codex";
}
export function formatCommandFailure(command, args, result) {
    const stderr = trimOutput(result.stderr);
    const stdout = trimOutput(result.stdout);
    return [
        `Command failed with exit code ${result.exitCode}: ${formatCommand(command, args)}`,
        stderr.length > 0 ? `stderr:\n${stderr}` : null,
        stdout.length > 0 ? `stdout:\n${stdout}` : null
    ]
        .filter((value) => value !== null)
        .join("\n");
}
async function runPoeCodeCommand(args, cwd, runner) {
    const result = await runner("poe-code", args, {
        cwd,
        timeoutMs: workflowSubprocessTimeoutMs
    });
    if (result.exitCode !== 0) {
        throw new UserError(formatCommandFailure("poe-code", args, result));
    }
}
export async function setupWorkflowAgent(automation, cwd, runner = runCommand) {
    const agent = resolveWorkflowAgent(automation);
    await runPoeCodeCommand(["install", agent, "--yes"], cwd, runner);
    await runPoeCodeCommand(["configure", agent, "--yes"], cwd, runner);
    return agent;
}
