Cyber Catalyst Team commited on
Commit
fae276b
·
1 Parent(s): 17a4f6d

Optimize execute_tool with process execution, and robustify JSON argument parsing

Browse files
Files changed (1) hide show
  1. backend.py +15 -5
backend.py CHANGED
@@ -373,10 +373,8 @@ async def execute_tool(name: str, arguments: dict) -> str:
373
  search_path = arguments.get("path", ".")
374
  path = _safe_path(search_path)
375
 
376
- # Escape single quotes in pattern for safety
377
- escaped_pattern = pattern.replace("'", "'\\''")
378
- process = await asyncio.create_subprocess_shell(
379
- f"grep -rn --include=* '{escaped_pattern}' '{path}'",
380
  stdout=asyncio.subprocess.PIPE,
381
  stderr=asyncio.subprocess.PIPE,
382
  cwd=WORKSPACE_DIR,
@@ -1805,7 +1803,19 @@ async def forge_execute(req: ForgeExecuteRequest, authorization: str = Header(No
1805
 
1806
  for tc in msg.tool_calls:
1807
  func_name = tc.function.name
1808
- func_args = json.loads(tc.function.arguments)
 
 
 
 
 
 
 
 
 
 
 
 
1809
  result = await execute_tool(func_name, func_args)
1810
 
1811
  final_messages.append({
 
373
  search_path = arguments.get("path", ".")
374
  path = _safe_path(search_path)
375
 
376
+ process = await asyncio.create_subprocess_exec(
377
+ "grep", "-rn", "--include=*", pattern, str(path),
 
 
378
  stdout=asyncio.subprocess.PIPE,
379
  stderr=asyncio.subprocess.PIPE,
380
  cwd=WORKSPACE_DIR,
 
1803
 
1804
  for tc in msg.tool_calls:
1805
  func_name = tc.function.name
1806
+ raw_args_str = tc.function.arguments
1807
+ try:
1808
+ func_args = json.loads(raw_args_str)
1809
+ except Exception:
1810
+ repaired_str = raw_args_str.strip()
1811
+ if not repaired_str.startswith("{"):
1812
+ repaired_str = "{" + repaired_str
1813
+ if not repaired_str.endswith("}"):
1814
+ repaired_str = repaired_str + "}"
1815
+ try:
1816
+ func_args = json.loads(repaired_str)
1817
+ except Exception:
1818
+ func_args = {}
1819
  result = await execute_tool(func_name, func_args)
1820
 
1821
  final_messages.append({