fix: Better formatting of truncated output strings (#42)

* fix: Better formatting of truncated output strings

* cr
This commit is contained in:
Brace Sproul 2025-05-28 15:46:18 -07:00 • committed by GitHub
parent 2381e9f703
commit 0fbcaa3131
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,3 +1,7 @@
import { createLogger, LogLevel } from "./logger.js";
const logger = createLogger(LogLevel.INFO, "TruncateOutputs");
export function truncateOutput(
output: string,
options?: {
@ -28,9 +32,19 @@ export function truncateOutput(
return output;
}
logger.warn(
`Truncating output due to its length exceeding the maximum allowed characters. Received ${output.length} characters, but only ${numStartCharacters + numEndCharacters} were allowed.`,
{
numAllowedStartCharacters: numStartCharacters,
numAllowedEndCharacters: numEndCharacters,
outputLength: output.length,
},
);
return (
`The following output was truncated due to its length exceeding the maximum allowed characters. Received ${output.length} characters, but only ${numStartCharacters + numEndCharacters} were allowed.\n\n` +
output.slice(0, numStartCharacters) +
`\n... Output too long. Truncated the middle ${output.length - numStartCharacters - numEndCharacters} characters of the output ...\n` +
`\n\n... [content truncated] ...\n\n` +
output.slice(-numEndCharacters)
);
}