feat(chunker): recursive priority splitting + token-aware budget

The Go splitter previously combined all separators into one regex, losing
the priority-based recursive behavior of the Python reference. This pass
walks the separator list in order: the first separator that yields >1
piece wins, mirroring docreader/splitter/splitter.py. Larger semantic
boundaries (\n\n) now win over smaller ones (\n).

Additional cross-cutting changes:
- ChunkingConfig gains Strategy / TokenLimit / Languages fields so the
  per-KB config can pick a tier and pin embedding token budgets.
- buildSplitterConfig in the knowledge service uses the new
  chunker.DefaultChunkSize / DefaultChunkOverlap constants (overlap
  consolidated to 80 ≈ 15%) instead of the previous hardcoded 50.
- ensureDefaults clamps ChunkSize to a per-language character budget
  derived from TokenLimit so chunks stay safe under embedding model
  token caps. Chinese (~1.7 chars/tok) gets a tighter clamp than
  English (~4 chars/tok).

https://claude.ai/code/session_01XADhx6mtu2ZYW3DE9Lun6k
This commit is contained in:
Claude
2026-05-02 09:22:02 +00:00
committed by lyingbug
parent ae6fde2310
commit 1585a6f12c
5 changed files with 97 additions and 20 deletions
+26 -18
View File
@@ -122,33 +122,41 @@ type splitUnit struct {
start, end int
}
// splitBySeparators splits text by separators in priority order, keeping separators.
// splitBySeparators splits text by separators in priority order, keeping the
// separators themselves as standalone units. Walks the separator list in
// order: the first separator that actually splits the text wins, the rest
// are not applied. Mirrors the recursive priority semantics of the Python
// reference splitter (docreader/splitter/splitter.py:_split).
func splitBySeparators(text string, separators []string) []string {
if len(separators) == 0 || text == "" {
return []string{text}
}
// Build regex that captures separators
var parts []string
for _, sep := range separators {
parts = append(parts, regexp.QuoteMeta(sep))
}
pattern := "(" + strings.Join(parts, "|") + ")"
re := regexp.MustCompile(pattern)
splits := re.Split(text, -1)
matches := re.FindAllString(text, -1)
var result []string
for i, s := range splits {
if s != "" {
result = append(result, s)
if sep == "" {
continue
}
if i < len(matches) && matches[i] != "" {
result = append(result, matches[i])
re := regexp.MustCompile("(" + regexp.QuoteMeta(sep) + ")")
splits := re.Split(text, -1)
matches := re.FindAllString(text, -1)
if len(matches) == 0 {
continue
}
var result []string
for i, s := range splits {
if s != "" {
result = append(result, s)
}
if i < len(matches) && matches[i] != "" {
result = append(result, matches[i])
}
}
if len(result) > 1 {
return result
}
}
return result
return []string{text}
}
// runeLen returns the number of runes in s.