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 { useFetchBuiltinPipelines } from '@/hooks/use-agent-request';
import { useFetchPipelineVersionCatalog } from '@/hooks/use-pipeline-version-request';
import {
buildParserOptionValue,
parseParserOptionValue,
ParserOptionKind,
} from '@/hooks/use-parser-options';
import { useCallback, useMemo, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { PipelineVersionLabel } from '../pipeline-version-label';
import {
filterTreeNodes,
TreeSelectNode,
withBranchIds,
} from '../tree-select-utils';
// Node id of a pipeline branch (the pipeline draft itself). Branches are never
// selectable in TreeSelect — only their version leaves are — so this id is
// never emitted as a value.
const buildPipelineNodeId = (pipelineId: string) =>
buildParserOptionValue(ParserOptionKind.Pipeline, pipelineId);
// Combines the cached pipeline/version catalog with builtin parser leaves.
export function useParserTreeData({ value }: { value?: string }) {
const { t } = useTranslation();
const { options: builtinOptions, loading: builtinLoading } =
useFetchBuiltinPipelines();
const { data: pipelines = [], loading: pipelineLoading } =
useFetchPipelineVersionCatalog();
const selectedPipelineId = useMemo(() => {
const parsed = parseParserOptionValue(value);
return parsed?.kind === ParserOptionKind.Pipeline
? parsed.rawId
: undefined;
}, [value]);
// Expansion is local; all version metadata is already in the catalog.
const [expandedIds, setExpandedIds] = useState<Set<string>>(new Set());
// Render-phase adjustment (React's endorsed "adjust state when a prop
// changes" pattern): the branch holding the selection starts expanded. The
// previous-id comparison keeps a user's later manual collapse intact.
const [prevSelectedPipelineId, setPrevSelectedPipelineId] = useState<
string | undefined
>(undefined);
if (selectedPipelineId !== prevSelectedPipelineId) {
setPrevSelectedPipelineId(selectedPipelineId);
if (selectedPipelineId) {
const nodeId = buildPipelineNodeId(selectedPipelineId);
setExpandedIds((prev) => {
if (prev.has(nodeId)) return prev;
const next = new Set(prev);
next.add(nodeId);
return next;
});
}
}
// Trim: some locales still define the marker with surrounding whitespace
// from when it was appended to the label text.
const builtInTag = t('knowledgeConfiguration.builtInSuffix').trim();
const treeData = useMemo<TreeSelectNode[]>(() => {
const pipelineNodes = pipelines.map((item): TreeSelectNode => {
const nodeId = buildPipelineNodeId(item.id);
const versions = item.versions;
let children: TreeSelectNode[];
if (versions.length === 0) {
// A pipeline draft without saved versions stays visible but
// offers nothing to select. The disabled placeholder row also keeps
// the branch non-empty, so TreeSelect never treats the draft itself
// as a selectable leaf.
children = [
{
id: `${nodeId}:empty`,
title: '',
disabled: true,
label: (
<span className="text-text-disabled">
{t('flow.version.noPublished')}
</span>
),
},
];
} else {
children = versions.map((version): TreeSelectNode => ({
id: buildParserOptionValue(
ParserOptionKind.Pipeline,
item.id,
version.id,
),
// The alias joins the plain-text title so TreeSelect search
// matches it; `label` renders it as secondary text instead.
title: version.alias
? `${version.name} ${version.alias}`
: version.name,
searchKeywords: version.alias ? [version.alias] : undefined,
// The trigger display needs the alias and the unpolluted title
// (title above has the alias joined in for search). The dropdown
// row caps the alias at a fixed width — the popover is w-auto, so
// an uncapped alias would blow it wide instead of truncating.
data: version.alias
? { title: version.name, alias: version.alias }
: undefined,
label: (
<PipelineVersionLabel
title={version.name}
alias={version.alias}
aliasClassName="max-w-60"
/>
),
}));
}
return { id: nodeId, title: item.name, children };
});
const builtins = builtinOptions.map((option): TreeSelectNode => ({
id: buildParserOptionValue(ParserOptionKind.BuiltIn, option.value),
title: option.label,
searchKeywords: [option.label],
label: (
<span className="flex items-center gap-1.5 min-w-0">
<span className="truncate">{option.label}</span>
<span className="shrink-0 rounded-md bg-bg-card px-1.5 py-0.5 text-xs text-text-secondary">
{builtInTag}
</span>
</span>
),
}));
// Pipeline branches first, builtin leaves below — the same ordering the
// flat parser select used.
return [...pipelineNodes, ...builtins];
}, [pipelines, builtinOptions, t, builtInTag]);
// TreeSelect's built-in search auto-expansion only runs for uncontrolled
// expansion; with expansion owned here, matching branches are expanded from
// the search event instead. filterTreeNodes mirrors TreeSelect's keep-rule
// exactly, and every branch surviving the filter gets expanded, matching
// the built-in behavior.
const handleSearchChange = useCallback(
(term: string) => {
if (!term.trim()) return;
const visible = filterTreeNodes(treeData, term);
setExpandedIds((prev) => withBranchIds(prev, visible));
},
[treeData],
);
// An unmatched value is "missing" only once everything that could contain
// it has loaded; while the selected pipeline's versions are still arriving
// the select must not flash the missing display.
const loading = builtinLoading || pipelineLoading;
const selectedLeafExists = useMemo(() => {
if (!value) return false;
const find = (nodes: TreeSelectNode[]): boolean =>
nodes.some(
(node) =>
node.id === value || (node.children ? find(node.children) : false),
);
return find(treeData);
}, [treeData, value]);
return {
treeData,
expandedIds,
onExpandedChange: setExpandedIds,
onSearchChange: handleSearchChange,
loading,
selectedLeafExists,
};
}