77 "io"
88 "net/http"
99 "os"
10+ "sort"
1011 "strings"
1112
1213 "github.com/acorn-io/gptscript/pkg/types"
@@ -17,133 +18,158 @@ var Tools = map[string]types.Tool{
1718 Description : "Reads the contents of a file" ,
1819 Arguments : types .ObjectSchema (
1920 "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 ,
3622 },
3723 "sys.write" : {
3824 Description : "Write the contents to a file" ,
3925 Arguments : types .ObjectSchema (
4026 "filename" , "The name of the file to write to" ,
4127 "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 ,
5729 },
5830 "sys.http.get" : {
5931 Description : "Download the contents of a http or https URL" ,
6032 Arguments : types .ObjectSchema (
6133 "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 ,
8735 },
8836 "sys.abort" : {
8937 Description : "Aborts execution" ,
9038 Arguments : types .ObjectSchema (
9139 "message" , "The description of the error or unexpected result that caused abort to be called" ,
9240 ),
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 ,
10242 },
10343 "sys.http.post" : {
10444 Description : "Write contents to a http or https URL using the POST method" ,
10545 Arguments : types .ObjectSchema (
10646 "url" , "The URL to POST to" ,
10747 "content" , "The content to POST" ,
10848 "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 ,
14050 },
14151}
14252
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+
14368func Builtin (name string ) (types.Tool , bool ) {
14469 t , ok := Tools [name ]
14570 t .Name = name
14671 t .ID = name
14772 t .Instructions = "#!" + name
14873 return t , ok
14974}
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