feat(ai): add partial JSON parsing for streaming tool calls

- Added partial-json package for parsing incomplete JSON during streaming
- Tool call arguments now contain partially parsed JSON during toolcall_delta events
- Enables progressive UI updates (e.g., showing file paths before content is complete)
- Arguments are always valid objects (minimum empty {}), never undefined
- Full validation still occurs at toolcall_end when arguments are complete
- Updated all providers (Anthropic, OpenAI Completions/Responses) to use parseStreamingJson
- Added comprehensive documentation and examples in README
- Added test to verify arguments are always defined during streaming
This commit is contained in:
Mario Zechner 2025-09-16 12:23:34 +02:00
parent 197259c88a
commit 39c626b6c9
10 changed files with 208 additions and 69 deletions

11
package-lock.json generated
View file

@ -1996,6 +1996,12 @@
}
}
},
"node_modules/partial-json": {
"version": "0.1.7",
"resolved": "https://registry.npmjs.org/partial-json/-/partial-json-0.1.7.tgz",
"integrity": "sha512-Njv/59hHaokb/hRUjce3Hdv12wd60MtM9Z5Olmn+nehe0QDAsRtRbJPvJ0Z91TusF0SuZRIvnM+S4l6EIP8leA==",
"license": "MIT"
},
"node_modules/pathe": {
"version": "2.0.3",
"resolved": "https://registry.npmjs.org/pathe/-/pathe-2.0.3.tgz",
@ -2816,7 +2822,7 @@
"version": "0.5.39",
"license": "MIT",
"dependencies": {
"@mariozechner/pi-tui": "^0.5.38",
"@mariozechner/pi-tui": "^0.5.39",
"@types/glob": "^8.1.0",
"chalk": "^5.5.0",
"glob": "^11.0.3",
@ -3205,6 +3211,7 @@
"ajv-formats": "^3.0.1",
"chalk": "^5.6.2",
"openai": "^5.20.0",
"partial-json": "^0.1.7",
"zod-to-json-schema": "^3.24.6"
},
"devDependencies": {
@ -3238,7 +3245,7 @@
"version": "0.5.39",
"license": "MIT",
"dependencies": {
"@mariozechner/pi-agent": "^0.5.38",
"@mariozechner/pi-agent": "^0.5.39",
"chalk": "^5.5.0"
},
"bin": {