From 92b6b7507d7455b587c1d5ce47510538d7af1303 Mon Sep 17 00:00:00 2001 From: wizardchen Date: Thu, 16 Apr 2026 18:28:34 +0800 Subject: [PATCH] refactor(chunk): improve parent-child chunk relationship handling in SplitTextParentChild - Updated the SplitTextParentChild function to maintain a newParents slice, ensuring accurate parent indexing when splitting text into child chunks. - This change enhances the integrity of parent-child relationships by correctly associating child chunks with their respective parents, improving overall chunk processing. --- internal/infrastructure/chunker/splitter.go | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/internal/infrastructure/chunker/splitter.go b/internal/infrastructure/chunker/splitter.go index a634f2c9..d110e22d 100644 --- a/internal/infrastructure/chunker/splitter.go +++ b/internal/infrastructure/chunker/splitter.go @@ -519,10 +519,18 @@ func SplitTextParentChild(text string, parentCfg, childCfg SplitterConfig) Paren return ParentChildResult{} } + var newParents []Chunk var children []ChildChunk childSeq := 0 - for pi, parent := range parents { + for _, parent := range parents { subs := SplitText(parent.Content, childCfg) + + parentIndex := -1 + if len(subs) > 1 || (len(subs) == 1 && subs[0].Content != parent.Content) { + parentIndex = len(newParents) + newParents = append(newParents, parent) + } + for _, sub := range subs { // Adjust offsets: sub positions are relative to parent content, // shift to document-level offsets. @@ -533,12 +541,12 @@ func SplitTextParentChild(text string, parentCfg, childCfg SplitterConfig) Paren sub.End += parent.Start children = append(children, ChildChunk{ Chunk: sub, - ParentIndex: pi, + ParentIndex: parentIndex, }) childSeq++ } } - return ParentChildResult{Parents: parents, Children: children} + return ParentChildResult{Parents: newParents, Children: children} } // ExtractImageRefs extracts markdown image references from text.