Skip to content

Agent Zero AI framework

Fix for dirty_json helper and associated test class

Dariusz Majewskie229f4a92fbc
diff --git a/python/helpers/dirty_json.py b/python/helpers/dirty_json.py
index 55d34a0c2..116899f11 100644
--- a/python/helpers/dirty_json.py
+++ b/python/helpers/dirty_json.py
@@ -250,7 +250,7 @@ class DirtyJson:
return result.strip()
def _peek(self, n):
- peek_index = self.index
+ peek_index = self.index + 1
result = ''
for _ in range(n):
if peek_index < len(self.json_string):
diff --git a/tests/__init__.py b/tests/__init__.py
new file mode 100644
index 000000000..e69de29bb
diff --git a/tests/helpers/__init__.py b/tests/helpers/__init__.py
new file mode 100644
index 000000000..e69de29bb
diff --git a/tests/helpers/test_json_parse_dirty.py b/tests/helpers/test_json_parse_dirty.py
new file mode 100644
index 000000000..41724bd08
--- /dev/null
+++ b/tests/helpers/test_json_parse_dirty.py
@@ -0,0 +1,60 @@
+import unittest
+from python.helpers.extract_tools import extract_json_object_string
+from python.helpers.dirty_json import DirtyJson
+from typing import Any
+
+
+def json_parse_dirty(json: str) -> dict[str, Any] | None:
+ ext_json = extract_json_object_string(json)
+ if ext_json:
+ data = DirtyJson.parse_string(ext_json)
+ if isinstance(data, dict):
+ return data
+ return None
+
+
+class TestJsonParseDirty(unittest.TestCase):
+ def test_valid_json(self):
+ json_string = '{"key": "value"}'
+ expected_output = {"key": "value"}
+ self.assertEqual(json_parse_dirty(json_string), expected_output)
+
+ def test_invalid_json(self):
+ json_string = 'invalid json'
+ self.assertIsNone(json_parse_dirty(json_string))
+
+ def test_partial_json(self):
+ json_string = 'some text before {"key": "value"} some text after'
+ expected_output = {"key": "value"}
+ self.assertEqual(json_parse_dirty(json_string), expected_output)
+
+ def test_no_closing_brace(self):
+ json_string = '{"key": "value"'
+ expected_output = {"key": "value"}
+ self.assertEqual(json_parse_dirty(json_string), expected_output)
+
+ def test_no_opening_brace(self):
+ json_string = '"key": "value"}'
+ self.assertIsNone(json_parse_dirty(json_string))
+
+ def test_agent_response(self):
+ json_string = ('{"thoughts": ["The user wants to save the source code of their Hello, World! application to a '
+ 'file.", "I can use the code_execution_tool with terminal runtime to achieve this."], '
+ '"tool_name": "code_execution_tool", "tool_args": {"runtime": "terminal", "code": "echo '
+ '\'print(\'Hello, World!\')\' > hello_world.py"}}')
+ expected_result = {
+ "thoughts": [
+ "The user wants to save the source code of their Hello, World! application to a file.",
+ "I can use the code_execution_tool with terminal runtime to achieve this."
+ ],
+ "tool_name": "code_execution_tool",
+ "tool_args": {
+ "runtime": "terminal",
+ "code": "echo \'print(\'Hello, World!\')\' > hello_world.py"
+ }
+ }
+ self.assertEqual(json_parse_dirty(json_string), expected_result)
+
+
+if __name__ == '__main__':
+ unittest.main()
\ No newline at end of file