mirror of
https://github.com/anthropics/claude-code-action.git
synced 2026-01-22 22:44:13 +08:00
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:
@@ -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) + "...";
|
||||
|
||||
@@ -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(
|
||||
|
||||
Reference in New Issue
Block a user