feat(api): enhance organization shared knowledge base and agent functionalities

- Introduced new interfaces for handling shared knowledge bases and agents, including visibility and selection modes for agents.
- Added API functions to list shared knowledge bases and agents within an organization, improving data retrieval for UI components.
- Updated existing components to utilize the new API endpoints and interfaces, ensuring accurate representation of shared resources.
- Enhanced error handling in API calls to provide clearer feedback on failures.

This update improves the functionality and user experience by providing better access to shared resources and enhancing the overall data structure within the application.
This commit is contained in:
wizardchen
2026-02-08 14:32:18 +08:00
committed by lyingbug
parent 1bdd9a4693
commit 9a073d62bf
25 changed files with 1119 additions and 118 deletions
@@ -108,6 +108,7 @@ func (r *agentShareRepository) ListByOrganization(ctx context.Context, orgID str
err := r.db.WithContext(ctx).
Joins("JOIN custom_agents ON custom_agents.id = agent_shares.agent_id AND custom_agents.tenant_id = agent_shares.source_tenant_id AND custom_agents.deleted_at IS NULL").
Preload("Agent").
Preload("Organization").
Where("agent_shares.organization_id = ? AND agent_shares.deleted_at IS NULL", orgID).
Order("agent_shares.created_at DESC").
Find(&shares).Error
@@ -117,6 +118,35 @@ func (r *agentShareRepository) ListByOrganization(ctx context.Context, orgID str
return shares, nil
}
// CountByOrganizations returns share counts per organization (only orgs in orgIDs). Excludes deleted agents.
func (r *agentShareRepository) CountByOrganizations(ctx context.Context, orgIDs []string) (map[string]int64, error) {
if len(orgIDs) == 0 {
return make(map[string]int64), nil
}
type row struct {
OrgID string `gorm:"column:organization_id"`
Count int64 `gorm:"column:count"`
}
var rows []row
err := r.db.WithContext(ctx).Model(&types.AgentShare{}).
Joins("JOIN custom_agents ON custom_agents.id = agent_shares.agent_id AND custom_agents.tenant_id = agent_shares.source_tenant_id AND custom_agents.deleted_at IS NULL").
Select("agent_shares.organization_id as organization_id, COUNT(*) as count").
Where("agent_shares.organization_id IN ? AND agent_shares.deleted_at IS NULL", orgIDs).
Group("agent_shares.organization_id").
Find(&rows).Error
if err != nil {
return nil, err
}
out := make(map[string]int64)
for _, o := range orgIDs {
out[o] = 0
}
for _, r := range rows {
out[r.OrgID] = r.Count
}
return out, nil
}
// ListSharedAgentsForUser lists all agents shared to organizations that the user belongs to
func (r *agentShareRepository) ListSharedAgentsForUser(ctx context.Context, userID string) ([]*types.AgentShare, error) {
var shares []*types.AgentShare
@@ -115,6 +115,7 @@ func (r *kbShareRepository) ListByOrganization(ctx context.Context, orgID string
err := r.db.WithContext(ctx).
Joins("JOIN knowledge_bases ON knowledge_bases.id = kb_shares.knowledge_base_id AND knowledge_bases.deleted_at IS NULL").
Preload("KnowledgeBase").
Preload("Organization").
Where("kb_shares.organization_id = ? AND kb_shares.deleted_at IS NULL", orgID).
Order("kb_shares.created_at DESC").
Find(&shares).Error
@@ -188,3 +189,32 @@ func (r *kbShareRepository) CountSharesByKnowledgeBaseIDs(ctx context.Context, k
}
return countMap, nil
}
// CountByOrganizations returns share counts per organization (only orgs in orgIDs). Excludes deleted KBs.
func (r *kbShareRepository) CountByOrganizations(ctx context.Context, orgIDs []string) (map[string]int64, error) {
if len(orgIDs) == 0 {
return make(map[string]int64), nil
}
type row struct {
OrgID string `gorm:"column:organization_id"`
Count int64 `gorm:"column:count"`
}
var rows []row
err := r.db.WithContext(ctx).Model(&types.KnowledgeBaseShare{}).
Joins("JOIN knowledge_bases ON knowledge_bases.id = kb_shares.knowledge_base_id AND knowledge_bases.deleted_at IS NULL").
Select("kb_shares.organization_id as organization_id, COUNT(*) as count").
Where("kb_shares.organization_id IN ? AND kb_shares.deleted_at IS NULL", orgIDs).
Group("kb_shares.organization_id").
Find(&rows).Error
if err != nil {
return nil, err
}
out := make(map[string]int64)
for _, o := range orgIDs {
out[o] = 0
}
for _, r := range rows {
out[r.OrgID] = r.Count
}
return out, nil
}