-
Notifications
You must be signed in to change notification settings - Fork 14
fix(agent): stream tool input as it arrives during execution #1994
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| import { describe, expect, it } from "vitest"; | ||
| import { tryParsePartialJson } from "./partial-json"; | ||
|
|
||
| describe("tryParsePartialJson", () => { | ||
| it("returns null for empty / whitespace input", () => { | ||
| expect(tryParsePartialJson("")).toBeNull(); | ||
| expect(tryParsePartialJson(" ")).toBeNull(); | ||
| }); | ||
|
|
||
| it("parses complete JSON unchanged", () => { | ||
| expect(tryParsePartialJson('{"a":1}')).toEqual({ a: 1 }); | ||
| expect(tryParsePartialJson("[1,2,3]")).toEqual([1, 2, 3]); | ||
| expect(tryParsePartialJson('"hello"')).toBe("hello"); | ||
| }); | ||
|
skoob13 marked this conversation as resolved.
|
||
|
|
||
| it("closes a single open object", () => { | ||
| expect(tryParsePartialJson("{")).toEqual({}); | ||
| }); | ||
|
|
||
| it("closes a partial string value and the surrounding object", () => { | ||
| expect(tryParsePartialJson('{"command": "call execute-')).toEqual({ | ||
| command: "call execute-", | ||
| }); | ||
| }); | ||
|
|
||
| it("closes a complete string value with no closing brace", () => { | ||
| expect(tryParsePartialJson('{"command": "tools"')).toEqual({ | ||
| command: "tools", | ||
| }); | ||
| }); | ||
|
|
||
| it("strips a trailing comma after a complete entry", () => { | ||
| expect(tryParsePartialJson('{"a": 1,')).toEqual({ a: 1 }); | ||
| }); | ||
|
|
||
| it("drops a trailing partial key with no value", () => { | ||
| expect(tryParsePartialJson('{"a": 1, "b":')).toEqual({ a: 1 }); | ||
| expect(tryParsePartialJson('{"a": 1, "b"')).toEqual({ a: 1 }); | ||
| }); | ||
|
|
||
| it("handles nested objects and arrays mid-stream", () => { | ||
| expect(tryParsePartialJson('{"q": {"sql": "SELECT 1')).toEqual({ | ||
| q: { sql: "SELECT 1" }, | ||
| }); | ||
| expect(tryParsePartialJson('{"items": [1, 2, 3')).toEqual({ | ||
| items: [1, 2, 3], | ||
| }); | ||
| }); | ||
|
|
||
| it("respects escaped quotes inside strings", () => { | ||
| expect(tryParsePartialJson('{"q": "say \\"hi\\"')).toEqual({ | ||
| q: 'say "hi"', | ||
| }); | ||
| }); | ||
|
|
||
| it("returns null when nothing parseable can be reconstructed", () => { | ||
| // Garbage that can't be balanced into valid JSON. | ||
| expect(tryParsePartialJson("not json at all")).toBeNull(); | ||
| }); | ||
|
|
||
| it("parses a typical exec command incrementally", () => { | ||
| // Simulate growth of a streamed { command: "call dashboard-update {...}" } | ||
| expect(tryParsePartialJson('{"command":')).toEqual({}); | ||
| expect(tryParsePartialJson('{"command": "ca')).toEqual({ command: "ca" }); | ||
| expect( | ||
| tryParsePartialJson('{"command": "call dashboard-update {\\"id\\":'), | ||
| ).toEqual({ command: 'call dashboard-update {"id":' }); | ||
| expect( | ||
| tryParsePartialJson('{"command": "call dashboard-update {\\"id\\": 1}"}'), | ||
| ).toEqual({ command: 'call dashboard-update {"id": 1}' }); | ||
| }); | ||
| }); | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| /** | ||
| * Best-effort parser for incomplete JSON streamed via Anthropic's | ||
| * `input_json_delta` events. Used to surface tool inputs while they are still | ||
| * being generated so the UI can show the args during execution instead of | ||
| * waiting for the finalized assistant message. | ||
| * | ||
| * Strategy: walk the input tracking open `{`/`[` and quote/escape state, then | ||
| * try a few completions in order of likelihood (close any open string, drop | ||
| * trailing commas/colons or partial keys, then close any open brackets). | ||
| * Returns `null` when no completion parses — callers should silently skip | ||
| * that delta and wait for more input. | ||
| */ | ||
| export function tryParsePartialJson(s: string): unknown { | ||
| const trimmed = s.trim(); | ||
| if (!trimmed) return null; | ||
|
|
||
| // Fast path: complete JSON. | ||
| try { | ||
| return JSON.parse(trimmed); | ||
| } catch {} | ||
|
|
||
| const closers: string[] = []; | ||
| let inString = false; | ||
| let escaped = false; | ||
|
|
||
| for (let i = 0; i < trimmed.length; i++) { | ||
| const ch = trimmed[i]; | ||
| if (inString) { | ||
| if (escaped) { | ||
| escaped = false; | ||
| } else if (ch === "\\") { | ||
| escaped = true; | ||
| } else if (ch === '"') { | ||
| inString = false; | ||
| } | ||
| continue; | ||
| } | ||
| if (ch === '"') inString = true; | ||
| else if (ch === "{") closers.push("}"); | ||
| else if (ch === "[") closers.push("]"); | ||
| else if (ch === "}" || ch === "]") closers.pop(); | ||
| } | ||
|
|
||
| const closeBrackets = (str: string): string => { | ||
| let out = str; | ||
| for (let i = closers.length - 1; i >= 0; i--) out += closers[i]; | ||
| return out; | ||
| }; | ||
|
|
||
| const candidates: string[] = []; | ||
|
|
||
| // 1. Close any open string + brackets. | ||
| const closedString = inString ? `${trimmed}"` : trimmed; | ||
| candidates.push(closeBrackets(closedString)); | ||
|
|
||
| // 2. Drop trailing partial token (comma, colon, or `"key":`/`"key"`) | ||
| // and close brackets. | ||
| let stripped = closedString.replace(/[,:]\s*$/, ""); | ||
| stripped = stripped.replace(/,?\s*"[^"]*"\s*:?\s*$/, ""); | ||
| candidates.push(closeBrackets(stripped)); | ||
|
|
||
| for (const candidate of candidates) { | ||
| try { | ||
| return JSON.parse(candidate); | ||
| } catch {} | ||
| } | ||
| return null; | ||
| } |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.