= {};
tagList.value.forEach((tag) => {
@@ -2346,6 +2416,39 @@ const handleKBEditorSuccess = (kbIdValue: string) => {
{{ $t(batchMode ? 'common.cancel' : 'menu.batchManage') }}
+
+
+
+
+
+
{{ group.label }}
+
{{ group.description }}
+
+
+
+
+
+
+
+
+
@@ -727,11 +730,18 @@ import ResourceOriginBadge from '@/components/ResourceOriginBadge.vue'
import { shouldShowResourceOriginBadge } from '@/utils/card-list-badge'
import { permissionCanManageKB } from '@/utils/kbPermission'
import ContextualGuide from '@/components/ContextualGuide.vue'
+import ResourceSortControl from '@/components/ResourceSortControl.vue'
import { isContextualGuideDone, markContextualGuideDone } from '@/config/contextualGuides'
import { useTenantModelReadiness } from '@/composables/useTenantModelReadiness'
import { useI18n } from 'vue-i18n'
import { useListUrlState } from '@/composables/useListUrlState'
import { useResourcePins } from '@/composables/useResourcePins'
+import {
+ DEFAULT_RESOURCE_SORT,
+ sortResourcesWithinGroups,
+ type ResourceSortAccessors,
+ type ResourceSortValue,
+} from '@/utils/resourceSorting'
const router = useRouter()
const route = useRoute()
@@ -741,6 +751,7 @@ const { loaded: modelsReadyLoaded, isReadyForDocumentKb } = useTenantModelReadin
const orgStore = useOrganizationStore()
const chatResources = useChatResourcesStore()
const { t } = useI18n()
+const selectedResourceSort = ref(DEFAULT_RESOURCE_SORT)
// 左侧空间选择:默认根据当前角色决定。
// Viewer 在该空间里通常 0 KB owned,"我的"会显示空状态、又把共享 KB 藏起来,
@@ -797,6 +808,18 @@ interface KB {
creator_name?: string;
}
+const flatKnowledgeBaseSortAccessors: ResourceSortAccessors = {
+ getName: item => item?.name,
+ getUpdatedAt: item => item?.updated_at ?? item?.shared_at,
+ getCreatedAt: item => item?.created_at ?? item?.shared_at,
+}
+
+const sharedKnowledgeBaseSortAccessors: ResourceSortAccessors = {
+ getName: item => item.knowledge_base?.name,
+ getUpdatedAt: item => item.knowledge_base?.updated_at ?? item.shared_at,
+ getCreatedAt: item => item.knowledge_base?.created_at ?? item.shared_at,
+}
+
const kbs = ref([])
const loading = ref(false)
const confirmDelete = useConfirmDelete()
@@ -828,50 +851,29 @@ const spaceSelectionOrgId = computed(() => {
const spaceKbsList = ref([])
const spaceKbsLoading = ref(false)
-// 「工作空间」视图下的稳定排序:本空间内「我创建」在前、「同事创建」在后;
-// 子段内保留服务端的置顶优先顺序。给 contributor 视图把「本空间 · 仅查看」
-// 分组标题正好插在过渡处;其他角色看不到标题,纯排序变化也无害。
-// Ordering for the 「本空间」 tab:
-// 1. pinned KBs (mine or teammate), newest pin first
-// 2. my non-pinned KBs
-// 3. teammate non-pinned KBs (rendered under the「本空间 · 仅查看」header)
-//
-// Pin is per-user as of migration 000050, so a teammate-created KB that
-// the caller has personally pinned must float into the pinned section
-// even though it would otherwise live in the teammate sub-group. The
-// previous version only bucketed by isMyKb and silently demoted these
-// pinned-but-teammate KBs.
+// 「工作空间」固定保持“置顶 → 我创建 → 同事创建”,用户选择只影响组内顺序。
const unsearchedSortedMineKbs = computed(() => {
- return [...kbs.value].sort((a, b) => {
- const ap = a.is_pinned ? 0 : 1
- const bp = b.is_pinned ? 0 : 1
- if (ap !== bp) return ap - bp
- if (a.is_pinned && b.is_pinned) {
- const at = a.pinned_at ? Date.parse(a.pinned_at as string) : 0
- const bt = b.pinned_at ? Date.parse(b.pinned_at as string) : 0
- if (at !== bt) return bt - at
- }
- const am = isMyKb(a) ? 0 : 1
- const bm = isMyKb(b) ? 0 : 1
- if (am !== bm) return am - bm
- const ac = a.created_at ? Date.parse(a.created_at as string) : 0
- const bc = b.created_at ? Date.parse(b.created_at as string) : 0
- return bc - ac
- })
+ return sortResourcesWithinGroups(
+ kbs.value,
+ selectedResourceSort.value,
+ kb => kb.is_pinned ? 'pinned' : isMyKb(kb) ? 'mine' : 'tenantOthers',
+ ['pinned', 'mine', 'tenantOthers'],
+ flatKnowledgeBaseSortAccessors,
+ )
})
const sortedMineKbs = computed(() => unsearchedSortedMineKbs.value.filter(item => matchesResourceQuery(item, keyword.value)))
-// 空间视角下的稳定排序:我创建的(is_mine)放在前面,剩下的共享部分再按
-// 可编辑 / 仅查看 排序——这样空间列表跟「全部」视图的视觉顺序一致。
+// 空间视角固定保持“我共享的 → 可编辑 → 仅查看”,用户选择只影响组内顺序。
const unsearchedSortedSpaceKbsList = computed(() => {
- return [...spaceKbsList.value].sort((a, b) => {
- const aMine = a.is_mine ? 0 : 1
- const bMine = b.is_mine ? 0 : 1
- if (aMine !== bMine) return aMine - bMine
- const aE = isSharedKbEditable(a.permission) ? 0 : 1
- const bE = isSharedKbEditable(b.permission) ? 0 : 1
- return aE - bE
- })
+ return sortResourcesWithinGroups(
+ spaceKbsList.value,
+ selectedResourceSort.value,
+ item => item.is_mine
+ ? 'sharedByMe'
+ : isSharedKbEditable(item.permission) ? 'sharedEditable' : 'sharedReadonly',
+ ['sharedByMe', 'sharedEditable', 'sharedReadonly'],
+ sharedKnowledgeBaseSortAccessors,
+ )
})
const sortedSpaceKbsList = computed(() => unsearchedSortedSpaceKbsList.value.filter(item => matchesResourceQuery(item.knowledge_base, keyword.value)))
const spaceCountByOrg = ref>({})
@@ -1054,21 +1056,28 @@ const spaceKbSectionCounts = computed>(() => {
return c
})
-// Filtered knowledge bases: 全部 = 我的 + 全部共享;我的 = 仅我的
-//
-// Favorites / Recents reuse the same render path as `all` — they're just
-// pre-filtered, pre-ordered slices, so the existing kb-card / shared
-// kb-card templates render them with zero extra markup. Order is
-// preserved via the upstream array (pins order is ts-desc).
+// 收藏、最近、全部和工作空间都使用同一排序选项,同时保持原有分组顺序。
const unsearchedFilteredKnowledgeBases = computed(() => {
if (spaceSelection.value === 'favorites') {
- return favoritesList.value
+ return sortResourcesWithinGroups(
+ favoritesList.value,
+ selectedResourceSort.value,
+ kbSectionOf,
+ ['pinned', 'mine', 'tenantOthers', 'sharedEditable', 'sharedReadonly'],
+ flatKnowledgeBaseSortAccessors,
+ )
}
if (spaceSelection.value === 'recents') {
- return recentsList.value
+ return sortResourcesWithinGroups(
+ recentsList.value,
+ selectedResourceSort.value,
+ kbSectionOf,
+ ['pinned', 'mine', 'tenantOthers', 'sharedEditable', 'sharedReadonly'],
+ flatKnowledgeBaseSortAccessors,
+ )
}
if (spaceSelection.value === 'mine') {
- return kbs.value.map(kb => ({ ...kb, isMine: true as const }))
+ return sortedMineKbs.value.map(kb => ({ ...kb, isMine: true as const }))
}
if (spaceSelection.value !== 'all') {
return []
@@ -1080,11 +1089,18 @@ const unsearchedFilteredKnowledgeBases = computed(() => {
// ≥2 entries (#795). mergeAllScopeKnowledgeBases de-duplicates by KB id
// (owned wins; most-privileged share kept) while preserving the existing
// pinned → mine → teammate → shared(editable-first) ordering.
- return mergeAllScopeKnowledgeBases(
+ const merged = mergeAllScopeKnowledgeBases(
kbs.value as unknown as OwnedKnowledgeBase[],
sharedKbs.value as unknown as SharedKnowledgeBaseLike[],
authStore.user?.id,
) as unknown as Array<(KB & { isMine: true }) | (SharedKnowledgeBase['knowledge_base'] & { isMine: false; permission: string; shared_at: string; share_id: string } & any)>
+ return sortResourcesWithinGroups(
+ merged,
+ selectedResourceSort.value,
+ kbSectionOf,
+ ['pinned', 'mine', 'tenantOthers', 'sharedEditable', 'sharedReadonly'],
+ flatKnowledgeBaseSortAccessors,
+ )
})
const filteredKnowledgeBases = computed(() => unsearchedFilteredKnowledgeBases.value.filter(item => matchesResourceQuery(item, keyword.value)))
@@ -1602,6 +1618,13 @@ watch(keyword, () => { collapsedKbSections.value = new Set() })
line-height: 20px;
}
+.header-actions {
+ display: flex;
+ align-items: center;
+ gap: 8px;
+ flex-shrink: 0;
+}
+
.header-action-btn {
padding: 0;
min-width: 28px;
diff --git a/frontend/src/views/knowledge/documentSorting.test.ts b/frontend/src/views/knowledge/documentSorting.test.ts
new file mode 100644
index 00000000..f38ea08b
--- /dev/null
+++ b/frontend/src/views/knowledge/documentSorting.test.ts
@@ -0,0 +1,36 @@
+import assert from 'node:assert/strict';
+import test from 'node:test';
+
+import {
+ DEFAULT_DOCUMENT_SORT,
+ DOCUMENT_SORT_OPTIONS,
+ getDocumentSortOption,
+ getDocumentSortParams,
+} from './documentSorting';
+
+test('文档排序默认使用创建时间倒序', () => {
+ assert.equal(DEFAULT_DOCUMENT_SORT, 'created_desc');
+ assert.deepEqual(getDocumentSortParams(DEFAULT_DOCUMENT_SORT), {
+ sort_by: 'created_at',
+ sort_order: 'desc',
+ });
+});
+
+test('文档排序提供三组共六个选项并映射到服务端白名单参数', () => {
+ assert.equal(DOCUMENT_SORT_OPTIONS.length, 6);
+ assert.deepEqual(
+ DOCUMENT_SORT_OPTIONS.map(({ value, sortBy, sortOrder }) => [value, sortBy, sortOrder]),
+ [
+ ['updated_desc', 'updated_at', 'desc'],
+ ['updated_asc', 'updated_at', 'asc'],
+ ['created_desc', 'created_at', 'desc'],
+ ['created_asc', 'created_at', 'asc'],
+ ['name_asc', 'file_name', 'asc'],
+ ['name_desc', 'file_name', 'desc'],
+ ],
+ );
+});
+
+test('未知排序值会安全回退到默认选项', () => {
+ assert.equal(getDocumentSortOption('invalid' as never).value, DEFAULT_DOCUMENT_SORT);
+});
diff --git a/frontend/src/views/knowledge/documentSorting.ts b/frontend/src/views/knowledge/documentSorting.ts
new file mode 100644
index 00000000..b169e823
--- /dev/null
+++ b/frontend/src/views/knowledge/documentSorting.ts
@@ -0,0 +1,90 @@
+import type {
+ KnowledgeListSortField,
+ KnowledgeListSortOrder,
+} from '@/api/knowledge-base';
+
+export type DocumentSortValue =
+ | 'updated_desc'
+ | 'updated_asc'
+ | 'created_desc'
+ | 'created_asc'
+ | 'name_asc'
+ | 'name_desc';
+
+export interface DocumentSortOption {
+ value: DocumentSortValue;
+ sortBy: KnowledgeListSortField;
+ sortOrder: KnowledgeListSortOrder;
+ groupKey: 'updatedTime' | 'createdTime' | 'fileName';
+ labelKey:
+ | 'recentlyUpdated'
+ | 'earliestUpdated'
+ | 'newestCreated'
+ | 'earliestCreated'
+ | 'nameAscending'
+ | 'nameDescending';
+}
+
+export const DEFAULT_DOCUMENT_SORT: DocumentSortValue = 'created_desc';
+
+export const DOCUMENT_SORT_OPTIONS: readonly DocumentSortOption[] = [
+ {
+ value: 'updated_desc',
+ sortBy: 'updated_at',
+ sortOrder: 'desc',
+ groupKey: 'updatedTime',
+ labelKey: 'recentlyUpdated',
+ },
+ {
+ value: 'updated_asc',
+ sortBy: 'updated_at',
+ sortOrder: 'asc',
+ groupKey: 'updatedTime',
+ labelKey: 'earliestUpdated',
+ },
+ {
+ value: 'created_desc',
+ sortBy: 'created_at',
+ sortOrder: 'desc',
+ groupKey: 'createdTime',
+ labelKey: 'newestCreated',
+ },
+ {
+ value: 'created_asc',
+ sortBy: 'created_at',
+ sortOrder: 'asc',
+ groupKey: 'createdTime',
+ labelKey: 'earliestCreated',
+ },
+ {
+ value: 'name_asc',
+ sortBy: 'file_name',
+ sortOrder: 'asc',
+ groupKey: 'fileName',
+ labelKey: 'nameAscending',
+ },
+ {
+ value: 'name_desc',
+ sortBy: 'file_name',
+ sortOrder: 'desc',
+ groupKey: 'fileName',
+ labelKey: 'nameDescending',
+ },
+];
+
+export function getDocumentSortOption(value: DocumentSortValue): DocumentSortOption {
+ return DOCUMENT_SORT_OPTIONS.find((option) => option.value === value)
+ ?? DOCUMENT_SORT_OPTIONS.find((option) => option.value === DEFAULT_DOCUMENT_SORT)
+ ?? DOCUMENT_SORT_OPTIONS[0];
+}
+
+export function getDocumentSortParams(value: DocumentSortValue): {
+ sort_by: KnowledgeListSortField;
+ sort_order: KnowledgeListSortOrder;
+} {
+ const option = getDocumentSortOption(value);
+ return {
+ sort_by: option.sortBy,
+ sort_order: option.sortOrder,
+ };
+}
diff --git a/frontend/src/views/organization/OrganizationList.vue b/frontend/src/views/organization/OrganizationList.vue
index 21a8e984..ab1ec6fa 100644
--- a/frontend/src/views/organization/OrganizationList.vue
+++ b/frontend/src/views/organization/OrganizationList.vue
@@ -9,6 +9,7 @@
{{ $t('organization.title') }}