chore: import clean Agent Desk snapshot (2/7)
This commit is contained in:
103
internal/ai/runtime/registry/registry.go
Normal file
103
internal/ai/runtime/registry/registry.go
Normal file
@@ -0,0 +1,103 @@
|
||||
package registry
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"agent-desk/internal/pkg/toolx"
|
||||
|
||||
einotool "github.com/cloudwego/eino/components/tool"
|
||||
)
|
||||
|
||||
type Registry struct {
|
||||
tools []Tool
|
||||
}
|
||||
|
||||
func NewRegistry(tools ...Tool) *Registry {
|
||||
return &Registry{
|
||||
tools: tools,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *Registry) Resolve(ctx Context) (*ToolSet, error) {
|
||||
ret := &ToolSet{
|
||||
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 {
|
||||
if toolDef == nil || !toolDef.Enabled(ctx) {
|
||||
continue
|
||||
}
|
||||
spec := toolDef.Spec()
|
||||
toolCode := strings.TrimSpace(spec.Code)
|
||||
if toolCode == "" {
|
||||
toolCode = strings.TrimSpace(toolDef.Code())
|
||||
}
|
||||
if len(allowedToolCodes) > 0 && !isAllowedToolCode(toolCode, allowedToolCodes) {
|
||||
continue
|
||||
}
|
||||
tool, err := toolDef.Build(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if tool == nil {
|
||||
continue
|
||||
}
|
||||
toolName := strings.TrimSpace(spec.Name)
|
||||
if toolName == "" {
|
||||
toolName = strings.TrimSpace(toolDef.Name())
|
||||
}
|
||||
if toolName == "" || toolCode == "" {
|
||||
continue
|
||||
}
|
||||
ret.StaticTools = append(ret.StaticTools, tool)
|
||||
ret.StaticToolCodes[toolName] = toolCode
|
||||
resolvedMetadata := toolx.ResolveToolMetadata(toolCode, toolName)
|
||||
if strings.TrimSpace(resolvedMetadata.ServerCode) == "" {
|
||||
resolvedMetadata.ServerCode = strings.TrimSpace(spec.ServerCode)
|
||||
}
|
||||
if strings.TrimSpace(resolvedMetadata.ToolName) == "" {
|
||||
resolvedMetadata.ToolName = toolName
|
||||
}
|
||||
if resolvedMetadata.SourceType == "" {
|
||||
resolvedMetadata.SourceType = spec.SourceType
|
||||
}
|
||||
ret.StaticToolMetadata[toolName] = ToolMetadata{
|
||||
ToolCode: resolvedMetadata.ToolCode,
|
||||
ServerCode: resolvedMetadata.ServerCode,
|
||||
ToolName: resolvedMetadata.ToolName,
|
||||
SourceType: resolvedMetadata.SourceType,
|
||||
}
|
||||
}
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
func isAllowedToolCode(toolCode string, allowedToolCodes map[string]struct{}) bool {
|
||||
if len(allowedToolCodes) == 0 {
|
||||
return true
|
||||
}
|
||||
toolCode = toolx.NormalizeToolCodeAlias(strings.TrimSpace(toolCode))
|
||||
if _, ok := allowedToolCodes[toolCode]; ok {
|
||||
return true
|
||||
}
|
||||
if toolx.IsAlwaysAllowedToolCode(toolCode) {
|
||||
return true
|
||||
}
|
||||
return toolx.IsImpliedAllowedToolCode(toolCode, allowedToolCodes)
|
||||
}
|
||||
|
||||
func makeAllowedToolCodeSet(input []string) map[string]struct{} {
|
||||
if len(input) == 0 {
|
||||
return nil
|
||||
}
|
||||
ret := make(map[string]struct{}, len(input))
|
||||
for _, item := range input {
|
||||
item = toolx.NormalizeToolCodeAlias(strings.TrimSpace(item))
|
||||
if item == "" {
|
||||
continue
|
||||
}
|
||||
ret[item] = struct{}{}
|
||||
}
|
||||
return ret
|
||||
}
|
||||
80
internal/ai/runtime/registry/registry_test.go
Normal file
80
internal/ai/runtime/registry/registry_test.go
Normal file
@@ -0,0 +1,80 @@
|
||||
package registry_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"agent-desk/internal/ai/runtime/registry"
|
||||
"agent-desk/internal/models"
|
||||
"agent-desk/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) Spec() toolx.ToolSpec {
|
||||
return toolx.ToolSpec{
|
||||
Code: t.code,
|
||||
Name: t.name,
|
||||
ServerCode: toolx.GraphCreateTicketConfirm.ServerCode,
|
||||
SourceType: toolx.GraphCreateTicketConfirm.SourceType,
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
44
internal/ai/runtime/registry/types.go
Normal file
44
internal/ai/runtime/registry/types.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package registry
|
||||
|
||||
import (
|
||||
"agent-desk/internal/models"
|
||||
"agent-desk/internal/pkg/enums"
|
||||
"agent-desk/internal/pkg/toolx"
|
||||
|
||||
einotool "github.com/cloudwego/eino/components/tool"
|
||||
)
|
||||
|
||||
type Context struct {
|
||||
Conversation models.Conversation
|
||||
AIAgent models.AIAgent
|
||||
AIConfig models.AIConfig
|
||||
UserMessage models.Message
|
||||
AllowedToolCodes []string
|
||||
}
|
||||
|
||||
type ToolMetadata struct {
|
||||
ToolCode string
|
||||
ServerCode string
|
||||
ToolName string
|
||||
SourceType enums.ToolSourceType
|
||||
}
|
||||
|
||||
// ToolSet 描述当前运行时可直接挂载到 ToolsNode 上的固定工具集合。
|
||||
//
|
||||
// 这里不承载通过 tool_search 动态暴露的 MCP 工具;动态工具仍由 engine 层单独装配。
|
||||
type ToolSet struct {
|
||||
// StaticTools 为固定可见工具实例,例如 Graph Tool。
|
||||
StaticTools []einotool.BaseTool
|
||||
// StaticToolCodes 为固定工具的 modelName -> toolCode 映射,用于 trace 和运行日志归因。
|
||||
StaticToolCodes map[string]string
|
||||
// StaticToolMetadata 为固定工具的 modelName -> metadata 映射,用于 trace、运行日志和后续装配。
|
||||
StaticToolMetadata map[string]ToolMetadata
|
||||
}
|
||||
|
||||
type Tool interface {
|
||||
Spec() toolx.ToolSpec
|
||||
Name() string
|
||||
Code() string
|
||||
Enabled(ctx Context) bool
|
||||
Build(ctx Context) (einotool.BaseTool, error)
|
||||
}
|
||||
Reference in New Issue
Block a user