@@ -142,6 +142,7 @@ export class Cline extends EventEmitter<ClineEvents> {
142142 private askResponse ?: ClineAskResponse
143143 private askResponseText ?: string
144144 private askResponseImages ?: string [ ]
145+ private lastMessageTs ?: number
145146 // Not private since it needs to be accessible by tools
146147 consecutiveMistakeCount : number = 0
147148 consecutiveMistakeCountForApplyDiff : Map < string , number > = new Map ( )
@@ -440,6 +441,7 @@ export class Cline extends EventEmitter<ClineEvents> {
440441 // This is a new partial message, so add it with partial
441442 // state.
442443 askTs = Date . now ( )
444+ this . lastMessageTs = askTs
443445 await this . addToClineMessages ( { ts : askTs , type : "ask" , ask : type , text, partial } )
444446 throw new Error ( "Current ask promise was ignored (#2)" )
445447 }
@@ -458,6 +460,8 @@ export class Cline extends EventEmitter<ClineEvents> {
458460 So in this case we must make sure that the message ts is never altered after first setting it.
459461 */
460462 askTs = lastMessage . ts
463+ this . lastMessageTs = askTs
464+ // lastMessage.ts = askTs
461465 lastMessage . text = text
462466 lastMessage . partial = false
463467 lastMessage . progressStatus = progressStatus
@@ -469,6 +473,7 @@ export class Cline extends EventEmitter<ClineEvents> {
469473 this . askResponseText = undefined
470474 this . askResponseImages = undefined
471475 askTs = Date . now ( )
476+ this . lastMessageTs = askTs
472477 await this . addToClineMessages ( { ts : askTs , type : "ask" , ask : type , text } )
473478 }
474479 }
@@ -478,10 +483,18 @@ export class Cline extends EventEmitter<ClineEvents> {
478483 this . askResponseText = undefined
479484 this . askResponseImages = undefined
480485 askTs = Date . now ( )
486+ this . lastMessageTs = askTs
481487 await this . addToClineMessages ( { ts : askTs , type : "ask" , ask : type , text } )
482488 }
483489
484- await pWaitFor ( ( ) => this . askResponse !== undefined , { interval : 100 } )
490+ await pWaitFor ( ( ) => this . askResponse !== undefined || this . lastMessageTs !== askTs , { interval : 100 } )
491+
492+ if ( this . lastMessageTs !== askTs ) {
493+ // Could happen if we send multiple asks in a row i.e. with
494+ // command_output. It's important that when we know an ask could
495+ // fail, it is handled gracefully.
496+ throw new Error ( "Current ask promise was ignored" )
497+ }
485498
486499 const result = { response : this . askResponse ! , text : this . askResponseText , images : this . askResponseImages }
487500 this . askResponse = undefined
@@ -524,13 +537,16 @@ export class Cline extends EventEmitter<ClineEvents> {
524537 } else {
525538 // this is a new partial message, so add it with partial state
526539 const sayTs = Date . now ( )
540+ this . lastMessageTs = sayTs
527541 await this . addToClineMessages ( { ts : sayTs , type : "say" , say : type , text, images, partial } )
528542 }
529543 } else {
530544 // New now have a complete version of a previously partial message.
531545 if ( isUpdatingPreviousPartial ) {
532546 // This is the complete version of a previously partial
533547 // message, so replace the partial with the complete version.
548+ this . lastMessageTs = lastMessage . ts
549+ // lastMessage.ts = sayTs
534550 lastMessage . text = text
535551 lastMessage . images = images
536552 lastMessage . partial = false
@@ -543,12 +559,14 @@ export class Cline extends EventEmitter<ClineEvents> {
543559 } else {
544560 // This is a new and complete message, so add it like normal.
545561 const sayTs = Date . now ( )
562+ this . lastMessageTs = sayTs
546563 await this . addToClineMessages ( { ts : sayTs , type : "say" , say : type , text, images } )
547564 }
548565 }
549566 } else {
550567 // this is a new non-partial message, so add it like normal
551568 const sayTs = Date . now ( )
569+ this . lastMessageTs = sayTs
552570 await this . addToClineMessages ( { ts : sayTs , type : "say" , say : type , text, images, checkpoint } )
553571 }
554572 }
0 commit comments