finish document manager

This commit is contained in:
ArnoChen
2025-02-17 01:05:31 +08:00
parent be4cdedfa1
commit c2a7079392
4 changed files with 104 additions and 90 deletions

View File

@@ -27,8 +27,6 @@ export type LightragStatus = {
status: 'healthy' status: 'healthy'
working_directory: string working_directory: string
input_directory: string input_directory: string
indexed_files: string[]
indexed_files_count: number
configuration: { configuration: {
llm_binding: string llm_binding: string
llm_binding_host: string llm_binding_host: string
@@ -104,11 +102,29 @@ export type QueryResponse = {
response: string response: string
} }
export type DocumentActionResponse = { export type DocActionResponse = {
status: 'success' | 'partial_success' | 'failure' status: 'success' | 'partial_success' | 'failure'
message: string message: string
} }
export type DocStatus = 'pending' | 'processing' | 'processed' | 'failed'
export type DocStatusResponse = {
id: string
content_summary: string
content_length: number
status: DocStatus
created_at: string
updated_at: string
chunks_count?: number
error?: string
metadata?: Record<string, any>
}
export type DocsStatusesResponse = {
statuses: Record<DocStatus, DocStatusResponse[]>
}
export const InvalidApiKeyError = 'Invalid API Key' export const InvalidApiKeyError = 'Invalid API Key'
export const RequireApiKeError = 'API Key required' export const RequireApiKeError = 'API Key required'
@@ -169,7 +185,7 @@ export const checkHealth = async (): Promise<
} }
} }
export const getDocuments = async (): Promise<string[]> => { export const getDocuments = async (): Promise<DocsStatusesResponse> => {
const response = await axiosInstance.get('/documents') const response = await axiosInstance.get('/documents')
return response.data return response.data
} }
@@ -253,7 +269,7 @@ export const queryTextStream = async (
export const insertText = async ( export const insertText = async (
text: string, text: string,
description?: string description?: string
): Promise<DocumentActionResponse> => { ): Promise<DocActionResponse> => {
const response = await axiosInstance.post('/documents/text', { text, description }) const response = await axiosInstance.post('/documents/text', { text, description })
return response.data return response.data
} }
@@ -261,7 +277,7 @@ export const insertText = async (
export const uploadDocument = async ( export const uploadDocument = async (
file: File, file: File,
onUploadProgress?: (percentCompleted: number) => void onUploadProgress?: (percentCompleted: number) => void
): Promise<DocumentActionResponse> => { ): Promise<DocActionResponse> => {
const formData = new FormData() const formData = new FormData()
formData.append('file', file) formData.append('file', file)
@@ -284,7 +300,7 @@ export const uploadDocument = async (
export const batchUploadDocuments = async ( export const batchUploadDocuments = async (
files: File[], files: File[],
onUploadProgress?: (fileName: string, percentCompleted: number) => void onUploadProgress?: (fileName: string, percentCompleted: number) => void
): Promise<DocumentActionResponse[]> => { ): Promise<DocActionResponse[]> => {
return await Promise.all( return await Promise.all(
files.map(async (file) => { files.map(async (file) => {
return await uploadDocument(file, (percentCompleted) => { return await uploadDocument(file, (percentCompleted) => {
@@ -294,7 +310,7 @@ export const batchUploadDocuments = async (
) )
} }
export const clearDocuments = async (): Promise<DocumentActionResponse> => { export const clearDocuments = async (): Promise<DocActionResponse> => {
const response = await axiosInstance.delete('/documents') const response = await axiosInstance.delete('/documents')
return response.data return response.data
} }

View File

@@ -14,8 +14,6 @@ const StatusCard = ({ status }: { status: LightragStatus | null }) => {
<span className="truncate">{status.working_directory}</span> <span className="truncate">{status.working_directory}</span>
<span>Input Directory:</span> <span>Input Directory:</span>
<span className="truncate">{status.input_directory}</span> <span className="truncate">{status.input_directory}</span>
<span>Indexed Files:</span>
<span>{status.indexed_files_count}</span>
</div> </div>
</div> </div>

View File

@@ -49,10 +49,10 @@ export default function UploadDocumentsDialog() {
toast.error('Upload Failed\n' + errorMessage(err)) toast.error('Upload Failed\n' + errorMessage(err))
} finally { } finally {
setIsUploading(false) setIsUploading(false)
setOpen(false) // setOpen(false)
} }
}, },
[setIsUploading, setProgresses, setOpen] [setIsUploading, setProgresses]
) )
return ( return (
@@ -66,7 +66,7 @@ export default function UploadDocumentsDialog() {
}} }}
> >
<DialogTrigger asChild> <DialogTrigger asChild>
<Button variant="default" side="bottom" tooltip='Upload documents' size="sm"> <Button variant="default" side="bottom" tooltip="Upload documents" size="sm">
<UploadIcon /> Upload <UploadIcon /> Upload
</Button> </Button>
</DialogTrigger> </DialogTrigger>

View File

@@ -9,39 +9,35 @@ import {
TableRow TableRow
} from '@/components/ui/Table' } from '@/components/ui/Table'
import { Card, CardHeader, CardTitle, CardContent, CardDescription } from '@/components/ui/Card' import { Card, CardHeader, CardTitle, CardContent, CardDescription } from '@/components/ui/Card'
import Progress from '@/components/ui/Progress'
import EmptyCard from '@/components/ui/EmptyCard' import EmptyCard from '@/components/ui/EmptyCard'
import Text from '@/components/ui/Text'
import UploadDocumentsDialog from '@/components/documents/UploadDocumentsDialog' import UploadDocumentsDialog from '@/components/documents/UploadDocumentsDialog'
import ClearDocumentsDialog from '@/components/documents/ClearDocumentsDialog' import ClearDocumentsDialog from '@/components/documents/ClearDocumentsDialog'
import { import { getDocuments, scanNewDocuments, DocsStatusesResponse } from '@/api/lightrag'
getDocuments,
// getDocumentsScanProgress,
scanNewDocuments
// LightragDocumentsScanProgress
} from '@/api/lightrag'
import { errorMessage } from '@/lib/utils' import { errorMessage } from '@/lib/utils'
import { toast } from 'sonner' import { toast } from 'sonner'
// import { useBackendState } from '@/stores/state' import { useBackendState } from '@/stores/state'
import { RefreshCwIcon, TrashIcon } from 'lucide-react' import { RefreshCwIcon } from 'lucide-react'
// type DocumentStatus = 'indexed' | 'pending' | 'indexing' | 'error'
export default function DocumentManager() { export default function DocumentManager() {
// const health = useBackendState.use.health() const health = useBackendState.use.health()
const [files, setFiles] = useState<string[]>([]) const [docs, setDocs] = useState<DocsStatusesResponse | null>(null)
const [indexedFiles, setIndexedFiles] = useState<string[]>([])
// const [scanProgress, setScanProgress] = useState<LightragDocumentsScanProgress | null>(null)
const fetchDocuments = useCallback(async () => { const fetchDocuments = useCallback(async () => {
try { try {
const docs = await getDocuments() const docs = await getDocuments()
setFiles(docs) if (docs && docs.statuses) {
setDocs(docs)
// console.log(docs)
} else {
setDocs(null)
}
} catch (err) { } catch (err) {
toast.error('Failed to load documents\n' + errorMessage(err)) toast.error('Failed to load documents\n' + errorMessage(err))
} }
}, [setFiles]) }, [setDocs])
useEffect(() => { useEffect(() => {
fetchDocuments() fetchDocuments()
@@ -56,28 +52,19 @@ export default function DocumentManager() {
} }
}, []) }, [])
// useEffect(() => { useEffect(() => {
// const interval = setInterval(async () => { const interval = setInterval(async () => {
// try { if (!health) {
// if (!health) return return
// const progress = await getDocumentsScanProgress() }
// setScanProgress((pre) => { try {
// if (pre?.is_scanning === progress.is_scanning && progress.is_scanning === false) { await fetchDocuments()
// return pre } catch (err) {
// } toast.error('Failed to get scan progress\n' + errorMessage(err))
// return progress }
// }) }, 5000)
// console.log(progress) return () => clearInterval(interval)
// } catch (err) { }, [health, fetchDocuments])
// toast.error('Failed to get scan progress\n' + errorMessage(err))
// }
// }, 2000)
// return () => clearInterval(interval)
// }, [health])
const handleDelete = async (fileName: string) => {
console.log(`deleting ${fileName}`)
}
return ( return (
<Card className="!size-full !rounded-none !border-none"> <Card className="!size-full !rounded-none !border-none">
@@ -100,16 +87,6 @@ export default function DocumentManager() {
<UploadDocumentsDialog /> <UploadDocumentsDialog />
</div> </div>
{/* {scanProgress?.is_scanning && (
<div className="space-y-2">
<div className="flex justify-between text-sm">
<span>Indexing {scanProgress.current_file}</span>
<span>{scanProgress.progress}%</span>
</div>
<Progress value={scanProgress.progress} />
</div>
)} */}
<Card> <Card>
<CardHeader> <CardHeader>
<CardTitle>Uploaded documents</CardTitle> <CardTitle>Uploaded documents</CardTitle>
@@ -117,44 +94,67 @@ export default function DocumentManager() {
</CardHeader> </CardHeader>
<CardContent> <CardContent>
{files.length == 0 && ( {!docs && (
<EmptyCard <EmptyCard
title="No documents uploades" title="No documents uploaded"
description="upload documents to see them here" description="upload documents to see them here"
/> />
)} )}
{files.length > 0 && ( {docs && (
<Table> <Table>
<TableHeader> <TableHeader>
<TableRow> <TableRow>
<TableHead>Filename</TableHead> <TableHead>ID</TableHead>
<TableHead>Summary</TableHead>
<TableHead>Status</TableHead> <TableHead>Status</TableHead>
<TableHead>Actions</TableHead> <TableHead>Length</TableHead>
<TableHead>Chunks</TableHead>
<TableHead>Created</TableHead>
<TableHead>Updated</TableHead>
<TableHead>Metadata</TableHead>
</TableRow> </TableRow>
</TableHeader> </TableHeader>
<TableBody> <TableBody className="text-sm">
{files.map((file) => ( {Object.entries(docs.statuses).map(([status, documents]) =>
<TableRow key={file}> documents.map((doc) => (
<TableCell>{file}</TableCell> <TableRow key={doc.id}>
<TableCell> <TableCell className="truncate font-mono">{doc.id}</TableCell>
{indexedFiles.includes(file) ? ( <TableCell className="max-w-xs min-w-24 truncate">
<span className="text-green-600">Indexed</span> <Text
) : ( text={doc.content_summary}
<span className="text-yellow-600">Pending</span> tooltip={doc.content_summary}
)} tooltipClassName="max-w-none overflow-visible block"
</TableCell> />
<TableCell> </TableCell>
<Button <TableCell>
variant="ghost" {status === 'processed' && (
size="sm" <span className="text-green-600">Completed</span>
onClick={() => handleDelete(file)} )}
// disabled={isUploading} {status === 'processing' && (
> <span className="text-blue-600">Processing</span>
<TrashIcon /> )}
</Button> {status === 'pending' && <span className="text-yellow-600">Pending</span>}
</TableCell> {status === 'failed' && <span className="text-red-600">Failed</span>}
</TableRow> {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>
<TableCell className="max-w-xs truncate">
{doc.metadata ? JSON.stringify(doc.metadata) : '-'}
</TableCell>
</TableRow>
))
)}
</TableBody> </TableBody>
</Table> </Table>
)} )}