From cf7204118ff47b0469bc9cfdb04971d14c1d9710 Mon Sep 17 00:00:00 2001 From: Shaojin Wen Date: Sat, 14 Mar 2026 17:46:54 +0800 Subject: [PATCH] fix(core): avoid corrupting JSON strings in SSE whitespace normalization Replace broad \s+} and \s+] regexes with a narrower pattern that only strips whitespace before closing braces/brackets when preceded by a JSON value terminator (", digit, ] or }). Prevents mangling string values like "hello }" which contain whitespace before braces. Co-Authored-By: Claude Opus 4.6 (1M context) --- packages/core/src/utils/anthropicSseParser.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/core/src/utils/anthropicSseParser.ts b/packages/core/src/utils/anthropicSseParser.ts index 4162ce658..f40204028 100644 --- a/packages/core/src/utils/anthropicSseParser.ts +++ b/packages/core/src/utils/anthropicSseParser.ts @@ -111,10 +111,10 @@ export function parseAnthropicSseData( // // Try to fix by removing whitespace before } and ] - // Remove trailing whitespace before closing braces - normalizedData = normalizedData.replace(/\s+}/g, '}'); - // Remove trailing whitespace before closing brackets - normalizedData = normalizedData.replace(/\s+]/g, ']'); + // Remove trailing whitespace before closing braces/brackets, but only + // when preceded by a JSON value terminator (" or digit or ] or }) + // to avoid corrupting whitespace inside string values like "hello }". + normalizedData = normalizedData.replace(/(["\d\]}])\s+([\]}])/g, '$1$2'); try { return JSON.parse(normalizedData) as AnthropicStreamEvent;