-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtools.go
More file actions
95 lines (84 loc) · 2.7 KB
/
tools.go
File metadata and controls
95 lines (84 loc) · 2.7 KB
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
package tools
import (
"encoding/json"
"fmt"
"os"
filepath "path/filepath"
"strings"
"github.com/BurntSushi/toml"
"github.com/elsejj/gpt/internal/utils"
"github.com/goccy/go-yaml"
)
// Tool represents the configuration for a language model tool, when specified, will override global settings.
type Tool struct {
Model string `yaml:"model,omitempty" json:"model,omitempty" toml:"model,omitempty"`
Key string `yaml:"key,omitempty" json:"key,omitempty" toml:"key,omitempty"`
URL string `yaml:"url,omitempty" json:"url,omitempty" toml:"url,omitempty"`
ReasonEffort string `yaml:"reason,omitempty" json:"reason,omitempty" toml:"reason,omitempty"`
Temperature *float64 `yaml:"temperature,omitempty" json:"temperature,omitempty" toml:"temperature,omitempty"`
SystemPrompt string `yaml:"system,omitempty" json:"system,omitempty" toml:"system,omitempty"`
UserTemplate string `yaml:"user,omitempty" json:"user,omitempty" toml:"user,omitempty"`
Action string `yaml:"action,omitempty" json:"action,omitempty" toml:"action,omitempty"`
MCPs []string `yaml:"mcps,omitempty" json:"mcps,omitempty" toml:"mcps,omitempty"`
}
var parsers = map[string]func([]byte, *Tool) error{
".yaml": parseYAML,
".yml": parseYAML,
".json": parseJSON,
".toml": parseTOML,
}
func Load(name string) (Tool, error) {
var tool Tool
path, err := findFile(name)
if err != nil {
return tool, err
}
ext := strings.ToLower(filepath.Ext(path))
parser, ok := parsers[ext]
if !ok {
return tool, fmt.Errorf("%s is not a supported format", name)
}
data, err := os.ReadFile(path)
if err != nil {
return tool, err
}
err = parser(data, &tool)
return tool, err
}
func (tool *Tool) UserPrompt(user string) string {
if tool.UserTemplate == "" {
return user
}
if strings.Contains(tool.UserTemplate, "{{user}}") {
return strings.ReplaceAll(tool.UserTemplate, "{{user}}", user)
}
return tool.UserTemplate + "\n" + user
}
func findFile(name string) (string, error) {
tryFiles := []string{}
ext := strings.ToLower(filepath.Ext(name))
if ext == "" {
for k := range parsers {
tryFiles = append(tryFiles, name+k)
tryFiles = append(tryFiles, utils.ConfigPath("tools", name+k))
}
} else {
tryFiles = append(tryFiles, name)
tryFiles = append(tryFiles, utils.ConfigPath("tools", name))
}
for _, f := range tryFiles {
if _, err := os.Stat(f); err == nil {
return f, nil
}
}
return "", fmt.Errorf("tool config file %s not found", name)
}
func parseJSON(data []byte, tool *Tool) error {
return json.Unmarshal(data, tool)
}
func parseYAML(data []byte, tool *Tool) error {
return yaml.Unmarshal(data, tool)
}
func parseTOML(data []byte, tool *Tool) error {
return toml.Unmarshal(data, tool)
}