2025-03-19 21:36:30 +08:00
|
|
|
|
import { useState, useEffect, useCallback } from 'react'
|
2025-03-08 09:26:21 +00:00
|
|
|
|
import { useTranslation } from 'react-i18next'
|
2025-03-19 21:36:30 +08:00
|
|
|
|
import { useSettingsStore } from '@/stores/settings'
|
2025-02-15 23:22:37 +08:00
|
|
|
|
import Button from '@/components/ui/Button'
|
|
|
|
|
|
import {
|
|
|
|
|
|
Table,
|
|
|
|
|
|
TableBody,
|
|
|
|
|
|
TableCell,
|
|
|
|
|
|
TableHead,
|
|
|
|
|
|
TableHeader,
|
|
|
|
|
|
TableRow
|
|
|
|
|
|
} from '@/components/ui/Table'
|
|
|
|
|
|
import { Card, CardHeader, CardTitle, CardContent, CardDescription } from '@/components/ui/Card'
|
|
|
|
|
|
import EmptyCard from '@/components/ui/EmptyCard'
|
2025-02-16 21:43:14 +08:00
|
|
|
|
import UploadDocumentsDialog from '@/components/documents/UploadDocumentsDialog'
|
|
|
|
|
|
import ClearDocumentsDialog from '@/components/documents/ClearDocumentsDialog'
|
2025-02-15 23:22:37 +08:00
|
|
|
|
|
2025-02-17 01:05:31 +08:00
|
|
|
|
import { getDocuments, scanNewDocuments, DocsStatusesResponse } from '@/api/lightrag'
|
2025-02-15 23:22:37 +08:00
|
|
|
|
import { errorMessage } from '@/lib/utils'
|
|
|
|
|
|
import { toast } from 'sonner'
|
2025-02-17 01:05:31 +08:00
|
|
|
|
import { useBackendState } from '@/stores/state'
|
2025-02-15 23:22:37 +08:00
|
|
|
|
|
2025-03-26 12:05:54 +08:00
|
|
|
|
import { RefreshCwIcon, ActivityIcon } from 'lucide-react'
|
2025-03-25 22:42:46 +08:00
|
|
|
|
import { DocStatusResponse } from '@/api/lightrag'
|
2025-03-26 12:05:54 +08:00
|
|
|
|
import PipelineStatusDialog from '@/components/documents/PipelineStatusDialog'
|
2025-03-25 22:42:46 +08:00
|
|
|
|
|
|
|
|
|
|
const getDisplayFileName = (doc: DocStatusResponse, maxLength: number = 20): string => {
|
|
|
|
|
|
// Check if file_path exists and is a non-empty string
|
|
|
|
|
|
if (!doc.file_path || typeof doc.file_path !== 'string' || doc.file_path.trim() === '') {
|
|
|
|
|
|
return doc.id;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Try to extract filename from path
|
|
|
|
|
|
const parts = doc.file_path.split('/');
|
|
|
|
|
|
const fileName = parts[parts.length - 1];
|
|
|
|
|
|
|
|
|
|
|
|
// Ensure extracted filename is valid
|
|
|
|
|
|
if (!fileName || fileName.trim() === '') {
|
|
|
|
|
|
return doc.id;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// If filename is longer than maxLength, truncate it and add ellipsis
|
2025-03-25 22:44:53 +08:00
|
|
|
|
return fileName.length > maxLength
|
|
|
|
|
|
? fileName.slice(0, maxLength) + '...'
|
2025-03-25 22:42:46 +08:00
|
|
|
|
: fileName;
|
|
|
|
|
|
};
|
2025-02-15 23:22:37 +08:00
|
|
|
|
|
|
|
|
|
|
export default function DocumentManager() {
|
2025-03-26 12:05:54 +08:00
|
|
|
|
const [showPipelineStatus, setShowPipelineStatus] = useState(false)
|
2025-03-08 09:26:21 +00:00
|
|
|
|
const { t } = useTranslation()
|
2025-02-17 01:05:31 +08:00
|
|
|
|
const health = useBackendState.use.health()
|
|
|
|
|
|
const [docs, setDocs] = useState<DocsStatusesResponse | null>(null)
|
2025-03-19 21:36:30 +08:00
|
|
|
|
const currentTab = useSettingsStore.use.currentTab()
|
2025-03-25 22:42:46 +08:00
|
|
|
|
const showFileName = useSettingsStore.use.showFileName()
|
|
|
|
|
|
const setShowFileName = useSettingsStore.use.setShowFileName()
|
2025-02-15 23:22:37 +08:00
|
|
|
|
|
|
|
|
|
|
const fetchDocuments = useCallback(async () => {
|
|
|
|
|
|
try {
|
|
|
|
|
|
const docs = await getDocuments()
|
2025-02-17 01:05:31 +08:00
|
|
|
|
if (docs && docs.statuses) {
|
2025-02-18 00:30:51 +08:00
|
|
|
|
// compose all documents count
|
|
|
|
|
|
const numDocuments = Object.values(docs.statuses).reduce(
|
|
|
|
|
|
(acc, status) => acc + status.length,
|
|
|
|
|
|
0
|
|
|
|
|
|
)
|
|
|
|
|
|
if (numDocuments > 0) {
|
|
|
|
|
|
setDocs(docs)
|
|
|
|
|
|
} else {
|
|
|
|
|
|
setDocs(null)
|
|
|
|
|
|
}
|
2025-02-17 01:05:31 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
setDocs(null)
|
|
|
|
|
|
}
|
2025-02-15 23:22:37 +08:00
|
|
|
|
} catch (err) {
|
2025-03-08 09:26:21 +00:00
|
|
|
|
toast.error(t('documentPanel.documentManager.errors.loadFailed', { error: errorMessage(err) }))
|
2025-02-15 23:22:37 +08:00
|
|
|
|
}
|
2025-03-13 15:15:42 +08:00
|
|
|
|
}, [setDocs, t])
|
2025-02-15 23:22:37 +08:00
|
|
|
|
|
2025-03-19 21:36:30 +08:00
|
|
|
|
// Fetch documents when the tab becomes visible
|
2025-02-15 23:22:37 +08:00
|
|
|
|
useEffect(() => {
|
2025-03-19 21:36:30 +08:00
|
|
|
|
if (currentTab === 'documents') {
|
2025-03-13 19:50:37 +08:00
|
|
|
|
fetchDocuments()
|
|
|
|
|
|
}
|
2025-03-19 21:36:30 +08:00
|
|
|
|
}, [currentTab, fetchDocuments])
|
2025-02-15 23:22:37 +08:00
|
|
|
|
|
|
|
|
|
|
const scanDocuments = useCallback(async () => {
|
|
|
|
|
|
try {
|
|
|
|
|
|
const { status } = await scanNewDocuments()
|
|
|
|
|
|
toast.message(status)
|
|
|
|
|
|
} catch (err) {
|
2025-03-08 09:26:21 +00:00
|
|
|
|
toast.error(t('documentPanel.documentManager.errors.scanFailed', { error: errorMessage(err) }))
|
2025-02-15 23:22:37 +08:00
|
|
|
|
}
|
2025-03-13 15:15:42 +08:00
|
|
|
|
}, [t])
|
2025-02-15 23:22:37 +08:00
|
|
|
|
|
2025-03-19 21:36:30 +08:00
|
|
|
|
// Set up polling when the documents tab is active and health is good
|
2025-02-17 01:05:31 +08:00
|
|
|
|
useEffect(() => {
|
2025-03-19 21:36:30 +08:00
|
|
|
|
if (currentTab !== 'documents' || !health) {
|
2025-03-13 19:50:37 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
2025-03-14 00:03:45 +08:00
|
|
|
|
|
2025-02-17 01:05:31 +08:00
|
|
|
|
const interval = setInterval(async () => {
|
|
|
|
|
|
try {
|
|
|
|
|
|
await fetchDocuments()
|
|
|
|
|
|
} catch (err) {
|
2025-03-08 09:26:21 +00:00
|
|
|
|
toast.error(t('documentPanel.documentManager.errors.scanProgressFailed', { error: errorMessage(err) }))
|
2025-02-17 01:05:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
}, 5000)
|
2025-03-14 00:03:45 +08:00
|
|
|
|
|
2025-02-17 01:05:31 +08:00
|
|
|
|
return () => clearInterval(interval)
|
2025-03-19 21:36:30 +08:00
|
|
|
|
}, [health, fetchDocuments, t, currentTab])
|
2025-02-15 23:22:37 +08:00
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
|
<Card className="!size-full !rounded-none !border-none">
|
|
|
|
|
|
<CardHeader>
|
2025-03-08 09:26:21 +00:00
|
|
|
|
<CardTitle className="text-lg">{t('documentPanel.documentManager.title')}</CardTitle>
|
2025-02-15 23:22:37 +08:00
|
|
|
|
</CardHeader>
|
|
|
|
|
|
<CardContent className="space-y-4">
|
|
|
|
|
|
<div className="flex gap-2">
|
2025-03-26 12:05:54 +08:00
|
|
|
|
<div className="flex gap-2">
|
|
|
|
|
|
<Button
|
|
|
|
|
|
variant="outline"
|
|
|
|
|
|
onClick={scanDocuments}
|
|
|
|
|
|
side="bottom"
|
|
|
|
|
|
tooltip={t('documentPanel.documentManager.scanTooltip')}
|
|
|
|
|
|
size="sm"
|
|
|
|
|
|
>
|
|
|
|
|
|
<RefreshCwIcon /> {t('documentPanel.documentManager.scanButton')}
|
|
|
|
|
|
</Button>
|
|
|
|
|
|
<Button
|
|
|
|
|
|
variant="outline"
|
|
|
|
|
|
onClick={() => setShowPipelineStatus(true)}
|
|
|
|
|
|
side="bottom"
|
|
|
|
|
|
tooltip="View pipeline status"
|
|
|
|
|
|
size="sm"
|
|
|
|
|
|
>
|
|
|
|
|
|
<ActivityIcon /> Pipeline Status
|
|
|
|
|
|
</Button>
|
|
|
|
|
|
</div>
|
2025-02-15 23:22:37 +08:00
|
|
|
|
<div className="flex-1" />
|
|
|
|
|
|
<ClearDocumentsDialog />
|
|
|
|
|
|
<UploadDocumentsDialog />
|
2025-03-26 12:05:54 +08:00
|
|
|
|
<PipelineStatusDialog
|
|
|
|
|
|
open={showPipelineStatus}
|
|
|
|
|
|
onOpenChange={setShowPipelineStatus}
|
|
|
|
|
|
/>
|
2025-02-15 23:22:37 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
<Card>
|
|
|
|
|
|
<CardHeader>
|
2025-03-25 22:42:46 +08:00
|
|
|
|
<div className="flex justify-between items-center">
|
|
|
|
|
|
<CardTitle>{t('documentPanel.documentManager.uploadedTitle')}</CardTitle>
|
|
|
|
|
|
<div className="flex items-center gap-2">
|
|
|
|
|
|
<span className="text-sm text-gray-500">{t('documentPanel.documentManager.fileNameLabel')}</span>
|
|
|
|
|
|
<Button
|
|
|
|
|
|
variant="outline"
|
|
|
|
|
|
size="sm"
|
|
|
|
|
|
onClick={() => setShowFileName(!showFileName)}
|
|
|
|
|
|
className="border-gray-200 dark:border-gray-700 hover:bg-gray-100 dark:hover:bg-gray-800"
|
|
|
|
|
|
>
|
2025-03-25 22:44:53 +08:00
|
|
|
|
{showFileName
|
2025-03-25 22:42:46 +08:00
|
|
|
|
? t('documentPanel.documentManager.hideButton')
|
|
|
|
|
|
: t('documentPanel.documentManager.showButton')
|
|
|
|
|
|
}
|
|
|
|
|
|
</Button>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
2025-03-08 09:26:21 +00:00
|
|
|
|
<CardDescription>{t('documentPanel.documentManager.uploadedDescription')}</CardDescription>
|
2025-02-15 23:22:37 +08:00
|
|
|
|
</CardHeader>
|
|
|
|
|
|
|
|
|
|
|
|
<CardContent>
|
2025-02-17 01:05:31 +08:00
|
|
|
|
{!docs && (
|
2025-02-15 23:22:37 +08:00
|
|
|
|
<EmptyCard
|
2025-03-08 09:26:21 +00:00
|
|
|
|
title={t('documentPanel.documentManager.emptyTitle')}
|
|
|
|
|
|
description={t('documentPanel.documentManager.emptyDescription')}
|
2025-02-15 23:22:37 +08:00
|
|
|
|
/>
|
|
|
|
|
|
)}
|
2025-02-17 01:05:31 +08:00
|
|
|
|
{docs && (
|
2025-02-15 23:22:37 +08:00
|
|
|
|
<Table>
|
|
|
|
|
|
<TableHeader>
|
|
|
|
|
|
<TableRow>
|
2025-03-08 09:26:21 +00:00
|
|
|
|
<TableHead>{t('documentPanel.documentManager.columns.id')}</TableHead>
|
|
|
|
|
|
<TableHead>{t('documentPanel.documentManager.columns.summary')}</TableHead>
|
|
|
|
|
|
<TableHead>{t('documentPanel.documentManager.columns.status')}</TableHead>
|
|
|
|
|
|
<TableHead>{t('documentPanel.documentManager.columns.length')}</TableHead>
|
|
|
|
|
|
<TableHead>{t('documentPanel.documentManager.columns.chunks')}</TableHead>
|
|
|
|
|
|
<TableHead>{t('documentPanel.documentManager.columns.created')}</TableHead>
|
|
|
|
|
|
<TableHead>{t('documentPanel.documentManager.columns.updated')}</TableHead>
|
2025-02-15 23:22:37 +08:00
|
|
|
|
</TableRow>
|
|
|
|
|
|
</TableHeader>
|
2025-02-17 01:05:31 +08:00
|
|
|
|
<TableBody className="text-sm">
|
|
|
|
|
|
{Object.entries(docs.statuses).map(([status, documents]) =>
|
|
|
|
|
|
documents.map((doc) => (
|
|
|
|
|
|
<TableRow key={doc.id}>
|
2025-03-25 22:42:46 +08:00
|
|
|
|
<TableCell className="truncate font-mono overflow-visible">
|
|
|
|
|
|
{showFileName ? (
|
|
|
|
|
|
<>
|
|
|
|
|
|
<div className="group relative overflow-visible">
|
|
|
|
|
|
<div className="truncate">
|
|
|
|
|
|
{getDisplayFileName(doc, 35)}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<div className="invisible group-hover:visible fixed z-[9999] mt-1 max-w-[800px] whitespace-normal break-all rounded-md bg-black/95 px-3 py-2 text-sm text-white shadow-lg dark:bg-white/95 dark:text-black">
|
|
|
|
|
|
{doc.file_path}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<div className="text-xs text-gray-500">{doc.id}</div>
|
|
|
|
|
|
</>
|
|
|
|
|
|
) : (
|
|
|
|
|
|
<div className="group relative overflow-visible">
|
|
|
|
|
|
<div className="truncate">
|
|
|
|
|
|
{doc.id}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<div className="invisible group-hover:visible fixed z-[9999] mt-1 max-w-[800px] whitespace-normal break-all rounded-md bg-black/95 px-3 py-2 text-sm text-white shadow-lg dark:bg-white/95 dark:text-black">
|
|
|
|
|
|
{doc.file_path}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
)}
|
|
|
|
|
|
</TableCell>
|
|
|
|
|
|
<TableCell className="max-w-xs min-w-24 truncate overflow-visible">
|
|
|
|
|
|
<div className="group relative overflow-visible">
|
|
|
|
|
|
<div className="truncate">
|
|
|
|
|
|
{doc.content_summary}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<div className="invisible group-hover:visible fixed z-[9999] mt-1 max-w-[800px] whitespace-normal break-all rounded-md bg-black/95 px-3 py-2 text-sm text-white shadow-lg dark:bg-white/95 dark:text-black">
|
|
|
|
|
|
{doc.content_summary}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
2025-02-17 01:05:31 +08:00
|
|
|
|
</TableCell>
|
|
|
|
|
|
<TableCell>
|
|
|
|
|
|
{status === 'processed' && (
|
2025-03-08 09:26:21 +00:00
|
|
|
|
<span className="text-green-600">{t('documentPanel.documentManager.status.completed')}</span>
|
2025-02-17 01:05:31 +08:00
|
|
|
|
)}
|
|
|
|
|
|
{status === 'processing' && (
|
2025-03-08 09:26:21 +00:00
|
|
|
|
<span className="text-blue-600">{t('documentPanel.documentManager.status.processing')}</span>
|
2025-02-17 01:05:31 +08:00
|
|
|
|
)}
|
2025-03-08 09:26:21 +00:00
|
|
|
|
{status === 'pending' && <span className="text-yellow-600">{t('documentPanel.documentManager.status.pending')}</span>}
|
|
|
|
|
|
{status === 'failed' && <span className="text-red-600">{t('documentPanel.documentManager.status.failed')}</span>}
|
2025-02-17 01:05:31 +08:00
|
|
|
|
{doc.error && (
|
|
|
|
|
|
<span className="ml-2 text-red-500" title={doc.error}>
|
|
|
|
|
|
⚠️
|
|
|
|
|
|
</span>
|
|
|
|
|
|
)}
|
|
|
|
|
|
</TableCell>
|
|
|
|
|
|
<TableCell>{doc.content_length ?? '-'}</TableCell>
|
|
|
|
|
|
<TableCell>{doc.chunks_count ?? '-'}</TableCell>
|
|
|
|
|
|
<TableCell className="truncate">
|
|
|
|
|
|
{new Date(doc.created_at).toLocaleString()}
|
|
|
|
|
|
</TableCell>
|
|
|
|
|
|
<TableCell className="truncate">
|
|
|
|
|
|
{new Date(doc.updated_at).toLocaleString()}
|
|
|
|
|
|
</TableCell>
|
|
|
|
|
|
</TableRow>
|
|
|
|
|
|
))
|
|
|
|
|
|
)}
|
2025-02-15 23:22:37 +08:00
|
|
|
|
</TableBody>
|
|
|
|
|
|
</Table>
|
|
|
|
|
|
)}
|
|
|
|
|
|
</CardContent>
|
|
|
|
|
|
</Card>
|
|
|
|
|
|
</CardContent>
|
|
|
|
|
|
</Card>
|
|
|
|
|
|
)
|
|
|
|
|
|
}
|