Optimize file_editor pattern with prefix check (#7428)

Co-authored-by: openhands <openhands@all-hands.dev>
This commit is contained in:
Robert Brennan 2025-03-22 17:31:18 -07:00 committed by GitHub
parent 8dda45bf99
commit b53a5e7528
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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)