From 200631cc0b9c6b4fb81b6ab06ce5fd215e248b96 Mon Sep 17 00:00:00 2001 From: mlogclub Date: Tue, 14 Apr 2026 09:56:37 +0800 Subject: [PATCH] feat: implement Spec method for tools and update tool resolution logic in the registry --- internal/ai/runtime/registry/registry.go | 20 +++++++++++++++++-- internal/ai/runtime/registry/registry_test.go | 9 +++++++++ internal/ai/runtime/registry/types.go | 2 ++ .../tools/analyze_conversation_tool.go | 4 ++++ .../tools/create_ticket_confirm_tool.go | 4 ++++ .../ai/runtime/tools/handoff_graph_tool.go | 4 ++++ .../tools/prepare_ticket_draft_tool.go | 4 ++++ internal/ai/runtime/tools/tool_search_tool.go | 4 ++++ .../tools/triage_service_request_tool.go | 4 ++++ 9 files changed, 53 insertions(+), 2 deletions(-) diff --git a/internal/ai/runtime/registry/registry.go b/internal/ai/runtime/registry/registry.go index b027605..6eb4fcf 100644 --- a/internal/ai/runtime/registry/registry.go +++ b/internal/ai/runtime/registry/registry.go @@ -29,7 +29,11 @@ func (r *Registry) Resolve(ctx Context) (*ToolSet, error) { if toolDef == nil || !toolDef.Enabled(ctx) { continue } - toolCode := strings.TrimSpace(toolDef.Code()) + spec := toolDef.Spec() + toolCode := strings.TrimSpace(spec.Code) + if toolCode == "" { + toolCode = strings.TrimSpace(toolDef.Code()) + } if len(allowedToolCodes) > 0 && !isAllowedToolCode(toolCode, allowedToolCodes) { continue } @@ -40,13 +44,25 @@ func (r *Registry) Resolve(ctx Context) (*ToolSet, error) { if tool == nil { continue } - toolName := strings.TrimSpace(toolDef.Name()) + 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 strings.TrimSpace(resolvedMetadata.SourceType) == "" { + resolvedMetadata.SourceType = strings.TrimSpace(spec.SourceType) + } ret.StaticToolMetadata[toolName] = ToolMetadata{ ToolCode: resolvedMetadata.ToolCode, ServerCode: resolvedMetadata.ServerCode, diff --git a/internal/ai/runtime/registry/registry_test.go b/internal/ai/runtime/registry/registry_test.go index 60eba89..e47d6bf 100644 --- a/internal/ai/runtime/registry/registry_test.go +++ b/internal/ai/runtime/registry/registry_test.go @@ -17,6 +17,15 @@ type stubTool struct { 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 { diff --git a/internal/ai/runtime/registry/types.go b/internal/ai/runtime/registry/types.go index 857f7fa..198923f 100644 --- a/internal/ai/runtime/registry/types.go +++ b/internal/ai/runtime/registry/types.go @@ -2,6 +2,7 @@ package registry import ( "cs-agent/internal/models" + "cs-agent/internal/pkg/toolx" einotool "github.com/cloudwego/eino/components/tool" ) @@ -34,6 +35,7 @@ type ToolSet struct { } type Tool interface { + Spec() toolx.ToolSpec Name() string Code() string Enabled(ctx Context) bool diff --git a/internal/ai/runtime/tools/analyze_conversation_tool.go b/internal/ai/runtime/tools/analyze_conversation_tool.go index 06a59b0..6e829b1 100644 --- a/internal/ai/runtime/tools/analyze_conversation_tool.go +++ b/internal/ai/runtime/tools/analyze_conversation_tool.go @@ -23,6 +23,10 @@ func NewAnalyzeConversationTool() *AnalyzeConversationTool { return &AnalyzeConversationTool{} } +func (t *AnalyzeConversationTool) Spec() toolx.ToolSpec { + return toolx.GraphAnalyzeConversation +} + func (t *AnalyzeConversationTool) Name() string { return toolx.GraphAnalyzeConversation.Name } diff --git a/internal/ai/runtime/tools/create_ticket_confirm_tool.go b/internal/ai/runtime/tools/create_ticket_confirm_tool.go index ddc8b96..9d6fce1 100644 --- a/internal/ai/runtime/tools/create_ticket_confirm_tool.go +++ b/internal/ai/runtime/tools/create_ticket_confirm_tool.go @@ -24,6 +24,10 @@ func NewCreateTicketGraphTool() *CreateTicketGraphTool { return &CreateTicketGraphTool{} } +func (t *CreateTicketGraphTool) Spec() toolx.ToolSpec { + return toolx.GraphCreateTicketConfirm +} + func (t *CreateTicketGraphTool) Name() string { return toolx.GraphCreateTicketConfirm.Name } diff --git a/internal/ai/runtime/tools/handoff_graph_tool.go b/internal/ai/runtime/tools/handoff_graph_tool.go index 30986a1..2163636 100644 --- a/internal/ai/runtime/tools/handoff_graph_tool.go +++ b/internal/ai/runtime/tools/handoff_graph_tool.go @@ -24,6 +24,10 @@ func NewHandoffGraphTool() *HandoffGraphTool { return &HandoffGraphTool{} } +func (t *HandoffGraphTool) Spec() toolx.ToolSpec { + return toolx.GraphHandoffConversation +} + func (t *HandoffGraphTool) Name() string { return toolx.GraphHandoffConversation.Name } diff --git a/internal/ai/runtime/tools/prepare_ticket_draft_tool.go b/internal/ai/runtime/tools/prepare_ticket_draft_tool.go index 5dd327a..c0744ed 100644 --- a/internal/ai/runtime/tools/prepare_ticket_draft_tool.go +++ b/internal/ai/runtime/tools/prepare_ticket_draft_tool.go @@ -23,6 +23,10 @@ func NewPrepareTicketDraftTool() *PrepareTicketDraftTool { return &PrepareTicketDraftTool{} } +func (t *PrepareTicketDraftTool) Spec() toolx.ToolSpec { + return toolx.GraphPrepareTicketDraft +} + func (t *PrepareTicketDraftTool) Name() string { return toolx.GraphPrepareTicketDraft.Name } diff --git a/internal/ai/runtime/tools/tool_search_tool.go b/internal/ai/runtime/tools/tool_search_tool.go index 1df2ddf..cbb9b5e 100644 --- a/internal/ai/runtime/tools/tool_search_tool.go +++ b/internal/ai/runtime/tools/tool_search_tool.go @@ -26,6 +26,10 @@ func NewToolSearchTool() *ToolSearchTool { return &ToolSearchTool{} } +func (t *ToolSearchTool) Spec() toolx.ToolSpec { + return toolx.BuiltinToolSearch +} + func (t *ToolSearchTool) Name() string { return toolx.BuiltinToolSearch.Name } diff --git a/internal/ai/runtime/tools/triage_service_request_tool.go b/internal/ai/runtime/tools/triage_service_request_tool.go index 38424a1..dbdf40c 100644 --- a/internal/ai/runtime/tools/triage_service_request_tool.go +++ b/internal/ai/runtime/tools/triage_service_request_tool.go @@ -23,6 +23,10 @@ func NewTriageServiceRequestTool() *TriageServiceRequestTool { return &TriageServiceRequestTool{} } +func (t *TriageServiceRequestTool) Spec() toolx.ToolSpec { + return toolx.GraphTriageServiceRequest +} + func (t *TriageServiceRequestTool) Name() string { return toolx.GraphTriageServiceRequest.Name }