| import { validateBoundedIntEnvVar } from '../envValidation.js' | |
| import { getTaskOutputPath } from './diskOutput.js' | |
| export const TASK_MAX_OUTPUT_UPPER_LIMIT = 160_000 | |
| export const TASK_MAX_OUTPUT_DEFAULT = 32_000 | |
| export function getMaxTaskOutputLength(): number { | |
| const result = validateBoundedIntEnvVar( | |
| 'TASK_MAX_OUTPUT_LENGTH', | |
| process.env.TASK_MAX_OUTPUT_LENGTH, | |
| TASK_MAX_OUTPUT_DEFAULT, | |
| TASK_MAX_OUTPUT_UPPER_LIMIT, | |
| ) | |
| return result.effective | |
| } | |
| /** | |
| * Format task output for API consumption, truncating if too large. | |
| * When truncated, includes a header with the file path and returns | |
| * the last N characters that fit within the limit. | |
| */ | |
| export function formatTaskOutput( | |
| output: string, | |
| taskId: string, | |
| ): { content: string; wasTruncated: boolean } { | |
| const maxLen = getMaxTaskOutputLength() | |
| if (output.length <= maxLen) { | |
| return { content: output, wasTruncated: false } | |
| } | |
| const filePath = getTaskOutputPath(taskId) | |
| const header = `[Truncated. Full output: ${filePath}]\n\n` | |
| const availableSpace = maxLen - header.length | |
| const truncated = output.slice(-availableSpace) | |
| return { content: header + truncated, wasTruncated: true } | |
| } | |