import { mergeSystemPrompts, } from "../../provider/model-provider.js";
import { buildToolFollowUpSystemPrompt, stripRequiredToolSystemPrompt, } from "../../prompt/runtime-prompts.js";
import { isRoutingDowngradeEligible } from "./provider-replay-context.js";
import { appendRuntimeObservation } from "./runtime-observation.js";
import { resolveProviderVisibleTools } from "./tool-constraint.js";
export function buildReplayToolOutputRepairContexts(input) {
    const toolStepCount = input.payload.toolStepCount + input.completedToolCalls.length;
    const base = {
        activeSkillIds: input.payload.activeSkillIds,
        advancedPhase: input.payload.advancedPhase,
        artifactObligations: input.payload.artifactObligations,
        availableTools: input.payload.availableTools,
        budget: input.replayProviderTurnBudget,
        downgradeEligible: isRoutingDowngradeEligible(input.payload.phaseName),
        objective: input.payload.objective,
        phaseName: input.payload.phaseName,
        profile: input.replayProfile,
        profileName: input.payload.profileName,
        requiredToolConstraint: input.repairRequiredToolConstraint,
        routeConfigOverride: input.repairRouteConfig,
        toolStepCount,
    };
    return {
        providerContext: {
            ...base,
            fixedRouteConfigOverride: input.fixedReplayRoute,
        },
        resolveContext: {
            ...base,
            fixedRouteConfigOverride: input.fixedReplayRoute,
        },
        toolStepCount,
    };
}
export function prepareReplayToolOutputRepair(input) {
    const requiresDeliverableAssertion = input.requiresDeliverableAssertion || input.repairCode === "PROMPT_DELIVERABLE_MISSING";
    const suppressStructuredControls = input.suppressStructuredControls || requiresDeliverableAssertion;
    const providerRepairMessages = appendRuntimeObservation(input.repairMessages, input.repairObservation);
    const repairRequiredToolConstraint = suppressStructuredControls
        ? undefined
        : input.unresolvedRequiredToolConstraint;
    return {
        providerRepairMessages,
        repairActionPolicy: input.repairActionPolicyOverride ??
            (suppressStructuredControls
                ? {
                    allowedActions: ["tool"],
                    directResponseAllowed: false,
                }
                : undefined),
        repairRequiredToolConstraint,
        repairSystemPrompt: mergeSystemPrompts(stripRequiredToolSystemPrompt(input.payload.systemPrompt, input.payload.requiredToolConstraint), buildToolFollowUpSystemPrompt(input.completedToolCalls)),
        requiresDeliverableAssertion,
        visibleTools: resolveProviderVisibleTools(input.payload.availableTools, repairRequiredToolConstraint, providerRepairMessages),
    };
}
//# sourceMappingURL=provider-replay-tool-output-repair-context.js.map