@@ -47,7 +47,7 @@ export class Run {
47
47
public readonly opts : RunOpts
48
48
public state : RunState = RunState . Creating
49
49
public calls : Call [ ] = [ ]
50
- public err = ''
50
+ public err = ""
51
51
public readonly path : string
52
52
53
53
private promise ?: Promise < string >
@@ -157,7 +157,7 @@ export class Run {
157
157
this . err = "Run has been aborted"
158
158
} else if ( code !== 0 ) {
159
159
this . state = RunState . Error
160
- this . err = this . stderr || ''
160
+ this . err = this . stderr || ""
161
161
} else {
162
162
this . state = RunState . Finished
163
163
}
@@ -226,14 +226,14 @@ export class Run {
226
226
227
227
res . on ( "error" , ( error : Error ) => {
228
228
this . state = RunState . Error
229
- this . err = error . message || ''
229
+ this . err = error . message || ""
230
230
reject ( this . err )
231
231
} )
232
232
} )
233
233
234
234
this . req . on ( "error" , ( error : Error ) => {
235
235
this . state = RunState . Error
236
- this . err = error . message || ''
236
+ this . err = error . message || ""
237
237
reject ( this . err )
238
238
} )
239
239
@@ -287,7 +287,7 @@ export class Run {
287
287
} else if ( f . type === RunEventType . RunFinish ) {
288
288
if ( f . err ) {
289
289
this . state = RunState . Error
290
- this . err = f . err || ''
290
+ this . err = f . err || ""
291
291
} else {
292
292
this . state = RunState . Finished
293
293
this . stdout = f . output || ""
@@ -343,12 +343,6 @@ export class Run {
343
343
return ""
344
344
}
345
345
346
- private emit ( event : RunEventType , data : any ) {
347
- for ( const cb of this . callbacks [ event ] || [ ] ) {
348
- cb ( data )
349
- }
350
- }
351
-
352
346
public on ( event : RunEventType . RunStart , listener : ( data : RunStartFrame ) => void ) : this;
353
347
public on ( event : RunEventType . RunFinish , listener : ( data : RunFinishFrame ) => void ) : this;
354
348
public on ( event : RunEventType . CallStart , listener : ( data : CallStartFrame ) => void ) : this;
@@ -358,8 +352,8 @@ export class Run {
358
352
public on ( event : RunEventType . CallFinish , listener : ( data : CallFinishFrame ) => void ) : this;
359
353
public on ( event : RunEventType . Event , listener : ( data : Frame ) => void ) : this;
360
354
public on ( event : RunEventType , listener : ( data : any ) => void ) : this {
361
- if ( ! this . callbacks [ event ] ) {
362
- this . callbacks [ event ] = [ ] ;
355
+ if ( ! this . callbacks [ event ] ) {
356
+ this . callbacks [ event ] = [ ]
363
357
}
364
358
365
359
this . callbacks [ event ] . push ( listener )
@@ -398,6 +392,12 @@ export class Run {
398
392
399
393
throw new Error ( "Run not started" )
400
394
}
395
+
396
+ private emit ( event : RunEventType , data : any ) {
397
+ for ( const cb of this . callbacks [ event ] || [ ] ) {
398
+ cb ( data )
399
+ }
400
+ }
401
401
}
402
402
403
403
export type Arguments = string | Record < string , string >
@@ -408,25 +408,6 @@ export interface ArgumentSchema {
408
408
required ?: string [ ]
409
409
}
410
410
411
- export type EnvVars = string [ ]
412
-
413
- export interface Parameters {
414
- name : string
415
- description : string
416
- maxTokens : number
417
- modelName : string
418
- modelProvider : boolean
419
- jsonResponse : boolean
420
- temperature : number
421
- cache ?: boolean
422
- internalPrompt : boolean
423
- arguments : ArgumentSchema
424
- tools : string [ ]
425
- globalTools : string [ ]
426
- export : string [ ]
427
- blocking : boolean
428
- }
429
-
430
411
export interface Program {
431
412
name : string
432
413
blocks : Block [ ]
@@ -446,7 +427,21 @@ export interface Repo {
446
427
revision : string
447
428
}
448
429
449
- export interface ToolDef extends Parameters {
430
+ export interface ToolDef {
431
+ name : string
432
+ description : string
433
+ maxTokens : number
434
+ modelName : string
435
+ modelProvider : boolean
436
+ jsonResponse : boolean
437
+ temperature : number
438
+ cache ?: boolean
439
+ internalPrompt : boolean
440
+ arguments : ArgumentSchema
441
+ tools : string [ ]
442
+ globalTools : string [ ]
443
+ export : string [ ]
444
+ blocking : boolean
450
445
instructions : string
451
446
}
452
447
@@ -493,12 +488,17 @@ export interface Call {
493
488
showSystemMessages ?: boolean
494
489
}
495
490
496
- export interface BaseFrame {
491
+ interface BaseFrame {
497
492
type : RunEventType
498
493
time : string
499
494
runID : string
500
495
}
501
496
497
+ interface CallFrame extends BaseFrame {
498
+ callContext : Call
499
+ input : Arguments
500
+ }
501
+
502
502
export interface RunStartFrame extends BaseFrame {
503
503
type : RunEventType . RunStart
504
504
program : Program
@@ -512,11 +512,6 @@ export interface RunFinishFrame extends BaseFrame {
512
512
output ?: string
513
513
}
514
514
515
- export interface CallFrame extends BaseFrame {
516
- callContext : Call
517
- input : Arguments
518
- }
519
-
520
515
export interface CallStartFrame extends CallFrame {
521
516
type : RunEventType . CallStart
522
517
content : string
@@ -649,7 +644,14 @@ function runBasicCommand(cmd: string, gptscriptURL?: string): Promise<string> {
649
644
return r . text ( )
650
645
}
651
646
652
- export function run ( toolName : string , opts : RunOpts ) : Run {
647
+ /**
648
+ * Runs a tool with the specified name and options.
649
+ *
650
+ * @param {string } toolName - The name of the tool to run. Can be a file path, URL, or GitHub URL.
651
+ * @param {RunOpts } [opts={}] - The options for running the tool.
652
+ * @return {Run } The Run object representing the running tool.
653
+ */
654
+ export function run ( toolName : string , opts : RunOpts = { } ) : Run {
653
655
const r : Run = new Run ( toolName , opts )
654
656
655
657
if ( opts . gptscriptURL ) {
@@ -661,7 +663,14 @@ export function run(toolName: string, opts: RunOpts): Run {
661
663
return r
662
664
}
663
665
664
- export function evaluate ( tool : ToolDef | ToolDef [ ] | string , opts : RunOpts ) : Run {
666
+ /**
667
+ * Evaluates the given tool and returns a Run object.
668
+ *
669
+ * @param {ToolDef | ToolDef[] | string } tool - The tool to be evaluated. Can be a single ToolDef object, an array of ToolDef objects, or a string representing the tool contents.
670
+ * @param {RunOpts } [opts={}] - Optional options for the evaluation.
671
+ * @return {Run } The Run object representing the evaluation.
672
+ */
673
+ export function evaluate ( tool : ToolDef | ToolDef [ ] | string , opts : RunOpts = { } ) : Run {
665
674
let toolString : string = ""
666
675
667
676
if ( Array . isArray ( tool ) ) {
@@ -695,12 +704,12 @@ export async function parse(fileName: string, gptscriptURL?: string): Promise<Bl
695
704
return parseBlocksFromNodes ( ( await r . json ( ) ) . nodes )
696
705
}
697
706
698
- export async function parseTool ( tool : string , gptscriptURL ?: string ) : Promise < Block [ ] > {
707
+ export async function parseTool ( toolContent : string , gptscriptURL ?: string ) : Promise < Block [ ] > {
699
708
const r : Run = new Run ( "" , { gptscriptURL : gptscriptURL } )
700
709
if ( gptscriptURL ) {
701
- r . request ( "parse" , { input : tool } )
710
+ r . request ( "parse" , { input : toolContent } )
702
711
} else {
703
- r . exec ( getCmdPath ( ) , [ "parse" , "-" ] , tool )
712
+ r . exec ( getCmdPath ( ) , [ "parse" , "-" ] , toolContent )
704
713
}
705
714
return parseBlocksFromNodes ( ( await r . json ( ) ) . nodes )
706
715
}
@@ -744,7 +753,7 @@ function parseBlocksFromNodes(nodes: any[]): Block[] {
744
753
blocks . push ( {
745
754
type : "tool" ,
746
755
...node . toolNode . tool ,
747
- } )
756
+ } as Tool )
748
757
}
749
758
if ( node . textNode ) {
750
759
const format = node . textNode . text . substring ( 1 , node . textNode . text . indexOf ( "\n" ) ) . trim ( ) || "text"
@@ -753,7 +762,7 @@ function parseBlocksFromNodes(nodes: any[]): Block[] {
753
762
type : "text" ,
754
763
format : format ,
755
764
content : node . textNode . text . substring ( node . textNode . text . indexOf ( "\n" ) + 1 ) . trim ( ) ,
756
- } )
765
+ } as Text )
757
766
}
758
767
}
759
768
return blocks
0 commit comments