( output: string, maxChars: number, context?: string, )
| 89 | * @param context - Optional context for the truncation message (e.g., "file content", "diff") |
| 90 | */ |
| 91 | export function truncateOutputFromEnd( |
| 92 | output: string, |
| 93 | maxChars: number, |
| 94 | context?: string, |
| 95 | ): TruncationResult { |
| 96 | if (!output || output.length <= maxChars) { |
| 97 | return { output, wasTruncated: false }; |
| 98 | } |
| 99 | |
| 100 | const truncatedContent = output.slice(0, maxChars); |
| 101 | |
| 102 | // Try to end at a clean line boundary |
| 103 | const lastNewline = truncatedContent.lastIndexOf("\n"); |
| 104 | const shouldSnapToLine = |
| 105 | lastNewline !== -1 && |
| 106 | truncatedContent.length - lastNewline < TRUNCATION_LINE_SNAP_THRESHOLD; |
| 107 | const cleanContent = shouldSnapToLine |
| 108 | ? truncatedContent.slice(0, lastNewline) |
| 109 | : truncatedContent; |
| 110 | |
| 111 | const actualCharsRemoved = output.length - cleanContent.length; |
| 112 | const contextStr = context ? ` of ${context}` : ""; |
| 113 | const suffix = `\n\n(${actualCharsRemoved} characters${contextStr} truncated)`; |
| 114 | |
| 115 | return { |
| 116 | output: cleanContent + suffix, |
| 117 | wasTruncated: true, |
| 118 | }; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Truncates output by line count from the end, preserving the beginning. |
no outgoing calls
no test coverage detected