Skip to content

Commit 1a62907

Browse files
authored
Merge pull request #1583 from endolith/improve_truncation
Improve truncation
2 parents a170425 + 5f9df7e commit 1a62907

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

interpreter/core/utils/truncate_output.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,16 @@ def truncate_output(data, max_output_chars=2800, add_scrollbars=False):
44

55
needs_truncation = False
66

7-
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"
7+
# Calculate how much to show from start and end
8+
chars_per_end = max_output_chars // 2
9+
10+
message = (f"Output truncated ({len(data):,} characters total). "
11+
f"Showing {chars_per_end:,} characters from start/end. "
12+
"To handle large outputs, store result in python var first "
13+
"`result = command()` then `computer.ai.summarize(result)` for "
14+
"a summary, search with `result.find('text')`, "
15+
"repeat shell commands with wc/grep/sed, etc. or break it down "
16+
"into smaller steps.\n\n")
817

918
# This won't work because truncated code is stored in interpreter.messages :/
1019
# 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):
2231

2332
# If data exceeds max length, truncate it and add message
2433
if len(data) > max_output_chars or needs_truncation:
25-
data = message + data[-max_output_chars:]
34+
first_part = data[:chars_per_end]
35+
last_part = data[-chars_per_end:]
36+
data = message + first_part + "\n[...]\n" + last_part
2637

2738
return data

0 commit comments

Comments
 (0)