fix(vscode-ide-companion): optimize stream termination handling and remove timeout for session_prompt

This commit is contained in:
yiliang114
2025-12-17 21:00:26 +08:00
parent 725843f9b3
commit 12f84fb730
2 changed files with 24 additions and 19 deletions

View File

@@ -54,27 +54,31 @@ export class AcpSessionManager {
}; };
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
// different timeout durations based on methods // No timeout for session_prompt as LLM tasks can take 5-10 minutes or longer
let timeoutDuration = 60000; // default 60 seconds // The request should always terminate with a stop_reason
if ( let timeoutId: NodeJS.Timeout | undefined;
method === AGENT_METHODS.session_prompt || let timeoutDuration: number | undefined;
method === AGENT_METHODS.initialize
) {
timeoutDuration = 120000; // 2min for session_prompt and initialize
}
const timeoutId = setTimeout(() => { if (method !== AGENT_METHODS.session_prompt) {
// Set timeout for other methods
timeoutDuration = method === AGENT_METHODS.initialize ? 120000 : 60000;
timeoutId = setTimeout(() => {
pendingRequests.delete(id); pendingRequests.delete(id);
reject(new Error(`Request ${method} timed out`)); reject(new Error(`Request ${method} timed out`));
}, timeoutDuration); }, timeoutDuration);
}
const pendingRequest: PendingRequest<T> = { const pendingRequest: PendingRequest<T> = {
resolve: (value: T) => { resolve: (value: T) => {
if (timeoutId) {
clearTimeout(timeoutId); clearTimeout(timeoutId);
}
resolve(value); resolve(value);
}, },
reject: (error: Error) => { reject: (error: Error) => {
if (timeoutId) {
clearTimeout(timeoutId); clearTimeout(timeoutId);
}
reject(error); reject(error);
}, },
timeoutId, timeoutId,

View File

@@ -440,24 +440,25 @@ export class SessionMessageHandler extends BaseMessageHandler {
const isTimeoutError = const isTimeoutError =
lower.includes('timeout') || lower.includes('timed out'); lower.includes('timeout') || lower.includes('timed out');
if (isTimeoutError) { if (isTimeoutError) {
// Timeout has no action for the user to perform, so only reset the information in the panel and no VS Code error alert will pop up. // Note: session_prompt no longer has a timeout, so this should rarely occur
// This path may still be hit for other methods (initialize, etc.) or network-level timeouts
console.warn( console.warn(
'[SessionMessageHandler] Prompt timed out; suppressing popup', '[SessionMessageHandler] Request timed out; suppressing popup',
); );
const timeoutMessage: ChatMessage = { const timeoutMessage: ChatMessage = {
role: 'assistant', role: 'assistant',
content: content:
'Request timed out (no response within 120 seconds). Please try again or simplify the request.', 'Request timed out. This may be due to a network issue. Please try again.',
timestamp: Date.now(), timestamp: Date.now(),
}; };
// Send a timeout message to the WebView without terminating the stream // Send a timeout message to the WebView
// In this way, the long-term task can continue to receive subsequent messages after timeout.
this.sendToWebView({ this.sendToWebView({
type: 'message', type: 'message',
data: timeoutMessage, data: timeoutMessage,
}); });
this.sendStreamEnd('timeout');
} else { } else {
// Handling of Non-Timeout Errors // Handling of Non-Timeout Errors
vscode.window.showErrorMessage(`Error sending message: ${error}`); vscode.window.showErrorMessage(`Error sending message: ${error}`);