import { semanticToolInputDigest } from "./provider-tool-repeat-guard.js";
import { buildToolFollowUpMessages, toProviderHistoryCompletedToolCall, } from "./provider-tool-observation.js";
import { resolveProviderVisibleTools, resolveUnresolvedToolConstraint } from "./tool-constraint.js";
export function buildProviderToolLoopFollowUpState(input) {
    const { context, currentToolResult } = input;
    const modelVisibleToolOutput = currentToolResult?.output ?? input.toolOutput;
    const nextRepairAttempt = context.repairAttempt ?? 0;
    const nextExecutionFailureCount = (context.executionFailureCount ?? 0) + (currentToolResult?.outcome === "error" ? 1 : 0);
    const completedToolCalls = [
        ...(context.completedToolCalls?.map((call) => toProviderHistoryCompletedToolCall(call, input.renderToolInputForProviderHistory)) ?? []),
        {
            errorCode: currentToolResult?.errorCode,
            failureKind: currentToolResult?.failureKind,
            outcome: currentToolResult?.outcome ?? "success",
            inputStableDigest: input.toolInputStableDigest ?? semanticToolInputDigest(context.toolInput),
            replayPolicy: input.toolReplayPolicyForFollowUp(context.toolName),
            ...(context.providerToolName ? { providerToolName: context.providerToolName } : {}),
            toolCallId: context.toolCallId,
            toolInput: input.renderToolInputForProviderHistory(context.toolName, context.toolInput),
            toolName: context.toolName,
            toolOutput: modelVisibleToolOutput,
        },
    ];
    const unresolvedRequiredToolConstraint = resolveUnresolvedToolConstraint(context.requiredToolConstraint, completedToolCalls);
    const [nextToolCall, ...remainingToolCalls] = context.pendingToolCalls ?? [];
    const consumedToolStepCount = context.toolStepCount + completedToolCalls.length;
    const followUpMessages = buildToolFollowUpMessages(context.messages, completedToolCalls, context.availableTools, input.renderToolInputForProviderHistory);
    const followUpVisibleTools = resolveProviderVisibleTools(context.availableTools, unresolvedRequiredToolConstraint, followUpMessages);
    return {
        completedToolCalls,
        consumedToolStepCount,
        followUpMessages,
        followUpVisibleTools,
        nextExecutionFailureCount,
        nextRepairAttempt,
        nextToolCall,
        remainingToolCalls,
        unresolvedRequiredToolConstraint,
    };
}
//# sourceMappingURL=provider-tool-follow-up-state.js.map