Просмотр исходного кода

Merge pull request #949 from RooVetGit/cte/fix-diff-editor-revert-on-abort

Fix file revert on abort
Chris Estreich 11 месяцев назад
Родитель
Сommit
d264befa32
1 измененных файлов с 17 добавлено и 5 удалено
  1. 17 5
      src/core/Cline.ts

+ 17 - 5
src/core/Cline.ts

@@ -732,13 +732,18 @@ export class Cline {
 	}
 	}
 
 
 	async abortTask() {
 	async abortTask() {
-		this.abort = true // Will stop any autonomously running promises.
+		// Will stop any autonomously running promises.
+		this.abort = true
+
 		this.terminalManager.disposeAll()
 		this.terminalManager.disposeAll()
 		this.urlContentFetcher.closeBrowser()
 		this.urlContentFetcher.closeBrowser()
 		this.browserSession.closeBrowser()
 		this.browserSession.closeBrowser()
-		// Need to await for when we want to make sure directories/files are
-		// reverted before re-starting the task from a checkpoint.
-		await this.diffViewProvider.revertChanges()
+
+		// If we're not streaming then `abortStream` (which reverts the diff
+		// view changes) won't be called, so we need to revert the changes here.
+		if (this.isStreaming && this.diffViewProvider.isEditing) {
+			await this.diffViewProvider.revertChanges()
+		}
 	}
 	}
 
 
 	// Tools
 	// Tools
@@ -2817,6 +2822,8 @@ export class Cline {
 			}
 			}
 
 
 			const abortStream = async (cancelReason: ClineApiReqCancelReason, streamingFailedMessage?: string) => {
 			const abortStream = async (cancelReason: ClineApiReqCancelReason, streamingFailedMessage?: string) => {
+				console.log(`[Cline#abortStream] cancelReason = ${cancelReason}`)
+
 				if (this.diffViewProvider.isEditing) {
 				if (this.diffViewProvider.isEditing) {
 					await this.diffViewProvider.revertChanges() // closes diff view
 					await this.diffViewProvider.revertChanges() // closes diff view
 				}
 				}
@@ -2871,6 +2878,7 @@ export class Cline {
 			const stream = this.attemptApiRequest(previousApiReqIndex) // yields only if the first chunk is successful, otherwise will allow the user to retry the request (most likely due to rate limit error, which gets thrown on the first chunk)
 			const stream = this.attemptApiRequest(previousApiReqIndex) // yields only if the first chunk is successful, otherwise will allow the user to retry the request (most likely due to rate limit error, which gets thrown on the first chunk)
 			let assistantMessage = ""
 			let assistantMessage = ""
 			let reasoningMessage = ""
 			let reasoningMessage = ""
+			this.isStreaming = true
 			try {
 			try {
 				for await (const chunk of stream) {
 				for await (const chunk of stream) {
 					if (!chunk) {
 					if (!chunk) {
@@ -2903,11 +2911,13 @@ export class Cline {
 					}
 					}
 
 
 					if (this.abort) {
 					if (this.abort) {
-						console.log("aborting stream...")
+						console.log(`aborting stream, this.abandoned = ${this.abandoned}`)
+
 						if (!this.abandoned) {
 						if (!this.abandoned) {
 							// only need to gracefully abort if this instance isn't abandoned (sometimes openrouter stream hangs, in which case this would affect future instances of cline)
 							// only need to gracefully abort if this instance isn't abandoned (sometimes openrouter stream hangs, in which case this would affect future instances of cline)
 							await abortStream("user_cancelled")
 							await abortStream("user_cancelled")
 						}
 						}
+
 						break // aborts the stream
 						break // aborts the stream
 					}
 					}
 
 
@@ -2940,6 +2950,8 @@ export class Cline {
 						// await this.providerRef.deref()?.postStateToWebview()
 						// await this.providerRef.deref()?.postStateToWebview()
 					}
 					}
 				}
 				}
+			} finally {
+				this.isStreaming = false
 			}
 			}
 
 
 			// need to call here in case the stream was aborted
 			// need to call here in case the stream was aborted