Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.
Go agentagenticaichatbotdsh-pluginembeddingsevaluationgenerative-aigolangknowledge-basellmmulti-tenantollamaopenaiquestion-answeringragrerankingsemantic-searchvector-searchwiki
fix(chunking): sync preview swagger and normalize splitter config
wizardchen1985a59b697d
diff --git a/docs/docs.go b/docs/docs.goindex 3f95953a9..17288343b 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.jsonindex e06b42980..4f61d92f3 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.yamlindex d2d09ac1c..69442dfd1 100644--- a/docs/swagger.yaml+++ b/docs/swagger.yaml@@ -5045,14 +5045,20 @@ definitions:type: objectinternal_handler.PreviewChunkingPayload:properties:+ child_chunk_size:+ type: integerchunk_overlap:type: integerchunk_size:type: integer+ enable_parent_child:+ type: booleanlanguages:items:type: stringtype: array+ parent_chunk_size:+ type: integerseparators:items:type: stringdiff --git a/internal/application/service/knowledge_process.go b/internal/application/service/knowledge_process.goindex 059b0e91f..03114d2eb 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.goindex 1be0dee5e..300f5a9b1 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 — wrappingdiff --git a/internal/handler/chunker_debug_test.go b/internal/handler/chunker_debug_test.goindex 5deb197ca..ba11b0eda 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.goindex a3dba8150..349e37af6 100644--- a/internal/infrastructure/chunker/strategy.go+++ b/internal/infrastructure/chunker/strategy.go@@ -185,6 +185,21 @@ func splitParentChild(text string, parentCfg, childCfg SplitterConfig, withDiagnreturn 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.goindex 205bf2763..b6742eb06 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")