forked from gptscript-ai/gptscript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoolstring.go
88 lines (80 loc) · 2.58 KB
/
toolstring.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
package types
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"strings"
)
func ToDisplayText(tool Tool, input string) string {
interpreter := tool.GetInterpreter()
if interpreter == "" {
return ""
}
if strings.HasPrefix(interpreter, "sys.") {
data := map[string]string{}
_ = json.Unmarshal([]byte(input), &data)
out, err := ToSysDisplayString(interpreter, data)
if err != nil {
return fmt.Sprintf("Running %s", interpreter)
}
return out
}
if tool.Source.Repo != nil {
repo := tool.Source.Repo
root := strings.TrimPrefix(repo.Root, "https://")
root = strings.TrimSuffix(root, ".git")
name := repo.Name
if name == "tool.gpt" {
name = ""
}
return fmt.Sprintf("Running %s from %s", tool.Name, filepath.Join(root, repo.Path, name))
}
if tool.Source.Location != "" {
return fmt.Sprintf("Running %s from %s", tool.Name, tool.Source.Location)
}
return ""
}
func ToSysDisplayString(id string, args map[string]string) (string, error) {
switch id {
case "sys.append":
return fmt.Sprintf("Appending to file `%s`", args["filename"]), nil
case "sys.download":
if location := args["location"]; location != "" {
return fmt.Sprintf("Downloading `%s` to `%s`", args["url"], location), nil
} else {
return fmt.Sprintf("Downloading `%s` to workspace", args["url"]), nil
}
case "sys.exec":
return fmt.Sprintf("Running `%s`", args["command"]), nil
case "sys.find":
dir := args["directory"]
if dir == "" {
dir = "."
}
return fmt.Sprintf("Finding `%s` in `%s`", args["pattern"], dir), nil
case "sys.http.get":
return fmt.Sprintf("Downloading `%s`", args["url"]), nil
case "sys.http.post":
return fmt.Sprintf("Sending to `%s`", args["url"]), nil
case "sys.http.html2text":
return fmt.Sprintf("Downloading `%s`", args["url"]), nil
case "sys.ls":
return fmt.Sprintf("Listing `%s`", args["dir"]), nil
case "sys.read":
return fmt.Sprintf("Reading `%s`", args["filename"]), nil
case "sys.remove":
return fmt.Sprintf("Removing `%s`", args["location"]), nil
case "sys.write":
return fmt.Sprintf("Writing `%s`", args["filename"]), nil
case "sys.context", "sys.stat", "sys.getenv", "sys.abort", "sys.chat.current", "sys.chat.finish", "sys.chat.history", "sys.echo", "sys.prompt", "sys.time.now", "sys.model.provider.credential":
return "", nil
case "sys.openapi":
if os.Getenv("GPTSCRIPT_OPENAPI_REVAMP") == "true" && args["operation"] != "" {
return fmt.Sprintf("Running API operation `%s` with arguments %s", args["operation"], args["args"]), nil
}
fallthrough
default:
return "", fmt.Errorf("unknown tool for display string: %s", id)
}
}