package tools import ( "context" "fmt" "sync" "time" "github.com/sipeed/picoclaw/pkg/logger" ) type ToolRegistry struct { tools map[string]Tool mu sync.RWMutex } func NewToolRegistry() *ToolRegistry { return &ToolRegistry{ tools: make(map[string]Tool), } } func (r *ToolRegistry) Register(tool Tool) { r.mu.Lock() defer r.mu.Unlock() r.tools[tool.Name()] = tool } func (r *ToolRegistry) Get(name string) (Tool, bool) { r.mu.RLock() defer r.mu.RUnlock() tool, ok := r.tools[name] return tool, ok } func (r *ToolRegistry) Execute(ctx context.Context, name string, args map[string]interface{}) (string, error) { logger.InfoCF("tool", "Tool execution started", map[string]interface{}{ "tool": name, "args": args, }) tool, ok := r.Get(name) if !ok { logger.ErrorCF("tool", "Tool not found", map[string]interface{}{ "tool": name, }) return "", fmt.Errorf("tool '%s' not found", name) } start := time.Now() result, err := tool.Execute(ctx, args) duration := time.Since(start) if err != nil { logger.ErrorCF("tool", "Tool execution failed", map[string]interface{}{ "tool": name, "duration": duration.Milliseconds(), "error": err.Error(), }) } else { logger.InfoCF("tool", "Tool execution completed", map[string]interface{}{ "tool": name, "duration_ms": duration.Milliseconds(), "result_length": len(result), }) } return result, err } func (r *ToolRegistry) GetDefinitions() []map[string]interface{} { r.mu.RLock() defer r.mu.RUnlock() definitions := make([]map[string]interface{}, 0, len(r.tools)) for _, tool := range r.tools { definitions = append(definitions, ToolToSchema(tool)) } return definitions }