Skip to content

Conversation

@roomote
Copy link
Contributor

@roomote roomote bot commented Aug 5, 2025

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 \n characters:

\n# Claude Chat History\n\n## 2025-08-05

Instead of the expected formatted output:

# Claude Chat History

## 2025-08-05

Solution

Added .replace(/\\n/g, '\n') to convert escaped newlines to actual newlines in two places:

  1. String chunks from the Claude Code process
  2. Text content from assistant messages

Testing

Added comprehensive tests to verify:

  • Escaped newlines in string chunks are converted correctly
  • Escaped newlines in assistant message text content are converted correctly
  • Mixed escape sequences are handled properly (only \n is 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.

  • Behavior:
    • Converts escaped newlines (\n) to actual newlines in ClaudeCodeHandler in claude-code.ts.
    • Applies conversion to string chunks and assistant message text content.
  • Testing:
    • Adds tests in claude-code.spec.ts for converting escaped newlines in string chunks and assistant messages.
    • Tests handle mixed escape sequences, ensuring only \n is converted.
    • All existing tests pass, ensuring no regressions.

This description was created by Ellipsis for d563fed. You can customize this summary. It will automatically update as commits are pushed.

- 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
@roomote roomote bot requested review from cte, jr and mrubens as code owners August 5, 2025 10:02
@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. bug Something isn't working labels Aug 5, 2025
Copy link
Contributor Author

@roomote roomote bot left a 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"),
Copy link
Contributor Author

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"),
Copy link
Contributor Author

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 () => {
Copy link
Contributor Author

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.

@hannesrudolph hannesrudolph added the Issue/PR - Triage New issue. Needs quick review to confirm validity and assign labels. label Aug 5, 2025
@daniel-lxs daniel-lxs moved this from Triage to PR [Needs Prelim Review] in Roo Code Roadmap Aug 6, 2025
@hannesrudolph hannesrudolph added PR - Needs Preliminary Review and removed Issue/PR - Triage New issue. Needs quick review to confirm validity and assign labels. labels Aug 6, 2025
@daniel-lxs daniel-lxs moved this from PR [Needs Prelim Review] to PR [Draft / In Progress] in Roo Code Roadmap Aug 12, 2025
@daniel-lxs daniel-lxs marked this pull request as draft August 12, 2025 16:36
@github-project-automation github-project-automation bot moved this from PR [Draft / In Progress] to Done in Roo Code Roadmap Sep 23, 2025
@github-project-automation github-project-automation bot moved this from New to Done in Roo Code Roadmap Sep 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working PR - Draft / In Progress size:L This PR changes 100-499 lines, ignoring generated files.

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

All line breaks are displayed as \n when use Provider Claude Code

3 participants