feat: enhance tool prompt formatting and add escaped text decoding for file editing

This commit is contained in:
Soulter
2026-04-07 21:32:11 +08:00
parent 8e7d995fec
commit 11282c769f
2 changed files with 15 additions and 3 deletions

View File

@@ -1350,7 +1350,7 @@ async def build_main_agent(
)
if config.computer_use_runtime == "local":
tool_prompt += f"Current workspace you can use: `{os.path.join(get_astrbot_workspaces_path(), event.unified_msg_origin)}`\n"
tool_prompt += f"\nCurrent workspace you can use: `{os.path.join(get_astrbot_workspaces_path(), event.unified_msg_origin)}`\n"
req.system_prompt += f"\n{tool_prompt}\n"

View File

@@ -151,6 +151,16 @@ def _normalize_rw_path(
return normalized_path
def _decode_escaped_text(value: str) -> str:
"""Decode common escaped control sequences used in tool arguments."""
return (
value.replace("\\r\\n", "\n")
.replace("\\n", "\n")
.replace("\\r", "\r")
.replace("\\t", "\t")
)
@dataclass
class FileReadTool(FunctionTool):
name: str = "astrbot_file_read_tool"
@@ -359,14 +369,16 @@ class FileEditTool(FunctionTool):
)
if not normalized_path:
raise ValueError("`path` must be a non-empty string.")
normalized_old = _decode_escaped_text(old)
normalized_new = _decode_escaped_text(new)
sb = await get_booter(
context.context.context,
context.context.event.unified_msg_origin,
)
result = await sb.fs.edit_file(
path=normalized_path,
old_string=old,
new_string=new,
old_string=normalized_old,
new_string=normalized_new,
replace_all=replace_all,
encoding="utf-8",
)