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
feat(web): preview source document from chunk reference images (#20895)
chanx3864521d466b
diff --git a/web/src/components/file-icon/index.tsx b/web/src/components/file-icon/index.tsxindex ece51eb3cb..4fd823340f 100644--- a/web/src/components/file-icon/index.tsx+++ b/web/src/components/file-icon/index.tsx@@ -26,9 +26,10 @@ import styles from './index.module.less';interface IProps {name: string;id: string;+ width?: number;}-const FileIcon = ({ name, id }: IProps) => {+const FileIcon = ({ name, id, width = 24 }: IProps) => {const fileExtension = getExtension(name);const { data: fileThumbnails, setDocumentIds } =@@ -47,9 +48,9 @@ const FileIcon = ({ name, id }: IProps) => {return blobUrl ? (<img src={blobUrl} className={styles.thumbnailImg}></img>) : hasSvgIcon(iconName) ? (- <SvgIcon name={iconName} width={24}></SvgIcon>+ <SvgIcon name={iconName} width={width}></SvgIcon>) : (- <LucideFile size={24}></LucideFile>+ <LucideFile size={width}></LucideFile>);};diff --git a/web/src/components/image/index.tsx b/web/src/components/image/index.tsxindex 1c1dc95e50..559dc990e9 100644--- a/web/src/components/image/index.tsx+++ b/web/src/components/image/index.tsx@@ -26,6 +26,7 @@ interface IImage extends React.ImgHTMLAttributes<HTMLImageElement> {documentId?: string;t?: string | number;label?: string;+ previewButton?: React.ReactNode;}type ImageCacheItem = {@@ -267,7 +268,7 @@ export const AuthenticatedImg = ({};const Image = React.forwardRef<HTMLImageElement, IImage>(function Image(- { id, documentId, t, label, className, ...props },+ { id, documentId, t, label, previewButton, className, ...props },ref,) {const src = useDocumentImageUrl(id, documentId, t);@@ -287,6 +288,7 @@ const Image = React.forwardRef<HTMLImageElement, IImage>(function Image(return (<div className="relative inline-block w-full">{imageElement}+ {previewButton}<div className="absolute bottom-2 right-2 bg-accent-primary text-white px-2 py-0.5 rounded-xl text-xs font-normal backdrop-blur-sm">{label}</div>diff --git a/web/src/components/next-markdown-content/index.tsx b/web/src/components/next-markdown-content/index.tsxindex 0baf4ea648..5d713ab12e 100644--- a/web/src/components/next-markdown-content/index.tsx+++ b/web/src/components/next-markdown-content/index.tsx@@ -40,6 +40,7 @@ import { useTranslation } from 'react-i18next';import 'katex/dist/katex.min.css'; // `rehype-katex` does not import the CSS for youimport {+ buildCitationDisplayMap,currentReg,escapeUnmatchedAngleBrackets,parseCitationIndex,@@ -387,14 +388,10 @@ function MarkdownContent({// its first occurrence, so the same ID always renders as the same [N]// chip. The actual chunk lookup uses that index against the positional// order of IReferenceObject.chunks (see getReferenceInfo).- const keyToDisplay = new Map<number | string, number>();+ const keyToDisplay = buildCitationDisplayMap(text);const replacedText = reactStringReplace(text, currentReg, (match) => {const chunkKey = getChunkKey(match);- let displayIndex = keyToDisplay.get(chunkKey);- if (displayIndex === undefined) {- displayIndex = keyToDisplay.size;- keyToDisplay.set(chunkKey, displayIndex);- }+ const displayIndex = keyToDisplay.get(chunkKey) ?? 0;return (<HoverCard key={`${String(chunkKey)}-${displayIndex}`}>diff --git a/web/src/components/next-message-item/reference-image-list.tsx b/web/src/components/next-message-item/reference-image-list.tsxindex 285dfb1e78..344e752d72 100644--- a/web/src/components/next-message-item/reference-image-list.tsx+++ b/web/src/components/next-message-item/reference-image-list.tsx@@ -15,6 +15,7 @@*/import Image, { useDocumentImageUrl } from '@/components/image';+import FileIcon from '@/components/file-icon';import {Carousel,CarouselContent,@@ -22,12 +23,15 @@ import {CarouselNext,CarouselPrevious,} from '@/components/ui/carousel';+import { useSetModalState } from '@/hooks/common-hooks';import { IReferenceChunk } from '@/interfaces/database/chat';+import PdfDrawer from '@/pages/next-search/document-preview-modal';+import { buildCitationDisplayMap } from '@/utils/chat';+import { middleEllipsis } from '@/utils/common-util';import { isPlainObject } from 'lodash';import { RotateCw, ZoomIn, ZoomOut } from 'lucide-react';import { useMemo } from 'react';import { PhotoProvider, PhotoView } from 'react-photo-view';-import { extractNumbersFromMessageContent } from './utils';type IProps = {referenceChunks?: IReferenceChunk[] | Record<string, IReferenceChunk>;messageContent: string;@@ -36,6 +40,7 @@ type IProps = {type ImageItem = {id: string;documentId: string;+ documentName: string;index: number;};@@ -59,25 +64,57 @@ const getButtonVisibilityClass = (imageCount: number) => {* @param documentId - The document identifier for the image* @param index - The display index of the image in the list*/-function ImagePhotoView({ id, documentId, index }: ImageItem) {+function ImagePhotoView({ id, documentId, documentName, index }: ImageItem) {const src = useDocumentImageUrl(id, documentId);-+ const { visible, showModal, hideModal } = useSetModalState();+ const previewButton = (+ <button+ type="button"+ title={documentName}+ onClick={(e) => {+ // Prevent triggering the image fullscreen preview+ e.stopPropagation();+ showModal();+ }}+ className="absolute bottom-2 left-2 flex max-w-[calc(100%-2.5rem)] items-end gap-1 rounded-md text-[11px] leading-tight "+ >+ <span className="[&>img]:h-4 [&>img]:w-4 [&>svg]:h-4 [&>svg]:w-4">+ <FileIcon id={documentId} name={documentName} width={14}></FileIcon>+ </span>+ <span className="truncate">{middleEllipsis(documentName, 10, 6)}</span>+ </button>+ );const imageElement = (<Imageid={id}documentId={documentId}className="h-40 w-full"label={`[${index + 1}]`}+ previewButton={previewButton}/>);+ const wrapped = (+ <div className="relative">+ {imageElement}+ {visible && (+ <PdfDrawer+ visible={visible}+ hideModal={hideModal}+ documentId={documentId}+ chunk={{ document_name: documentName }}+ ></PdfDrawer>+ )}+ </div>+ );+// When src is not yet available, render image without PhotoView// to avoid PhotoView caching an empty src that would show blank previewif (!src) {- return imageElement;+ return wrapped;}- return <PhotoView src={src}>{imageElement}</PhotoView>;+ return <PhotoView src={src}>{wrapped}</PhotoView>;}function ImageCarousel({ images }: { images: ImageItem[] }) {@@ -113,7 +150,7 @@ function ImageCarousel({ images }: { images: ImageItem[] }) {}}><CarouselContent>- {images.map(({ id, documentId, index }) => (+ {images.map(({ id, documentId, documentName, index }) => (<CarouselItemkey={index}className="@@ -127,6 +164,7 @@ function ImageCarousel({ images }: { images: ImageItem[] }) {<ImagePhotoViewid={id}documentId={documentId}+ documentName={documentName}index={index}></ImagePhotoView></CarouselItem>@@ -143,39 +181,35 @@ export function ReferenceImageList({referenceChunks,messageContent,}: IProps) {- const allChunkIndexes = extractNumbersFromMessageContent(messageContent);const images = useMemo(() => {- if (Array.isArray(referenceChunks)) {- return referenceChunks- .map((chunk, idx) => ({+ // Same positional mapping as the citation chips in next-markdown-content:+ // each unique marker is a 0-based position by first occurrence and the+ // chunk is read by insertion order. This works for both plain chat and+ // agent references (whose markers are [ID:<chunkId>] and chunks are arrays).+ const displayIndexes = [+ ...buildCitationDisplayMap(messageContent).values(),+ ];+ const orderedChunks = Array.isArray(referenceChunks)+ ? referenceChunks+ : isPlainObject(referenceChunks)+ ? Object.values(referenceChunks ?? {})+ : [];++ return displayIndexes+ .map((position) => {+ const chunk = orderedChunks[position];+ if (!chunk?.image_id || !chunk?.document_id) {+ return null;+ }+ return {id: chunk.image_id,documentId: chunk.document_id,- index: idx,- }))- .filter(- (item, idx) =>- allChunkIndexes.includes(idx) && item.id && item.documentId,- );- }-- if (isPlainObject(referenceChunks)) {- return Object.entries(referenceChunks || {}).reduce<ImageItem[]>(- (pre, [idx, chunk]) => {- if (allChunkIndexes.includes(Number(idx)) && chunk.image_id) {- return pre.concat({- id: chunk.image_id,- documentId: chunk.document_id,- index: Number(idx),- });- }- return pre;- },- [],- );- }-- return [];- }, [allChunkIndexes, referenceChunks]);+ documentName: chunk.document_name,+ index: position,+ } as ImageItem;+ })+ .filter((item): item is ImageItem => item !== null);+ }, [messageContent, referenceChunks]);const imageCount = images?.length || 0;diff --git a/web/src/utils/chat.ts b/web/src/utils/chat.tsindex 382b6ab24e..b16563da62 100644--- a/web/src/utils/chat.ts+++ b/web/src/utils/chat.ts@@ -23,6 +23,7 @@ import type { CompletionChunk } from '@/services/chat-completion-stream';import { omit } from 'lodash';import { v4 as uuid } from 'uuid';import {+ buildCitationDisplayMap,citationMarkerReg,normalizeCitationDigits,parseCitationIndex,@@ -244,7 +245,11 @@ export function setChatVariableEnabledFieldValuePage() {const oldReg = /(#{2}[0-9\u0660-\u0669\u06F0-\u06F9]+\${2})/g;export const currentReg = citationMarkerReg;-export { normalizeCitationDigits, parseCitationIndex };+export {+ buildCitationDisplayMap,+ normalizeCitationDigits,+ parseCitationIndex,+};// To be compatible with the old index matching modeexport const replaceTextByOldReg = (text: string) => {diff --git a/web/src/utils/citation-utils.ts b/web/src/utils/citation-utils.tsindex 95e5a58f01..4387834654 100644--- a/web/src/utils/citation-utils.ts+++ b/web/src/utils/citation-utils.ts@@ -43,6 +43,25 @@ export const parseCitationIndex = (value: string): CitationKey => {if (/^[0-9a-fA-F]+$/.test(normalized)) return normalized;return Number.NaN;};-export const citationMarkerReg =/\[(?:ID:)?([0-9a-fA-F\u0660-\u0669\u06F0-\u06F9]+)\]/g;++// Map each unique citation marker ([n] or [ID:<chunkId>]) to its stable+// 0-based display position by first occurrence. The chip renders position + 1+// and the chunk is looked up positionally via Object.values(chunks)[position].+// Works for both plain chat (numeric markers) and agents ([ID:<chunkId>]).+export const buildCitationDisplayMap = (+ content: string,+): Map<CitationKey, number> => {+ const map = new Map<CitationKey, number>();+ const reg = new RegExp(citationMarkerReg.source, citationMarkerReg.flags);+ let match: RegExpExecArray | null;+ while ((match = reg.exec(content ?? '')) !== null) {+ const key = parseCitationIndex(match[0]);+ if (Number.isNaN(key)) continue;+ if (!map.has(key)) {+ map.set(key, map.size);+ }+ }+ return map;+};