collapse-tool-output.ts 595 B

12345678910111213141516171819
  1. export function collapseToolOutput(output: string, maxLines: number, maxChars: number) {
  2. const lines = output.split("\n")
  3. if (lines.length <= maxLines && Array.from(output).length <= maxChars) {
  4. return { output, overflow: false }
  5. }
  6. const preview = lines.slice(0, maxLines).join("\n")
  7. if (Array.from(preview).length > maxChars) {
  8. return {
  9. output:
  10. Array.from(preview)
  11. .slice(0, Math.max(0, maxChars - 1))
  12. .join("") + "…",
  13. overflow: true,
  14. }
  15. }
  16. return { output: [...lines.slice(0, maxLines), "…"].join("\n"), overflow: true }
  17. }