-
Notifications
You must be signed in to change notification settings - Fork 2.6k
fix: resolve Claude Code provider JSON parsing and reasoning block display #5049
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
fix: resolve Claude Code provider JSON parsing and reasoning block display #5049
Conversation
…splay - Implement proper JSON buffering to handle incomplete data chunks - Change thinking content to yield as 'reasoning' type for proper UI display - Add comprehensive tests for Claude Code provider functionality - Fix raw JSON output appearing instead of parsed content
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This pull request fixes the JSON buffering issue and corrects the display of thinking content by yielding it as a "reasoning" type, improving the integration with the Claude Code provider. The key changes include:
- Implementing line-based buffering in src/api/providers/claude-code.ts to handle incomplete JSON chunks.
- Modifying the thinking content handling to yield "reasoning" instead of "text" in src/api/providers/claude-code.ts.
- Extending the ClaudeCodeContent type in src/integrations/claude-code/types.ts to support the new thinking content.
- Adding comprehensive tests in src/api/providers/tests/claude-code.spec.ts to cover mixed, thinking, and error scenarios.
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/integrations/claude-code/types.ts | Extended ClaudeCodeContent union type to include thinking content. |
| src/api/providers/claude-code.ts | Added buffering to process incoming data and updated reasoning block display. |
| src/api/providers/tests/claude-code.spec.ts | Added tests to verify thinking content and mixed content handling. |
src/api/providers/claude-code.ts
Outdated
| } | ||
| } else { | ||
| console.warn("Unsupported content type:", content.type) | ||
| console.warn("Unsupported content type:", (content as any).type) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this type assertion necessary? Since you've already extended the ClaudeCodeContent type to include thinking content, TypeScript should handle this properly without the cast to any.
If there are other content types not covered in the type definition, would it be better to add them to the ClaudeCodeContent union type for better type safety?
|
|
||
| // Emit process close after data | ||
| setTimeout(() => { | ||
| mockProcess.emit("close", 0) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The tests use hardcoded setTimeout delays which could be flaky in CI environments. Have you considered using a more deterministic approach? For example:
// Instead of setTimeout, use immediate execution
mockProcess.stdout.emit("data", JSON.stringify(thinkingResponse) + "\n")
mockProcess.emit("close", 0)
// Or use a test utility for controlled async behavior
await nextTick() // or similarThis would make the tests more reliable and faster to execute.
src/api/providers/claude-code.ts
Outdated
| for (const line of lines) { | ||
| dataQueue.push(line) | ||
| if (line.trim() !== "") { | ||
| dataQueue.push(line.trim()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I noticed an inconsistency in how empty lines are handled. Here you trim before checking if the line is empty, but in line 56 you check buffer.trim().
Would it be cleaner to handle this consistently? Perhaps:
// Process complete lines
for (const line of lines) {
const trimmedLine = line.trim()
if (trimmedLine !== "") {
dataQueue.push(trimmedLine)
}
}And similarly for the buffer check in the close handler.
- Remove unnecessary type assertion for better type safety - Add helper function for content text extraction - Improve buffer trimming consistency - Enhance error logging with data preview for debugging - Replace setTimeout with setImmediate in tests for better reliability Addresses review feedback to improve code quality and maintainability.
- Remove unnecessary type assertion for better type safety - Add isLikelyValidJSON helper to validate JSON structure before processing - Implement getContentText helper for cleaner error message extraction - Standardize buffer trimming logic across the codebase - Enhance error logging with data preview for better debugging - Replace setTimeout with setImmediate for more reliable test timing - Add test case for incomplete JSON handling on process close These changes address all review feedback and improve the robustness of JSON parsing in the Claude Code provider.
daniel-lxs
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
…splay (#5049) Co-authored-by: Daniel Riccio <[email protected]>
…splay (RooCodeInc#5049) Co-authored-by: Daniel Riccio <[email protected]>
…splay (RooCodeInc#5049) Co-authored-by: Daniel Riccio <[email protected]>
Description
Fixes the Claude Code provider showing raw JSON output instead of properly parsed content. This PR addresses two key issues that were causing poor user experience with the Claude Code integration.
Changes Made
src/api/providers/claude-code.tsto handle incomplete data chunks that were causing JSON parsing failures"reasoning"type instead of"text"type so it renders as collapsible reasoning blocks in the UIClaudeCodeContenttype insrc/integrations/claude-code/types.tsto support both text and thinking contentsrc/api/providers/__tests__/claude-code.spec.tscovering thinking content, mixed content, and error scenariosTesting
Technical Details
JSON Buffering Implementation:
Reasoning Block Fix:
Verification of Fix
ChatRow.tsxhas propercase "reasoning":handling that rendersReasoningBlockcomponentsChecklist
Important
Fix JSON parsing and reasoning block display in Claude Code provider by implementing line-based buffering and updating content types.
claude-code.tsto handle incomplete JSON data.isLikelyValidJSON()to validate JSON before processing."reasoning"type inclaude-code.ts.ClaudeCodeContenttype intypes.tsto supportthinkingcontent.claude-code.spec.tsfor thinking content, mixed content, and error scenarios.getContentText()inclaude-code.tsto extract text from content objects.attemptParseChunk()inclaude-code.ts.This description was created by
for dd9e146. You can customize this summary. It will automatically update as commits are pushed.