Skip to content

Commit 734bdea

Browse files
committed
feat: add a new SDK server
This server will be used in the SDKs for running gptscripts. Signed-off-by: Donnie Adams <[email protected]>
1 parent c2edee9 commit 734bdea

File tree

25 files changed

+1175
-272
lines changed

25 files changed

+1175
-272
lines changed

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,14 @@ require (
1414
github.com/fatih/color v1.16.0
1515
github.com/getkin/kin-openapi v0.123.0
1616
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510
17+
github.com/google/uuid v1.6.0
1718
github.com/gptscript-ai/chat-completion-client v0.0.0-20240515050533-bdef9f2226a9
1819
github.com/hexops/autogold/v2 v2.2.1
1920
github.com/hexops/valast v1.4.4
2021
github.com/jaytaylor/html2text v0.0.0-20230321000545-74c2419ad056
2122
github.com/mholt/archiver/v4 v4.0.0-alpha.8
2223
github.com/olahol/melody v1.1.4
23-
github.com/rs/cors v1.10.1
24+
github.com/rs/cors v1.11.0
2425
github.com/samber/lo v1.38.1
2526
github.com/sirupsen/logrus v1.9.3
2627
github.com/spf13/cobra v1.8.0

go.sum

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ github.com/google/pprof v0.0.0-20200212024743-f11f1df84d12/go.mod h1:ZgVRPoUq/hf
119119
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
120120
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaUGG7oYTSPP8MxqL4YI3kZKwcP4=
121121
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3pAXIBCelhxNneeOaAeabZDe5s4K6zSpQ=
122+
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
123+
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
122124
github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg=
123125
github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=
124126
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
@@ -219,8 +221,8 @@ github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/f
219221
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
220222
github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8=
221223
github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4=
222-
github.com/rs/cors v1.10.1 h1:L0uuZVXIKlI1SShY2nhFfo44TYvDPQ1w4oFkUJNfhyo=
223-
github.com/rs/cors v1.10.1/go.mod h1:XyqrcTp5zjWr1wsJ8PIRZssZ8b/WMcMf71DJnit4EMU=
224+
github.com/rs/cors v1.11.0 h1:0B9GE/r9Bc2UxRMMtymBkHTenPkHDv0CW4Y98GBY+po=
225+
github.com/rs/cors v1.11.0/go.mod h1:XyqrcTp5zjWr1wsJ8PIRZssZ8b/WMcMf71DJnit4EMU=
224226
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
225227
github.com/rwcarlsen/goexif v0.0.0-20190401172101-9e8deecbddbd/go.mod h1:hPqNNc0+uJM6H+SuU8sEs5K5IQeKccPqeSjfgcKGgPk=
226228
github.com/samber/lo v1.38.1 h1:j2XEAqXKb09Am4ebOg31SpvzUTTs6EN3VfgeLUhPdXM=

pkg/builtin/builtin.go

Lines changed: 139 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -35,161 +35,197 @@ var SafeTools = map[string]struct{}{
3535

3636
var tools = map[string]types.Tool{
3737
"sys.time.now": {
38-
Parameters: types.Parameters{
39-
Description: "Returns the current date and time in RFC3339 format",
38+
ToolDef: types.ToolDef{
39+
Parameters: types.Parameters{
40+
Description: "Returns the current date and time in RFC3339 format",
41+
},
42+
BuiltinFunc: SysTimeNow,
4043
},
41-
BuiltinFunc: SysTimeNow,
4244
},
4345
"sys.ls": {
44-
Parameters: types.Parameters{
45-
Description: "Lists the contents of a directory",
46-
Arguments: types.ObjectSchema(
47-
"dir", "The directory to list"),
46+
ToolDef: types.ToolDef{
47+
Parameters: types.Parameters{
48+
Description: "Lists the contents of a directory",
49+
Arguments: types.ObjectSchema(
50+
"dir", "The directory to list"),
51+
},
52+
BuiltinFunc: SysLs,
4853
},
49-
BuiltinFunc: SysLs,
5054
},
5155
"sys.read": {
52-
Parameters: types.Parameters{
53-
Description: "Reads the contents of a file",
54-
Arguments: types.ObjectSchema(
55-
"filename", "The name of the file to read"),
56+
ToolDef: types.ToolDef{
57+
Parameters: types.Parameters{
58+
Description: "Reads the contents of a file",
59+
Arguments: types.ObjectSchema(
60+
"filename", "The name of the file to read"),
61+
},
62+
BuiltinFunc: SysRead,
5663
},
57-
BuiltinFunc: SysRead,
5864
},
5965
"sys.write": {
60-
Parameters: types.Parameters{
61-
Description: "Write the contents to a file",
62-
Arguments: types.ObjectSchema(
63-
"filename", "The name of the file to write to",
64-
"content", "The content to write"),
66+
ToolDef: types.ToolDef{
67+
Parameters: types.Parameters{
68+
Description: "Write the contents to a file",
69+
Arguments: types.ObjectSchema(
70+
"filename", "The name of the file to write to",
71+
"content", "The content to write"),
72+
},
73+
BuiltinFunc: SysWrite,
6574
},
66-
BuiltinFunc: SysWrite,
6775
},
6876
"sys.append": {
69-
Parameters: types.Parameters{
70-
Description: "Appends the contents to a file",
71-
Arguments: types.ObjectSchema(
72-
"filename", "The name of the file to append to",
73-
"content", "The content to append"),
77+
ToolDef: types.ToolDef{
78+
Parameters: types.Parameters{
79+
Description: "Appends the contents to a file",
80+
Arguments: types.ObjectSchema(
81+
"filename", "The name of the file to append to",
82+
"content", "The content to append"),
83+
},
84+
BuiltinFunc: SysAppend,
7485
},
75-
BuiltinFunc: SysAppend,
7686
},
7787
"sys.http.get": {
78-
Parameters: types.Parameters{
79-
Description: "Download the contents of a http or https URL",
80-
Arguments: types.ObjectSchema(
81-
"url", "The URL to download"),
88+
ToolDef: types.ToolDef{
89+
Parameters: types.Parameters{
90+
Description: "Download the contents of a http or https URL",
91+
Arguments: types.ObjectSchema(
92+
"url", "The URL to download"),
93+
},
94+
BuiltinFunc: SysHTTPGet,
8295
},
83-
BuiltinFunc: SysHTTPGet,
8496
},
8597
"sys.http.html2text": {
86-
Parameters: types.Parameters{
87-
Description: "Download the contents of a http or https URL returning the content as rendered text converted from HTML",
88-
Arguments: types.ObjectSchema(
89-
"url", "The URL to download"),
98+
ToolDef: types.ToolDef{
99+
Parameters: types.Parameters{
100+
Description: "Download the contents of a http or https URL returning the content as rendered text converted from HTML",
101+
Arguments: types.ObjectSchema(
102+
"url", "The URL to download"),
103+
},
104+
BuiltinFunc: SysHTTPHtml2Text,
90105
},
91-
BuiltinFunc: SysHTTPHtml2Text,
92106
},
93107
"sys.abort": {
94-
Parameters: types.Parameters{
95-
Description: "Aborts execution",
96-
Arguments: types.ObjectSchema(
97-
"message", "The description of the error or unexpected result that caused abort to be called",
98-
),
108+
ToolDef: types.ToolDef{
109+
Parameters: types.Parameters{
110+
Description: "Aborts execution",
111+
Arguments: types.ObjectSchema(
112+
"message", "The description of the error or unexpected result that caused abort to be called",
113+
),
114+
},
115+
BuiltinFunc: SysAbort,
99116
},
100-
BuiltinFunc: SysAbort,
101117
},
102118
"sys.chat.finish": {
103-
Parameters: types.Parameters{
104-
Description: "Concludes the conversation. This can not be used to ask a question.",
105-
Arguments: types.ObjectSchema(
106-
"return", "The instructed value to return or a summary of the dialog if no value is instructed",
107-
),
119+
ToolDef: types.ToolDef{
120+
Parameters: types.Parameters{
121+
Description: "Concludes the conversation. This can not be used to ask a question.",
122+
Arguments: types.ObjectSchema(
123+
"return", "The instructed value to return or a summary of the dialog if no value is instructed",
124+
),
125+
},
126+
BuiltinFunc: SysChatFinish,
108127
},
109-
BuiltinFunc: SysChatFinish,
110128
},
111129
"sys.http.post": {
112-
Parameters: types.Parameters{
113-
Description: "Write contents to a http or https URL using the POST method",
114-
Arguments: types.ObjectSchema(
115-
"url", "The URL to POST to",
116-
"content", "The content to POST",
117-
"contentType", "The \"content type\" of the content such as application/json or text/plain"),
130+
ToolDef: types.ToolDef{
131+
Parameters: types.Parameters{
132+
Description: "Write contents to a http or https URL using the POST method",
133+
Arguments: types.ObjectSchema(
134+
"url", "The URL to POST to",
135+
"content", "The content to POST",
136+
"contentType", "The \"content type\" of the content such as application/json or text/plain"),
137+
},
138+
BuiltinFunc: SysHTTPPost,
118139
},
119-
BuiltinFunc: SysHTTPPost,
120140
},
121141
"sys.find": {
122-
Parameters: types.Parameters{
123-
Description: "Traverse a directory looking for files that match a pattern in the style of the unix find command",
124-
Arguments: types.ObjectSchema(
125-
"pattern", "The file pattern to look for. The pattern is a traditional unix glob format with * matching any character and ? matching a single character",
126-
"directory", "The directory to search in. The current directory \".\" will be used as the default if no argument is passed",
127-
),
142+
ToolDef: types.ToolDef{
143+
Parameters: types.Parameters{
144+
Description: "Traverse a directory looking for files that match a pattern in the style of the unix find command",
145+
Arguments: types.ObjectSchema(
146+
"pattern", "The file pattern to look for. The pattern is a traditional unix glob format with * matching any character and ? matching a single character",
147+
"directory", "The directory to search in. The current directory \".\" will be used as the default if no argument is passed",
148+
),
149+
},
150+
BuiltinFunc: SysFind,
128151
},
129-
BuiltinFunc: SysFind,
130152
},
131153
"sys.exec": {
132-
Parameters: types.Parameters{
133-
Description: "Execute a command and get the output of the command",
134-
Arguments: types.ObjectSchema(
135-
"command", "The command to run including all applicable arguments",
136-
"directory", "The directory to use as the current working directory of the command. The current directory \".\" will be used if no argument is passed",
137-
),
154+
ToolDef: types.ToolDef{
155+
Parameters: types.Parameters{
156+
Description: "Execute a command and get the output of the command",
157+
Arguments: types.ObjectSchema(
158+
"command", "The command to run including all applicable arguments",
159+
"directory", "The directory to use as the current working directory of the command. The current directory \".\" will be used if no argument is passed",
160+
),
161+
},
162+
BuiltinFunc: SysExec,
138163
},
139-
BuiltinFunc: SysExec,
140164
},
141165
"sys.getenv": {
142-
Parameters: types.Parameters{
143-
Description: "Gets the value of an OS environment variable",
144-
Arguments: types.ObjectSchema(
145-
"name", "The environment variable name to lookup"),
166+
ToolDef: types.ToolDef{
167+
Parameters: types.Parameters{
168+
Description: "Gets the value of an OS environment variable",
169+
Arguments: types.ObjectSchema(
170+
"name", "The environment variable name to lookup"),
171+
},
172+
BuiltinFunc: SysGetenv,
146173
},
147-
BuiltinFunc: SysGetenv,
148174
},
149175
"sys.download": {
150-
Parameters: types.Parameters{
151-
Description: "Downloads a URL, saving the contents to disk at a given location",
152-
Arguments: types.ObjectSchema(
153-
"url", "The URL to download, either http or https.",
154-
"location", "(optional) The on disk location to store the file. If no location is specified a temp location will be used. If the target file already exists it will fail unless override is set to true.",
155-
"override", "If true and a file at the location exists, the file will be overwritten, otherwise fail. Default is false"),
176+
ToolDef: types.ToolDef{
177+
Parameters: types.Parameters{
178+
Description: "Downloads a URL, saving the contents to disk at a given location",
179+
Arguments: types.ObjectSchema(
180+
"url", "The URL to download, either http or https.",
181+
"location", "(optional) The on disk location to store the file. If no location is specified a temp location will be used. If the target file already exists it will fail unless override is set to true.",
182+
"override", "If true and a file at the location exists, the file will be overwritten, otherwise fail. Default is false"),
183+
},
184+
BuiltinFunc: SysDownload,
156185
},
157-
BuiltinFunc: SysDownload,
158186
},
159187
"sys.remove": {
160-
Parameters: types.Parameters{
161-
Description: "Removes the specified files",
162-
Arguments: types.ObjectSchema(
163-
"location", "The file to remove"),
188+
ToolDef: types.ToolDef{
189+
Parameters: types.Parameters{
190+
Description: "Removes the specified files",
191+
Arguments: types.ObjectSchema(
192+
"location", "The file to remove"),
193+
},
194+
BuiltinFunc: SysRemove,
164195
},
165-
BuiltinFunc: SysRemove,
166196
},
167197
"sys.stat": {
168-
Parameters: types.Parameters{
169-
Description: "Gets size, modfied time, and mode of the specified file",
170-
Arguments: types.ObjectSchema(
171-
"filepath", "The complete path and filename of the file",
172-
),
198+
ToolDef: types.ToolDef{
199+
Parameters: types.Parameters{
200+
Description: "Gets size, modfied time, and mode of the specified file",
201+
Arguments: types.ObjectSchema(
202+
"filepath", "The complete path and filename of the file",
203+
),
204+
},
205+
BuiltinFunc: SysStat,
173206
},
174-
BuiltinFunc: SysStat,
175207
},
176208
"sys.prompt": {
177-
Parameters: types.Parameters{
178-
Description: "Prompts the user for input",
179-
Arguments: types.ObjectSchema(
180-
"message", "The message to display to the user",
181-
"fields", "A comma-separated list of fields to prompt for",
182-
"sensitive", "(true or false) Whether the input should be hidden",
183-
),
209+
ToolDef: types.ToolDef{
210+
Parameters: types.Parameters{
211+
Description: "Prompts the user for input",
212+
Arguments: types.ObjectSchema(
213+
"message", "The message to display to the user",
214+
"fields", "A comma-separated list of fields to prompt for",
215+
"sensitive", "(true or false) Whether the input should be hidden",
216+
),
217+
},
218+
BuiltinFunc: SysPrompt,
184219
},
185-
BuiltinFunc: SysPrompt,
186220
},
187221
"sys.chat.history": {
188-
Parameters: types.Parameters{
189-
Description: "Retrieves the previous chat dialog",
190-
Arguments: types.ObjectSchema(),
222+
ToolDef: types.ToolDef{
223+
Parameters: types.Parameters{
224+
Description: "Retrieves the previous chat dialog",
225+
Arguments: types.ObjectSchema(),
226+
},
227+
BuiltinFunc: SysChatHistory,
191228
},
192-
BuiltinFunc: SysChatHistory,
193229
},
194230
}
195231

pkg/cli/clicky.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package cli
2+
3+
import (
4+
"github.com/gptscript-ai/gptscript/pkg/serves"
5+
"github.com/spf13/cobra"
6+
)
7+
8+
type Clicky struct {
9+
*GPTScript
10+
}
11+
12+
func (c *Clicky) Customize(cmd *cobra.Command) {
13+
cmd.Args = cobra.NoArgs
14+
cmd.Hidden = true
15+
}
16+
17+
func (c *Clicky) Run(cmd *cobra.Command, _ []string) error {
18+
opts, err := c.NewGPTScriptOpts()
19+
if err != nil {
20+
return err
21+
}
22+
23+
return serves.Start(cmd.Context(), serves.Options{
24+
Options: opts,
25+
ListenAddress: c.ListenAddress,
26+
Debug: c.Debug,
27+
})
28+
}

pkg/cli/eval.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,18 @@ type Eval struct {
2828

2929
func (e *Eval) Run(cmd *cobra.Command, args []string) error {
3030
tool := types.Tool{
31-
Parameters: types.Parameters{
32-
Description: "inline script",
33-
Tools: e.Tools,
34-
MaxTokens: e.MaxTokens,
35-
ModelName: e.Model,
36-
JSONResponse: e.JSON,
37-
InternalPrompt: e.InternalPrompt,
38-
Chat: e.Chat,
31+
ToolDef: types.ToolDef{
32+
Parameters: types.Parameters{
33+
Description: "inline script",
34+
Tools: e.Tools,
35+
MaxTokens: e.MaxTokens,
36+
ModelName: e.Model,
37+
JSONResponse: e.JSON,
38+
InternalPrompt: e.InternalPrompt,
39+
Chat: e.Chat,
40+
},
41+
Instructions: strings.Join(args, " "),
3942
},
40-
Instructions: strings.Join(args, " "),
4143
}
4244

4345
if e.Temperature != "" {

0 commit comments

Comments
 (0)