feat: refactor tool handling to use ToolSet for improved organization and clarity
This commit is contained in:
@@ -11,10 +11,12 @@ import (
|
||||
"cs-agent/internal/ai/runtime/internal/impl/callbacks"
|
||||
"cs-agent/internal/ai/runtime/internal/impl/factory"
|
||||
"cs-agent/internal/ai/runtime/internal/impl/retrievers"
|
||||
"cs-agent/internal/ai/runtime/registry"
|
||||
"cs-agent/internal/models"
|
||||
"cs-agent/internal/pkg/utils"
|
||||
|
||||
"github.com/cloudwego/eino/adk"
|
||||
einotool "github.com/cloudwego/eino/components/tool"
|
||||
"github.com/cloudwego/eino/schema"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
@@ -85,7 +87,7 @@ func (s *Service) Run(ctx context.Context, req Request) (*Summary, error) {
|
||||
summary.ToolCodes = appendIfMissing(summary.ToolCodes, "builtin/tool_search")
|
||||
toolDefsByModelName["tool_search"] = "builtin/tool_search"
|
||||
}
|
||||
for modelName, toolCode := range req.ExtraToolCodes {
|
||||
for modelName, toolCode := range toolSetStaticToolCodes(req.ToolSet) {
|
||||
toolCode = strings.TrimSpace(toolCode)
|
||||
modelName = strings.TrimSpace(modelName)
|
||||
if toolCode == "" || modelName == "" {
|
||||
@@ -119,8 +121,8 @@ func (s *Service) Run(ctx context.Context, req Request) (*Summary, error) {
|
||||
SelectedSkill: req.SelectedSkill,
|
||||
InstructionToolDefinitions: filteredToolDefs,
|
||||
DynamicMCPToolDefinitions: filteredToolDefs,
|
||||
StaticTools: req.ExtraTools,
|
||||
StaticToolCodes: req.ExtraToolCodes,
|
||||
StaticTools: toolSetStaticTools(req.ToolSet),
|
||||
StaticToolCodes: toolSetStaticToolCodes(req.ToolSet),
|
||||
Collector: collector,
|
||||
})
|
||||
if err != nil {
|
||||
@@ -238,7 +240,7 @@ func (s *Service) Resume(ctx context.Context, req ResumeRequest) (*Summary, erro
|
||||
summary.ToolCodes = appendIfMissing(summary.ToolCodes, "builtin/tool_search")
|
||||
toolDefsByModelName["tool_search"] = "builtin/tool_search"
|
||||
}
|
||||
for modelName, toolCode := range req.ExtraToolCodes {
|
||||
for modelName, toolCode := range toolSetStaticToolCodes(req.ToolSet) {
|
||||
toolCode = strings.TrimSpace(toolCode)
|
||||
modelName = strings.TrimSpace(modelName)
|
||||
if toolCode == "" || modelName == "" {
|
||||
@@ -255,8 +257,8 @@ func (s *Service) Resume(ctx context.Context, req ResumeRequest) (*Summary, erro
|
||||
AIConfig: req.AIConfig,
|
||||
InstructionToolDefinitions: toolDefs,
|
||||
DynamicMCPToolDefinitions: toolDefs,
|
||||
StaticTools: req.ExtraTools,
|
||||
StaticToolCodes: req.ExtraToolCodes,
|
||||
StaticTools: toolSetStaticTools(req.ToolSet),
|
||||
StaticToolCodes: toolSetStaticToolCodes(req.ToolSet),
|
||||
Collector: collector,
|
||||
})
|
||||
if err != nil {
|
||||
@@ -543,3 +545,17 @@ func buildKnowledgeContext(items []rag.RetrieveResult) string {
|
||||
}
|
||||
return strings.TrimSpace(builder.String())
|
||||
}
|
||||
|
||||
func toolSetStaticTools(toolSet *registry.ToolSet) []einotool.BaseTool {
|
||||
if toolSet == nil {
|
||||
return nil
|
||||
}
|
||||
return toolSet.StaticTools
|
||||
}
|
||||
|
||||
func toolSetStaticToolCodes(toolSet *registry.ToolSet) map[string]string {
|
||||
if toolSet == nil {
|
||||
return nil
|
||||
}
|
||||
return toolSet.StaticToolCodes
|
||||
}
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
package engine
|
||||
|
||||
import (
|
||||
"cs-agent/internal/ai/runtime/registry"
|
||||
"cs-agent/internal/models"
|
||||
|
||||
einotool "github.com/cloudwego/eino/components/tool"
|
||||
)
|
||||
|
||||
type Request struct {
|
||||
@@ -15,18 +14,16 @@ type Request struct {
|
||||
SkillRouteReason string
|
||||
SkillRouteTrace string
|
||||
CheckPointID string
|
||||
ExtraTools []einotool.BaseTool
|
||||
ExtraToolCodes map[string]string
|
||||
ToolSet *registry.ToolSet
|
||||
}
|
||||
|
||||
type ResumeRequest struct {
|
||||
Conversation *models.Conversation
|
||||
AIAgent *models.AIAgent
|
||||
AIConfig *models.AIConfig
|
||||
CheckPointID string
|
||||
ResumeData map[string]any
|
||||
ExtraTools []einotool.BaseTool
|
||||
ExtraToolCodes map[string]string
|
||||
Conversation *models.Conversation
|
||||
AIAgent *models.AIAgent
|
||||
AIConfig *models.AIConfig
|
||||
CheckPointID string
|
||||
ResumeData map[string]any
|
||||
ToolSet *registry.ToolSet
|
||||
}
|
||||
|
||||
type InterruptContextSummary struct {
|
||||
|
||||
@@ -20,8 +20,8 @@ func NewRegistry(tools ...Tool) *Registry {
|
||||
|
||||
func (r *Registry) Resolve(ctx Context) (*ToolSet, error) {
|
||||
ret := &ToolSet{
|
||||
Tools: make([]einotool.BaseTool, 0, len(r.tools)),
|
||||
ToolCodes: make(map[string]string),
|
||||
StaticTools: make([]einotool.BaseTool, 0, len(r.tools)),
|
||||
StaticToolCodes: make(map[string]string),
|
||||
}
|
||||
allowedToolCodes := makeAllowedToolCodeSet(ctx.AllowedToolCodes)
|
||||
for _, toolDef := range r.tools {
|
||||
@@ -45,8 +45,8 @@ func (r *Registry) Resolve(ctx Context) (*ToolSet, error) {
|
||||
if toolName == "" || toolCode == "" {
|
||||
continue
|
||||
}
|
||||
ret.Tools = append(ret.Tools, tool)
|
||||
ret.ToolCodes[toolName] = toolCode
|
||||
ret.StaticTools = append(ret.StaticTools, tool)
|
||||
ret.StaticToolCodes[toolName] = toolCode
|
||||
}
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
@@ -14,9 +14,14 @@ type Context struct {
|
||||
AllowedToolCodes []string
|
||||
}
|
||||
|
||||
// ToolSet 描述当前运行时可直接挂载到 ToolsNode 上的固定工具集合。
|
||||
//
|
||||
// 这里不承载通过 tool_search 动态暴露的 MCP 工具;动态工具仍由 engine 层单独装配。
|
||||
type ToolSet struct {
|
||||
Tools []einotool.BaseTool
|
||||
ToolCodes map[string]string
|
||||
// StaticTools 为固定可见工具实例,例如 Graph Tool。
|
||||
StaticTools []einotool.BaseTool
|
||||
// StaticToolCodes 为固定工具的 modelName -> toolCode 映射,用于 trace 和运行日志归因。
|
||||
StaticToolCodes map[string]string
|
||||
}
|
||||
|
||||
type Tool interface {
|
||||
|
||||
@@ -50,8 +50,7 @@ func (s *service) Run(ctx context.Context, req Request) (*Summary, error) {
|
||||
SkillRouteReason: req.SkillRouteReason,
|
||||
SkillRouteTrace: req.SkillRouteTrace,
|
||||
CheckPointID: req.CheckPointID,
|
||||
ExtraTools: req.ExtraTools,
|
||||
ExtraToolCodes: req.ExtraToolCodes,
|
||||
ToolSet: req.ToolSet,
|
||||
})
|
||||
if err != nil {
|
||||
ret := toSummary(summary)
|
||||
@@ -72,13 +71,12 @@ func (s *service) Resume(ctx context.Context, req ResumeRequest) (*Summary, erro
|
||||
return nil, err
|
||||
}
|
||||
summary, err := s.runtime.Resume(ctx, engine.ResumeRequest{
|
||||
Conversation: req.Conversation,
|
||||
AIAgent: req.AIAgent,
|
||||
AIConfig: req.AIConfig,
|
||||
CheckPointID: req.CheckPointID,
|
||||
ResumeData: req.ResumeData,
|
||||
ExtraTools: req.ExtraTools,
|
||||
ExtraToolCodes: req.ExtraToolCodes,
|
||||
Conversation: req.Conversation,
|
||||
AIAgent: req.AIAgent,
|
||||
AIConfig: req.AIConfig,
|
||||
CheckPointID: req.CheckPointID,
|
||||
ResumeData: req.ResumeData,
|
||||
ToolSet: req.ToolSet,
|
||||
})
|
||||
if err != nil {
|
||||
return toSummary(summary), err
|
||||
@@ -87,7 +85,7 @@ func (s *service) Resume(ctx context.Context, req ResumeRequest) (*Summary, erro
|
||||
}
|
||||
|
||||
func (s *service) prepareToolsForRun(req *Request) error {
|
||||
if req == nil || len(req.ExtraTools) > 0 || len(req.ExtraToolCodes) > 0 || s.registry == nil {
|
||||
if req == nil || req.ToolSet != nil || s.registry == nil {
|
||||
return nil
|
||||
}
|
||||
toolSet, err := s.registry.Resolve(registry.Context{
|
||||
@@ -100,13 +98,12 @@ func (s *service) prepareToolsForRun(req *Request) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req.ExtraTools = toolSet.Tools
|
||||
req.ExtraToolCodes = toolSet.ToolCodes
|
||||
req.ToolSet = toolSet
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *service) prepareToolsForResume(req *ResumeRequest) error {
|
||||
if req == nil || len(req.ExtraTools) > 0 || len(req.ExtraToolCodes) > 0 || s.registry == nil {
|
||||
if req == nil || req.ToolSet != nil || s.registry == nil {
|
||||
return nil
|
||||
}
|
||||
toolSet, err := s.registry.Resolve(registry.Context{
|
||||
@@ -118,8 +115,7 @@ func (s *service) prepareToolsForResume(req *ResumeRequest) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req.ExtraTools = toolSet.Tools
|
||||
req.ExtraToolCodes = toolSet.ToolCodes
|
||||
req.ToolSet = toolSet
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
package runtime
|
||||
|
||||
import (
|
||||
"cs-agent/internal/ai/runtime/registry"
|
||||
"cs-agent/internal/models"
|
||||
|
||||
einotool "github.com/cloudwego/eino/components/tool"
|
||||
)
|
||||
|
||||
type Request struct {
|
||||
@@ -16,18 +15,16 @@ type Request struct {
|
||||
SkillRouteReason string
|
||||
SkillRouteTrace string
|
||||
CheckPointID string
|
||||
ExtraTools []einotool.BaseTool
|
||||
ExtraToolCodes map[string]string
|
||||
ToolSet *registry.ToolSet
|
||||
}
|
||||
|
||||
type ResumeRequest struct {
|
||||
Conversation *models.Conversation
|
||||
AIAgent *models.AIAgent
|
||||
AIConfig *models.AIConfig
|
||||
CheckPointID string
|
||||
ResumeData map[string]any
|
||||
ExtraTools []einotool.BaseTool
|
||||
ExtraToolCodes map[string]string
|
||||
Conversation *models.Conversation
|
||||
AIAgent *models.AIAgent
|
||||
AIConfig *models.AIConfig
|
||||
CheckPointID string
|
||||
ResumeData map[string]any
|
||||
ToolSet *registry.ToolSet
|
||||
}
|
||||
|
||||
type InterruptContextSummary struct {
|
||||
|
||||
Reference in New Issue
Block a user