Build and run agents you can see, understand and trust.
Python agentchatbotlarge-language-modelsllmllm-agentmcpmulti-agentmulti-modalreact-agentrealtime-agentvoice-agentvoice-assistant
fix(tool): expand user home in text file tools for paths with tilde (#1262)
YingchaoX722a60c95531
diff --git a/src/agentscope/tool/_text_file/_view_text_file.py b/src/agentscope/tool/_text_file/_view_text_file.pyindex 7e2131f40..5955a0a76 100644--- a/src/agentscope/tool/_text_file/_view_text_file.py+++ b/src/agentscope/tool/_text_file/_view_text_file.py@@ -26,6 +26,7 @@ async def view_text_file(`ToolResponse`:The tool response containing the file content or an error message."""+ file_path = os.path.expanduser(file_path)if not os.path.exists(file_path):return ToolResponse(content=[diff --git a/src/agentscope/tool/_text_file/_write_text_file.py b/src/agentscope/tool/_text_file/_write_text_file.pyindex 74d50263c..e5e06ad9a 100644--- a/src/agentscope/tool/_text_file/_write_text_file.py+++ b/src/agentscope/tool/_text_file/_write_text_file.py@@ -41,6 +41,7 @@ async def insert_text_file(],)+ file_path = os.path.expanduser(file_path)if not os.path.exists(file_path):return ToolResponse(content=[@@ -124,7 +125,7 @@ async def write_text_file(`ToolResponse`:The tool response containing the result of the writing operation."""-+ file_path = os.path.expanduser(file_path)if not os.path.exists(file_path):with open(file_path, "w", encoding="utf-8") as file:file.write(content)diff --git a/tests/tool_test.py b/tests/tool_test.pyindex 7a2142b6a..7e7d1f10e 100644--- a/tests/tool_test.py+++ b/tests/tool_test.py@@ -205,6 +205,21 @@ print("456")"""res.content[0]["text"],)+ # Test tilde expansion: path with ~ should resolve to user home+ home = os.path.expanduser("~")+ test_name = f".agentscope_tool_test_{shortuuid.uuid()}.txt"+ tilde_path = f"~/{test_name}"+ real_path = os.path.join(home, test_name)+ try:+ with open(real_path, "w", encoding="utf-8") as f:+ f.write("tilde expansion works\n")+ res = await view_text_file(file_path=tilde_path)+ self.assertIn("tilde expansion works", res.content[0]["text"])+ self.assertIn("1: tilde expansion works", res.content[0]["text"])+ finally:+ if os.path.exists(real_path):+ os.remove(real_path)+async def test_write_text_file(self) -> None:"""Test writing to text file."""# create and write a new file