@@ -2,11 +2,12 @@ package builtin
2
2
3
3
import (
4
4
"context"
5
- "encoding/base64"
6
5
"encoding/json"
7
6
"fmt"
7
+ "io"
8
+ "net/http"
8
9
"os"
9
- "unicode/utf8 "
10
+ "strings "
10
11
11
12
"github.com/acorn-io/gptscript/pkg/types"
12
13
)
@@ -30,10 +31,7 @@ var Tools = map[string]types.Tool{
30
31
return "" , err
31
32
}
32
33
33
- if utf8 .Valid (data ) {
34
- return string (data ), nil
35
- }
36
- return base64 .StdEncoding .EncodeToString (data ), nil
34
+ return string (data ), nil
37
35
},
38
36
},
39
37
"sys.write" : {
@@ -57,6 +55,74 @@ var Tools = map[string]types.Tool{
57
55
return "" , os .WriteFile (params .Filename , data , 0644 )
58
56
},
59
57
},
58
+ "sys.http.get" : {
59
+ Description : "Download the contents of a http or https URL" ,
60
+ Arguments : types .ObjectSchema (
61
+ "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
+ },
87
+ },
88
+ "sys.http.post" : {
89
+ Description : "Write contents to a http or https URL using the POST method" ,
90
+ Arguments : types .ObjectSchema (
91
+ "url" , "The URL to POST to" ,
92
+ "content" , "The content to POST" ,
93
+ "contentType" , "The \" content type\" of the content such as application/json or text/plain" ),
94
+ BuiltinFunc : func (ctx context.Context , env []string , input string ) (string , error ) {
95
+ var params struct {
96
+ URL string `json:"url,omitempty"`
97
+ Content string `json:"content,omitempty"`
98
+ ContentType string `json:"contentType,omitempty"`
99
+ }
100
+ if err := json .Unmarshal ([]byte (input ), & params ); err != nil {
101
+ return "" , err
102
+ }
103
+
104
+ req , err := http .NewRequestWithContext (ctx , http .MethodPost , params .URL , strings .NewReader (params .Content ))
105
+ if err != nil {
106
+ return "" , err
107
+ }
108
+ if params .ContentType != "" {
109
+ req .Header .Set ("Content-Type" , params .ContentType )
110
+ }
111
+
112
+ resp , err := http .DefaultClient .Do (req )
113
+ if err != nil {
114
+ return "" , err
115
+ }
116
+ defer resp .Body .Close ()
117
+
118
+ _ , _ = io .ReadAll (resp .Body )
119
+ if resp .StatusCode > 399 {
120
+ return "" , fmt .Errorf ("failed to post %s: %s" , params .URL , resp .Status )
121
+ }
122
+
123
+ return fmt .Sprintf ("Wrote %d to %s" , len ([]byte (params .Content )), params .URL ), nil
124
+ },
125
+ },
60
126
}
61
127
62
128
func Builtin (name string ) (types.Tool , bool ) {
0 commit comments