import { combineQueuedMessagesForEditor } from "../chat-input-actions.js";
import { setChatSessionEditorText } from "./chat-session-host-editor.js";
import { decrementOptimisticUserSignature, incrementOptimisticUserSignature, isChatSessionBashRunning, isChatSessionStreaming, notifyChatSessionStatus, notifyChatSessionWarning, requiredChatSessionCommand, syncChatSessionAnimationTick, } from "./chat-session-host-runtime.js";
import { errorMessage, parseBashInput, userMessageSignature, } from "./chat-session-host-utils.js";
export async function interruptChatSession(state) {
    try {
        restoreQueuedMessagesToEditor(state);
        state.sdkBusy = false;
        state.workingMessage = undefined;
        await state.commands.interrupt?.();
    }
    catch (err) {
        state.statusMessage = errorMessage(err);
    }
    finally {
        syncChatSessionAnimationTick(state);
        state.requestRender?.();
    }
}
export async function submitChatSession(state, mode = "auto", submittedText) {
    const text = (submittedText ?? state.inputBuffer).trim();
    if (!text)
        return;
    if (text.startsWith("/") && state.commands.handleSlashCommand) {
        const handled = await state.commands.handleSlashCommand(text);
        if (handled) {
            setChatSessionEditorText(state, "");
            state.requestRender?.();
            return;
        }
    }
    if (state.compacting) {
        setChatSessionEditorText(state, "");
        state.compactionQueuedMessages = [...state.compactionQueuedMessages, text];
        notifyChatSessionStatus(state, "Queued message until compaction completes");
        return;
    }
    const bash = parseBashInput(text);
    if (bash?.command) {
        if (isChatSessionBashRunning(state)) {
            notifyChatSessionWarning(state, "A bash command is already running. esc cancel first.");
            setChatSessionEditorText(state, text);
            return;
        }
        setChatSessionEditorText(state, "");
        await runChatSessionBashCommand(state, bash.command, bash.excludeFromContext);
        return;
    }
    setChatSessionEditorText(state, "");
    const isPaused = state.isPaused?.() === true;
    const isStreaming = isChatSessionStreaming(state);
    const shouldAppendOptimisticUser = mode === "auto" && !isStreaming;
    const optimisticSignature = shouldAppendOptimisticUser
        ? userMessageSignature(text)
        : undefined;
    if (optimisticSignature !== undefined) {
        state.liveChat.appendUserText(text);
        state.bodyViewport.scrollToBottom();
        incrementOptimisticUserSignature(state, optimisticSignature);
    }
    state.requestRender?.();
    try {
        if (isPaused) {
            state.sdkBusy = true;
            state.statusMessage = "resuming…";
            syncChatSessionAnimationTick(state);
            state.requestRender?.();
            await requiredChatSessionCommand(state, "resume")(text);
            state.sdkBusy = false;
            state.statusMessage = "";
            syncChatSessionAnimationTick(state);
            return;
        }
        if (mode === "followUp" && isStreaming) {
            await queueChatSessionFollowUp(state, text);
            return;
        }
        if (isStreaming) {
            await queueChatSessionSteer(state, text);
        }
        else {
            state.sdkBusy = true;
            syncChatSessionAnimationTick(state);
            await state.commands.ensureAttached?.();
            await requiredChatSessionCommand(state, "prompt")(text);
            state.sdkBusy = false;
            syncChatSessionAnimationTick(state);
        }
    }
    catch (err) {
        if (optimisticSignature !== undefined) {
            decrementOptimisticUserSignature(state, optimisticSignature);
        }
        state.sdkBusy = false;
        state.statusMessage = errorMessage(err);
        syncChatSessionAnimationTick(state);
        state.requestRender?.();
    }
}
export async function abortChatSessionCompaction(state) {
    try {
        await state.commands.abortCompaction?.();
        state.compacting = false;
        state.sdkBusy = false;
        notifyChatSessionStatus(state, "Compaction cancelled");
    }
    catch (err) {
        notifyChatSessionWarning(state, errorMessage(err));
    }
    finally {
        syncChatSessionAnimationTick(state);
        state.requestRender?.();
    }
}
export async function abortChatSessionBash(state) {
    try {
        await state.commands.abortBash?.();
        state.localBashRunning = false;
        notifyChatSessionStatus(state, "Bash command cancelled");
    }
    catch (err) {
        notifyChatSessionWarning(state, errorMessage(err));
    }
    finally {
        state.requestRender?.();
    }
}
export async function flushChatSessionCompactionQueue(state) {
    const queued = [...state.compactionQueuedMessages];
    state.compactionQueuedMessages = [];
    if (queued.length === 0)
        return;
    let nextIndex = 0;
    try {
        const first = queued[0];
        if (first !== undefined) {
            await requiredChatSessionCommand(state, "prompt")(first);
            nextIndex = 1;
        }
        for (; nextIndex < queued.length; nextIndex++) {
            await queueChatSessionFollowUp(state, queued[nextIndex]);
        }
    }
    catch (err) {
        state.compactionQueuedMessages = [
            ...queued.slice(nextIndex),
            ...state.compactionQueuedMessages,
        ];
        notifyChatSessionWarning(state, errorMessage(err));
        state.requestRender?.();
    }
}
export function restoreQueuedMessagesToEditor(state) {
    const queuedMessages = [
        ...state.pendingSteeringMessages,
        ...state.pendingFollowUpMessages,
        ...state.compactionQueuedMessages,
    ];
    if (queuedMessages.length === 0) {
        notifyChatSessionStatus(state, "No queued messages to restore");
        return false;
    }
    const restoredText = combineQueuedMessagesForEditor(queuedMessages, state.inputBuffer);
    state.pendingSteeringMessages = [];
    state.pendingFollowUpMessages = [];
    state.compactionQueuedMessages = [];
    setChatSessionEditorText(state, restoredText);
    state.getAgentSession?.()?.clearQueue();
    state.requestRender?.();
    return true;
}
async function runChatSessionBashCommand(state, command, excludeFromContext) {
    const runBash = state.commands.runBash;
    if (!runBash) {
        notifyChatSessionWarning(state, "no bash command configured for this chat session");
        return;
    }
    const bashMessage = {
        role: "bashExecution",
        command,
        output: "",
        exitCode: undefined,
        cancelled: false,
        truncated: false,
        timestamp: Date.now(),
        ...(excludeFromContext ? { excludeFromContext: true } : {}),
    };
    const bashEntry = {
        role: "tool",
        kind: "bashExecution",
        message: bashMessage,
        isPartial: true,
    };
    state.transcript.push(bashEntry);
    state.localBashRunning = true;
    state.bodyViewport.scrollToBottom();
    state.requestRender?.();
    try {
        const result = await runBash({
            command,
            excludeFromContext,
            onChunk: (chunk) => {
                bashMessage.output += chunk;
                state.requestRender?.();
            },
        });
        bashMessage.output = result.output;
        bashMessage.exitCode = result.exitCode;
        bashMessage.cancelled = result.cancelled;
        bashMessage.truncated = result.truncated;
        if (result.fullOutputPath !== undefined) {
            bashMessage.fullOutputPath = result.fullOutputPath;
        }
    }
    catch (err) {
        bashMessage.output = errorMessage(err);
        bashMessage.exitCode = undefined;
        bashMessage.cancelled = false;
        bashMessage.truncated = false;
    }
    finally {
        bashEntry.isPartial = false;
        state.localBashRunning = false;
        state.requestRender?.();
    }
}
async function queueChatSessionSteer(state, text) {
    const agentSession = state.getAgentSession?.();
    if (agentSession?.isStreaming) {
        await agentSession.prompt(text, { streamingBehavior: "steer" });
        return;
    }
    await requiredChatSessionCommand(state, "steer")(text);
}
async function queueChatSessionFollowUp(state, text) {
    const agentSession = state.getAgentSession?.();
    if (agentSession?.isStreaming) {
        await agentSession.prompt(text, { streamingBehavior: "followUp" });
        return;
    }
    await requiredChatSessionCommand(state, "followUp")(text);
}
//# sourceMappingURL=chat-session-host-actions.js.map