feat: add StaticToolMetadata to ToolSet and update related logic for improved tool metadata handling
This commit is contained in:
@@ -20,8 +20,9 @@ func NewRegistry(tools ...Tool) *Registry {
|
||||
|
||||
func (r *Registry) Resolve(ctx Context) (*ToolSet, error) {
|
||||
ret := &ToolSet{
|
||||
StaticTools: make([]einotool.BaseTool, 0, len(r.tools)),
|
||||
StaticToolCodes: make(map[string]string),
|
||||
StaticTools: make([]einotool.BaseTool, 0, len(r.tools)),
|
||||
StaticToolCodes: make(map[string]string),
|
||||
StaticToolMetadata: make(map[string]ToolMetadata),
|
||||
}
|
||||
allowedToolCodes := makeAllowedToolCodeSet(ctx.AllowedToolCodes)
|
||||
for _, toolDef := range r.tools {
|
||||
@@ -45,6 +46,16 @@ func (r *Registry) Resolve(ctx Context) (*ToolSet, error) {
|
||||
}
|
||||
ret.StaticTools = append(ret.StaticTools, tool)
|
||||
ret.StaticToolCodes[toolName] = toolCode
|
||||
serverCode, resolvedToolName, sourceType, _ := toolx.BuildToolMetadata(toolCode)
|
||||
if resolvedToolName == "" {
|
||||
resolvedToolName = toolName
|
||||
}
|
||||
ret.StaticToolMetadata[toolName] = ToolMetadata{
|
||||
ToolCode: toolCode,
|
||||
ServerCode: serverCode,
|
||||
ToolName: resolvedToolName,
|
||||
SourceType: sourceType,
|
||||
}
|
||||
}
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
package registry_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"cs-agent/internal/ai/runtime/registry"
|
||||
"cs-agent/internal/models"
|
||||
"cs-agent/internal/pkg/toolx"
|
||||
|
||||
einotool "github.com/cloudwego/eino/components/tool"
|
||||
"github.com/cloudwego/eino/schema"
|
||||
)
|
||||
|
||||
type stubTool struct {
|
||||
name string
|
||||
code string
|
||||
}
|
||||
|
||||
func (t stubTool) Name() string { return t.name }
|
||||
func (t stubTool) Code() string { return t.code }
|
||||
func (t stubTool) Enabled(registry.Context) bool {
|
||||
return true
|
||||
}
|
||||
func (t stubTool) Build(registry.Context) (einotool.BaseTool, error) {
|
||||
return stubBaseTool{name: t.name}, nil
|
||||
}
|
||||
|
||||
type stubBaseTool struct {
|
||||
name string
|
||||
}
|
||||
|
||||
func (t stubBaseTool) Info(context.Context) (*schema.ToolInfo, error) {
|
||||
return &schema.ToolInfo{Name: t.name}, nil
|
||||
}
|
||||
|
||||
func TestResolveBuildsStaticToolMetadata(t *testing.T) {
|
||||
r := registry.NewRegistry(stubTool{
|
||||
name: toolx.GraphCreateTicketConfirm.Name,
|
||||
code: toolx.GraphCreateTicketConfirm.Code,
|
||||
})
|
||||
toolSet, err := r.Resolve(registry.Context{
|
||||
Conversation: &models.Conversation{ID: 1},
|
||||
AIAgent: &models.AIAgent{ID: 1},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("resolve returned error: %v", err)
|
||||
}
|
||||
if toolSet == nil {
|
||||
t.Fatalf("expected tool set")
|
||||
}
|
||||
if len(toolSet.StaticToolMetadata) != 1 {
|
||||
t.Fatalf("expected 1 metadata item, got %d", len(toolSet.StaticToolMetadata))
|
||||
}
|
||||
item, ok := toolSet.StaticToolMetadata[toolx.GraphCreateTicketConfirm.Name]
|
||||
if !ok {
|
||||
t.Fatalf("missing metadata for %s", toolx.GraphCreateTicketConfirm.Name)
|
||||
}
|
||||
if item.ToolCode != toolx.GraphCreateTicketConfirm.Code {
|
||||
t.Fatalf("unexpected tool code: %s", item.ToolCode)
|
||||
}
|
||||
if item.ServerCode != toolx.GraphCreateTicketConfirm.ServerCode {
|
||||
t.Fatalf("unexpected server code: %s", item.ServerCode)
|
||||
}
|
||||
if item.ToolName != toolx.GraphCreateTicketConfirm.Name {
|
||||
t.Fatalf("unexpected tool name: %s", item.ToolName)
|
||||
}
|
||||
if item.SourceType != toolx.GraphCreateTicketConfirm.SourceType {
|
||||
t.Fatalf("unexpected source type: %s", item.SourceType)
|
||||
}
|
||||
}
|
||||
@@ -14,6 +14,13 @@ type Context struct {
|
||||
AllowedToolCodes []string
|
||||
}
|
||||
|
||||
type ToolMetadata struct {
|
||||
ToolCode string
|
||||
ServerCode string
|
||||
ToolName string
|
||||
SourceType string
|
||||
}
|
||||
|
||||
// ToolSet 描述当前运行时可直接挂载到 ToolsNode 上的固定工具集合。
|
||||
//
|
||||
// 这里不承载通过 tool_search 动态暴露的 MCP 工具;动态工具仍由 engine 层单独装配。
|
||||
@@ -22,6 +29,8 @@ type ToolSet struct {
|
||||
StaticTools []einotool.BaseTool
|
||||
// StaticToolCodes 为固定工具的 modelName -> toolCode 映射,用于 trace 和运行日志归因。
|
||||
StaticToolCodes map[string]string
|
||||
// StaticToolMetadata 为固定工具的 modelName -> metadata 映射,用于 trace、运行日志和后续装配。
|
||||
StaticToolMetadata map[string]ToolMetadata
|
||||
}
|
||||
|
||||
type Tool interface {
|
||||
|
||||
Reference in New Issue
Block a user