import { spawn as defaultSpawn } from "@poe-code/agent-spawn";
import { resolveWorkflowKind } from "../drivers/kind.js";
import { getDriver } from "../drivers/registry.js";
export async function runAttempt(args) {
    const runner = new AttemptRunner(args);
    return runner.run();
}
class AttemptRunner {
    args;
    constructor(args) {
        this.args = args;
    }
    async run() {
        const planKind = resolveWorkflowKind(this.args.task);
        const driver = getDriver(planKind);
        if (driver === undefined) {
            throw new Error(`no driver registered for kind ${planKind}`);
        }
        return driver.run(this.createDriverContext());
    }
    createDriverContext() {
        return {
            task: this.args.task,
            attempt: this.args.attempt,
            workspaceDir: this.args.workspaceDir ?? "",
            planPath: this.args.task.sourcePath ?? null,
            cfg: this.args.cfg,
            abort: this.args.abort,
            emit: (event) => this.args.deps.onEvent?.(event),
            spawn: this.args.deps.spawn ?? defaultSpawn,
            taskPromptTemplate: this.args.deps.taskPromptTemplate,
            refreshTask: this.args.deps.refreshTask,
            reconcile: this.args.deps.reconcile,
            logger: this.args.deps.logger ?? noopLogger
        };
    }
}
const noopLogger = {
    warn() { }
};
