feat(api): integrate per-organization resource counts into organization listing

- Added `resource_counts` to the `ListOrganizationsResponse` to include per-organization knowledge base and agent counts, enhancing the sidebar display.
- Removed the deprecated `getMeResourceCounts` API endpoint, consolidating resource count retrieval into the organization listing.
- Updated the organization store to manage and utilize the new resource counts effectively.
- Adjusted UI components to leverage the new resource counts structure, improving data retrieval and display for agents and knowledge bases.

This update streamlines resource count management and enhances the user experience by providing relevant data in a single API call.
This commit is contained in:
wizardchen
2026-02-08 14:59:21 +08:00
committed by lyingbug
parent 9a073d62bf
commit cbe66aba56
13 changed files with 333 additions and 125 deletions
+7 -19
View File
@@ -159,9 +159,16 @@ export interface ApiResponse<T> {
message?: string
}
/** Per-org resource counts (included in list my organizations to avoid extra GET /me/resource-counts) */
export interface ResourceCountsByOrg {
knowledge_bases: { by_organization: Record<string, number> }
agents: { by_organization: Record<string, number> }
}
export interface ListOrganizationsResponse {
organizations: Organization[]
total: number
resource_counts?: ResourceCountsByOrg
}
export interface ListMembersResponse {
@@ -642,25 +649,6 @@ export async function listSharedAgents(): Promise<ApiResponse<SharedAgentInfo[]>
}
}
/** Response of GET /me/resource-counts: per-org counts for sidebar (one request instead of N). */
export interface MeResourceCountsResponse {
knowledge_bases: { by_organization: Record<string, number> }
agents: { by_organization: Record<string, number> }
}
/**
* Get per-organization resource counts for list sidebar (one request).
* 全部 total is not included; frontend keeps 全部 = 我的 + 他人共享给我.
*/
export async function getMeResourceCounts(): Promise<ApiResponse<MeResourceCountsResponse>> {
try {
const response = await get('/api/v1/me/resource-counts')
return response as unknown as ApiResponse<MeResourceCountsResponse>
} catch (error: any) {
return { success: false, message: error.message || 'Failed to get resource counts' }
}
}
/**
* List all agents in an organization (including those shared by current tenant), for list page when a space is selected.
*/
+29 -13
View File
@@ -5,7 +5,8 @@ import type {
OrganizationMember,
SharedKnowledgeBase,
SharedAgentInfo,
OrganizationPreview
OrganizationPreview,
ResourceCountsByOrg
} from '@/api/organization'
import {
listMyOrganizations,
@@ -33,6 +34,10 @@ export const useOrganizationStore = defineStore('organization', () => {
const previewData = ref<OrganizationPreview | null>(null)
const loading = ref(false)
const error = ref<string | null>(null)
/** 各空间内知识库/智能体数量(由 GET /organizations 的 resource_counts 填充,供列表侧栏使用) */
const resourceCounts = ref<ResourceCountsByOrg | null>(null)
/** 用于去重:同一时刻只允许一次 GET /organizations 请求 */
let fetchOrganizationsPromise: Promise<void> | null = null
// Computed
const myOrganizations = computed(() => organizations.value)
@@ -53,23 +58,32 @@ export const useOrganizationStore = defineStore('organization', () => {
// Actions
/**
* Fetch all organizations the user belongs to
* Fetch all organizations the user belongs to.
* 去重:并发调用只发一次请求,共用同一 Promise。
*/
async function fetchOrganizations() {
if (fetchOrganizationsPromise) return fetchOrganizationsPromise
loading.value = true
error.value = null
try {
const response = await listMyOrganizations()
if (response.success && response.data) {
organizations.value = response.data.organizations
} else {
error.value = response.message || 'Failed to fetch organizations'
fetchOrganizationsPromise = (async () => {
try {
const response = await listMyOrganizations()
if (response.success && response.data) {
organizations.value = response.data.organizations
resourceCounts.value = response.data.resource_counts ?? null
} else {
resourceCounts.value = null
error.value = response.message || 'Failed to fetch organizations'
}
} catch (e: any) {
error.value = e.message || 'Failed to fetch organizations'
resourceCounts.value = null
} finally {
loading.value = false
fetchOrganizationsPromise = null
}
} catch (e: any) {
error.value = e.message || 'Failed to fetch organizations'
} finally {
loading.value = false
}
})()
return fetchOrganizationsPromise
}
/**
@@ -413,6 +427,7 @@ export const useOrganizationStore = defineStore('organization', () => {
currentMembers.value = []
sharedKnowledgeBases.value = []
sharedAgents.value = []
resourceCounts.value = null
previewData.value = null
error.value = null
}
@@ -424,6 +439,7 @@ export const useOrganizationStore = defineStore('organization', () => {
currentMembers,
sharedKnowledgeBases,
sharedAgents,
resourceCounts,
previewData,
loading,
error,
+4 -7
View File
@@ -540,7 +540,7 @@ import { formatStringDate } from '@/utils/index'
import { useI18n } from 'vue-i18n'
import { createSessions } from '@/api/chat/index'
import { useOrganizationStore } from '@/stores/organization'
import { setSharedAgentDisabledByMe, listOrganizationSharedAgents, getMeResourceCounts } from '@/api/organization'
import { setSharedAgentDisabledByMe, listOrganizationSharedAgents } from '@/api/organization'
import { useSettingsStore } from '@/stores/settings'
import { useMenuStore } from '@/stores/menu'
import type { SharedAgentInfo, OrganizationSharedAgentItem } from '@/api/organization'
@@ -674,12 +674,9 @@ const fetchList = () => {
orgStore.fetchSharedAgents(),
orgStore.fetchOrganizations()
]).finally(() => { loading.value = false }).then(() => {
// 一次请求拉取各空间内全部智能体数量(含我共享的),用于侧栏展示
getMeResourceCounts().then((res) => {
if (res.success && res.data?.agents?.by_organization) {
spaceAgentCountByOrg.value = { ...res.data.agents.by_organization }
}
})
// 各空间智能体数量已由 GET /organizations 的 resource_counts 带回,存于 orgStore.resourceCounts
const counts = orgStore.resourceCounts?.agents?.by_organization
if (counts) spaceAgentCountByOrg.value = { ...counts }
})
}
@@ -540,7 +540,7 @@ import { listKnowledgeBases, deleteKnowledgeBase } from '@/api/knowledge-base'
import { formatStringDate } from '@/utils/index'
import { useUIStore } from '@/stores/ui'
import { useOrganizationStore } from '@/stores/organization'
import { listOrganizationSharedKnowledgeBases, getMeResourceCounts, type SharedKnowledgeBase, type OrganizationSharedKnowledgeBaseItem, type SourceFromAgentInfo } from '@/api/organization'
import { listOrganizationSharedKnowledgeBases, type SharedKnowledgeBase, type OrganizationSharedKnowledgeBaseItem, type SourceFromAgentInfo } from '@/api/organization'
import KnowledgeBaseEditorModal from './KnowledgeBaseEditorModal.vue'
import ShareKnowledgeBaseDialog from '@/components/ShareKnowledgeBaseDialog.vue'
import ListSpaceSidebar from '@/components/ListSpaceSidebar.vue'
@@ -703,12 +703,9 @@ const fetchList = () => {
orgStore.fetchSharedKnowledgeBases(),
orgStore.fetchOrganizations()
]).finally(() => { loading.value = false }).then(() => {
// 一次请求拉取各空间内全部知识库数量(含我共享的),用于侧栏展示
getMeResourceCounts().then((res) => {
if (res.success && res.data?.knowledge_bases?.by_organization) {
spaceCountByOrg.value = { ...res.data.knowledge_bases.by_organization }
}
})
// 各空间知识库数量已由 GET /organizations 的 resource_counts 带回,存于 orgStore.resourceCounts
const counts = orgStore.resourceCounts?.knowledge_bases?.by_organization
if (counts) spaceCountByOrg.value = { ...counts }
})
}