Files
zeekayandHanzo Dev 8405b1eb0d Declare operations on the group that serves them
zip's registration is a verb method on the group, so a route that is not a typed
operation says which method it answers with. The verb table that bound a mapping
string to a registrar holds the method itself now — every one of these is a raw
registration, since the handler is resolved by reflection from a controller
method name — so the table is a value rather than a set of closures.

Co-authored-by: Hanzo Dev <[email protected]>
2026-09-17 06:08:44 -07:00

114 lines
3.6 KiB
Go

// Copyright 2023-2025 Hanzo AI Inc. All Rights Reserved.
// Portions Copyright 2025 The OpenAgent Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package builtin_tool
import (
"context"
"encoding/json"
"github.com/ThinkInAIXYZ/go-mcp/protocol"
timetools "github.com/hanzoai/ai/agent/builtin_tool/time"
webtools "github.com/hanzoai/ai/agent/builtin_tool/web"
)
type BuiltinTool interface {
GetName() string
GetDescription() string
GetInputSchema() any
Execute(ctx context.Context, arguments map[string]any) (*protocol.CallToolResult, error)
}
type ToolRegistry struct {
tools map[string]BuiltinTool
}
func NewToolRegistry() *ToolRegistry {
registry := &ToolRegistry{
tools: make(map[string]BuiltinTool),
}
registry.RegisterTool(&timetools.CurrentTimeTool{}) // get current time
registry.RegisterTool(&timetools.LocalTimeToTimestampTool{}) // local time to timestamp
registry.RegisterTool(&timetools.TimestampToLocalTimeTool{}) // timestamp to local time
registry.RegisterTool(&timetools.TimezoneConversionTool{}) // timezone conversion
registry.RegisterTool(&timetools.WeekdayTool{}) // weekday calculator
// The web. Registered unconditionally: a tool is DECLARED here and its backend
// is installed by the host, so an agent's tool list does not change shape with
// deployment. A capability the host did not install answers that it is
// unavailable rather than returning nothing — an agent told "no results" would
// conclude the web is empty on the subject and answer from memory instead.
registry.RegisterTool(&webtools.SearchTool{}) // web_search
registry.RegisterTool(&webtools.FetchTool{}) // fetch_url (native crawl)
registry.RegisterTool(&webtools.ResearchTool{}) // deep_research
return registry
}
func (r *ToolRegistry) RegisterTool(tool BuiltinTool) {
r.tools[tool.GetName()] = tool
}
func (r *ToolRegistry) GetTool(name string) (BuiltinTool, bool) {
tool, exists := r.tools[name]
return tool, exists
}
func (r *ToolRegistry) GetAllTools() map[string]BuiltinTool {
return r.tools
}
func (r *ToolRegistry) GetToolsAsProtocolTools() []*protocol.Tool {
var tools []*protocol.Tool
for _, tool := range r.tools {
// InputSchema to protocol.InputSchema
schemaInterface := tool.GetInputSchema()
schemaBytes, err := json.Marshal(schemaInterface)
if err != nil {
continue // 跳过无法序列化的工具
}
var inputSchema protocol.InputSchema
if err := json.Unmarshal(schemaBytes, &inputSchema); err != nil {
continue // 跳过无法反序列化的工具
}
tools = append(tools, &protocol.Tool{
Name: tool.GetName(),
Description: tool.GetDescription(),
InputSchema: inputSchema,
})
}
return tools
}
func (r *ToolRegistry) ExecuteTool(ctx context.Context, name string, arguments map[string]any) (*protocol.CallToolResult, error) {
tool, exists := r.GetTool(name)
if !exists {
return &protocol.CallToolResult{
IsError: true,
Content: []protocol.Content{
&protocol.TextContent{
Type: "text",
Text: "Tool not found: " + name,
},
},
}, nil
}
return tool.Execute(ctx, arguments)
}