-
Notifications
You must be signed in to change notification settings - Fork 2.6k
fix: convert escaped newlines to actual newlines in Claude Code output #6711
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
Conversation
- Fix issue where Claude Code outputs containing \n were displayed literally - Add text.replace(/\\n/g, n) for both string chunks and text content - Add comprehensive tests to verify the fix works correctly - Resolves #6709
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.
Reviewing my own code is like debugging in a mirror - everything looks backwards but the bugs are still mine.
| yield { | ||
| type: "text", | ||
| text: chunk, | ||
| text: chunk.replace(/\\n/g, "\n"), |
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.
Good fix! Though I'm wondering - should we also handle other escape sequences like or that might appear in Claude Code output? Currently we only handle .
| yield { | ||
| type: "text", | ||
| text: content.text, | ||
| text: content.text.replace(/\\n/g, "\n"), |
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.
Same consideration here - should we handle and as well?
Also, a brief comment explaining why this transformation is necessary would help future maintainers understand the context (e.g., "Convert escaped newlines from Claude Code process output to actual newlines for proper rendering").
| consoleSpy.mockRestore() | ||
| }) | ||
|
|
||
| test("should convert escaped newlines to actual newlines in string chunks", async () => { |
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.
Excellent test coverage! The tests thoroughly verify the newline conversion behavior.
One minor suggestion: Consider adding a test case for edge cases like empty strings or null/undefined text content to ensure the replace operation handles these gracefully.
This PR fixes an issue where escaped newline characters (
\n) in Claude Code output were being displayed literally instead of being converted to actual line breaks.Problem
When using the Claude Code provider, messages containing escaped newlines were displayed as literal
\ncharacters:Instead of the expected formatted output:
Solution
Added
.replace(/\\n/g, '\n')to convert escaped newlines to actual newlines in two places:Testing
Added comprehensive tests to verify:
\nis converted)All existing tests continue to pass.
Fixes #6709
Important
Converts escaped newlines to actual newlines in Claude Code output, with tests ensuring correct behavior in
claude-code.ts.\n) to actual newlines inClaudeCodeHandlerinclaude-code.ts.claude-code.spec.tsfor converting escaped newlines in string chunks and assistant messages.\nis converted.This description was created by
for d563fed. You can customize this summary. It will automatically update as commits are pushed.