@@ -113,38 +113,48 @@ export function detectsCommonPromptPattern(cursorLine: string): IPromptDetection
113
113
export async function waitForIdleWithPromptHeuristics (
114
114
onData : Event < unknown > ,
115
115
instance : ITerminalInstance ,
116
- initialTimeoutMs : number ,
117
- extendedTimeoutMs : number = 2000
116
+ idlePollIntervalMs : number ,
117
+ extendedTimeoutMs : number ,
118
118
) : Promise < IPromptDetectionResult > {
119
- await waitForIdle ( onData , initialTimeoutMs ) ;
119
+ await waitForIdle ( onData , idlePollIntervalMs ) ;
120
120
121
- try {
122
- const xterm = await instance . xtermReadyPromise ;
123
- if ( xterm ) {
121
+ const xterm = await instance . xtermReadyPromise ;
122
+ if ( ! xterm ) {
123
+ return { detected : false , reason : `Xterm not available, using ${ idlePollIntervalMs } ms timeout` } ;
124
+ }
125
+ const startTime = Date . now ( ) ;
126
+
127
+ // Attempt to detect a prompt pattern after idle
128
+ while ( Date . now ( ) - startTime < extendedTimeoutMs ) {
129
+ try {
124
130
let content = '' ;
125
131
const buffer = xterm . raw . buffer . active ;
126
132
const line = buffer . getLine ( buffer . baseY + buffer . cursorY ) ;
127
133
if ( line ) {
128
- content = line . translateToString ( true ) + '\n' ;
134
+ content = line . translateToString ( true ) ;
129
135
}
130
-
131
- // If we detect a common prompt pattern, we're done
132
136
const promptResult = detectsCommonPromptPattern ( content ) ;
133
137
if ( promptResult . detected ) {
134
138
return promptResult ;
135
139
}
140
+ } catch ( error ) {
141
+ // Continue polling even if there's an error reading terminal content
142
+ }
143
+ await waitForIdle ( onData , Math . min ( idlePollIntervalMs , extendedTimeoutMs - ( Date . now ( ) - startTime ) ) ) ;
144
+ }
136
145
137
- // Otherwise, wait for the extended timeout period
138
- await waitForIdle ( onData , extendedTimeoutMs ) ;
139
- return { detected : false , reason : 'Extended timeout reached without prompt detection' } ;
146
+ // Extended timeout reached without detecting a prompt
147
+ try {
148
+ let content = '' ;
149
+ const buffer = xterm . raw . buffer . active ;
150
+ const line = buffer . getLine ( buffer . baseY + buffer . cursorY ) ;
151
+ if ( line ) {
152
+ content = line . translateToString ( true ) + '\n' ;
140
153
}
154
+ return { detected : false , reason : `Extended timeout reached without prompt detection. Last line: "${ content . trim ( ) } "` } ;
141
155
} catch ( error ) {
142
- // If there's an error getting terminal content, fall back to extended timeout
143
- await waitForIdle ( onData , extendedTimeoutMs ) ;
144
- return { detected : false , reason : `Error reading terminal content: ${ error } ` } ;
156
+ return { detected : false , reason : `Extended timeout reached. Error reading terminal content: ${ error } ` } ;
145
157
}
146
-
147
- return { detected : false , reason : 'Xterm not available' } ;
148
158
}
149
159
150
160
/**
0 commit comments