MCPcopy Index your code
hub / github.com/CodebuffAI/codebuff / useSuggestionEngine

Function useSuggestionEngine

cli/src/hooks/use-suggestion-engine.ts:586–783  ·  view source on GitHub ↗
({
  inputValue,
  cursorPosition,
  slashCommands,
  localAgents,
  fileTree,
  disableAgentSuggestions = false,
  currentAgentMode,
}: SuggestionEngineOptions)

Source from the content-addressed store, hash-verified

584}
585
586export const useSuggestionEngine = ({
587 inputValue,
588 cursorPosition,
589 slashCommands,
590 localAgents,
591 fileTree,
592 disableAgentSuggestions = false,
593 currentAgentMode,
594}: SuggestionEngineOptions): SuggestionEngineResult => {
595 const deferredInput = useDeferredValue(inputValue)
596 const slashCacheRef = useRef<Map<string, MatchedSlashCommand[]>>(
597 new Map<string, SlashCommand[]>(),
598 )
599 const agentCacheRef = useRef<Map<string, MatchedAgentInfo[]>>(
600 new Map<string, MatchedAgentInfo[]>(),
601 )
602 const fileCacheRef = useRef<Map<string, MatchedFileInfo[]>>(
603 new Map<string, MatchedFileInfo[]>(),
604 )
605 const fileRefreshIdRef = useRef(0)
606 const [filePaths, setFilePaths] = useState<PathInfo[]>(() =>
607 flattenFileTree(fileTree),
608 )
609
610 useEffect(() => {
611 slashCacheRef.current.clear()
612 }, [slashCommands])
613
614 useEffect(() => {
615 agentCacheRef.current.clear()
616 }, [localAgents])
617
618 useEffect(() => {
619 fileCacheRef.current.clear()
620 }, [filePaths])
621
622 useEffect(() => {
623 setFilePaths(flattenFileTree(fileTree))
624 }, [fileTree])
625
626 const slashContext = useMemo(
627 () => parseSlashContext(deferredInput),
628 [deferredInput],
629 )
630
631 // Note: mentionContext uses inputValue directly (not deferredInput) because
632 // the cursor position must match the text being parsed. Using deferredInput
633 // with current cursorPosition causes desync during heavy renders, making the
634 // @ menu fail to appear intermittently (especially after long conversations).
635 const mentionContext = useMemo(
636 () => parseMentionContext(inputValue, cursorPosition),
637 [inputValue, cursorPosition],
638 )
639
640 useEffect(() => {
641 if (!mentionContext.active) {
642 return
643 }

Callers 1

ChatFunction · 0.90

Calls 11

flattenFileTreeFunction · 0.85
parseSlashContextFunction · 0.85
parseMentionContextFunction · 0.85
refreshFilePathsFunction · 0.85
filterSlashCommandsFunction · 0.85
filterAgentMatchesFunction · 0.85
getFileNameFunction · 0.85
setMethod · 0.80
filterFileMatchesFunction · 0.70
getMethod · 0.65
clearMethod · 0.45

Tested by

no test coverage detected