(content: string, lineLimit?: number, characterLimit?: number)
| 238 | * // Result: First ~8000 chars + "[...60002 characters omitted...]" + Last ~32000 chars |
| 239 | */ |
| 240 | export function truncateOutput(content: string, lineLimit?: number, characterLimit?: number): string { |
| 241 | // If no limits are specified, return original content |
| 242 | if (!lineLimit && !characterLimit) { |
| 243 | return content |
| 244 | } |
| 245 | |
| 246 | // Character limit takes priority over line limit |
| 247 | if (characterLimit && content.length > characterLimit) { |
| 248 | const beforeLimit = Math.floor(characterLimit * 0.2) // 20% of characters before |
| 249 | const afterLimit = characterLimit - beforeLimit // remaining 80% after |
| 250 | |
| 251 | const startSection = content.slice(0, beforeLimit) |
| 252 | const endSection = content.slice(-afterLimit) |
| 253 | const omittedChars = content.length - characterLimit |
| 254 | |
| 255 | return startSection + `\n[...${omittedChars} characters omitted...]\n` + endSection |
| 256 | } |
| 257 | |
| 258 | // If character limit is not exceeded or not specified, check line limit |
| 259 | if (!lineLimit) { |
| 260 | return content |
| 261 | } |
| 262 | |
| 263 | // Count total lines |
| 264 | let totalLines = 0 |
| 265 | let pos = -1 |
| 266 | while ((pos = content.indexOf("\n", pos + 1)) !== -1) { |
| 267 | totalLines++ |
| 268 | } |
| 269 | totalLines++ // Account for last line without line feed (\n) |
| 270 | |
| 271 | if (totalLines <= lineLimit) { |
| 272 | return content |
| 273 | } |
| 274 | |
| 275 | const beforeLimit = Math.floor(lineLimit * 0.2) // 20% of lines before |
| 276 | const afterLimit = lineLimit - beforeLimit // remaining 80% after |
| 277 | |
| 278 | // Find start section end position |
| 279 | let startEndPos = -1 |
| 280 | let lineCount = 0 |
| 281 | pos = 0 |
| 282 | while (lineCount < beforeLimit && (pos = content.indexOf("\n", pos)) !== -1) { |
| 283 | startEndPos = pos |
| 284 | lineCount++ |
| 285 | pos++ |
| 286 | } |
| 287 | |
| 288 | // Find end section start position |
| 289 | let endStartPos = content.length |
| 290 | lineCount = 0 |
| 291 | pos = content.length |
| 292 | while (lineCount < afterLimit && (pos = content.lastIndexOf("\n", pos - 1)) !== -1) { |
| 293 | endStartPos = pos + 1 // Start after the line feed (\n) |
| 294 | lineCount++ |
| 295 | } |
| 296 | |
| 297 | const omittedLines = totalLines - lineLimit |
no outgoing calls