diff --git a/src/ui/LoadingAnimation.tsx b/src/ui/LoadingAnimation.tsx new file mode 100644 index 000000000..e0747d12c --- /dev/null +++ b/src/ui/LoadingAnimation.tsx @@ -0,0 +1,31 @@ +import { Text } from "ink"; +import React, { useEffect, useState } from "react"; + +interface LoadingAnimationProps { + visible?: boolean; + color?: string; +} + +const LoadingAnimation: React.FC = ({ + visible = true, + color = "gray", +}) => { + const [currentIndex, setCurrentIndex] = useState(0); + const animationChars = ["-", "/", "|", "\\", "-", "/"]; + + useEffect(() => { + if (!visible) return; + + const interval = setInterval(() => { + setCurrentIndex((prev) => (prev + 1) % animationChars.length); + }, 150); + + return () => clearInterval(interval); + }, [visible, animationChars.length]); + + if (!visible) return null; + + return {animationChars[currentIndex]}; +}; + +export default LoadingAnimation; diff --git a/src/ui/SlashCommandUI.tsx b/src/ui/SlashCommandUI.tsx new file mode 100644 index 000000000..a18914ac1 --- /dev/null +++ b/src/ui/SlashCommandUI.tsx @@ -0,0 +1,110 @@ +import { Box, Text } from "ink"; +import React from "react"; +import { type AssistantConfig } from "@continuedev/sdk"; + +interface SlashCommandUIProps { + assistant: AssistantConfig; + filter: string; + selectedIndex: number; + onSelect: (command: string) => void; +} + +interface SlashCommand { + name: string; + description: string; + category: "system" | "assistant"; +} + +const SlashCommandUI: React.FC = ({ + assistant, + filter, + selectedIndex, + onSelect, +}) => { + // Get all available slash commands + const getSlashCommands = (): SlashCommand[] => { + const systemCommands: SlashCommand[] = [ + { name: "help", description: "Show help message", category: "system" }, + { name: "exit", description: "Exit the chat", category: "system" }, + { name: "login", description: "Authenticate with your account", category: "system" }, + { name: "logout", description: "Sign out of your current session", category: "system" }, + { name: "whoami", description: "Check who you're currently logged in as", category: "system" }, + { name: "models", description: "List available AI models", category: "system" }, + ]; + + const assistantCommands: SlashCommand[] = assistant.prompts?.map( + (prompt) => ({ + name: prompt?.name || "", + description: prompt?.description || "", + category: "assistant" as const, + }) + ) || []; + + return [...systemCommands, ...assistantCommands]; + }; + + const allCommands = getSlashCommands(); + + // Filter commands based on the current filter + const filteredCommands = allCommands.filter( + (cmd) => cmd.name.toLowerCase().includes(filter.toLowerCase()) + ); + + if (filteredCommands.length === 0) { + return ( + + No matching commands found + + ); + } + + return ( + + + + Slash Commands + + + + {filteredCommands.map((command, index) => { + const isSelected = index === selectedIndex; + const categoryColor = command.category === "system" ? "cyan" : "yellow"; + + return ( + + + {isSelected ? "▶ " : " "} + /{command.name} + - {command.description} + + + ); + })} + + + + Use ↑/↓ to navigate, Enter to select, Tab to complete + + + + ); +}; + +export default SlashCommandUI; \ No newline at end of file diff --git a/src/ui/TUIChat.tsx b/src/ui/TUIChat.tsx index bcc94f57d..60d579dd2 100644 --- a/src/ui/TUIChat.tsx +++ b/src/ui/TUIChat.tsx @@ -5,6 +5,7 @@ import React, { useEffect, useState } from "react"; import { handleSlashCommands } from "../slashCommands.js"; import { StreamCallbacks, streamChatResponse } from "../streamChatResponse.js"; import { getToolDisplayName } from "../tools.js"; +import LoadingAnimation from "./LoadingAnimation.js"; import ToolResultSummary from "./ToolResultSummary.js"; import UserInput from "./UserInput.js"; @@ -294,7 +295,8 @@ const TUIChat: React.FC = ({ {/* Status */} {isWaitingForResponse && ( - + + esc to interrupt )} @@ -305,6 +307,7 @@ const TUIChat: React.FC = ({ isWaitingForResponse={isWaitingForResponse} inputMode={inputMode} onInterrupt={handleInterrupt} + assistant={assistant.config} /> ● Continue CLI @@ -314,4 +317,4 @@ const TUIChat: React.FC = ({ ); }; -export default TUIChat; +export default TUIChat; \ No newline at end of file diff --git a/src/ui/UserInput.tsx b/src/ui/UserInput.tsx index 075a02bc3..8601f5a0e 100644 --- a/src/ui/UserInput.tsx +++ b/src/ui/UserInput.tsx @@ -1,5 +1,7 @@ +import { type AssistantConfig } from "@continuedev/sdk"; import { Box, Text, useApp, useInput } from "ink"; import React, { useState } from "react"; +import SlashCommandUI from "./SlashCommandUI.js"; import { TextBuffer } from "./TextBuffer.js"; interface UserInputProps { @@ -7,6 +9,7 @@ interface UserInputProps { isWaitingForResponse: boolean; inputMode: boolean; onInterrupt?: () => void; + assistant: AssistantConfig; } const UserInput: React.FC = ({ @@ -14,12 +17,91 @@ const UserInput: React.FC = ({ isWaitingForResponse, inputMode, onInterrupt, + assistant, }) => { const [textBuffer] = useState(() => new TextBuffer()); const [inputText, setInputText] = useState(""); const [cursorPosition, setCursorPosition] = useState(0); + const [showSlashCommands, setShowSlashCommands] = useState(false); + const [slashCommandFilter, setSlashCommandFilter] = useState(""); + const [selectedCommandIndex, setSelectedCommandIndex] = useState(0); const { exit } = useApp(); + // Get all available slash commands + const getSlashCommands = () => { + const systemCommands = [ + { name: "help", description: "Show help message" }, + { name: "exit", description: "Exit the chat" }, + { name: "login", description: "Authenticate with your account" }, + { name: "logout", description: "Sign out of your current session" }, + { + name: "whoami", + description: "Check who you're currently logged in as", + }, + { name: "models", description: "List available AI models" }, + ]; + + const assistantCommands = + assistant.prompts?.map((prompt) => ({ + name: prompt?.name || "", + description: prompt?.description || "", + })) || []; + + return [...systemCommands, ...assistantCommands]; + }; + + // Update slash command UI state based on input + const updateSlashCommandState = (text: string, cursor: number) => { + // Check if we're in a slash command context + const beforeCursor = text.slice(0, cursor); + const lastSlashIndex = beforeCursor.lastIndexOf("/"); + + if (lastSlashIndex !== -1) { + // Check if there's any whitespace between the last slash and cursor + const afterSlash = beforeCursor.slice(lastSlashIndex + 1); + + if (!afterSlash.includes(" ") && !afterSlash.includes("\n")) { + // We're in a slash command context + setShowSlashCommands(true); + setSlashCommandFilter(afterSlash); + setSelectedCommandIndex(0); + } else { + setShowSlashCommands(false); + } + } else { + setShowSlashCommands(false); + } + }; + + // Get filtered commands for navigation + const getFilteredCommands = () => { + const allCommands = getSlashCommands(); + return allCommands.filter((cmd) => + cmd.name.toLowerCase().includes(slashCommandFilter.toLowerCase()) + ); + }; + + // Handle slash command selection + const selectSlashCommand = (commandName: string) => { + const beforeCursor = inputText.slice(0, cursorPosition); + const lastSlashIndex = beforeCursor.lastIndexOf("/"); + + if (lastSlashIndex !== -1) { + const beforeSlash = inputText.slice(0, lastSlashIndex); + const afterCursor = inputText.slice(cursorPosition); + + // Replace the partial command with the full command + const newText = beforeSlash + "/" + commandName + " " + afterCursor; + const newCursorPos = lastSlashIndex + 1 + commandName.length + 1; + + textBuffer.setText(newText); + textBuffer.setCursor(newCursorPos); + setInputText(newText); + setCursorPosition(newCursorPos); + setShowSlashCommands(false); + } + }; + useInput((input, key) => { if (key.ctrl && (input === "c" || input === "d")) { exit(); @@ -32,10 +114,49 @@ const UserInput: React.FC = ({ return; } + // Handle escape key to close slash command UI + if (key.escape && showSlashCommands && inputMode) { + setShowSlashCommands(false); + return; + } + if (!inputMode) { return; } + // Handle slash command navigation + if (showSlashCommands) { + const filteredCommands = getFilteredCommands(); + + if (key.upArrow) { + setSelectedCommandIndex((prev) => + prev > 0 ? prev - 1 : filteredCommands.length - 1 + ); + return; + } + + if (key.downArrow) { + setSelectedCommandIndex((prev) => + prev < filteredCommands.length - 1 ? prev + 1 : 0 + ); + return; + } + + if (key.return && !key.shift) { + if (filteredCommands.length > 0) { + selectSlashCommand(filteredCommands[selectedCommandIndex].name); + } + return; + } + + if (key.tab) { + if (filteredCommands.length > 0) { + selectSlashCommand(filteredCommands[selectedCommandIndex].name); + } + return; + } + } + // Handle Enter key (regular enter for submit) if (key.return && !key.shift) { if (textBuffer.text.trim() && !isWaitingForResponse) { @@ -43,6 +164,7 @@ const UserInput: React.FC = ({ textBuffer.clear(); setInputText(""); setCursorPosition(0); + setShowSlashCommands(false); } return; } @@ -51,8 +173,11 @@ const UserInput: React.FC = ({ if (input === "\r" || input === "\\\r") { const handled = textBuffer.handleInput("\n", key); if (handled) { - setInputText(textBuffer.text); - setCursorPosition(textBuffer.cursor); + const newText = textBuffer.text; + const newCursor = textBuffer.cursor; + setInputText(newText); + setCursorPosition(newCursor); + updateSlashCommandState(newText, newCursor); } return; } @@ -62,8 +187,11 @@ const UserInput: React.FC = ({ // Update React state to trigger re-render if (handled) { - setInputText(textBuffer.text); - setCursorPosition(textBuffer.cursor); + const newText = textBuffer.text; + const newCursor = textBuffer.cursor; + setInputText(newText); + setCursorPosition(newCursor); + updateSlashCommandState(newText, newCursor); } }); @@ -141,11 +269,24 @@ const UserInput: React.FC = ({ }; return ( - - - ●{" "} - - {renderInputText()} + + {/* Input box */} + + + ●{" "} + + {renderInputText()} + + + {/* Slash command UI */} + {showSlashCommands && inputMode && ( + + )} ); };