From badec2fb4e8515a299891e169524dee46e76b5c7 Mon Sep 17 00:00:00 2001
From: endolith <endolith@gmail.com>
Date: Sat, 25 Jan 2025 22:55:17 -0500
Subject: [PATCH 1/3] Remove middle when truncating instead of beginning

This should give more useful context to the assistant, as the most
important info is usually in the beginning and end.
---
 interpreter/core/utils/truncate_output.py | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/interpreter/core/utils/truncate_output.py b/interpreter/core/utils/truncate_output.py
index 628ff504e3..009f91716a 100644
--- a/interpreter/core/utils/truncate_output.py
+++ b/interpreter/core/utils/truncate_output.py
@@ -4,7 +4,13 @@ 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 = ("Output truncated. "
+               f"Showing {chars_per_end} characters from start/end. "
+               "You should try again and use computer.ai.summarize(output) "
+               "over the output, 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 +28,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

From 83f3f2308071dd0688382f542e7d60fe9f3cb2e8 Mon Sep 17 00:00:00 2001
From: endolith <endolith@gmail.com>
Date: Sat, 25 Jan 2025 23:44:25 -0500
Subject: [PATCH 2/3] Improve suggestions for handling truncated output

Smaller models get confused and call the summarize command incorrectly,
or try to call it on the output of shell commands, etc.  Give a few
suggestions of how to better deal with long output.
---
 interpreter/core/utils/truncate_output.py | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/interpreter/core/utils/truncate_output.py b/interpreter/core/utils/truncate_output.py
index 009f91716a..c94e6daf31 100644
--- a/interpreter/core/utils/truncate_output.py
+++ b/interpreter/core/utils/truncate_output.py
@@ -9,8 +9,11 @@ def truncate_output(data, max_output_chars=2800, add_scrollbars=False):
 
     message = ("Output truncated. "
                f"Showing {chars_per_end} characters from start/end. "
-               "You should try again and use computer.ai.summarize(output) "
-               "over the output, or break it down into smaller steps.\n\n")
+               "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:

From 63347359f2296dfe2cd49a4540e6b314a70da1ab Mon Sep 17 00:00:00 2001
From: endolith <endolith@gmail.com>
Date: Sun, 26 Jan 2025 10:03:25 -0500
Subject: [PATCH 3/3] Print total length when truncating long outputs

Provides a little more context for the assistant to work with
---
 interpreter/core/utils/truncate_output.py | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/interpreter/core/utils/truncate_output.py b/interpreter/core/utils/truncate_output.py
index c94e6daf31..4de2869b66 100644
--- a/interpreter/core/utils/truncate_output.py
+++ b/interpreter/core/utils/truncate_output.py
@@ -7,8 +7,8 @@ def truncate_output(data, max_output_chars=2800, add_scrollbars=False):
     # Calculate how much to show from start and end
     chars_per_end = max_output_chars // 2
 
-    message = ("Output truncated. "
-               f"Showing {chars_per_end} characters from start/end. "
+    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')`, "