fix(avatar): index knowledge documents asynchronously
This commit is contained in:
@@ -305,6 +305,7 @@ export interface KnowledgeDoc {
|
||||
vectorized?: boolean
|
||||
embeddingModel?: string
|
||||
chunkCount?: number
|
||||
errorMessage?: string
|
||||
createdAt: string
|
||||
}
|
||||
|
||||
@@ -335,7 +336,8 @@ export const uploadKnowledgeDoc = (avatarId: string, file: File) => {
|
||||
const form = new FormData()
|
||||
form.append('file', file)
|
||||
return request.post<KnowledgeDoc>(`/avatar/${avatarId}/knowledge/docs`, form, {
|
||||
headers: { 'Content-Type': 'multipart/form-data' }
|
||||
headers: { 'Content-Type': 'multipart/form-data' },
|
||||
timeout: 120000
|
||||
})
|
||||
}
|
||||
|
||||
@@ -343,6 +345,9 @@ export const uploadKnowledgeDoc = (avatarId: string, file: File) => {
|
||||
export const deleteKnowledgeDoc = (avatarId: string, docId: string) =>
|
||||
request.delete(`/avatar/${avatarId}/knowledge/docs/${docId}`)
|
||||
|
||||
export const retryKnowledgeDoc = (avatarId: string, docId: string) =>
|
||||
request.post<KnowledgeDoc>(`/avatar/${avatarId}/knowledge/docs/${docId}/retry`)
|
||||
|
||||
// 标准问答对列表
|
||||
export const getQAPairs = (avatarId: string) =>
|
||||
request.get<QAPair[]>(`/avatar/${avatarId}/knowledge/qa`)
|
||||
|
||||
@@ -27,7 +27,7 @@
|
||||
<p class="upload-hint">支持 MD / TXT / PDF / DOC / DOCX / XLSX,上传后自动向量化</p>
|
||||
<input ref="fileInput" type="file" accept=".md,.txt,.pdf,.doc,.docx,.xlsx" class="hidden-input" @change="onFileChange" />
|
||||
</div>
|
||||
<p v-if="uploading" class="uploading-text">上传并向量化中…</p>
|
||||
<p v-if="uploading" class="uploading-text">文件上传中…</p>
|
||||
<p v-if="uploadError" class="error-text">{{ uploadError }}</p>
|
||||
</div>
|
||||
|
||||
@@ -42,7 +42,10 @@
|
||||
<p class="card-meta">{{ doc.fileType.toUpperCase() }} · {{ formatSize(doc.fileSize) }} · {{ formatDate(doc.createdAt) }}</p>
|
||||
<p class="card-detail">{{ documentState(doc).detail }}</p>
|
||||
</div>
|
||||
<button class="card-delete" @click="removeDoc(doc.id)">删除</button>
|
||||
<div class="card-actions">
|
||||
<button v-if="documentState(doc).tone === 'failed'" class="card-retry" @click="retryDoc(doc.id)">重新索引</button>
|
||||
<button class="card-delete" @click="removeDoc(doc.id)">删除</button>
|
||||
</div>
|
||||
</article>
|
||||
</div>
|
||||
<div v-else class="card-empty">📂 暂无文档,先上传一个知识文件</div>
|
||||
@@ -78,7 +81,7 @@
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { ref, onMounted, computed } from 'vue'
|
||||
import { ref, onMounted, onUnmounted, computed } from 'vue'
|
||||
import { useRoute, useRouter } from 'vue-router'
|
||||
import { useAvatarStore } from '@/store/avatar'
|
||||
import { pickScopedAvatarId, unwrapListData } from '@/utils/avatar-page-data.js'
|
||||
@@ -87,6 +90,7 @@ import {
|
||||
getKnowledgeDocs,
|
||||
uploadKnowledgeDoc,
|
||||
deleteKnowledgeDoc,
|
||||
retryKnowledgeDoc,
|
||||
getQAPairs,
|
||||
deleteQAPair,
|
||||
searchKnowledge,
|
||||
@@ -107,6 +111,7 @@ const uploading = ref(false)
|
||||
const uploadError = ref('')
|
||||
const dragOver = ref(false)
|
||||
const fileInput = ref<HTMLInputElement | null>(null)
|
||||
let documentPollingTimer: ReturnType<typeof setInterval> | undefined
|
||||
|
||||
const query = ref('')
|
||||
const searching = ref(false)
|
||||
@@ -123,7 +128,26 @@ const documentState = (doc: any) => {
|
||||
if (['uploaded', 'parsing'].includes(String(doc.status || '').toLowerCase())) {
|
||||
return { tone: 'pending', label: '处理中', detail: '正在解析并建立知识索引' }
|
||||
}
|
||||
return { tone: 'failed', label: '处理失败', detail: '未能建立知识索引,请删除后重新上传' }
|
||||
return { tone: 'failed', label: '处理失败', detail: doc.errorMessage || '未能建立知识索引,请重新索引或重新上传' }
|
||||
}
|
||||
|
||||
const hasPendingDocuments = () => docs.value.some((doc) =>
|
||||
['uploaded', 'parsing'].includes(String(doc.status || '').toLowerCase())
|
||||
)
|
||||
|
||||
const stopDocumentPolling = () => {
|
||||
if (documentPollingTimer) {
|
||||
clearInterval(documentPollingTimer)
|
||||
documentPollingTimer = undefined
|
||||
}
|
||||
}
|
||||
|
||||
const startDocumentPolling = () => {
|
||||
if (documentPollingTimer || !hasPendingDocuments()) return
|
||||
documentPollingTimer = setInterval(async () => {
|
||||
await loadDocs()
|
||||
if (!hasPendingDocuments()) stopDocumentPolling()
|
||||
}, 2000)
|
||||
}
|
||||
|
||||
const loadDocs = async () => {
|
||||
@@ -131,6 +155,7 @@ const loadDocs = async () => {
|
||||
try {
|
||||
const res: any = await getKnowledgeDocs(avatarId.value)
|
||||
docs.value = unwrapListData(res)
|
||||
startDocumentPolling()
|
||||
} catch (e) {
|
||||
console.error(e)
|
||||
}
|
||||
@@ -182,6 +207,17 @@ const doUpload = async (file: File) => {
|
||||
}
|
||||
}
|
||||
|
||||
const retryDoc = async (id: string) => {
|
||||
if (!avatarId.value) return
|
||||
uploadError.value = ''
|
||||
try {
|
||||
await retryKnowledgeDoc(avatarId.value, id)
|
||||
await loadDocs()
|
||||
} catch (e: any) {
|
||||
uploadError.value = e?.message || '重新索引失败'
|
||||
}
|
||||
}
|
||||
|
||||
const removeDoc = async (id: string) => {
|
||||
if (!avatarId.value) return
|
||||
await deleteKnowledgeDoc(avatarId.value, id)
|
||||
@@ -257,6 +293,8 @@ onMounted(async () => {
|
||||
if (avatarId.value) store.currentAvatarId = avatarId.value
|
||||
await Promise.all([loadDocs(), loadQA()])
|
||||
})
|
||||
|
||||
onUnmounted(stopDocumentPolling)
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
@@ -300,7 +338,10 @@ onMounted(async () => {
|
||||
.status-pill.missing { color: #B91C1C; background: #FEF2F2; }
|
||||
.status-pill.failed { color: #B91C1C; background: #FEF2F2; }
|
||||
.card-meta, .card-detail { margin: 5px 0 0; color: #9398AE; font-size: 11px; line-height: 1.4; }.card-detail { color: #8B6B58; }
|
||||
.card-delete { flex: 0 0 auto; align-self: center; border: 0; color: #EF4444; background: #FEF2F2; border-radius: 8px; padding: 7px 9px; font-size: 12px; cursor: pointer; }
|
||||
.card-actions { flex: 0 0 auto; display: flex; flex-direction: column; align-items: stretch; gap: 6px; }
|
||||
.card-delete, .card-retry { align-self: center; border: 0; border-radius: 8px; padding: 7px 9px; font-size: 12px; cursor: pointer; white-space: nowrap; }
|
||||
.card-delete { color: #EF4444; background: #FEF2F2; }
|
||||
.card-retry { color: #C15F18; background: #FFF3E6; }
|
||||
.card-empty { padding: 42px 16px; border: 1px dashed #F1D9C3; border-radius: 16px; color: #9398AE; background: #fff; font-size: 14px; text-align: center; }
|
||||
.qa-card { align-items: stretch; text-align: left; }.qa-card.qa-disabled { opacity: .58; }
|
||||
.qa-card .card-content,
|
||||
|
||||
Reference in New Issue
Block a user