diff --git a/interpreter/core/utils/truncate_output.py b/interpreter/core/utils/truncate_output.py index 628ff504e3..4de2869b66 100644 --- a/interpreter/core/utils/truncate_output.py +++ b/interpreter/core/utils/truncate_output.py @@ -4,7 +4,16 @@ def truncate_output(data, max_output_chars=2800, add_scrollbars=False): needs_truncation = False - message = f"Output truncated. Showing the last {max_output_chars} characters. You should try again and use computer.ai.summarize(output) over the output, or break it down into smaller steps.\n\n" + # Calculate how much to show from start and end + chars_per_end = max_output_chars // 2 + + message = (f"Output truncated ({len(data):,} characters total). " + f"Showing {chars_per_end:,} characters from start/end. " + "To handle large outputs, store result in python var first " + "`result = command()` then `computer.ai.summarize(result)` for " + "a summary, search with `result.find('text')`, " + "repeat shell commands with wc/grep/sed, etc. or break it down " + "into smaller steps.\n\n") # This won't work because truncated code is stored in interpreter.messages :/ # If the full code was stored, we could do this: @@ -22,6 +31,8 @@ def truncate_output(data, max_output_chars=2800, add_scrollbars=False): # If data exceeds max length, truncate it and add message if len(data) > max_output_chars or needs_truncation: - data = message + data[-max_output_chars:] + first_part = data[:chars_per_end] + last_part = data[-chars_per_end:] + data = message + first_part + "\n[...]\n" + last_part return data