Files
ai-agent/internal/ai/application/runtime/tool_catalog.go
T
mlogclub 5d7c10aeab refactor: rename agent widget references to AI agent for consistency
- Updated runtime configuration to use __CS_AI_AGENT_WIDGET_CONFIG__ instead of __CS_AGENT_WIDGET_CONFIG__.
- Changed message types in support host bridge from "cs-agent" to "cs-ai-agent".
- Minified SDK script updated to reflect new AI agent naming conventions.
- Adjusted scrollbar styles in main.scss to use .cs-ai-agent-scrollbar instead of .cs-agent-scrollbar.
2026-05-30 21:19:36 +08:00

91 lines
2.4 KiB
Go

package runtime
import (
"encoding/json"
"strings"
"cs-ai-agent/internal/ai/runtime/registry"
"cs-ai-agent/internal/ai/runtime/tools"
"cs-ai-agent/internal/models"
"cs-ai-agent/internal/pkg/toolx"
)
type toolCatalog struct {
registry *registry.Registry
}
func newToolCatalog() *toolCatalog {
return &toolCatalog{
registry: registry.NewRegistry(buildRuntimeStaticTools()...),
}
}
func buildRuntimeStaticTools() []registry.Tool {
ret := make([]registry.Tool, 0, len(toolx.ListRuntimeStaticToolSpecs()))
for _, spec := range toolx.ListRuntimeStaticToolSpecs() {
tool := tools.NewRuntimeStaticTool(spec.Code)
if tool == nil {
continue
}
ret = append(ret, tool)
}
return ret
}
func (c *toolCatalog) resolveForRun(req Request) (*registry.ToolSet, error) {
return c.registry.Resolve(registry.Context{
Conversation: req.Conversation,
AIAgent: req.AIAgent,
AIConfig: req.AIConfig,
UserMessage: req.UserMessage,
AllowedToolCodes: c.parseAgentAllowedToolCodes(req.AIAgent),
})
}
func (c *toolCatalog) resolveForResume(req ResumeRequest) (*registry.ToolSet, error) {
return c.registry.Resolve(registry.Context{
Conversation: req.Conversation,
AIAgent: req.AIAgent,
AIConfig: req.AIConfig,
AllowedToolCodes: c.parseAgentAllowedToolCodes(req.AIAgent),
})
}
func (c *toolCatalog) parseSkillAllowedToolCodes(skill *models.SkillDefinition) []string {
if skill == nil {
return nil
}
raw := strings.TrimSpace(skill.ToolWhitelist)
if raw == "" {
return nil
}
var items []string
if err := json.Unmarshal([]byte(raw), &items); err != nil {
return nil
}
return toolx.NormalizeToolCodes(items)
}
func (c *toolCatalog) parseAgentAllowedToolCodes(aiAgent models.AIAgent) []string {
ret := make([]string, 0)
if raw := strings.TrimSpace(aiAgent.AllowedMCPTools); raw != "" {
items, err := toolx.ParseAgentMCPToolsJSON(raw)
if err == nil {
for _, item := range items {
ret = append(ret, item.ToolCode)
}
}
}
if raw := strings.TrimSpace(aiAgent.AllowedGraphTools); raw != "" {
var graphTools []string
if err := json.Unmarshal([]byte(raw), &graphTools); err == nil {
ret = append(ret, graphTools...)
}
}
return toolx.NormalizeToolCodes(ret)
}
func (c *toolCatalog) resolveAllowedToolCodes(aiAgent models.AIAgent, skill *models.SkillDefinition) []string {
return toolx.IntersectToolCodes(c.parseAgentAllowedToolCodes(aiAgent), c.parseSkillAllowedToolCodes(skill))
}