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
@@ -3,6 +3,7 @@ import type {
ResolvedRuntimeSettings,
RuntimeSettingsStore
} from '../runtime-settings-store'
import { RetryableGraphExtractionError } from './graph-extractor'
import { createModelGraphExtractor } from './model-extractor'
function store(
@@ -168,4 +169,63 @@ describe('createModelGraphExtractor', () => {
await expect(extract('extract this')).rejects.toThrow('API Key')
})
it('classifies empty successful responses as retryable with diagnostics', async () => {
const extract = createModelGraphExtractor(
store({ modelProtocol: 'openai-chat-completions' }),
vi.fn(async () =>
jsonResponse({
choices: [{
finish_reason: 'length',
message: { content: '' }
}]
})
)
)
await expect(extract('extract this')).rejects.toMatchObject({
name: 'RetryableGraphExtractionError',
message: '模型未返回图谱内容(结束原因:length)'
})
})
it('classifies request timeouts as retryable', async () => {
const fetcher = vi.fn(
async (_input: Parameters<typeof fetch>[0], init?: RequestInit) =>
new Promise<Response>((_resolve, reject) => {
init?.signal?.addEventListener(
'abort',
() => reject(init.signal?.reason),
{ once: true }
)
})
)
const extract = createModelGraphExtractor(
store({ modelProtocol: 'openai-chat-completions' }),
fetcher,
5
)
const result = extract('extract this')
await expect(result).rejects.toBeInstanceOf(
RetryableGraphExtractionError
)
await expect(result).rejects.toThrow(
'模型图谱抽取响应超时'
)
})
it('preserves non-retryable HTTP status with a malformed error body', async () => {
const extract = createModelGraphExtractor(
store({ modelProtocol: 'openai-chat-completions' }),
vi.fn(async () =>
new Response('<html>unauthorized</html>', { status: 401 })
)
)
await expect(extract('extract this')).rejects.toMatchObject({
name: 'Error',
message: '模型图谱抽取失败(HTTP 401)'
})
})
})