Skip to content

Drop in a screenshot and convert it to clean code (HTML/Tailwind/React/Vue)

Python

improve: enhance prompt summary formatting for better terminal display

Abi Rajaa1c2793e0eee
diff --git a/backend/tests/test_prompt_summary.py b/backend/tests/test_prompt_summary.py
index 14547811..e7e10828 100644
--- a/backend/tests/test_prompt_summary.py
+++ b/backend/tests/test_prompt_summary.py
@@ -1,4 +1,6 @@
-from utils import format_prompt_summary
+import io
+import sys
+from utils import format_prompt_summary, print_prompt_summary
def test_format_prompt_summary():
@@ -21,5 +23,60 @@ def test_format_prompt_summary():
]
summary = format_prompt_summary(messages)
- assert "system: lorem ipsum" in summary
+ assert "SYSTEM: lorem ipsum" in summary
assert "[2 images]" in summary
+
+
+def test_print_prompt_summary():
+ messages = [
+ {"role": "system", "content": "short message"},
+ {"role": "user", "content": "hello"},
+ ]
+
+ # Capture stdout
+ captured_output = io.StringIO()
+ sys.stdout = captured_output
+
+ print_prompt_summary(messages)
+
+ # Reset stdout
+ sys.stdout = sys.__stdout__
+
+ output = captured_output.getvalue()
+
+ # Check that output contains box characters and content
+ assert "┌─" in output
+ assert "└─" in output
+ assert "PROMPT SUMMARY" in output
+ assert "SYSTEM: short message" in output
+ assert "USER: hello" in output
+
+
+def test_print_prompt_summary_long_content():
+ messages = [
+ {"role": "system", "content": "This is a very long system message that should be wrapped properly within the box boundaries"},
+ {"role": "user", "content": "short"},
+ ]
+
+ # Capture stdout
+ captured_output = io.StringIO()
+ sys.stdout = captured_output
+
+ print_prompt_summary(messages)
+
+ # Reset stdout
+ sys.stdout = sys.__stdout__
+
+ output = captured_output.getvalue()
+ lines = output.strip().split('\n')
+
+ # Check that all lines have consistent box formatting
+ for line in lines:
+ if line.startswith('│') and line.endswith('│'):
+ # All content lines should have same length
+ assert len(line) == len(lines[0]) if lines[0].startswith('┌') else True
+
+ # Check content is present
+ assert "PROMPT SUMMARY" in output
+ assert "SYSTEM:" in output
+ assert "USER: short" in output
diff --git a/backend/utils.py b/backend/utils.py
index 265df9bb..efc04fb9 100644
--- a/backend/utils.py
+++ b/backend/utils.py
@@ -30,17 +30,47 @@ def format_prompt_summary(prompt_messages: List[ChatCompletionMessageParam]) ->
text = text[:40] + "..."
img_part = f" + [{image_count} images]" if image_count else ""
- parts.append(f"{role}: {text}{img_part}")
+ parts.append(f" {role.upper()}: {text}{img_part}")
- return " / ".join(parts)
+ return "\n".join(parts)
def print_prompt_summary(prompt_messages: List[ChatCompletionMessageParam]):
- print("-" * 20)
- print("Prompt summary:")
- print(format_prompt_summary(prompt_messages))
- print("-" * 20)
- print("")
+ summary = format_prompt_summary(prompt_messages)
+ lines = summary.split('\n')
+
+ # Find the maximum line length, with a minimum of 20 and maximum of 80
+ max_length = max(len(line) for line in lines) if lines else 20
+ max_length = max(20, min(80, max_length))
+
+ # Ensure title fits
+ title = "PROMPT SUMMARY"
+ max_length = max(max_length, len(title) + 4)
+
+ print("┌─" + "─" * max_length + "─┐")
+ title_padding = (max_length - len(title)) // 2
+ print(f"│ {' ' * title_padding}{title}{' ' * (max_length - len(title) - title_padding)} │")
+ print("├─" + "─" * max_length + "─┤")
+
+ for line in lines:
+ if len(line) <= max_length:
+ print(f"│ {line:<{max_length}} │")
+ else:
+ # Wrap long lines
+ words = line.split()
+ current_line = ""
+ for word in words:
+ if len(current_line + " " + word) <= max_length:
+ current_line += (" " + word) if current_line else word
+ else:
+ if current_line:
+ print(f"│ {current_line:<{max_length}} │")
+ current_line = word
+ if current_line:
+ print(f"│ {current_line:<{max_length}} │")
+
+ print("└─" + "─" * max_length + "─┘")
+ print()
def truncate_data_strings(data: List[ChatCompletionMessageParam]): # type: ignore