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:
1 parent
7240517807
commit
89629b8f03
1 file changed
+12
-4
@@ -44,11 +44,19 @@
|
|||||||
const componentId = id || uuidv4();
|
const componentId = id || uuidv4();
|
||||||
|
|
||||||
function parseJSONString(str: string) {
|
function parseJSONString(str: string) {
|
||||||
try {
|
// Iteratively unwrap nested JSON-encoded strings. Same result as the previous
|
||||||
return parseJSONString(JSON.parse(str));
|
// recursive form, but without the stack-overflow-and-recover path it hit on
|
||||||
} catch (e) {
|
// scalar values (e.g. JSON.parse('5') -> 5 -> infinite self-recursion).
|
||||||
return str;
|
// 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) {
|
function formatJSONString(str: string) {
|
||||||
|
|||||||
Reference in new issue
Block a user