From b53a5e752816a6f6c69de319b98073fae58ad3e9 Mon Sep 17 00:00:00 2001 From: Robert Brennan Date: Sat, 22 Mar 2025 17:31:18 -0700 Subject: [PATCH] Optimize file_editor pattern with prefix check (#7428) Co-authored-by: openhands --- openhands/events/serialization/action.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) 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)