feat(agent): enhance agent and skill management features
- Updated the API to include disabled agent IDs for better management of user-specific agent visibility. - Introduced a new function to set the disabled state for shared agents, allowing users to manage their shared agent preferences. - Added a new Skills API to retrieve available skills, enhancing the overall agent capabilities. - Improved the AgentSelector and related components to filter out disabled agents, ensuring a cleaner user experience. - Updated localization strings to reflect new features and improve clarity in agent and skill management. This update enhances the user experience by providing better control over agent visibility and introducing new skill management functionalities.
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/types"
|
||||
"github.com/Tencent/WeKnora/internal/types/interfaces"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type tenantDisabledSharedAgentRepository struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
// NewTenantDisabledSharedAgentRepository creates a new repository
|
||||
func NewTenantDisabledSharedAgentRepository(db *gorm.DB) interfaces.TenantDisabledSharedAgentRepository {
|
||||
return &tenantDisabledSharedAgentRepository{db: db}
|
||||
}
|
||||
|
||||
func (r *tenantDisabledSharedAgentRepository) ListByTenantID(ctx context.Context, tenantID uint64) ([]*types.TenantDisabledSharedAgent, error) {
|
||||
var list []*types.TenantDisabledSharedAgent
|
||||
err := r.db.WithContext(ctx).Where("tenant_id = ?", tenantID).Find(&list).Error
|
||||
return list, err
|
||||
}
|
||||
|
||||
func (r *tenantDisabledSharedAgentRepository) ListDisabledOwnAgentIDs(ctx context.Context, tenantID uint64) ([]string, error) {
|
||||
var ids []string
|
||||
err := r.db.WithContext(ctx).Model(&types.TenantDisabledSharedAgent{}).
|
||||
Where("tenant_id = ? AND source_tenant_id = ?", tenantID, tenantID).
|
||||
Pluck("agent_id", &ids).Error
|
||||
return ids, err
|
||||
}
|
||||
|
||||
func (r *tenantDisabledSharedAgentRepository) Add(ctx context.Context, tenantID uint64, agentID string, sourceTenantID uint64) error {
|
||||
rec := &types.TenantDisabledSharedAgent{
|
||||
TenantID: tenantID,
|
||||
AgentID: agentID,
|
||||
SourceTenantID: sourceTenantID,
|
||||
}
|
||||
return r.db.WithContext(ctx).Where(rec).FirstOrCreate(rec).Error
|
||||
}
|
||||
|
||||
func (r *tenantDisabledSharedAgentRepository) Remove(ctx context.Context, tenantID uint64, agentID string, sourceTenantID uint64) error {
|
||||
return r.db.WithContext(ctx).
|
||||
Where("tenant_id = ? AND agent_id = ? AND source_tenant_id = ?", tenantID, agentID, sourceTenantID).
|
||||
Delete(&types.TenantDisabledSharedAgent{}).Error
|
||||
}
|
||||
@@ -24,24 +24,27 @@ var (
|
||||
|
||||
// agentShareService implements AgentShareService interface
|
||||
type agentShareService struct {
|
||||
shareRepo interfaces.AgentShareRepository
|
||||
orgRepo interfaces.OrganizationRepository
|
||||
agentRepo interfaces.CustomAgentRepository
|
||||
userRepo interfaces.UserRepository
|
||||
shareRepo interfaces.AgentShareRepository
|
||||
disabledRepo interfaces.TenantDisabledSharedAgentRepository
|
||||
orgRepo interfaces.OrganizationRepository
|
||||
agentRepo interfaces.CustomAgentRepository
|
||||
userRepo interfaces.UserRepository
|
||||
}
|
||||
|
||||
// NewAgentShareService creates a new agent share service
|
||||
func NewAgentShareService(
|
||||
shareRepo interfaces.AgentShareRepository,
|
||||
disabledRepo interfaces.TenantDisabledSharedAgentRepository,
|
||||
orgRepo interfaces.OrganizationRepository,
|
||||
agentRepo interfaces.CustomAgentRepository,
|
||||
userRepo interfaces.UserRepository,
|
||||
) interfaces.AgentShareService {
|
||||
return &agentShareService{
|
||||
shareRepo: shareRepo,
|
||||
orgRepo: orgRepo,
|
||||
agentRepo: agentRepo,
|
||||
userRepo: userRepo,
|
||||
shareRepo: shareRepo,
|
||||
disabledRepo: disabledRepo,
|
||||
orgRepo: orgRepo,
|
||||
agentRepo: agentRepo,
|
||||
userRepo: userRepo,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -202,9 +205,33 @@ func (s *agentShareService) ListSharedAgents(ctx context.Context, userID string,
|
||||
for _, info := range agentInfoMap {
|
||||
result = append(result, info)
|
||||
}
|
||||
|
||||
// Set DisabledByMe from tenant_disabled_shared_agents for current tenant
|
||||
disabledList, err := s.disabledRepo.ListByTenantID(ctx, currentTenantID)
|
||||
if err != nil {
|
||||
return result, nil // non-fatal: return list without DisabledByMe
|
||||
}
|
||||
disabledSet := make(map[string]bool)
|
||||
for _, d := range disabledList {
|
||||
disabledSet[fmt.Sprintf("%s_%d", d.AgentID, d.SourceTenantID)] = true
|
||||
}
|
||||
for _, info := range result {
|
||||
if info.Agent != nil {
|
||||
key := fmt.Sprintf("%s_%d", info.Agent.ID, info.SourceTenantID)
|
||||
info.DisabledByMe = disabledSet[key]
|
||||
}
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// SetSharedAgentDisabledByMe adds or removes (tenantID, agentID, sourceTenantID) from tenant_disabled_shared_agents.
|
||||
func (s *agentShareService) SetSharedAgentDisabledByMe(ctx context.Context, tenantID uint64, agentID string, sourceTenantID uint64, disabled bool) error {
|
||||
if disabled {
|
||||
return s.disabledRepo.Add(ctx, tenantID, agentID, sourceTenantID)
|
||||
}
|
||||
return s.disabledRepo.Remove(ctx, tenantID, agentID, sourceTenantID)
|
||||
}
|
||||
|
||||
// GetSharedAgentForUser returns the shared agent by agentID if the user has access; source tenant is resolved from the user's share. One share lookup + one agent lookup.
|
||||
func (s *agentShareService) GetSharedAgentForUser(ctx context.Context, userID string, currentTenantID uint64, agentID string) (*types.CustomAgent, error) {
|
||||
if agentID == "" {
|
||||
@@ -250,3 +277,8 @@ func (s *agentShareService) GetShareByAgentAndOrg(ctx context.Context, agentID s
|
||||
}
|
||||
return share, nil
|
||||
}
|
||||
|
||||
// GetShareByAgentIDForUser returns one share for the given agentID that the user can access (user in org), excluding source_tenant_id == excludeTenantID.
|
||||
func (s *agentShareService) GetShareByAgentIDForUser(ctx context.Context, userID, agentID string, excludeTenantID uint64) (*types.AgentShare, error) {
|
||||
return s.shareRepo.GetShareByAgentIDForUser(ctx, userID, agentID, excludeTenantID)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user