registry.go 2.29 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Package tools provides built-in tool implementations for the agent loop.
package tools

import (
	"fmt"
	"sort"

	"github.com/ollama/ollama/api"
)

// Tool defines the interface for agent tools.
type Tool interface {
	// Name returns the tool's unique identifier.
	Name() string
	// Description returns a human-readable description of what the tool does.
	Description() string
	// Schema returns the tool's parameter schema for the LLM.
	Schema() api.ToolFunction
	// Execute runs the tool with the given arguments.
	Execute(args map[string]any) (string, error)
}

// Registry manages available tools.
type Registry struct {
	tools map[string]Tool
}

// NewRegistry creates a new tool registry.
func NewRegistry() *Registry {
	return &Registry{
		tools: make(map[string]Tool),
	}
}

// Register adds a tool to the registry.
func (r *Registry) Register(tool Tool) {
	r.tools[tool.Name()] = tool
}

// Get retrieves a tool by name.
func (r *Registry) Get(name string) (Tool, bool) {
	tool, ok := r.tools[name]
	return tool, ok
}

// Tools returns all registered tools in Ollama API format, sorted by name.
func (r *Registry) Tools() api.Tools {
	// Get sorted names for deterministic ordering
	names := make([]string, 0, len(r.tools))
	for name := range r.tools {
		names = append(names, name)
	}
	sort.Strings(names)

	var tools api.Tools
	for _, name := range names {
		tool := r.tools[name]
		tools = append(tools, api.Tool{
			Type:     "function",
			Function: tool.Schema(),
		})
	}
	return tools
}

// Execute runs a tool call and returns the result.
func (r *Registry) Execute(call api.ToolCall) (string, error) {
	tool, ok := r.tools[call.Function.Name]
	if !ok {
		return "", fmt.Errorf("unknown tool: %s", call.Function.Name)
	}
	return tool.Execute(call.Function.Arguments.ToMap())
}

// Names returns the names of all registered tools, sorted alphabetically.
func (r *Registry) Names() []string {
	names := make([]string, 0, len(r.tools))
	for name := range r.tools {
		names = append(names, name)
	}
	sort.Strings(names)
	return names
}

// Count returns the number of registered tools.
func (r *Registry) Count() int {
	return len(r.tools)
}

// DefaultRegistry creates a registry with all built-in tools.
func DefaultRegistry() *Registry {
	r := NewRegistry()
	r.Register(&WebSearchTool{})
	r.Register(&BashTool{})
	return r
}