Skip to content

πŸ™Œ OpenHands: AI-Driven Development

Optimize file_editor pattern with prefix check (#7428)

Robert Brennanb53a5e752816
diff --git a/openhands/events/serialization/action.py b/openhands/events/serialization/action.py
index c314201750..462e5dd250 100644
--- a/openhands/events/serialization/action.py
+++ b/openhands/events/serialization/action.py
@@ -1,5 +1,3 @@
-import re
-
from openhands.core.exceptions import LLMMalformedActionError
from openhands.events.action.action import Action
from openhands.events.action.agent import (
@@ -53,14 +51,20 @@ def handle_action_deprecated_args(args: dict) -> dict:
if 'translated_ipython_code' in args:
code = args.pop('translated_ipython_code')
- # Check if it's a file_editor call
- file_editor_pattern = r'print\(file_editor\(\*\*(.*?)\)\)'
- if code is not None and (match := re.match(file_editor_pattern, code)):
+ # Check if it's a file_editor call using a prefix check for efficiency
+ file_editor_prefix = 'print(file_editor(**'
+ if (
+ code is not None
+ and code.startswith(file_editor_prefix)
+ and code.endswith('))')
+ ):
try:
# Extract and evaluate the dictionary string
import ast
- file_args = ast.literal_eval(match.group(1))
+ # Extract the dictionary string between the prefix and the closing parentheses
+ dict_str = code[len(file_editor_prefix) : -2] # Remove prefix and '))'
+ file_args = ast.literal_eval(dict_str)
# Update args with the extracted file editor arguments
args.update(file_args)