import { createRandomId } from "@best-agent/foundation";
export async function appendToolReasoningPrelude(input) {
    if (input.options.skipPrelude || !input.reasoning || input.reasoning.trim().length === 0) {
        return;
    }
    await input.runtime.items.pushItems([
        {
            id: createRandomId("item"),
            threadId: input.turn.threadId,
            turnId: input.turn.id,
            createdAt: new Date().toISOString(),
            kind: "reasoning",
            status: "draft",
            summary: `The ${input.profile.name} profile selected tool ${input.toolName}: ${input.reasoning.trim()}`,
        },
    ]);
}
export async function prepareToolInvocationForExecution(input) {
    const { activeSkillIds, approvalTurn, currentPhaseName, options, persistTerminalToolFailure, previewCallId, profile, reasoning, runtime, shellDebugBase, toolInput, toolName, turn, } = input;
    const toolContext = runtime.preparedTools.createToolContext(turn, {
        heartbeat: () => { },
        toolInputMode: options.toolInputMode,
    });
    let prepared;
    try {
        if (shellDebugBase) {
            runtime.tracing.logShellToolDebug(runtime.cwd, "shell.prepare.start", shellDebugBase);
        }
        prepared = await runtime.preparedTools.tools.prepare(toolName, toolInput, toolContext);
        if (shellDebugBase) {
            runtime.tracing.logShellToolDebug(runtime.cwd, "shell.prepare.done", {
                ...shellDebugBase,
                capabilityRequests: prepared.capabilityRequests.map((request) => runtime.tracing.describeCapabilityRequestForDebugLog(request)),
                displayInput: runtime.formatting.previewDebugText(prepared.displayInput, 500),
                normalizedInput: runtime.formatting.previewToolInputForDebugLog(prepared.normalizedInput),
            });
        }
    }
    catch (error) {
        if (shellDebugBase) {
            runtime.tracing.logShellToolDebug(runtime.cwd, "shell.prepare.error", {
                ...shellDebugBase,
                error: runtime.tracing.describeErrorForDebugLog(error),
            });
        }
        const fallbackToolCallId = options.skipPrelude && options.resumeContext?.toolCallId
            ? options.resumeContext.toolCallId
            : previewCallId;
        const failedHistoryInput = runtime.formatting.renderToolInputForProviderHistory(toolName, toolInput);
        const toolCallItem = runtime.toolCalls.createToolCallItem(turn, toolName, runtime.formatting.formatToolInputForPrompt(failedHistoryInput), fallbackToolCallId, runtime.formatting.isContentBearingToolInputForCheckpoint(toolName, toolInput)
            ? failedHistoryInput
            : undefined, options.resumeContext?.providerToolName);
        if (!options.skipPrelude) {
            await runtime.items.pushItems([toolCallItem]);
        }
        const failureResumeContext = options.resumeContext ?? {
            activeSkillIds,
            availableTools: [],
            approvalTurnId: approvalTurn?.id,
            cacheControl: undefined,
            messages: [],
            mode: "tool-plan",
            phaseName: currentPhaseName,
            profileName: profile.name,
            requiredToolConstraint: undefined,
            systemPrompt: undefined,
            toolCallId: toolCallItem.callId,
            toolInput,
            toolName,
            toolStepCount: 0,
            ...(reasoning ? { reasoning } : {}),
        };
        failureResumeContext.toolCallId = toolCallItem.callId;
        failureResumeContext.toolInput = toolInput;
        failureResumeContext.toolName = toolName;
        const failureOutput = await persistTerminalToolFailure(toolCallItem.callId, toolInput, failureResumeContext, error);
        if (failureOutput !== undefined) {
            return { kind: "terminal-failure", output: failureOutput };
        }
        throw error;
    }
    const toolResumeContext = options.resumeContext ?? {
        activeSkillIds,
        availableTools: [],
        approvalTurnId: approvalTurn?.id,
        cacheControl: undefined,
        messages: [],
        mode: "tool-plan",
        phaseName: currentPhaseName,
        profileName: profile.name,
        requiredToolConstraint: undefined,
        systemPrompt: undefined,
        toolCallId: previewCallId ?? createRandomId("item"),
        toolInput: prepared.normalizedInput,
        toolName,
        toolStepCount: 0,
        ...(reasoning ? { reasoning } : {}),
    };
    toolResumeContext.toolInput = prepared.normalizedInput;
    const preparedToolResumeContext = {
        ...toolResumeContext,
        toolInput: prepared.normalizedInput,
    };
    if (options.customLoopReplay) {
        const replayed = runtime.replay.takeCustomLoopToolReplayResult(options.customLoopReplay, toolName, prepared.normalizedInput);
        if (replayed !== undefined) {
            return { kind: "replayed", output: replayed };
        }
    }
    return {
        kind: "prepared",
        prepared,
        preparedToolResumeContext,
        toolResumeContext,
    };
}
//# sourceMappingURL=tool-preparation-runner.js.map