@@ -5,6 +5,8 @@ import {fileURLToPath} from "url"
5
5
import { gunzipSync } from "zlib"
6
6
7
7
export interface GlobalOpts {
8
+ URL ?: string
9
+ Token ?: string
8
10
CacheDir ?: string
9
11
APIKey ?: string
10
12
BaseURL ?: string
@@ -48,6 +50,8 @@ export interface RunOpts {
48
50
env ?: string [ ]
49
51
forceSequential ?: boolean
50
52
53
+ URL ?: string
54
+ Token ?: string
51
55
CacheDir ?: string
52
56
APIKey ?: string
53
57
BaseURL ?: string
@@ -75,21 +79,24 @@ export class GPTScript {
75
79
private static instanceCount : number = 0
76
80
77
81
78
- private ready : boolean
79
82
private readonly opts : GlobalOpts
80
83
81
84
constructor ( opts ?: GlobalOpts ) {
82
85
this . opts = opts || { }
83
- this . ready = false
84
86
GPTScript . instanceCount ++
87
+
88
+ let startSDK = ! GPTScript . serverProcess && ! GPTScript . serverURL && ! this . opts . URL
89
+
85
90
if ( ! GPTScript . serverURL ) {
86
- GPTScript . serverURL = process . env . GPTSCRIPT_URL ?? "http://127.0.0.1:0"
87
- if ( ! GPTScript . serverURL . startsWith ( "http://" ) && ! GPTScript . serverURL . startsWith ( "https://" ) ) {
88
- GPTScript . serverURL = "http://" + GPTScript . serverURL
89
- }
91
+ GPTScript . serverURL = process . env . GPTSCRIPT_URL ?? ""
92
+ startSDK = startSDK && ! GPTScript . serverURL
90
93
}
91
94
92
- if ( GPTScript . instanceCount === 1 && ! process . env . GPTSCRIPT_URL ) {
95
+ if ( ! this . opts . Token ) {
96
+ this . opts . Token = process . env . GPTSCRIPT_TOKEN
97
+ }
98
+
99
+ if ( startSDK ) {
93
100
let env = process . env
94
101
if ( this . opts . Env ) {
95
102
env = {
@@ -113,7 +120,7 @@ export class GPTScript {
113
120
}
114
121
} )
115
122
116
- GPTScript . serverProcess = child_process . spawn ( getCmdPath ( ) , [ "sys.sdkserver" , "--listen-address" , GPTScript . serverURL . replace ( "http://" , "" ) ] , {
123
+ GPTScript . serverProcess = child_process . spawn ( getCmdPath ( ) , [ "sys.sdkserver" , "--listen-address" , "127.0.0.1:0" ] , {
117
124
env : env ,
118
125
stdio : [ "pipe" , "ignore" , "pipe" ]
119
126
} )
@@ -125,25 +132,31 @@ export class GPTScript {
125
132
}
126
133
127
134
GPTScript . serverURL = `http://${ url } `
128
- if ( ! this . opts . Env ) {
129
- this . opts . Env = [ ]
130
- }
131
- this . opts . Env . push ( `GPTSCRIPT_URL=${ GPTScript . serverURL } ` )
132
135
133
136
GPTScript . serverProcess . stderr ?. removeAllListeners ( )
134
137
} )
135
138
} else {
139
+ if ( ! this . opts . URL ) {
140
+ this . opts . URL = GPTScript . serverURL
141
+ }
142
+
136
143
if ( ! this . opts . Env ) {
137
144
this . opts . Env = [ ]
138
145
}
139
- this . opts . Env . push ( "GPTSCRIPT_URL=" + GPTScript . serverURL )
146
+ if ( this . opts . URL ) {
147
+ this . opts . Env . push ( `GPTSCRIPT_URL=${ this . opts . URL } ` )
148
+ }
149
+
150
+ if ( this . opts . Token ) {
151
+ this . opts . Env . push ( `GPTSCRIPT_TOKEN=${ this . opts . Token } ` )
152
+ }
140
153
}
141
154
}
142
155
143
156
close ( ) : void {
144
157
GPTScript . instanceCount --
145
158
if ( GPTScript . instanceCount === 0 && GPTScript . serverProcess ) {
146
- GPTScript . serverURL = process . env . GPTSCRIPT_URL ?? "http://127.0.0.1:0 "
159
+ GPTScript . serverURL = process . env . GPTSCRIPT_URL ?? ""
147
160
GPTScript . serverProcess . kill ( "SIGTERM" )
148
161
GPTScript . serverProcess . stdin ?. end ( )
149
162
}
@@ -168,10 +181,10 @@ export class GPTScript {
168
181
}
169
182
170
183
async runBasicCommand ( cmd : string , body ?: any ) : Promise < string > {
171
- if ( ! this . ready ) {
172
- this . ready = await this . testGPTScriptURL ( 20 )
184
+ if ( ! this . opts . URL ) {
185
+ await this . testGPTScriptURL ( 20 )
173
186
}
174
- const r = new RunSubcommand ( cmd , "" , { } , GPTScript . serverURL )
187
+ const r = new RunSubcommand ( cmd , "" , { URL : this . opts . URL , Token : this . opts . Token } )
175
188
r . requestNoStream ( body )
176
189
return r . text ( )
177
190
}
@@ -184,15 +197,14 @@ export class GPTScript {
184
197
* @return {Run } The Run object representing the running tool.
185
198
*/
186
199
async run ( toolName : string , opts : RunOpts = { } ) : Promise < Run > {
187
- if ( ! this . ready ) {
188
- this . ready = await this . testGPTScriptURL ( 20 )
200
+ if ( ! this . opts . URL ) {
201
+ await this . testGPTScriptURL ( 20 )
189
202
}
190
-
191
203
if ( this . opts . Env ) {
192
204
opts . env = this . opts . Env . concat ( opts . env || [ ] )
193
205
}
194
206
195
- return ( new Run ( "run" , toolName , { ...this . opts , ...opts } , GPTScript . serverURL ) ) . nextChat ( opts . input )
207
+ return ( new Run ( "run" , toolName , { ...this . opts , ...opts } ) ) . nextChat ( opts . input )
196
208
}
197
209
198
210
/**
@@ -203,37 +215,40 @@ export class GPTScript {
203
215
* @return {Run } The Run object representing the evaluation.
204
216
*/
205
217
async evaluate ( tool : Tool | ToolDef | ToolDef [ ] , opts : RunOpts = { } ) : Promise < Run > {
206
- if ( ! this . ready ) {
207
- this . ready = await this . testGPTScriptURL ( 20 )
218
+ if ( ! this . opts . URL ) {
219
+ await this . testGPTScriptURL ( 20 )
208
220
}
209
-
210
221
if ( this . opts . Env ) {
211
222
opts . env = this . opts . Env . concat ( opts . env || [ ] )
212
223
}
213
- return ( new Run ( "evaluate" , tool , { ...this . opts , ...opts } , GPTScript . serverURL ) ) . nextChat ( opts . input )
224
+ return ( new Run ( "evaluate" , tool , { ...this . opts , ...opts } ) ) . nextChat ( opts . input )
214
225
}
215
226
216
227
async parse ( fileName : string , disableCache ?: boolean ) : Promise < Block [ ] > {
217
- if ( ! this . ready ) {
218
- this . ready = await this . testGPTScriptURL ( 20 )
228
+ if ( ! this . opts . URL ) {
229
+ await this . testGPTScriptURL ( 20 )
219
230
}
220
- const r : Run = new RunSubcommand ( "parse" , fileName , { disableCache : disableCache } , GPTScript . serverURL )
231
+ const r : Run = new RunSubcommand ( "parse" , fileName , {
232
+ disableCache : disableCache ,
233
+ URL : this . opts . URL ,
234
+ Token : this . opts . Token
235
+ } )
221
236
r . request ( { file : fileName } )
222
237
return parseBlocksFromNodes ( ( await r . json ( ) ) . nodes )
223
238
}
224
239
225
240
async parseContent ( toolContent : string ) : Promise < Block [ ] > {
226
- if ( ! this . ready ) {
227
- this . ready = await this . testGPTScriptURL ( 20 )
241
+ if ( ! this . opts . URL ) {
242
+ await this . testGPTScriptURL ( 20 )
228
243
}
229
- const r : Run = new RunSubcommand ( "parse" , "" , { } , GPTScript . serverURL )
244
+ const r : Run = new RunSubcommand ( "parse" , "" , { URL : this . opts . URL , Token : this . opts . Token } )
230
245
r . request ( { content : toolContent } )
231
246
return parseBlocksFromNodes ( ( await r . json ( ) ) . nodes )
232
247
}
233
248
234
249
async stringify ( blocks : Block [ ] ) : Promise < string > {
235
- if ( ! this . ready ) {
236
- this . ready = await this . testGPTScriptURL ( 20 )
250
+ if ( ! this . opts . URL ) {
251
+ await this . testGPTScriptURL ( 20 )
237
252
}
238
253
const nodes : any [ ] = [ ]
239
254
@@ -253,16 +268,16 @@ export class GPTScript {
253
268
}
254
269
}
255
270
256
- const r : Run = new RunSubcommand ( "fmt" , "" , { } , GPTScript . serverURL )
271
+ const r : Run = new RunSubcommand ( "fmt" , "" , { URL : this . opts . URL , Token : this . opts . Token } )
257
272
r . request ( { nodes : nodes } )
258
273
return r . text ( )
259
274
}
260
275
261
276
async confirm ( response : AuthResponse ) : Promise < void > {
262
- if ( ! this . ready ) {
263
- this . ready = await this . testGPTScriptURL ( 20 )
277
+ if ( ! this . opts . URL ) {
278
+ await this . testGPTScriptURL ( 20 )
264
279
}
265
- const resp = await fetch ( `${ GPTScript . serverURL } /confirm/${ response . id } ` , {
280
+ const resp = await fetch ( `${ this . opts . URL } /confirm/${ response . id } ` , {
266
281
method : "POST" ,
267
282
body : JSON . stringify ( response )
268
283
} )
@@ -273,10 +288,10 @@ export class GPTScript {
273
288
}
274
289
275
290
async promptResponse ( response : PromptResponse ) : Promise < void > {
276
- if ( ! this . ready ) {
277
- this . ready = await this . testGPTScriptURL ( 20 )
291
+ if ( ! this . opts . URL ) {
292
+ await this . testGPTScriptURL ( 20 )
278
293
}
279
- const resp = await fetch ( `${ GPTScript . serverURL } /prompt-response/${ response . id } ` , {
294
+ const resp = await fetch ( `${ this . opts . URL } /prompt-response/${ response . id } ` , {
280
295
method : "POST" ,
281
296
body : JSON . stringify ( response . responses )
282
297
} )
@@ -335,42 +350,42 @@ export class GPTScript {
335
350
}
336
351
337
352
async listCredentials ( context : Array < string > , allContexts : boolean ) : Promise < Array < Credential > > {
338
- if ( ! this . ready ) {
339
- this . ready = await this . testGPTScriptURL ( 20 )
353
+ if ( ! this . opts . URL ) {
354
+ await this . testGPTScriptURL ( 20 )
340
355
}
341
356
342
- const r : Run = new RunSubcommand ( "credentials" , "" , { } , GPTScript . serverURL )
357
+ const r : Run = new RunSubcommand ( "credentials" , "" , { URL : this . opts . URL , Token : this . opts . Token } )
343
358
r . request ( { context, allContexts} )
344
359
const out = await r . json ( )
345
360
return out . map ( ( c : any ) => jsonToCredential ( JSON . stringify ( c ) ) )
346
361
}
347
362
348
363
async createCredential ( credential : Credential ) : Promise < void > {
349
- if ( ! this . ready ) {
350
- this . ready = await this . testGPTScriptURL ( 20 )
364
+ if ( ! this . opts . URL ) {
365
+ await this . testGPTScriptURL ( 20 )
351
366
}
352
367
353
- const r : Run = new RunSubcommand ( "credentials/create" , "" , { } , GPTScript . serverURL )
368
+ const r : Run = new RunSubcommand ( "credentials/create" , "" , { URL : this . opts . URL , Token : this . opts . Token } )
354
369
r . request ( { content : credentialToJSON ( credential ) } )
355
370
await r . text ( )
356
371
}
357
372
358
373
async revealCredential ( context : Array < string > , name : string ) : Promise < Credential > {
359
- if ( ! this . ready ) {
360
- this . ready = await this . testGPTScriptURL ( 20 )
374
+ if ( ! this . opts . URL ) {
375
+ await this . testGPTScriptURL ( 20 )
361
376
}
362
377
363
- const r : Run = new RunSubcommand ( "credentials/reveal" , "" , { } , GPTScript . serverURL )
378
+ const r : Run = new RunSubcommand ( "credentials/reveal" , "" , { URL : this . opts . URL , Token : this . opts . Token } )
364
379
r . request ( { context, name} )
365
380
return jsonToCredential ( await r . text ( ) )
366
381
}
367
382
368
383
async deleteCredential ( context : string , name : string ) : Promise < void > {
369
- if ( ! this . ready ) {
370
- this . ready = await this . testGPTScriptURL ( 20 )
384
+ if ( ! this . opts . URL ) {
385
+ await this . testGPTScriptURL ( 20 )
371
386
}
372
387
373
- const r : Run = new RunSubcommand ( "credentials/delete" , "" , { } , GPTScript . serverURL )
388
+ const r : Run = new RunSubcommand ( "credentials/delete" , "" , { URL : this . opts . URL , Token : this . opts . Token } )
374
389
r . request ( { context : [ context ] , name} )
375
390
await r . text ( )
376
391
}
@@ -382,20 +397,29 @@ export class GPTScript {
382
397
* @return {Promise<LoadResponse> } The loaded program.
383
398
*/
384
399
private async _load ( payload : any ) : Promise < LoadResponse > {
385
- if ( ! this . ready ) {
386
- this . ready = await this . testGPTScriptURL ( 20 )
400
+ if ( ! this . opts . URL ) {
401
+ await this . testGPTScriptURL ( 20 )
387
402
}
388
- const r : Run = new RunSubcommand ( "load" , payload . toolDefs || [ ] , { } , GPTScript . serverURL )
403
+ const r : Run = new RunSubcommand ( "load" , payload . toolDefs || [ ] , { URL : this . opts . URL , Token : this . opts . Token } )
389
404
390
405
r . request ( payload )
391
406
return ( await r . json ( ) ) as LoadResponse
392
407
}
393
408
394
- private async testGPTScriptURL ( count : number ) : Promise < boolean > {
409
+ private async testGPTScriptURL ( count : number ) : Promise < void > {
395
410
while ( count > 0 ) {
396
411
try {
397
412
await fetch ( `${ GPTScript . serverURL } /healthz` )
398
- return true
413
+ this . opts . URL = GPTScript . serverURL
414
+ if ( ! this . opts . Env ) {
415
+ this . opts . Env = [ ]
416
+ }
417
+ this . opts . Env . push ( `GPTSCRIPT_URL=${ this . opts . URL } ` )
418
+ if ( this . opts . Token ) {
419
+ this . opts . Env . push ( `GPTSCRIPT_TOKEN=${ this . opts . Token } ` )
420
+ }
421
+
422
+ return
399
423
} catch {
400
424
if ( count === 0 ) {
401
425
}
@@ -418,7 +442,6 @@ export class Run {
418
442
419
443
protected stdout ?: string
420
444
421
- private readonly gptscriptURL ?: string
422
445
private readonly requestPath : string = ""
423
446
private promise ?: Promise < string >
424
447
private req ?: http . ClientRequest
@@ -429,13 +452,11 @@ export class Run {
429
452
private prg ?: Program
430
453
private respondingToolId ?: string
431
454
432
- constructor ( subCommand : string , tools : ToolDef | ToolDef [ ] | string , opts : RunOpts , gptscriptURL ?: string ) {
455
+ constructor ( subCommand : string , tools : ToolDef | ToolDef [ ] | string , opts : RunOpts ) {
433
456
this . id = randomId ( "run-" )
434
457
this . requestPath = subCommand
435
458
this . opts = opts
436
459
this . tools = tools
437
-
438
- this . gptscriptURL = gptscriptURL
439
460
}
440
461
441
462
nextChat ( input : string = "" ) : Run {
@@ -445,7 +466,7 @@ export class Run {
445
466
446
467
let run = this
447
468
if ( run . state !== RunState . Creating ) {
448
- run = new ( this . constructor as any ) ( this . requestPath , this . tools , this . opts , this . gptscriptURL )
469
+ run = new ( this . constructor as any ) ( this . requestPath , this . tools , this . opts )
449
470
}
450
471
451
472
if ( this . chatState && this . state === RunState . Continue ) {
@@ -493,10 +514,10 @@ export class Run {
493
514
}
494
515
495
516
request ( tool : any ) {
496
- if ( ! this . gptscriptURL ) {
497
- throw new Error ( "request() requires gptscriptURL to be set" )
517
+ if ( ! this . opts . URL ) {
518
+ throw new Error ( "request() requires URL to be set" )
498
519
}
499
- const options = this . requestOptions ( this . gptscriptURL , this . requestPath , tool )
520
+ const options = this . requestOptions ( this . opts . URL , this . opts . Token || "" , this . requestPath , tool )
500
521
options . headers = { "Transfer-Encoding" : "chunked" , ...options . headers } as any
501
522
502
523
this . promise = new Promise < string > ( async ( resolve , reject ) => {
@@ -580,15 +601,15 @@ export class Run {
580
601
}
581
602
582
603
requestNoStream ( tool : any ) {
583
- if ( ! this . gptscriptURL ) {
604
+ if ( ! this . opts . URL ) {
584
605
throw new Error ( "request() requires gptscriptURL to be set" )
585
606
}
586
607
587
- const options = this . requestOptions ( this . gptscriptURL , this . requestPath , tool ) as any
608
+ const options = this . requestOptions ( this . opts . URL , this . opts . Token || "" , this . requestPath , tool ) as any
588
609
if ( tool ) {
589
610
options . body = JSON . stringify ( { ...tool , ...this . opts } )
590
611
}
591
- const req = new Request ( this . gptscriptURL + "/" + this . requestPath , options )
612
+ const req = new Request ( this . opts . URL + "/" + this . requestPath , options )
592
613
593
614
this . promise = new Promise < string > ( async ( resolve , reject ) => {
594
615
fetch ( req ) . then ( resp => {
@@ -601,23 +622,28 @@ export class Run {
601
622
} )
602
623
}
603
624
604
- requestOptions ( gptscriptURL : string , path : string , tool : any ) {
625
+ requestOptions ( gptscriptURL : string , token : string , path : string , tool : any ) {
605
626
let method = "GET"
606
627
if ( tool ) {
607
628
method = "POST"
608
629
}
609
630
610
631
const url = new URL ( gptscriptURL )
611
632
633
+ const headers = {
634
+ "Content-Type" : "application/json"
635
+ } as any
636
+ if ( token ) {
637
+ headers [ "Authorization" ] = `Bearer ${ token } `
638
+ }
639
+
612
640
return {
613
641
hostname : url . hostname ,
614
642
port : url . port || 80 ,
615
643
protocol : url . protocol || "http:" ,
616
644
path : "/" + path ,
617
645
method : method ,
618
- headers : {
619
- "Content-Type" : "application/json"
620
- } ,
646
+ headers : headers
621
647
}
622
648
}
623
649
@@ -747,8 +773,8 @@ export class Run {
747
773
}
748
774
749
775
class RunSubcommand extends Run {
750
- constructor ( subCommand : string , tool : ToolDef | ToolDef [ ] | string , opts : RunOpts , gptscriptURL ?: string ) {
751
- super ( subCommand , tool , opts , gptscriptURL )
776
+ constructor ( subCommand : string , tool : ToolDef | ToolDef [ ] | string , opts : RunOpts ) {
777
+ super ( subCommand , tool , opts )
752
778
}
753
779
754
780
processStdout ( data : string | object ) : string {
0 commit comments