Skip to content

Commit a7d31aa

Browse files
authored
Revert "Remove the ask promise error" (#2117)
Revert "Remove the ask promise error (#2107)" This reverts commit 712ca71.
1 parent 592494f commit a7d31aa

File tree

3 files changed

+29
-23
lines changed

3 files changed

+29
-23
lines changed

src/core/Cline.ts

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -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()
@@ -332,17 +333,7 @@ export class Cline extends EventEmitter<ClineEvents> {
332333
}
333334

334335
private async addToClineMessages(message: ClineMessage) {
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-
336+
this.clineMessages.push(message)
346337
await this.providerRef.deref()?.postStateToWebview()
347338
this.emit("message", { action: "created", message })
348339
await this.saveClineMessages()
@@ -450,6 +441,7 @@ export class Cline extends EventEmitter<ClineEvents> {
450441
// This is a new partial message, so add it with partial
451442
// state.
452443
askTs = Date.now()
444+
this.lastMessageTs = askTs
453445
await this.addToClineMessages({ ts: askTs, type: "ask", ask: type, text, partial })
454446
throw new Error("Current ask promise was ignored (#2)")
455447
}
@@ -468,6 +460,8 @@ export class Cline extends EventEmitter<ClineEvents> {
468460
So in this case we must make sure that the message ts is never altered after first setting it.
469461
*/
470462
askTs = lastMessage.ts
463+
this.lastMessageTs = askTs
464+
// lastMessage.ts = askTs
471465
lastMessage.text = text
472466
lastMessage.partial = false
473467
lastMessage.progressStatus = progressStatus
@@ -479,6 +473,7 @@ export class Cline extends EventEmitter<ClineEvents> {
479473
this.askResponseText = undefined
480474
this.askResponseImages = undefined
481475
askTs = Date.now()
476+
this.lastMessageTs = askTs
482477
await this.addToClineMessages({ ts: askTs, type: "ask", ask: type, text })
483478
}
484479
}
@@ -488,10 +483,18 @@ export class Cline extends EventEmitter<ClineEvents> {
488483
this.askResponseText = undefined
489484
this.askResponseImages = undefined
490485
askTs = Date.now()
486+
this.lastMessageTs = askTs
491487
await this.addToClineMessages({ ts: askTs, type: "ask", ask: type, text })
492488
}
493489

494-
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+
}
495498

496499
const result = { response: this.askResponse!, text: this.askResponseText, images: this.askResponseImages }
497500
this.askResponse = undefined
@@ -519,8 +522,6 @@ export class Cline extends EventEmitter<ClineEvents> {
519522
throw new Error(`[Cline#say] task ${this.taskId}.${this.instanceId} aborted`)
520523
}
521524

522-
const sayTs = (checkpoint?.startTime as number) ?? Date.now()
523-
524525
if (partial !== undefined) {
525526
const lastMessage = this.clineMessages.at(-1)
526527
const isUpdatingPreviousPartial =
@@ -535,13 +536,17 @@ export class Cline extends EventEmitter<ClineEvents> {
535536
this.updateClineMessage(lastMessage)
536537
} else {
537538
// this is a new partial message, so add it with partial state
539+
const sayTs = Date.now()
540+
this.lastMessageTs = sayTs
538541
await this.addToClineMessages({ ts: sayTs, type: "say", say: type, text, images, partial })
539542
}
540543
} else {
541544
// New now have a complete version of a previously partial message.
542545
if (isUpdatingPreviousPartial) {
543546
// This is the complete version of a previously partial
544547
// message, so replace the partial with the complete version.
548+
this.lastMessageTs = lastMessage.ts
549+
// lastMessage.ts = sayTs
545550
lastMessage.text = text
546551
lastMessage.images = images
547552
lastMessage.partial = false
@@ -553,11 +558,15 @@ export class Cline extends EventEmitter<ClineEvents> {
553558
this.updateClineMessage(lastMessage)
554559
} else {
555560
// This is a new and complete message, so add it like normal.
561+
const sayTs = Date.now()
562+
this.lastMessageTs = sayTs
556563
await this.addToClineMessages({ ts: sayTs, type: "say", say: type, text, images })
557564
}
558565
}
559566
} else {
560567
// this is a new non-partial message, so add it like normal
568+
const sayTs = Date.now()
569+
this.lastMessageTs = sayTs
561570
await this.addToClineMessages({ ts: sayTs, type: "say", say: type, text, images, checkpoint })
562571
}
563572
}
@@ -2394,16 +2403,14 @@ export class Cline extends EventEmitter<ClineEvents> {
23942403
}
23952404
})
23962405

2397-
service.on("checkpoint", ({ isFirst, fromHash: from, toHash: to, startTime }) => {
2406+
service.on("checkpoint", ({ isFirst, fromHash: from, toHash: to }) => {
23982407
try {
23992408
this.providerRef.deref()?.postMessageToWebview({ type: "currentCheckpointUpdated", text: to })
24002409

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-
)
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+
})
24072414
} catch (err) {
24082415
log(
24092416
"[Cline#initializeCheckpoints] caught unexpected error in on('checkpoint'), disabling checkpoints",

src/services/checkpoints/ShadowCheckpointService.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ export abstract class ShadowCheckpointService extends EventEmitter {
218218
const duration = Date.now() - startTime
219219

220220
if (isFirst || result.commit) {
221-
this.emit("checkpoint", { type: "checkpoint", isFirst, fromHash, toHash, duration, startTime })
221+
this.emit("checkpoint", { type: "checkpoint", isFirst, fromHash, toHash, duration })
222222
}
223223

224224
if (result.commit) {

src/services/checkpoints/types.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ export interface CheckpointEventMap {
2929
fromHash: string
3030
toHash: string
3131
duration: number
32-
startTime: number
3332
}
3433
restore: { type: "restore"; commitHash: string; duration: number }
3534
error: { type: "error"; error: Error }

0 commit comments

Comments
 (0)