// Import useInput from vendored Ink for bracketed paste support import { Box, Text, useInput } from "ink"; import SpinnerLib from "ink-spinner"; import type { ComponentType } from "react"; import { useEffect, useRef, useState } from "react"; import type { PermissionMode } from "../../permissions/mode"; import { permissionMode } from "../../permissions/mode"; import { useTerminalWidth } from "../hooks/useTerminalWidth"; import { colors } from "./colors"; import { InputAssist } from "./InputAssist"; import { PasteAwareTextInput } from "./PasteAwareTextInput"; import { ShimmerText } from "./ShimmerText"; // Type assertion for ink-spinner compatibility const Spinner = SpinnerLib as ComponentType<{ type?: string }>; // Only show token count when it exceeds this threshold const COUNTER_VISIBLE_THRESHOLD = 1000; export function Input({ visible = true, streaming, commandRunning = false, tokenCount, thinkingMessage, onSubmit, permissionMode: externalMode, onPermissionModeChange, onExit, onInterrupt, interruptRequested = false, }: { visible?: boolean; streaming: boolean; commandRunning?: boolean; tokenCount: number; thinkingMessage: string; onSubmit: (message?: string) => Promise<{ submitted: boolean }>; permissionMode?: PermissionMode; onPermissionModeChange?: (mode: PermissionMode) => void; onExit?: () => void; onInterrupt?: () => void; interruptRequested?: boolean; }) { const [value, setValue] = useState(""); const [escapePressed, setEscapePressed] = useState(false); const escapeTimerRef = useRef | null>(null); const [ctrlCPressed, setCtrlCPressed] = useState(false); const ctrlCTimerRef = useRef | null>(null); const previousValueRef = useRef(value); const [currentMode, setCurrentMode] = useState( externalMode || permissionMode.getMode(), ); const [isAutocompleteActive, setIsAutocompleteActive] = useState(false); const [cursorPos, setCursorPos] = useState(undefined); const [currentCursorPosition, setCurrentCursorPosition] = useState(0); // Sync with external mode changes (from plan approval dialog) useEffect(() => { if (externalMode !== undefined) { setCurrentMode(externalMode); } }, [externalMode]); // Shimmer animation state const [shimmerOffset, setShimmerOffset] = useState(-3); // Terminal width (reactive to window resizing) const columns = useTerminalWidth(); const contentWidth = Math.max(0, columns - 2); // Handle escape key for interrupt (when streaming) or double-escape-to-clear (when not) useInput((_input, key) => { if (key.escape) { // When streaming, use Esc to interrupt if (streaming && onInterrupt && !interruptRequested) { onInterrupt(); return; } // When input is non-empty, use double-escape to clear if (value) { if (escapePressed) { // Second escape - clear input setValue(""); setEscapePressed(false); if (escapeTimerRef.current) clearTimeout(escapeTimerRef.current); } else { // First escape - start 1-second timer setEscapePressed(true); if (escapeTimerRef.current) clearTimeout(escapeTimerRef.current); escapeTimerRef.current = setTimeout(() => { setEscapePressed(false); }, 1000); } } } }); // Handle CTRL-C for double-ctrl-c-to-exit useInput((input, key) => { if (input === "c" && key.ctrl) { if (ctrlCPressed) { // Second CTRL-C - call onExit callback which handles stats and exit if (onExit) onExit(); } else { // First CTRL-C - wipe input and start 1-second timer setValue(""); setCtrlCPressed(true); if (ctrlCTimerRef.current) clearTimeout(ctrlCTimerRef.current); ctrlCTimerRef.current = setTimeout(() => { setCtrlCPressed(false); }, 1000); } } }); // Handle Shift+Tab for permission mode cycling useInput((_input, key) => { if (key.shift && key.tab) { // Cycle through permission modes const modes: PermissionMode[] = [ "default", "acceptEdits", "plan", "bypassPermissions", ]; const currentIndex = modes.indexOf(currentMode); const nextIndex = (currentIndex + 1) % modes.length; const nextMode = modes[nextIndex] ?? "default"; // Update both singleton and local state permissionMode.setMode(nextMode); setCurrentMode(nextMode); // Notify parent of mode change if (onPermissionModeChange) { onPermissionModeChange(nextMode); } } }); // Reset escape and ctrl-c state when user types (value changes) useEffect(() => { if (value !== previousValueRef.current && value !== "") { setEscapePressed(false); if (escapeTimerRef.current) clearTimeout(escapeTimerRef.current); setCtrlCPressed(false); if (ctrlCTimerRef.current) clearTimeout(ctrlCTimerRef.current); } previousValueRef.current = value; }, [value]); // Clean up timers on unmount useEffect(() => { return () => { if (escapeTimerRef.current) clearTimeout(escapeTimerRef.current); if (ctrlCTimerRef.current) clearTimeout(ctrlCTimerRef.current); }; }, []); // Shimmer animation effect useEffect(() => { if (!streaming || !visible) return; const id = setInterval(() => { setShimmerOffset((prev) => { const len = thinkingMessage.length; const next = prev + 1; return next > len + 3 ? -3 : next; }); }, 120); // Speed of shimmer animation return () => clearInterval(id); }, [streaming, thinkingMessage, visible]); const handleSubmit = async () => { // Don't submit if autocomplete is active with matches if (isAutocompleteActive) { return; } if (streaming || commandRunning) { return; } const previousValue = value; setValue(""); // Clear immediately for responsiveness const result = await onSubmit(previousValue); // If message was NOT submitted (e.g. pending approval), restore it if (!result.submitted) { setValue(previousValue); } }; // Handle file selection from autocomplete const handleFileSelect = (selectedPath: string) => { // Find the last "@" and replace everything after it with the selected path const atIndex = value.lastIndexOf("@"); if (atIndex === -1) return; const beforeAt = value.slice(0, atIndex); const afterAt = value.slice(atIndex + 1); const spaceIndex = afterAt.indexOf(" "); let newValue: string; let newCursorPos: number; // Replace the query part with the selected path if (spaceIndex === -1) { // No space after @query, replace to end newValue = `${beforeAt}@${selectedPath} `; newCursorPos = newValue.length; } else { // Space exists, replace only the query part const afterQuery = afterAt.slice(spaceIndex); newValue = `${beforeAt}@${selectedPath}${afterQuery}`; newCursorPos = beforeAt.length + selectedPath.length + 1; // After the path } setValue(newValue); setCursorPos(newCursorPos); // Reset cursor position after a short delay so it only applies once setTimeout(() => setCursorPos(undefined), 50); }; // Get display name and color for permission mode const getModeInfo = () => { switch (currentMode) { case "acceptEdits": return { name: "accept edits", color: colors.status.processing }; case "plan": return { name: "plan (read-only) mode", color: colors.status.success }; case "bypassPermissions": return { name: "yolo (allow all) mode", color: colors.status.error, }; default: return null; } }; const modeInfo = getModeInfo(); const shouldShowTokenCount = streaming && tokenCount > COUNTER_VISIBLE_THRESHOLD; // Create a horizontal line using box-drawing characters const horizontalLine = "─".repeat(columns); // If not visible, render nothing but keep component mounted to preserve state if (!visible) { return null; } return ( {/* Live status / token counter - only show when streaming */} {streaming && ( {" ("} {interruptRequested ? "interrupting" : "esc to interrupt"} {shouldShowTokenCount && ` · ${tokenCount} ↑`} {")"} )} {/* Top horizontal divider */} {horizontalLine} {/* Two-column layout for input, matching message components */} {">"} {/* Bottom horizontal divider */} {horizontalLine} {ctrlCPressed ? ( Press CTRL-C again to exit ) : escapePressed ? ( Press Esc again to clear ) : modeInfo ? ( ⏵⏵ {modeInfo.name} {" "} (shift+tab to cycle) ) : ( Press / for commands or @ for files )} https://discord.gg/letta ); }