/**
 * Chat Mode Extension for Clawx TUI.
 *
 * Provides /chat command to toggle chat-only mode (no tools).
 * Auto-detects when a model doesn't support tools and switches.
 * Swaps the system prompt between agent mode and chat mode.
 *
 * When switching back to agent mode (via /chat toggle or model change),
 * restores tools and the full agent system prompt.
 */
export function createChatModeExtension(options) {
    return function chatModeExtension(pi) {
        let chatMode = options.startInChatMode ?? false;
        let sshEnabled = options.sshEnabled ?? false;
        let savedToolNames = [];
        const SSH_TOOL = "ssh_run";
        function enableChatMode(ctx) {
            if (chatMode)
                return;
            chatMode = true;
            // Save current tools before disabling
            savedToolNames = pi.getAllTools().map((t) => t.name);
            pi.setActiveTools([]);
            ctx.ui.notify("Chat mode enabled — tools disabled", "info");
            options.onChatModeChange?.(true);
        }
        function disableChatMode(ctx) {
            if (!chatMode)
                return;
            chatMode = false;
            // Restore saved tools (or all if none saved)
            const allTools = pi.getAllTools().map((t) => t.name);
            const restore = savedToolNames.length > 0 ? savedToolNames : allTools;
            pi.setActiveTools(restore);
            ctx.ui.notify("Agent mode enabled — tools restored", "info");
            options.onChatModeChange?.(false);
        }
        // Apply initial state on session start
        pi.on("session_start", async (_event, ctx) => {
            if (chatMode) {
                savedToolNames = pi.getAllTools().map((t) => t.name);
                pi.setActiveTools([]);
                ctx.ui.setStatus("mode", "chat mode");
            }
            else {
                // Deactivate SSH tools unless --ssh was passed
                if (!sshEnabled) {
                    const activeTools = pi.getAllTools().map((t) => t.name).filter((n) => n !== SSH_TOOL);
                    pi.setActiveTools(activeTools);
                }
                ctx.ui.setStatus("mode", "agent mode");
            }
        });
        // Register /chat command to toggle
        pi.registerCommand("chat", {
            description: "Toggle chat mode (no tools, just conversation)",
            handler: async (_args, ctx) => {
                if (chatMode) {
                    disableChatMode(ctx);
                    ctx.ui.setStatus("mode", "agent mode");
                }
                else {
                    enableChatMode(ctx);
                    ctx.ui.setStatus("mode", "chat mode");
                }
            },
        });
        // Register /ssh command to toggle SSH tools
        pi.registerCommand("ssh", {
            description: "Toggle SSH tools (remote command execution)",
            handler: async (_args, ctx) => {
                if (chatMode) {
                    ctx.ui.notify("Cannot enable SSH in chat mode — switch to agent mode first with /chat", "warning");
                    return;
                }
                sshEnabled = !sshEnabled;
                const currentTools = pi.getAllTools().map((t) => t.name);
                if (sshEnabled) {
                    // Activate all tools including SSH
                    pi.setActiveTools(currentTools);
                    ctx.ui.notify("SSH tools enabled — model can now run commands on remote systems", "warning");
                }
                else {
                    // Deactivate SSH tool
                    pi.setActiveTools(currentTools.filter((n) => n !== SSH_TOOL));
                    ctx.ui.notify("SSH tools disabled", "info");
                }
            },
        });
        // Swap system prompt before each agent turn
        pi.on("before_agent_start", async (event) => {
            if (chatMode) {
                return {
                    systemPrompt: options.chatSystemPrompt,
                };
            }
            // In agent mode, ensure the agent prompt is used
            return {
                systemPrompt: options.agentSystemPrompt,
            };
        });
        // Detect tools error and auto-switch to chat mode
        pi.on("turn_end", async (event, ctx) => {
            if (chatMode)
                return;
            const msg = event.message;
            if (msg.role === "assistant" &&
                "stopReason" in msg &&
                msg.stopReason === "error") {
                const errorMsg = ("errorMessage" in msg ? msg.errorMessage : "") || "";
                if (errorMsg.includes("does not support tools") ||
                    errorMsg.includes("does not support tool")) {
                    enableChatMode(ctx);
                    ctx.ui.setStatus("mode", "chat mode");
                    ctx.ui.notify("Model doesn't support tools — switched to chat mode. Use /chat to toggle back.", "warning");
                }
            }
        });
        // When model changes while in chat mode, stay in chat mode.
        // User can /chat to restore agent mode if the new model supports tools.
        // If in agent mode and new model doesn't support tools, turn_end
        // will auto-switch to chat mode after the first error.
    };
}
//# sourceMappingURL=chat-mode.js.map