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
refactor(knowledge): improve folder path handling and add tests for underscore scenarios
wizardchen2f2be9f60158
diff --git a/client/knowledge.go b/client/knowledge.goindex 8a96ce462..1c6efe16d 100644--- a/client/knowledge.go+++ b/client/knowledge.go@@ -87,14 +87,14 @@ var ErrDuplicateURL = errors.New("URL already exists")// KnowledgeProcessOverrides stores per-upload parse config overrides sent as process_config.// When nil, the server uses the knowledge base defaults only.type KnowledgeProcessOverrides struct {- ParserEngineRules []ParserEngineRule `json:"parser_engine_rules,omitempty"`- ChunkingConfig *ChunkingConfig `json:"chunking_config,omitempty"`- EnableMultimodel *bool `json:"enable_multimodel,omitempty"`- VLMConfig *VLMConfig `json:"vlm_config,omitempty"`- ASRConfig *ASRConfig `json:"asr_config,omitempty"`- QuestionGenerationConfig *QuestionGenerationConfig `json:"question_generation_config,omitempty"`- GraphEnabled *bool `json:"graph_enabled,omitempty"`- ExtractConfig *ExtractConfig `json:"extract_config,omitempty"`+ ParserEngineRules []ParserEngineRule `json:"parser_engine_rules,omitempty"`+ ChunkingConfig *ChunkingConfig `json:"chunking_config,omitempty"`+ EnableMultimodel *bool `json:"enable_multimodel,omitempty"`+ VLMConfig *VLMConfig `json:"vlm_config,omitempty"`+ ASRConfig *ASRConfig `json:"asr_config,omitempty"`+ QuestionGenerationConfig *QuestionGenerationConfig `json:"question_generation_config,omitempty"`+ GraphEnabled *bool `json:"graph_enabled,omitempty"`+ ExtractConfig *ExtractConfig `json:"extract_config,omitempty"`}// CreateKnowledgeFromFile creates a knowledge entry from a local file path@@ -332,8 +332,8 @@ type KnowledgeListFilter struct {Source string// StartTime / EndTime filter on knowledge updated_at. Zero values are skipped.// They are serialized in RFC3339 format.- StartTime time.Time- EndTime time.Time+ StartTime time.Time+ EndTime time.TimeFolderPath *stringFolderRecursive bool}@@ -637,7 +637,7 @@ func (c *Client) ReparseKnowledge(ctx context.Context, knowledgeID string) (*Kno// - pending — task has not started// - processing — DocReader / chunking / embedding stage// - finalizing — primary parse done, enrichment subtasks (summary,-// question generation, graph extract) still running+// question generation, graph extract) still running//// Returns an error when the knowledge is in a terminal state// (completed, failed) or already being deleted.diff --git a/internal/application/repository/knowledge.go b/internal/application/repository/knowledge.goindex 121162d39..c2989e8f2 100644--- a/internal/application/repository/knowledge.go+++ b/internal/application/repository/knowledge.go@@ -13,6 +13,9 @@ import (var ErrKnowledgeNotFound = errors.New("knowledge not found")+// likeEscapeChar is the SQL ESCAPE character paired with escapeLikeKeyword.+const likeEscapeChar = `\`+// escapeLikeKeyword escapes SQL LIKE wildcards (%, _) in a keyword// so they are treated as literal characters.func escapeLikeKeyword(keyword string) string {@@ -158,9 +161,10 @@ func applyKnowledgeListFilter(query *gorm.DB, filter types.KnowledgeListFilter)// needed; otherwise match the folder itself plus everything below it.if filter.FolderPath != "" {query = query.Where(- "(folder_path = ? OR folder_path LIKE ?)",+ "(folder_path = ? OR folder_path LIKE ? ESCAPE ?)",filter.FolderPath,escapeLikeKeyword(filter.FolderPath)+"/%",+ likeEscapeChar,)}}@@ -264,8 +268,8 @@ func (r *knowledgeRepository) RenameKnowledgeFolderPath(var rows []*types.Knowledgeif err := r.db.WithContext(ctx).Select("id", "folder_path").- Where("tenant_id = ? AND knowledge_base_id = ? AND (folder_path = ? OR folder_path LIKE ?)",- tenantID, kbID, from, escapeLikeKeyword(from)+"/%").+ Where("tenant_id = ? AND knowledge_base_id = ? AND (folder_path = ? OR folder_path LIKE ? ESCAPE ?)",+ tenantID, kbID, from, escapeLikeKeyword(from)+"/%", likeEscapeChar).Find(&rows).Error; err != nil {return 0, err}diff --git a/internal/application/repository/knowledge_folder_move_test.go b/internal/application/repository/knowledge_folder_move_test.goindex 21a565144..a3f1ee73f 100644--- a/internal/application/repository/knowledge_folder_move_test.go+++ b/internal/application/repository/knowledge_folder_move_test.go@@ -95,3 +95,21 @@ func TestRenameKnowledgeFolderPath(t *testing.T) {_, err = repo.RenameKnowledgeFolderPath(ctx, tenantID, kbID, "", "whatever")assert.Error(t, err, "renaming the root itself is meaningless and must be rejected")}++func TestRenameKnowledgeFolderPathUnderscoreInPath(t *testing.T) {+ db := setupKnowledgeTestDB(t)+ repo := NewKnowledgeRepository(db).(*knowledgeRepository)+ ctx := context.Background()++ const tenantID = uint64(1)+ kbID := uuid.New().String()++ parent := insertKnowledgeInFolder(t, db, tenantID, kbID, "my_docs", "intro.md")+ child := insertKnowledgeInFolder(t, db, tenantID, kbID, "my_docs/spec", "design.md")++ affected, err := repo.RenameKnowledgeFolderPath(ctx, tenantID, kbID, "my_docs", "handbook")+ require.NoError(t, err)+ assert.Equal(t, int64(2), affected)+ assert.Equal(t, "handbook", folderPathOf(t, db, parent))+ assert.Equal(t, "handbook/spec", folderPathOf(t, db, child))+}diff --git a/internal/application/repository/knowledge_folder_test.go b/internal/application/repository/knowledge_folder_test.goindex b41112446..874bdb5d9 100644--- a/internal/application/repository/knowledge_folder_test.go+++ b/internal/application/repository/knowledge_folder_test.go@@ -79,6 +79,34 @@ func TestListPagedKnowledgeFolderScope(t *testing.T) {assert.Equal(t, []string{specID}, ids)}+func TestListPagedKnowledgeFolderScopeUnderscoreInPath(t *testing.T) {+ db := setupKnowledgeTestDB(t)+ repo := NewKnowledgeRepository(db).(*knowledgeRepository)+ ctx := context.Background()++ const tenantID = uint64(1)+ kbID := uuid.New().String()++ parentID := insertKnowledgeInFolder(t, db, tenantID, kbID, "my_docs", "intro.md")+ childID := insertKnowledgeInFolder(t, db, tenantID, kbID, "my_docs/spec", "design.md")+ // A sibling whose name merely shares a prefix must not be swept into subtree scope.+ lookalikeID := insertKnowledgeInFolder(t, db, tenantID, kbID, "myXdocs/spec", "other.md")++ page := &types.Pagination{Page: 1, PageSize: 100}+ rows, total, err := repo.ListPagedKnowledgeByKnowledgeBaseID(ctx, tenantID, kbID, page, types.KnowledgeListFilter{+ FolderPath: "my_docs",+ FolderScope: types.FolderScopeSubtree,+ })+ require.NoError(t, err)+ ids := make([]string, 0, len(rows))+ for _, row := range rows {+ ids = append(ids, row.ID)+ }+ assert.ElementsMatch(t, []string{parentID, childID}, ids)+ assert.Equal(t, int64(2), total)+ assert.NotContains(t, ids, lookalikeID)+}+func TestListKnowledgeFolderCounts(t *testing.T) {db := setupKnowledgeTestDB(t)repo := NewKnowledgeRepository(db).(*knowledgeRepository)