From c9f23076c2aa49a45d70211bb83ae166e2ffcfae Mon Sep 17 00:00:00 2001 From: Saoud Rizwan <7799382+saoudrizwan@users.noreply.github.com> Date: Wed, 3 Dec 2025 19:18:17 -0800 Subject: [PATCH] fix: consolidate successive error retry messages in chat UI (#7880) When API requests fail and auto-retry is enabled, multiple error_retry messages were shown (e.g., "Attempt 1 of 3", "Attempt 2 of 3", etc.). This change consolidates them to only show the latest retry message, reducing visual clutter during retry sequences. --- src/shared/combineErrorRetryMessages.ts | 46 +++++++++++++++++++++ webview-ui/src/components/chat/ChatView.tsx | 3 +- 2 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 src/shared/combineErrorRetryMessages.ts diff --git a/src/shared/combineErrorRetryMessages.ts b/src/shared/combineErrorRetryMessages.ts new file mode 100644 index 00000000..3716fa22 --- /dev/null +++ b/src/shared/combineErrorRetryMessages.ts @@ -0,0 +1,46 @@ +import { ClineMessage } from "./ExtensionMessage" + +/** + * Consolidates error_retry messages in a retry sequence, keeping only the latest one. + * + * When an API request fails and auto-retry is enabled, multiple error_retry messages are created + * (e.g., "Attempt 1 of 3", "Attempt 2 of 3", "Attempt 3 of 3"), interleaved with api_req_retried + * messages. This function filters out earlier retry messages, showing only the most recent one. + * + * @param messages - An array of ClineMessage objects to process. + * @returns A new array of ClineMessage objects with error_retry sequences consolidated. + * + * @example + * const messages: ClineMessage[] = [ + * { type: 'say', say: 'error_retry', text: '{"attempt":1,"maxAttempts":3}', ts: 1000 }, + * { type: 'say', say: 'api_req_retried', ts: 1001 }, + * { type: 'say', say: 'error_retry', text: '{"attempt":2,"maxAttempts":3}', ts: 1002 }, + * { type: 'say', say: 'api_req_retried', ts: 1003 }, + * { type: 'say', say: 'error_retry', text: '{"attempt":3,"maxAttempts":3}', ts: 1004 }, + * ]; + * const result = combineErrorRetryMessages(messages); + * // Result: [{ type: 'say', say: 'error_retry', text: '{"attempt":3,"maxAttempts":3}', ts: 1004 }] + */ +export function combineErrorRetryMessages(messages: ClineMessage[]): ClineMessage[] { + const result: ClineMessage[] = [] + + for (let i = 0; i < messages.length; i++) { + const message = messages[i] + + if (message.say === "error_retry") { + // Look ahead to see if the next non-api_req_retried message is also an error_retry + let nextMessage = messages[i + 1] + if (nextMessage?.say === "api_req_retried") { + nextMessage = messages[i + 2] + } + if (nextMessage?.say === "error_retry") { + // Skip this message, we'll show the next one (or a later one in the sequence) + continue + } + } + + result.push(message) + } + + return result +} diff --git a/webview-ui/src/components/chat/ChatView.tsx b/webview-ui/src/components/chat/ChatView.tsx index 3a6e9af2..417d3332 100644 --- a/webview-ui/src/components/chat/ChatView.tsx +++ b/webview-ui/src/components/chat/ChatView.tsx @@ -1,6 +1,7 @@ import { findLast } from "@shared/array" import { combineApiRequests } from "@shared/combineApiRequests" import { combineCommandSequences } from "@shared/combineCommandSequences" +import { combineErrorRetryMessages } from "@shared/combineErrorRetryMessages" import { combineHookSequences } from "@shared/combineHookSequences" import type { ClineApiReqInfo, ClineMessage } from "@shared/ExtensionMessage" import { getApiMetrics } from "@shared/getApiMetrics" @@ -64,7 +65,7 @@ const ChatView = ({ isHidden, showAnnouncement, hideAnnouncement, showHistoryVie // Only combine hook sequences if hooks are enabled (both user setting and feature flag) const areHooksEnabled = hooksEnabled?.user const withHooks = areHooksEnabled ? combineHookSequences(slicedMessages) : slicedMessages - return combineApiRequests(combineCommandSequences(withHooks)) + return combineErrorRetryMessages(combineApiRequests(combineCommandSequences(withHooks))) }, [messages, hooksEnabled]) // has to be after api_req_finished are all reduced into api_req_started messages const apiMetrics = useMemo(() => getApiMetrics(modifiedMessages), [modifiedMessages])