dfaf862bc0
- Updated the AgentSelector component to improve agent selection logic, ensuring accurate representation of selected agents. - Introduced new AgentShareSettings component for managing agent sharing with organizations, including UI for selecting organizations and displaying shared agents. - Enhanced API interactions for sharing agents, including new methods for listing and managing agent shares. - Improved localization support for agent sharing features across multiple languages. - Updated various components to reflect changes in agent capabilities and sharing status, enhancing user experience and clarity. This update strengthens the agent management features, providing users with better tools for sharing and selecting agents within organizations.
157 lines
5.8 KiB
Go
157 lines
5.8 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
|
|
"github.com/Tencent/WeKnora/internal/types"
|
|
"github.com/Tencent/WeKnora/internal/types/interfaces"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
var (
|
|
ErrAgentShareNotFound = errors.New("agent share not found")
|
|
ErrAgentShareAlreadyExists = errors.New("agent already shared to this organization")
|
|
)
|
|
|
|
// agentShareRepository implements AgentShareRepository interface
|
|
type agentShareRepository struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
// NewAgentShareRepository creates a new agent share repository
|
|
func NewAgentShareRepository(db *gorm.DB) interfaces.AgentShareRepository {
|
|
return &agentShareRepository{db: db}
|
|
}
|
|
|
|
// Create creates a new agent share record
|
|
func (r *agentShareRepository) Create(ctx context.Context, share *types.AgentShare) error {
|
|
var count int64
|
|
r.db.WithContext(ctx).Model(&types.AgentShare{}).
|
|
Where("agent_id = ? AND source_tenant_id = ? AND organization_id = ? AND deleted_at IS NULL",
|
|
share.AgentID, share.SourceTenantID, share.OrganizationID).
|
|
Count(&count)
|
|
if count > 0 {
|
|
return ErrAgentShareAlreadyExists
|
|
}
|
|
return r.db.WithContext(ctx).Create(share).Error
|
|
}
|
|
|
|
// GetByID gets a share record by ID
|
|
func (r *agentShareRepository) GetByID(ctx context.Context, id string) (*types.AgentShare, error) {
|
|
var share types.AgentShare
|
|
err := r.db.WithContext(ctx).Where("id = ?", id).First(&share).Error
|
|
if err != nil {
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return nil, ErrAgentShareNotFound
|
|
}
|
|
return nil, err
|
|
}
|
|
return &share, nil
|
|
}
|
|
|
|
// GetByAgentAndOrg gets a share record by agent ID and organization ID
|
|
func (r *agentShareRepository) GetByAgentAndOrg(ctx context.Context, agentID string, orgID string) (*types.AgentShare, error) {
|
|
var share types.AgentShare
|
|
err := r.db.WithContext(ctx).
|
|
Where("agent_id = ? AND organization_id = ?", agentID, orgID).
|
|
First(&share).Error
|
|
if err != nil {
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return nil, ErrAgentShareNotFound
|
|
}
|
|
return nil, err
|
|
}
|
|
return &share, nil
|
|
}
|
|
|
|
// Update updates a share record
|
|
func (r *agentShareRepository) Update(ctx context.Context, share *types.AgentShare) error {
|
|
return r.db.WithContext(ctx).Model(&types.AgentShare{}).
|
|
Where("id = ?", share.ID).Updates(share).Error
|
|
}
|
|
|
|
// Delete soft deletes a share record
|
|
func (r *agentShareRepository) Delete(ctx context.Context, id string) error {
|
|
return r.db.WithContext(ctx).Where("id = ?", id).Delete(&types.AgentShare{}).Error
|
|
}
|
|
|
|
// DeleteByAgentIDAndSourceTenant soft deletes all share records for an agent (id, tenant_id)
|
|
func (r *agentShareRepository) DeleteByAgentIDAndSourceTenant(ctx context.Context, agentID string, sourceTenantID uint64) error {
|
|
return r.db.WithContext(ctx).
|
|
Where("agent_id = ? AND source_tenant_id = ?", agentID, sourceTenantID).
|
|
Delete(&types.AgentShare{}).Error
|
|
}
|
|
|
|
// DeleteByOrganizationID soft deletes all share records for an organization
|
|
func (r *agentShareRepository) DeleteByOrganizationID(ctx context.Context, orgID string) error {
|
|
return r.db.WithContext(ctx).Where("organization_id = ?", orgID).Delete(&types.AgentShare{}).Error
|
|
}
|
|
|
|
// ListByAgent lists all share records for an agent
|
|
func (r *agentShareRepository) ListByAgent(ctx context.Context, agentID string) ([]*types.AgentShare, error) {
|
|
var shares []*types.AgentShare
|
|
err := r.db.WithContext(ctx).
|
|
Preload("Organization").
|
|
Where("agent_id = ?", agentID).
|
|
Order("created_at DESC").
|
|
Find(&shares).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return shares, nil
|
|
}
|
|
|
|
// ListByOrganization lists all share records for an organization (excluding deleted agents)
|
|
func (r *agentShareRepository) ListByOrganization(ctx context.Context, orgID string) ([]*types.AgentShare, error) {
|
|
var shares []*types.AgentShare
|
|
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").
|
|
Where("agent_shares.organization_id = ? AND agent_shares.deleted_at IS NULL", orgID).
|
|
Order("agent_shares.created_at DESC").
|
|
Find(&shares).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return shares, 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
|
|
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").
|
|
Joins("JOIN organization_members ON organization_members.organization_id = agent_shares.organization_id").
|
|
Joins("JOIN organizations ON organizations.id = agent_shares.organization_id AND organizations.deleted_at IS NULL").
|
|
Where("organization_members.user_id = ?", userID).
|
|
Where("agent_shares.deleted_at IS NULL").
|
|
Order("agent_shares.created_at DESC").
|
|
Find(&shares).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return shares, nil
|
|
}
|
|
|
|
// GetShareByAgentIDForUser returns one share for the given agentID that the user can access (user in org), excluding source_tenant_id == excludeTenantID. Single query.
|
|
func (r *agentShareRepository) GetShareByAgentIDForUser(ctx context.Context, userID, agentID string, excludeTenantID uint64) (*types.AgentShare, error) {
|
|
var share types.AgentShare
|
|
err := r.db.WithContext(ctx).
|
|
Joins("JOIN organization_members ON organization_members.organization_id = agent_shares.organization_id").
|
|
Where("agent_shares.agent_id = ?", agentID).
|
|
Where("organization_members.user_id = ?", userID).
|
|
Where("agent_shares.source_tenant_id != ?", excludeTenantID).
|
|
Where("agent_shares.deleted_at IS NULL").
|
|
First(&share).Error
|
|
if err != nil {
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return nil, ErrAgentShareNotFound
|
|
}
|
|
return nil, err
|
|
}
|
|
return &share, nil
|
|
}
|