feat(chat): add @Skill/@MCP mentions with scoped agent runtime

Wire per-turn @mentions for skills, MCP services, tags, and files through
QA handlers into agent config, runtime_context/must_use user blocks, and
tool registration. Fix tag-only scopes dropping KB tools, refresh suggested
questions on mention changes, and strengthen MCP must_use so @selected
services are invoked before local KB search.
This commit is contained in:
wizardchen
2026-06-29 16:01:58 +08:00
committed by lyingbug
parent 40f217c228
commit 049f85139d
44 changed files with 1814 additions and 378 deletions
+57 -4
View File
@@ -62,15 +62,68 @@ func convertMentionedItems(items []MentionedItemRequest) types.MentionedItems {
result := make(types.MentionedItems, len(items))
for i, item := range items {
result[i] = types.MentionedItem{
ID: item.ID,
Name: item.Name,
Type: item.Type,
KBType: item.KBType,
ID: item.ID,
Name: item.Name,
Type: item.Type,
KBType: item.KBType,
KBID: item.KBID,
KBName: item.KBName,
ServiceID: item.ServiceID,
SkillName: item.SkillName,
}
}
return result
}
func tagScopesFromMentionedItems(items []MentionedItemRequest) []types.TagScope {
byKB := make(map[string][]string)
seen := make(map[string]map[string]bool)
for _, item := range items {
if item.Type != "tag" || item.ID == "" || item.KBID == "" {
continue
}
if seen[item.KBID] == nil {
seen[item.KBID] = make(map[string]bool)
}
if seen[item.KBID][item.ID] {
continue
}
seen[item.KBID][item.ID] = true
byKB[item.KBID] = append(byKB[item.KBID], item.ID)
}
scopes := make([]types.TagScope, 0, len(byKB))
for kbID, tagIDs := range byKB {
scopes = append(scopes, types.TagScope{KnowledgeBaseID: kbID, TagIDs: tagIDs})
}
return scopes
}
func mentionedIDsByType(items []MentionedItemRequest, itemType string) []string {
seen := make(map[string]bool)
result := make([]string, 0)
for _, item := range items {
if item.Type != itemType || item.ID == "" || seen[item.ID] {
continue
}
seen[item.ID] = true
result = append(result, item.ID)
}
return result
}
func dedupRequestStrings(values []string) []string {
seen := make(map[string]bool, len(values))
result := make([]string, 0, len(values))
for _, value := range values {
if value == "" || seen[value] {
continue
}
seen[value] = true
result = append(result, value)
}
return result
}
// setSSEHeaders sets the standard Server-Sent Events headers
func setSSEHeaders(c *gin.Context) {
c.Header("Content-Type", "text/event-stream")
+20
View File
@@ -31,6 +31,10 @@ type qaRequestContext struct {
assistantMessage *types.Message
knowledgeBaseIDs []string
knowledgeIDs []string
tagScopes []types.TagScope
tagIDs []string
mcpServiceIDs []string
skillNames []string
summaryModelID string
webSearchEnabled bool
enableMemory bool // Whether memory feature is enabled
@@ -59,6 +63,9 @@ func (rc *qaRequestContext) buildQARequest() *types.QARequest {
CustomAgent: rc.customAgent,
KnowledgeBaseIDs: rc.knowledgeBaseIDs,
KnowledgeIDs: rc.knowledgeIDs,
TagScopes: rc.tagScopes,
MCPServiceIDs: rc.mcpServiceIDs,
SkillNames: rc.skillNames,
ImageURLs: imageURLs,
ImageDescription: imageDescription,
UserMessageID: rc.userMessageID,
@@ -240,6 +247,11 @@ func (h *Handler) parseQARequest(c *gin.Context, logPrefix string) (*qaRequestCo
// had memory enabled in practice, keep that behaviour.
enableMemory := h.resolveEnableMemory(ctx, request.EnableMemory)
tagScopes := tagScopesFromMentionedItems(request.MentionedItems)
tagIDs := dedupRequestStrings(append(request.TagIDs, mentionedIDsByType(request.MentionedItems, "tag")...))
mcpServiceIDs := dedupRequestStrings(append(request.MCPServiceIDs, mentionedIDsByType(request.MentionedItems, "mcp")...))
skillNames := dedupRequestStrings(append(request.SkillNames, mentionedIDsByType(request.MentionedItems, "skill")...))
// Build request context
reqCtx := &qaRequestContext{
ctx: ctx,
@@ -259,6 +271,10 @@ func (h *Handler) parseQARequest(c *gin.Context, logPrefix string) (*qaRequestCo
},
knowledgeBaseIDs: secutils.SanitizeForLogArray(kbIDs),
knowledgeIDs: secutils.SanitizeForLogArray(knowledgeIDs),
tagScopes: tagScopes,
tagIDs: secutils.SanitizeForLogArray(tagIDs),
mcpServiceIDs: secutils.SanitizeForLogArray(mcpServiceIDs),
skillNames: secutils.SanitizeForLogArray(skillNames),
summaryModelID: secutils.SanitizeForLog(request.SummaryModelID),
webSearchEnabled: request.WebSearchEnabled,
enableMemory: enableMemory,
@@ -887,6 +903,10 @@ func (h *Handler) persistLastRequestState(parentCtx context.Context, reqCtx *qaR
ModelID: reqCtx.summaryModelID,
KnowledgeBaseIDs: reqCtx.knowledgeBaseIDs,
KnowledgeIDs: reqCtx.knowledgeIDs,
TagIDs: reqCtx.tagIDs,
MCPServiceIDs: reqCtx.mcpServiceIDs,
SkillNames: reqCtx.skillNames,
MentionedItems: reqCtx.mentionedItems,
WebSearchEnabled: reqCtx.webSearchEnabled,
}
+11 -4
View File
@@ -21,10 +21,14 @@ type GenerateTitleRequest struct {
// MentionedItemRequest represents a mentioned item in the request
type MentionedItemRequest struct {
ID string `json:"id"`
Name string `json:"name"`
Type string `json:"type"` // "kb" for knowledge base, "file" for file
KBType string `json:"kb_type"` // "document" or "faq" (only for kb type)
ID string `json:"id"`
Name string `json:"name"`
Type string `json:"type"` // "kb", "file", "tag", "mcp", "skill"
KBType string `json:"kb_type"` // "document" or "faq" (only for kb type)
KBID string `json:"kb_id"` // Parent knowledge base for file/tag mentions
KBName string `json:"kb_name"` // Display name for parent KB
ServiceID string `json:"service_id"` // Parent MCP service for MCP tool mentions
SkillName string `json:"skill_name"` // Preloaded agent skill name
}
// ImageAttachment represents an image in a chat request.
@@ -45,6 +49,9 @@ type CreateKnowledgeQARequest struct {
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)
MCPServiceIDs []string `json:"mcp_service_ids"` // Per-request MCP services selected via @mention
SkillNames []string `json:"skill_names"` // Per-request Skills selected via @mention
TagIDs []string `json:"tag_ids"` // @mentioned tag IDs (display/debug; scoped via MentionedItems)
MentionedItems []MentionedItemRequest `json:"mentioned_items"` // @mentioned knowledge bases and files
DisableTitle bool `json:"disable_title"` // Whether to disable auto title generation
// EnableMemory is the per-request override for the memory feature.