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
feat(wiki): implement folder management features in wiki API
wizardchene76738d054c1
diff --git a/frontend/src/api/wiki/index.ts b/frontend/src/api/wiki/index.tsindex d52627ab3..96f31fb6d 100644--- a/frontend/src/api/wiki/index.ts+++ b/frontend/src/api/wiki/index.ts@@ -42,12 +42,27 @@ export interface WikiPageListResponse {total_pages: number;}-export interface WikiCategoryPathListResponse {- paths: { path: string[]; count: number }[];- total: number;- page: number;- page_size: number;- total_pages: number;+export interface WikiFolder {+ id: string;+ tenant_id: number;+ knowledge_base_id: string;+ parent_id: string;+ name: string;+ path: string;+ depth: number;+ sort_order: number;+ created_at: string;+ updated_at: string;+}++export interface WikiFolderNode extends WikiFolder {+ page_count: number;+ has_children: boolean;+}++export interface WikiFolderListResponse {+ parent_id: string;+ folders: WikiFolderNode[];}export interface WikiGraphMeta {@@ -114,18 +129,44 @@ export function listWikiPages(kbId: string, params?: {return get(`/api/v1/knowledgebase/${kbId}/wiki/pages${qs ? '?' + qs : ''}`);}-export function listWikiCategories(kbId: string, params: {- page_type: string;- parent_path?: string;- page?: number;- page_size?: number;-}) {+// listWikiFolders returns the direct child folders of parentId ("" = root),+// each enriched with a recursive page_count and a has_children flag so the tree+// can render expand affordances and empty folders without a second request.+// pageTypes scopes the view to a sidebar tab: only folders whose subtree holds+// a page of those types (or are entirely empty) come back, and page_count is+// counted within those types.+export function listWikiFolders(kbId: string, parentId = '', pageTypes = '') {const query = new URLSearchParams();- query.set('page_type', params.page_type);- if (params.parent_path) query.set('parent_path', params.parent_path);- if (params.page !== undefined) query.set('page', String(params.page));- if (params.page_size !== undefined) query.set('page_size', String(params.page_size));- return get(`/api/v1/knowledgebase/${kbId}/wiki/categories?${query.toString()}`);+ if (parentId) query.set('parent_id', parentId);+ if (pageTypes) query.set('page_types', pageTypes);+ const qs = query.toString();+ return get(`/api/v1/knowledgebase/${kbId}/wiki/folders${qs ? '?' + qs : ''}`);+}++// createWikiFolder creates a new empty folder under parentId ("" = root).+export function createWikiFolder(kbId: string, parentId: string, name: string) {+ return post(`/api/v1/knowledgebase/${kbId}/wiki/folders`, { parent_id: parentId, name });+}++// updateWikiFolder renames and/or reparents a folder. Pass move_parent: true+// (and parent_id) to reparent; omit it for a pure rename.+export function updateWikiFolder(+ kbId: string,+ folderId: string,+ data: { name?: string; parent_id?: string; move_parent?: boolean },+) {+ return put(`/api/v1/knowledgebase/${kbId}/wiki/folders/${folderId}`, data);+}++// deleteWikiFolder removes an empty folder (no pages, no sub-folders).+export function deleteWikiFolder(kbId: string, folderId: string) {+ return del(`/api/v1/knowledgebase/${kbId}/wiki/folders/${folderId}`);+}++// moveWikiPage relocates a page into folderId ("" = root). The slug is sent in+// the body because wiki slugs are hierarchical.+export function moveWikiPage(kbId: string, slug: string, folderId: string) {+ return put(`/api/v1/knowledgebase/${kbId}/wiki/move-page`, { slug, folder_id: folderId });}export function createWikiPage(kbId: string, data: Partial<WikiPage>) {diff --git a/frontend/src/i18n/locales/en-US.ts b/frontend/src/i18n/locales/en-US.tsindex c3f34ba75..113e151fa 100755--- a/frontend/src/i18n/locales/en-US.ts+++ b/frontend/src/i18n/locales/en-US.ts@@ -2520,7 +2520,29 @@ export default {searchNoResults: 'No matching pages found',loadMore: 'Load more ({remaining} remaining)',filterAll: 'All Types',- loadMoreFolders: 'Load more folders',+ newFolder: 'New Folder',+ newRootFolder: 'New Folder',+ newSubfolder: 'New Subfolder',+ folderActions: 'Folder actions',+ folderNamePlaceholder: 'Enter folder name',+ createFolderSuccess: 'Folder created',+ createFolderFailed: 'Failed to create folder',+ movePageSuccess: 'Page moved',+ movePageFailed: 'Failed to move page',+ renameFolder: 'Rename Folder',+ renameFolderSuccess: 'Folder renamed',+ renameFolderFailed: 'Failed to rename folder',+ deleteFolder: 'Delete Folder',+ deleteFolderConfirm: 'Delete folder "{name}"?',+ deleteFolderSuccess: 'Folder deleted',+ deleteFolderFailed: 'Failed to delete folder',+ deleteFolderNotEmpty: 'Folder is not empty. Remove its pages and sub-folders first.',+ moveFolderSuccess: 'Folder moved',+ moveFolderFailed: 'Failed to move folder',+ moveFolderIntoSelf: 'Cannot move a folder into itself or its descendant',+ moveConfirm: 'Move to "{target}"?',+ rootFolderLabel: 'Root',+ dropToRoot: 'Drop here to move to root',filterSummary: 'Summaries',filterKnowledge: 'Knowledge',filterEntity: 'Entities',diff --git a/frontend/src/i18n/locales/ko-KR.ts b/frontend/src/i18n/locales/ko-KR.tsindex 1e1ea8d91..693aa43cd 100755--- a/frontend/src/i18n/locales/ko-KR.ts+++ b/frontend/src/i18n/locales/ko-KR.ts@@ -3379,7 +3379,29 @@ export default {searchNoResults: "일치하는 페이지가 없습니다",loadMore: "더 불러오기 ({remaining}개 남음)",filterAll: "전체 유형",- loadMoreFolders: "더 많은 디렉터리 불러오기",+ newFolder: "새 디렉터리",+ newRootFolder: "새 디렉터리",+ newSubfolder: "새 하위 디렉터리",+ folderActions: "디렉터리 작업",+ folderNamePlaceholder: "디렉터리 이름을 입력하세요",+ createFolderSuccess: "디렉터리가 생성되었습니다",+ createFolderFailed: "디렉터리 생성 실패",+ movePageSuccess: "페이지가 이동되었습니다",+ movePageFailed: "페이지 이동 실패",+ renameFolder: "디렉터리 이름 변경",+ renameFolderSuccess: "디렉터리 이름이 변경되었습니다",+ renameFolderFailed: "디렉터리 이름 변경 실패",+ deleteFolder: "디렉터리 삭제",+ deleteFolderConfirm: "디렉터리 \"{name}\"을(를) 삭제하시겠습니까?",+ deleteFolderSuccess: "디렉터리가 삭제되었습니다",+ deleteFolderFailed: "디렉터리 삭제 실패",+ deleteFolderNotEmpty: "디렉터리가 비어 있지 않습니다. 먼저 페이지와 하위 디렉터리를 제거하세요.",+ moveFolderSuccess: "디렉터리가 이동되었습니다",+ moveFolderFailed: "디렉터리 이동 실패",+ moveFolderIntoSelf: "디렉터리를 자기 자신이나 하위 디렉터리로 이동할 수 없습니다",+ moveConfirm: "'{target}'(으)로 이동할까요?",+ rootFolderLabel: "루트",+ dropToRoot: "여기에 놓아 루트로 이동",filterSummary: "요약",filterKnowledge: "지식",filterEntity: "엔티티",diff --git a/frontend/src/i18n/locales/ru-RU.ts b/frontend/src/i18n/locales/ru-RU.tsindex 5ad93d415..af5346663 100755--- a/frontend/src/i18n/locales/ru-RU.ts+++ b/frontend/src/i18n/locales/ru-RU.ts@@ -2884,7 +2884,29 @@ export default {searchNoResults: 'Страницы не найдены',loadMore: 'Загрузить ещё (осталось {remaining})',filterAll: 'Все типы',- loadMoreFolders: 'Загрузить ещё каталоги',+ newFolder: 'Новый каталог',+ newRootFolder: 'Новый каталог',+ newSubfolder: 'Новый подкаталог',+ folderActions: 'Действия с каталогом',+ folderNamePlaceholder: 'Введите название каталога',+ createFolderSuccess: 'Каталог создан',+ createFolderFailed: 'Не удалось создать каталог',+ movePageSuccess: 'Страница перемещена',+ movePageFailed: 'Не удалось переместить страницу',+ renameFolder: 'Переименовать каталог',+ renameFolderSuccess: 'Каталог переименован',+ renameFolderFailed: 'Не удалось переименовать каталог',+ deleteFolder: 'Удалить каталог',+ deleteFolderConfirm: 'Удалить каталог «{name}»?',+ deleteFolderSuccess: 'Каталог удалён',+ deleteFolderFailed: 'Не удалось удалить каталог',+ deleteFolderNotEmpty: 'Каталог не пуст. Сначала удалите страницы и подкаталоги.',+ moveFolderSuccess: 'Каталог перемещён',+ moveFolderFailed: 'Не удалось переместить каталог',+ moveFolderIntoSelf: 'Нельзя переместить каталог в себя или свой подкаталог',+ moveConfirm: 'Переместить в «{target}»?',+ rootFolderLabel: 'Корень',+ dropToRoot: 'Перетащите сюда, чтобы переместить в корень',filterSummary: 'Резюме',filterKnowledge: 'Знания',filterEntity: 'Сущности',diff --git a/frontend/src/i18n/locales/zh-CN.ts b/frontend/src/i18n/locales/zh-CN.tsindex 67c3f0ee1..d596b3d0d 100755--- a/frontend/src/i18n/locales/zh-CN.ts+++ b/frontend/src/i18n/locales/zh-CN.ts@@ -3384,7 +3384,29 @@ export default {searchNoResults: "没有找到匹配的页面",loadMore: "加载更多(还剩 {remaining} 条)",filterAll: "全部类型",- loadMoreFolders: "加载更多目录",+ newFolder: "新建目录",+ newRootFolder: "新建目录",+ newSubfolder: "新建子目录",+ folderActions: "目录操作",+ folderNamePlaceholder: "请输入目录名称",+ createFolderSuccess: "目录已创建",+ createFolderFailed: "创建目录失败",+ movePageSuccess: "页面已移动",+ movePageFailed: "移动页面失败",+ renameFolder: "重命名目录",+ renameFolderSuccess: "目录已重命名",+ renameFolderFailed: "重命名目录失败",+ deleteFolder: "删除目录",+ deleteFolderConfirm: "确定要删除目录“{name}”吗?",+ deleteFolderSuccess: "目录已删除",+ deleteFolderFailed: "删除目录失败",+ deleteFolderNotEmpty: "目录非空,请先移除其中的页面和子目录",+ moveFolderSuccess: "目录已移动",+ moveFolderFailed: "移动目录失败",+ moveFolderIntoSelf: "不能将目录移动到自身或其子目录中",+ moveConfirm: "移动到「{target}」?",+ rootFolderLabel: "根目录",+ dropToRoot: "拖到空白处移动到根目录",filterSummary: "摘要",filterKnowledge: "知识",filterEntity: "实体",diff --git a/frontend/src/utils/index.ts b/frontend/src/utils/index.tsindex 0afdaadb6..814829b59 100644--- a/frontend/src/utils/index.ts+++ b/frontend/src/utils/index.ts@@ -47,9 +47,12 @@ const DEFAULT_VALID_TYPES = new Set(["pdf", "txt", "md", "docx", "doc", "pptx",* @param validTypes - override the default extension whitelist with a dynamic set (e.g. from engine registry).*/export function kbFileTypeVerification(file: any, silent = false, validTypes?: Set<string> | string[]) {- const allowed = validTypes+ const provided = validTypes? (validTypes instanceof Set ? validTypes : new Set(validTypes))- : DEFAULT_VALID_TYPES;+ : undefined;+ // An empty whitelist means the engine registry hasn't loaded yet; fall back to+ // the default set rather than rejecting every file.+ const allowed = provided && provided.size > 0 ? provided : DEFAULT_VALID_TYPES;const type = file.name.substring(file.name.lastIndexOf(".") + 1).toLowerCase();if (!allowed.has(type)) {diff --git a/frontend/src/views/knowledge/utils/uploadSources.ts b/frontend/src/views/knowledge/utils/uploadSources.tsindex a2624f74d..895b57753 100644--- a/frontend/src/views/knowledge/utils/uploadSources.ts+++ b/frontend/src/views/knowledge/utils/uploadSources.ts@@ -31,11 +31,15 @@ export function filterUploadFiles(options: FilterUploadFilesOptions = {},): FilterUploadFilesResult {const list = Array.from(files)- const dynamicTypes = options.supportedFileTypes+ const dynamicTypesRaw = options.supportedFileTypes? options.supportedFileTypes instanceof Set? options.supportedFileTypes: new Set(options.supportedFileTypes): undefined+ // An empty set means the parser-engine list hasn't loaded yet (race with the+ // async fetch on mount). Treat it as "unknown" and fall back to the default+ // whitelist instead of rejecting every file as unsupported.+ const dynamicTypes = dynamicTypesRaw && dynamicTypesRaw.size > 0 ? dynamicTypesRaw : undefinedconst validFiles: File[] = []let skippedCount = 0diff --git a/frontend/src/views/knowledge/wiki/WikiBrowser.vue b/frontend/src/views/knowledge/wiki/WikiBrowser.vueindex eb36cf426..e1584fe39 100644--- a/frontend/src/views/knowledge/wiki/WikiBrowser.vue+++ b/frontend/src/views/knowledge/wiki/WikiBrowser.vue@@ -290,21 +290,67 @@<!-- Active-tab list --><template v-if="activeGroup">- <div ref="treeListRef" class="wiki-tree-list">+ <!-- Toolbar row: right-aligned "new root folder". Always present+ so it never shifts the list mid-drag. -->+ <div class="wiki-tree-toolbar">+ <WikiFolderActions+ create-only+ :label="$t('knowledgeEditor.wikiBrowser.newRootFolder')"+ @create="(name: string) => createFolder('', [], name)"+ />+ </div>+ <!-- The list container is itself the "move to root" drop target;+ folder rows stop propagation so their own drop wins. This+ avoids inserting/removing a drop bar during the drag, which+ was cancelling the native drag after the first success. -->+ <div+ ref="treeListRef"+ :class="['wiki-tree-list', { 'wiki-tree-list--root-drop': dropTargetKey === '__root__' }]"+ @dragover.prevent="onRootDragOver"+ @dragleave="onDirectoryDragLeave('__root__')"+ @drop.prevent="onDropOnDirectory($event, '', [])"+ ><templatev-for="item in activeTreeRows":key="item.rowKey"><divv-if="item.kind === 'directory'"- class="wiki-directory-item"+ :class="['wiki-directory-item', { 'wiki-directory-item--drop': dropTargetKey === item.pathKey }]":style="{ paddingLeft: `${6 + item.depth * 14}px` }"+ :draggable="editingFolderId !== item.folderId"@click="toggleDirectory(item.pathKey)"+ @dragstart="onFolderDragStart($event, item.folderId, item.path)"+ @dragend="onPageDragEnd"+ @dragover.prevent.stop="onDirectoryDragOver($event, item.pathKey)"+ @dragleave.stop="onDirectoryDragLeave(item.pathKey)"+ @drop.prevent.stop="onDropOnDirectory($event, item.folderId, item.path)"><t-icon :name="item.collapsed ? 'chevron-right' : 'chevron-down'" class="wiki-directory-toggle" /><t-icon name="folder" class="wiki-directory-icon" />- <span class="wiki-directory-title">{{ item.label }}</span>- <span class="wiki-directory-count">{{ item.count }}</span>+ <input+ v-if="editingFolderId === item.folderId"+ v-model="editingName"+ class="wiki-directory-rename-input"+ :placeholder="$t('knowledgeEditor.wikiBrowser.folderNamePlaceholder')"+ @click.stop+ @keydown.enter="commitRenameFolder(item.folderId, item.label)"+ @keydown.esc="cancelRenameFolder"+ @blur="commitRenameFolder(item.folderId, item.label)"+ />+ <template v-else>+ <span class="wiki-directory-title">{{ item.label }}</span>+ <span class="wiki-directory-count">{{ item.count }}</span>+ <WikiFolderActions+ v-if="item.folderId"+ :name="item.label"+ :page-count="item.count"+ :has-children="item.hasChildren"+ @create="(name: string) => createFolder(item.folderId, item.path, name)"+ @rename="() => startRenameFolder(item.folderId, item.label)"+ @delete="() => deleteFolder(item.folderId)"+ />+ </template></div><divv-else-if="item.kind === 'load-more'"@@ -319,25 +365,15 @@<span>{{ $t('knowledgeEditor.wikiBrowser.logLoadMore') }}</span></template></div>- <div- v-else-if="item.kind === 'load-more-dirs'"- class="wiki-directory-load-more"- :data-loadmore-key="item.rowKey"- :style="{ paddingLeft: `${12 + item.depth * 14}px` }"- @click="loadCategoriesForType(item.type, { parentPath: item.path })"- >- <t-loading v-if="item.loading" size="small" />- <template v-else>- <t-icon name="folder-open" />- <span>{{ $t('knowledgeEditor.wikiBrowser.loadMoreFolders') }}</span>- </template>- </div><divv-else:class="['wiki-page-item', 'wiki-page-item--tree', { active: selectedPage?.id === item.page.id }]":style="{ paddingLeft: `${12 + item.depth * 14}px` }":title="item.page.title"+ draggable="true"@click="selectPage(item.page)"+ @dragstart="onPageDragStart($event, item.page)"+ @dragend="onPageDragEnd"><t-icon :name="getPageIcon(item.page)" :class="['wiki-page-file-icon', `wiki-page-file-icon--${item.page.page_type}`]" /><span class="wiki-page-item-title">{{ item.page.title }}</span>@@ -666,6 +702,30 @@:embeddedMode="true"/></t-drawer>++ <!-- In-place move confirmation, anchored at the drop point. Confirming runs+ the actual move API; cancelling discards the staged move. -->+ <teleport to="body">+ <div v-if="pendingMove" class="wiki-move-confirm-mask" @click="cancelPendingMove">+ <div+ class="wiki-move-confirm"+ :style="{ left: `${pendingMove.x}px`, top: `${pendingMove.y}px` }"+ @click.stop+ >+ <div class="wiki-move-confirm-text">+ {{ $t('knowledgeEditor.wikiBrowser.moveConfirm', { target: pendingMove.targetLabel }) }}+ </div>+ <div class="wiki-move-confirm-footer">+ <t-button size="small" variant="outline" @click="cancelPendingMove">+ {{ $t('common.cancel') }}+ </t-button>+ <t-button size="small" theme="primary" @click="confirmPendingMove">+ {{ $t('common.confirm') }}+ </t-button>+ </div>+ </div>+ </div>+ </teleport></div></template>@@ -679,11 +739,16 @@ import { marked } from 'marked'import { MessagePlugin } from 'tdesign-vue-next'import { hydrateProtectedFileImages } from '@/utils/security'import picturePreview from '@/components/picture-preview.vue'+import WikiFolderActions from './WikiFolderActions.vue'import { createSessions } from '@/api/chat'import ChatView from '@/views/chat/index.vue'import {listWikiPages,- listWikiCategories,+ listWikiFolders,+ createWikiFolder,+ updateWikiFolder,+ deleteWikiFolder,+ moveWikiPage,getWikiPage,getWikiIndex,getWikiLog,@@ -693,6 +758,7 @@ import {listWikiIssues,updateWikiIssueStatus,type WikiPage,+ type WikiFolderNode,type WikiGraphData,type WikiStats,type WikiPageIssue,@@ -748,6 +814,10 @@ interface PageTypeBucket {loading: booleaninitialized: boolean // true once the first page has been fetchedcategoryPaths: Array<{ path: string[]; count: number }>+ // folderIdByPath maps a materialized folder path ("AI/LLM") to its stable+ // wiki_folders id, populated as folder levels load. Drag-and-drop and the+ // directory rows resolve a folder's id through this map.+ folderIdByPath: Record<string, string>categoriesLoading: booleancategoriesInitialized: boolean// categoryPages tracks the directory-skeleton pagination per parent level,@@ -769,9 +839,8 @@ interface DirectoryCategoryState {initialized: boolean}type WikiTreeRow =- | { kind: 'directory'; rowKey: string; pathKey: string; label: string; depth: number; count: number; collapsed: boolean }+ | { kind: 'directory'; rowKey: string; pathKey: string; folderId: string; path: string[]; label: string; depth: number; count: number; hasChildren: boolean; collapsed: boolean }| { kind: 'load-more'; rowKey: string; type: string; path: string[]; depth: number; loading: boolean }- | { kind: 'load-more-dirs'; rowKey: string; type: string; path: string[]; depth: number; loading: boolean }| { kind: 'page'; rowKey: string; page: WikiPage; depth: number }interface WikiTreeDirectory {label: string@@ -1545,24 +1614,6 @@ const activeTreeRows = computed<WikiTreeRow[]>(() => {cursor.pages.push(page)}- // appendDirLoadMore renders a "load more folders" row when the directory- // skeleton for `parentPath` has more pages to pull.- function appendDirLoadMore(parentPath: string[], depth: number) {- const catKey = directoryPathKey(groupType, parentPath)- const catState = bucket?.categoryPages[catKey]- if (!catState) return- const hasMore = catState.initialized && catState.nextPage <= catState.totalPages- if (!catState.loading && !hasMore) return- rows.push({- kind: 'load-more-dirs',- rowKey: `load-more-dirs:${catKey}`,- type: groupType,- path: parentPath,- depth,- loading: !!catState.loading,- })- }-function appendDirectory(dir: WikiTreeDirectory, depth: number) {const key = directoryPathKey(groupType, dir.path)const collapsed = collapsedDirectories.value.has(key)@@ -1570,9 +1621,12 @@ const activeTreeRows = computed<WikiTreeRow[]>(() => {kind: 'directory',rowKey: `dir:${key}`,pathKey: key,+ folderId: bucket?.folderIdByPath[dir.path.join('/')] || '',+ path: dir.path,label: dir.label,depth,count: dir.count,+ hasChildren: dir.dirs.size > 0,collapsed,})if (collapsed) return@@ -1580,7 +1634,6 @@ const activeTreeRows = computed<WikiTreeRow[]>(() => {for (const child of dir.dirs.values()) {appendDirectory(child, depth + 1)}- appendDirLoadMore(dir.path, depth + 1)for (const page of dir.pages) {rows.push({kind: 'page',@@ -1606,7 +1659,6 @@ const activeTreeRows = computed<WikiTreeRow[]>(() => {for (const dir of root.dirs.values()) {appendDirectory(dir, 0)}- appendDirLoadMore([], 0)for (const page of root.pages) {rows.push({kind: 'page',@@ -1673,8 +1725,6 @@ function dispatchLoadMore(rowKey: string) {if (!row) returnif (row.kind === 'load-more') {loadPagesForType(row.type, { categoryPath: row.path })- } else if (row.kind === 'load-more-dirs') {- loadCategoriesForType(row.type, { parentPath: row.path })}}@@ -1807,11 +1857,6 @@ function handleContentClick(e: MouseEvent) {// for common wikis. Later pages are pulled on scroll.const WIKI_SIDEBAR_PAGE_SIZE = 100-// WIKI_CATEGORY_PAGE_SIZE is the directory-skeleton fetch batch per level. A-// level is paged in on demand; when more siblings remain a "load more folders"-// row is rendered to pull the next page.-const WIKI_CATEGORY_PAGE_SIZE = 100-function emptyBucket(): PageTypeBucket {return {items: [],@@ -1820,6 +1865,7 @@ function emptyBucket(): PageTypeBucket {loading: false,initialized: false,categoryPaths: [],+ folderIdByPath: {},categoriesLoading: false,categoriesInitialized: false,categoryPages: {},@@ -1834,11 +1880,13 @@ function ensureBucket(type: string): PageTypeBucket {return pagesByType.value[type]}-// loadCategoriesForType pages the directory skeleton one level at a time. Each-// call fetches the next unloaded page of `parentPath`'s direct children;-// deeper levels are pulled lazily when their folder is expanded (see-// toggleDirectory), and a "load more folders" row pulls the next page within a-// level. Re-expanding an already-loaded level does not refetch.+// loadCategoriesForType pulls the child folders of one directory level from the+// authoritative wiki_folders tree. The folder tree is the single source of+// truth for placement, so empty folders show up here too (they carry a 0+// page_count). The parent level is resolved to its stable folder id through+// bucket.folderIdByPath; the root level uses id "". Each level's children are+// returned in one shot (the tree is navigation-sized), so there is no+// per-level "load more folders" pagination anymore.async function loadCategoriesForType(type: string, opts: { reset?: boolean; parentPath?: string[] } = {}) {const bucket = ensureBucket(type)const parentPath = opts.parentPath || []@@ -1846,13 +1894,19 @@ async function loadCategoriesForType(type: string, opts: { reset?: boolean; pareconst isRoot = parentPath.length === 0if (opts.reset) {bucket.categoryPaths = []+ bucket.folderIdByPath = {}bucket.categoriesInitialized = falsebucket.categoryPages = {}}+ const parentId = isRoot ? '' : bucket.folderIdByPath[parentPath.join('/')]+ // A deeper level whose parent folder id we have not yet recorded cannot be+ // resolved against the (id-based) folders endpoint — skip until it loads.+ if (!isRoot && parentId === undefined) return+let state = bucket.categoryPages[parentKey]if (state?.loading) return- if (state && state.initialized && state.nextPage > state.totalPages) return+ if (state && state.initialized) return // a level is loaded in a single requestif (!state) {state = { nextPage: 1, totalPages: 1, loading: false, initialized: false }}@@ -1863,46 +1917,250 @@ async function loadCategoriesForType(type: string, opts: { reset?: boolean; paresetState({ ...state, loading: true })if (isRoot) bucket.categoriesLoading = truetry {- const res = await listWikiCategories(props.knowledgeBaseId, {- page_type: tabPageTypes(type),- parent_path: parentPath.join('/'),- page: state.nextPage,- page_size: WIKI_CATEGORY_PAGE_SIZE,- })+ const res = await listWikiFolders(props.knowledgeBaseId, parentId || '', tabPageTypes(type))const body: any = (res as any).data || res- const paths = Array.isArray(body?.paths) ? body.paths : []- const incoming: Array<{ path: string[]; count: number }> = paths- .map((entry: any) => ({- path: Array.isArray(entry?.path)- ? entry.path.map((part: unknown) => String(part || '').trim()).filter(Boolean)- : [],- count: Number(entry?.count) || 0,+ const folders: WikiFolderNode[] = Array.isArray(body?.folders) ? body.folders : []+ const incoming = folders+ .map(folder => ({+ path: String(folder.path || '').split('/').map(part => part.trim()).filter(Boolean),+ count: Number(folder.page_count) || 0,+ id: String(folder.id || ''),}))- .filter((entry: { path: string[]; count: number }) => entry.path.length > 0)+ .filter(entry => entry.path.length > 0)const existing = new Map(bucket.categoryPaths.map(entry => [directoryPathKey(type, entry.path), entry]))+ const folderIds = { ...bucket.folderIdByPath }for (const entry of incoming) {- existing.set(directoryPathKey(type, entry.path), entry)+ existing.set(directoryPathKey(type, entry.path), { path: entry.path, count: entry.count })+ folderIds[entry.path.join('/')] = entry.id}bucket.categoryPaths = Array.from(existing.values())- setState({- nextPage: state.nextPage + 1,- totalPages: Number(body?.total_pages) || 1,- loading: false,- initialized: true,- })+ bucket.folderIdByPath = folderIds+ setState({ nextPage: 2, totalPages: 1, loading: false, initialized: true })if (isRoot) bucket.categoriesInitialized = true- initializeDefaultCollapsedDirectories(type, incoming.map((entry: { path: string[]; count: number }) => ({+ initializeDefaultCollapsedDirectories(type, incoming.map(entry => ({category_path: entry.path,} as WikiPage)))} catch (e) {- console.error(`Failed to load wiki categories of type ${type}:`, e)+ console.error(`Failed to load wiki folders of type ${type}:`, e)setState({ ...state, loading: false })} finally {if (isRoot) bucket.categoriesLoading = false}}+// reloadDirectoryForType resets and reloads both the folder skeleton and the+// pages for one tab. Used after a structural mutation (move page, create /+// rename / delete folder) so the tree reflects the new layout authoritatively+// instead of guessing at the optimistic delta.+async function reloadDirectoryForType(type: string) {+ clearDirectoryStateForType(type)+ await loadPagesForType(type, { reset: true })+ await loadCategoriesForType(type, { reset: true })+}++// --- Drag-and-drop: move a page or folder into a folder ------------------+// draggedItem holds whatever is being dragged — a page (move into folder) or a+// folder (reparent). dropTargetKey is the row highlighted as the hover target+// ("__root__" for the toolbar drop zone). Both clear on dragend / drop so a+// cancelled drag leaves no sticky highlight.+type DraggedItem =+ | { kind: 'page'; page: WikiPage }+ | { kind: 'folder'; folderId: string; path: string[] }+const draggedItem = ref<DraggedItem | null>(null)+const dropTargetKey = ref<string>('')++// primeDragData sets dataTransfer so the native HTML5 drag actually starts.+// Firefox refuses to initiate a drag unless some data is set, and without an+// explicit effectAllowed/dropEffect the cursor shows "no-drop" and the drop+// event never fires — which is why the move felt impossible to trigger.+function primeDragData(e: DragEvent) {+ if (!e.dataTransfer) return+ e.dataTransfer.effectAllowed = 'move'+ try {+ e.dataTransfer.setData('text/plain', '')+ } catch {+ // Some browsers throw if setData is called outside a real dragstart; ignore.+ }+}++function onPageDragStart(e: DragEvent, page: WikiPage) {+ draggedItem.value = { kind: 'page', page }+ primeDragData(e)+}++function onFolderDragStart(e: DragEvent, folderId: string, path: string[]) {+ if (!folderId) return+ draggedItem.value = { kind: 'folder', folderId, path }+ primeDragData(e)+}++function onPageDragEnd() {+ draggedItem.value = null+ dropTargetKey.value = ''+}++function onDirectoryDragOver(e: DragEvent, pathKey: string) {+ if (!draggedItem.value) return+ if (e.dataTransfer) e.dataTransfer.dropEffect = 'move'+ dropTargetKey.value = pathKey+}++// onRootDragOver fires on the list container itself. Folder rows stop their+// dragover from bubbling here, so reaching this handler means the cursor is+// over empty list space (or a page row) — i.e. a "move to root" target.+function onRootDragOver(e: DragEvent) {+ if (!draggedItem.value) return+ if (e.dataTransfer) e.dataTransfer.dropEffect = 'move'+ dropTargetKey.value = '__root__'+}++function onDirectoryDragLeave(pathKey: string) {+ if (dropTargetKey.value === pathKey) dropTargetKey.value = ''+}++// A move is staged here on drop and only executed once the user confirms via+// the in-place confirmation popup (anchored at the drop point). This avoids+// silently mutating the tree on an accidental drag.+const pendingMove = ref<+ | { item: DraggedItem; folderId: string; targetLabel: string; x: number; y: number }+ | null+>(null)++function onDropOnDirectory(e: DragEvent, folderId: string, path: string[]) {+ const item = draggedItem.value+ draggedItem.value = null+ dropTargetKey.value = ''+ if (!item) return+ const targetPath = path.join('/')++ if (item.kind === 'page') {+ // No-op when the page already lives in this folder.+ if ((pageCategoryPath(item.page) || []).join('/') === targetPath) return+ } else {+ // Folder reparent. Reject no-op (same parent) and the illegal move of a+ // folder into itself or one of its descendants.+ const sourcePath = item.path.join('/')+ if (sourcePath === targetPath) return+ if (folderId === item.folderId) return+ if (targetPath === sourcePath || targetPath.startsWith(`${sourcePath}/`)) {+ MessagePlugin.warning(t('knowledgeEditor.wikiBrowser.moveFolderIntoSelf'))+ return+ }+ }++ const targetLabel = path.length > 0+ ? path[path.length - 1]+ : t('knowledgeEditor.wikiBrowser.rootFolderLabel')+ pendingMove.value = { item, folderId, targetLabel, x: e.clientX, y: e.clientY }+}++function cancelPendingMove() {+ pendingMove.value = null+}++async function confirmPendingMove() {+ const move = pendingMove.value+ pendingMove.value = null+ if (!move) return+ const { item, folderId } = move++ if (item.kind === 'page') {+ try {+ await moveWikiPage(props.knowledgeBaseId, item.page.slug, folderId)+ MessagePlugin.success(t('knowledgeEditor.wikiBrowser.movePageSuccess'))+ await reloadDirectoryForType(activeTab.value)+ } catch (e) {+ console.error('Failed to move wiki page:', e)+ MessagePlugin.error(t('knowledgeEditor.wikiBrowser.movePageFailed'))+ }+ return+ }++ try {+ await updateWikiFolder(props.knowledgeBaseId, item.folderId, { parent_id: folderId, move_parent: true })+ MessagePlugin.success(t('knowledgeEditor.wikiBrowser.moveFolderSuccess'))+ await reloadDirectoryForType(activeTab.value)+ } catch (e: any) {+ console.error('Failed to move wiki folder:', e)+ MessagePlugin.error(+ e?.response?.data?.message || t('knowledgeEditor.wikiBrowser.moveFolderFailed'),+ )+ }+}++// --- Folder create / rename / delete -------------------------------------+// These are invoked by the in-place WikiFolderActions popup (no full-page+// dialog). The popup owns the input / confirm surface; these handlers only do+// the API call, surface a toast, and reload the affected tab so the tree+// reflects the new layout authoritatively.+async function createFolder(parentId: string, parentPath: string[], name: string) {+ try {+ await createWikiFolder(props.knowledgeBaseId, parentId, name)+ MessagePlugin.success(t('knowledgeEditor.wikiBrowser.createFolderSuccess'))+ // Keep the parent expanded so the new child is visible.+ if (parentPath.length > 0) {+ collapsedDirectories.value = new Set(+ [...collapsedDirectories.value].filter(k => k !== directoryPathKey(activeTab.value, parentPath)),+ )+ }+ await reloadDirectoryForType(activeTab.value)+ } catch (e: any) {+ console.error('Failed to create wiki folder:', e)+ MessagePlugin.error(e?.response?.data?.message || t('knowledgeEditor.wikiBrowser.createFolderFailed'))+ }+}++// Inline rename: the directory row swaps its label for a text input instead+// of opening a popup. editingFolderId marks the active row; editingName backs+// the input. Committed on Enter / blur, abandoned on Escape.+const editingFolderId = ref('')+const editingName = ref('')++function startRenameFolder(folderId: string, currentName: string) {+ if (!folderId) return+ editingFolderId.value = folderId+ editingName.value = currentName+ // Only one rename input exists at a time; focus + select it once rendered.+ nextTick(() => {+ const el = document.querySelector('.wiki-directory-rename-input') as HTMLInputElement | null+ el?.focus()+ el?.select()+ })+}++function cancelRenameFolder() {+ editingFolderId.value = ''+ editingName.value = ''+}++async function commitRenameFolder(folderId: string, originalName: string) {+ if (editingFolderId.value !== folderId) return+ const name = editingName.value.trim()+ cancelRenameFolder()+ if (!name || name === originalName) return+ try {+ await updateWikiFolder(props.knowledgeBaseId, folderId, { name })+ MessagePlugin.success(t('knowledgeEditor.wikiBrowser.renameFolderSuccess'))+ await reloadDirectoryForType(activeTab.value)+ } catch (e: any) {+ console.error('Failed to rename wiki folder:', e)+ MessagePlugin.error(e?.response?.data?.message || t('knowledgeEditor.wikiBrowser.renameFolderFailed'))+ }+}++async function deleteFolder(folderId: string) {+ if (!folderId) return+ try {+ await deleteWikiFolder(props.knowledgeBaseId, folderId)+ MessagePlugin.success(t('knowledgeEditor.wikiBrowser.deleteFolderSuccess'))+ await reloadDirectoryForType(activeTab.value)+ } catch (e: any) {+ console.error('Failed to delete wiki folder:', e)+ MessagePlugin.error(e?.response?.data?.message || t('knowledgeEditor.wikiBrowser.deleteFolderFailed'))+ }+}+// loadPagesForType fetches the next page for a single type bucket. The// first call seeds `total` from the backend so subsequent `hasMore`// checks work without another round-trip. Guard against concurrent@@ -4271,6 +4529,9 @@ onUnmounted(() => {border-radius: 6px;cursor: pointer;margin-bottom: 2px;+ // Without this, mousedown-drag on the title text starts a text selection+ // instead of an HTML5 drag, so the page never picks up.+ user-select: none;transition: background 0.15s;&:hover {@@ -4295,14 +4556,85 @@ onUnmounted(() => {margin: 1px 0;color: var(--td-text-color-secondary);background: transparent;+ user-select: none;transition: background 0.15s, color 0.15s;&:hover {background: var(--td-bg-color-container-hover);color: var(--td-text-color-primary);++ :deep(.wiki-directory-action--reveal) {+ opacity: 1;+ }}}+.wiki-directory-rename-input {+ flex: 1;+ min-width: 0;+ height: 24px;+ border: 1px solid var(--td-brand-color);+ border-radius: 4px;+ padding: 0 6px;+ font-size: 13px;+ color: var(--td-text-color-primary);+ background: var(--td-bg-color-container);+ outline: none;+}++.wiki-directory-item--drop {+ background: var(--td-brand-color-light);+ box-shadow: inset 0 0 0 1px var(--td-brand-color);+}++.wiki-tree-toolbar {+ display: flex;+ justify-content: flex-end;+ align-items: center;+ padding: 2px 6px 0;+}++// While dragging, the whole list is the "move to root" target; a subtle inset+// ring signals it without inserting any element that would shift the layout.+.wiki-tree-list--root-drop {+ border-radius: 6px;+ box-shadow: inset 0 0 0 1px var(--td-brand-color);+}++// In-place move confirmation anchored at the drop point (teleported to body).+.wiki-move-confirm-mask {+ position: fixed;+ inset: 0;+ z-index: 3500;+}++.wiki-move-confirm {+ position: fixed;+ // x/y are the drop coords; nudge so the card sits just below-right of the+ // cursor. max-width + viewport clamping keep it on-screen for edge drops.+ transform: translate(8px, 8px);+ max-width: min(280px, calc(100vw - 24px));+ padding: 12px;+ border-radius: 8px;+ background: var(--td-bg-color-container);+ box-shadow: var(--td-shadow-2);+ border: 1px solid var(--td-component-border);+}++.wiki-move-confirm-text {+ font-size: 13px;+ line-height: 1.4;+ color: var(--td-text-color-primary);+ word-break: break-word;+}++.wiki-move-confirm-footer {+ display: flex;+ justify-content: flex-end;+ gap: 8px;+ margin-top: 12px;+}+.wiki-directory-toggle,.wiki-directory-icon,.wiki-page-file-icon {diff --git a/frontend/src/views/knowledge/wiki/WikiFolderActions.vue b/frontend/src/views/knowledge/wiki/WikiFolderActions.vuenew file mode 100644index 000000000..519d8dd58--- /dev/null+++ b/frontend/src/views/knowledge/wiki/WikiFolderActions.vue@@ -0,0 +1,230 @@+<template>+ <!-- Anchored in-place popup for folder actions. The trigger is icon-only to+ keep the directory row compact; everything (menu, name input, delete+ confirm) happens inside this one popup so no full-page dialog is spawned.+ click.stop on the trigger and content prevents the surrounding directory+ row from treating the interaction as an expand/collapse toggle. -->+ <t-popup+ v-model:visible="open"+ trigger="click"+ placement="bottom-start"+ destroy-on-close+ overlay-class-name="card-more-popup wiki-folder-action-overlay"+ @visible-change="onVisibleChange"+ >+ <span+ :class="[+ 'wiki-directory-action',+ { 'wiki-directory-action--reveal': !createOnly, 'is-open': open, 'wiki-directory-action--labeled': createOnly && label },+ ]"+ :title="createOnly ? t('knowledgeEditor.wikiBrowser.newFolder') : t('knowledgeEditor.wikiBrowser.folderActions')"+ @click.stop+ @dragstart.prevent.stop+ >+ <t-icon :name="createOnly ? 'folder-add' : 'more'" />+ <span v-if="createOnly && label" class="wiki-directory-action-label">{{ label }}</span>+ </span>+ <template #content>+ <div class="wiki-folder-menu" @click.stop>+ <div v-if="mode === 'menu'" class="popup-menu">+ <div class="popup-menu-item" @click="enterMode('create')">+ <t-icon name="folder-add" class="menu-icon" />+ <span>{{ t('knowledgeEditor.wikiBrowser.newSubfolder') }}</span>+ </div>+ <div class="popup-menu-item" @click="emitRename">+ <t-icon name="edit" class="menu-icon" />+ <span>{{ t('knowledgeEditor.wikiBrowser.renameFolder') }}</span>+ </div>+ <div class="popup-menu-item delete" @click="enterMode('delete')">+ <t-icon name="delete" class="menu-icon" />+ <span>{{ t('knowledgeEditor.wikiBrowser.deleteFolder') }}</span>+ </div>+ </div>++ <template v-else-if="mode === 'create'">+ <t-input+ ref="inputRef"+ v-model="nameInput"+ :placeholder="t('knowledgeEditor.wikiBrowser.folderNamePlaceholder')"+ @enter="submitName"+ />+ <div class="wiki-folder-menu-footer">+ <t-button size="small" variant="outline" @click="open = false">+ {{ t('common.cancel') }}+ </t-button>+ <t-button size="small" theme="primary" :disabled="!nameInput.trim()" @click="submitName">+ {{ t('common.confirm') }}+ </t-button>+ </div>+ </template>++ <template v-else>+ <div v-if="!deletable" class="wiki-folder-confirm">+ {{ t('knowledgeEditor.wikiBrowser.deleteFolderNotEmpty') }}+ </div>+ <div v-else class="wiki-folder-confirm">+ {{ t('knowledgeEditor.wikiBrowser.deleteFolderConfirm', { name }) }}+ </div>+ <div class="wiki-folder-menu-footer">+ <t-button size="small" variant="outline" @click="open = false">+ {{ deletable ? t('common.cancel') : t('common.confirm') }}+ </t-button>+ <t-button v-if="deletable" size="small" theme="danger" @click="submitDelete">+ {{ t('common.confirm') }}+ </t-button>+ </div>+ </template>+ </div>+ </template>+ </t-popup>+</template>++<script setup lang="ts">+import { ref, computed, nextTick } from 'vue'+import { useI18n } from 'vue-i18n'++const props = withDefaults(defineProps<{+ name?: string+ pageCount?: number+ hasChildren?: boolean+ // createOnly turns the trigger into a single "new folder" affordance that+ // opens straight to the name input (used by the root-folder toolbar entry).+ createOnly?: boolean+ // label, when set on a createOnly trigger, renders text next to the icon so+ // the toolbar entry reads as a button instead of a bare icon.+ label?: string+}>(), {+ name: '',+ pageCount: 0,+ hasChildren: false,+ createOnly: false,+ label: '',+})++const emit = defineEmits<{+ (e: 'create', name: string): void+ (e: 'rename'): void+ (e: 'delete'): void+}>()++const { t } = useI18n()++const open = ref(false)+const mode = ref<'menu' | 'create' | 'delete'>('menu')+const nameInput = ref('')+const inputRef = ref<{ focus: () => void } | null>(null)++const deletable = computed(() => props.pageCount === 0 && !props.hasChildren)++function onVisibleChange(visible: boolean) {+ if (!visible) {+ mode.value = 'menu'+ nameInput.value = ''+ return+ }+ // createOnly skips the menu and lands directly on the name input.+ if (props.createOnly) {+ enterMode('create')+ } else {+ mode.value = 'menu'+ }+}++function enterMode(next: 'create' | 'delete') {+ mode.value = next+ if (next === 'create') {+ nameInput.value = ''+ nextTick(() => inputRef.value?.focus())+ }+}++function emitRename() {+ emit('rename')+ open.value = false+}++function submitName() {+ const value = nameInput.value.trim()+ if (!value) return+ emit('create', value)+ open.value = false+}++function submitDelete() {+ emit('delete')+ open.value = false+}+</script>++<style lang="less" scoped>+.wiki-directory-action {+ flex: 0 0 auto;+ display: inline-flex;+ align-items: center;+ font-size: 15px;+ color: var(--td-text-color-placeholder);+ cursor: pointer;+ transition: color 0.15s, opacity 0.15s;++ &:hover {+ color: var(--td-brand-color);+ }+}++// Hover-reveal for the per-folder "more" trigger: hidden by default, shown when+// the row is hovered (parent supplies that rule via :deep) or the popup is open.+.wiki-directory-action--reveal {+ opacity: 0;++ &.is-open {+ opacity: 1;+ }+}++// Labeled variant: the toolbar "new folder" button reads as icon + text.+.wiki-directory-action--labeled {+ gap: 4px;+ padding: 2px 6px;+ border-radius: 6px;+ font-size: 12px;+ color: var(--td-text-color-secondary);++ &:hover {+ color: var(--td-brand-color);+ background: var(--td-bg-color-container-hover);+ }+}++.wiki-directory-action-label {+ line-height: 1;+}+</style>++<style lang="less">+// Popup content teleports to <body>, so these must be global. The menu chrome+// (background, border, shadow, item styling) comes from the shared+// `card-more-popup` + `.popup-menu` rules in assets/dropdown-menu.less so this+// menu matches every other dropdown in the app; only the create-input and+// delete-confirm panes need a little extra layout here.+.wiki-folder-action-overlay {+ .wiki-folder-menu {+ min-width: 188px;+ }++ .wiki-folder-confirm {+ font-size: 13px;+ line-height: 1.5;+ color: var(--td-text-color-primary);+ padding: 4px 6px 8px;+ max-width: 280px;+ }++ .wiki-folder-menu-footer {+ display: flex;+ justify-content: flex-end;+ gap: 8px;+ margin-top: 8px;+ padding: 0 2px;+ }+}+</style>diff --git a/frontend/src/views/platform/index.vue b/frontend/src/views/platform/index.vueindex 40929e53f..630b1a4a2 100644--- a/frontend/src/views/platform/index.vue+++ b/frontend/src/views/platform/index.vue@@ -135,8 +135,19 @@ const checkKnowledgeBaseInitialization = async (): Promise<boolean> => {}+// isFileDrag distinguishes an OS file drag (the only thing the global upload+// drop zone cares about) from an in-app element drag such as the wiki+// folder/page drag-and-drop. Element drags carry only "text/*" types, never+// "Files", so we bail out and let the originating component handle the drop.+const isFileDrag = (event: DragEvent): boolean => {+ const types = event.dataTransfer?.types+ if (!types) return false+ return Array.from(types).includes('Files')+}+// 全局拖拽事件处理const handleGlobalDragEnter = (event: DragEvent) => {+ if (!isFileDrag(event)) return;event.preventDefault();dragCounter++;if (event.dataTransfer) {@@ -146,6 +157,7 @@ const handleGlobalDragEnter = (event: DragEvent) => {}const handleGlobalDragOver = (event: DragEvent) => {+ if (!isFileDrag(event)) return;event.preventDefault();if (event.dataTransfer) {event.dataTransfer.dropEffect = 'copy';@@ -153,6 +165,7 @@ const handleGlobalDragOver = (event: DragEvent) => {}const handleGlobalDragLeave = (event: DragEvent) => {+ if (!isFileDrag(event)) return;event.preventDefault();dragCounter--;if (dragCounter === 0) {@@ -161,6 +174,7 @@ const handleGlobalDragLeave = (event: DragEvent) => {}const handleGlobalDrop = async (event: DragEvent) => {+ if (!isFileDrag(event)) return;event.preventDefault();dragCounter = 0;ismask.value = false;diff --git a/internal/application/repository/wiki_page.go b/internal/application/repository/wiki_page.goindex f8e507773..1db1f0cab 100644--- a/internal/application/repository/wiki_page.go+++ b/internal/application/repository/wiki_page.go@@ -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 []stringif 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+// ErrWikiFolderNotFound is returned when a wiki folder is not found.+var ErrWikiFolderNotFound = errors.New("wiki folder not found")++// 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 nil, err}- 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)]-- // 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)+ return &folder, 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 q+ return nil, err}+ return &folder, nil+}- var total int64- if err := scoped().Distinct(childCol).Count(&total).Error; err != nil {- return nil, 0, err+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}- if total == 0 {- return nil, 0, nil+ return folders, nil+}++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+}- if page < 1 {- page = 1+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}- if pageSize < 1 {- pageSize = 2000+ if result.RowsAffected == 0 {+ return ErrWikiFolderNotFound}+ return nil+}- type row struct {- Label string `gorm:"column:label"`- Cnt int64 `gorm:"column:cnt"`+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}- 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+ 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+}- 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) 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, int(total), nil+ 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 thediff --git a/internal/application/repository/wiki_page_test.go b/internal/application/repository/wiki_page_test.goindex bde52fb8f..cd184ed39 100644--- a/internal/application/repository/wiki_page_test.go+++ b/internal/application/repository/wiki_page_test.go@@ -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.Titlepage.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 {- require.NoError(t, repo.Create(ctx, p))+ 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))}- 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)+ // Root children: AI, 人物 (ordered by name within equal sort_order).+ roots, err := repo.ListChildFolders(ctx, "kb-f", types.WikiFolderRootID)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++ // 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))}- assert.Equal(t, int64(3), byLabel["AI"])- assert.Equal(t, int64(1), byLabel["人物"])- // Child of [AI]: only the [AI, LLM] page.- children, total, err := repo.ListDistinctCategoryPathsByType(ctx, "kb-c", types4, []string{"AI"}, 1, 100)+ aiCount, err := repo.CountPagesInFolder(ctx, "kb-f", "f-ai")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)-- // 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)+ assert.Equal(t, int64(1), aiCount, "archived page excluded; LLM page is under f-llm, not f-ai")++ // ListDistinctCategoryPaths returns the folder paths split into segments.+ paths, err := repo.ListDistinctCategoryPaths(ctx, "kb-f", 100)+ require.NoError(t, err)+ assert.Contains(t, paths, []string{"AI"})+ assert.Contains(t, paths, []string{"AI", "LLM"})+ assert.Contains(t, paths, []string{"人物"})++ // 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 verifiesdiff --git a/internal/application/service/wiki_ingest.go b/internal/application/service/wiki_ingest.goindex 591d291b1..60aa2e38c 100644--- a/internal/application/service/wiki_ingest.go+++ b/internal/application/service/wiki_ingest.go@@ -659,12 +659,15 @@ type WikiBatchContext struct {// three valid values.ExtractionGranularity types.WikiExtractionGranularity- // PlannedCategory holds the per-slug directory path assigned by the batch- // taxonomy planning pass (planBatchTaxonomy), keyed by page slug. Reduce- // applies it only to pages that don't already have a category, so the whole- // batch lands on one coherent tree without churning user-curated pages.- // Populated before the reduce phase and read-only during it.- PlannedCategory map[string][]string+ // PlannedFolderID holds the per-slug wiki_folders.id assigned by the batch+ // taxonomy planning pass (planBatchTaxonomy + folder resolution), keyed by+ // page slug. Reduce applies it only to pages that aren't already filed+ // (FolderID == ""), so the whole batch lands on one coherent tree without+ // churning user-curated placements. The folders themselves are created+ // sequentially before reduce, so the parallel reduce phase only assigns+ // pre-resolved ids and never races on folder creation. Read-only during+ // reduce.+ PlannedFolderID map[string]string}// SlugUpdate represents a single update operation for a specific slugdiff --git a/internal/application/service/wiki_ingest_batch.go b/internal/application/service/wiki_ingest_batch.goindex 4be3ebdb0..a78aacc54 100644--- a/internal/application/service/wiki_ingest_batch.go+++ b/internal/application/service/wiki_ingest_batch.go@@ -490,7 +490,8 @@ func (s *wikiIngestService) ProcessWikiIngest(ctx context.Context, t *asynq.Task// single pass assigns every new entity/concept slug a coherent category_path// that reuses existing folders. Reduce then only applies the plan to pages// that don't already have a category (user-curated pages are never churned).- batchCtx.PlannedCategory = s.planBatchTaxonomy(ctx, chatModel, kb, slugUpdates, lang)+ batchCtx.PlannedFolderID = s.resolvePlannedFolders(ctx, kb,+ s.planBatchTaxonomy(ctx, chatModel, kb, slugUpdates, lang))// 2. REDUCE PHASE (Parallel upserting grouped by Slug)egReduce, reduceCtx := errgroup.WithContext(ctx)@@ -1637,13 +1638,14 @@ func (s *wikiIngestService) reduceSlugUpdates(}}- // Apply the batch taxonomy plan, but only to pages that don't already have a- // category — so brand-new pages get filed coherently while previously-filed- // or user-curated categories are preserved (manual edits are authoritative).- if len(page.CategoryPath) == 0 && batchCtx != nil {- if planned := types.CleanWikiCategoryPath(batchCtx.PlannedCategory[slug]); len(planned) > 0 {- page.CategoryPath = types.StringArray(planned)- page.WikiPath = ""+ // Apply the batch taxonomy plan, but only to pages that aren't already+ // filed — so brand-new pages get a coherent folder while previously-filed+ // or user-moved pages keep their placement (manual edits are authoritative).+ // The page's category_path cache is derived from folder_id downstream by+ // CreatePage/UpdatePage, so assigning the folder id is sufficient here.+ if page.FolderID == "" && batchCtx != nil {+ if fid := batchCtx.PlannedFolderID[slug]; fid != "" {+ page.FolderID = fid}}diff --git a/internal/application/service/wiki_ingest_taxonomy.go b/internal/application/service/wiki_ingest_taxonomy.goindex 4b3c19b92..2d3636b4e 100644--- a/internal/application/service/wiki_ingest_taxonomy.go+++ b/internal/application/service/wiki_ingest_taxonomy.go@@ -101,6 +101,44 @@ func (s *wikiIngestService) planBatchTaxonomy(return result}+// resolvePlannedFolders reifies the planner's per-slug paths into real+// wiki_folders rows and returns a slug -> folder id map. Folder creation is+// done here, sequentially and before the parallel reduce phase, so reduce only+// assigns pre-resolved ids and never races two goroutines into creating the+// same folder. Distinct paths are resolved once and cached. Blank paths (and+// any resolution failure) map to the root and are simply omitted.+func (s *wikiIngestService) resolvePlannedFolders(+ ctx context.Context, kb *types.KnowledgeBase, planned map[string][]string,+) map[string]string {+ if kb == nil || len(planned) == 0 || s.wikiService == nil {+ return nil+ }+ pathCache := make(map[string]string) // "a/b" -> folder id+ out := make(map[string]string, len(planned))+ for slug, path := range planned {+ clean := types.CleanWikiCategoryPath(path)+ if len(clean) == 0 {+ continue+ }+ key := strings.Join(clean, "/")+ fid, ok := pathCache[key]+ if !ok {+ resolved, _, err := s.wikiService.FindOrCreateFolderPath(ctx, kb.ID, kb.TenantID, clean)+ if err != nil {+ logger.Warnf(ctx, "wiki ingest: resolve folder %q failed: %v", key, err)+ pathCache[key] = "" // negative-cache so we don't retry per slug+ continue+ }+ fid = resolved+ pathCache[key] = fid+ }+ if fid != "" {+ out[slug] = fid+ }+ }+ return out+}+// selectRelevantFolders narrows the existing folder pool to the subset worth// showing the planner for THIS batch. A healthy navigation directory is small,// so it is fed whole (perfect reuse recall, no embedding cost). Only once foldersdiff --git a/internal/application/service/wiki_page.go b/internal/application/service/wiki_page.goindex b2b57904f..61a7c4bf6 100644--- a/internal/application/service/wiki_page.go+++ b/internal/application/service/wiki_page.go@@ -67,6 +67,9 @@ func (s *wikiPageService) CreatePage(ctx context.Context, page *types.WikiPage)// Parse outbound links from contentpage.OutLinks = s.parseOutLinks(page.Content)+ if err := s.applyFolderToPage(ctx, page); err != nil {+ return nil, err+ }normalizeWikiHierarchy(page)now := time.Now()@@ -117,13 +120,17 @@ func (s *wikiPageService) UpdatePage(ctx context.Context, page *types.WikiPage)existing.ChunkRefs = page.ChunkRefsexisting.PageMetadata = page.PageMetadataexisting.ParentSlug = page.ParentSlug- existing.CategoryPath = page.CategoryPath- existing.WikiPath = page.WikiPath- existing.Depth = page.Depth+ existing.FolderID = page.FolderIDexisting.SortOrder = page.SortOrderexisting.Status = page.Statusexisting.UpdatedAt = time.Now()+ // CategoryPath is a derived cache of FolderID — recompute it from the+ // folder chain rather than trusting whatever the caller sent.+ if err := s.applyFolderToPage(ctx, existing); err != nil {+ return nil, err+ }+// Outbound links are a pure derivative of content, so they only shift// when content shifts. Re-parse unconditionally to stay consistent with// the stored body.@@ -831,21 +838,12 @@ func (s *wikiPageService) FindSimilarPages(ctx context.Context, kbID string, quereturn s.repo.FindSimilarPages(ctx, kbID, query, pageTypes, limit)}-// ListDistinctCategoryPaths returns unique category_path folders already used-// by entity/concept pages. Used by wiki ingest to ground extraction prompts.+// ListDistinctCategoryPaths returns the existing wiki folder paths. Used by+// wiki ingest's taxonomy planner to ground folder reuse.func (s *wikiPageService) ListDistinctCategoryPaths(ctx context.Context, kbID string, maxPaths int) ([][]string, error) {return s.repo.ListDistinctCategoryPaths(ctx, kbID, maxPaths)}-// ListDistinctCategoryPathsByType returns the direct child folders of-// parentPath across the given page types, paginated, plus the total distinct-// child count. The browser uses this to expand the directory tree level by-// level; passing several types lets one tab (e.g. the merged knowledge tab)-// build a single directory skeleton server-side.-func (s *wikiPageService) ListDistinctCategoryPathsByType(ctx context.Context, kbID string, pageTypes []string, parentPath []string, page, pageSize int) ([]types.WikiCategoryPath, int, error) {- return s.repo.ListDistinctCategoryPathsByType(ctx, kbID, pageTypes, parentPath, page, pageSize)-}-// CountByType is a service-layer pass-through over the repo. Used by// the index intro path to frame the LLM prompt's "showing N of M" hint.func (s *wikiPageService) CountByType(ctx context.Context, kbID string) (map[string]int64, error) {@@ -969,7 +967,6 @@ func normalizeWikiHierarchy(page *types.WikiPage) {cleanPath := types.StringArray(types.CleanWikiCategoryPath(page.CategoryPath))page.CategoryPath = cleanPathpage.Depth = len(cleanPath)- page.CategoryL1, page.CategoryL2, page.CategoryL3 = types.WikiCategoryLevels(cleanPath)display := strings.TrimSpace(page.Title)if display == "" {@@ -1050,6 +1047,389 @@ func (s *wikiPageService) UpdateIssueStatus(ctx context.Context, issueID string,return s.repo.UpdateIssueStatus(ctx, issueID, status)}+// --- Folder tree (wiki_folders) ---++// wikiFolderSegments splits a materialized folder path ("AI/RAG") into cleaned+// segments. Empty/blank path yields nil (the wiki root).+func wikiFolderSegments(path string) []string {+ if strings.TrimSpace(path) == "" {+ return nil+ }+ return types.CleanWikiCategoryPath(strings.Split(path, "/"))+}++// applyFolderToPage refreshes a page's derived category_path cache from its+// authoritative FolderID. Root ("") clears the path. A folder id that does not+// resolve is treated as a hard error so we never silently misplace a page.+func (s *wikiPageService) applyFolderToPage(ctx context.Context, page *types.WikiPage) error {+ if page == nil {+ return nil+ }+ if strings.TrimSpace(page.FolderID) == "" {+ page.FolderID = ""+ page.CategoryPath = nil+ return nil+ }+ folder, err := s.repo.GetFolderByID(ctx, page.KnowledgeBaseID, page.FolderID)+ if err != nil {+ if errors.Is(err, repository.ErrWikiFolderNotFound) {+ return fmt.Errorf("wiki page references unknown folder %q", page.FolderID)+ }+ return fmt.Errorf("resolve page folder: %w", err)+ }+ page.CategoryPath = types.StringArray(wikiFolderSegments(folder.Path))+ return nil+}++// GetFolder retrieves a single folder by id.+func (s *wikiPageService) GetFolder(ctx context.Context, kbID string, id string) (*types.WikiFolder, error) {+ return s.repo.GetFolderByID(ctx, kbID, id)+}++// ListChildFolders returns the direct children of parentID for a tree view+// scoped to pageTypes. PageCount is recursive (the folder's whole subtree) so+// a parent reflects everything filed beneath it, and a folder is only shown+// when its subtree holds a page of pageTypes or it is entirely empty — which+// keeps, say, the summary tab from listing folders that only hold entity pages.+func (s *wikiPageService) ListChildFolders(+ ctx context.Context, kbID string, parentID string, pageTypes []string,+) ([]types.WikiFolderNode, error) {+ all, err := s.repo.ListAllFolders(ctx, kbID)+ if err != nil {+ return nil, err+ }+ scopedDirect, err := s.repo.CountPagesByFolder(ctx, kbID, pageTypes)+ if err != nil {+ return nil, err+ }+ allDirect := scopedDirect+ if len(pageTypes) > 0 {+ allDirect, err = s.repo.CountPagesByFolder(ctx, kbID, nil)+ if err != nil {+ return nil, err+ }+ }+ recScoped := recursiveFolderCounts(all, scopedDirect)+ recAll := recursiveFolderCounts(all, allDirect)+ // A folder belongs in this view if it (recursively) contains a page of the+ // requested types, or if it is a completely empty container (no pages of+ // any type anywhere underneath).+ relevant := func(id string) bool { return recScoped[id] > 0 || recAll[id] == 0 }++ out := make([]types.WikiFolderNode, 0)+ for _, f := range all {+ if f.ParentID != parentID || !relevant(f.ID) {+ continue+ }+ hasChildren := false+ for _, g := range all {+ if g.ParentID == f.ID && relevant(g.ID) {+ hasChildren = true+ break+ }+ }+ out = append(out, types.WikiFolderNode{+ WikiFolder: *f,+ PageCount: recScoped[f.ID],+ HasChildren: hasChildren,+ })+ }+ return out, nil+}++// recursiveFolderCounts maps each folder id to the sum of `direct` page counts+// over the folder and all of its descendants, using the materialized path so a+// single pass over the (navigation-sized) folder set suffices.+func recursiveFolderCounts(all []*types.WikiFolder, direct map[string]int64) map[string]int64 {+ res := make(map[string]int64, len(all))+ for _, f := range all {+ sum := direct[f.ID]+ prefix := f.Path + "/"+ for _, g := range all {+ if g.ID != f.ID && strings.HasPrefix(g.Path, prefix) {+ sum += direct[g.ID]+ }+ }+ res[f.ID] = sum+ }+ return res+}++// validateFolderName trims and rejects blank names or names carrying directory+// separators (a folder name is a single tree level).+func validateFolderName(name string) (string, error) {+ name = strings.TrimSpace(name)+ if name == "" {+ return "", errors.New("folder name is required")+ }+ if strings.ContainsAny(name, "/||/") {+ return "", fmt.Errorf("folder name %q must not contain a path separator", name)+ }+ return name, nil+}++// CreateFolder creates a new empty folder under parentID.+func (s *wikiPageService) CreateFolder(+ ctx context.Context, kbID string, tenantID uint64, parentID string, name string,+) (*types.WikiFolder, error) {+ name, err := validateFolderName(name)+ if err != nil {+ return nil, err+ }++ parentPath := ""+ depth := 1+ if parentID != types.WikiFolderRootID {+ parent, err := s.repo.GetFolderByID(ctx, kbID, parentID)+ if err != nil {+ return nil, err+ }+ parentPath = parent.Path+ depth = parent.Depth + 1+ }++ if _, err := s.repo.GetChildFolderByName(ctx, kbID, parentID, name); err == nil {+ return nil, repository.ErrWikiFolderConflict+ } else if !errors.Is(err, repository.ErrWikiFolderNotFound) {+ return nil, err+ }++ path := name+ if parentPath != "" {+ path = parentPath + "/" + name+ }+ now := time.Now()+ folder := &types.WikiFolder{+ ID: uuid.New().String(),+ TenantID: tenantID,+ KnowledgeBaseID: kbID,+ ParentID: parentID,+ Name: name,+ Path: path,+ Depth: depth,+ CreatedAt: now,+ UpdatedAt: now,+ }+ if err := s.repo.CreateFolder(ctx, folder); err != nil {+ return nil, fmt.Errorf("create wiki folder: %w", err)+ }+ return folder, nil+}++// FindOrCreateFolderPath resolves a category path to a leaf folder id, creating+// any missing intermediate folders along the way. Concurrency-safe against the+// unique (kb, parent, name) constraint via a re-fetch on create conflict.+func (s *wikiPageService) FindOrCreateFolderPath(+ ctx context.Context, kbID string, tenantID uint64, path []string,+) (string, []string, error) {+ clean := types.CleanWikiCategoryPath(path)+ if len(clean) == 0 {+ return types.WikiFolderRootID, nil, nil+ }+ parentID := types.WikiFolderRootID+ parentPath := ""+ for depth, name := range clean {+ child, err := s.repo.GetChildFolderByName(ctx, kbID, parentID, name)+ if err != nil {+ if !errors.Is(err, repository.ErrWikiFolderNotFound) {+ return "", nil, err+ }+ fp := name+ if parentPath != "" {+ fp = parentPath + "/" + name+ }+ now := time.Now()+ child = &types.WikiFolder{+ ID: uuid.New().String(),+ TenantID: tenantID,+ KnowledgeBaseID: kbID,+ ParentID: parentID,+ Name: name,+ Path: fp,+ Depth: depth + 1,+ CreatedAt: now,+ UpdatedAt: now,+ }+ if cerr := s.repo.CreateFolder(ctx, child); cerr != nil {+ // Lost a create race (or unique violation): the sibling must+ // now exist — re-fetch it rather than failing the whole plan.+ child, err = s.repo.GetChildFolderByName(ctx, kbID, parentID, name)+ if err != nil {+ return "", nil, fmt.Errorf("create wiki folder %q: %w", fp, cerr)+ }+ }+ }+ parentID = child.ID+ parentPath = child.Path+ }+ return parentID, clean, nil+}++// MovePage relocates a page into folderID ("" = root) and refreshes its cached+// category path. Bookkeeping-only write (no version bump).+func (s *wikiPageService) MovePage(+ ctx context.Context, kbID string, slug string, folderID string,+) (*types.WikiPage, error) {+ page, err := s.repo.GetBySlug(ctx, kbID, slug)+ if err != nil {+ return nil, err+ }+ page.FolderID = strings.TrimSpace(folderID)+ if err := s.applyFolderToPage(ctx, page); err != nil {+ return nil, err+ }+ page.UpdatedAt = time.Now()+ normalizeWikiHierarchy(page)+ if err := s.repo.UpdateMeta(ctx, page); err != nil {+ return nil, fmt.Errorf("move wiki page: %w", err)+ }+ return page, nil+}++// RenameOrMoveFolder renames and/or reparents a folder, then recomputes the+// materialized path/depth of the entire subtree and the cached category path of+// every page underneath. Guards against cycles (moving a folder into itself or+// one of its descendants) and sibling name collisions.+func (s *wikiPageService) RenameOrMoveFolder(+ ctx context.Context, kbID string, id string, newName string, newParentID string, moveParent bool,+) (*types.WikiFolder, error) {+ folder, err := s.repo.GetFolderByID(ctx, kbID, id)+ if err != nil {+ return nil, err+ }++ name := folder.Name+ if strings.TrimSpace(newName) != "" {+ if name, err = validateFolderName(newName); err != nil {+ return nil, err+ }+ }++ targetParent := folder.ParentID+ if moveParent {+ targetParent = newParentID+ }++ parentPath := ""+ depthBase := 0+ if targetParent != types.WikiFolderRootID {+ if targetParent == folder.ID {+ return nil, errors.New("cannot move a folder into itself")+ }+ parent, err := s.repo.GetFolderByID(ctx, kbID, targetParent)+ if err != nil {+ return nil, err+ }+ if parent.Path == folder.Path || strings.HasPrefix(parent.Path, folder.Path+"/") {+ return nil, errors.New("cannot move a folder into its own descendant")+ }+ parentPath = parent.Path+ depthBase = parent.Depth+ }++ if existing, err := s.repo.GetChildFolderByName(ctx, kbID, targetParent, name); err == nil {+ if existing.ID != folder.ID {+ return nil, repository.ErrWikiFolderConflict+ }+ } else if !errors.Is(err, repository.ErrWikiFolderNotFound) {+ return nil, err+ }++ oldPath := folder.Path+ newPath := name+ if parentPath != "" {+ newPath = parentPath + "/" + name+ }+ if newPath == oldPath && targetParent == folder.ParentID {+ return folder, nil // no-op+ }++ all, err := s.repo.ListAllFolders(ctx, kbID)+ if err != nil {+ return nil, err+ }+ now := time.Now()+ affected := make([]string, 0)+ var updated *types.WikiFolder+ for _, f := range all {+ switch {+ case f.ID == folder.ID:+ f.ParentID = targetParent+ f.Name = name+ f.Path = newPath+ f.Depth = depthBase + 1+ case strings.HasPrefix(f.Path, oldPath+"/"):+ f.Path = newPath + f.Path[len(oldPath):]+ f.Depth = len(wikiFolderSegments(f.Path))+ default:+ continue+ }+ f.UpdatedAt = now+ if err := s.repo.UpdateFolder(ctx, f); err != nil {+ return nil, err+ }+ affected = append(affected, f.ID)+ if f.ID == folder.ID {+ updated = f+ }+ }++ if err := s.recomputePagesForFolders(ctx, kbID, affected); err != nil {+ return nil, err+ }+ if updated == nil {+ updated = folder+ }+ return updated, nil+}++// recomputePagesForFolders refreshes the cached category_path/wiki_path/depth of+// every page filed under any of the given folder ids (used after a folder+// subtree is moved/renamed). Bookkeeping-only writes (no version bump).+func (s *wikiPageService) recomputePagesForFolders(ctx context.Context, kbID string, folderIDs []string) error {+ if len(folderIDs) == 0 {+ return nil+ }+ pages, err := s.repo.ListPagesByFolderIDs(ctx, kbID, folderIDs)+ if err != nil {+ return err+ }+ for _, page := range pages {+ if err := s.applyFolderToPage(ctx, page); err != nil {+ return err+ }+ page.UpdatedAt = time.Now()+ normalizeWikiHierarchy(page)+ if err := s.repo.UpdateMeta(ctx, page); err != nil {+ logger.Warnf(ctx, "wiki: recompute folder path for page %s failed: %v", page.Slug, err)+ }+ }+ return nil+}++// DeleteFolder removes a folder that has no pages and no child folders. The UI+// must relocate contents first; this keeps deletion non-destructive.+func (s *wikiPageService) DeleteFolder(ctx context.Context, kbID string, id string) error {+ if _, err := s.repo.GetFolderByID(ctx, kbID, id); err != nil {+ return err+ }+ children, err := s.repo.ListChildFolders(ctx, kbID, id)+ if err != nil {+ return err+ }+ if len(children) > 0 {+ return errors.New("folder is not empty: it still has sub-folders")+ }+ pages, err := s.repo.ListPagesByFolderIDs(ctx, kbID, []string{id})+ if err != nil {+ return err+ }+ if len(pages) > 0 {+ return errors.New("folder is not empty: it still contains pages")+ }+ return s.repo.DeleteFolder(ctx, kbID, id)+}+// InjectCrossLinks scans affected pages and injects [[wiki-links]] for mentions// of other wiki page titles in the content. Pure text replacement, no LLM call.// Shares the linkifyContent helper with the ingest pipeline so both paths honordiff --git a/internal/handler/wiki_page.go b/internal/handler/wiki_page.goindex 38a184c7c..2d7ea4cd9 100644--- a/internal/handler/wiki_page.go+++ b/internal/handler/wiki_page.go@@ -98,6 +98,13 @@ func (h *WikiPageHandler) ListPages(c *gin.Context) {page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))pageSize, _ := strconv.Atoi(c.DefaultQuery("page_size", "20"))categoryPath := parseWikiCategoryPath(c.Query("category_path"))+ // folder_id is an exact placement filter. An explicitly-present but empty+ // value means "root" (folder_id = ''); an absent param means "no filter".+ var folderID *string+ if raw, ok := c.GetQuery("folder_id"); ok {+ raw = strings.TrimSpace(raw)+ folderID = &raw+ }var categoryDepth *intif raw := c.Query("category_depth"); raw != "" {if depth, parseErr := strconv.Atoi(raw); parseErr == nil && depth >= 0 {@@ -110,6 +117,7 @@ func (h *WikiPageHandler) ListPages(c *gin.Context) {PageType: c.Query("page_type"),Status: c.Query("status"),Query: c.Query("query"),+ FolderID: folderID,CategoryPath: types.StringArray(categoryPath),CategoryDepth: categoryDepth,Page: page,@@ -127,63 +135,190 @@ func (h *WikiPageHandler) ListPages(c *gin.Context) {c.JSON(http.StatusOK, resp)}-// ListCategories godoc-// @Summary List wiki category paths-// @Description Retrieve direct child directories for a wiki page type and optional parent_path+// ListFolders godoc+// @Summary List wiki folders+// @Description Retrieve the direct child folders of a parent folder (parent_id empty = root level), each with its page count and a has-children flag for the directory tree.// @Tags Wiki// @Produce json// @Param kb_id path string true "Knowledge base ID"-// @Param page_type query string true "Wiki page type; comma-separated for multiple (e.g. entity,concept)"-// @Param parent_path query string false "Parent category path, slash-separated"-// @Param page query int false "Page number (1-based)"-// @Param page_size query int false "Page size"-// @Success 200 {object} types.WikiCategoryPathListResponse+// @Param parent_id query string false "Parent folder id (empty = root)"+// @Success 200 {object} types.WikiFolderListResponse// @Failure 400 {object} errors.AppError// @Security Bearer-// @Router /knowledgebase/{kb_id}/wiki/categories [get]-func (h *WikiPageHandler) ListCategories(c *gin.Context) {+// @Router /knowledgebase/{kb_id}/wiki/folders [get]+func (h *WikiPageHandler) ListFolders(c *gin.Context) {kbID, _, err := h.validateWikiKB(c)if err != nil {c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})return}+ parentID := strings.TrimSpace(c.Query("parent_id"))+ var pageTypes []string+ if raw := strings.TrimSpace(c.Query("page_types")); raw != "" {+ for _, part := range strings.Split(raw, ",") {+ if p := strings.TrimSpace(part); p != "" {+ pageTypes = append(pageTypes, p)+ }+ }+ }+ folders, err := h.wikiService.ListChildFolders(c.Request.Context(), kbID, parentID, pageTypes)+ if err != nil {+ c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})+ return+ }+ if folders == nil {+ folders = []types.WikiFolderNode{}+ }+ c.JSON(http.StatusOK, types.WikiFolderListResponse{ParentID: parentID, Folders: folders})+}- pageTypes := types.SplitWikiPageTypes(c.Query("page_type"))- if len(pageTypes) == 0 {- c.JSON(http.StatusBadRequest, gin.H{"error": "page_type is required"})+// CreateFolder godoc+// @Summary Create a wiki folder+// @Description Create a new (initially empty) directory node under parent_id+// @Tags Wiki+// @Accept json+// @Produce json+// @Param kb_id path string true "Knowledge base ID"+// @Param folder body types.WikiFolderCreateRequest true "Folder data"+// @Success 201 {object} types.WikiFolder+// @Failure 400 {object} errors.AppError+// @Failure 409 {object} errors.AppError+// @Security Bearer+// @Router /knowledgebase/{kb_id}/wiki/folders [post]+func (h *WikiPageHandler) CreateFolder(c *gin.Context) {+ kbID, tenantID, err := h.validateWikiKB(c)+ if err != nil {+ c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})return}- page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))- if page < 1 {- page = 1+ var req types.WikiFolderCreateRequest+ if err := c.ShouldBindJSON(&req); err != nil {+ c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request body: " + err.Error()})+ return}- pageSize, _ := strconv.Atoi(c.DefaultQuery("page_size", "2000"))- if pageSize <= 0 {- pageSize = 2000+ folder, err := h.wikiService.CreateFolder(c.Request.Context(), kbID, tenantID, strings.TrimSpace(req.ParentID), req.Name)+ if err != nil {+ writeWikiFolderError(c, err)+ return}- parentPath := parseWikiCategoryPath(c.Query("parent_path"))- // Grouping and pagination are pushed to SQL; the service returns just the- // requested page of child folders plus the total distinct count.- paths, total, err := h.wikiService.ListDistinctCategoryPathsByType(c.Request.Context(), kbID, pageTypes, parentPath, page, pageSize)+ c.JSON(http.StatusCreated, folder)+}++// UpdateFolder godoc+// @Summary Rename or move a wiki folder+// @Description Rename and/or reparent a folder; the whole subtree's paths and the affected pages' cached paths are recomputed+// @Tags Wiki+// @Accept json+// @Produce json+// @Param kb_id path string true "Knowledge base ID"+// @Param folder_id path string true "Folder ID"+// @Param folder body types.WikiFolderUpdateRequest true "Folder update"+// @Success 200 {object} types.WikiFolder+// @Failure 400 {object} errors.AppError+// @Failure 404 {object} errors.AppError+// @Failure 409 {object} errors.AppError+// @Security Bearer+// @Router /knowledgebase/{kb_id}/wiki/folders/{folder_id} [put]+func (h *WikiPageHandler) UpdateFolder(c *gin.Context) {+ kbID, _, err := h.validateWikiKB(c)if err != nil {- c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})+ c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})return}- if paths == nil {- paths = []types.WikiCategoryPath{}+ folderID := secutils.SanitizeForLog(c.Param("folder_id"))+ if folderID == "" {+ c.JSON(http.StatusBadRequest, gin.H{"error": "Folder ID is required"})+ return}- totalPages := 0- if pageSize > 0 {- totalPages = (total + pageSize - 1) / pageSize+ var req types.WikiFolderUpdateRequest+ if err := c.ShouldBindJSON(&req); err != nil {+ c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request body: " + err.Error()})+ return}+ folder, err := h.wikiService.RenameOrMoveFolder(+ c.Request.Context(), kbID, folderID, req.Name, strings.TrimSpace(req.ParentID), req.MoveParent)+ if err != nil {+ writeWikiFolderError(c, err)+ return+ }+ c.JSON(http.StatusOK, folder)+}- c.JSON(http.StatusOK, types.WikiCategoryPathListResponse{- Paths: paths,- Total: total,- Page: page,- PageSize: pageSize,- TotalPages: totalPages,- })+// DeleteFolder godoc+// @Summary Delete an empty wiki folder+// @Description Delete a folder that has no pages and no child folders+// @Tags Wiki+// @Param kb_id path string true "Knowledge base ID"+// @Param folder_id path string true "Folder ID"+// @Success 204+// @Failure 400 {object} errors.AppError+// @Failure 404 {object} errors.AppError+// @Security Bearer+// @Router /knowledgebase/{kb_id}/wiki/folders/{folder_id} [delete]+func (h *WikiPageHandler) DeleteFolder(c *gin.Context) {+ kbID, _, err := h.validateWikiKB(c)+ if err != nil {+ c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})+ return+ }+ folderID := secutils.SanitizeForLog(c.Param("folder_id"))+ if folderID == "" {+ c.JSON(http.StatusBadRequest, gin.H{"error": "Folder ID is required"})+ return+ }+ if err := h.wikiService.DeleteFolder(c.Request.Context(), kbID, folderID); err != nil {+ writeWikiFolderError(c, err)+ return+ }+ c.Status(http.StatusNoContent)+}++// MovePage godoc+// @Summary Move a wiki page into a folder+// @Description Relocate a page (identified by slug in the body) into a folder (folder_id empty = root); the page's cached category path is recomputed+// @Tags Wiki+// @Accept json+// @Produce json+// @Param kb_id path string true "Knowledge base ID"+// @Param move body types.WikiPageMoveRequest true "Move target"+// @Success 200 {object} types.WikiPage+// @Failure 404 {object} errors.AppError+// @Security Bearer+// @Router /knowledgebase/{kb_id}/wiki/move-page [put]+func (h *WikiPageHandler) MovePage(c *gin.Context) {+ kbID, _, err := h.validateWikiKB(c)+ if err != nil {+ c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})+ return+ }+ var req types.WikiPageMoveRequest+ if err := c.ShouldBindJSON(&req); err != nil {+ c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request body: " + err.Error()})+ return+ }+ slug := strings.TrimSpace(req.Slug)+ if slug == "" {+ c.JSON(http.StatusBadRequest, gin.H{"error": "Page slug is required"})+ return+ }+ page, err := h.wikiService.MovePage(c.Request.Context(), kbID, slug, strings.TrimSpace(req.FolderID))+ if err != nil {+ writeWikiFolderError(c, err)+ return+ }+ c.JSON(http.StatusOK, page)+}++// writeWikiFolderError maps folder/page service errors to HTTP status codes.+func writeWikiFolderError(c *gin.Context, err error) {+ switch {+ case stderrors.Is(err, repository.ErrWikiFolderNotFound), stderrors.Is(err, repository.ErrWikiPageNotFound):+ c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})+ case stderrors.Is(err, repository.ErrWikiFolderConflict):+ c.JSON(http.StatusConflict, gin.H{"error": err.Error()})+ default:+ c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})+ }}func parseWikiCategoryPath(raw string) []string {diff --git a/internal/router/router.go b/internal/router/router.goindex ce197fe24..bb9790db2 100644--- a/internal/router/router.go+++ b/internal/router/router.go@@ -1748,12 +1748,18 @@ func RegisterWikiPageRoutes(r *gin.RouterGroup, wikiHandler *handler.WikiPageHan{// Page CRUDwiki.GET("/pages", g.Viewer(), wikiHandler.ListPages)- wiki.GET("/categories", g.Viewer(), wikiHandler.ListCategories)wiki.POST("/pages", g.OwnedWikiKBOrAdmin(), wikiHandler.CreatePage)+ wiki.PUT("/move-page", g.OwnedWikiKBOrAdmin(), wikiHandler.MovePage)wiki.GET("/pages/*slug", g.Viewer(), wikiHandler.GetPage)wiki.PUT("/pages/*slug", g.OwnedWikiKBOrAdmin(), wikiHandler.UpdatePage)wiki.DELETE("/pages/*slug", g.OwnedWikiKBOrAdmin(), wikiHandler.DeletePage)+ // Folder tree (directory nodes)+ wiki.GET("/folders", g.Viewer(), wikiHandler.ListFolders)+ wiki.POST("/folders", g.OwnedWikiKBOrAdmin(), wikiHandler.CreateFolder)+ wiki.PUT("/folders/:folder_id", g.OwnedWikiKBOrAdmin(), wikiHandler.UpdateFolder)+ wiki.DELETE("/folders/:folder_id", g.OwnedWikiKBOrAdmin(), wikiHandler.DeleteFolder)+// Special pageswiki.GET("/index", g.Viewer(), wikiHandler.GetIndex)wiki.GET("/log", g.Viewer(), wikiHandler.GetLog)diff --git a/internal/types/interfaces/wiki_page.go b/internal/types/interfaces/wiki_page.goindex d2600b27a..30c73dc65 100644--- a/internal/types/interfaces/wiki_page.go+++ b/internal/types/interfaces/wiki_page.go@@ -156,17 +156,43 @@ type WikiPageService interface {// merge targets server-side.FindSimilarPages(ctx context.Context, kbID string, query string, pageTypes []string, limit int) ([]*types.WikiPageLite, error)- // ListDistinctCategoryPaths returns unique non-empty category_path- // values from entity/concept pages. Used by wiki ingest to inject- // established folder taxonomy into extraction prompts.+ // ListDistinctCategoryPaths returns the existing wiki folder paths (split+ // into segments), capped at maxPaths. Used by wiki ingest's taxonomy+ // planner as the pool of folders to reuse.ListDistinctCategoryPaths(ctx context.Context, kbID string, maxPaths int) ([][]string, error)- // ListDistinctCategoryPathsByType returns the direct child folders of- // parentPath for the given page types (with per-child page counts),- // paginated. The second return value is the total number of distinct- // child folders before pagination. Used by the wiki browser to render the- // directory tree level by level.- ListDistinctCategoryPathsByType(ctx context.Context, kbID string, pageTypes []string, parentPath []string, page, pageSize int) ([]types.WikiCategoryPath, int, error)+ // --- Folder tree (wiki_folders) ---++ // ListChildFolders returns the direct child folders of parentID ("" =+ // root) relevant to the given page types, each enriched with a recursive+ // page count (the folder's whole subtree, restricted to pageTypes) and+ // whether it has relevant sub-folders. A folder is shown when its subtree+ // holds at least one page of pageTypes, or when it is entirely empty (a+ // user-created container with no pages of any type). When pageTypes is+ // empty every type counts.+ ListChildFolders(+ ctx context.Context, kbID string, parentID string, pageTypes []string,+ ) ([]types.WikiFolderNode, error)+ // GetFolder retrieves a single folder by id.+ GetFolder(ctx context.Context, kbID string, id string) (*types.WikiFolder, error)+ // CreateFolder creates a new (initially empty) folder under parentID and+ // returns it. Fails if a sibling with the same name already exists.+ CreateFolder(ctx context.Context, kbID string, tenantID uint64, parentID string, name string) (*types.WikiFolder, error)+ // RenameOrMoveFolder renames and/or reparents a folder, recomputing the+ // materialized path/depth of the whole subtree and the cached paths of+ // every page underneath. Returns the updated folder.+ RenameOrMoveFolder(ctx context.Context, kbID string, id string, newName string, newParentID string, moveParent bool) (*types.WikiFolder, error)+ // DeleteFolder removes an empty folder. Fails if it still contains pages+ // or child folders (the UI must move or delete contents first).+ DeleteFolder(ctx context.Context, kbID string, id string) error+ // FindOrCreateFolderPath resolves a category path (e.g. ["AI","RAG"]) to a+ // folder id, creating any missing intermediate folders. Returns the leaf+ // folder id and the canonical (cleaned) path. An empty/blank path resolves+ // to the root ("").+ FindOrCreateFolderPath(ctx context.Context, kbID string, tenantID uint64, path []string) (string, []string, error)+ // MovePage relocates a page into folderID ("" = root), recomputing its+ // cached category path. Returns the updated page.+ MovePage(ctx context.Context, kbID string, slug string, folderID string) (*types.WikiPage, error)// CountByType returns page counts grouped by type for a knowledge// base. Re-exposed at the service layer so the index intro@@ -276,14 +302,41 @@ type WikiPageRepository interface {// surface candidate merge targets server-side.FindSimilarPages(ctx context.Context, kbID string, query string, pageTypes []string, limit int) ([]*types.WikiPageLite, error)- // ListDistinctCategoryPaths scans entity/concept pages and returns- // unique non-empty category_path values for wiki ingest prompts.+ // ListDistinctCategoryPaths returns the materialized paths of existing+ // wiki folders (split into segments), capped at maxPaths. Used by the+ // wiki ingest taxonomy planner as the pool of folders to reuse.ListDistinctCategoryPaths(ctx context.Context, kbID string, maxPaths int) ([][]string, error)- // ListDistinctCategoryPathsByType returns the direct child folders of- // parentPath for the given page types (with per-child page counts),- // paginated, plus the total number of distinct child folders.- ListDistinctCategoryPathsByType(ctx context.Context, kbID string, pageTypes []string, parentPath []string, page, pageSize int) ([]types.WikiCategoryPath, int, error)+ // --- Folder tree (wiki_folders) ---++ // CreateFolder inserts a new directory node.+ CreateFolder(ctx context.Context, folder *types.WikiFolder) error+ // GetFolderByID retrieves a folder by id within a knowledge base.+ GetFolderByID(ctx context.Context, kbID string, id string) (*types.WikiFolder, error)+ // GetChildFolderByName returns the live child folder of parentID with the+ // given name, or ErrWikiFolderNotFound. Used by find-or-create.+ GetChildFolderByName(ctx context.Context, kbID string, parentID string, name string) (*types.WikiFolder, error)+ // ListChildFolders returns the direct child folders of parentID, ordered+ // by sort_order then name.+ ListChildFolders(ctx context.Context, kbID string, parentID string) ([]*types.WikiFolder, error)+ // ListAllFolders returns every folder in the knowledge base.+ ListAllFolders(ctx context.Context, kbID string) ([]*types.WikiFolder, error)+ // UpdateFolder rewrites a folder's mutable fields (name, parent_id, path,+ // depth, sort_order, updated_at).+ UpdateFolder(ctx context.Context, folder *types.WikiFolder) error+ // DeleteFolder soft-deletes a folder by id.+ DeleteFolder(ctx context.Context, kbID string, id string) error+ // CountPagesInFolder returns the number of live (non-archived) pages+ // directly in the folder (folder_id = id).+ CountPagesInFolder(ctx context.Context, kbID string, folderID string) (int64, error)+ // CountPagesByFolder returns live (non-archived) page counts grouped by+ // folder_id. When pageTypes is non-empty the count is restricted to those+ // page types; otherwise all types are counted. Pages at the wiki root carry+ // the empty-string key.+ CountPagesByFolder(ctx context.Context, kbID string, pageTypes []string) (map[string]int64, error)+ // ListPagesByFolderIDs returns all pages whose folder_id is in the set.+ // Used to recompute cached paths when a folder subtree is moved/renamed.+ ListPagesByFolderIDs(ctx context.Context, kbID string, folderIDs []string) ([]*types.WikiPage, error)// ListAll retrieves all wiki pages in a knowledge base (for link rebuilding, graph generation).ListAll(ctx context.Context, kbID string) ([]*types.WikiPage, error)diff --git a/internal/types/wiki_page.go b/internal/types/wiki_page.goindex 9439064f6..7f0ac6c13 100644--- a/internal/types/wiki_page.go+++ b/internal/types/wiki_page.go@@ -88,21 +88,6 @@ func SplitWikiPageTypes(raw string) []string {return out}-// WikiCategoryLevels flattens a (already cleaned) category path into the three-// fixed level columns used for SQL grouping. Missing levels yield "".-func WikiCategoryLevels(path []string) (l1, l2, l3 string) {- if len(path) > 0 {- l1 = path[0]- }- if len(path) > 1 {- l2 = path[1]- }- if len(path) > 2 {- l3 = path[2]- }- return l1, l2, l3-}-func isWikiTypeCategoryLabel(label string) bool {normalized := strings.ToLower(strings.TrimSpace(label))normalized = strings.TrimSuffix(normalized, "s")@@ -182,11 +167,17 @@ type WikiPage struct {Aliases StringArray `json:"aliases" gorm:"type:json"`// ParentSlug optionally points at the wiki page that should act as this// page's semantic parent in the directory tree. The parent may be empty- // when the page is grouped only by CategoryPath.+ // when the page is grouped only by FolderID.ParentSlug string `json:"parent_slug,omitempty" gorm:"type:varchar(255);index"`+ // FolderID is the single source of truth for where this page sits in the+ // directory tree — a reference to wiki_folders.id ("" = wiki root). The+ // CategoryPath / WikiPath / Depth fields below are denormalized caches+ // recomputed from this folder's chain on every write so list/index/search+ // queries don't have to join wiki_folders.+ FolderID string `json:"folder_id,omitempty" gorm:"column:folder_id;type:varchar(36);index;default:''"`// CategoryPath is the directory breadcrumb that groups this page in the- // wiki browser, e.g. ["AI", "LLM 应用", "RAG"]. It is intentionally- // label-based so intermediate directory nodes do not need to be real pages.+ // wiki browser, e.g. ["AI", "LLM 应用", "RAG"]. Derived cache of the+ // folder chain identified by FolderID.CategoryPath StringArray `json:"category_path,omitempty" gorm:"type:json"`// WikiPath is a normalized, sortable path derived from page_type,// category_path, and title. It keeps large directory listings cheap to sort.@@ -196,13 +187,6 @@ type WikiPage struct {// SortOrder allows generated or manually edited pages to control sibling// ordering before falling back to title.SortOrder int `json:"sort_order,omitempty" gorm:"default:0;index"`- // CategoryL1/L2/L3 mirror CategoryPath[0..2] as flat columns. They exist- // solely so the directory-listing query can GROUP BY / paginate folders in- // plain, cross-dialect SQL (no JSON parsing). They are NOT part of the API- // surface (hence json:"-") and are kept in sync by normalizeWikiHierarchy.- CategoryL1 string `json:"-" gorm:"column:category_l1;type:varchar(255);default:''"`- CategoryL2 string `json:"-" gorm:"column:category_l2;type:varchar(255);default:''"`- CategoryL3 string `json:"-" gorm:"column:category_l3;type:varchar(255);default:''"`// References to source knowledge IDs that contributed to this page.// Format matches the legacy "<knowledge_id>|<doc_title>" convention used// across the ingest pipeline, so retract / display code can split on `|`@@ -241,6 +225,73 @@ func (WikiPage) TableName() string {return "wiki_pages"}+// WikiFolderRootID is the sentinel parent/folder id meaning "the wiki root"+// (a page or folder directly under the top level, with no parent folder).+const WikiFolderRootID = ""++// WikiFolder is a first-class directory node in the wiki browser. Folders+// exist independently of pages — an empty folder persists so users can lay+// out a skeleton and file pages into it later. The tree is an adjacency list+// (ParentID, "" = root); Path is the materialized "/"-joined name chain kept+// purely for cheap display/sort. A wiki page's placement is WikiPage.FolderID.+type WikiFolder struct {+ ID string `json:"id" gorm:"type:varchar(36);primaryKey"`+ TenantID uint64 `json:"tenant_id" gorm:"index"`+ KnowledgeBaseID string `json:"knowledge_base_id" gorm:"type:varchar(36);index"`+ ParentID string `json:"parent_id" gorm:"column:parent_id;type:varchar(36);index;default:''"`+ Name string `json:"name" gorm:"type:varchar(255)"`+ Path string `json:"path" gorm:"type:varchar(1024)"`+ Depth int `json:"depth" gorm:"default:0"`+ SortOrder int `json:"sort_order" gorm:"default:0"`+ CreatedAt time.Time `json:"created_at"`+ UpdatedAt time.Time `json:"updated_at"`+ DeletedAt gorm.DeletedAt `json:"deleted_at" gorm:"index"`+}++// TableName specifies the database table name+func (WikiFolder) TableName() string {+ return "wiki_folders"+}++// WikiFolderNode is one directory node returned to the browser, enriched with+// the live page count directly under it and whether it has child folders so+// the UI can render an expand affordance without a second round-trip.+type WikiFolderNode struct {+ WikiFolder+ PageCount int64 `json:"page_count"`+ HasChildren bool `json:"has_children"`+}++// WikiFolderListResponse is the payload for listing the direct children of a+// folder (parent_id="" = root level).+type WikiFolderListResponse struct {+ ParentID string `json:"parent_id"`+ Folders []WikiFolderNode `json:"folders"`+}++// WikiFolderCreateRequest creates a new (initially empty) folder under ParentID.+type WikiFolderCreateRequest struct {+ ParentID string `json:"parent_id"`+ Name string `json:"name"`+}++// WikiFolderUpdateRequest renames and/or reparents a folder. ParentID is+// applied only when MoveParent is true so a pure rename doesn't have to+// re-send the (possibly root "") parent and risk an accidental move.+type WikiFolderUpdateRequest struct {+ Name string `json:"name,omitempty"`+ ParentID string `json:"parent_id,omitempty"`+ MoveParent bool `json:"move_parent,omitempty"`+}++// WikiPageMoveRequest relocates the page identified by Slug into FolderID+// ("" = root). Slug is carried in the body (not the path) because wiki slugs+// are hierarchical ("entity/acme") and would collide with gin's catch-all.+type WikiPageMoveRequest struct {+ Slug string `json:"slug" binding:"required"`+ FolderID string `json:"folder_id"`+}+// WikiExtractionGranularity controls how aggressive Pass 0 (candidate slug// extraction) is. Higher granularity = more slugs, lower = tighter focus on// the document's main subjects.@@ -368,6 +419,7 @@ type WikiPageListRequest struct {PageType string `json:"page_type,omitempty"` // filter by typeStatus string `json:"status,omitempty"` // filter by statusQuery string `json:"query,omitempty"` // full-text search+ FolderID *string `json:"folder_id,omitempty"` // exact folder placement ("" = root)CategoryPath StringArray `json:"category_path,omitempty"` // exact directory pathCategoryDepth *int `json:"category_depth,omitempty"` // exact directory depth, including 0 for rootPage int `json:"page,omitempty"` // pagination page (1-based)@@ -385,20 +437,6 @@ type WikiPageListResponse struct {TotalPages int `json:"total_pages"`}-// WikiCategoryPathListResponse returns the directory skeleton for wiki pages.-type WikiCategoryPathListResponse struct {- Paths []WikiCategoryPath `json:"paths"`- Total int `json:"total"`- Page int `json:"page"`- PageSize int `json:"page_size"`- TotalPages int `json:"total_pages"`-}--// WikiCategoryPath is one directory node in the wiki sidebar skeleton.-type WikiCategoryPath struct {- Path []string `json:"path"`- Count int64 `json:"count"`-}// WikiGraphMode enumerates the graph query modes exposed to the API.const (diff --git a/migrations/versioned/000037_wiki_and_indexing.down.sql b/migrations/versioned/000037_wiki_and_indexing.down.sqlindex dbe111f4c..cb3d90863 100644--- a/migrations/versioned/000037_wiki_and_indexing.down.sql+++ b/migrations/versioned/000037_wiki_and_indexing.down.sql@@ -6,6 +6,8 @@ ALTER TABLE knowledge_bases DROP COLUMN IF EXISTS indexing_strategy;DROP TABLE IF EXISTS wiki_page_issues;+DROP TABLE IF EXISTS wiki_folders;+DROP TABLE IF EXISTS wiki_pages;ALTER TABLE knowledge_bases DROP COLUMN IF EXISTS wiki_config;diff --git a/migrations/versioned/000037_wiki_and_indexing.up.sql b/migrations/versioned/000037_wiki_and_indexing.up.sqlindex aa28fb4c9..dfd0d5311 100644--- a/migrations/versioned/000037_wiki_and_indexing.up.sql+++ b/migrations/versioned/000037_wiki_and_indexing.up.sql@@ -23,13 +23,15 @@ 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 is the single source of truth for a page's placement in the+ -- directory tree (FK to wiki_folders.id; '' = wiki root). category_path /+ -- wiki_path / depth below are denormalized projections of the folder chain,+ -- kept in sync on write so list/index/search queries need no folder join.+ folder_id VARCHAR(36) NOT NULL DEFAULT '',category_path JSONB DEFAULT '[]'::JSONB,wiki_path VARCHAR(1024) NOT NULL DEFAULT '',depth INT NOT NULL DEFAULT 0,sort_order INT 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 JSONB DEFAULT '[]'::JSONB,chunk_refs JSONB DEFAULT '[]'::JSONB,in_links JSONB DEFAULT '[]'::JSONB,@@ -59,8 +61,8 @@ CREATE INDEX IF NOT EXISTS idx_wiki_pages_parent_slugCREATE INDEX IF NOT EXISTS idx_wiki_pages_treeON wiki_pages (knowledge_base_id, page_type, wiki_path, sort_order, title);-CREATE INDEX IF NOT EXISTS idx_wiki_pages_category_levels- ON wiki_pages (knowledge_base_id, page_type, category_l1, category_l2, category_l3);+CREATE INDEX IF NOT EXISTS idx_wiki_pages_folder+ ON wiki_pages (knowledge_base_id, folder_id);CREATE INDEX IF NOT EXISTS idx_wiki_pages_tenant_idON wiki_pages (tenant_id);@@ -71,6 +73,40 @@ CREATE INDEX IF NOT EXISTS idx_wiki_pages_deleted_atCREATE INDEX IF NOT EXISTS idx_wiki_pages_fulltextON wiki_pages USING GIN (to_tsvector('simple', coalesce(title, '') || ' ' || coalesce(content, '')));+-- ---------------------------------------------------------------------------+-- 1b) wiki_folders table+-- First-class directory nodes for the wiki browser. A folder exists+-- independently of any page, so empty folders persist (the user can build a+-- skeleton and file pages into it later). parent_id forms an adjacency-list+-- tree ('' = root); path is the materialized "/"-joined name chain kept for+-- cheap display/sort. wiki_pages.folder_id references id; renaming/moving a+-- folder updates this row's subtree and the affected pages' cached path.+-- ---------------------------------------------------------------------------+CREATE TABLE IF NOT EXISTS wiki_folders (+ id VARCHAR(36) PRIMARY KEY,+ tenant_id BIGINT 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 INT NOT NULL DEFAULT 0,+ sort_order INT NOT NULL DEFAULT 0,+ created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),+ updated_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),+ deleted_at TIMESTAMP WITH TIME ZONE+);++-- A folder name is unique among its live siblings under the same parent.+CREATE UNIQUE INDEX IF NOT EXISTS idx_wiki_folders_parent_name+ ON wiki_folders (knowledge_base_id, parent_id, name)+ WHERE deleted_at IS NULL;++CREATE INDEX IF NOT EXISTS idx_wiki_folders_parent+ ON wiki_folders (knowledge_base_id, parent_id);++CREATE INDEX IF NOT EXISTS idx_wiki_folders_deleted_at+ ON wiki_folders (deleted_at);+-- ----------------------------------------------------------------------------- 2) wiki_page_issues table-- Reports issues against generated wiki pages (LLM-flagged or user-flagged).diff --git a/migrations/versioned/000061_wiki_page_hierarchy.down.sql b/migrations/versioned/000061_wiki_page_hierarchy.down.sqlindex eab1b6051..1d0e240cd 100644--- a/migrations/versioned/000061_wiki_page_hierarchy.down.sql+++ b/migrations/versioned/000061_wiki_page_hierarchy.down.sql@@ -1,14 +1,16 @@-- Migration: 000061_wiki_page_hierarchy (rollback)-DROP INDEX IF EXISTS idx_wiki_pages_category_levels;DROP INDEX IF EXISTS idx_wiki_pages_tree;DROP INDEX IF EXISTS idx_wiki_pages_parent_slug;-ALTER TABLE wiki_pages DROP COLUMN IF EXISTS category_l3;-ALTER TABLE wiki_pages DROP COLUMN IF EXISTS category_l2;-ALTER TABLE wiki_pages DROP COLUMN IF EXISTS category_l1;ALTER TABLE wiki_pages DROP COLUMN IF EXISTS sort_order;ALTER TABLE wiki_pages DROP COLUMN IF EXISTS depth;ALTER TABLE wiki_pages DROP COLUMN IF EXISTS wiki_path;ALTER TABLE wiki_pages DROP COLUMN IF EXISTS category_path;ALTER TABLE wiki_pages DROP COLUMN IF EXISTS parent_slug;++ALTER TABLE wiki_pages DROP COLUMN IF EXISTS folder_id;++DROP TABLE IF EXISTS wiki_folders;++DROP INDEX IF EXISTS idx_wiki_pages_folder_id;\ No newline at end of filediff --git a/migrations/versioned/000061_wiki_page_hierarchy.up.sql b/migrations/versioned/000061_wiki_page_hierarchy.up.sqlindex d22d44ca5..e6cc7d659 100644--- a/migrations/versioned/000061_wiki_page_hierarchy.up.sql+++ b/migrations/versioned/000061_wiki_page_hierarchy.up.sql@@ -8,11 +8,44 @@ ALTER TABLE wiki_pages ADD COLUMN IF NOT EXISTS category_path JSONB DEFAULT '[]'ALTER TABLE wiki_pages ADD COLUMN IF NOT EXISTS wiki_path VARCHAR(1024) NOT NULL DEFAULT '';ALTER TABLE wiki_pages ADD COLUMN IF NOT EXISTS depth INT NOT NULL DEFAULT 0;ALTER TABLE wiki_pages ADD COLUMN IF NOT EXISTS sort_order INT NOT NULL DEFAULT 0;--- Flat mirrors of category_path[0..2] for cheap, cross-dialect directory--- grouping/pagination (kept in sync by the application on write).-ALTER TABLE wiki_pages ADD COLUMN IF NOT EXISTS category_l1 VARCHAR(255) NOT NULL DEFAULT '';-ALTER TABLE wiki_pages ADD COLUMN IF NOT EXISTS category_l2 VARCHAR(255) NOT NULL DEFAULT '';-ALTER TABLE wiki_pages ADD COLUMN IF NOT EXISTS category_l3 VARCHAR(255) NOT NULL DEFAULT '';++ALTER TABLE wiki_pages ADD COLUMN IF NOT EXISTS folder_id VARCHAR(36) NOT NULL DEFAULT '';++CREATE INDEX IF NOT EXISTS idx_wiki_pages_folder_id+ ON wiki_pages (folder_id);+-- ---------------------------------------------------------------------------+-- 1) wiki_folders table+-- First-class directory nodes for the wiki browser. A folder exists+-- independently of any page, so empty folders persist (the user can build a+-- skeleton and file pages into it later). parent_id forms an adjacency-list+-- tree ('' = root); path is the materialized "/"-joined name chain kept for+-- cheap display/sort. wiki_pages.folder_id references id; renaming/moving a+-- folder updates this row's subtree and the affected pages' cached path.+-- ---------------------------------------------------------------------------+CREATE TABLE IF NOT EXISTS wiki_folders (+ id VARCHAR(36) PRIMARY KEY,+ tenant_id BIGINT 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 INT NOT NULL DEFAULT 0,+ sort_order INT NOT NULL DEFAULT 0,+ created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),+ updated_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),+ deleted_at TIMESTAMP WITH TIME ZONE+);++-- A folder name is unique among its live siblings under the same parent.+CREATE UNIQUE INDEX IF NOT EXISTS idx_wiki_folders_parent_name+ ON wiki_folders (knowledge_base_id, parent_id, name)+ WHERE deleted_at IS NULL;++CREATE INDEX IF NOT EXISTS idx_wiki_folders_parent+ ON wiki_folders (knowledge_base_id, parent_id);++CREATE INDEX IF NOT EXISTS idx_wiki_folders_deleted_at+ ON wiki_folders (deleted_at);UPDATE wiki_pagesSET@@ -25,21 +58,10 @@ SETENDWHERE wiki_path = '' OR wiki_path IS NULL OR category_path IS NULL OR depth IS NULL;--- Backfill the flat level columns from the JSON category_path.-UPDATE wiki_pages-SET- category_l1 = COALESCE(category_path->>0, ''),- category_l2 = COALESCE(category_path->>1, ''),- category_l3 = COALESCE(category_path->>2, '')-WHERE category_path IS NOT NULL;-CREATE INDEX IF NOT EXISTS idx_wiki_pages_parent_slugON wiki_pages (knowledge_base_id, parent_slug);CREATE INDEX IF NOT EXISTS idx_wiki_pages_treeON wiki_pages (knowledge_base_id, page_type, wiki_path, sort_order, title);-CREATE INDEX IF NOT EXISTS idx_wiki_pages_category_levels- ON wiki_pages (knowledge_base_id, page_type, category_l1, category_l2, category_l3);-DO $$ BEGIN RAISE NOTICE '[Migration 000061] wiki page hierarchy schema applied successfully'; END $$;