7
7
"io"
8
8
"net/http"
9
9
"os"
10
+ "sort"
10
11
"strings"
11
12
12
13
"github.com/acorn-io/gptscript/pkg/types"
@@ -17,133 +18,158 @@ var Tools = map[string]types.Tool{
17
18
Description : "Reads the contents of a file" ,
18
19
Arguments : types .ObjectSchema (
19
20
"filename" , "The name of the file to read" ),
20
- BuiltinFunc : func (ctx context.Context , env []string , input string ) (string , error ) {
21
- var params struct {
22
- Filename string `json:"filename,omitempty"`
23
- }
24
- if err := json .Unmarshal ([]byte (input ), & params ); err != nil {
25
- return "" , err
26
- }
27
-
28
- log .Debugf ("Reading file %s" , params .Filename )
29
- data , err := os .ReadFile (params .Filename )
30
- if err != nil {
31
- return "" , err
32
- }
33
-
34
- return string (data ), nil
35
- },
21
+ BuiltinFunc : SysRead ,
36
22
},
37
23
"sys.write" : {
38
24
Description : "Write the contents to a file" ,
39
25
Arguments : types .ObjectSchema (
40
26
"filename" , "The name of the file to write to" ,
41
27
"content" , "The content to write" ),
42
- BuiltinFunc : func (ctx context.Context , env []string , input string ) (string , error ) {
43
- var params struct {
44
- Filename string `json:"filename,omitempty"`
45
- Content string `json:"content,omitempty"`
46
- }
47
- if err := json .Unmarshal ([]byte (input ), & params ); err != nil {
48
- return "" , err
49
- }
50
-
51
- data := []byte (params .Content )
52
- msg := fmt .Sprintf ("Wrote %d bytes to file %s" , len (data ), params .Filename )
53
- log .Debugf (msg )
54
-
55
- return "" , os .WriteFile (params .Filename , data , 0644 )
56
- },
28
+ BuiltinFunc : SysWrite ,
57
29
},
58
30
"sys.http.get" : {
59
31
Description : "Download the contents of a http or https URL" ,
60
32
Arguments : types .ObjectSchema (
61
33
"url" , "The URL to download" ),
62
- BuiltinFunc : func (ctx context.Context , env []string , input string ) (string , error ) {
63
- var params struct {
64
- URL string `json:"url,omitempty"`
65
- }
66
- if err := json .Unmarshal ([]byte (input ), & params ); err != nil {
67
- return "" , err
68
- }
69
-
70
- log .Debugf ("http get %s" , params .URL )
71
- resp , err := http .Get (params .URL )
72
- if err != nil {
73
- return "" , err
74
- }
75
- defer resp .Body .Close ()
76
- if resp .StatusCode != http .StatusOK {
77
- return "" , fmt .Errorf ("failed to download %s: %s" , params .URL , resp .Status )
78
- }
79
-
80
- data , err := io .ReadAll (resp .Body )
81
- if err != nil {
82
- return "" , err
83
- }
84
-
85
- return string (data ), nil
86
- },
34
+ BuiltinFunc : SysHTTPGet ,
87
35
},
88
36
"sys.abort" : {
89
37
Description : "Aborts execution" ,
90
38
Arguments : types .ObjectSchema (
91
39
"message" , "The description of the error or unexpected result that caused abort to be called" ,
92
40
),
93
- BuiltinFunc : func (ctx context.Context , env []string , input string ) (string , error ) {
94
- var params struct {
95
- Message string `json:"message,omitempty"`
96
- }
97
- if err := json .Unmarshal ([]byte (input ), & params ); err != nil {
98
- return "" , err
99
- }
100
- return "" , fmt .Errorf ("ABORT: %s" , params .Message )
101
- },
41
+ BuiltinFunc : SysAbort ,
102
42
},
103
43
"sys.http.post" : {
104
44
Description : "Write contents to a http or https URL using the POST method" ,
105
45
Arguments : types .ObjectSchema (
106
46
"url" , "The URL to POST to" ,
107
47
"content" , "The content to POST" ,
108
48
"contentType" , "The \" content type\" of the content such as application/json or text/plain" ),
109
- BuiltinFunc : func (ctx context.Context , env []string , input string ) (string , error ) {
110
- var params struct {
111
- URL string `json:"url,omitempty"`
112
- Content string `json:"content,omitempty"`
113
- ContentType string `json:"contentType,omitempty"`
114
- }
115
- if err := json .Unmarshal ([]byte (input ), & params ); err != nil {
116
- return "" , err
117
- }
118
-
119
- req , err := http .NewRequestWithContext (ctx , http .MethodPost , params .URL , strings .NewReader (params .Content ))
120
- if err != nil {
121
- return "" , err
122
- }
123
- if params .ContentType != "" {
124
- req .Header .Set ("Content-Type" , params .ContentType )
125
- }
126
-
127
- resp , err := http .DefaultClient .Do (req )
128
- if err != nil {
129
- return "" , err
130
- }
131
- defer resp .Body .Close ()
132
-
133
- _ , _ = io .ReadAll (resp .Body )
134
- if resp .StatusCode > 399 {
135
- return "" , fmt .Errorf ("failed to post %s: %s" , params .URL , resp .Status )
136
- }
137
-
138
- return fmt .Sprintf ("Wrote %d to %s" , len ([]byte (params .Content )), params .URL ), nil
139
- },
49
+ BuiltinFunc : SysHTTPPost ,
140
50
},
141
51
}
142
52
53
+ func ListTools () (result []types.Tool ) {
54
+ var keys []string
55
+ for k := range Tools {
56
+ keys = append (keys , k )
57
+ }
58
+
59
+ sort .Strings (keys )
60
+ for _ , key := range keys {
61
+ t , _ := Builtin (key )
62
+ result = append (result , t )
63
+ }
64
+
65
+ return
66
+ }
67
+
143
68
func Builtin (name string ) (types.Tool , bool ) {
144
69
t , ok := Tools [name ]
145
70
t .Name = name
146
71
t .ID = name
147
72
t .Instructions = "#!" + name
148
73
return t , ok
149
74
}
75
+
76
+ func SysRead (ctx context.Context , env []string , input string ) (string , error ) {
77
+ var params struct {
78
+ Filename string `json:"filename,omitempty"`
79
+ }
80
+ if err := json .Unmarshal ([]byte (input ), & params ); err != nil {
81
+ return "" , err
82
+ }
83
+
84
+ log .Debugf ("Reading file %s" , params .Filename )
85
+ data , err := os .ReadFile (params .Filename )
86
+ if err != nil {
87
+ return "" , err
88
+ }
89
+
90
+ return string (data ), nil
91
+ }
92
+
93
+ func SysWrite (ctx context.Context , env []string , input string ) (string , error ) {
94
+ var params struct {
95
+ Filename string `json:"filename,omitempty"`
96
+ Content string `json:"content,omitempty"`
97
+ }
98
+ if err := json .Unmarshal ([]byte (input ), & params ); err != nil {
99
+ return "" , err
100
+ }
101
+
102
+ data := []byte (params .Content )
103
+ msg := fmt .Sprintf ("Wrote %d bytes to file %s" , len (data ), params .Filename )
104
+ log .Debugf (msg )
105
+
106
+ return "" , os .WriteFile (params .Filename , data , 0644 )
107
+ }
108
+
109
+ func SysHTTPGet (ctx context.Context , env []string , input string ) (string , error ) {
110
+ var params struct {
111
+ URL string `json:"url,omitempty"`
112
+ }
113
+ if err := json .Unmarshal ([]byte (input ), & params ); err != nil {
114
+ return "" , err
115
+ }
116
+
117
+ log .Debugf ("http get %s" , params .URL )
118
+ resp , err := http .Get (params .URL )
119
+ if err != nil {
120
+ return "" , err
121
+ }
122
+ defer resp .Body .Close ()
123
+ if resp .StatusCode != http .StatusOK {
124
+ return "" , fmt .Errorf ("failed to download %s: %s" , params .URL , resp .Status )
125
+ }
126
+
127
+ data , err := io .ReadAll (resp .Body )
128
+ if err != nil {
129
+ return "" , err
130
+ }
131
+
132
+ return string (data ), nil
133
+ }
134
+
135
+ func SysHTTPPost (ctx context.Context , env []string , input string ) (string , error ) {
136
+ var params struct {
137
+ URL string `json:"url,omitempty"`
138
+ Content string `json:"content,omitempty"`
139
+ ContentType string `json:"contentType,omitempty"`
140
+ }
141
+ if err := json .Unmarshal ([]byte (input ), & params ); err != nil {
142
+ return "" , err
143
+ }
144
+
145
+ req , err := http .NewRequestWithContext (ctx , http .MethodPost , params .URL , strings .NewReader (params .Content ))
146
+ if err != nil {
147
+ return "" , err
148
+ }
149
+ if params .ContentType != "" {
150
+ req .Header .Set ("Content-Type" , params .ContentType )
151
+ }
152
+
153
+ resp , err := http .DefaultClient .Do (req )
154
+ if err != nil {
155
+ return "" , err
156
+ }
157
+ defer resp .Body .Close ()
158
+
159
+ _ , _ = io .ReadAll (resp .Body )
160
+ if resp .StatusCode > 399 {
161
+ return "" , fmt .Errorf ("failed to post %s: %s" , params .URL , resp .Status )
162
+ }
163
+
164
+ return fmt .Sprintf ("Wrote %d to %s" , len ([]byte (params .Content )), params .URL ), nil
165
+ }
166
+
167
+ func SysAbort (ctx context.Context , env []string , input string ) (string , error ) {
168
+ var params struct {
169
+ Message string `json:"message,omitempty"`
170
+ }
171
+ if err := json .Unmarshal ([]byte (input ), & params ); err != nil {
172
+ return "" , err
173
+ }
174
+ return "" , fmt .Errorf ("ABORT: %s" , params .Message )
175
+ }
0 commit comments