🐛 fix: extract JSON from turn_end in PiAgent json mode NDJSON stream#85
Open
SamuelLHuber wants to merge 1 commit intoevmts:mainfrom
Open
🐛 fix: extract JSON from turn_end in PiAgent json mode NDJSON stream#85SamuelLHuber wants to merge 1 commit intoevmts:mainfrom
SamuelLHuber wants to merge 1 commit intoevmts:mainfrom
Conversation
When PiAgent uses mode: 'json', pi outputs NDJSON with session metadata as the first object. The actual response is in the turn_end event's message.content. Previously, tryParseJson() would fail on the entire stdout since it's not a single JSON object. The first line (session metadata) would be parsed, resulting in wrong output. This adds extractTextFromPiNdjson() which scans the NDJSON stream backwards to find turn_end or agent_end events and extracts the assistant message content.
Contributor
Author
CI Status NoteThe CI failures are pre-existing issues on the
These same 12 tests fail on Verification
|
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.
Problem
The usage of Dynamic Task Discovery with PiAgent is not working due to Pi's output not being parsed correctly into a task list.
When
PiAgentusesmode: "json", pi outputs NDJSON (newline-delimited JSON) where:{"type":"session",...}turn_endcontaining the actual responsePreviously, the code tried to parse the entire stdout as a single JSON object, which failed. The first line (session metadata) would be returned instead of the actual response.
Solution
Added
extractTextFromPiNdjson()which:turn_endoragent_endeventsChanges
src/agents/cli.ts: AddextractTextFromPiNdjson()function, updatePiAgent.generate()tests/pi-support.test.ts: Add 3 tests for NDJSON extractionTesting
--mode jsonExample
Before:
After: