Skip to content

Commit 7e5b2aa

Browse files
committed
test: more robust arg parsing
Signed-off-by: vsoch <vsoch@users.noreply.github.com>
1 parent d110635 commit 7e5b2aa

2 files changed

Lines changed: 18 additions & 21 deletions

File tree

resource_secretary/agents/secretary.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ async def deliberate(
168168
obs = "OBSERVATIONS:\n"
169169
for call in calls:
170170
p_name, f_name, args_str = call
171-
args = utils.parse_args_string(args_str)
171+
args = utils.parse_args(args_str)
172172
result = self.execute_call(p_name, f_name, args)
173173

174174
# Results are usually json, but not always

resource_secretary/utils/text.py

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,6 @@ def format_calls(calls_block):
1616
return calls
1717

1818

19-
import ast
20-
import re
21-
22-
23-
def parse_args_string(args_str):
24-
flattened = args_str.replace("\n", " ").replace("\r", " ")
25-
flattened = re.sub(r"\s+", " ", flattened).strip()
26-
try:
27-
tree = ast.parse(f"f({flattened})")
28-
29-
kwargs = {}
30-
for kw in tree.body[0].value.keywords:
31-
kwargs[kw.arg] = ast.literal_eval(kw.value)
32-
33-
return kwargs
34-
except SyntaxError as e:
35-
print(f"Syntax error in agent output: {e}")
36-
return {}
37-
38-
3919
def ensure_command(command):
4020
"""
4121
Ensure the command if provided as string is split into list.
@@ -45,6 +25,23 @@ def ensure_command(command):
4525
return command
4626

4727

28+
import ast
29+
import re
30+
31+
32+
def parse_args(args_str):
33+
pattern = r"(\w+)\s*=\s*(\{.*?\}|'[^']*'|\"[^\"]*\"|[^,]+)"
34+
matches = re.findall(pattern, args_str)
35+
result = {}
36+
for key, value in matches:
37+
value = value.strip()
38+
try:
39+
result[key] = ast.literal_eval(value)
40+
except:
41+
result[key] = value.strip("'\"")
42+
return result
43+
44+
4845
def from_string_arg(val):
4946
"""
5047
When we parse a call (from string) we need to convert into Python types.

0 commit comments

Comments
 (0)