Skip to content

The agent that grows with you

fix(desktop): drop provider-echoed duplicate text so replies don't render twice

Brooklyn Nicholsond690e0220e3d
diff --git a/apps/desktop/src/lib/chat-messages.ts b/apps/desktop/src/lib/chat-messages.ts
index 52eb1cf1e27..3f6fd28b628 100644
--- a/apps/desktop/src/lib/chat-messages.ts
+++ b/apps/desktop/src/lib/chat-messages.ts
@@ -250,6 +250,41 @@ export function collectUnspokenTurnSpeech(
const normalizeWs = (value: string) => value.replace(/\s+/g, ' ').trim()
+/**
+ * Drop earlier text parts that a later text part repeats verbatim (after
+ * whitespace normalization). Providers that continue a turn after a tool
+ * call sometimes re-send the previous assistant text as the next message's
+ * prefix (tool_calls row, then a stop row with identical prose) — the turn
+ * merge then holds the same paragraph twice and everything in it renders
+ * twice, most visibly ::preview frames. The LAST occurrence is the
+ * authoritative one; keep it.
+ */
+export function dedupeRepeatedTextInParts(parts: ChatMessagePart[]): ChatMessagePart[] {
+ const lastByText = new Map<string, number>()
+
+ parts.forEach((part, index) => {
+ if (part.type === 'text') {
+ const key = normalizeWs(part.text)
+
+ if (key) {
+ lastByText.set(key, index)
+ }
+ }
+ })
+
+ const dropped = parts.filter((part, index) => {
+ if (part.type !== 'text') {
+ return true
+ }
+
+ const key = normalizeWs(part.text)
+
+ return !key || lastByText.get(key) === index
+ })
+
+ return dropped.length === parts.length ? parts : dropped
+}
+
/**
* Merge the final assistant text into a message's parts.
*
@@ -1242,7 +1277,9 @@ export function toChatMessages(messages: SessionMessage[]): ChatMessage[] {
flushPendingTools(messages.length)
const withoutGeneratedImageEchoes = result.map(message =>
- message.role === 'assistant' ? { ...message, parts: dedupeGeneratedImageEchoesInParts(message.parts) } : message
+ message.role === 'assistant'
+ ? { ...message, parts: dedupeRepeatedTextInParts(dedupeGeneratedImageEchoesInParts(message.parts)) }
+ : message
)
return withUniqueToolCallIds(