RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.
Go agent-harnessagent-native-databaseagentic-aiagentic-retrievalagentic-searchaiai-agentscontext-enginecontext-engineeringcontext-managementharness-engineeringknowledge-compilationragretrieval-augmented-generationretrieval-harnesssearch-harness
import { type SelectWithSearchFlagOptionType } from '@/components/originui/select-with-search';
import {
findEntityDisplayNameByKeyword,
getEntityDisplayName,
} from '@/components/structure-graph/adapters';
import { type ClickableNode } from '@/components/structure-graph/representation-renderer';
import { CompilationTemplateKind } from '@/constants/compilation';
import { useFetchDocumentStructureGraph } from '@/hooks/use-document-request';
import { useDebounce } from 'ahooks';
import { useCallback, useMemo, useState } from 'react';
import { useSelectedTemplate } from './use-selected-template';
export function useGraphEntitySearch(
onNodeClick?: (node: ClickableNode) => void,
) {
const [graphKeywords, setGraphKeywords] = useState('');
const [searchKeyword, setSearchKeyword] = useState(''); // ExpandableSearchInput value
const [selectedNodeId, setSelectedNodeId] = useState(''); // entity name
// Non-graph kinds search server-side with the same ?keywords= query the
// graph-kind Enter search uses; the two inputs never show at once, and the
// handlers below keep at most one of the two keyword sources non-empty.
const debouncedSearchKeyword = useDebounce(searchKeyword, { wait: 500 });
const { data, loading } = useFetchDocumentStructureGraph(
debouncedSearchKeyword || graphKeywords,
);
const templates = useMemo(() => data?.templates ?? [], [data?.templates]);
const {
selectedTemplateId,
setSelectedTemplateId,
selectedTemplate,
selectedKind,
} = useSelectedTemplate(templates);
const isGraphKind = selectedKind === CompilationTemplateKind.KnowledgeGraph;
const entityOptions = useMemo<SelectWithSearchFlagOptionType[]>(
() =>
(selectedTemplate?.entities ?? []).map((entity) => {
const name = getEntityDisplayName(entity);
return {
label: name,
value: name,
keywords: [name, ...(entity.aliases ?? [])],
};
}),
[selectedTemplate?.entities],
);
// Only refill the select when the selected entity is still in the current
// graph data, to avoid showing raw text with no matching option
const selectedEntityName =
selectedNodeId &&
(selectedTemplate?.entities ?? []).some(
(entity) => getEntityDisplayName(entity) === selectedNodeId,
)
? selectedNodeId
: '';
// Picking an option behaves like an Enter search: refetch the server-side
// keyword subgraph for that entity (fresh graph + entity count) and keep the
// node highlighted, in addition to the chunk navigation.
const handleSelectEntity = useCallback(
(name: string) => {
if (!name) {
setGraphKeywords('');
setSearchKeyword('');
setSelectedNodeId('');
return;
}
setSelectedNodeId(name);
setGraphKeywords(name);
setSearchKeyword('');
const entity = (selectedTemplate?.entities ?? []).find(
(item) => getEntityDisplayName(item) === name,
);
if (entity?.source_chunk_ids?.length) {
onNodeClick?.({
id: entity.id ?? name,
name,
source_chunk_ids: entity.source_chunk_ids,
});
}
},
[selectedTemplate?.entities, onNodeClick],
);
const handleNoMatchEnter = useCallback(
(keywords: string) => {
// Enter on a keyword that exactly names an entity must behave like
// picking it from the dropdown. Only unmatched text falls back to the
// raw keyword subgraph with no highlighted node.
const entityName = findEntityDisplayNameByKeyword(
selectedTemplate?.entities ?? [],
keywords,
);
if (entityName) {
handleSelectEntity(entityName);
return;
}
setGraphKeywords(keywords);
setSearchKeyword('');
setSelectedNodeId('');
},
[selectedTemplate?.entities, handleSelectEntity],
);
const handleSearchKeywordChange = useCallback((value: string) => {
setSearchKeyword(value);
}, []);
// Two-way binding: clicking a graph node selects it in the dropdown,
// then forwards to chunk navigation like before
const handleNodeClick = useCallback(
(node: ClickableNode) => {
if (isGraphKind && node.name) {
setSelectedNodeId(node.name);
}
if (!node.source_chunk_ids?.length) return;
onNodeClick?.(node);
},
[isGraphKind, onNodeClick],
);
const handleTemplateChange = useCallback(
(templateId: string) => {
setSelectedTemplateId(templateId);
setGraphKeywords('');
setSearchKeyword('');
setSelectedNodeId('');
},
[setSelectedTemplateId],
);
return {
data,
loading,
templates,
selectedTemplateId,
selectedTemplate,
isGraphKind,
entityOptions,
searchKeyword,
// Mirror the fetch driver (debouncedSearchKeyword || graphKeywords): the
// two keyword inputs swap whenever the result kind flips isGraphKind, so
// when the expandable-input channel drove the current subgraph, show its
// keyword — not a stale graph keyword left behind by an earlier failed
// Enter search.
graphSelectValue:
selectedEntityName || debouncedSearchKeyword || graphKeywords,
highlightNodeId: selectedEntityName || null,
handleSelectEntity,
handleNoMatchEnter,
handleSearchKeywordChange,
handleTemplateChange,
handleNodeClick,
};
}