import { repairDurableGenerateTerminalOutput } from "./provider-terminal-repair-runner.js";
import { repairDurableGenerateFinalAnswer } from "./provider-final-answer-repair-runner.js";
import { validateProviderResponseFinalAnswer } from "./provider-final-answer-handler.js";
export { repairDurableGenerateTerminalOutput } from "./provider-terminal-repair-runner.js";
export { repairDurableGenerateFinalAnswer } from "./provider-final-answer-repair-runner.js";
const repairGenerateTraceOutcomesKey = Symbol("repairGenerateTraceOutcomes");
export function getRepairGenerateTraceOutcomes(error) {
    return typeof error === "object" && error !== null
        ? error[repairGenerateTraceOutcomesKey]
        : undefined;
}
function throwWithRepairGenerateTraceOutcomes(error, outcomes) {
    if (typeof error === "object" && error !== null && outcomes.length > 0) {
        Object.defineProperty(error, repairGenerateTraceOutcomesKey, {
            configurable: true,
            value: outcomes,
        });
    }
    throw error;
}
export async function repairDurableGenerateResponse(input) {
    const terminalRepair = await repairDurableGenerateTerminalOutput(input);
    const repairGenerateOutcomes = [];
    const responseText = terminalRepair.responseText;
    if (input.validateFinalAnswer === false) {
        if (terminalRepair.repaired) {
            repairGenerateOutcomes.push({ runtimeOutcome: "accepted" });
        }
        return {
            finalAnswerRepaired: false,
            repairGenerateOutcomes,
            responseText,
            terminalOutputRepaired: terminalRepair.repaired,
        };
    }
    const terminalRepairFinalValidation = terminalRepair.repaired
        ? validateProviderResponseFinalAnswer({
            availableTools: input.availableTools,
            messages: input.messages,
            objective: input.objective,
            text: responseText,
        })
        : undefined;
    let finalAnswerRepair;
    try {
        finalAnswerRepair = await repairDurableGenerateFinalAnswer({
            availableTools: input.availableTools,
            cacheControl: input.cacheControl,
            finalAnswerRepairAttempted: input.finalAnswerRepairAttempted,
            messages: input.messages,
            objective: input.objective,
            operation: input.operation,
            requestBudgetSnapshot: input.requestBudgetSnapshot,
            responseText,
            runtime: input.runtime,
            systemPrompt: input.systemPrompt,
            turn: input.turn,
        });
    }
    catch (error) {
        if (terminalRepair.repaired) {
            throwWithRepairGenerateTraceOutcomes(error, [
                {
                    runtimeOutcome: "repaired",
                    validation: terminalRepairFinalValidation && !terminalRepairFinalValidation.ok
                        ? {
                            message: terminalRepairFinalValidation.observation.message,
                            reasonCode: "final-answer-unsupported",
                        }
                        : {
                            message: "Generate repair response was superseded by final-answer repair.",
                            reasonCode: "final-answer-unsupported",
                        },
                },
            ]);
        }
        throw error;
    }
    if (terminalRepair.repaired) {
        repairGenerateOutcomes.push(finalAnswerRepair.repaired
            ? {
                runtimeOutcome: "repaired",
                validation: finalAnswerRepair.repairValidation ?? {
                    message: "Generate repair response was superseded by final-answer repair.",
                    reasonCode: "final-answer-unsupported",
                },
            }
            : { runtimeOutcome: "accepted" });
    }
    if (finalAnswerRepair.repaired) {
        repairGenerateOutcomes.push({ runtimeOutcome: "accepted" });
    }
    return {
        finalAnswerRepaired: finalAnswerRepair.repaired,
        repairGenerateOutcomes,
        responseText: finalAnswerRepair.responseText,
        terminalOutputRepaired: terminalRepair.repaired,
    };
}
//# sourceMappingURL=provider-durable-response-repair.js.map