diff --git a/frontend/src/components/command/CommandSuggestions.tsx b/frontend/src/components/command/CommandSuggestions.tsx index eaabd7fd..dbdbbff5 100644 --- a/frontend/src/components/command/CommandSuggestions.tsx +++ b/frontend/src/components/command/CommandSuggestions.tsx @@ -18,26 +18,34 @@ export function CommandSuggestions({ query, commands, onSelect, + onClose, selectedIndex = 0 }: CommandSuggestionsProps) { - const containerRef = useRef(null) + const listRef = useRef(null) const filteredCommands = commands.filter(command => - command.name.toLowerCase().includes(query.toLowerCase()) || - command.description?.toLowerCase().includes(query.toLowerCase()) + command.name.toLowerCase().includes(query.toLowerCase()) ) - - - - - // Scroll selected item into view useEffect(() => { - if (!isOpen || !containerRef.current) return + if (!isOpen) return - const selectedItem = containerRef.current.querySelector(`[data-selected="true"]`) as HTMLElement + const handleClickOutside = (e: MouseEvent) => { + if (listRef.current && !listRef.current.contains(e.target as Node)) { + onClose() + } + } + + document.addEventListener('mousedown', handleClickOutside) + return () => document.removeEventListener('mousedown', handleClickOutside) + }, [isOpen, onClose]) + + useEffect(() => { + if (!isOpen || !listRef.current) return + + const selectedItem = listRef.current.children[selectedIndex] as HTMLElement if (selectedItem) { - selectedItem.scrollIntoView({ block: 'nearest', behavior: 'smooth' }) + selectedItem.scrollIntoView({ block: 'nearest' }) } }, [selectedIndex, isOpen]) @@ -47,45 +55,33 @@ export function CommandSuggestions({ return (
-
- {filteredCommands.map((command, index) => { - const isSelected = index === selectedIndex - const displayName = `/${command.name}` - - return ( -
onSelect(command)} - - > - -
-
{displayName}
- {command.description && ( -
{command.description}
- )} -
+ {filteredCommands.map((command, index) => { + const isSelected = index === selectedIndex + const displayName = `/${command.name}` + + return ( +
- -
-
- ↑↓ Navigate - ↵ Select - Esc Close -
-
+ + ) + })}
) } diff --git a/frontend/src/components/message/PromptInput.tsx b/frontend/src/components/message/PromptInput.tsx index d97f295c..38819eb1 100644 --- a/frontend/src/components/message/PromptInput.tsx +++ b/frontend/src/components/message/PromptInput.tsx @@ -109,12 +109,11 @@ export function PromptInput({ const commandMatch = prompt.match(/^\/([a-zA-Z0-9_-]+)(?:\s+(.*))?$/) if (commandMatch) { - const [, commandName] = commandMatch + const [, commandName, commandArgs] = commandMatch const command = filterCommands(commandName)[0] if (command) { - - executeCommand(command) + executeCommand(command, commandArgs?.trim() || '') setPrompt('') if (textareaRef.current) { textareaRef.current.style.height = 'auto' @@ -149,23 +148,41 @@ export function PromptInput({ setShowSuggestions(false) setSuggestionQuery('') - const cursorPosition = textareaRef.current.selectionStart - const commandMatch = prompt.slice(0, cursorPosition).match(/(^|\s)\/([a-zA-Z0-9_-]*)$/) - - if (commandMatch) { - const beforeCommand = prompt.slice(0, commandMatch.index) - const afterCommand = prompt.slice(cursorPosition) - const newPrompt = beforeCommand + '/' + command.name + ' ' + afterCommand + if (command.template) { + const cleanedTemplate = command.template + .replace(/\$ARGUMENTS/g, '') + .replace(/\$\d+/g, '') + .trim() - setPrompt(newPrompt) + setPrompt(cleanedTemplate) setTimeout(() => { if (textareaRef.current) { - const newCursorPos = beforeCommand.length + command.name.length + 2 textareaRef.current.focus() - textareaRef.current.setSelectionRange(newCursorPos, newCursorPos) + textareaRef.current.setSelectionRange(cleanedTemplate.length, cleanedTemplate.length) + textareaRef.current.style.height = 'auto' + textareaRef.current.style.height = `${textareaRef.current.scrollHeight}px` } }, 0) + } else { + const cursorPosition = textareaRef.current.selectionStart + const commandMatch = prompt.slice(0, cursorPosition).match(/(^|\s)\/([a-zA-Z0-9_-]*)$/) + + if (commandMatch) { + const beforeCommand = prompt.slice(0, commandMatch.index) + const afterCommand = prompt.slice(cursorPosition) + const newPrompt = beforeCommand + '/' + command.name + ' ' + afterCommand + + setPrompt(newPrompt) + + setTimeout(() => { + if (textareaRef.current) { + const newCursorPos = beforeCommand.length + command.name.length + 2 + textareaRef.current.focus() + textareaRef.current.setSelectionRange(newCursorPos, newCursorPos) + } + }, 0) + } } } diff --git a/frontend/src/hooks/useCommandHandler.ts b/frontend/src/hooks/useCommandHandler.ts index 43bc3e35..94e78443 100644 --- a/frontend/src/hooks/useCommandHandler.ts +++ b/frontend/src/hooks/useCommandHandler.ts @@ -27,7 +27,7 @@ export function useCommandHandler({ const createSession = useCreateSession(opcodeUrl, directory) const [loading, setLoading] = useState(false) - const executeCommand = useCallback(async (command: CommandType) => { + const executeCommand = useCallback(async (command: CommandType, args: string = '') => { if (!opcodeUrl) return setLoading(true) @@ -35,7 +35,6 @@ export function useCommandHandler({ try { const client = createOpenCodeClient(opcodeUrl, directory) - // Handle special commands that need UI interaction switch (command.name) { case 'sessions': case 'resume': @@ -48,10 +47,9 @@ export function useCommandHandler({ break case 'themes': - // Themes command will be sent to server and appear as message await client.sendCommand(sessionID, { command: command.name, - arguments: '' + arguments: args }) break @@ -59,16 +57,13 @@ export function useCommandHandler({ onShowHelpDialog?.() break -case 'new': + case 'new': case 'clear': - // Create a new session and navigate to it try { const newSession = await createSession.mutateAsync({ agent: undefined }) if (newSession?.id) { - // Navigate to the correct repo session URL pattern - // We need to get the current repo ID from the URL const currentPath = window.location.pathname const repoMatch = currentPath.match(/\/repos\/(\d+)\/sessions\//) if (repoMatch) { @@ -76,7 +71,6 @@ case 'new': const newPath = `/repos/${repoId}/sessions/${newSession.id}` navigate(newPath) } else { - // Fallback: try to navigate to session directly if route exists navigate(`/session/${newSession.id}`) } } @@ -95,18 +89,16 @@ case 'new': case 'details': case 'editor': case 'init': - // These commands will be sent to the server and appear as messages await client.sendCommand(sessionID, { command: command.name, - arguments: '' + arguments: args }) break default: - // Send custom commands to server await client.sendCommand(sessionID, { command: command.name, - arguments: '' + arguments: args }) } } catch (error) { diff --git a/frontend/src/hooks/useCommands.ts b/frontend/src/hooks/useCommands.ts index 9a8cc569..4979d6e2 100644 --- a/frontend/src/hooks/useCommands.ts +++ b/frontend/src/hooks/useCommands.ts @@ -189,8 +189,7 @@ export function useCommands(opcodeUrl: string | null) { const searchTerm = query.toLowerCase() return commands.filter(command => - command.name.toLowerCase().includes(searchTerm) || - command.description?.toLowerCase().includes(searchTerm) + command.name.toLowerCase().includes(searchTerm) ) }