-
Notifications
You must be signed in to change notification settings - Fork 4
/
tools.go
188 lines (158 loc) · 4.61 KB
/
tools.go
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package main
import (
"encoding/json"
"fmt"
"log"
"os"
"os/exec"
"strings"
ai "github.com/sashabaranov/go-openai"
"github.com/sashabaranov/go-openai/jsonschema"
)
type SoulShackTool interface {
GetTool() (ai.Tool, error)
Execute(ctx ChatContextInterface, tool ai.ToolCall) (ai.ChatCompletionMessage, error)
}
type ToolRegistry struct {
Tools map[string]SoulShackTool
Definitions []ai.Tool
}
func NewToolRegistry(toolsDir string) (*ToolRegistry, error) {
toolRegistry := &ToolRegistry{
Tools: make(map[string]SoulShackTool),
}
log.Println("loading tools from:", toolsDir)
files, err := os.ReadDir(toolsDir)
if err != nil {
return nil, err
}
for _, file := range files {
log.Println("found:", file.Name())
if file.IsDir() {
continue
}
toolPath := toolsDir + "/" + file.Name()
shellTool := &ShellTool{
Command: toolPath,
}
// Load metadata (name, description, schema)
if err := shellTool.LoadMetadata(); err != nil {
log.Printf("failed to load metadata for tool %s: %v", toolPath, err)
continue
}
log.Println("registered tool:", shellTool.Name)
toolRegistry.Tools[shellTool.Name] = shellTool
}
return toolRegistry, nil
}
func (r *ToolRegistry) RegisterTool(name string, tool SoulShackTool) {
log.Println("registering tool:", name)
// validate the tool by calling GetTool
_, err := tool.GetTool()
if err != nil {
log.Printf("failed to validate tool %s: %v", name, err)
return
}
r.Tools[name] = tool
}
func (r *ToolRegistry) GetToolByName(name string) (SoulShackTool, error) {
tool, ok := r.Tools[name]
if !ok {
return nil, fmt.Errorf("tool not found in registry: %s", name)
}
return tool, nil
}
func (r *ToolRegistry) GetToolDefinitions() []ai.Tool {
if len(r.Definitions) > 0 {
return r.Definitions
}
for _, tool := range r.Tools {
definition, err := tool.GetTool()
if err != nil {
log.Printf("failed to get tool definition: %v", err)
continue
}
r.Definitions = append(r.Definitions, definition)
}
return r.Definitions
}
// generic tool that can be configured to execute binaries or scripts.
type ShellTool struct {
Command string
Name string
Description string
Properties jsonschema.Definition
}
// loads schema for a ShellTool.
//
// {
// "name": "get_current_date_with_format",
// "description": "provides the current time and date in the specified unix date command format",
// "type": "object",
// "properties": {
// "format": {
// "type": "string",
// "description": "The format for the date. use unix date command format (e.g., +%Y-%m-%d %H:%M:%S). always include the leading + sign."
// }
// },
// "required": ["format"],
// "additionalProperties": false
// }
//
func (s *ShellTool) LoadMetadata() error {
schemaOutput, err := s.runCommand("--schema")
if err != nil {
return fmt.Errorf("failed to get schema: %v", err)
}
err = json.Unmarshal([]byte(schemaOutput), &s)
if err != nil {
return fmt.Errorf("failed to unmarshal schema: %v", err)
}
// i probably don't understand the go-openai library parser
err = json.Unmarshal([]byte(schemaOutput), &s.Properties)
if err != nil {
return fmt.Errorf("failed to unmarshal schema: %v", err)
}
tool := ai.Tool{}
err = json.Unmarshal([]byte(schemaOutput), &tool)
if err != nil {
return fmt.Errorf("failed to unmarshal schema: %v", err)
}
return nil
}
func (s *ShellTool) runCommand(arg string) (string, error) {
cmd := exec.Command(s.Command, arg)
output, err := cmd.Output()
if err != nil {
return "", err
}
return strings.TrimSpace(string(output)), nil
}
func (s *ShellTool) GetTool() (ai.Tool, error) {
return ai.Tool{
Type: ai.ToolTypeFunction,
Function: &ai.FunctionDefinition{
Name: s.Name,
Description: s.Description,
Parameters: s.Properties,
Strict: true,
},
}, nil
}
func (s *ShellTool) Execute(ctx ChatContextInterface, tool ai.ToolCall) (ai.ChatCompletionMessage, error) {
log.Printf("shelltool cmd: %s", s.Command)
// arguments are passed as a JSON string, parse it
var args json.RawMessage
err := json.Unmarshal([]byte(tool.Function.Arguments), &args)
if err != nil {
return ai.ChatCompletionMessage{Role: ai.ChatMessageRoleTool, ToolCallID: tool.ID, Name: s.Name}, err
}
cmd := exec.Command(s.Command, "--execute", string(args))
output, err := cmd.CombinedOutput()
log.Println("shelltool usr:", cmd.ProcessState.UserTime())
log.Println("shelltool sys:", cmd.ProcessState.SystemTime())
log.Println("shelltool rc:", cmd.ProcessState.ExitCode())
out := strings.TrimSpace(string(output))
msg := ai.ChatCompletionMessage{ToolCallID: tool.ID, Name: s.Name, Role: ai.ChatMessageRoleTool, Content: out}
return msg, err
}