Spaces:
Paused
Paused
Dariusz Majewski commited on
Commit ·
c7c6881
1
Parent(s): 43eeb15
Fix for dirty_json helper and associated test class
Browse files- python/helpers/dirty_json.py +1 -1
- tests/__init__.py +0 -0
- tests/helpers/__init__.py +0 -0
- tests/helpers/test_json_parse_dirty.py +60 -0
python/helpers/dirty_json.py
CHANGED
|
@@ -250,7 +250,7 @@ class DirtyJson:
|
|
| 250 |
return result.strip()
|
| 251 |
|
| 252 |
def _peek(self, n):
|
| 253 |
-
peek_index = self.index
|
| 254 |
result = ''
|
| 255 |
for _ in range(n):
|
| 256 |
if peek_index < len(self.json_string):
|
|
|
|
| 250 |
return result.strip()
|
| 251 |
|
| 252 |
def _peek(self, n):
|
| 253 |
+
peek_index = self.index + 1
|
| 254 |
result = ''
|
| 255 |
for _ in range(n):
|
| 256 |
if peek_index < len(self.json_string):
|
tests/__init__.py
ADDED
|
File without changes
|
tests/helpers/__init__.py
ADDED
|
File without changes
|
tests/helpers/test_json_parse_dirty.py
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import unittest
|
| 2 |
+
from python.helpers.extract_tools import extract_json_object_string
|
| 3 |
+
from python.helpers.dirty_json import DirtyJson
|
| 4 |
+
from typing import Any
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
def json_parse_dirty(json: str) -> dict[str, Any] | None:
|
| 8 |
+
ext_json = extract_json_object_string(json)
|
| 9 |
+
if ext_json:
|
| 10 |
+
data = DirtyJson.parse_string(ext_json)
|
| 11 |
+
if isinstance(data, dict):
|
| 12 |
+
return data
|
| 13 |
+
return None
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
class TestJsonParseDirty(unittest.TestCase):
|
| 17 |
+
def test_valid_json(self):
|
| 18 |
+
json_string = '{"key": "value"}'
|
| 19 |
+
expected_output = {"key": "value"}
|
| 20 |
+
self.assertEqual(json_parse_dirty(json_string), expected_output)
|
| 21 |
+
|
| 22 |
+
def test_invalid_json(self):
|
| 23 |
+
json_string = 'invalid json'
|
| 24 |
+
self.assertIsNone(json_parse_dirty(json_string))
|
| 25 |
+
|
| 26 |
+
def test_partial_json(self):
|
| 27 |
+
json_string = 'some text before {"key": "value"} some text after'
|
| 28 |
+
expected_output = {"key": "value"}
|
| 29 |
+
self.assertEqual(json_parse_dirty(json_string), expected_output)
|
| 30 |
+
|
| 31 |
+
def test_no_closing_brace(self):
|
| 32 |
+
json_string = '{"key": "value"'
|
| 33 |
+
expected_output = {"key": "value"}
|
| 34 |
+
self.assertEqual(json_parse_dirty(json_string), expected_output)
|
| 35 |
+
|
| 36 |
+
def test_no_opening_brace(self):
|
| 37 |
+
json_string = '"key": "value"}'
|
| 38 |
+
self.assertIsNone(json_parse_dirty(json_string))
|
| 39 |
+
|
| 40 |
+
def test_agent_response(self):
|
| 41 |
+
json_string = ('{"thoughts": ["The user wants to save the source code of their Hello, World! application to a '
|
| 42 |
+
'file.", "I can use the code_execution_tool with terminal runtime to achieve this."], '
|
| 43 |
+
'"tool_name": "code_execution_tool", "tool_args": {"runtime": "terminal", "code": "echo '
|
| 44 |
+
'\'print(\'Hello, World!\')\' > hello_world.py"}}')
|
| 45 |
+
expected_result = {
|
| 46 |
+
"thoughts": [
|
| 47 |
+
"The user wants to save the source code of their Hello, World! application to a file.",
|
| 48 |
+
"I can use the code_execution_tool with terminal runtime to achieve this."
|
| 49 |
+
],
|
| 50 |
+
"tool_name": "code_execution_tool",
|
| 51 |
+
"tool_args": {
|
| 52 |
+
"runtime": "terminal",
|
| 53 |
+
"code": "echo \'print(\'Hello, World!\')\' > hello_world.py"
|
| 54 |
+
}
|
| 55 |
+
}
|
| 56 |
+
self.assertEqual(json_parse_dirty(json_string), expected_result)
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
if __name__ == '__main__':
|
| 60 |
+
unittest.main()
|