Skip to content

Commit 73e3c39

Browse files
committed
feat: add support for the SDK server
With this change, the gptscript SDK server will be fork/exec-ed instead of fork/exec-ing for every gptscript command. This produces enhancements with daemons in gptscript. This change also intentionally removes support for browser-based applications.
1 parent 525ae1d commit 73e3c39

File tree

7 files changed

+390
-1096
lines changed

7 files changed

+390
-1096
lines changed

README.md

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ running `npm install`.
1818

1919
## Usage
2020

21-
To use the module and run gptscripts, you need to first set the OPENAI_API_KEY environment variable to your OpenAI API
22-
key.
21+
To use the module and run gptscripts, you need to first set the `OPENAI_API_KEY` environment variable to your OpenAI API
22+
key. You can also set the `GPTSCRIPT_BIN` environment variable to change the execution of the gptscripts.
2323

2424
To ensure it is working properly, you can run the following command:
2525

@@ -31,11 +31,10 @@ You will see "Hello, World!" in the output of the command.
3131

3232
## Client
3333

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

4039
## Options
4140

@@ -45,7 +44,6 @@ None of the options is required, and the defaults will reduce the number of call
4544
- `disableCache`: Enable or disable caching, default (true)
4645
- `cacheDir`: Specify the cache directory
4746
- `quiet`: No output logging
48-
- `chdir`: Change current working directory
4947
- `subTool`: Use tool of this name, not the first tool
5048
- `workspace`: Directory to use for the workspace, if specified it will not be deleted on exit
5149

@@ -61,9 +59,10 @@ Lists all the available built-in tools.
6159
const gptscript = require('@gptscript-ai/gptscript');
6260

6361
async function listTools() {
64-
const client = new gptscript.Client();
62+
const client = gptscript.Client.Instance;
6563
const tools = await client.listTools();
6664
console.log(tools);
65+
client.close()
6766
}
6867
```
6968

@@ -79,10 +78,12 @@ const gptscript = require('@gptscript-ai/gptscript');
7978
async function listModels() {
8079
let models = [];
8180
try {
82-
const client = new gptscript.Client();
81+
const client = gptscript.Client.Instance;
8382
models = await client.listModels();
8483
} catch (error) {
8584
console.error(error);
85+
} finally {
86+
client.close()
8687
}
8788
}
8889
```
@@ -98,10 +99,12 @@ const gptscript = require('@gptscript-ai/gptscript');
9899

99100
async function version() {
100101
try {
101-
const client = new gptscript.Client();
102+
const client = gptscript.Client.Instance;
102103
console.log(await client.version());
103104
} catch (error) {
104105
console.error(error);
106+
} finally {
107+
client.close()
105108
}
106109
}
107110
```
@@ -119,11 +122,13 @@ const t = {
119122
};
120123

121124
try {
122-
const client = new gptscript.Client();
125+
const client = gptscript.Client.Instance;
123126
const run = client.evaluate(t);
124127
console.log(await run.text());
125128
} catch (error) {
126129
console.error(error);
130+
} finally {
131+
client.close();
127132
}
128133
```
129134

@@ -141,11 +146,13 @@ const opts = {
141146

142147
async function execFile() {
143148
try {
144-
const client = new gptscript.Client();
149+
const client = gptscript.Client.Instance;
145150
const run = client.run('./hello.gpt', opts);
146151
console.log(await run.text());
147152
} catch (e) {
148153
console.error(e);
154+
} finally {
155+
client.close();
149156
}
150157
}
151158
```
@@ -179,7 +186,7 @@ const opts = {
179186

180187
async function streamExecFileWithEvents() {
181188
try {
182-
const client = new gptscript.Client();
189+
const client = gptscript.Client.Instance;
183190
const run = client.run('./test.gpt', opts);
184191

185192
run.on(gptscript.RunEventType.Event, data => {
@@ -189,6 +196,8 @@ async function streamExecFileWithEvents() {
189196
await run.text();
190197
} catch (e) {
191198
console.error(e);
199+
} finally {
200+
client.close();
192201
}
193202
}
194203
```
@@ -218,7 +227,7 @@ const t = {
218227
};
219228

220229
async function streamExecFileWithEvents() {
221-
const client = new gptscript.Client();
230+
const client = gptscript.Client.Instance;
222231
let run = client.evaluate(t, opts);
223232
try {
224233
// Wait for the initial run to complete.
@@ -236,6 +245,8 @@ async function streamExecFileWithEvents() {
236245
}
237246
} catch (e) {
238247
console.error(e);
248+
} finally {
249+
client.close();
239250
}
240251

241252

babel.test.cjs

Lines changed: 0 additions & 18 deletions
This file was deleted.

0 commit comments

Comments
 (0)