diff --git a/src/microbots/MicroBot.py b/src/microbots/MicroBot.py index 36e21e3..c4b7744 100644 --- a/src/microbots/MicroBot.py +++ b/src/microbots/MicroBot.py @@ -1,5 +1,4 @@ import json -from pprint import pformat import re import time from dataclasses import dataclass @@ -242,7 +241,7 @@ def run( try: output_json = json.loads(llm_command_output.stdout) if "content" in output_json: - output_text = pformat(output_json["content"]) + output_text = output_json["content"].encode("utf-8").decode("unicode_escape") except json.JSONDecodeError: pass else: diff --git a/test/bot/test_microbot.py b/test/bot/test_microbot.py index 5cbfbb0..403c52e 100644 --- a/test/bot/test_microbot.py +++ b/test/bot/test_microbot.py @@ -4,6 +4,7 @@ This test will create multiple custom bots - a reading bot, a writing bot using the base class. """ +import json import os from pathlib import Path import subprocess @@ -526,7 +527,6 @@ def test_pformat_produces_readable_output(self): command = """echo '{"input": {"command": "view", "path": "/tmp/test.txt"}}' | anthropic-text-editor""" # With json.dumps (old behavior) - escapes quotes making it harder to read - import json json_output = json.dumps(command) # With pformat (new behavior) - more readable @@ -544,6 +544,42 @@ def test_pformat_produces_readable_output(self): raw_output = command assert r'\"' not in raw_output, "Raw command should not have escaped quotes" assert '"input"' in raw_output, "Raw command should preserve double quotes" + + def test_anthropic_text_editor_unicode_escape_decoding(self): + """Test that unicode-escaped output is properly decoded. + + This unit test verifies that the unicode_escape decoding logic introduced + for anthropic-text-editor output works correctly. When the tool returns + JSON with a "content" field containing unicode-escaped strings (e.g., \\n, \\t), + the content should be decoded to actual newlines and tabs. + """ + # Simulate anthropic-text-editor returning JSON with unicode-escaped content + escaped_output = '{"content": "Line 1\\nLine 2\\nLine 3\\tTabbed"}' + + # Parse the JSON (simulating json.loads in the actual code) + output_json = json.loads(escaped_output) + + # Apply the unicode_escape decoding (the line being tested) + decoded_output = output_json["content"].encode("utf-8").decode("unicode_escape") + + # Verify the decoded output has actual newlines and tabs + expected_output = "Line 1\nLine 2\nLine 3\tTabbed" + assert decoded_output == expected_output, \ + "Output should contain decoded newlines and tabs" + + # Verify the escaped versions are NOT in the decoded output + assert "\\n" not in decoded_output, \ + "Decoded output should not contain escaped newlines" + assert "\\t" not in decoded_output, \ + "Decoded output should not contain escaped tabs" + + # Verify line count - should be 3 lines (split by actual newlines) + lines = decoded_output.split("\n") + assert len(lines) == 3, "Should have 3 lines after decoding" + assert lines[0] == "Line 1" + assert lines[1] == "Line 2" + assert lines[2] == "Line 3\tTabbed" # This line contains an actual tab + def test_tool_usage_instructions_appended_to_system_prompt(self): """Test that tool usage instructions are appended to the system prompt when creating LLM.""" from microbots.tools.tool import Tool