feat(agent): enhance agent sharing and selection features
- 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.
This commit is contained in:
@@ -20,6 +20,8 @@ type Handler struct {
|
||||
config *config.Config // Application configuration
|
||||
knowledgebaseService interfaces.KnowledgeBaseService // Service for managing knowledge bases
|
||||
customAgentService interfaces.CustomAgentService // Service for managing custom agents
|
||||
tenantService interfaces.TenantService // Service for loading tenant (shared agent context)
|
||||
agentShareService interfaces.AgentShareService // Service for resolving shared agents (KB scope in retrieval)
|
||||
}
|
||||
|
||||
// NewHandler creates a new instance of Handler with all necessary dependencies
|
||||
@@ -30,6 +32,8 @@ func NewHandler(
|
||||
config *config.Config,
|
||||
knowledgebaseService interfaces.KnowledgeBaseService,
|
||||
customAgentService interfaces.CustomAgentService,
|
||||
tenantService interfaces.TenantService,
|
||||
agentShareService interfaces.AgentShareService,
|
||||
) *Handler {
|
||||
return &Handler{
|
||||
sessionService: sessionService,
|
||||
@@ -38,6 +42,8 @@ func NewHandler(
|
||||
config: config,
|
||||
knowledgebaseService: knowledgebaseService,
|
||||
customAgentService: customAgentService,
|
||||
tenantService: tenantService,
|
||||
agentShareService: agentShareService,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -18,19 +18,20 @@ import (
|
||||
|
||||
// qaRequestContext holds all the common data needed for QA requests
|
||||
type qaRequestContext struct {
|
||||
ctx context.Context
|
||||
c *gin.Context
|
||||
sessionID string
|
||||
requestID string
|
||||
query string
|
||||
session *types.Session
|
||||
customAgent *types.CustomAgent
|
||||
assistantMessage *types.Message
|
||||
knowledgeBaseIDs []string
|
||||
knowledgeIDs []string
|
||||
summaryModelID string
|
||||
webSearchEnabled bool
|
||||
mentionedItems types.MentionedItems
|
||||
ctx context.Context
|
||||
c *gin.Context
|
||||
sessionID string
|
||||
requestID string
|
||||
query string
|
||||
session *types.Session
|
||||
customAgent *types.CustomAgent
|
||||
assistantMessage *types.Message
|
||||
knowledgeBaseIDs []string
|
||||
knowledgeIDs []string
|
||||
summaryModelID string
|
||||
webSearchEnabled bool
|
||||
mentionedItems types.MentionedItems
|
||||
effectiveTenantID uint64 // when using shared agent, tenant ID for model/KB/MCP resolution; 0 = use context tenant
|
||||
}
|
||||
|
||||
// parseQARequest parses and validates a QA request, returns the request context
|
||||
@@ -71,18 +72,36 @@ func (h *Handler) parseQARequest(c *gin.Context, logPrefix string) (*qaRequestCo
|
||||
return nil, nil, errors.NewNotFoundError("Session not found")
|
||||
}
|
||||
|
||||
// Get custom agent if agent_id is provided
|
||||
// Get custom agent if agent_id is provided. Backend resolves shared agent from share relation (no client-provided tenant).
|
||||
var customAgent *types.CustomAgent
|
||||
var effectiveTenantID uint64
|
||||
if request.AgentID != "" {
|
||||
logger.Infof(ctx, "Fetching custom agent, agent ID: %s", secutils.SanitizeForLog(request.AgentID))
|
||||
agent, err := h.customAgentService.GetAgentByID(ctx, request.AgentID)
|
||||
if err != nil {
|
||||
logger.Warnf(ctx, "Failed to get custom agent, agent ID: %s, error: %v, using default config",
|
||||
secutils.SanitizeForLog(request.AgentID), err)
|
||||
logger.Infof(ctx, "Resolving agent, agent ID: %s", secutils.SanitizeForLog(request.AgentID))
|
||||
userIDVal, _ := c.Get(types.UserIDContextKey.String())
|
||||
currentTenantID := c.GetUint64(types.TenantIDContextKey.String())
|
||||
if h.agentShareService != nil && userIDVal != nil && currentTenantID != 0 {
|
||||
userID, _ := userIDVal.(string)
|
||||
agent, err := h.agentShareService.GetSharedAgentForUser(ctx, userID, currentTenantID, request.AgentID)
|
||||
if err == nil && agent != nil {
|
||||
effectiveTenantID = agent.TenantID
|
||||
customAgent = agent
|
||||
logger.Infof(ctx, "Using shared agent: ID=%s, Name=%s, effectiveTenantID=%d (retrieval scope)",
|
||||
customAgent.ID, customAgent.Name, effectiveTenantID)
|
||||
}
|
||||
}
|
||||
if customAgent == nil {
|
||||
agent, err := h.customAgentService.GetAgentByID(ctx, request.AgentID)
|
||||
if err == nil {
|
||||
customAgent = agent
|
||||
logger.Infof(ctx, "Using own agent: ID=%s, Name=%s, AgentMode=%s",
|
||||
customAgent.ID, customAgent.Name, customAgent.Config.AgentMode)
|
||||
} else {
|
||||
logger.Warnf(ctx, "Failed to get custom agent, agent ID: %s, error: %v, using default config",
|
||||
secutils.SanitizeForLog(request.AgentID), err)
|
||||
}
|
||||
} else {
|
||||
customAgent = agent
|
||||
logger.Infof(ctx, "Using custom agent: ID=%s, Name=%s, IsBuiltin=%v, AgentMode=%s",
|
||||
customAgent.ID, customAgent.Name, customAgent.IsBuiltin, customAgent.Config.AgentMode)
|
||||
logger.Infof(ctx, "Using custom agent: ID=%s, Name=%s, IsBuiltin=%v, AgentMode=%s, effectiveTenantID=%d",
|
||||
customAgent.ID, customAgent.Name, customAgent.IsBuiltin, customAgent.Config.AgentMode, effectiveTenantID)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -143,11 +162,12 @@ func (h *Handler) parseQARequest(c *gin.Context, logPrefix string) (*qaRequestCo
|
||||
RequestID: c.GetString(types.RequestIDContextKey.String()),
|
||||
IsCompleted: false,
|
||||
},
|
||||
knowledgeBaseIDs: secutils.SanitizeForLogArray(kbIDs),
|
||||
knowledgeIDs: secutils.SanitizeForLogArray(knowledgeIDs),
|
||||
summaryModelID: secutils.SanitizeForLog(request.SummaryModelID),
|
||||
webSearchEnabled: request.WebSearchEnabled,
|
||||
mentionedItems: convertMentionedItems(request.MentionedItems),
|
||||
knowledgeBaseIDs: secutils.SanitizeForLogArray(kbIDs),
|
||||
knowledgeIDs: secutils.SanitizeForLogArray(knowledgeIDs),
|
||||
summaryModelID: secutils.SanitizeForLog(request.SummaryModelID),
|
||||
webSearchEnabled: request.WebSearchEnabled,
|
||||
mentionedItems: convertMentionedItems(request.MentionedItems),
|
||||
effectiveTenantID: effectiveTenantID,
|
||||
}
|
||||
|
||||
return reqCtx, &request, nil
|
||||
@@ -169,9 +189,18 @@ func (h *Handler) setupSSEStream(reqCtx *qaRequestContext, generateTitle bool) *
|
||||
// Write initial agent_query event
|
||||
h.writeAgentQueryEvent(reqCtx.ctx, reqCtx.sessionID, reqCtx.assistantMessage.ID)
|
||||
|
||||
// Base context for async work: when using shared agent, use source tenant for model/KB/MCP resolution
|
||||
baseCtx := reqCtx.ctx
|
||||
if reqCtx.effectiveTenantID != 0 && h.tenantService != nil {
|
||||
if tenant, err := h.tenantService.GetTenantByID(reqCtx.ctx, reqCtx.effectiveTenantID); err == nil && tenant != nil {
|
||||
baseCtx = context.WithValue(context.WithValue(reqCtx.ctx, types.TenantIDContextKey, reqCtx.effectiveTenantID), types.TenantInfoContextKey, tenant)
|
||||
logger.Infof(reqCtx.ctx, "Using effective tenant %d for shared agent (model/KB/MCP)", reqCtx.effectiveTenantID)
|
||||
}
|
||||
}
|
||||
|
||||
// Create EventBus and cancellable context
|
||||
eventBus := event.NewEventBus()
|
||||
asyncCtx, cancel := context.WithCancel(logger.CloneContext(reqCtx.ctx))
|
||||
asyncCtx, cancel := context.WithCancel(logger.CloneContext(baseCtx))
|
||||
|
||||
streamCtx := &sseStreamContext{
|
||||
eventBus: eventBus,
|
||||
|
||||
@@ -33,7 +33,7 @@ type CreateKnowledgeQARequest struct {
|
||||
KnowledgeBaseIDs []string `json:"knowledge_base_ids"` // Selected knowledge base ID for this request
|
||||
KnowledgeIds []string `json:"knowledge_ids"` // Selected knowledge ID for this request
|
||||
AgentEnabled bool `json:"agent_enabled"` // Whether agent mode is enabled for this request
|
||||
AgentID string `json:"agent_id"` // Selected custom agent ID for this request
|
||||
AgentID string `json:"agent_id"` // Selected custom agent ID (backend resolves shared agent and its tenant from share relation)
|
||||
WebSearchEnabled bool `json:"web_search_enabled"` // Whether web search is enabled for this request
|
||||
SummaryModelID string `json:"summary_model_id"` // Optional summary model ID for this request (overrides session default)
|
||||
MentionedItems []MentionedItemRequest `json:"mentioned_items"` // @mentioned knowledge bases and files
|
||||
|
||||
Reference in New Issue
Block a user