1
1
import * as gptscript from "../src/gptscript"
2
2
import path from "path"
3
3
4
- const client = new gptscript . Client ( process . env . GPTSCRIPT_URL , process . env . GPTSCRIPT_BIN )
4
+ let client : gptscript . Client
5
5
6
6
describe ( "gptscript module" , ( ) => {
7
- beforeAll ( ( ) => {
7
+ beforeAll ( async ( ) => {
8
8
if ( ! process . env . OPENAI_API_KEY && ! process . env . GPTSCRIPT_URL ) {
9
9
throw new Error ( "neither OPENAI_API_KEY nor GPTSCRIPT_URL is set" )
10
10
}
11
+
12
+ client = await gptscript . Client . init ( )
13
+ } )
14
+ afterAll ( ( ) => {
15
+ client . close ( )
16
+ } )
17
+
18
+ test ( "creating an closing another client should work" , async ( ) => {
19
+ const other = await gptscript . Client . init ( )
20
+ await other . version ( )
21
+ other . close ( )
11
22
} )
12
23
13
24
test ( "listTools returns available tools" , async ( ) => {
@@ -47,17 +58,13 @@ describe("gptscript module", () => {
47
58
disableCache : true ,
48
59
}
49
60
50
- try {
51
- const run = client . evaluate ( t as any , opts )
52
- run . on ( gptscript . RunEventType . CallProgress , data => {
53
- out += `system: ${ ( data as any ) . content } `
54
- } )
61
+ const run = client . evaluate ( t as any , opts )
62
+ run . on ( gptscript . RunEventType . CallProgress , ( data : gptscript . CallFrame ) => {
63
+ for ( let output of data . output ) out += `system: ${ output . content } `
64
+ } )
55
65
56
- await run . text ( )
57
- err = run . err
58
- } catch ( e ) {
59
- console . error ( e )
60
- }
66
+ await run . text ( )
67
+ err = run . err
61
68
62
69
expect ( out ) . toContain ( "Calvin Coolidge" )
63
70
expect ( err ) . toEqual ( "" )
@@ -71,45 +78,20 @@ describe("gptscript module", () => {
71
78
context : [ path . join ( __dirname , "fixtures" , "acorn-labs-context.gpt" ) ]
72
79
}
73
80
74
- try {
75
- const run = client . evaluate ( t as any , { disableCache : true } )
76
- out = await run . text ( )
77
- err = run . err
78
- } catch ( e ) {
79
- console . error ( e )
80
- }
81
+ const run = client . evaluate ( t as any , { disableCache : true } )
82
+ out = await run . text ( )
83
+ err = run . err
81
84
82
85
expect ( out ) . toContain ( "Acorn Labs" )
83
86
expect ( err ) . toEqual ( "" )
84
87
} )
85
88
86
- describe ( "run with test.gpt fixture" , ( ) => {
87
- test ( "should execute test.gpt correctly" , async ( ) => {
88
- const testGptPath = path . join ( __dirname , "fixtures" , "test.gpt" )
89
-
90
- try {
91
- const result = await client . run ( testGptPath ) . text ( )
92
- expect ( result ) . toBeDefined ( )
93
- expect ( result ) . toContain ( "Calvin Coolidge" )
94
- } catch ( error ) {
95
- console . error ( error )
96
- fail ( "run threw an unexpected error." )
97
- }
98
- } )
89
+ test ( "should execute test.gpt correctly" , async ( ) => {
90
+ const testGptPath = path . join ( __dirname , "fixtures" , "test.gpt" )
99
91
100
- test ( "should execute test.gpt correctly when chdir is set" , async ( ) => {
101
- const testGptPath = path . join ( __dirname , "fixtures" )
102
-
103
- try {
104
- // By changing the directory here, we should be able to find the test.gpt file without prepending the path.
105
- const result = await client . run ( "test.gpt" , { chdir : testGptPath } ) . text ( )
106
- expect ( result ) . toBeDefined ( )
107
- expect ( result ) . toContain ( "Calvin Coolidge" )
108
- } catch ( error ) {
109
- console . error ( error )
110
- fail ( "run threw an unexpected error." )
111
- }
112
- } )
92
+ const result = await client . run ( testGptPath ) . text ( )
93
+ expect ( result ) . toBeDefined ( )
94
+ expect ( result ) . toContain ( "Calvin Coolidge" )
113
95
} )
114
96
115
97
test ( "run executes and stream a file correctly" , async ( ) => {
@@ -120,16 +102,12 @@ describe("gptscript module", () => {
120
102
disableCache : true ,
121
103
}
122
104
123
- try {
124
- const run = client . run ( testGptPath , opts )
125
- run . on ( gptscript . RunEventType . CallProgress , data => {
126
- out += `system: ${ ( data as any ) . content } `
127
- } )
128
- await run . text ( )
129
- err = run . err
130
- } catch ( e ) {
131
- console . error ( e )
132
- }
105
+ const run = client . run ( testGptPath , opts )
106
+ run . on ( gptscript . RunEventType . CallProgress , data => {
107
+ for ( let output of data . output ) out += `system: ${ output . content } `
108
+ } )
109
+ await run . text ( )
110
+ err = run . err
133
111
134
112
expect ( out ) . toContain ( "Calvin Coolidge" )
135
113
expect ( err ) . toEqual ( "" )
@@ -143,16 +121,12 @@ describe("gptscript module", () => {
143
121
disableCache : true ,
144
122
}
145
123
146
- try {
147
- const run = client . run ( testGptPath , opts )
148
- run . on ( gptscript . RunEventType . CallProgress , data => {
149
- out += `system: ${ ( data as any ) . content } `
150
- } )
151
- await run . text ( )
152
- err = run . err
153
- } catch ( e ) {
154
- console . error ( e )
155
- }
124
+ const run = client . run ( testGptPath , opts )
125
+ run . on ( gptscript . RunEventType . CallProgress , data => {
126
+ for ( let output of data . output ) out += `system: ${ output . content } `
127
+ } )
128
+ await run . text ( )
129
+ err = run . err
156
130
157
131
expect ( out ) . toContain ( "Hello!" )
158
132
expect ( err ) . toEqual ( "" )
@@ -315,26 +289,22 @@ describe("gptscript module", () => {
315
289
"Alaska Time Zone"
316
290
]
317
291
318
- try {
319
- await run . text ( )
320
- for ( let i : number = 0 ; i < inputs . length ; i ++ ) {
321
- run = run . nextChat ( inputs [ i ] )
322
- err = run . err
323
-
324
- if ( err ) {
325
- break
326
- }
292
+ await run . text ( )
293
+ for ( let i : number = 0 ; i < inputs . length ; i ++ ) {
294
+ run = run . nextChat ( inputs [ i ] )
295
+ err = run . err
327
296
328
- expect ( await run . text ( ) ) . toContain ( expectedOutputs [ i ] )
329
- expect ( run . state ) . toEqual ( gptscript . RunState . Continue )
297
+ if ( err ) {
298
+ break
330
299
}
331
300
332
- run = run . nextChat ( "bye" )
333
- await run . text ( )
334
- } catch ( e ) {
335
- console . error ( e )
301
+ expect ( await run . text ( ) ) . toContain ( expectedOutputs [ i ] )
302
+ expect ( run . state ) . toEqual ( gptscript . RunState . Continue )
336
303
}
337
304
305
+ run = run . nextChat ( "bye" )
306
+ await run . text ( )
307
+
338
308
expect ( run . state ) . toEqual ( gptscript . RunState . Finished )
339
309
expect ( err ) . toEqual ( "" )
340
310
} , 60000 )
@@ -358,26 +328,22 @@ describe("gptscript module", () => {
358
328
"Lake Huron"
359
329
]
360
330
361
- try {
362
- await run . text ( )
363
- for ( let i : number = 0 ; i < inputs . length ; i ++ ) {
364
- run = run . nextChat ( inputs [ i ] )
365
- err = run . err
366
-
367
- if ( err ) {
368
- break
369
- }
331
+ await run . text ( )
332
+ for ( let i : number = 0 ; i < inputs . length ; i ++ ) {
333
+ run = run . nextChat ( inputs [ i ] )
334
+ err = run . err
370
335
371
- expect ( await run . text ( ) ) . toContain ( expectedOutputs [ i ] )
372
- expect ( run . state ) . toEqual ( gptscript . RunState . Continue )
336
+ if ( err ) {
337
+ break
373
338
}
374
339
375
- run = run . nextChat ( "bye" )
376
- await run . text ( )
377
- } catch ( e ) {
378
- console . error ( e )
340
+ expect ( await run . text ( ) ) . toContain ( expectedOutputs [ i ] )
341
+ expect ( run . state ) . toEqual ( gptscript . RunState . Continue )
379
342
}
380
343
344
+ run = run . nextChat ( "bye" )
345
+ await run . text ( )
346
+
381
347
expect ( run . state ) . toEqual ( gptscript . RunState . Finished )
382
348
expect ( err ) . toEqual ( "" )
383
349
} , 60000 )
0 commit comments