export async function runProviderGenerateOnlyTurn(input) {
    let preparedRequest = await input.prepareRequest();
    await input.assertTurnAttemptWritable();
    let response;
    try {
        response = await input.invokeGenerate(preparedRequest);
    }
    catch (error) {
        if (!input.canRetryContextOverflow(error, preparedRequest.snapshot)) {
            throw error;
        }
        preparedRequest = await input.prepareRequest({ forceCompaction: true });
        await input.assertTurnAttemptWritable();
        response = await input.invokeGenerate(preparedRequest);
    }
    if (!input.sawReasoning() && response.reasoningText) {
        input.emitReasoning(response.reasoningText);
    }
    return input.repairResponse(preparedRequest, response.text);
}
export async function runProviderDecisionTurn(input) {
    let preparedRequest = await input.buildDecideMemoryRequest();
    let requiredToolConstraint = input.resolveRequiredToolConstraint(preparedRequest.request.messages, input.availableTools);
    let providerVisibleTools = input.resolveVisibleTools(input.availableTools, requiredToolConstraint, preparedRequest.request.messages);
    let decision;
    let decisionMessages = preparedRequest.request.messages;
    let decisionSystemPrompt = preparedRequest.request.systemPrompt;
    let decisionPreview = input.createDecisionPreview(providerVisibleTools, input.providerTurnBudgeted ? input.noActionTimeoutMs() : undefined);
    const invokeCurrentDecision = async () => {
        await input.assertTurnAttemptWritable();
        return input.invokeDecide(preparedRequest, {
            decisionMessages,
            decisionPreview,
            decisionSystemPrompt,
            requiredToolConstraint,
            visibleTools: providerVisibleTools,
        });
    };
    try {
        decision = await invokeCurrentDecision();
    }
    catch (error) {
        if (input.canRetryContextOverflow(error, preparedRequest.snapshot)) {
            await decisionPreview.finalize();
            preparedRequest = await input.buildDecideMemoryRequest({ forceCompaction: true });
            requiredToolConstraint = input.resolveRequiredToolConstraint(preparedRequest.request.messages, input.availableTools);
            providerVisibleTools = input.resolveVisibleTools(input.availableTools, requiredToolConstraint, preparedRequest.request.messages);
            decisionMessages = preparedRequest.request.messages;
            decisionSystemPrompt = preparedRequest.request.systemPrompt;
            decisionPreview = input.createDecisionPreview(providerVisibleTools, input.providerTurnBudgeted ? input.noActionTimeoutMs() : undefined);
            decision = await invokeCurrentDecision();
        }
        else {
            throw error;
        }
    }
    await decisionPreview.finalize();
    const responseText = await input.resolveDecision({
        decision,
        decisionMessages,
        decisionPreview,
        decisionSystemPrompt,
        preparedRequest,
        requiredToolConstraint,
        visibleTools: providerVisibleTools,
    });
    return { completionGuardRepaired: false, responseText, terminalOutputRepaired: false };
}
//# sourceMappingURL=provider-turn-runner.js.map