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)
|
||||
}
|
||||
|
||||
@@ -114,6 +114,7 @@ func BuildContainer(container *dig.Container) *dig.Container {
|
||||
must(container.Provide(repository.NewOrganizationRepository))
|
||||
must(container.Provide(repository.NewKBShareRepository))
|
||||
must(container.Provide(repository.NewAgentShareRepository))
|
||||
must(container.Provide(repository.NewTenantDisabledSharedAgentRepository))
|
||||
must(container.Provide(service.NewWebSearchStateService))
|
||||
|
||||
// MCP manager for managing MCP client connections
|
||||
|
||||
@@ -14,13 +14,15 @@ import (
|
||||
|
||||
// CustomAgentHandler defines the HTTP handler for custom agent operations
|
||||
type CustomAgentHandler struct {
|
||||
service interfaces.CustomAgentService
|
||||
service interfaces.CustomAgentService
|
||||
disabledRepo interfaces.TenantDisabledSharedAgentRepository
|
||||
}
|
||||
|
||||
// NewCustomAgentHandler creates a new custom agent handler instance
|
||||
func NewCustomAgentHandler(service interfaces.CustomAgentService) *CustomAgentHandler {
|
||||
func NewCustomAgentHandler(service interfaces.CustomAgentService, disabledRepo interfaces.TenantDisabledSharedAgentRepository) *CustomAgentHandler {
|
||||
return &CustomAgentHandler{
|
||||
service: service,
|
||||
service: service,
|
||||
disabledRepo: disabledRepo,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,10 +36,10 @@ type CreateAgentRequest struct {
|
||||
|
||||
// UpdateAgentRequest defines the request body for updating an agent
|
||||
type UpdateAgentRequest struct {
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
Avatar string `json:"avatar"`
|
||||
Config types.CustomAgentConfig `json:"config"`
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
Avatar string `json:"avatar"`
|
||||
Config types.CustomAgentConfig `json:"config"`
|
||||
}
|
||||
|
||||
// CreateAgent godoc
|
||||
@@ -161,9 +163,14 @@ func (h *CustomAgentHandler) ListAgents(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// Per-tenant "disabled by me" for own agents (only affects this tenant's conversation dropdown)
|
||||
tenantID, _ := c.Get(types.TenantIDContextKey.String())
|
||||
disabledOwnIDs, _ := h.disabledRepo.ListDisabledOwnAgentIDs(ctx, tenantID.(uint64))
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"success": true,
|
||||
"data": agents,
|
||||
"success": true,
|
||||
"data": agents,
|
||||
"disabled_own_agent_ids": disabledOwnIDs,
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -18,12 +18,13 @@ import (
|
||||
|
||||
// OrganizationHandler implements HTTP request handlers for organization management
|
||||
type OrganizationHandler struct {
|
||||
orgService interfaces.OrganizationService
|
||||
shareService interfaces.KBShareService
|
||||
agentShareService interfaces.AgentShareService
|
||||
userService interfaces.UserService
|
||||
knowledgeRepo interfaces.KnowledgeRepository
|
||||
chunkRepo interfaces.ChunkRepository
|
||||
orgService interfaces.OrganizationService
|
||||
shareService interfaces.KBShareService
|
||||
agentShareService interfaces.AgentShareService
|
||||
customAgentService interfaces.CustomAgentService
|
||||
userService interfaces.UserService
|
||||
knowledgeRepo interfaces.KnowledgeRepository
|
||||
chunkRepo interfaces.ChunkRepository
|
||||
}
|
||||
|
||||
// NewOrganizationHandler creates a new organization handler
|
||||
@@ -31,17 +32,19 @@ func NewOrganizationHandler(
|
||||
orgService interfaces.OrganizationService,
|
||||
shareService interfaces.KBShareService,
|
||||
agentShareService interfaces.AgentShareService,
|
||||
customAgentService interfaces.CustomAgentService,
|
||||
userService interfaces.UserService,
|
||||
knowledgeRepo interfaces.KnowledgeRepository,
|
||||
chunkRepo interfaces.ChunkRepository,
|
||||
) *OrganizationHandler {
|
||||
return &OrganizationHandler{
|
||||
orgService: orgService,
|
||||
shareService: shareService,
|
||||
agentShareService: agentShareService,
|
||||
userService: userService,
|
||||
knowledgeRepo: knowledgeRepo,
|
||||
chunkRepo: chunkRepo,
|
||||
orgService: orgService,
|
||||
shareService: shareService,
|
||||
agentShareService: agentShareService,
|
||||
customAgentService: customAgentService,
|
||||
userService: userService,
|
||||
knowledgeRepo: knowledgeRepo,
|
||||
chunkRepo: chunkRepo,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1252,6 +1255,46 @@ func (h *OrganizationHandler) ListSharedAgents(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, gin.H{"success": true, "data": list, "total": len(list)})
|
||||
}
|
||||
|
||||
// SetSharedAgentDisabledByMeRequest is the body for POST /shared-agents/disabled
|
||||
type SetSharedAgentDisabledByMeRequest struct {
|
||||
AgentID string `json:"agent_id" binding:"required"`
|
||||
Disabled bool `json:"disabled"`
|
||||
}
|
||||
|
||||
// SetSharedAgentDisabledByMe sets whether the current tenant has disabled this shared agent for their conversation dropdown
|
||||
func (h *OrganizationHandler) SetSharedAgentDisabledByMe(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
userID, _ := c.Get(types.UserIDContextKey.String())
|
||||
tenantID, _ := c.Get(types.TenantIDContextKey.String())
|
||||
uid := userID.(string)
|
||||
tid := tenantID.(uint64)
|
||||
|
||||
var req SetSharedAgentDisabledByMeRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.Error(apperrors.NewBadRequestError("Invalid request").WithDetails(err.Error()))
|
||||
return
|
||||
}
|
||||
// Derive sourceTenantID: own agent (current tenant) or from shared list
|
||||
var sourceTenantID uint64
|
||||
agent, err := h.customAgentService.GetAgentByID(ctx, req.AgentID)
|
||||
if err == nil && agent != nil && agent.TenantID == tid {
|
||||
sourceTenantID = tid
|
||||
} else {
|
||||
share, err := h.agentShareService.GetShareByAgentIDForUser(ctx, uid, req.AgentID, tid)
|
||||
if err != nil || share == nil {
|
||||
c.Error(apperrors.NewForbiddenError("No access to this agent"))
|
||||
return
|
||||
}
|
||||
sourceTenantID = share.SourceTenantID
|
||||
}
|
||||
if err := h.agentShareService.SetSharedAgentDisabledByMe(ctx, tid, req.AgentID, sourceTenantID, req.Disabled); err != nil {
|
||||
logger.Errorf(ctx, "SetSharedAgentDisabledByMe failed: %v", err)
|
||||
c.Error(apperrors.NewInternalServerError("Failed to update preference"))
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"success": true})
|
||||
}
|
||||
|
||||
// toOrgResponse converts an organization to response format
|
||||
func (h *OrganizationHandler) toOrgResponse(ctx context.Context, org *types.Organization, currentUserID string) types.OrganizationResponse {
|
||||
resp := types.OrganizationResponse{
|
||||
|
||||
@@ -529,4 +529,5 @@ func RegisterOrganizationRoutes(r *gin.RouterGroup, orgHandler *handler.Organiza
|
||||
r.GET("/shared-knowledge-bases", orgHandler.ListSharedKnowledgeBases)
|
||||
// Shared agents route
|
||||
r.GET("/shared-agents", orgHandler.ListSharedAgents)
|
||||
r.POST("/shared-agents/disabled", orgHandler.SetSharedAgentDisabledByMe)
|
||||
}
|
||||
|
||||
@@ -135,10 +135,14 @@ type AgentShareService interface {
|
||||
ListSharesByAgent(ctx context.Context, agentID string) ([]*types.AgentShare, error)
|
||||
ListSharesByOrganization(ctx context.Context, orgID string) ([]*types.AgentShare, error)
|
||||
ListSharedAgents(ctx context.Context, userID string, currentTenantID uint64) ([]*types.SharedAgentInfo, error)
|
||||
// SetSharedAgentDisabledByMe sets whether the current tenant has "disabled" this shared agent for their conversation dropdown (per-user preference).
|
||||
SetSharedAgentDisabledByMe(ctx context.Context, tenantID uint64, agentID string, sourceTenantID uint64, disabled bool) error
|
||||
// GetSharedAgentForUser returns the shared agent by agentID if the user has access (source tenant is resolved from share); used to resolve KB scope for @ mention.
|
||||
GetSharedAgentForUser(ctx context.Context, userID string, currentTenantID uint64, agentID string) (*types.CustomAgent, error)
|
||||
GetShare(ctx context.Context, shareID string) (*types.AgentShare, error)
|
||||
GetShareByAgentAndOrg(ctx context.Context, agentID string, orgID string) (*types.AgentShare, error)
|
||||
// GetShareByAgentIDForUser returns one share for the given agentID that the user can access, excluding source_tenant_id == excludeTenantID (e.g. current tenant to get shared-from-other only).
|
||||
GetShareByAgentIDForUser(ctx context.Context, userID, agentID string, excludeTenantID uint64) (*types.AgentShare, error)
|
||||
}
|
||||
|
||||
// AgentShareRepository defines the agent sharing repository interface
|
||||
@@ -156,3 +160,12 @@ type AgentShareRepository interface {
|
||||
// GetShareByAgentIDForUser returns one share for the given agentID that the user can access (user in org), excluding source_tenant_id == excludeTenantID.
|
||||
GetShareByAgentIDForUser(ctx context.Context, userID, agentID string, excludeTenantID uint64) (*types.AgentShare, error)
|
||||
}
|
||||
|
||||
// TenantDisabledSharedAgentRepository stores per-tenant "disabled" agents (hidden from conversation dropdown; own and shared)
|
||||
type TenantDisabledSharedAgentRepository interface {
|
||||
ListByTenantID(ctx context.Context, tenantID uint64) ([]*types.TenantDisabledSharedAgent, error)
|
||||
// ListDisabledOwnAgentIDs returns agent IDs that this tenant has disabled for their own agents (source_tenant_id = tenant_id)
|
||||
ListDisabledOwnAgentIDs(ctx context.Context, tenantID uint64) ([]string, error)
|
||||
Add(ctx context.Context, tenantID uint64, agentID string, sourceTenantID uint64) error
|
||||
Remove(ctx context.Context, tenantID uint64, agentID string, sourceTenantID uint64) error
|
||||
}
|
||||
|
||||
@@ -241,6 +241,21 @@ type SharedAgentInfo struct {
|
||||
SharedAt time.Time `json:"shared_at"`
|
||||
SharedByUserID string `json:"shared_by_user_id,omitempty"`
|
||||
SharedByUsername string `json:"shared_by_username,omitempty"`
|
||||
// DisabledByMe: current tenant has hidden this shared agent from their conversation dropdown (per-user preference)
|
||||
DisabledByMe bool `json:"disabled_by_me"`
|
||||
}
|
||||
|
||||
// TenantDisabledSharedAgent records that a tenant has "disabled" a shared agent for their own dropdown
|
||||
type TenantDisabledSharedAgent struct {
|
||||
TenantID uint64 `json:"tenant_id" gorm:"primaryKey"`
|
||||
AgentID string `json:"agent_id" gorm:"type:varchar(36);primaryKey"`
|
||||
SourceTenantID uint64 `json:"source_tenant_id" gorm:"primaryKey"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
}
|
||||
|
||||
// TableName returns the table name for GORM
|
||||
func (TenantDisabledSharedAgent) TableName() string {
|
||||
return "tenant_disabled_shared_agents"
|
||||
}
|
||||
|
||||
// ----------------------
|
||||
|
||||
Reference in New Issue
Block a user