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
+3
View File
@@ -47,6 +47,9 @@ type AgentConfig struct {
// Runtime-only fields (not persisted)
VLMModelID string `json:"-"` // VLM model ID for tool result image analysis (set from CustomAgent config)
// Per-request @mention pins (runtime only; injected as <must_use> in the user message).
PinnedMCPServiceIDs []string `json:"-"`
PinnedSkillNames []string `json:"-"`
// LLM call timeout in seconds (default: 120). Controls the maximum time for a single LLM call.
LLMCallTimeout int `json:"llm_call_timeout,omitempty"`
+4
View File
@@ -165,10 +165,14 @@ func (c *ChatManage) Clone() *ChatManage {
if t != nil {
kidsCopy := make([]string, len(t.KnowledgeIDs))
copy(kidsCopy, t.KnowledgeIDs)
tagIDsCopy := make([]string, len(t.TagIDs))
copy(tagIDsCopy, t.TagIDs)
searchTargets[i] = &SearchTarget{
Type: t.Type,
KnowledgeBaseID: t.KnowledgeBaseID,
TenantID: t.TenantID,
KnowledgeIDs: kidsCopy,
TagIDs: tagIDsCopy,
}
}
}
+2 -1
View File
@@ -73,11 +73,12 @@ type CustomAgentService interface {
// - agentID: Agent ID
// - kbIDs: Optional knowledge base IDs to override agent config
// - knowledgeIDs: Optional knowledge item IDs to further filter
// - tagIDs: Optional knowledge tag IDs; resolved to knowledge item IDs (OR semantics)
// - limit: Maximum number of questions to return
// Returns:
// - List of suggested questions
// - Possible errors
GetSuggestedQuestions(ctx context.Context, agentID string, kbIDs []string, knowledgeIDs []string, limit int) ([]types.SuggestedQuestion, error)
GetSuggestedQuestions(ctx context.Context, agentID string, kbIDs []string, knowledgeIDs []string, tagIDs []string, limit int) ([]types.SuggestedQuestion, error)
}
// CustomAgentRepository defines the custom agent repository interface
+3 -3
View File
@@ -194,9 +194,9 @@ type KnowledgeService interface {
UpdateLastFAQImportResultDisplayStatus(ctx context.Context, kbID string, displayStatus string) error
// SearchKnowledge searches knowledge items by keyword across the tenant.
// fileTypes: optional list of file extensions to filter by (e.g., ["csv", "xlsx"])
SearchKnowledge(ctx context.Context, keyword string, offset, limit int, fileTypes []string) ([]*types.Knowledge, bool, error)
SearchKnowledge(ctx context.Context, keyword string, offset, limit int, fileTypes []string) ([]*types.Knowledge, bool, int64, error)
// SearchKnowledgeForScopes searches knowledge within the given (tenant_id, kb_id) scopes (e.g. for shared agent context).
SearchKnowledgeForScopes(ctx context.Context, scopes []types.KnowledgeSearchScope, keyword string, offset, limit int, fileTypes []string) ([]*types.Knowledge, bool, error)
SearchKnowledgeForScopes(ctx context.Context, scopes []types.KnowledgeSearchScope, keyword string, offset, limit int, fileTypes []string) ([]*types.Knowledge, bool, int64, error)
}
// KnowledgeRepository defines the interface for knowledge repositories.
@@ -262,7 +262,7 @@ type KnowledgeRepository interface {
// Used by data source sync to locate existing items by external_id.
FindByMetadataKey(ctx context.Context, tenantID uint64, kbID string, key string, value string) (*types.Knowledge, error)
// SearchKnowledgeInScopes searches knowledge items by keyword within the given (tenant_id, kb_id) scopes (own + shared).
SearchKnowledgeInScopes(ctx context.Context, scopes []types.KnowledgeSearchScope, keyword string, offset, limit int, fileTypes []string) ([]*types.Knowledge, bool, error)
SearchKnowledgeInScopes(ctx context.Context, scopes []types.KnowledgeSearchScope, keyword string, offset, limit int, fileTypes []string) ([]*types.Knowledge, bool, int64, error)
// ListIDsByTagIDs returns all knowledge IDs that have any of the specified tag IDs (OR semantics).
ListIDsByTagIDs(ctx context.Context, tenantID uint64, kbID string, tagIDs []string) ([]string, error)
// SetKnowledgeTags replaces all tags for a single knowledge entry (deletes old, inserts new).
+9 -5
View File
@@ -24,10 +24,14 @@ type History struct {
// MentionedItem represents a mentioned knowledge base or file
type MentionedItem 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
}
// MessageImage represents an image attached to a chat message
@@ -211,7 +215,7 @@ type Message struct {
// Empty for non-retrieval intents or assistant messages.
RenderedContent string `json:"-" gorm:"type:text;column:rendered_content;default:''"`
// Channel indicates the source channel of this message (e.g., "web", "api", "im")
Channel string `json:"channel,omitempty" gorm:"type:varchar(50);default:''"`
Channel string `json:"channel,omitempty" gorm:"type:varchar(50);default:''"`
// KnowledgeID links this message to a Knowledge entry in the chat history knowledge base
// Used for vector search indexing: when set, the message content has been indexed as a Knowledge passage
KnowledgeID string `json:"knowledge_id,omitempty" gorm:"type:varchar(36);index"`
+16 -13
View File
@@ -4,18 +4,21 @@ package types
// replacing the previous 14-parameter method signatures.
// EventBus is passed separately to avoid circular dependency with the event package.
type QARequest struct {
Session *Session // The conversation session
Query string // User query text
AssistantMessageID string // Pre-created assistant message ID
SummaryModelID string // Optional model override; empty = use agent/KB default
CustomAgent *CustomAgent // Optional custom agent for config override
KnowledgeBaseIDs []string // Knowledge base IDs to search (from request + @mentions)
KnowledgeIDs []string // Specific knowledge (file) IDs to search
ImageURLs []string // Image URLs for multimodal input
ImageDescription string // VLM-generated image description (fallback for non-vision models)
UserMessageID string // Created user message ID
WebSearchEnabled bool // Whether web search is enabled for this request
EnableMemory bool // Whether memory feature is enabled
QuotedContext string // Quoted message content from IM quote-reply (appended at LLM prompt stage, not used for retrieval)
Session *Session // The conversation session
Query string // User query text
AssistantMessageID string // Pre-created assistant message ID
SummaryModelID string // Optional model override; empty = use agent/KB default
CustomAgent *CustomAgent // Optional custom agent for config override
KnowledgeBaseIDs []string // Knowledge base IDs to search (from request + @mentions)
KnowledgeIDs []string // Specific knowledge (file) IDs to search
TagScopes []TagScope // Tag-constrained KB scopes from @mentions
MCPServiceIDs []string // Per-request MCP service IDs from @mentions
SkillNames []string // Per-request preloaded skill names from @mentions
ImageURLs []string // Image URLs for multimodal input
ImageDescription string // VLM-generated image description (fallback for non-vision models)
UserMessageID string // Created user message ID
WebSearchEnabled bool // Whether web search is enabled for this request
EnableMemory bool // Whether memory feature is enabled
QuotedContext string // Quoted message content from IM quote-reply (appended at LLM prompt stage, not used for retrieval)
Attachments MessageAttachments // File attachments (processed and ready for prompt injection)
}
+8
View File
@@ -15,6 +15,12 @@ const (
SearchTargetTypeKnowledge SearchTargetType = "knowledge"
)
// TagScope represents a tag-constrained retrieval scope inside one knowledge base.
type TagScope struct {
KnowledgeBaseID string `json:"knowledge_base_id"`
TagIDs []string `json:"tag_ids"`
}
// SearchTarget represents a unified search target
// Either search an entire knowledge base, or specific knowledge files within a knowledge base
type SearchTarget struct {
@@ -28,6 +34,8 @@ type SearchTarget struct {
// KnowledgeIDs is the list of specific knowledge IDs to search within the knowledge base
// Only used when Type is SearchTargetTypeKnowledge
KnowledgeIDs []string `json:"knowledge_ids,omitempty"`
// TagIDs limits retrieval to chunks/documents carrying any of these KB-local tags.
TagIDs []string `json:"tag_ids,omitempty"`
}
// SearchTargets is a list of search targets, pre-computed at request entry point
+10 -6
View File
@@ -199,12 +199,16 @@ func (c *SummaryConfig) Scan(value interface{}) error {
// to the frontend by GetSession so the chat input can restore the same agent,
// model, KB scope, etc. the user had selected last time.
type SessionLastRequestState struct {
AgentID string `json:"agent_id,omitempty"`
AgentEnabled bool `json:"agent_enabled"`
ModelID string `json:"model_id,omitempty"`
KnowledgeBaseIDs []string `json:"knowledge_base_ids,omitempty"`
KnowledgeIDs []string `json:"knowledge_ids,omitempty"`
WebSearchEnabled bool `json:"web_search_enabled"`
AgentID string `json:"agent_id,omitempty"`
AgentEnabled bool `json:"agent_enabled"`
ModelID string `json:"model_id,omitempty"`
KnowledgeBaseIDs []string `json:"knowledge_base_ids,omitempty"`
KnowledgeIDs []string `json:"knowledge_ids,omitempty"`
TagIDs []string `json:"tag_ids,omitempty"`
MCPServiceIDs []string `json:"mcp_service_ids,omitempty"`
SkillNames []string `json:"skill_names,omitempty"`
MentionedItems MentionedItems `json:"mentioned_items,omitempty"`
WebSearchEnabled bool `json:"web_search_enabled"`
}
// Value implements driver.Valuer for SessionLastRequestState (JSONB).