import { getWorkflowStepKind, isSystemWorkflowStep, isWorkflowCallStep } from '../step-kind.js';
import { determineRuleTransition } from './transitions.js';
export class WorkflowEngineStepCoordinator {
    deps;
    constructor(deps) {
        this.deps = deps;
    }
    getStep(name) {
        const step = this.deps.config.steps.find((candidate) => candidate.name === name);
        if (!step) {
            throw new Error(`Unknown step: ${name}`);
        }
        return step;
    }
    resolveRuntimeForStep(step) {
        if (isWorkflowCallStep(step)) {
            return this.deps.workflowCallRunner.resolveRuntime(step);
        }
        return undefined;
    }
    async runStep(step, prebuiltInstruction, runtime) {
        const updateSession = this.deps.updatePersonaSession;
        let result;
        if (step.parallel && step.parallel.length > 0) {
            result = await this.deps.parallelRunner.runParallelStep(step, this.deps.state, this.deps.task, this.deps.getMaxSteps(), updateSession, runtime);
        }
        else if (step.arpeggio) {
            result = await this.deps.arpeggioRunner.runArpeggioStep(step, this.deps.state, runtime);
        }
        else if (step.teamLeader) {
            result = await this.deps.teamLeaderRunner.runTeamLeaderStep(step, this.deps.state, this.deps.task, this.deps.getMaxSteps(), updateSession, runtime);
        }
        else if (isSystemWorkflowStep(step)) {
            result = {
                response: await this.deps.systemStepExecutor.run(step, this.deps.state),
                instruction: '',
            };
        }
        else if (isWorkflowCallStep(step)) {
            result = await this.deps.workflowCallRunner.run(step, runtime);
        }
        else {
            result = await this.deps.stepExecutor.runNormalStep(step, this.deps.state, this.deps.task, this.deps.getMaxSteps(), updateSession, prebuiltInstruction, runtime);
        }
        for (const { step: reportedStep, filePath, fileName } of this.deps.stepExecutor.drainReportFiles()) {
            this.deps.emitReport(reportedStep, filePath, fileName);
        }
        return result;
    }
    resolveNextStepFromDone(step, response) {
        const transition = this.resolveTransitionFromDone(step, response);
        if (transition.nextStep) {
            return transition.nextStep;
        }
        throw new Error(`Step "${step.name}" resolved to a return transition where a next step is required`);
    }
    resolveTransitionFromDone(step, response) {
        if (response.status !== 'done') {
            throw new Error(`Unhandled response status: ${response.status}`);
        }
        if (response.matchedRuleIndex != null && step.rules) {
            const transition = determineRuleTransition(step, response.matchedRuleIndex);
            if (transition && (transition.nextStep || transition.returnValue || transition.requiresUserInput)) {
                return transition;
            }
        }
        throw new Error(`No matching rule found for step "${step.name}" (status: ${response.status}, kind: ${getWorkflowStepKind(step)})`);
    }
    buildInstruction(step, stepIteration) {
        return this.deps.stepExecutor.buildInstruction(step, stepIteration, this.deps.state, this.deps.task, this.deps.getMaxSteps());
    }
    buildPhase1Instruction(step, instruction, runtime) {
        return this.deps.stepExecutor.buildPhase1Instruction(instruction, step, runtime);
    }
    runLoopMonitorJudge(monitor, cycleCount, triggeringStep, triggeringRuntime) {
        return this.deps.loopMonitorJudgeRunner.run(monitor, cycleCount, triggeringStep, triggeringRuntime);
    }
}
//# sourceMappingURL=WorkflowEngineStepCoordinator.js.map