feat: slash command UI and ASCII spinner

This commit is contained in:
Nate 2025-07-13 18:45:52 -07:00
parent 863b156e63
commit 7eefce2de3
4 changed files with 296 additions and 11 deletions

View file

@ -0,0 +1,31 @@
import { Text } from "ink";
import React, { useEffect, useState } from "react";
interface LoadingAnimationProps {
visible?: boolean;
color?: string;
}
const LoadingAnimation: React.FC<LoadingAnimationProps> = ({
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 <Text color={color}>{animationChars[currentIndex]}</Text>;
};
export default LoadingAnimation;

110
src/ui/SlashCommandUI.tsx Normal file
View file

@ -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<SlashCommandUIProps> = ({
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 (
<Box
borderStyle="round"
borderColor="gray"
paddingX={1}
marginX={1}
marginBottom={1}
>
<Text color="gray">No matching commands found</Text>
</Box>
);
}
return (
<Box
borderStyle="round"
borderColor="gray"
paddingX={1}
marginX={1}
marginBottom={1}
flexDirection="column"
>
<Box marginBottom={1}>
<Text color="gray" bold>
Slash Commands
</Text>
</Box>
{filteredCommands.map((command, index) => {
const isSelected = index === selectedIndex;
const categoryColor = command.category === "system" ? "cyan" : "yellow";
return (
<Box key={command.name} marginBottom={index === filteredCommands.length - 1 ? 0 : 1}>
<Text
color={isSelected ? "black" : "white"}
backgroundColor={isSelected ? "white" : undefined}
bold={isSelected}
>
{isSelected ? "▶ " : " "}
<Text color={categoryColor}>/{command.name}</Text>
<Text color="gray"> - {command.description}</Text>
</Text>
</Box>
);
})}
<Box marginTop={1}>
<Text color="gray" dimColor>
Use ↑/↓ to navigate, Enter to select, Tab to complete
</Text>
</Box>
</Box>
);
};
export default SlashCommandUI;

View file

@ -5,6 +5,7 @@ import React, { useEffect, useState } from "react";
import { handleSlashCommands } from "../slashCommands.js"; import { handleSlashCommands } from "../slashCommands.js";
import { StreamCallbacks, streamChatResponse } from "../streamChatResponse.js"; import { StreamCallbacks, streamChatResponse } from "../streamChatResponse.js";
import { getToolDisplayName } from "../tools.js"; import { getToolDisplayName } from "../tools.js";
import LoadingAnimation from "./LoadingAnimation.js";
import ToolResultSummary from "./ToolResultSummary.js"; import ToolResultSummary from "./ToolResultSummary.js";
import UserInput from "./UserInput.js"; import UserInput from "./UserInput.js";
@ -294,7 +295,8 @@ const TUIChat: React.FC<TUIChatProps> = ({
<Box flexDirection="column" flexShrink={0}> <Box flexDirection="column" flexShrink={0}>
{/* Status */} {/* Status */}
{isWaitingForResponse && ( {isWaitingForResponse && (
<Box paddingX={1}> <Box paddingX={1} flexDirection="row" gap={1}>
<LoadingAnimation visible={isWaitingForResponse} />
<Text color="gray">esc to interrupt</Text> <Text color="gray">esc to interrupt</Text>
</Box> </Box>
)} )}
@ -305,6 +307,7 @@ const TUIChat: React.FC<TUIChatProps> = ({
isWaitingForResponse={isWaitingForResponse} isWaitingForResponse={isWaitingForResponse}
inputMode={inputMode} inputMode={inputMode}
onInterrupt={handleInterrupt} onInterrupt={handleInterrupt}
assistant={assistant.config}
/> />
<Box marginRight={2} justifyContent="flex-end"> <Box marginRight={2} justifyContent="flex-end">
<Text color="gray">● Continue CLI</Text> <Text color="gray">● Continue CLI</Text>

View file

@ -1,5 +1,7 @@
import { type AssistantConfig } from "@continuedev/sdk";
import { Box, Text, useApp, useInput } from "ink"; import { Box, Text, useApp, useInput } from "ink";
import React, { useState } from "react"; import React, { useState } from "react";
import SlashCommandUI from "./SlashCommandUI.js";
import { TextBuffer } from "./TextBuffer.js"; import { TextBuffer } from "./TextBuffer.js";
interface UserInputProps { interface UserInputProps {
@ -7,6 +9,7 @@ interface UserInputProps {
isWaitingForResponse: boolean; isWaitingForResponse: boolean;
inputMode: boolean; inputMode: boolean;
onInterrupt?: () => void; onInterrupt?: () => void;
assistant: AssistantConfig;
} }
const UserInput: React.FC<UserInputProps> = ({ const UserInput: React.FC<UserInputProps> = ({
@ -14,12 +17,91 @@ const UserInput: React.FC<UserInputProps> = ({
isWaitingForResponse, isWaitingForResponse,
inputMode, inputMode,
onInterrupt, onInterrupt,
assistant,
}) => { }) => {
const [textBuffer] = useState(() => new TextBuffer()); const [textBuffer] = useState(() => new TextBuffer());
const [inputText, setInputText] = useState(""); const [inputText, setInputText] = useState("");
const [cursorPosition, setCursorPosition] = useState(0); const [cursorPosition, setCursorPosition] = useState(0);
const [showSlashCommands, setShowSlashCommands] = useState(false);
const [slashCommandFilter, setSlashCommandFilter] = useState("");
const [selectedCommandIndex, setSelectedCommandIndex] = useState(0);
const { exit } = useApp(); 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) => { useInput((input, key) => {
if (key.ctrl && (input === "c" || input === "d")) { if (key.ctrl && (input === "c" || input === "d")) {
exit(); exit();
@ -32,10 +114,49 @@ const UserInput: React.FC<UserInputProps> = ({
return; return;
} }
// Handle escape key to close slash command UI
if (key.escape && showSlashCommands && inputMode) {
setShowSlashCommands(false);
return;
}
if (!inputMode) { if (!inputMode) {
return; 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) // Handle Enter key (regular enter for submit)
if (key.return && !key.shift) { if (key.return && !key.shift) {
if (textBuffer.text.trim() && !isWaitingForResponse) { if (textBuffer.text.trim() && !isWaitingForResponse) {
@ -43,6 +164,7 @@ const UserInput: React.FC<UserInputProps> = ({
textBuffer.clear(); textBuffer.clear();
setInputText(""); setInputText("");
setCursorPosition(0); setCursorPosition(0);
setShowSlashCommands(false);
} }
return; return;
} }
@ -51,8 +173,11 @@ const UserInput: React.FC<UserInputProps> = ({
if (input === "\r" || input === "\\\r") { if (input === "\r" || input === "\\\r") {
const handled = textBuffer.handleInput("\n", key); const handled = textBuffer.handleInput("\n", key);
if (handled) { if (handled) {
setInputText(textBuffer.text); const newText = textBuffer.text;
setCursorPosition(textBuffer.cursor); const newCursor = textBuffer.cursor;
setInputText(newText);
setCursorPosition(newCursor);
updateSlashCommandState(newText, newCursor);
} }
return; return;
} }
@ -62,8 +187,11 @@ const UserInput: React.FC<UserInputProps> = ({
// Update React state to trigger re-render // Update React state to trigger re-render
if (handled) { if (handled) {
setInputText(textBuffer.text); const newText = textBuffer.text;
setCursorPosition(textBuffer.cursor); const newCursor = textBuffer.cursor;
setInputText(newText);
setCursorPosition(newCursor);
updateSlashCommandState(newText, newCursor);
} }
}); });
@ -141,11 +269,24 @@ const UserInput: React.FC<UserInputProps> = ({
}; };
return ( return (
<Box borderStyle="round" borderTop={true} paddingX={1} borderColor="gray"> <Box flexDirection="column">
<Text color="green" bold> {/* Input box */}
●{" "} <Box borderStyle="round" borderTop={true} paddingX={1} borderColor="gray">
</Text> <Text color="green" bold>
{renderInputText()} ●{" "}
</Text>
{renderInputText()}
</Box>
{/* Slash command UI */}
{showSlashCommands && inputMode && (
<SlashCommandUI
assistant={assistant}
filter={slashCommandFilter}
selectedIndex={selectedCommandIndex}
onSelect={selectSlashCommand}
/>
)}
</Box> </Box>
); );
}; };