diff --git a/surfsense_web/components/assistant-ui/markdown-text.tsx b/surfsense_web/components/assistant-ui/markdown-text.tsx index 532ae7663..5bc905645 100644 --- a/surfsense_web/components/assistant-ui/markdown-text.tsx +++ b/surfsense_web/components/assistant-ui/markdown-text.tsx @@ -20,7 +20,8 @@ const CITATION_REGEX = /\[citation:(doc-)?(\d+)\]/g; // Track chunk IDs to citation numbers mapping for consistent numbering // This map is reset when a new message starts rendering -let chunkIdToCitationNumber: Map = new Map(); +// Uses string keys to differentiate between doc and regular chunks (e.g., "doc-123" vs "123") +let chunkIdToCitationNumber: Map = new Map(); let nextCitationNumber = 1; /** @@ -37,11 +38,11 @@ export function resetCitationCounter() { */ function getCitationNumber(chunkId: number, isDocsChunk: boolean): number { const key = isDocsChunk ? `doc-${chunkId}` : String(chunkId); - const existingNumber = chunkIdToCitationNumber.get(key as unknown as number); + const existingNumber = chunkIdToCitationNumber.get(key); if (existingNumber === undefined) { - chunkIdToCitationNumber.set(key as unknown as number, nextCitationNumber++); + chunkIdToCitationNumber.set(key, nextCitationNumber++); } - return chunkIdToCitationNumber.get(key as unknown as number)!; + return chunkIdToCitationNumber.get(key)!; } /** diff --git a/surfsense_web/components/new-chat/source-detail-panel.tsx b/surfsense_web/components/new-chat/source-detail-panel.tsx index dc0c3c3f8..df2809fdb 100644 --- a/surfsense_web/components/new-chat/source-detail-panel.tsx +++ b/surfsense_web/components/new-chat/source-detail-panel.tsx @@ -21,10 +21,16 @@ import { Badge } from "@/components/ui/badge"; import { Button } from "@/components/ui/button"; import { Collapsible, CollapsibleContent, CollapsibleTrigger } from "@/components/ui/collapsible"; import { ScrollArea } from "@/components/ui/scroll-area"; +import type { + GetDocumentByChunkResponse, + GetSurfsenseDocsByChunkResponse, +} from "@/contracts/types/document.types"; import { documentsApiService } from "@/lib/apis/documents-api.service"; import { cacheKeys } from "@/lib/query-client/cache-keys"; import { cn } from "@/lib/utils"; +type DocumentData = GetDocumentByChunkResponse | GetSurfsenseDocsByChunkResponse; + interface SourceDetailPanelProps { open: boolean; onOpenChange: (open: boolean) => void; @@ -133,14 +139,16 @@ export function SourceDetailPanel({ data: documentData, isLoading: isDocumentByChunkFetching, error: documentByChunkFetchingError, - } = useQuery({ + } = useQuery({ queryKey: isDocsChunk ? cacheKeys.documents.byChunk(`doc-${chunkId}`) : cacheKeys.documents.byChunk(chunkId.toString()), - queryFn: () => - isDocsChunk - ? documentsApiService.getSurfsenseDocByChunk(chunkId) - : documentsApiService.getDocumentByChunk({ chunk_id: chunkId }), + queryFn: async () => { + if (isDocsChunk) { + return documentsApiService.getSurfsenseDocByChunk(chunkId); + } + return documentsApiService.getDocumentByChunk({ chunk_id: chunkId }); + }, enabled: !!chunkId && open, staleTime: 5 * 60 * 1000, }); @@ -332,7 +340,7 @@ export function SourceDetailPanel({ {documentData?.title || title || "Source Document"}

- {documentData + {documentData && "document_type" in documentData ? formatDocumentType(documentData.document_type) : sourceType && formatDocumentType(sourceType)} {documentData?.chunks && ( @@ -498,7 +506,8 @@ export function SourceDetailPanel({

{/* Document Metadata */} - {documentData.document_metadata && + {"document_metadata" in documentData && + documentData.document_metadata && Object.keys(documentData.document_metadata).length > 0 && ( ; export type DeleteDocumentRequest = z.infer; export type DeleteDocumentResponse = z.infer; export type DocumentTypeEnum = z.infer; +export type SurfsenseDocsChunk = z.infer; +export type SurfsenseDocsDocument = z.infer; +export type SurfsenseDocsDocumentWithChunks = z.infer; +export type GetSurfsenseDocsByChunkRequest = z.infer; +export type GetSurfsenseDocsByChunkResponse = z.infer; diff --git a/surfsense_web/lib/apis/documents-api.service.ts b/surfsense_web/lib/apis/documents-api.service.ts index 372baee4d..2e7d18e44 100644 --- a/surfsense_web/lib/apis/documents-api.service.ts +++ b/surfsense_web/lib/apis/documents-api.service.ts @@ -17,6 +17,7 @@ import { getDocumentsResponse, getDocumentTypeCountsRequest, getDocumentTypeCountsResponse, + getSurfsenseDocsByChunkResponse, type SearchDocumentsRequest, searchDocumentsRequest, searchDocumentsResponse, @@ -214,10 +215,9 @@ class DocumentsApiService { * Used for resolving [citation:doc-XXX] citations */ getSurfsenseDocByChunk = async (chunkId: number) => { - // Response shape matches getDocumentByChunkResponse structure return baseApiService.get( `/api/v1/surfsense-docs/by-chunk/${chunkId}`, - getDocumentByChunkResponse + getSurfsenseDocsByChunkResponse ); };