Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion phone_agent/actions/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from phone_agent.config.timing import TIMING_CONFIG
from phone_agent.device_factory import get_device_factory
from phone_agent.utils import regex_util


@dataclass
Expand Down Expand Up @@ -358,7 +359,7 @@ def parse_action(response: str) -> dict[str, Any]:
response = response.replace('\n', '\\n')
response = response.replace('\r', '\\r')
response = response.replace('\t', '\\t')

response = regex_util.escape_inner_quotes(response)
tree = ast.parse(response, mode="eval")
if not isinstance(tree.body, ast.Call):
raise ValueError("Expected a function call")
Expand Down
21 changes: 21 additions & 0 deletions phone_agent/utils/regex_util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import re

def escape_inner_quotes(expr: str) -> str:
"""
解析指令字符串,转义 value 内部嵌套的、未转义的双引号。
使用正向断言确保只匹配真正的结构化引号。
"""
def repl(match):
key = match.group(1)
val = match.group(2)
# 核心修复:把 value 内部「前面没有斜杠」的双引号加上斜杠
# 比如:把 [输入了""] 变成 [输入了\"\"]
fixed_val = re.sub(r'(?<!\\)"', r'\\"', val)
return f'{key}="{fixed_val}"'

# 优化后的正则表达式:
# (\w+)\s*=\s* : 匹配 key 和等号
# "([\s\S]*?)" : 匹配 value(非贪婪模式)
# (?=\s*[,)]) : 【重要】正向肯定断言,要求引号后面必须紧跟逗号或右括号
pattern = re.compile(r'(\w+)\s*=\s*"([\s\S]*?)"(?=\s*[,)])', re.DOTALL)
return pattern.sub(repl, expr)