import type { CommandRunResult } from "./types"; const DEFAULT_OUTPUT_LIMIT = 4000; export function truncateOutput(output: string, limit = DEFAULT_OUTPUT_LIMIT): string { const normalized = output.trim(); if (normalized.length <= limit) return normalized; return `${normalized.slice(0, limit)}\n… output truncated (${normalized.length - limit} more characters)`; } export function commandOutput(result: Pick): string { return truncateOutput([result.stdout, result.stderr].filter((part) => part.trim().length > 0).join("\n")); } export function formatWarnings(title: string, warnings: string[]): string { if (warnings.length === 0) return ""; return `${title}:\n\n${warnings.map((warning) => `⚠️ ${warning}`).join("\n")}`; } export function formatCommandIssue(toolId: string, action: string, result: CommandRunResult): string { const output = commandOutput(result); const suffix = result.killed ? " (killed/timeout)" : ""; if (!output) return `⚠️ ${toolId} ${action} failed with exit code ${result.code}${suffix}`; return `⚠️ ${toolId} ${action} failed with exit code ${result.code}${suffix}:\n${output}`; } export function formatDiagnosticOutput(toolId: string, output: string): string { const compact = truncateOutput(output); if (!compact) return `⚠️ ${toolId} found issues`; return `⚠️ ${toolId} found issues:\n${compact}`; } export function hasIssueOutput(output: string): boolean { return output.includes("⚠️"); } export function joinSections(title: string, lines: string[]): string { const nonEmpty = lines.filter((line) => line.trim().length > 0); if (nonEmpty.length === 0) return ""; return `${title}:\n\n${nonEmpty.join("\n")}`; }