Skip to content

perf(ai-chat-ui): skip doomed JSON.parse on streaming tool arguments - #17975

Open
safisa wants to merge 1 commit into
eclipse-theia:masterfrom
safisa:safi/toolcall-utils-streaming-parse
Open

perf(ai-chat-ui): skip doomed JSON.parse on streaming tool arguments#17975
safisa wants to merge 1 commit into
eclipse-theia:masterfrom
safisa:safi/toolcall-utils-streaming-parse

Conversation

@safisa

@safisa safisa commented Aug 31, 2026

Copy link
Copy Markdown
Contributor

What it does

Fixes #17974.

extractJsonStringField tried JSON.parse first 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 through JSON.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

  1. cd packages/ai-chat-ui && npx mocha --config ../../configs/mocharc.yml "./lib/**/*.spec.js" — 278 passing, including 3 new boundary tests.
  2. In agent mode, have an agent call writeFileContent with a large content argument and profile the frontend while the arguments stream: JSON.parse under extractJsonStringField no longer shows up.

Follow-ups

The regex still matches the first "<field>": "..." occurrence anywhere in the string, so a content value that itself contains e.g. a "path" property can show a wrong label mid-stream (cosmetic, pre-existing; noted in #17974).

Breaking changes

  • This PR introduces breaking changes and requires careful review. If yes, the breaking changes section in the changelog has been updated.

Attribution

Review checklist

Reminder for reviewers

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
@github-project-automation github-project-automation Bot moved this to Waiting on reviewers in PR Backlog Aug 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Waiting on reviewers

Development

Successfully merging this pull request may close these issues.

ai-chat-ui: extractJsonStringField parses the full string before its regex fallback

1 participant