feat: Introduce agent type presets for smart-reasoning mode
- Added a new configuration file `agent_type_presets.yaml` to define various agent type presets, including RAG Q&A, Wiki Q&A, Hybrid (Wiki + RAG), and Data Analysis. - Updated the `builtin_agents.yaml` to associate built-in agents with the new agent type presets, enhancing their configuration options. - Enhanced the agent system prompt templates to reflect the new agent types and their functionalities. - Implemented API endpoints to retrieve agent type presets, allowing for dynamic loading in the frontend. - Updated the frontend to support agent type selection in the agent editor modal, improving user experience by auto-filling relevant configurations based on selected presets. These changes significantly enhance the flexibility and usability of the agent system, allowing users to easily configure agents based on predefined types.
This commit is contained in:
@@ -37,10 +37,6 @@ type AgentConfig struct {
|
||||
// Whether to retrieve knowledge base only when explicitly mentioned with @ (default: false)
|
||||
RetrieveKBOnlyWhenMentioned bool `json:"retrieve_kb_only_when_mentioned"`
|
||||
|
||||
// RetrievalPreference controls which retrieval tools the agent uses:
|
||||
// "auto" (default), "vector_only", "wiki_only", "hybrid"
|
||||
RetrievalPreference string `json:"retrieval_preference"`
|
||||
|
||||
// Whether to retain retrieval history (like wiki_read_page results) across turns (default: false)
|
||||
RetainRetrievalHistory bool `json:"retain_retrieval_history"`
|
||||
|
||||
|
||||
@@ -0,0 +1,213 @@
|
||||
package types
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// YAML data structures for config/agent_type_presets.yaml
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
// AgentTypePresetI18n holds localised label and description for a single locale.
|
||||
type AgentTypePresetI18n struct {
|
||||
Label string `yaml:"label" json:"label"`
|
||||
Description string `yaml:"description" json:"description"`
|
||||
}
|
||||
|
||||
// AgentTypeKBFilter describes how the agent editor should filter selectable KBs
|
||||
// when this preset is active. Predicates are evaluated against the KB's
|
||||
// capabilities (see KBCapabilities).
|
||||
//
|
||||
// If all three slices are empty, the preset imposes no KB restriction.
|
||||
type AgentTypeKBFilter struct {
|
||||
// AnyOf: KB must expose at least ONE of these capability flags.
|
||||
AnyOf []string `yaml:"any_of" json:"any_of,omitempty"`
|
||||
// AllOf: KB must expose ALL of these capability flags.
|
||||
AllOf []string `yaml:"all_of" json:"all_of,omitempty"`
|
||||
// NoneOf: KB must expose NONE of these capability flags.
|
||||
NoneOf []string `yaml:"none_of" json:"none_of,omitempty"`
|
||||
}
|
||||
|
||||
// AgentTypePresetEntry is one entry in the agent_type_presets list in YAML.
|
||||
//
|
||||
// Only fields relevant for auto-filling the editor form are included under
|
||||
// `config`. Everything else (retrieval thresholds, etc.) falls back to the
|
||||
// user-edited defaults / CustomAgent.EnsureDefaults.
|
||||
type AgentTypePresetEntry struct {
|
||||
ID string `yaml:"id" json:"id"`
|
||||
I18n map[string]AgentTypePresetI18n `yaml:"i18n" json:"i18n"`
|
||||
Config *AgentTypePresetConfig `yaml:"config" json:"config,omitempty"`
|
||||
KBFilter *AgentTypeKBFilter `yaml:"kb_filter" json:"kb_filter,omitempty"`
|
||||
}
|
||||
|
||||
// AgentTypePresetConfig is a restricted view of CustomAgentConfig used purely
|
||||
// as the preset payload. Any field left at its zero value is NOT applied by
|
||||
// the preset so the user's existing value stays untouched.
|
||||
//
|
||||
// We mirror json tags (not yaml tags) directly from CustomAgentConfig so the
|
||||
// frontend can apply them via simple Object.assign.
|
||||
type AgentTypePresetConfig struct {
|
||||
SystemPromptID string `yaml:"system_prompt_id" json:"system_prompt_id,omitempty"`
|
||||
Temperature float64 `yaml:"temperature" json:"temperature,omitempty"`
|
||||
MaxIterations int `yaml:"max_iterations" json:"max_iterations,omitempty"`
|
||||
AllowedTools []string `yaml:"allowed_tools" json:"allowed_tools,omitempty"`
|
||||
RetainRetrievalHistory bool `yaml:"retain_retrieval_history" json:"retain_retrieval_history,omitempty"`
|
||||
FAQPriorityEnabled bool `yaml:"faq_priority_enabled" json:"faq_priority_enabled,omitempty"`
|
||||
WebSearchEnabled bool `yaml:"web_search_enabled" json:"web_search_enabled,omitempty"`
|
||||
SupportedFileTypes []string `yaml:"supported_file_types" json:"supported_file_types,omitempty"`
|
||||
// KBSelectionMode presets the KB picker mode: "all" | "selected" | "none".
|
||||
// When empty, the user's current value stays untouched.
|
||||
KBSelectionMode string `yaml:"kb_selection_mode" json:"kb_selection_mode,omitempty"`
|
||||
}
|
||||
|
||||
// agentTypePresetsFile is the top-level YAML structure.
|
||||
type agentTypePresetsFile struct {
|
||||
Presets []AgentTypePresetEntry `yaml:"agent_type_presets"`
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Global registry (populated from YAML at startup)
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
var (
|
||||
agentTypePresets map[string]*AgentTypePresetEntry // keyed by preset ID
|
||||
agentTypePresetsMu sync.RWMutex
|
||||
agentTypePresetsOnce sync.Once
|
||||
agentTypePresetIDs []string // insertion order, for stable UI listing
|
||||
)
|
||||
|
||||
// LoadAgentTypePresetsConfig loads agent-type preset definitions from the given
|
||||
// config directory (e.g. "./config"). The file must be named
|
||||
// "agent_type_presets.yaml". Called once at startup (after LoadConfig determines
|
||||
// the config directory).
|
||||
//
|
||||
// If the file does not exist, this is a no-op and the registry stays empty —
|
||||
// in that case the frontend will simply not offer the agent-type dropdown.
|
||||
func LoadAgentTypePresetsConfig(configDir string) error {
|
||||
var loadErr error
|
||||
agentTypePresetsOnce.Do(func() {
|
||||
filePath := filepath.Join(configDir, "agent_type_presets.yaml")
|
||||
data, err := os.ReadFile(filePath)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return
|
||||
}
|
||||
loadErr = fmt.Errorf("read agent_type_presets.yaml: %w", err)
|
||||
return
|
||||
}
|
||||
|
||||
var file agentTypePresetsFile
|
||||
if err := yaml.Unmarshal(data, &file); err != nil {
|
||||
loadErr = fmt.Errorf("parse agent_type_presets.yaml: %w", err)
|
||||
return
|
||||
}
|
||||
|
||||
agentTypePresetsMu.Lock()
|
||||
defer agentTypePresetsMu.Unlock()
|
||||
|
||||
agentTypePresets = make(map[string]*AgentTypePresetEntry, len(file.Presets))
|
||||
agentTypePresetIDs = make([]string, 0, len(file.Presets))
|
||||
for i := range file.Presets {
|
||||
entry := &file.Presets[i]
|
||||
if entry.ID == "" {
|
||||
continue
|
||||
}
|
||||
agentTypePresets[entry.ID] = entry
|
||||
agentTypePresetIDs = append(agentTypePresetIDs, entry.ID)
|
||||
}
|
||||
})
|
||||
return loadErr
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Public API
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
// ListAgentTypePresetsWithContext returns all registered agent-type presets in
|
||||
// their YAML-declared order, with i18n resolved against the request locale.
|
||||
//
|
||||
// The returned slice is a fresh copy safe to mutate by the caller (e.g. to
|
||||
// tweak the i18n label before encoding).
|
||||
func ListAgentTypePresetsWithContext(ctx context.Context) []AgentTypePresetEntry {
|
||||
locale := localeFromCtx(ctx)
|
||||
|
||||
agentTypePresetsMu.RLock()
|
||||
defer agentTypePresetsMu.RUnlock()
|
||||
|
||||
out := make([]AgentTypePresetEntry, 0, len(agentTypePresetIDs))
|
||||
for _, id := range agentTypePresetIDs {
|
||||
entry := agentTypePresets[id]
|
||||
if entry == nil {
|
||||
continue
|
||||
}
|
||||
// Shallow copy and narrow i18n down to the caller's locale so the
|
||||
// API response is compact. We keep both "default" and the resolved
|
||||
// locale entry in case the frontend wants to fall back.
|
||||
cp := *entry
|
||||
if len(entry.I18n) > 0 {
|
||||
cp.I18n = resolveAgentTypeI18n(entry.I18n, locale)
|
||||
}
|
||||
out = append(out, cp)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// GetAgentTypePreset returns the raw entry for the given preset ID, or nil.
|
||||
// Used by prompt-resolution at startup.
|
||||
func GetAgentTypePreset(id string) *AgentTypePresetEntry {
|
||||
agentTypePresetsMu.RLock()
|
||||
defer agentTypePresetsMu.RUnlock()
|
||||
return agentTypePresets[id]
|
||||
}
|
||||
|
||||
// ResolveAgentTypePresetPromptRefs iterates over all presets and resolves
|
||||
// system_prompt_id references into the actual prompt template content via the
|
||||
// provided resolver. This lets downstream services treat the `system_prompt_id`
|
||||
// field as a concrete prompt string if they need to.
|
||||
//
|
||||
// We keep system_prompt_id as-is in the preset (it's also what the editor
|
||||
// writes back to CustomAgentConfig), so this hook is only used to validate
|
||||
// that every referenced template actually exists at startup.
|
||||
func ResolveAgentTypePresetPromptRefs(resolver func(id string) string) {
|
||||
agentTypePresetsMu.Lock()
|
||||
defer agentTypePresetsMu.Unlock()
|
||||
for _, entry := range agentTypePresets {
|
||||
if entry == nil || entry.Config == nil || entry.Config.SystemPromptID == "" {
|
||||
continue
|
||||
}
|
||||
if content := resolver(entry.Config.SystemPromptID); content == "" {
|
||||
fmt.Printf(
|
||||
"Warning: agent type preset %q references system_prompt_id %q but template not found\n",
|
||||
entry.ID, entry.Config.SystemPromptID,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// resolveAgentTypeI18n picks a reasonable subset of i18n entries for the given
|
||||
// locale: the exact-match entry (if any) plus the "default" fallback. Callers
|
||||
// that want full i18n can pass an empty locale to get everything.
|
||||
func resolveAgentTypeI18n(m map[string]AgentTypePresetI18n, locale string) map[string]AgentTypePresetI18n {
|
||||
out := make(map[string]AgentTypePresetI18n, 2)
|
||||
if v, ok := m["default"]; ok {
|
||||
out["default"] = v
|
||||
}
|
||||
if locale != "" {
|
||||
if v, ok := m[locale]; ok {
|
||||
out[locale] = v
|
||||
}
|
||||
}
|
||||
// If neither matched, surface the whole map so the frontend can decide.
|
||||
if len(out) == 0 {
|
||||
for k, v := range m {
|
||||
out[k] = v
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
@@ -36,6 +36,27 @@ const (
|
||||
AgentModeSmartReasoning = "smart-reasoning"
|
||||
)
|
||||
|
||||
// AgentType constants for Smart-Reasoning agent presets.
|
||||
// These presets bundle a recommended system prompt template,
|
||||
// tool allowlist, KB compatibility hint, and other defaults so users
|
||||
// don't have to configure everything from scratch.
|
||||
// AgentTypeCustom means the user wants full control and we won't
|
||||
// auto-fill anything based on the preset.
|
||||
const (
|
||||
// AgentTypeRAGQA prefers vector/keyword chunk retrieval on document KBs.
|
||||
AgentTypeRAGQA = "rag-qa"
|
||||
// AgentTypeWikiQA prefers wiki-page navigation on wiki-enabled KBs.
|
||||
AgentTypeWikiQA = "wiki-qa"
|
||||
// AgentTypeHybridRAGWiki orchestrates Wiki + RAG on KBs where both are enabled.
|
||||
AgentTypeHybridRAGWiki = "hybrid-rag-wiki"
|
||||
// AgentTypeDataAnalysis runs SQL / statistics over tabular files (CSV, Excel)
|
||||
// uploaded into the KB. Retrieval semantics (vector/wiki/…) are largely
|
||||
// irrelevant — this type is about data_schema + data_analysis tools.
|
||||
AgentTypeDataAnalysis = "data-analysis"
|
||||
// AgentTypeCustom is the "no preset" option; user-configured end to end.
|
||||
AgentTypeCustom = "custom"
|
||||
)
|
||||
|
||||
// CustomAgent represents a configurable AI agent (similar to GPTs)
|
||||
type CustomAgent struct {
|
||||
// Unique identifier of the agent (composite primary key with TenantID)
|
||||
@@ -69,6 +90,12 @@ type CustomAgentConfig struct {
|
||||
// ===== Basic Settings =====
|
||||
// Agent mode: "quick-answer" for RAG mode, "smart-reasoning" for ReAct agent mode
|
||||
AgentMode string `yaml:"agent_mode" json:"agent_mode"`
|
||||
// AgentType is a preset category under smart-reasoning mode that pre-fills
|
||||
// system prompt, allowed tools and recommended KB compatibility.
|
||||
// Valid values: "rag-qa", "wiki-qa", "hybrid-rag-wiki", "custom".
|
||||
// Empty / unknown values are treated as "custom" (no preset applied).
|
||||
// Ignored for quick-answer mode.
|
||||
AgentType string `yaml:"agent_type" json:"agent_type,omitempty"`
|
||||
// System prompt for the agent (unified prompt, uses web_search_status placeholder for dynamic behavior)
|
||||
SystemPrompt string `yaml:"system_prompt" json:"system_prompt"`
|
||||
// SystemPromptID references a template ID in prompt_templates/ YAML files.
|
||||
@@ -119,13 +146,6 @@ type CustomAgentConfig struct {
|
||||
// When false, knowledge base retrieval happens according to KBSelectionMode
|
||||
RetrieveKBOnlyWhenMentioned bool `yaml:"retrieve_kb_only_when_mentioned" json:"retrieve_kb_only_when_mentioned"`
|
||||
|
||||
// RetrievalPreference controls which retrieval tools the agent uses:
|
||||
// "auto" - auto-detect from KB capabilities (default, preserves existing behavior)
|
||||
// "vector_only" - only use vector/keyword search tools, no wiki tools
|
||||
// "wiki_only" - only use wiki tools, no vector/keyword search tools
|
||||
// "hybrid" - use both vector/keyword search AND wiki tools
|
||||
RetrievalPreference string `yaml:"retrieval_preference" json:"retrieval_preference"`
|
||||
|
||||
// Whether to retain retrieval history across turns
|
||||
RetainRetrievalHistory bool `yaml:"retain_retrieval_history" json:"retain_retrieval_history"`
|
||||
|
||||
@@ -269,9 +289,6 @@ func (a *CustomAgent) EnsureDefaults() {
|
||||
if a.Config.FallbackStrategy == "" {
|
||||
a.Config.FallbackStrategy = "model"
|
||||
}
|
||||
if a.Config.RetrievalPreference == "" {
|
||||
a.Config.RetrievalPreference = "auto"
|
||||
}
|
||||
if a.Config.MaxCompletionTokens == 0 {
|
||||
a.Config.MaxCompletionTokens = 2048
|
||||
}
|
||||
|
||||
@@ -532,6 +532,53 @@ func (kb *KnowledgeBase) EnsureDefaults() {
|
||||
}
|
||||
}
|
||||
|
||||
// KBCapabilities describes the functional features a knowledge base exposes.
|
||||
// It is computed from the KB's configuration (IndexingStrategy, Type, WikiConfig, …)
|
||||
// and surfaced in the JSON representation of a KnowledgeBase so that the frontend
|
||||
// can filter / enable / disable KB options based on what the selected agent type needs.
|
||||
type KBCapabilities struct {
|
||||
// Vector means semantic (embedding) search is indexed.
|
||||
Vector bool `json:"vector"`
|
||||
// Keyword means BM25 / sparse keyword search is indexed.
|
||||
Keyword bool `json:"keyword"`
|
||||
// Wiki means the wiki feature is enabled and authored pages exist / will be generated.
|
||||
Wiki bool `json:"wiki"`
|
||||
// Graph means knowledge-graph extraction is enabled.
|
||||
Graph bool `json:"graph"`
|
||||
// FAQ means the KB is a FAQ-type KB (Q/A pairs).
|
||||
FAQ bool `json:"faq"`
|
||||
}
|
||||
|
||||
// Capabilities returns the computed capability flags for this KB.
|
||||
// Safe to call on a nil KB (returns zero value).
|
||||
func (kb *KnowledgeBase) Capabilities() KBCapabilities {
|
||||
if kb == nil {
|
||||
return KBCapabilities{}
|
||||
}
|
||||
return KBCapabilities{
|
||||
Vector: kb.IsVectorEnabled(),
|
||||
Keyword: kb.IsKeywordEnabled(),
|
||||
Wiki: kb.IsWikiEnabled(),
|
||||
Graph: kb.IsGraphEnabled(),
|
||||
FAQ: kb.Type == KnowledgeBaseTypeFAQ,
|
||||
}
|
||||
}
|
||||
|
||||
// MarshalJSON augments the default JSON encoding of KnowledgeBase with a computed
|
||||
// `capabilities` field so clients (agent editor) can filter KBs by feature.
|
||||
// It preserves all existing fields verbatim.
|
||||
func (kb *KnowledgeBase) MarshalJSON() ([]byte, error) {
|
||||
type alias KnowledgeBase
|
||||
aux := struct {
|
||||
*alias
|
||||
Capabilities KBCapabilities `json:"capabilities"`
|
||||
}{
|
||||
alias: (*alias)(kb),
|
||||
Capabilities: kb.Capabilities(),
|
||||
}
|
||||
return json.Marshal(aux)
|
||||
}
|
||||
|
||||
// IsWikiEnabled checks if the wiki feature is enabled for this knowledge base.
|
||||
// Wiki requires both IndexingStrategy.WikiEnabled and WikiConfig with Enabled=true.
|
||||
func (kb *KnowledgeBase) IsWikiEnabled() bool {
|
||||
|
||||
Reference in New Issue
Block a user