perf(ai-chat-ui): skip doomed JSON.parse on streaming tool arguments - #17975
Open
safisa wants to merge 1 commit into
Open
perf(ai-chat-ui): skip doomed JSON.parse on streaming tool arguments#17975safisa wants to merge 1 commit into
safisa wants to merge 1 commit into
Conversation
extractJsonStringField attempted JSON.parse on every call even while tool call arguments were still streaming, where the truncated JSON always throws after scanning the whole string - quadratic as arguments grow. Only attempt the parse once the arguments object can be complete (ends with '}') and use the regex extraction otherwise. Fixes eclipse-theia#17974
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What it does
Fixes #17974.
extractJsonStringFieldtriedJSON.parsefirst and used its regex fallback only when the parse threw. While tool call arguments stream in, the JSON is truncated, so the parse always throws — after scanning the entire string. Called per render from the tool-call label, this gets quadratic as the arguments grow (measured ~27 s of CPU for 2 MB of arguments at 64-char deltas).Now the full parse is only attempted once the arguments object can actually be complete (trimmed string ends with
}); otherwise the regex extraction runs directly. Complete JSON still goes throughJSON.parse, so proper unescaping (e.g.\", Windows paths with\\) is preserved — pinned by the existing tests. Inputs that never start with{behave exactly as before.How to test
cd packages/ai-chat-ui && npx mocha --config ../../configs/mocharc.yml "./lib/**/*.spec.js"— 278 passing, including 3 new boundary tests.writeFileContentwith a largecontentargument and profile the frontend while the arguments stream:JSON.parseunderextractJsonStringFieldno longer shows up.Follow-ups
The regex still matches the first
"<field>": "..."occurrence anywhere in the string, so acontentvalue that itself contains e.g. a"path"property can show a wrong label mid-stream (cosmetic, pre-existing; noted in #17974).Breaking changes
Attribution
Review checklist
nlsservice (for details, please see the Internationalization/Localization section in the Coding Guidelines)Reminder for reviewers