@@ -214,7 +214,7 @@ export class Run {
214214 public readonly filePath : string
215215 public readonly content : string
216216 public state : RunState = RunState . Creating
217- public calls : CallFrame [ ] = [ ]
217+ public calls : Record < string , CallFrame > = { }
218218 public err : string = ""
219219
220220 protected stdout ?: string
@@ -226,6 +226,10 @@ export class Run {
226226 private stderr ?: string
227227 private callbacks : Record < string , ( ( f : Frame ) => void ) [ ] > = { }
228228 private chatState ?: string
229+ private callIdsByParentIds : Record < string , string [ ] > = { }
230+ private parentCallId : string = ""
231+ private prg ?: Program
232+ private respondingToolId ?: string
229233
230234 constructor ( subCommand : string , path : string , content : string , opts : RunOpts , gptscriptURL ?: string ) {
231235 this . id = randomId ( "run-" )
@@ -279,6 +283,7 @@ export class Run {
279283 if ( out . done === undefined || ! out . done ) {
280284 this . chatState = JSON . stringify ( out . state )
281285 this . state = RunState . Continue
286+ this . respondingToolId = out . toolId
282287 } else {
283288 this . state = RunState . Finished
284289 this . chatState = undefined
@@ -412,7 +417,61 @@ export class Run {
412417 }
413418 }
414419
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 {
416475 for ( let event of data . split ( "\n" ) ) {
417476 event = event . trim ( )
418477
@@ -448,6 +507,7 @@ export class Run {
448507
449508 if ( f . type === RunEventType . RunStart ) {
450509 this . state = RunState . Running
510+ this . prg = f . program
451511 } else if ( f . type === RunEventType . RunFinish ) {
452512 if ( f . error ) {
453513 this . state = RunState . Error
@@ -457,14 +517,11 @@ export class Run {
457517 this . stdout = f . output || ""
458518 }
459519 } 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
467523 }
524+ this . calls [ f . id ] = f
468525 }
469526
470527 this . emit ( RunEventType . Event , f )
@@ -474,48 +531,6 @@ export class Run {
474531 return ""
475532 }
476533
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-
519534 private emit ( event : RunEventType , data : any ) {
520535 for ( const cb of this . callbacks [ event ] || [ ] ) {
521536 cb ( data )
@@ -556,7 +571,7 @@ export interface ArgumentSchema {
556571
557572export interface Program {
558573 name : string
559- blocks : Block [ ]
574+ toolSet : Record < string , Tool >
560575 openAPICache : Record < string , any >
561576}
562577
0 commit comments