refactor(knowledge): improve folder path handling and add tests for underscore scenarios

- Updated SQL queries to properly escape LIKE wildcards in folder path filters.
- Added tests for renaming knowledge folder paths, including cases with underscores in paths.
- Enhanced existing tests to ensure correct folder listing behavior when using underscores.
main
wizardchen 2026-08-04 20:36:57 +08:00 committed by lyingbug
parent bb23444e39
commit 2f2be9f601
4 changed files with 64 additions and 14 deletions

View File

@ -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.Time
FolderPath *string
FolderRecursive 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.

View File

@ -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.Knowledge
if 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
}

View File

@ -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))
}

View File

@ -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)