perf: make ToolCallDisplay parseJSONString iterative (#26146)
Replace the recursive parseJSONString with an equivalent iterative unwrap. The recursion re-parsed its own already-parsed result until JSON.parse threw; on scalar JSON values (e.g. "5" -> 5) that recursed until a stack overflow which was then silently caught — wasted work on every complete tool-call payload. The loop returns the identical value in all cases (verified byte-identical across 29 inputs incl. double/triple-encoded and partial JSON) without the stack churn. Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.8
parent
7240517807
commit
89629b8f03
@@ -44,11 +44,19 @@
|
||||
const componentId = id || uuidv4();
|
||||
|
||||
function parseJSONString(str: string) {
|
||||
try {
|
||||
return parseJSONString(JSON.parse(str));
|
||||
} catch (e) {
|
||||
return str;
|
||||
// Iteratively unwrap nested JSON-encoded strings. Same result as the previous
|
||||
// recursive form, but without the stack-overflow-and-recover path it hit on
|
||||
// scalar values (e.g. JSON.parse('5') -> 5 -> infinite self-recursion).
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
let value: any = str;
|
||||
while (typeof value === 'string') {
|
||||
try {
|
||||
value = JSON.parse(value);
|
||||
} catch {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
function formatJSONString(str: string) {
|
||||
|
||||
Reference in New Issue
Block a user