Skip to content

Commit ae0f262

Browse files
authored
Merge pull request #18 from thedadams/sdk-server
feat: add support for the new SDK server
2 parents 07a3b27 + ee5c87c commit ae0f262

12 files changed

+530
-370
lines changed

README.md

+105-16
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,7 @@ Additionally, you need the `gptscript` binary. You can install it on your system
1818

1919
## Client
2020

21-
There are currently a couple "global" options, and the client helps to manage those. A client without any options is
22-
likely what you want. However, here are the current global options:
23-
24-
- `gptscriptURL`: The URL (including `http(s)://) of an "SDK server" to use instead of the fork/exec model.
25-
- `gptscriptBin`: The path to a `gptscript` binary to use instead of the bundled one.
21+
The client allows the caller to run gptscript files, tools, and other operations (see below). There are currently no options for this client, so calling `NewClient()` is all you need. Although, the intention is that a single client is all you need for the life of your application, you should call `Close()` on the client when you are done.
2622

2723
## Options
2824

@@ -32,12 +28,12 @@ None of the options is required, and the defaults will reduce the number of call
3228
- `cache`: Enable or disable caching. Default (true).
3329
- `cacheDir`: Specify the cache directory.
3430
- `quiet`: No output logging
35-
- `chdir`: Change current working directory
3631
- `subTool`: Use tool of this name, not the first tool
3732
- `input`: Input arguments for the tool run
3833
- `workspace`: Directory to use for the workspace, if specified it will not be deleted on exit
3934
- `inlcudeEvents`: Whether to include the streaming of events. Default (false). Note that if this is true, you must stream the events. See below for details.
4035
- `chatState`: The chat state to continue, or null to start a new chat and return the state
36+
- `confirm`: Prompt before running potentially dangerous commands
4137

4238
## Functions
4339

@@ -57,7 +53,11 @@ import (
5753
)
5854

5955
func listTools(ctx context.Context) (string, error) {
60-
client := gptscript.NewClient(gptscript.ClientOpts{})
56+
client, err := gptscript.NewClient()
57+
if err != nil {
58+
return "", err
59+
}
60+
defer client.Close()
6161
return client.ListTools(ctx)
6262
}
6363
```
@@ -78,7 +78,11 @@ import (
7878
)
7979

8080
func listModels(ctx context.Context) ([]string, error) {
81-
client := gptscript.NewClient(gptscript.ClientOpts{})
81+
client, err := gptscript.NewClient()
82+
if err != nil {
83+
return nil, err
84+
}
85+
defer client.Close()
8286
return client.ListModels(ctx)
8387
}
8488
```
@@ -97,7 +101,12 @@ import (
97101
)
98102

99103
func parse(ctx context.Context, fileName string) ([]gptscript.Node, error) {
100-
client := gptscript.NewClient(gptscript.ClientOpts{})
104+
client, err := gptscript.NewClient()
105+
if err != nil {
106+
return nil, err
107+
}
108+
defer client.Close()
109+
101110
return client.Parse(ctx, fileName)
102111
}
103112
```
@@ -116,7 +125,12 @@ import (
116125
)
117126

118127
func parseTool(ctx context.Context, contents string) ([]gptscript.Node, error) {
119-
client := gptscript.NewClient(gptscript.ClientOpts{})
128+
client, err := gptscript.NewClient()
129+
if err != nil {
130+
return nil, err
131+
}
132+
defer client.Close()
133+
120134
return client.ParseTool(ctx, contents)
121135
}
122136
```
@@ -135,7 +149,12 @@ import (
135149
)
136150

137151
func parse(ctx context.Context, nodes []gptscript.Node) (string, error) {
138-
client := gptscript.NewClient(gptscript.ClientOpts{})
152+
client, err := gptscript.NewClient()
153+
if err != nil {
154+
return "", err
155+
}
156+
defer client.Close()
157+
139158
return client.Fmt(ctx, nodes)
140159
}
141160
```
@@ -158,8 +177,13 @@ func runTool(ctx context.Context) (string, error) {
158177
Instructions: "who was the president of the united states in 1928?",
159178
}
160179

161-
client := gptscript.NewClient(gptscript.ClientOpts{})
162-
run, err := client.Evaluate(ctx, gptscript.Opts{}, t)
180+
client, err := gptscript.NewClient()
181+
if err != nil {
182+
return "", err
183+
}
184+
defer client.Close()
185+
186+
run, err := client.Evaluate(ctx, gptscript.Options{}, t)
163187
if err != nil {
164188
return "", err
165189
}
@@ -182,12 +206,17 @@ import (
182206
)
183207

184208
func runFile(ctx context.Context) (string, error) {
185-
opts := gptscript.Opts{
209+
opts := gptscript.Options{
186210
DisableCache: &[]bool{true}[0],
187211
Input: "--input hello",
188212
}
189213

190-
client := gptscript.NewClient(gptscript.ClientOpts{})
214+
client, err := gptscript.NewClient()
215+
if err != nil {
216+
return "", err
217+
}
218+
defer client.Close()
219+
191220
run, err := client.Run(ctx, "./hello.gpt", opts)
192221
if err != nil {
193222
return "", err
@@ -217,7 +246,12 @@ func streamExecTool(ctx context.Context) error {
217246
Input: "--input world",
218247
}
219248

220-
client := gptscript.NewClient(gptscript.ClientOpts{})
249+
client, err := gptscript.NewClient()
250+
if err != nil {
251+
return "", err
252+
}
253+
defer client.Close()
254+
221255
run, err := client.Run(ctx, "./hello.gpt", opts)
222256
if err != nil {
223257
return err
@@ -233,6 +267,61 @@ func streamExecTool(ctx context.Context) error {
233267
}
234268
```
235269

270+
### Confirm
271+
272+
Using the `confirm: true` option allows a user to inspect potentially dangerous commands before they are run. The caller has the ability to allow or disallow their running. In order to do this, a caller should look for the `CallConfirm` event. This also means that `IncludeEvent` should be `true`.
273+
274+
```go
275+
package main
276+
277+
import (
278+
"context"
279+
280+
"github.com/gptscript-ai/go-gptscript"
281+
)
282+
283+
func runFileWithConfirm(ctx context.Context) (string, error) {
284+
opts := gptscript.Options{
285+
DisableCache: &[]bool{true}[0],
286+
Input: "--input hello",
287+
Confirm: true,
288+
IncludeEvents: true,
289+
}
290+
291+
client, err := gptscript.NewClient()
292+
if err != nil {
293+
return "", err
294+
}
295+
defer client.Close()
296+
297+
run, err := client.Run(ctx, "./hello.gpt", opts)
298+
if err != nil {
299+
return "", err
300+
}
301+
302+
for event := range run.Events() {
303+
if event.Type == gptscript.EventTypeCallConfirm {
304+
// event.Tool has the information on the command being run.
305+
// and event.Input will have the input to the command being run.
306+
307+
err = client.Confirm(ctx, gptscript.AuthResponse{
308+
ID: event.ID,
309+
Accept: true, // Or false if not allowed.
310+
Message: "", // A message explaining why the command is not allowed (ignored if allowed).
311+
})
312+
if err != nil {
313+
// Handle error
314+
}
315+
}
316+
317+
// Process event...
318+
}
319+
320+
return run.Text()
321+
}
322+
```
323+
324+
236325
## Types
237326

238327
### Tool Parameters

0 commit comments

Comments
 (0)