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
fix(agent): keep uploaded file metadata tied to the selected file (#21189)
Lucien3d3a6929a26a
diff --git a/web/src/pages/agent/debug-content/uploader.test.tsx b/web/src/pages/agent/debug-content/uploader.test.tsxnew file mode 100644index 0000000000..1c977d2de5--- /dev/null+++ b/web/src/pages/agent/debug-content/uploader.test.tsx@@ -0,0 +1,210 @@+import { useUploadAgentFile } from '@/hooks/use-agent-request';+import { act, fireEvent, render, waitFor } from '@testing-library/react';+import * as React from 'react';++import { FileUploadDirectUpload } from './uploader';++jest.mock('@/hooks/use-agent-request', () => ({+ useUploadAgentFile: jest.fn(),+}));++jest.mock('sonner', () => ({ toast: jest.fn() }));++jest.mock('@/components/ui/button', () => {+ const React = jest.requireActual<typeof import('react')>('react');+ return {+ Button: React.forwardRef<+ HTMLButtonElement,+ JSX.IntrinsicElements['button'] & {+ variant?: string;+ size?: string;+ }+ >(function MockButton({ variant, size, ...props }, ref) {+ return (+ <button {...props} data-variant={variant} data-size={size} ref={ref} />+ );+ }),+ };+});++type UploadedFile = { id: string; name: string };+type UploadResponse = { code: number; data?: UploadedFile };++const UploadAgentFile = jest.fn<Promise<UploadResponse>, [File[]]>();++/** Creates a controllable response so tests choose upload completion order without timers. */+function deferred<T>() {+ let resolve!: (value: T) => void;+ let reject!: (error: Error) => void;+ const promise = new Promise<T>((resolvePromise, rejectPromise) => {+ resolve = resolvePromise;+ reject = rejectPromise;+ });+ return { promise, resolve, reject };+}++/** Renders the real uploader/store with per-File requests and observes emitted form values. */+function setup(+ files: File[],+ options: { maxFiles?: number; value?: UploadedFile[] } = {},+) {+ const requests = new Map(+ files.map((file) => [file, deferred<UploadResponse>()]),+ );+ UploadAgentFile.mockImplementation(([file]) => requests.get(file)!.promise);+ const onChange = jest.fn();+ const view = render(+ <FileUploadDirectUpload+ value={options.value ?? []}+ onChange={onChange}+ maxFiles={options.maxFiles}+ />,+ );+ const input =+ view.container.querySelector<HTMLInputElement>('input[type="file"]')!;++ /** Selects files through the input and waits for the expected upload requests to start. */+ async function select(selectedFiles: File[], expectedUploads: number) {+ fireEvent.change(input, { target: { files: selectedFiles } });+ await waitFor(() =>+ expect(UploadAgentFile).toHaveBeenCalledTimes(expectedUploads),+ );+ }++ /** Resolves this File's current request and flushes the resulting React updates. */+ async function complete(file: File, data: UploadedFile) {+ await act(async () => {+ requests.get(file)!.resolve({ code: 0, data });+ });+ }++ /** Clicks an item's real delete control to exercise the store's selection callback. */+ function remove(index: number) {+ const item = view.getAllByRole('listitem')[index];+ fireEvent.click(+ item.querySelector<HTMLButtonElement>(+ '[data-slot="file-upload-item-delete"]',+ )!,+ );+ }++ return { ...view, input, onChange, requests, select, complete, remove };+}++describe('Agent file upload identity', () => {+ beforeEach(() => {+ jest.clearAllMocks();+ jest.mocked(useUploadAgentFile).mockReturnValue({+ uploadAgentFile: UploadAgentFile,+ } as never);+ });++ it('removes the matching response when uploads complete out of order', async () => {+ const a = new File(['a'], 'A.pdf');+ const b = new File(['b'], 'B.pdf');+ const uploadA = { id: 'upload-A', name: a.name };+ const uploadB = { id: 'upload-B', name: b.name };+ const view = setup([a, b]);++ await view.select([a, b], 2);+ await view.complete(b, uploadB);+ await view.complete(a, uploadA);+ view.remove(0);++ expect(view.queryByText(a.name)).not.toBeInTheDocument();+ expect(view.getByText(b.name)).toBeInTheDocument();+ expect(view.onChange).toHaveBeenLastCalledWith([uploadB]);+ });++ it.each(['response', 'rejection'])(+ 'keeps a successful upload when removing a failed file (%s)',+ async (failure) => {+ const a = new File(['a'], 'A.pdf');+ const b = new File(['b'], 'B.pdf');+ const uploadB = { id: 'upload-B', name: b.name };+ const view = setup([a, b]);++ await view.select([a, b], 2);+ await act(async () => {+ if (failure === 'response') {+ view.requests.get(a)!.resolve({ code: 1 });+ } else {+ view.requests.get(a)!.reject(new Error('Network error'));+ }+ });+ await view.complete(b, uploadB);+ view.remove(0);++ expect(view.onChange).toHaveBeenLastCalledWith([uploadB]);+ expect(view.getByText(b.name)).toBeInTheDocument();+ },+ );++ it('ignores an upload that completes after its file was removed', async () => {+ const a = new File(['a'], 'A.pdf');+ const b = new File(['b'], 'B.pdf');+ const uploadB = { id: 'upload-B', name: b.name };+ const view = setup([a, b]);++ await view.select([a, b], 2);+ view.remove(0);+ await view.complete(b, uploadB);+ const changeCount = view.onChange.mock.calls.length;+ await view.complete(a, { id: 'removed-A', name: a.name });++ expect(view.onChange).toHaveBeenCalledTimes(changeCount);+ expect(view.onChange).toHaveBeenLastCalledWith([uploadB]);+ expect(view.queryByText(a.name)).not.toBeInTheDocument();+ });++ it('distinguishes different files with the same name', async () => {+ const a = new File(['first'], 'report.pdf');+ const b = new File(['second'], 'report.pdf');+ const uploadB = { id: 'upload-B', name: b.name };+ const view = setup([a, b]);++ await view.select([a, b], 2);+ await view.complete(b, uploadB);+ await view.complete(a, { id: 'upload-A', name: a.name });+ view.remove(0);++ expect(view.getAllByRole('listitem')).toHaveLength(1);+ expect(view.onChange).toHaveBeenLastCalledWith([uploadB]);+ });++ it('ignores the previous request when the same file is removed and re-added', async () => {+ const file = new File(['a'], 'A.pdf');+ const view = setup([file]);++ await view.select([file], 1);+ const previousRequest = view.requests.get(file)!;+ view.remove(0);+ view.requests.set(file, deferred<UploadResponse>());+ await view.select([file], 2);+ const uploadedFile = { id: 'new-upload', name: file.name };+ await view.complete(file, uploadedFile);+ await act(async () => {+ previousRequest.resolve({+ code: 0,+ data: { id: 'removed-upload', name: file.name },+ });+ });++ expect(view.onChange).toHaveBeenLastCalledWith([uploadedFile]);+ });++ it('supports single-file uploads without dropping initial form values', async () => {+ const file = new File(['a'], 'A.pdf');+ const existing = { id: 'existing-upload', name: 'existing.pdf' };+ const uploadedFile = { id: 'new-upload', name: file.name };+ const view = setup([file], { maxFiles: 1, value: [existing] });++ expect(view.input.multiple).toBe(false);+ await view.select([file], 1);+ await view.complete(file, uploadedFile);+ expect(view.onChange).toHaveBeenLastCalledWith([existing, uploadedFile]);+ view.remove(0);++ expect(view.onChange).toHaveBeenLastCalledWith([existing]);+ });+});diff --git a/web/src/pages/agent/debug-content/uploader.tsx b/web/src/pages/agent/debug-content/uploader.tsxindex ed147b23aa..a187fecb40 100644--- a/web/src/pages/agent/debug-content/uploader.tsx+++ b/web/src/pages/agent/debug-content/uploader.tsx@@ -24,23 +24,45 @@ type FileUploadDirectUploadProps = {maxFiles?: number;};+/**+ * Uses value as initial upload metadata and emits it with successful current selections.+ * Each selection owns a record so removed or replaced uploads cannot change the field.+ */export function FileUploadDirectUpload({value,onChange,maxFiles,}: FileUploadDirectUploadProps) {const [files, setFiles] = React.useState<File[]>([]);- const uploadedFilesRef = React.useRef<Record<string, any>[]>(+ const initialUploadedFilesRef = React.useRef<Record<string, any>[]>(Array.isArray(value) ? value : value ? [value] : [],);+ const uploadedFilesRef = React.useRef(+ new Map<File, { data?: Record<string, any> }>(),+ );const { uploadAgentFile } = useUploadAgentFile();+ /** Reports initial metadata plus successful current selections in selection order. */+ const notifyUploadedFilesChange = React.useCallback(() => {+ onChange([+ ...initialUploadedFilesRef.current,+ ...Array.from(uploadedFilesRef.current.values()).flatMap(({ data }) =>+ data ? [data] : [],+ ),+ ]);+ }, [onChange]);++ /** Uploads concurrently, ignoring callbacks whose selection record is no longer current. */const onUpload: NonNullable<FileUploadProps['onUpload']> = React.useCallback(async (files, { onSuccess, onError }) => {try {const uploadPromises = files.map(async (file) => {+ const upload = uploadedFilesRef.current.get(file);+ if (!upload) return;+const handleError = (error?: any) => {+ if (uploadedFilesRef.current.get(file) !== upload) return;onError(file,error instanceof Error ? error : new Error('Upload failed'),@@ -48,13 +70,11 @@ export function FileUploadDirectUpload({};try {const ret = await uploadAgentFile([file]);+ if (uploadedFilesRef.current.get(file) !== upload) return;if (ret.code === 0) {onSuccess(file);- uploadedFilesRef.current = [- ...uploadedFilesRef.current,- ret.data,- ];- onChange(uploadedFilesRef.current);+ upload.data = ret.data;+ notifyUploadedFilesChange();} else {handleError();}@@ -70,7 +90,7 @@ export function FileUploadDirectUpload({console.error('Unexpected error during upload:', error);}},- [onChange, uploadAgentFile],+ [notifyUploadedFilesChange, uploadAgentFile],);const onFileReject = React.useCallback((file: File, message: string) => {@@ -79,20 +99,25 @@ export function FileUploadDirectUpload({});}, []);+ /** Reconciles File identities; deleting a record invalidates its pending callbacks. */const handleFilesChange = React.useCallback((newFiles: File[]) => {- // Find removed files and update uploadedFilesRef- const removedFiles = files.filter((f) => !newFiles.includes(f));- if (removedFiles.length > 0) {- const removedIndices = removedFiles.map((f) => files.indexOf(f));- uploadedFilesRef.current = uploadedFilesRef.current.filter(- (_, idx) => !removedIndices.includes(idx),- );- onChange(uploadedFilesRef.current);+ let removedFiles = false;+ for (const file of uploadedFilesRef.current.keys()) {+ if (!newFiles.includes(file)) {+ uploadedFilesRef.current.delete(file);+ removedFiles = true;+ }+ }+ for (const file of newFiles) {+ if (!uploadedFilesRef.current.has(file)) {+ uploadedFilesRef.current.set(file, {});+ }}+ if (removedFiles) notifyUploadedFilesChange();setFiles(newFiles);},- [files, onChange],+ [notifyUploadedFilesChange],);return (