@@ -214,7 +214,7 @@ export class Run {
214
214
public readonly filePath : string
215
215
public readonly content : string
216
216
public state : RunState = RunState . Creating
217
- public calls : CallFrame [ ] = [ ]
217
+ public calls : Record < string , CallFrame > = { }
218
218
public err : string = ""
219
219
220
220
protected stdout ?: string
@@ -226,6 +226,10 @@ export class Run {
226
226
private stderr ?: string
227
227
private callbacks : Record < string , ( ( f : Frame ) => void ) [ ] > = { }
228
228
private chatState ?: string
229
+ private callIdsByParentIds : Record < string , string [ ] > = { }
230
+ private parentCallId : string = ""
231
+ private prg ?: Program
232
+ private respondingToolId ?: string
229
233
230
234
constructor ( subCommand : string , path : string , content : string , opts : RunOpts , gptscriptURL ?: string ) {
231
235
this . id = randomId ( "run-" )
@@ -279,6 +283,7 @@ export class Run {
279
283
if ( out . done === undefined || ! out . done ) {
280
284
this . chatState = JSON . stringify ( out . state )
281
285
this . state = RunState . Continue
286
+ this . respondingToolId = out . toolId
282
287
} else {
283
288
this . state = RunState . Finished
284
289
this . chatState = undefined
@@ -412,7 +417,61 @@ export class Run {
412
417
}
413
418
}
414
419
415
- emitEvent ( data : string ) : string {
420
+ public on ( event : RunEventType . RunStart | RunEventType . RunFinish , listener : ( data : RunFrame ) => void ) : this;
421
+ public on ( event : RunEventType . CallStart | RunEventType . CallProgress | RunEventType . CallContinue | RunEventType . CallChat | RunEventType . CallConfirm | RunEventType . CallFinish , listener : ( data : CallFrame ) => void ) : this;
422
+ public on ( event : RunEventType . Prompt , listener : ( data : PromptFrame ) => void ) : this;
423
+ public on ( event : RunEventType . Event , listener : ( data : Frame ) => void ) : this;
424
+ public on ( event : RunEventType , listener : ( data : any ) => void ) : this {
425
+ if ( ! this . callbacks [ event ] ) {
426
+ this . callbacks [ event ] = [ ]
427
+ }
428
+
429
+ this . callbacks [ event ] . push ( listener )
430
+
431
+ return this
432
+ }
433
+
434
+ public text ( ) : Promise < string > {
435
+ if ( this . err ) {
436
+ throw new Error ( this . err )
437
+ }
438
+
439
+ if ( ! this . promise ) {
440
+ throw new Error ( "Run not started" )
441
+ }
442
+
443
+ return this . promise
444
+ }
445
+
446
+ public async json ( ) : Promise < any > {
447
+ return JSON . parse ( await this . text ( ) )
448
+ }
449
+
450
+ public currentChatState ( ) : string | undefined {
451
+ return this . chatState
452
+ }
453
+
454
+ public firstCallId ( ) : string {
455
+ return this . parentCallId
456
+ }
457
+
458
+ public program ( ) : Program | undefined {
459
+ return this . prg
460
+ }
461
+
462
+ public respondingTool ( ) : Tool | undefined {
463
+ return this . respondingToolId ? this . prg ?. toolSet [ this . respondingToolId ] : undefined
464
+ }
465
+
466
+ public close ( ) : void {
467
+ if ( this . req ) {
468
+ this . req . destroy ( )
469
+ return
470
+ }
471
+ throw new Error ( "Run not started" )
472
+ }
473
+
474
+ private emitEvent ( data : string ) : string {
416
475
for ( let event of data . split ( "\n" ) ) {
417
476
event = event . trim ( )
418
477
@@ -448,6 +507,7 @@ export class Run {
448
507
449
508
if ( f . type === RunEventType . RunStart ) {
450
509
this . state = RunState . Running
510
+ this . prg = f . program
451
511
} else if ( f . type === RunEventType . RunFinish ) {
452
512
if ( f . error ) {
453
513
this . state = RunState . Error
@@ -457,14 +517,11 @@ export class Run {
457
517
this . stdout = f . output || ""
458
518
}
459
519
} else if ( ( f . type as string ) . startsWith ( "call" ) ) {
460
- f = ( f as CallFrame )
461
- const idx = this . calls ?. findIndex ( ( x ) => x . id === f . id )
462
-
463
- if ( idx === - 1 ) {
464
- this . calls . push ( f )
465
- } else {
466
- this . calls [ idx ] = f
520
+ f = f as CallFrame
521
+ if ( f . parentID === "" && this . parentCallId === "" ) {
522
+ this . parentCallId = f . id
467
523
}
524
+ this . calls [ f . id ] = f
468
525
}
469
526
470
527
this . emit ( RunEventType . Event , f )
@@ -474,48 +531,6 @@ export class Run {
474
531
return ""
475
532
}
476
533
477
- public on ( event : RunEventType . RunStart | RunEventType . RunFinish , listener : ( data : RunFrame ) => void ) : this;
478
- public on ( event : RunEventType . CallStart | RunEventType . CallProgress | RunEventType . CallContinue | RunEventType . CallChat | RunEventType . CallConfirm | RunEventType . CallFinish , listener : ( data : CallFrame ) => void ) : this;
479
- public on ( event : RunEventType . Prompt , listener : ( data : PromptFrame ) => void ) : this;
480
- public on ( event : RunEventType . Event , listener : ( data : Frame ) => void ) : this;
481
- public on ( event : RunEventType , listener : ( data : any ) => void ) : this {
482
- if ( ! this . callbacks [ event ] ) {
483
- this . callbacks [ event ] = [ ]
484
- }
485
-
486
- this . callbacks [ event ] . push ( listener )
487
-
488
- return this
489
- }
490
-
491
- public text ( ) : Promise < string > {
492
- if ( this . err ) {
493
- throw new Error ( this . err )
494
- }
495
-
496
- if ( ! this . promise ) {
497
- throw new Error ( "Run not started" )
498
- }
499
-
500
- return this . promise
501
- }
502
-
503
- public async json ( ) : Promise < any > {
504
- return JSON . parse ( await this . text ( ) )
505
- }
506
-
507
- public currentChatState ( ) : string | undefined {
508
- return this . chatState
509
- }
510
-
511
- public close ( ) : void {
512
- if ( this . req ) {
513
- this . req . destroy ( )
514
- return
515
- }
516
- throw new Error ( "Run not started" )
517
- }
518
-
519
534
private emit ( event : RunEventType , data : any ) {
520
535
for ( const cb of this . callbacks [ event ] || [ ] ) {
521
536
cb ( data )
@@ -556,7 +571,7 @@ export interface ArgumentSchema {
556
571
557
572
export interface Program {
558
573
name : string
559
- blocks : Block [ ]
574
+ toolSet : Record < string , Tool >
560
575
openAPICache : Record < string , any >
561
576
}
562
577
0 commit comments