fix: Strip ANSI color sequences from tool output

Add stripAnsiCodes function to sanitizer and apply it in formatResultContent
to remove terminal color escape codes (e.g., [1;33m for yellow/bold) from
tool output before displaying it in GitHub comments.

This ensures clean, readable output without raw ANSI escape sequences
appearing in the formatted tool results.
This commit is contained in:
Claude
2025-12-12 02:17:56 +00:00
parent f0c8eb2980
commit 7c0df70e8f
4 changed files with 79 additions and 0 deletions

View File

@@ -2,6 +2,7 @@
import { readFileSync, existsSync } from "fs";
import { exit } from "process";
import { stripAnsiCodes } from "../github/utils/sanitizer";
export type ToolUse = {
type: string;
@@ -172,6 +173,9 @@ export function formatResultContent(content: any): string {
contentStr = String(content).trim();
}
// Strip ANSI escape codes from terminal output
contentStr = stripAnsiCodes(contentStr);
// Truncate very long results
if (contentStr.length > 3000) {
contentStr = contentStr.substring(0, 2997) + "...";

View File

@@ -1,3 +1,11 @@
export function stripAnsiCodes(content: string): string {
// Matches ANSI escape sequences:
// - \x1B[ (CSI) followed by parameters and a final byte
// - \x1B followed by single-character sequences
// Common sequences: \x1B[1;33m (colors), \x1B[0m (reset), \x1B[K (clear line)
return content.replace(/\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])/g, "");
}
export function stripInvisibleCharacters(content: string): string {
content = content.replace(/[\u200B\u200C\u200D\uFEFF]/g, "");
content = content.replace(