From 79631aa7f5ab60e2b6f7bdc6097c33b49d8f4d48 Mon Sep 17 00:00:00 2001 From: Saoud Rizwan <7799382+saoudrizwan@users.noreply.github.com> Date: Tue, 10 Sep 2024 19:28:08 -0400 Subject: [PATCH] Use compiling markers to set process as hot for longer until new output comes back --- src/ClaudeDev.ts | 2 +- src/integrations/TerminalManager.ts | 42 ++++++++++++++++++++++------- 2 files changed, 34 insertions(+), 10 deletions(-) diff --git a/src/ClaudeDev.ts b/src/ClaudeDev.ts index f52c691..d75869a 100644 --- a/src/ClaudeDev.ts +++ b/src/ClaudeDev.ts @@ -1790,7 +1790,7 @@ ${ await delay(500) // delay after saving file await pWaitFor(() => busyTerminals.every((t) => !this.terminalManager.isProcessHot(t.id)), { interval: 100, - timeout: 7_500, + timeout: 15_000, }).catch(() => {}) // terminals are cool, let's retrieve their output details += "\n\n# Active Terminals" diff --git a/src/integrations/TerminalManager.ts b/src/integrations/TerminalManager.ts index a7d0fc3..9838597 100644 --- a/src/integrations/TerminalManager.ts +++ b/src/integrations/TerminalManager.ts @@ -251,7 +251,8 @@ interface TerminalProcessEvents { } // how long to wait after a process outputs anything before we consider it "cool" again -const PROCESS_HOT_TIMEOUT = 3_500 +const PROCESS_HOT_TIMEOUT_NORMAL = 2_000 +const PROCESS_HOT_TIMEOUT_COMPILING = 15_000 export class TerminalProcess extends EventEmitter { waitForShellIntegration: boolean = true @@ -274,14 +275,7 @@ export class TerminalProcess extends EventEmitter { let didOutputNonCommand = false let didEmitEmptyLine = false for await (let data of stream) { - // Set to hot to stall API requests until terminal is cool again - this.isHot = true - if (this.hotTimer) { - clearTimeout(this.hotTimer) - } - this.hotTimer = setTimeout(() => { - this.isHot = false - }, PROCESS_HOT_TIMEOUT) + // 1. Process chunk and remove artifacts if (isFirstChunk) { /* The first chunk we get from this stream needs to be processed to be more human readable, ie remove vscode's custom escape sequences and identifiers, removing duplicate first char bug, etc. @@ -336,6 +330,36 @@ export class TerminalProcess extends EventEmitter { // FIXME: right now it seems that data chunks returned to us from the shell integration stream contains random commas, which from what I can tell is not the expected behavior. There has to be a better solution here than just removing all commas. data = data.replace(/,/g, "") + // 2. Set isHot depending on the command + // Set to hot to stall API requests until terminal is cool again + this.isHot = true + if (this.hotTimer) { + clearTimeout(this.hotTimer) + } + // these markers indicate the command is some kind of local dev server recompiling the app, which we want to wait for output of before sending request to claude + const compilingMarkers = ["compiling", "building", "bundling", "transpiling", "generating"] + const markerNullifiers = [ + "finish", + "complete", + "succeed", + "done", + "end", + "stop", + "exit", + "terminate", + "error", + "failed", + ] + const isCompiling = + compilingMarkers.some((marker) => command.toLowerCase().includes(marker.toLowerCase())) && + !markerNullifiers.some((nullifier) => command.toLowerCase().includes(nullifier.toLowerCase())) + this.hotTimer = setTimeout( + () => { + this.isHot = false + }, + isCompiling ? PROCESS_HOT_TIMEOUT_COMPILING : PROCESS_HOT_TIMEOUT_NORMAL + ) + // For non-immediately returning commands we want to show loading spinner right away but this wouldnt happen until it emits a line break, so as soon as we get any output we emit "" to let webview know to show spinner if (!didEmitEmptyLine && !this.fullOutput && data) { this.emit("line", "") // empty line to indicate start of command output stream