fix: stabilize knowledge graph extraction for long documents

Long document imports could send oversized graph prompts, surface empty model
responses without useful diagnostics, and fail the entire new document even
when its full-text index was usable. Graph extraction now uses bounded token
batches with retry and timeout handling, preserves successful partial results,
and reports graph failures independently from new-document indexing.

Chinese graph retrieval now recognizes bounded CJK query terms, while graph
search keeps one-hop relationship context visible around matched entities.

Release note: 修复长文档知识图谱抽取易因空响应或超时导致整篇文档导入失败的问题;现在图谱按 Token 分批重试,失败时仍保留新文档全文检索,并改进中文图谱问答和关系上下文展示。
This commit is contained in:
mesalogo
2026-08-25 08:03:15 +08:00
parent 098f21b47a
commit 88658aceaa
12 changed files with 679 additions and 64 deletions
+15 -8
View File
@@ -1237,7 +1237,7 @@ describe('KnowledgeWorkspace', () => {
)
})
it('renders and filters graph nodes with their relationships', async () => {
it('renders graph filters while preserving matched entity context', async () => {
render(<KnowledgeWorkspace {...createProps()} />)
fireEvent.click(screen.getByRole('tab', { name: '知识图谱' }))
@@ -1257,18 +1257,25 @@ describe('KnowledgeWorkspace', () => {
target: { value: 'Electron' }
})
expect(
screen.queryByRole('option', { name: 'GoodBuddy · 概念 (CONCEPT)' })
).not.toBeInTheDocument()
expect(screen.queryByText('使用 (USES)')).not.toBeInTheDocument()
screen.getByRole('option', { name: 'GoodBuddy · 概念 (CONCEPT)' })
).toBeInTheDocument()
expect(screen.getByText('使用 (USES)')).toBeInTheDocument()
expect(g6Mock.graph.setOptions).toHaveBeenLastCalledWith(
expect.objectContaining({
data: {
nodes: [
nodes: expect.arrayContaining([
expect.objectContaining({
id: 'entity-1'
}),
expect.objectContaining({
id: 'entity-2'
})
],
edges: []
]),
edges: [
expect.objectContaining({
id: 'relation-1'
})
]
}
})
)
@@ -1535,7 +1542,7 @@ describe('KnowledgeWorkspace', () => {
)
const renderCalls = g6Mock.graph.render.mock.calls.length
fireEvent.change(screen.getByLabelText('搜索图谱实体'), {
target: { value: 'Electron' }
target: { value: '不存在的实体' }
})
await waitFor(() =>
expect(g6Mock.graph.render.mock.calls.length).toBeGreaterThan(
+31 -6
View File
@@ -3422,16 +3422,41 @@ function GraphView({
)
const visibleNodes = useMemo(() => {
const normalized = query.trim().toLocaleLowerCase(locale)
return graphNodes.filter(
const typeFilteredNodes = graphNodes.filter(
(node) =>
(typeFilter === 'all' ||
normalizeEntityTypeAlias(node.type, ontology) === typeFilter) &&
(!normalized ||
typeFilter === 'all' ||
normalizeEntityTypeAlias(node.type, ontology) === typeFilter
)
if (!normalized) {
return typeFilteredNodes
}
const matchedIds = new Set(
typeFilteredNodes
.filter((node) =>
`${node.label} ${node.type} ${node.description ?? ''} ${(node.aliases ?? []).join(' ')}`
.toLocaleLowerCase(locale)
.includes(normalized))
.includes(normalized)
)
.map((node) => node.id)
)
}, [graphNodes, locale, ontology, query, typeFilter])
const visibleIds = new Set(matchedIds)
for (const relation of graphRelations) {
if (matchedIds.has(relation.sourceId)) {
visibleIds.add(relation.targetId)
}
if (matchedIds.has(relation.targetId)) {
visibleIds.add(relation.sourceId)
}
}
return typeFilteredNodes.filter((node) => visibleIds.has(node.id))
}, [
graphNodes,
graphRelations,
locale,
ontology,
query,
typeFilter
])
const visibleIds = useMemo(
() => new Set(visibleNodes.map((node) => node.id)),
[visibleNodes]