Files
DivaCode/internal/tools/registry.go

63 lines
1.2 KiB
Go

package tools
import (
"context"
"sync"
)
type ToolFunc func(ctx context.Context, args string) (string, error)
type Tool struct {
Name string `json:"name"`
Description string `json:"description"`
Parameters []Param `json:"parameters"`
Execute ToolFunc `json:"-"`
}
type Param struct {
Name string `json:"name"`
Type string `json:"type"`
Required bool `json:"required"`
Description string `json:"description"`
}
type Registry struct {
mu sync.Mutex
tools map[string]*Tool
}
func NewRegistry() *Registry {
return &Registry{tools: make(map[string]*Tool)}
}
func (r *Registry) Register(t *Tool) {
r.mu.Lock()
defer r.mu.Unlock()
r.tools[t.Name] = t
}
func (r *Registry) Get(name string) (*Tool, bool) {
r.mu.Lock()
defer r.mu.Unlock()
t, ok := r.tools[name]
return t, ok
}
func (r *Registry) List() []*Tool {
r.mu.Lock()
defer r.mu.Unlock()
out := make([]*Tool, 0, len(r.tools))
for _, t := range r.tools {
out = append(out, t)
}
return out
}
func (r *Registry) Execute(ctx context.Context, name, args string) (string, error) {
t, ok := r.Get(name)
if !ok {
return "", nil
}
return t.Execute(ctx, args)
}