Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

Fix: show linked knowledge base names instead of IDs when searching in the "Link to knowledge base" dialog (#18581)

euvre830b6740cc51
diff --git a/web/src/components/ui/__tests__/multi-select.test.tsx b/web/src/components/ui/__tests__/multi-select.test.tsx
new file mode 100644
index 0000000000..3915b9a23b
--- /dev/null
+++ b/web/src/components/ui/__tests__/multi-select.test.tsx
@@ -0,0 +1,64 @@
+import { render, screen } from '@testing-library/react';
+
+import { MultiSelect } from '../multi-select';
+
+// jsdom does not provide ResizeObserver or scrollIntoView, which cmdk
+// relies on.
+beforeAll(() => {
+ global.ResizeObserver = class {
+ observe() {}
+ unobserve() {}
+ disconnect() {}
+ };
+ Element.prototype.scrollIntoView = () => {};
+});
+
+const noop = () => {};
+
+describe('MultiSelect badge labels', () => {
+ it('keeps the label of a selected value after its option disappears', () => {
+ const { rerender } = render(
+ <MultiSelect
+ options={[
+ { label: 'Alpha', value: 'a' },
+ { label: 'Beta', value: 'b' },
+ ]}
+ defaultValue={['a']}
+ onValueChange={noop}
+ />,
+ );
+ expect(screen.getByText('Alpha')).toBeTruthy();
+
+ // The option list is narrowed (e.g. by a server-side search) and no
+ // longer contains the selected value; the badge must not fall back to
+ // the raw value.
+ rerender(
+ <MultiSelect
+ options={[{ label: 'Beta', value: 'b' }]}
+ defaultValue={['a']}
+ onValueChange={noop}
+ />,
+ );
+ expect(screen.getByText('Alpha')).toBeTruthy();
+ expect(screen.queryByText('a')).toBeNull();
+ });
+
+ it('resolves a never-seen selected value via getOptionLabel', () => {
+ render(
+ <MultiSelect
+ options={[{ label: 'Beta', value: 'b' }]}
+ defaultValue={['x']}
+ onValueChange={noop}
+ getOptionLabel={(value) => (value === 'x' ? 'X-ray' : undefined)}
+ />,
+ );
+ expect(screen.getByText('X-ray')).toBeTruthy();
+ });
+
+ it('falls back to the raw value when no label is known', () => {
+ render(
+ <MultiSelect options={[]} defaultValue={['z']} onValueChange={noop} />,
+ );
+ expect(screen.getByText('z')).toBeTruthy();
+ });
+});
diff --git a/web/src/components/ui/multi-select.tsx b/web/src/components/ui/multi-select.tsx
index dd447a3eb2..46c41c61d4 100644
--- a/web/src/components/ui/multi-select.tsx
+++ b/web/src/components/ui/multi-select.tsx
@@ -194,6 +194,13 @@ interface MultiSelectProps
isSearching?: boolean;
shouldFilter?: boolean;
onListScroll?: (e: React.UIEvent<HTMLDivElement>) => void;
+ /**
+ * Optional label resolver for a selected value that is not present in
+ * `options` (e.g. while options are narrowed by a search keyword). When it
+ * returns a label, the badge shows that label instead of falling back to
+ * the raw value with a warning marker.
+ */
+ getOptionLabel?: (value: string) => string | undefined;
}
export const MultiSelect = React.forwardRef<
@@ -220,6 +227,7 @@ export const MultiSelect = React.forwardRef<
isSearching = false,
shouldFilter,
onListScroll,
+ getOptionLabel,
...props
},
ref,
@@ -251,6 +259,16 @@ export const MultiSelect = React.forwardRef<
);
}, [options]);
+ // Remember the label of every option the component has seen, so a badge
+ // keeps its label when the option later disappears from `options` (e.g.
+ // the list is narrowed by a server-side search).
+ const rememberedLabels = React.useRef(new Map<string, React.ReactNode>());
+ React.useEffect(() => {
+ flatOptions.forEach((option) => {
+ rememberedLabels.current.set(option.value, option.label);
+ });
+ }, [flatOptions]);
+
const disabledValueSet = React.useMemo(() => {
return new Set(
flatOptions
@@ -362,6 +380,15 @@ export const MultiSelect = React.forwardRef<
<div className="flex flex-wrap items-center">
{selectedValues?.slice(0, maxCount)?.map((value) => {
const option = flatOptions.find((o) => o.value === value);
+ // A selected value may be absent from `options` while the
+ // list is narrowed (e.g. by a search keyword); fall back
+ // to the explicit `getOptionLabel` resolver and then to
+ // the remembered label of a previously seen option so the
+ // badge stays readable.
+ const label =
+ option?.label ??
+ getOptionLabel?.(value) ??
+ rememberedLabels.current.get(value);
const IconComponent = option?.icon;
return (
<Badge
@@ -378,20 +405,20 @@ export const MultiSelect = React.forwardRef<
{IconComponent && (
<IconComponent className="h-4 w-4" />
)}
- {/* A selected value with no matching option (e.g. the
+ {/* A selected value with no known label (e.g. the
entity no longer exists) gets a warning marker and
falls back to the raw value so the badge stays
readable and removable. */}
- {!option && (
+ {label == null && (
<TriangleAlert className="h-4 w-4 flex-shrink-0 text-text-disabled" />
)}
<div
className={cn(
'max-w-28 text-ellipsis overflow-hidden',
- { 'text-text-disabled': !option },
+ { 'text-text-disabled': label == null },
)}
>
- {option?.label ?? value}
+ {label ?? value}
</div>
{canRemoveValue(value) && (
<XCircle
diff --git a/web/src/pages/files/link-to-dataset-dialog.tsx b/web/src/pages/files/link-to-dataset-dialog.tsx
index e85cb26945..6828ff892e 100644
--- a/web/src/pages/files/link-to-dataset-dialog.tsx
+++ b/web/src/pages/files/link-to-dataset-dialog.tsx
@@ -24,7 +24,7 @@ import { IDataset } from '@/interfaces/database/dataset';
import { useDebounce } from 'ahooks';
import { zodResolver } from '@hookform/resolvers/zod';
import { Link2 } from 'lucide-react';
-import { useEffect, useMemo, useRef, useState } from 'react';
+import { useEffect, useMemo, useState } from 'react';
import { useForm } from 'react-hook-form';
import { useTranslation } from 'react-i18next';
import { z } from 'zod';
@@ -64,24 +64,38 @@ function LinkToDatasetForm({
// Cache all datasets the user has seen so names survive across searches
// and scroll. The paginated list may not contain every selected dataset
- // (e.g. initially-connected ones), so resolve the rest by ID.
- const datasetCacheRef = useRef(new Map<string, IDataset>());
+ // (e.g. initially-connected ones), so resolve the rest by ID. Kept as
+ // state so badges re-render once a missing name is fetched.
+ const [datasetCache, setDatasetCache] = useState<Map<string, IDataset>>(
+ new Map(),
+ );
const missingIds = useMemo(() => {
const loadedIds = new Set(list.map((d) => d.id));
return selectedIds.filter(
- (id) => !loadedIds.has(id) && !datasetCacheRef.current.has(id),
+ (id) => !loadedIds.has(id) && !datasetCache.has(id),
);
- }, [list, selectedIds]);
+ }, [list, selectedIds, datasetCache]);
const { data: missingDatasets } = useFetchDatasetsByIds(missingIds);
useEffect(() => {
- list.forEach((item) => {
- datasetCacheRef.current.set(item.id, item);
- });
- missingDatasets?.forEach((item) => {
- datasetCacheRef.current.set(item.id, item);
+ setDatasetCache((prev) => {
+ const next = new Map(prev);
+ let changed = false;
+ list.forEach((item) => {
+ if (!next.has(item.id)) {
+ next.set(item.id, item);
+ changed = true;
+ }
+ });
+ missingDatasets?.forEach((item) => {
+ if (!next.has(item.id)) {
+ next.set(item.id, item);
+ changed = true;
+ }
+ });
+ return changed ? next : prev;
});
}, [list, missingDatasets]);
@@ -96,7 +110,7 @@ function LinkToDatasetForm({
function onSubmit(data: z.infer<typeof FormSchema>) {
const kbsInfo = data.knowledgeIds.map((id) => {
- const dataset = datasetCacheRef.current.get(id);
+ const dataset = datasetCache.get(id);
return {
kb_id: id,
kb_name: dataset?.name ?? id,
@@ -105,10 +119,6 @@ function LinkToDatasetForm({
onConnectToKnowledgeOk(kbsInfo);
}
- // useEffect(() => {
- // form.setValue('knowledgeIds', initialConnectedIds); // this is invalid
- // }, [form, initialConnectedIds]);
-
return (
<Form {...form}>
<form
@@ -134,7 +144,9 @@ function LinkToDatasetForm({
isSearching={loading}
shouldFilter={false}
onListScroll={hasNextPage ? handleScroll : undefined}
- // {...field}
+ // Selected datasets filtered out of the current list page
+ // still show their names via the seen-datasets cache.
+ getOptionLabel={(value) => datasetCache.get(value)?.name}
modalPopover
/>
</FormControl>