@@ -142,7 +142,6 @@ export class Cline extends EventEmitter<ClineEvents> {
142142 private askResponse ?: ClineAskResponse
143143 private askResponseText ?: string
144144 private askResponseImages ?: string [ ]
145- private lastMessageTs ?: number
146145 // Not private since it needs to be accessible by tools
147146 consecutiveMistakeCount : number = 0
148147 consecutiveMistakeCountForApplyDiff : Map < string , number > = new Map ( )
@@ -333,7 +332,17 @@ export class Cline extends EventEmitter<ClineEvents> {
333332 }
334333
335334 private async addToClineMessages ( message : ClineMessage ) {
336- this . clineMessages . push ( message )
335+ // Find the correct position to insert the message based on timestamp
336+ const insertIndex = this . clineMessages . findIndex ( ( existingMsg ) => existingMsg . ts > message . ts )
337+
338+ if ( insertIndex === - 1 ) {
339+ // If no message with a later timestamp is found, append to the end
340+ this . clineMessages . push ( message )
341+ } else {
342+ // Insert the message at the correct position to maintain chronological order
343+ this . clineMessages . splice ( insertIndex , 0 , message )
344+ }
345+
337346 await this . providerRef . deref ( ) ?. postStateToWebview ( )
338347 this . emit ( "message" , { action : "created" , message } )
339348 await this . saveClineMessages ( )
@@ -441,7 +450,6 @@ export class Cline extends EventEmitter<ClineEvents> {
441450 // This is a new partial message, so add it with partial
442451 // state.
443452 askTs = Date . now ( )
444- this . lastMessageTs = askTs
445453 await this . addToClineMessages ( { ts : askTs , type : "ask" , ask : type , text, partial } )
446454 throw new Error ( "Current ask promise was ignored (#2)" )
447455 }
@@ -460,8 +468,6 @@ export class Cline extends EventEmitter<ClineEvents> {
460468 So in this case we must make sure that the message ts is never altered after first setting it.
461469 */
462470 askTs = lastMessage . ts
463- this . lastMessageTs = askTs
464- // lastMessage.ts = askTs
465471 lastMessage . text = text
466472 lastMessage . partial = false
467473 lastMessage . progressStatus = progressStatus
@@ -473,7 +479,6 @@ export class Cline extends EventEmitter<ClineEvents> {
473479 this . askResponseText = undefined
474480 this . askResponseImages = undefined
475481 askTs = Date . now ( )
476- this . lastMessageTs = askTs
477482 await this . addToClineMessages ( { ts : askTs , type : "ask" , ask : type , text } )
478483 }
479484 }
@@ -483,18 +488,10 @@ export class Cline extends EventEmitter<ClineEvents> {
483488 this . askResponseText = undefined
484489 this . askResponseImages = undefined
485490 askTs = Date . now ( )
486- this . lastMessageTs = askTs
487491 await this . addToClineMessages ( { ts : askTs , type : "ask" , ask : type , text } )
488492 }
489493
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- }
494+ await pWaitFor ( ( ) => this . askResponse !== undefined , { interval : 100 } )
498495
499496 const result = { response : this . askResponse ! , text : this . askResponseText , images : this . askResponseImages }
500497 this . askResponse = undefined
@@ -522,6 +519,8 @@ export class Cline extends EventEmitter<ClineEvents> {
522519 throw new Error ( `[Cline#say] task ${ this . taskId } .${ this . instanceId } aborted` )
523520 }
524521
522+ const sayTs = ( checkpoint ?. startTime as number ) ?? Date . now ( )
523+
525524 if ( partial !== undefined ) {
526525 const lastMessage = this . clineMessages . at ( - 1 )
527526 const isUpdatingPreviousPartial =
@@ -536,17 +535,13 @@ export class Cline extends EventEmitter<ClineEvents> {
536535 this . updateClineMessage ( lastMessage )
537536 } else {
538537 // this is a new partial message, so add it with partial state
539- const sayTs = Date . now ( )
540- this . lastMessageTs = sayTs
541538 await this . addToClineMessages ( { ts : sayTs , type : "say" , say : type , text, images, partial } )
542539 }
543540 } else {
544541 // New now have a complete version of a previously partial message.
545542 if ( isUpdatingPreviousPartial ) {
546543 // This is the complete version of a previously partial
547544 // message, so replace the partial with the complete version.
548- this . lastMessageTs = lastMessage . ts
549- // lastMessage.ts = sayTs
550545 lastMessage . text = text
551546 lastMessage . images = images
552547 lastMessage . partial = false
@@ -558,15 +553,11 @@ export class Cline extends EventEmitter<ClineEvents> {
558553 this . updateClineMessage ( lastMessage )
559554 } else {
560555 // This is a new and complete message, so add it like normal.
561- const sayTs = Date . now ( )
562- this . lastMessageTs = sayTs
563556 await this . addToClineMessages ( { ts : sayTs , type : "say" , say : type , text, images } )
564557 }
565558 }
566559 } else {
567560 // this is a new non-partial message, so add it like normal
568- const sayTs = Date . now ( )
569- this . lastMessageTs = sayTs
570561 await this . addToClineMessages ( { ts : sayTs , type : "say" , say : type , text, images, checkpoint } )
571562 }
572563 }
@@ -2403,14 +2394,16 @@ export class Cline extends EventEmitter<ClineEvents> {
24032394 }
24042395 } )
24052396
2406- service . on ( "checkpoint" , ( { isFirst, fromHash : from , toHash : to } ) => {
2397+ service . on ( "checkpoint" , ( { isFirst, fromHash : from , toHash : to , startTime } ) => {
24072398 try {
24082399 this . providerRef . deref ( ) ?. postMessageToWebview ( { type : "currentCheckpointUpdated" , text : to } )
24092400
2410- this . say ( "checkpoint_saved" , to , undefined , undefined , { isFirst, from, to } ) . catch ( ( err ) => {
2411- log ( "[Cline#initializeCheckpoints] caught unexpected error in say('checkpoint_saved')" )
2412- console . error ( err )
2413- } )
2401+ this . say ( "checkpoint_saved" , to , undefined , undefined , { isFirst, from, to, startTime } ) . catch (
2402+ ( err ) => {
2403+ log ( "[Cline#initializeCheckpoints] caught unexpected error in say('checkpoint_saved')" )
2404+ console . error ( err )
2405+ } ,
2406+ )
24142407 } catch ( err ) {
24152408 log (
24162409 "[Cline#initializeCheckpoints] caught unexpected error in on('checkpoint'), disabling checkpoints" ,
0 commit comments