| 1 | import { type AssistantConfig } from "@continuedev/sdk"; |
| 2 | import { Box, Text } from "ink"; |
| 3 | import React, { useMemo } from "react"; |
| 4 | |
| 5 | import { getAllSlashCommands } from "../commands/commands.js"; |
| 6 | |
| 7 | interface SlashCommandUIProps { |
| 8 | assistant?: AssistantConfig; |
| 9 | filter: string; |
| 10 | selectedIndex: number; |
| 11 | isRemoteMode?: boolean; |
| 12 | } |
| 13 | |
| 14 | const SlashCommandUI: React.FC<SlashCommandUIProps> = ({ |
| 15 | assistant, |
| 16 | filter, |
| 17 | selectedIndex, |
| 18 | isRemoteMode = false, |
| 19 | }) => { |
| 20 | // Memoize the slash commands to prevent excessive re-renders |
| 21 | const allCommands = useMemo(() => { |
| 22 | if (assistant || isRemoteMode) { |
| 23 | return getAllSlashCommands(assistant || ({} as AssistantConfig), { |
| 24 | isRemoteMode, |
| 25 | }); |
| 26 | } |
| 27 | |
| 28 | // Fallback - basic commands without assistant |
| 29 | return [ |
| 30 | { name: "help", description: "Show help message" }, |
| 31 | { name: "clear", description: "Clear the chat history" }, |
| 32 | { name: "exit", description: "Exit the chat" }, |
| 33 | ]; |
| 34 | }, [isRemoteMode, assistant?.prompts]); |
| 35 | |
| 36 | // Filter commands based on the current filter |
| 37 | const filteredCommands = allCommands |
| 38 | .filter((cmd) => cmd.name.toLowerCase().includes(filter.toLowerCase())) |
| 39 | .sort((a, b) => { |
| 40 | const aStartsWith = a.name.toLowerCase().startsWith(filter.toLowerCase()); |
| 41 | const bStartsWith = b.name.toLowerCase().startsWith(filter.toLowerCase()); |
| 42 | |
| 43 | if (aStartsWith && !bStartsWith) return -1; |
| 44 | if (!aStartsWith && bStartsWith) return 1; |
| 45 | |
| 46 | return a.name.localeCompare(b.name); |
| 47 | }); |
| 48 | |
| 49 | if (filteredCommands.length === 0) { |
| 50 | return ( |
| 51 | <Box paddingX={1} marginX={1} marginBottom={1}> |
| 52 | <Text color="gray">No matching commands found</Text> |
| 53 | </Box> |
| 54 | ); |
| 55 | } |
| 56 | |
| 57 | return ( |
| 58 | <Box paddingX={1} marginX={1} marginBottom={1} flexDirection="column"> |
| 59 | {filteredCommands.map((command, index) => { |
| 60 | const isSelected = index === selectedIndex; |
| 61 | |
| 62 | // Find the longest command name to vertically align command descriptions |
| 63 | const maxCommandLength = Math.max( |
| 64 | ...filteredCommands.map((cmd) => cmd.name.length), |
| 65 | ); |
| 66 | const paddedCommandName = `/${command.name}`.padEnd( |
| 67 | maxCommandLength + 1, |
| 68 | ); |
| 69 | |
| 70 | return ( |
| 71 | <Box key={command.name}> |
| 72 | <Text color={isSelected ? "blue" : "white"} bold={isSelected}> |
| 73 | {" "} |
| 74 | {paddedCommandName} |
| 75 | <Text color={isSelected ? "blue" : "gray"}> |
| 76 | {" "} |
| 77 | {command.description} |
| 78 | </Text> |
| 79 | </Text> |
| 80 | </Box> |
| 81 | ); |
| 82 | })} |
| 83 | |
| 84 | <Box marginTop={1}> |
| 85 | <Text color="gray" dimColor> |
| 86 | ↑/↓ to navigate, Enter to select, Tab to complete |
| 87 | </Text> |
| 88 | </Box> |
| 89 | </Box> |
| 90 | ); |
| 91 | }; |
| 92 | |
| 93 | export { SlashCommandUI }; |