fix(knowledge): stitch chunks by text overlap, not by position
Reconstructing full document content from chunks relied on position math (offset = content length - (EndAt - lastEndAt)) that assumes len([]rune(Content)) == EndAt - StartAt. Two upstream behaviors break that invariant and caused the merged "全文" view to drop or duplicate text: - The parent-child chunker prepends a synthetic table header to split tables; that header is zero-width in position space (start == end), so Content is longer than EndAt - StartAt. - Chunk Content can retain HTML entities (", > ...), inflating the rune count relative to the source span. Replace position-based trimming with text-overlap matching: find where the accumulated text's suffix reappears at the head of the next chunk and join there. Positions are only used to size the search window. This naturally skips the prepended header and is immune to entity length drift. Extract the logic into searchutil.AppendWithOverlap / MergeTextChunks and reuse it across the three backend stitching sites (wiki_ingest reconstructContent, graph mergeChunkContents, chat_pipeline mergeOverlappingChunks). Mirror the same algorithm in the frontend doc-content merge. Also fix the markdown hr/heading styles (thin solid divider, primary heading color).
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"encoding/json"
|
||||
"sort"
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/searchutil"
|
||||
"github.com/Tencent/WeKnora/internal/types"
|
||||
)
|
||||
|
||||
@@ -32,35 +33,14 @@ func (p *PluginMerge) mergeOverlappingChunks(
|
||||
|
||||
// Partial overlap: append the non-overlapping suffix.
|
||||
//
|
||||
// Offset math assumes len([]rune(Content)) == EndAt-StartAt, but a
|
||||
// few upstream paths break that invariant:
|
||||
// - Parent-child chunker prepends table headers, so Content may be
|
||||
// longer than EndAt-StartAt.
|
||||
// - Legacy data / mixed chunk sources may carry EndAt-StartAt that
|
||||
// exceed the actual rune length of Content.
|
||||
// We clamp offset into [0, len(contentRunes)] so the merge degrades
|
||||
// gracefully instead of panicking with a negative slice bound.
|
||||
// 重叠去重统一交给 searchutil.AppendWithOverlap(按文本匹配),它能
|
||||
// 兼容父子分块器补写的零宽表头,以及 HTML 实体导致的 content 长度与
|
||||
// EndAt-StartAt 不一致——这两种情况都会让按位置裁剪错位、丢字或重复。
|
||||
// StartAt/EndAt 仅用于估算搜索窗口大小。
|
||||
if chunks[i].EndAt > lastChunk.EndAt {
|
||||
contentRunes := []rune(chunks[i].Content)
|
||||
suffixLen := chunks[i].EndAt - lastChunk.EndAt
|
||||
offset := len(contentRunes) - suffixLen
|
||||
if offset < 0 || offset > len(contentRunes) {
|
||||
pipelineWarn(ctx, "Merge", "overlap_offset_clamp", map[string]interface{}{
|
||||
"knowledge_id": knowledgeID,
|
||||
"chunk_id": chunks[i].ID,
|
||||
"content_runes": len(contentRunes),
|
||||
"chunk_start": chunks[i].StartAt,
|
||||
"chunk_end": chunks[i].EndAt,
|
||||
"last_end": lastChunk.EndAt,
|
||||
"computed_offset": offset,
|
||||
})
|
||||
if offset < 0 {
|
||||
offset = 0
|
||||
} else {
|
||||
offset = len(contentRunes)
|
||||
}
|
||||
}
|
||||
lastChunk.Content = lastChunk.Content + string(contentRunes[offset:])
|
||||
lastChunk.Content = searchutil.AppendWithOverlap(
|
||||
lastChunk.Content, chunks[i].Content, lastChunk.EndAt-chunks[i].StartAt,
|
||||
)
|
||||
lastChunk.EndAt = chunks[i].EndAt
|
||||
lastChunk.SubChunkID = append(lastChunk.SubChunkID, chunks[i].ID)
|
||||
|
||||
|
||||
@@ -15,6 +15,7 @@ import (
|
||||
"github.com/Tencent/WeKnora/internal/logger"
|
||||
"github.com/Tencent/WeKnora/internal/models/chat"
|
||||
"github.com/Tencent/WeKnora/internal/models/utils"
|
||||
"github.com/Tencent/WeKnora/internal/searchutil"
|
||||
"github.com/Tencent/WeKnora/internal/types"
|
||||
"github.com/google/uuid"
|
||||
"golang.org/x/sync/errgroup"
|
||||
@@ -345,29 +346,9 @@ func (b *graphBuilder) findRelationChunkIDs(source, target string, entities []*t
|
||||
// mergeChunkContents merges content from multiple document chunks
|
||||
// It accounts for overlapping portions between chunks to ensure coherent content
|
||||
func (b *graphBuilder) mergeChunkContents(chunks []*types.Chunk) string {
|
||||
if len(chunks) == 0 {
|
||||
return ""
|
||||
}
|
||||
|
||||
chunkContents := chunks[0].Content
|
||||
preChunk := chunks[0]
|
||||
|
||||
for i := 1; i < len(chunks); i++ {
|
||||
// Only add non-overlapping content parts
|
||||
if preChunk.EndAt > chunks[i].StartAt {
|
||||
// Calculate overlap starting position
|
||||
startPos := preChunk.EndAt - chunks[i].StartAt
|
||||
if startPos >= 0 && startPos < len([]rune(chunks[i].Content)) {
|
||||
chunkContents = chunkContents + string([]rune(chunks[i].Content)[startPos:])
|
||||
}
|
||||
} else {
|
||||
// If there's no overlap between chunks, add all content
|
||||
chunkContents = chunkContents + chunks[i].Content
|
||||
}
|
||||
preChunk = chunks[i]
|
||||
}
|
||||
|
||||
return chunkContents
|
||||
// 重叠去重统一交给公共逻辑(按文本匹配,兼容补写表头 / HTML 实体)。
|
||||
// 无间隙分隔符,保持与原实现一致的直接拼接行为。
|
||||
return searchutil.MergeTextChunks(chunks, "")
|
||||
}
|
||||
|
||||
// BuildGraph constructs the knowledge graph
|
||||
|
||||
@@ -1878,47 +1878,8 @@ func reconstructContent(chunks []*types.Chunk) string {
|
||||
}
|
||||
}
|
||||
|
||||
// Sort by StartAt, then ChunkIndex
|
||||
sort.Slice(textChunks, func(i, j int) bool {
|
||||
if textChunks[i].StartAt == textChunks[j].StartAt {
|
||||
return textChunks[i].ChunkIndex < textChunks[j].ChunkIndex
|
||||
}
|
||||
return textChunks[i].StartAt < textChunks[j].StartAt
|
||||
})
|
||||
|
||||
var sb strings.Builder
|
||||
lastEndAt := -1
|
||||
for _, c := range textChunks {
|
||||
toAppend := c.Content
|
||||
|
||||
if c.StartAt > lastEndAt || c.EndAt == 0 {
|
||||
// Non-overlapping or missing position info
|
||||
if sb.Len() > 0 {
|
||||
sb.WriteString("\n")
|
||||
}
|
||||
sb.WriteString(toAppend)
|
||||
if c.EndAt > 0 {
|
||||
lastEndAt = c.EndAt
|
||||
}
|
||||
} else if c.EndAt > lastEndAt {
|
||||
// Partial overlap
|
||||
contentRunes := []rune(toAppend)
|
||||
offset := len(contentRunes) - (c.EndAt - lastEndAt)
|
||||
if offset >= 0 && offset < len(contentRunes) {
|
||||
sb.WriteString(string(contentRunes[offset:]))
|
||||
} else {
|
||||
// Fallback if offset calculation is invalid
|
||||
if sb.Len() > 0 {
|
||||
sb.WriteString("\n")
|
||||
}
|
||||
sb.WriteString(toAppend)
|
||||
}
|
||||
lastEndAt = c.EndAt
|
||||
}
|
||||
// If c.EndAt <= lastEndAt, it's fully contained, so skip appending text
|
||||
}
|
||||
|
||||
return sb.String()
|
||||
// 重叠去重与排序统一交给公共逻辑(按文本匹配,兼容补写表头 / HTML 实体)。
|
||||
return searchutil.MergeTextChunks(textChunks, "\n")
|
||||
}
|
||||
|
||||
// reconstructEnrichedContent rebuilds document text and inlines image_info
|
||||
|
||||
Reference in New Issue
Block a user