mirror of
https://github.com/JefferyHcool/BiliNote.git
synced 2026-05-06 20:42:52 +08:00
fix(chat): 索引改为后台异步执行,前端轮询状态并展示进度提示
后端: - /chat/index 改为 BackgroundTasks 异步执行,立即返回 - /chat/status 返回细粒度状态(idle/indexing/indexed/failed) - 内存追踪索引进度,避免重复触发 前端: - ChatPanel 每 2 秒轮询索引状态,索引完成后自动停止 - 索引中显示"正在索引笔记内容..."及首次下载模型提示 - 索引失败显示重试按钮 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -7,7 +7,7 @@ import { Loader2, Trash2, ChevronDown, ChevronUp, BookOpen, UserRound, Bot } fro
|
||||
import { toast } from 'react-hot-toast'
|
||||
import { useChatStore } from '@/store/chatStore'
|
||||
import { useTaskStore } from '@/store/taskStore'
|
||||
import { askQuestion, getChatStatus, indexTask, type ChatSource } from '@/services/chat'
|
||||
import { askQuestion, getChatStatus, indexTask, type ChatSource, type IndexStatus } from '@/services/chat'
|
||||
|
||||
interface ChatPanelProps {
|
||||
taskId: string
|
||||
@@ -46,8 +46,7 @@ function SourceBadges({ sources }: { sources: ChatSource[] }) {
|
||||
export default function ChatPanel({ taskId }: ChatPanelProps) {
|
||||
const [input, setInput] = useState('')
|
||||
const [loading, setLoading] = useState(false)
|
||||
const [indexing, setIndexing] = useState(false)
|
||||
const [indexed, setIndexed] = useState<boolean | null>(null)
|
||||
const [indexStatus, setIndexStatus] = useState<IndexStatus | null>(null)
|
||||
|
||||
const messages = useChatStore(state => state.chatHistory[taskId]) ?? []
|
||||
const addMessage = useChatStore(state => state.addMessage)
|
||||
@@ -60,36 +59,37 @@ export default function ChatPanel({ taskId }: ChatPanelProps) {
|
||||
[tasks, currentTaskId],
|
||||
)
|
||||
|
||||
// 检查索引状态
|
||||
// 检查索引状态,未索引时自动触发,indexing 时轮询
|
||||
useEffect(() => {
|
||||
if (!taskId) return
|
||||
let cancelled = false
|
||||
let timer: ReturnType<typeof setTimeout> | null = null
|
||||
|
||||
const check = async () => {
|
||||
const poll = async () => {
|
||||
try {
|
||||
const res = await getChatStatus(taskId)
|
||||
if (cancelled) return
|
||||
if (res.indexed) {
|
||||
setIndexed(true)
|
||||
} else {
|
||||
setIndexing(true)
|
||||
setIndexStatus(res.status)
|
||||
|
||||
if (res.status === 'idle') {
|
||||
// 未索引,触发后台索引
|
||||
await indexTask(taskId)
|
||||
if (!cancelled) {
|
||||
setIndexed(true)
|
||||
setIndexing(false)
|
||||
}
|
||||
if (!cancelled) setIndexStatus('indexing')
|
||||
}
|
||||
|
||||
// indexing 状态持续轮询
|
||||
if (res.status === 'indexing' || res.status === 'idle') {
|
||||
timer = setTimeout(poll, 2000)
|
||||
}
|
||||
} catch {
|
||||
if (!cancelled) {
|
||||
setIndexed(false)
|
||||
setIndexing(false)
|
||||
}
|
||||
if (!cancelled) setIndexStatus('failed')
|
||||
}
|
||||
}
|
||||
|
||||
check()
|
||||
poll()
|
||||
return () => {
|
||||
cancelled = true
|
||||
if (timer) clearTimeout(timer)
|
||||
}
|
||||
}, [taskId])
|
||||
|
||||
@@ -178,16 +178,19 @@ export default function ChatPanel({ taskId }: ChatPanelProps) {
|
||||
[],
|
||||
)
|
||||
|
||||
if (indexing) {
|
||||
if (indexStatus === null || indexStatus === 'indexing' || indexStatus === 'idle') {
|
||||
return (
|
||||
<div className="flex h-full flex-col items-center justify-center gap-2 text-neutral-400">
|
||||
<Loader2 className="h-5 w-5 animate-spin" />
|
||||
<span className="text-sm">正在索引笔记内容...</span>
|
||||
<div className="flex h-full flex-col items-center justify-center gap-3 text-neutral-400">
|
||||
<Loader2 className="h-6 w-6 animate-spin" />
|
||||
<div className="text-center">
|
||||
<p className="text-sm font-medium">正在索引笔记内容...</p>
|
||||
<p className="mt-1 text-xs">首次使用需下载 Embedding 模型(约 80MB),请耐心等待</p>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
if (indexed === false) {
|
||||
if (indexStatus === 'failed') {
|
||||
return (
|
||||
<div className="flex h-full flex-col items-center justify-center gap-2 text-neutral-400">
|
||||
<span className="text-sm">索引失败,请重试</span>
|
||||
@@ -195,14 +198,12 @@ export default function ChatPanel({ taskId }: ChatPanelProps) {
|
||||
size="sm"
|
||||
variant="outline"
|
||||
onClick={async () => {
|
||||
setIndexing(true)
|
||||
setIndexStatus('indexing')
|
||||
try {
|
||||
await indexTask(taskId)
|
||||
setIndexed(true)
|
||||
} catch {
|
||||
toast.error('索引失败')
|
||||
} finally {
|
||||
setIndexing(false)
|
||||
toast.error('索引请求失败')
|
||||
setIndexStatus('failed')
|
||||
}
|
||||
}}
|
||||
>
|
||||
|
||||
@@ -18,8 +18,11 @@ export interface AskResponse {
|
||||
sources: ChatSource[]
|
||||
}
|
||||
|
||||
export type IndexStatus = 'idle' | 'indexing' | 'indexed' | 'failed'
|
||||
|
||||
export interface ChatStatusResponse {
|
||||
indexed: boolean
|
||||
status: IndexStatus
|
||||
}
|
||||
|
||||
export const indexTask = async (taskId: string): Promise<void> => {
|
||||
|
||||
Reference in New Issue
Block a user