feat(wiki): implement folder management features in wiki API

Add new API functions for managing wiki folders, including creating, updating, deleting, and listing folders. Introduce a new WikiFolder interface to represent folder structure and enhance the WikiBrowser component with folder actions. Update internationalization strings for folder-related actions in English, Korean, Russian, and Chinese locales. Refactor existing code to accommodate folder hierarchy and improve user experience in the wiki browser.
This commit is contained in:
wizardchen
2026-06-16 23:48:35 +08:00
committed by lyingbug
parent 83eeec0ddf
commit e76738d054
24 changed files with 1903 additions and 399 deletions
+157 -106
View File
@@ -109,10 +109,8 @@ func (r *wikiPageRepository) UpdateMeta(ctx context.Context, page *types.WikiPag
"chunk_refs": page.ChunkRefs,
"page_metadata": page.PageMetadata,
"parent_slug": page.ParentSlug,
"folder_id": page.FolderID,
"category_path": page.CategoryPath,
"category_l1": page.CategoryL1,
"category_l2": page.CategoryL2,
"category_l3": page.CategoryL3,
"wiki_path": page.WikiPath,
"depth": page.Depth,
"sort_order": page.SortOrder,
@@ -180,6 +178,9 @@ func (r *wikiPageRepository) List(ctx context.Context, req *types.WikiPageListRe
// whose stored text is json.Marshal of the cleaned path, so we compare
// against the same encoding. Postgres needs an explicit jsonb cast for array
// equality; SQLite stores JSON as TEXT and compares directly.
if req.FolderID != nil {
query = query.Where("folder_id = ?", *req.FolderID)
}
if req.CategoryDepth != nil {
query = query.Where("depth = ?", *req.CategoryDepth)
}
@@ -414,13 +415,11 @@ func (r *wikiPageRepository) ListBySlugs(
return out, nil
}
// ListDistinctCategoryPaths returns the distinct non-empty category paths used
// by entity/concept pages. The distinctness is pushed entirely to SQL over the
// flat category_l1/l2/l3 columns (indexed by idx_wiki_pages_category_levels),
// so the cost scales with the number of distinct folders — not the page count —
// and there is no full-table scan to bound. maxPaths caps the returned folder
// count. Used by the batch taxonomy planner as the candidate pool of existing
// folders to reuse (similarity preprocessing then narrows it per batch).
// ListDistinctCategoryPaths returns the materialized paths of existing wiki
// folders (split into segments), ordered by path and capped at maxPaths. Used
// by the batch taxonomy planner as the candidate pool of folders to reuse
// (similarity preprocessing then narrows it per batch). The folder tree is the
// single source of truth, so this no longer scans page rows.
func (r *wikiPageRepository) ListDistinctCategoryPaths(
ctx context.Context,
kbID string,
@@ -429,123 +428,175 @@ func (r *wikiPageRepository) ListDistinctCategoryPaths(
if maxPaths <= 0 {
maxPaths = 150
}
type row struct {
L1 string `gorm:"column:category_l1"`
L2 string `gorm:"column:category_l2"`
L3 string `gorm:"column:category_l3"`
}
var rows []row
var paths []string
if err := r.db.WithContext(ctx).
Model(&types.WikiPage{}).
Distinct("category_l1", "category_l2", "category_l3").
Where("knowledge_base_id = ? AND page_type IN ? AND status <> ? AND category_l1 <> ?",
kbID,
[]string{types.WikiPageTypeEntity, types.WikiPageTypeConcept},
types.WikiPageStatusArchived,
"",
).
Order("category_l1 ASC").
Order("category_l2 ASC").
Order("category_l3 ASC").
Model(&types.WikiFolder{}).
Where("knowledge_base_id = ? AND path <> ?", kbID, "").
Order("path ASC").
Limit(maxPaths).
Scan(&rows).Error; err != nil {
Pluck("path", &paths).Error; err != nil {
return nil, err
}
out := make([][]string, 0, len(rows))
for _, rw := range rows {
path := make([]string, 0, 3)
for _, label := range []string{rw.L1, rw.L2, rw.L3} {
if label == "" {
break // levels are contiguous; an empty level ends the path
}
path = append(path, label)
}
if len(path) > 0 {
out = append(out, path)
out := make([][]string, 0, len(paths))
for _, p := range paths {
if seg := types.CleanWikiCategoryPath(strings.Split(p, "/")); len(seg) > 0 {
out = append(out, seg)
}
}
return out, nil
}
// wikiCategoryLevelColumns maps a parent depth (0/1/2) to the flat level column
// holding that depth's direct child. Depth >= WikiCategoryMaxDepth has no child.
var wikiCategoryLevelColumns = []string{"category_l1", "category_l2", "category_l3"}
// --- Folder tree (wiki_folders) ---
// ListDistinctCategoryPathsByType returns the DIRECT child folders of
// parentPath for the given page types, with the page count under each child,
// paginated. Grouping and pagination are pushed entirely to SQL over the flat
// category_l1/l2/l3 columns (kept in sync by normalizeWikiHierarchy), so this
// is cheap, indexable, and identical across Postgres and SQLite — no JSON
// parsing and no full in-memory scan. The second return value is the total
// number of distinct child folders (before pagination).
func (r *wikiPageRepository) ListDistinctCategoryPathsByType(
ctx context.Context,
kbID string,
pageTypes []string,
parentPath []string,
page, pageSize int,
) ([]types.WikiCategoryPath, int, error) {
if len(pageTypes) == 0 {
return nil, 0, nil
}
parent := types.CleanWikiCategoryPath(parentPath)
if len(parent) >= len(wikiCategoryLevelColumns) {
return nil, 0, nil // no level deeper than the cap can exist
}
childCol := wikiCategoryLevelColumns[len(parent)]
// ErrWikiFolderNotFound is returned when a wiki folder is not found.
var ErrWikiFolderNotFound = errors.New("wiki folder not found")
// Build the shared WHERE as a closure so the count and the page query each
// start from a fresh statement (GORM chains mutate in place otherwise).
scoped := func() *gorm.DB {
q := r.db.WithContext(ctx).
Model(&types.WikiPage{}).
Where("knowledge_base_id = ? AND page_type IN ? AND status <> ?",
kbID, pageTypes, types.WikiPageStatusArchived).
Where(childCol+" <> ?", "")
for i, label := range parent {
q = q.Where(wikiCategoryLevelColumns[i]+" = ?", label)
// ErrWikiFolderConflict is returned when a sibling folder with the same name
// already exists under the same parent.
var ErrWikiFolderConflict = errors.New("wiki folder name conflict")
func (r *wikiPageRepository) CreateFolder(ctx context.Context, folder *types.WikiFolder) error {
return r.db.WithContext(ctx).Create(folder).Error
}
func (r *wikiPageRepository) GetFolderByID(ctx context.Context, kbID string, id string) (*types.WikiFolder, error) {
var folder types.WikiFolder
if err := r.db.WithContext(ctx).
Where("knowledge_base_id = ? AND id = ?", kbID, id).
First(&folder).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, ErrWikiFolderNotFound
}
return q
return nil, err
}
return &folder, nil
}
var total int64
if err := scoped().Distinct(childCol).Count(&total).Error; err != nil {
return nil, 0, err
}
if total == 0 {
return nil, 0, nil
func (r *wikiPageRepository) GetChildFolderByName(
ctx context.Context, kbID string, parentID string, name string,
) (*types.WikiFolder, error) {
var folder types.WikiFolder
if err := r.db.WithContext(ctx).
Where("knowledge_base_id = ? AND parent_id = ? AND name = ?", kbID, parentID, name).
First(&folder).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, ErrWikiFolderNotFound
}
return nil, err
}
return &folder, nil
}
if page < 1 {
page = 1
}
if pageSize < 1 {
pageSize = 2000
func (r *wikiPageRepository) ListChildFolders(
ctx context.Context, kbID string, parentID string,
) ([]*types.WikiFolder, error) {
var folders []*types.WikiFolder
if err := r.db.WithContext(ctx).
Where("knowledge_base_id = ? AND parent_id = ?", kbID, parentID).
Order("sort_order ASC").
Order("name ASC").
Find(&folders).Error; err != nil {
return nil, err
}
return folders, nil
}
type row struct {
Label string `gorm:"column:label"`
Cnt int64 `gorm:"column:cnt"`
}
var rows []row
if err := scoped().
Select(childCol + " AS label, COUNT(*) AS cnt").
Group(childCol).
Order("label ASC").
Offset((page - 1) * pageSize).
Limit(pageSize).
Scan(&rows).Error; err != nil {
return nil, 0, err
func (r *wikiPageRepository) ListAllFolders(ctx context.Context, kbID string) ([]*types.WikiFolder, error) {
var folders []*types.WikiFolder
if err := r.db.WithContext(ctx).
Where("knowledge_base_id = ?", kbID).
Order("depth ASC").
Order("path ASC").
Find(&folders).Error; err != nil {
return nil, err
}
return folders, nil
}
out := make([]types.WikiCategoryPath, 0, len(rows))
for _, rw := range rows {
full := append(append([]string(nil), parent...), rw.Label)
out = append(out, types.WikiCategoryPath{Path: full, Count: rw.Cnt})
func (r *wikiPageRepository) UpdateFolder(ctx context.Context, folder *types.WikiFolder) error {
result := r.db.WithContext(ctx).
Model(&types.WikiFolder{}).
Where("id = ?", folder.ID).
Updates(map[string]interface{}{
"parent_id": folder.ParentID,
"name": folder.Name,
"path": folder.Path,
"depth": folder.Depth,
"sort_order": folder.SortOrder,
"updated_at": folder.UpdatedAt,
})
if result.Error != nil {
return result.Error
}
return out, int(total), nil
if result.RowsAffected == 0 {
return ErrWikiFolderNotFound
}
return nil
}
func (r *wikiPageRepository) DeleteFolder(ctx context.Context, kbID string, id string) error {
result := r.db.WithContext(ctx).
Where("knowledge_base_id = ? AND id = ?", kbID, id).
Delete(&types.WikiFolder{})
if result.Error != nil {
return result.Error
}
if result.RowsAffected == 0 {
return ErrWikiFolderNotFound
}
return nil
}
func (r *wikiPageRepository) CountPagesInFolder(ctx context.Context, kbID string, folderID string) (int64, error) {
var count int64
if err := r.db.WithContext(ctx).
Model(&types.WikiPage{}).
Where("knowledge_base_id = ? AND folder_id = ? AND status <> ?",
kbID, folderID, types.WikiPageStatusArchived).
Count(&count).Error; err != nil {
return 0, err
}
return count, nil
}
func (r *wikiPageRepository) CountPagesByFolder(
ctx context.Context, kbID string, pageTypes []string,
) (map[string]int64, error) {
type folderCount struct {
FolderID string
Cnt int64
}
var rows []folderCount
q := r.db.WithContext(ctx).
Model(&types.WikiPage{}).
Select("folder_id, COUNT(*) as cnt").
Where("knowledge_base_id = ? AND status <> ?", kbID, types.WikiPageStatusArchived)
if len(pageTypes) > 0 {
q = q.Where("page_type IN ?", pageTypes)
}
if err := q.Group("folder_id").Scan(&rows).Error; err != nil {
return nil, err
}
out := make(map[string]int64, len(rows))
for _, row := range rows {
out[row.FolderID] = row.Cnt
}
return out, nil
}
func (r *wikiPageRepository) ListPagesByFolderIDs(
ctx context.Context, kbID string, folderIDs []string,
) ([]*types.WikiPage, error) {
if len(folderIDs) == 0 {
return nil, nil
}
var pages []*types.WikiPage
if err := r.db.WithContext(ctx).
Where("knowledge_base_id = ? AND folder_id IN ?", kbID, folderIDs).
Find(&pages).Error; err != nil {
return nil, err
}
return pages, nil
}
// ListSummariesByKnowledgeIDs returns summary-page content keyed by the
@@ -30,13 +30,11 @@ CREATE TABLE IF NOT EXISTS wiki_pages (
content TEXT NOT NULL DEFAULT '',
summary TEXT NOT NULL DEFAULT '',
parent_slug VARCHAR(255) NOT NULL DEFAULT '',
folder_id VARCHAR(36) NOT NULL DEFAULT '',
category_path TEXT DEFAULT '[]',
wiki_path VARCHAR(1024) NOT NULL DEFAULT '',
depth INTEGER NOT NULL DEFAULT 0,
sort_order INTEGER NOT NULL DEFAULT 0,
category_l1 VARCHAR(255) NOT NULL DEFAULT '',
category_l2 VARCHAR(255) NOT NULL DEFAULT '',
category_l3 VARCHAR(255) NOT NULL DEFAULT '',
source_refs TEXT DEFAULT '[]',
chunk_refs TEXT DEFAULT '[]',
in_links TEXT DEFAULT '[]',
@@ -50,11 +48,29 @@ CREATE TABLE IF NOT EXISTS wiki_pages (
);
`
// wikiFoldersTestDDL mirrors the production wiki_folders DDL for SQLite.
const wikiFoldersTestDDL = `
CREATE TABLE IF NOT EXISTS wiki_folders (
id VARCHAR(36) PRIMARY KEY,
tenant_id INTEGER NOT NULL DEFAULT 0,
knowledge_base_id VARCHAR(36) NOT NULL,
parent_id VARCHAR(36) NOT NULL DEFAULT '',
name VARCHAR(255) NOT NULL,
path VARCHAR(1024) NOT NULL DEFAULT '',
depth INTEGER NOT NULL DEFAULT 0,
sort_order INTEGER NOT NULL DEFAULT 0,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
deleted_at DATETIME
);
`
func setupWikiPagesTestDB(t *testing.T) *gorm.DB {
t.Helper()
db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
require.NoError(t, err)
require.NoError(t, db.Exec(wikiPagesTestDDL).Error)
require.NoError(t, db.Exec(wikiFoldersTestDDL).Error)
return db
}
@@ -89,9 +105,6 @@ func makeCategorizedWikiPage(kbID, slug, pageType, status string, categoryPath .
if len(categoryPath) > 0 {
page.WikiPath = pageType + "/" + strings.Join(categoryPath, "/") + "/" + page.Title
page.Depth = len(categoryPath)
// Mirror the flat level columns the directory query groups on, the same
// way normalizeWikiHierarchy would on a real write.
page.CategoryL1, page.CategoryL2, page.CategoryL3 = types.WikiCategoryLevels(categoryPath)
}
return page
}
@@ -130,57 +143,68 @@ func TestList_WikiPathSortReturnsCategorizedPagesFirst(t *testing.T) {
assert.Equal(t, "entity/001-root", got[2].Slug)
}
// TestListDistinctCategoryPathsByType_GroupsAndPaginatesInSQL verifies the
// directory listing groups direct child folders, counts pages under each
// (including deeper pages), and paginates — all pushed to SQL over the flat
// level columns, so it must behave identically on SQLite.
func TestListDistinctCategoryPathsByType_GroupsAndPaginatesInSQL(t *testing.T) {
// TestFolderTree_CRUDAndChildListing exercises the wiki_folders repository:
// child listing ordered by sort_order/name, find-by-name, page counting under
// a folder, and that ListDistinctCategoryPaths reflects the folder paths.
func TestFolderTree_CRUDAndChildListing(t *testing.T) {
db := setupWikiPagesTestDB(t)
repo := NewWikiPageRepository(db)
ctx := context.Background()
pages := []*types.WikiPage{
makeCategorizedWikiPage("kb-c", "entity/a1", types.WikiPageTypeEntity, types.WikiPageStatusPublished, "AI"),
makeCategorizedWikiPage("kb-c", "entity/a2", types.WikiPageTypeEntity, types.WikiPageStatusPublished, "AI"),
makeCategorizedWikiPage("kb-c", "entity/a3", types.WikiPageTypeEntity, types.WikiPageStatusPublished, "AI", "LLM"),
makeCategorizedWikiPage("kb-c", "entity/p1", types.WikiPageTypeEntity, types.WikiPageStatusPublished, "人物"),
makeWikiPage("kb-c", "entity/root", types.WikiPageTypeEntity, types.WikiPageStatusPublished),
makeCategorizedWikiPage("kb-c", "entity/arch", types.WikiPageTypeEntity, types.WikiPageStatusArchived, "AI"),
mk := func(id, parentID, name, path string, depth int) *types.WikiFolder {
return &types.WikiFolder{
ID: id, TenantID: 1, KnowledgeBaseID: "kb-f",
ParentID: parentID, Name: name, Path: path, Depth: depth,
CreatedAt: time.Now(), UpdatedAt: time.Now(),
}
}
for _, p := range pages {
ai := mk("f-ai", types.WikiFolderRootID, "AI", "AI", 1)
people := mk("f-people", types.WikiFolderRootID, "人物", "人物", 1)
llm := mk("f-llm", "f-ai", "LLM", "AI/LLM", 2)
for _, f := range []*types.WikiFolder{ai, people, llm} {
require.NoError(t, repo.CreateFolder(ctx, f))
}
// Root children: AI, 人物 (ordered by name within equal sort_order).
roots, err := repo.ListChildFolders(ctx, "kb-f", types.WikiFolderRootID)
require.NoError(t, err)
require.Len(t, roots, 2)
// Direct child of AI is LLM.
child, err := repo.GetChildFolderByName(ctx, "kb-f", "f-ai", "LLM")
require.NoError(t, err)
assert.Equal(t, "f-llm", child.ID)
// Missing child surfaces the typed not-found error.
_, err = repo.GetChildFolderByName(ctx, "kb-f", "f-ai", "Nope")
assert.ErrorIs(t, err, ErrWikiFolderNotFound)
// Pages filed into folders are counted (archived excluded).
pAI := makeCategorizedWikiPage("kb-f", "entity/a1", types.WikiPageTypeEntity, types.WikiPageStatusPublished, "AI")
pAI.FolderID = "f-ai"
pLLM := makeCategorizedWikiPage("kb-f", "entity/a2", types.WikiPageTypeEntity, types.WikiPageStatusPublished, "AI", "LLM")
pLLM.FolderID = "f-llm"
pArch := makeCategorizedWikiPage("kb-f", "entity/a3", types.WikiPageTypeEntity, types.WikiPageStatusArchived, "AI")
pArch.FolderID = "f-ai"
for _, p := range []*types.WikiPage{pAI, pLLM, pArch} {
require.NoError(t, repo.Create(ctx, p))
}
types4 := []string{types.WikiPageTypeEntity}
// Root level: two folders (AI, 人物); the loose root page and archived page
// are excluded. AI counts all 3 non-archived pages whose first level is AI.
roots, total, err := repo.ListDistinctCategoryPathsByType(ctx, "kb-c", types4, nil, 1, 100)
aiCount, err := repo.CountPagesInFolder(ctx, "kb-f", "f-ai")
require.NoError(t, err)
assert.Equal(t, 2, total)
require.Len(t, roots, 2)
byLabel := map[string]int64{}
for _, p := range roots {
require.Len(t, p.Path, 1)
byLabel[p.Path[0]] = p.Count
}
assert.Equal(t, int64(3), byLabel["AI"])
assert.Equal(t, int64(1), byLabel["人物"])
assert.Equal(t, int64(1), aiCount, "archived page excluded; LLM page is under f-llm, not f-ai")
// Child of [AI]: only the [AI, LLM] page.
children, total, err := repo.ListDistinctCategoryPathsByType(ctx, "kb-c", types4, []string{"AI"}, 1, 100)
// ListDistinctCategoryPaths returns the folder paths split into segments.
paths, err := repo.ListDistinctCategoryPaths(ctx, "kb-f", 100)
require.NoError(t, err)
assert.Equal(t, 1, total)
require.Len(t, children, 1)
assert.Equal(t, []string{"AI", "LLM"}, children[0].Path)
assert.Equal(t, int64(1), children[0].Count)
assert.Contains(t, paths, []string{"AI"})
assert.Contains(t, paths, []string{"AI", "LLM"})
assert.Contains(t, paths, []string{"人物"})
// Pagination: page size 1 over the two root folders returns one row but the
// total still reflects both distinct folders.
firstPage, total, err := repo.ListDistinctCategoryPathsByType(ctx, "kb-c", types4, nil, 1, 1)
// Pages can be fetched by their folder ids for subtree recompute.
pages, err := repo.ListPagesByFolderIDs(ctx, "kb-f", []string{"f-ai", "f-llm"})
require.NoError(t, err)
assert.Equal(t, 2, total)
require.Len(t, firstPage, 1)
assert.Len(t, pages, 3)
}
// TestListByTypeLight_ProjectsNarrowColumnsAndExcludesArchived verifies