@@ -439,18 +439,33 @@ export function useInputHandler({
439439 filteredSuggestions . length - 1 ,
440440 ) ;
441441 const selectedCommand = filteredSuggestions [ safeIndex ] ;
442- // If the command has arguments placeholders (e.g. <name>), strip them for the actual input
443- // Or keep them? Usually we just want the command base.
444- // But if the user defined command as "/mcp <action>", we probably want "/mcp ".
445- // Let's check how other commands are defined.
446- // "/chat save <name>" -> we probably want "/chat save "?
447- // The current logic uses the full string.
448-
449- // Actually, let's keep it simple first: just ensure cursor goes to end.
450- const newInput = selectedCommand . command + " " ;
451- // By passing the cursor position to setInput, we avoid the stale closure issue
452- // where setCursorPosition would clamp based on the old input length.
453- setInput ( newInput , newInput . length ) ;
442+
443+ // Strip placeholders like <name>, [action], etc.
444+ // Examples: "/chat save <name>" -> "/chat save ", "/mcp <action>" -> "/mcp "
445+ let completedCommand = selectedCommand . command ;
446+
447+ // Remove everything starting from the first space followed by a placeholder
448+ // or just placeholders themselves
449+ completedCommand = completedCommand
450+ . replace ( / \s * < [ ^ > ] + > / g, "" )
451+ . replace ( / \s * \[ [ ^ \] ] + \] / g, "" ) ;
452+
453+ // If it doesn't end with a space and it was a placeholder-heavy command, add a space
454+ if (
455+ ! completedCommand . endsWith ( " " ) &&
456+ selectedCommand . command . includes ( "<" )
457+ ) {
458+ completedCommand += " " ;
459+ } else if ( ! completedCommand . endsWith ( " " ) ) {
460+ completedCommand += " " ;
461+ }
462+
463+ // Preserve any text that might be after the cursor if we're in the middle of input
464+ const afterCursor = input . slice ( cursorPosition ) ;
465+ const newInput = completedCommand + afterCursor ;
466+
467+ // Set input and position cursor at the end of the completed command part
468+ setInput ( newInput , completedCommand . length ) ;
454469
455470 setShowCommandSuggestions ( false ) ;
456471 setSelectedCommandIndex ( 0 ) ;
0 commit comments