import { assertReplayToolOutputDeliverables } from "./provider-replay-artifact-assertion.js";
import { runReplayRepairDecision } from "./provider-replay-decision-runner.js";
import { buildReplayToolOutputRepairContexts, prepareReplayToolOutputRepair, } from "./provider-replay-tool-output-repair-context.js";
export async function repairReplayToolOutputWithObservation(input) {
    const { completedToolCalls, fixedReplayRoute, payload, repairCode, repairMessages, repairObservation, repairRouteConfig, replayProfile, replayProviderTurnBudget, runtime, turn, unresolvedRequiredToolConstraint, } = input;
    const repair = prepareReplayToolOutputRepair({
        completedToolCalls,
        payload,
        repairActionPolicyOverride: input.repairActionPolicyOverride,
        repairCode,
        repairMessages,
        repairObservation,
        requiresDeliverableAssertion: input.requiresDeliverableAssertion,
        suppressStructuredControls: input.suppressStructuredControls,
        unresolvedRequiredToolConstraint,
    });
    const repairContexts = buildReplayToolOutputRepairContexts({
        completedToolCalls,
        fixedReplayRoute,
        payload,
        repairRequiredToolConstraint: repair.repairRequiredToolConstraint,
        repairRouteConfig,
        replayProfile,
        replayProviderTurnBudget,
    });
    const repairedText = await runReplayRepairDecision({
        actionPolicy: repair.repairActionPolicy,
        allowPhaseResult: false,
        allowPlanUpdate: false,
        artifactObligations: payload.artifactObligations ?? [],
        availableTools: payload.availableTools,
        baseMessages: repairMessages,
        cacheControl: payload.cacheControl,
        controls: {
            allowPhaseResult: false,
            allowPlanUpdate: false,
            ...(payload.phaseResultPhaseName
                ? { phaseResultPhaseName: payload.phaseResultPhaseName }
                : {}),
        },
        correctionOperation: "tool-followup",
        phaseResultPhaseName: payload.phaseResultPhaseName,
        providerContext: repairContexts.providerContext,
        repairCode,
        repairObservation,
        responseRepairDeliverableFailures: input.responseRepairDeliverableFailures,
        requiredToolConstraint: repair.repairRequiredToolConstraint,
        resolveContext: repairContexts.resolveContext,
        runtime,
        objective: payload.objective,
        systemPrompt: repair.repairSystemPrompt,
        toolsForTrace: payload.availableTools,
        toolStepCount: repairContexts.toolStepCount,
        traceSystemPrompt: payload.systemPrompt,
        turn,
        visibleTools: repair.visibleTools,
    });
    await assertReplayToolOutputDeliverables({
        payload,
        repairMessages,
        requiresDeliverableAssertion: repair.requiresDeliverableAssertion,
        runtime,
        turn,
    });
    return repairedText;
}
//# sourceMappingURL=provider-replay-tool-output-repair-runner.js.map