Skip to content

Commit 56c7164

Browse files
authored
Merge pull request #21 from thedadams/remote-sdk
feat: add remote SDK environment variable
2 parents fa1e07d + e12a763 commit 56c7164

File tree

5 files changed

+246
-48
lines changed

5 files changed

+246
-48
lines changed

README.md

+19-19
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ you will see "Hello, World!" in the output of the command.
3131
These are optional options that can be passed to the various `exec` functions.
3232
None of the options is required, and the defaults will reduce the number of calls made to the Model API.
3333

34-
- `cache`: Enable or disable caching. Default (true).
34+
- `disableCache`: Enable or disable caching. Default (true).
3535
- `cacheDir`: Specify the cache directory.
3636
- `quiet`: No output logging
3737
- `chdir`: Change current working directory
@@ -99,7 +99,7 @@ Executes a GPT script file with optional input and arguments. The script is rela
9999
const gptscript = require('@gptscript-ai/gptscript');
100100

101101
const opts = {
102-
cache: false,
102+
disableCache: false,
103103
};
104104

105105
async function execFile() {
@@ -120,7 +120,7 @@ Executes a gptscript with optional input and arguments, and returns the output s
120120
const gptscript = require('@gptscript-ai/gptscript');
121121

122122
const opts = {
123-
cache: false,
123+
disableCache: false,
124124
};
125125

126126
const t = new gptscript.Tool({
@@ -154,7 +154,7 @@ Executes a gptscript with optional input and arguments, and returns the output a
154154
const gptscript = require('@gptscript-ai/gptscript');
155155

156156
const opts = {
157-
cache: false,
157+
disableCache: false,
158158
};
159159

160160
const t = new gptscript.Tool({
@@ -192,7 +192,7 @@ The script is relative to the callers source directory.
192192
const gptscript = require('@gptscript-ai/gptscript');
193193

194194
const opts = {
195-
cache: false,
195+
disableCache: false,
196196
};
197197

198198
async function streamExecFile() {
@@ -222,7 +222,7 @@ The script is relative to the callers source directory.
222222
const gptscript = require('@gptscript-ai/gptscript');
223223

224224
const opts = {
225-
cache: false,
225+
disableCache: false,
226226
};
227227

228228
async function streamExecFileWithEvents() {
@@ -252,19 +252,19 @@ async function streamExecFileWithEvents() {
252252

253253
### Tool Parameters
254254

255-
| Argument | Type | Default | Description |
256-
|-------------------|----------------|-------------|-----------------------------------------------------------------------------------------------|
257-
| name | string | `""` | The name of the tool. Optional only on the first tool if there are multiple tools defined. |
258-
| description | string | `""` | A brief description of what the tool does, this is important for explaining to the LLM when it should be used. |
259-
| tools | array | `[]` | An array of tools that the current tool might depend on or use. |
260-
| maxTokens | number/undefined | `undefined` | The maximum number of tokens to be used. Prefer `undefined` for uninitialized or optional values. |
261-
| model | string | `""` | The model that the tool uses, if applicable. |
262-
| cache | boolean | `true` | Whether caching is enabled for the tool. |
263-
| temperature | number/undefined | `undefined` | The temperature setting for the model, affecting randomness. `undefined` for default behavior. |
264-
| args | object | `{}` | Additional arguments specific to the tool, described by key-value pairs. |
265-
| internalPrompt | boolean | `false` | An internal prompt used by the tool, if any. |
266-
| instructions | string | `""` | Instructions on how to use the tool. |
267-
| jsonResponse | boolean | `false` | Whether the tool returns a JSON response instead of plain text. You must include the word 'json' in the body of the prompt |
255+
| Argument | Type | Default | Description |
256+
|----------------|----------------|-------------|-----------------------------------------------------------------------------------------------|
257+
| name | string | `""` | The name of the tool. Optional only on the first tool if there are multiple tools defined. |
258+
| description | string | `""` | A brief description of what the tool does, this is important for explaining to the LLM when it should be used. |
259+
| tools | array | `[]` | An array of tools that the current tool might depend on or use. |
260+
| maxTokens | number/undefined | `undefined` | The maximum number of tokens to be used. Prefer `undefined` for uninitialized or optional values. |
261+
| model | string | `""` | The model that the tool uses, if applicable. |
262+
| disableCache | boolean | `true` | Whether caching is enabled for the tool. |
263+
| temperature | number/undefined | `undefined` | The temperature setting for the model, affecting randomness. `undefined` for default behavior. |
264+
| args | object | `{}` | Additional arguments specific to the tool, described by key-value pairs. |
265+
| internalPrompt | boolean | `false` | An internal prompt used by the tool, if any. |
266+
| instructions | string | `""` | Instructions on how to use the tool. |
267+
| jsonResponse | boolean | `false` | Whether the tool returns a JSON response instead of plain text. You must include the word 'json' in the body of the prompt |
268268

269269
### FreeForm Parameters
270270

src/exec.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,7 @@ async function streamExecWithEvents(command, args, stdin, cwd = './', env) {
7575
const namedPipe = '\\\\.\\pipe\\gptscript-' + Math.floor(Math.random() * 1000000);
7676
events = new stream.Readable({
7777
encoding: 'utf-8',
78-
read() {
79-
}
78+
read() {}
8079
});
8180

8281
server = net.createServer((connection) => {

src/gptscript.js

+61-19
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const execlib = require('./exec');
2-
const path = require('path');
2+
const reqlib = require('./request');
33
const tools = require('./tool');
4+
const path = require('path');
45

56
function getCmdPath() {
67
if (process.env.GPTSCRIPT_BIN) {
@@ -10,7 +11,7 @@ function getCmdPath() {
1011
}
1112

1213
const optToArg = {
13-
cache: "--disable-cache=",
14+
disableCache: "--disable-cache=",
1415
cacheDir: "--cache-dir=",
1516
quiet: "--quiet=",
1617
chdir: "--chdir=",
@@ -20,11 +21,7 @@ function toArgs(opts) {
2021
let args = ["--quiet=false"];
2122
for (const [key, value] of Object.entries(opts)) {
2223
if (optToArg[key]) {
23-
if (key === "cache") {
24-
args.push(optToArg[key] + !value);
25-
} else {
26-
args.push(optToArg[key] + value);
27-
}
24+
args.push(optToArg[key] + value);
2825
}
2926
}
3027
return args;
@@ -34,12 +31,7 @@ function getToolString(tool) {
3431
let toolString;
3532

3633
if (Array.isArray(tool)) {
37-
toolString = tool.map(singleTool => {
38-
if (!(singleTool instanceof tools.Tool || singleTool instanceof tools.FreeForm)) {
39-
throw new TypeError("Each tool must be an instance of Tool or FreeForm.");
40-
}
41-
return singleTool.toString();
42-
}).join('\n---\n');
34+
toolString = toolArrayToContents(tool);
4335
} else {
4436
if (!(tool instanceof tools.Tool || tool instanceof tools.FreeForm)) {
4537
throw new TypeError("The tool must be an instance of Tool or FreeForm.");
@@ -49,6 +41,15 @@ function getToolString(tool) {
4941
return toolString;
5042
}
5143

44+
function toolArrayToContents(toolArray) {
45+
return toolArray.map(singleTool => {
46+
if (!(singleTool instanceof tools.Tool || singleTool instanceof tools.FreeForm)) {
47+
throw new TypeError("Each tool must be an instance of Tool or FreeForm.");
48+
}
49+
return singleTool.toString();
50+
}).join('\n---\n');
51+
}
52+
5253
function cliArgBuilder(args, stdin, gptPath, input) {
5354
let returnArgs = []
5455
returnArgs.push(...args);
@@ -85,50 +86,91 @@ function streamRunWithEvents(args = [], stdin, gptPath = './', input = "", env =
8586
return execlib.streamExecWithEvents(cmdPath, cmdArgs, stdin, './', env);
8687
}
8788

88-
function listTools() {
89-
return run(['--list-tools']);
89+
async function listTools() {
90+
if (process.env['GPTSCRIPT_URL']) {
91+
return await reqlib.makeRequest('list-tools');
92+
}
93+
return await run(['--list-tools']);
9094
}
9195

92-
function version() {
93-
return run(['--version']);
96+
async function version() {
97+
if (process.env['GPTSCRIPT_URL']) {
98+
return await reqlib.makeRequest('version');
99+
}
100+
return await run(['--version']);
94101
}
95102

96103
async function listModels() {
104+
if (process.env['GPTSCRIPT_URL']) {
105+
return await reqlib.makeRequest('list-models');
106+
}
97107
const models = await run(['--list-models']);
98108
return models.trim().split('\n');
99109
}
100110

101111
async function exec(tool, opts = {}) {
112+
if (process.env['GPTSCRIPT_URL']) {
113+
if (Array.isArray(tool)) {
114+
return await reqlib.makeRequest('run-tool', {content: toolArrayToContents(tool)}, opts);
115+
}
116+
return await reqlib.makeRequest('run-tool', tool, opts);
117+
}
118+
102119
const args = toArgs(opts);
103120
const toolString = getToolString(tool);
104121
return await run(args, toolString);
105122
}
106123

107-
function execFile(scriptPath, input = "", opts = {}) {
124+
async function execFile(scriptPath, input = "", opts = {}) {
125+
if (process.env['GPTSCRIPT_URL']) {
126+
return await reqlib.makeRequest('run-file', {file: scriptPath, input: input}, opts);
127+
}
108128
const args = toArgs(opts);
109-
return run(args, undefined, scriptPath, input);
129+
return await run(args, undefined, scriptPath, input);
110130
}
111131

112132
function streamExec(tool, opts = {}) {
133+
if (process.env['GPTSCRIPT_URL']) {
134+
if (Array.isArray(tool)) {
135+
return reqlib.makeRequest('run-tool', {content: toolArrayToContents(tool)}, opts);
136+
}
137+
return reqlib.makeStreamRequest('run-tool-stream', tool, opts);
138+
}
139+
113140
const args = toArgs(opts);
114141
const toolString = getToolString(tool);
115142

116143
return streamRun(args, toolString);
117144
}
118145

119146
function streamExecWithEvents(tool, opts = {}) {
147+
if (process.env['GPTSCRIPT_URL']) {
148+
if (Array.isArray(tool)) {
149+
return reqlib.makeRequest('run-tool', {content: toolArrayToContents(tool)}, opts);
150+
}
151+
return reqlib.makeStreamRequestWithEvents('run-tool-stream-with-events', tool, opts);
152+
}
153+
120154
const args = toArgs(opts);
121155
const toolString = getToolString(tool);
122156

123157
return streamRunWithEvents(args, toolString);
124158
}
125159

126160
function streamExecFile(scriptPath, input = "", opts = {}) {
161+
if (process.env['GPTSCRIPT_URL']) {
162+
return reqlib.makeStreamRequest('run-file-stream', {file: scriptPath, input: input}, opts);
163+
}
164+
127165
const args = toArgs(opts);
128166
return streamRun(args, undefined, scriptPath, input);
129167
}
130168

131169
function streamExecFileWithEvents(scriptPath, input = "", opts = {}) {
170+
if (process.env['GPTSCRIPT_URL']) {
171+
return reqlib.makeStreamRequestWithEvents('run-file-stream-with-events', {file: scriptPath, input: input}, opts);
172+
}
173+
132174
const args = toArgs(opts);
133175

134176
return streamRunWithEvents(args, undefined, scriptPath, input);

0 commit comments

Comments
 (0)