From 1985a59b697d7b8dafcc4ab5d40a5483d635069b Mon Sep 17 00:00:00 2001 From: wizardchen Date: Wed, 5 Aug 2026 15:05:13 +0800 Subject: [PATCH] fix(chunking): sync preview swagger and normalize splitter config Centralize ingestion base splitter defaults in chunker.NormalizeSplitterConfig so the preview endpoint applies the same normalization as upload. Regenerate Swagger for parent-child preview fields and add regression tests for single- level preview, default parent/child sizes, and context headers. --- docs/docs.go | 9 +++ docs/swagger.json | 9 +++ docs/swagger.yaml | 6 ++ .../application/service/knowledge_process.go | 14 +--- internal/handler/chunker_debug.go | 4 +- internal/handler/chunker_debug_test.go | 64 ++++++++++++++++++- internal/infrastructure/chunker/strategy.go | 15 +++++ .../infrastructure/chunker/strategy_test.go | 32 ++++++++++ 8 files changed, 137 insertions(+), 16 deletions(-) diff --git a/docs/docs.go b/docs/docs.go index 3f95953a..17288343 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -21472,18 +21472,27 @@ const docTemplate = `{ "internal_handler.PreviewChunkingPayload": { "type": "object", "properties": { + "child_chunk_size": { + "type": "integer" + }, "chunk_overlap": { "type": "integer" }, "chunk_size": { "type": "integer" }, + "enable_parent_child": { + "type": "boolean" + }, "languages": { "type": "array", "items": { "type": "string" } }, + "parent_chunk_size": { + "type": "integer" + }, "separators": { "type": "array", "items": { diff --git a/docs/swagger.json b/docs/swagger.json index e06b4298..4f61d92f 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -21465,18 +21465,27 @@ "internal_handler.PreviewChunkingPayload": { "type": "object", "properties": { + "child_chunk_size": { + "type": "integer" + }, "chunk_overlap": { "type": "integer" }, "chunk_size": { "type": "integer" }, + "enable_parent_child": { + "type": "boolean" + }, "languages": { "type": "array", "items": { "type": "string" } }, + "parent_chunk_size": { + "type": "integer" + }, "separators": { "type": "array", "items": { diff --git a/docs/swagger.yaml b/docs/swagger.yaml index d2d09ac1..69442dfd 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -5045,14 +5045,20 @@ definitions: type: object internal_handler.PreviewChunkingPayload: properties: + child_chunk_size: + type: integer chunk_overlap: type: integer chunk_size: type: integer + enable_parent_child: + type: boolean languages: items: type: string type: array + parent_chunk_size: + type: integer separators: items: type: string diff --git a/internal/application/service/knowledge_process.go b/internal/application/service/knowledge_process.go index 059b0e91..03114d2e 100644 --- a/internal/application/service/knowledge_process.go +++ b/internal/application/service/knowledge_process.go @@ -220,24 +220,14 @@ func buildSplitterConfig(kb *types.KnowledgeBase) chunker.SplitterConfig { } func buildSplitterConfigFromChunking(cc types.ChunkingConfig) chunker.SplitterConfig { - chunkCfg := chunker.SplitterConfig{ + return chunker.NormalizeSplitterConfig(chunker.SplitterConfig{ ChunkSize: cc.ChunkSize, ChunkOverlap: cc.ChunkOverlap, Separators: cc.Separators, Strategy: cc.Strategy, TokenLimit: cc.TokenLimit, Languages: cc.Languages, - } - if chunkCfg.ChunkSize <= 0 { - chunkCfg.ChunkSize = chunker.DefaultChunkSize - } - if chunkCfg.ChunkOverlap <= 0 { - chunkCfg.ChunkOverlap = chunker.DefaultChunkOverlap - } - if len(chunkCfg.Separators) == 0 { - chunkCfg.Separators = []string{"\n\n", "\n", "。"} - } - return chunkCfg + }) } // buildParentChildConfigs derives parent and child SplitterConfig from ChunkingConfig. diff --git a/internal/handler/chunker_debug.go b/internal/handler/chunker_debug.go index 1be0dee5..300f5a9b 100644 --- a/internal/handler/chunker_debug.go +++ b/internal/handler/chunker_debug.go @@ -147,14 +147,14 @@ func PreviewChunking(c *gin.Context) { return } - cfg := chunker.SplitterConfig{ + cfg := chunker.NormalizeSplitterConfig(chunker.SplitterConfig{ ChunkSize: req.ChunkingConfig.ChunkSize, ChunkOverlap: req.ChunkingConfig.ChunkOverlap, Separators: req.ChunkingConfig.Separators, Strategy: req.ChunkingConfig.Strategy, TokenLimit: req.ChunkingConfig.TokenLimit, Languages: req.ChunkingConfig.Languages, - } + }) // Run the splitter on a goroutine so we can honor the request timeout. // The splitter is CPU-bound and doesn't accept a context — wrapping diff --git a/internal/handler/chunker_debug_test.go b/internal/handler/chunker_debug_test.go index 5deb197c..ba11b0ed 100644 --- a/internal/handler/chunker_debug_test.go +++ b/internal/handler/chunker_debug_test.go @@ -214,12 +214,12 @@ func TestPreviewChunking_ParentChildMatchesIngestion(t *testing.T) { t.Fatalf("status %d body=%s", w.Code, w.Body.String()) } - base := chunker.SplitterConfig{ + base := chunker.NormalizeSplitterConfig(chunker.SplitterConfig{ ChunkSize: payload.ChunkSize, ChunkOverlap: payload.ChunkOverlap, Separators: payload.Separators, Strategy: payload.Strategy, - } + }) parentCfg, childCfg := chunker.DeriveParentChildConfigs(base, payload.ParentChunkSize, payload.ChildChunkSize) want := chunker.SplitParentChild(text, parentCfg, childCfg) @@ -236,5 +236,65 @@ func TestPreviewChunking_ParentChildMatchesIngestion(t *testing.T) { if int(previewChunk["start"].(float64)) != child.Start || int(previewChunk["end"].(float64)) != child.End { t.Errorf("chunk %d span: got %v-%v want %d-%d", i, previewChunk["start"], previewChunk["end"], child.Start, child.End) } + gotHeader, _ := previewChunk["context_header"].(string) + if gotHeader != child.ContextHeader { + t.Errorf("chunk %d context_header: got %q want %q", i, gotHeader, child.ContextHeader) + } + } +} + +func TestPreviewChunking_SingleLevelUnchanged(t *testing.T) { + text := strings.Repeat("Paragraph one.\n\nParagraph two.\n\n", 20) + payload := PreviewChunkingPayload{ + ChunkSize: 200, + ChunkOverlap: 20, + Separators: []string{"\n\n", "\n"}, + Strategy: chunker.StrategyLegacy, + } + w, parsed := postPreview(t, PreviewChunkingRequest{Text: text, ChunkingConfig: payload}) + if w.Code != http.StatusOK { + t.Fatalf("status %d body=%s", w.Code, w.Body.String()) + } + + want, _ := chunker.SplitWithDiagnostics(text, chunker.NormalizeSplitterConfig(chunker.SplitterConfig{ + ChunkSize: payload.ChunkSize, + ChunkOverlap: payload.ChunkOverlap, + Separators: payload.Separators, + Strategy: payload.Strategy, + })) + data := parsed["data"].(map[string]any) + got := data["chunks"].([]any) + if len(got) != len(want) { + t.Fatalf("chunk count: got %d want %d", len(got), len(want)) + } +} + +func TestPreviewChunking_ParentChildDefaultSizes(t *testing.T) { + text := strings.Repeat("## Record\n"+strings.Repeat("A sufficiently long entry body. ", 10)+"\n\n", 12) + payload := PreviewChunkingPayload{ + ChunkSize: 300, + ChunkOverlap: 30, + Separators: []string{"\n\n", "\n"}, + EnableParentChild: true, + Strategy: chunker.StrategyHeading, + } + w, parsed := postPreview(t, PreviewChunkingRequest{Text: text, ChunkingConfig: payload}) + if w.Code != http.StatusOK { + t.Fatalf("status %d body=%s", w.Code, w.Body.String()) + } + + base := chunker.NormalizeSplitterConfig(chunker.SplitterConfig{ + ChunkSize: payload.ChunkSize, + ChunkOverlap: payload.ChunkOverlap, + Separators: payload.Separators, + Strategy: payload.Strategy, + }) + parentCfg, childCfg := chunker.DeriveParentChildConfigs(base, 0, 0) + want := chunker.SplitParentChild(text, parentCfg, childCfg) + + data := parsed["data"].(map[string]any) + got := data["chunks"].([]any) + if len(got) != len(want.Children) { + t.Fatalf("chunk count: got %d want %d", len(got), len(want.Children)) } } diff --git a/internal/infrastructure/chunker/strategy.go b/internal/infrastructure/chunker/strategy.go index a3dba815..349e37af 100644 --- a/internal/infrastructure/chunker/strategy.go +++ b/internal/infrastructure/chunker/strategy.go @@ -185,6 +185,21 @@ func splitParentChild(text string, parentCfg, childCfg SplitterConfig, withDiagn return ParentChildResult{Parents: newParents, Children: children}, diag } +// NormalizeSplitterConfig applies the same base splitter defaults used by +// knowledge ingestion before parent-child derivation or single-pass splitting. +func NormalizeSplitterConfig(cfg SplitterConfig) SplitterConfig { + if cfg.ChunkSize <= 0 { + cfg.ChunkSize = DefaultChunkSize + } + if cfg.ChunkOverlap <= 0 { + cfg.ChunkOverlap = DefaultChunkOverlap + } + if len(cfg.Separators) == 0 { + cfg.Separators = []string{"\n\n", "\n", "。"} + } + return cfg +} + // DeriveParentChildConfigs produces the exact parent and child splitter // configurations used by knowledge ingestion. Keeping this here lets preview // and ingestion remain in lockstep as the parent-child defaults evolve. diff --git a/internal/infrastructure/chunker/strategy_test.go b/internal/infrastructure/chunker/strategy_test.go index 205bf276..b6742eb0 100644 --- a/internal/infrastructure/chunker/strategy_test.go +++ b/internal/infrastructure/chunker/strategy_test.go @@ -224,6 +224,38 @@ func TestEnsureDefaults(t *testing.T) { } } +func TestNormalizeSplitterConfig_MatchesIngestionDefaults(t *testing.T) { + cfg := NormalizeSplitterConfig(SplitterConfig{}) + if cfg.ChunkSize != DefaultChunkSize { + t.Errorf("chunk size: got %d want %d", cfg.ChunkSize, DefaultChunkSize) + } + if cfg.ChunkOverlap != DefaultChunkOverlap { + t.Errorf("chunk overlap: got %d want %d", cfg.ChunkOverlap, DefaultChunkOverlap) + } + if len(cfg.Separators) != 3 { + t.Fatalf("separators: got %v", cfg.Separators) + } +} + +func TestDeriveParentChildConfigs_DefaultSizes(t *testing.T) { + base := SplitterConfig{ + ChunkSize: 1000, + ChunkOverlap: 100, + Separators: []string{"\n\n", "\n"}, + Strategy: StrategyHeading, + } + parent, child := DeriveParentChildConfigs(base, 0, 0) + if parent.ChunkSize != 4096 || child.ChunkSize != 384 { + t.Fatalf("default sizes: parent=%d child=%d", parent.ChunkSize, child.ChunkSize) + } + if parent.Strategy != StrategyHeading || child.Strategy != StrategyHeading { + t.Fatalf("strategy not propagated: parent=%q child=%q", parent.Strategy, child.Strategy) + } + if child.ChunkOverlap != 384/5 { + t.Fatalf("child overlap: got %d want %d", child.ChunkOverlap, 384/5) + } +} + func TestValidateChunks_Empty(t *testing.T) { if v := ValidateChunks(nil, 1000, 500); v.OK { t.Error("nil chunks should be invalid")