Merge remote-tracking branch 'origin/main'

This commit is contained in:
Hua
2026-03-24 12:31:06 +08:00
3 changed files with 38 additions and 4 deletions

View File

@@ -93,8 +93,10 @@ class ReadFileTool(_FsTool):
"required": ["path"], "required": ["path"],
} }
async def execute(self, path: str, offset: int = 1, limit: int | None = None, **kwargs: Any) -> Any: async def execute(self, path: str | None = None, offset: int = 1, limit: int | None = None, **kwargs: Any) -> Any:
try: try:
if not path:
return "Error reading file: Unknown path"
fp = self._resolve(path) fp = self._resolve(path)
if not fp.exists(): if not fp.exists():
return f"Error: File not found: {path}" return f"Error: File not found: {path}"
@@ -174,8 +176,12 @@ class WriteFileTool(_FsTool):
"required": ["path", "content"], "required": ["path", "content"],
} }
async def execute(self, path: str, content: str, **kwargs: Any) -> str: async def execute(self, path: str | None = None, content: str | None = None, **kwargs: Any) -> str:
try: try:
if not path:
raise ValueError("Unknown path")
if content is None:
raise ValueError("Unknown content")
fp = self._resolve(path) fp = self._resolve(path)
fp.parent.mkdir(parents=True, exist_ok=True) fp.parent.mkdir(parents=True, exist_ok=True)
fp.write_text(content, encoding="utf-8") fp.write_text(content, encoding="utf-8")
@@ -248,10 +254,18 @@ class EditFileTool(_FsTool):
} }
async def execute( async def execute(
self, path: str, old_text: str, new_text: str, self, path: str | None = None, old_text: str | None = None,
new_text: str | None = None,
replace_all: bool = False, **kwargs: Any, replace_all: bool = False, **kwargs: Any,
) -> str: ) -> str:
try: try:
if not path:
raise ValueError("Unknown path")
if old_text is None:
raise ValueError("Unknown old_text")
if new_text is None:
raise ValueError("Unknown new_text")
fp = self._resolve(path) fp = self._resolve(path)
if not fp.exists(): if not fp.exists():
return f"Error: File not found: {path}" return f"Error: File not found: {path}"
@@ -350,10 +364,12 @@ class ListDirTool(_FsTool):
} }
async def execute( async def execute(
self, path: str, recursive: bool = False, self, path: str | None = None, recursive: bool = False,
max_entries: int | None = None, **kwargs: Any, max_entries: int | None = None, **kwargs: Any,
) -> str: ) -> str:
try: try:
if path is None:
raise ValueError("Unknown path")
dp = self._resolve(path) dp = self._resolve(path)
if not dp.exists(): if not dp.exists():
return f"Error: Directory not found: {path}" return f"Error: Directory not found: {path}"

View File

@@ -70,6 +70,7 @@ dev = [
"matrix-nio[e2e]>=0.25.2", "matrix-nio[e2e]>=0.25.2",
"mistune>=3.0.0,<4.0.0", "mistune>=3.0.0,<4.0.0",
"nh3>=0.2.17,<1.0.0", "nh3>=0.2.17,<1.0.0",
"mypy>=1.19.1",
] ]
[project.scripts] [project.scripts]

View File

@@ -77,6 +77,11 @@ class TestReadFileTool:
assert "Error" in result assert "Error" in result
assert "not found" in result assert "not found" in result
@pytest.mark.asyncio
async def test_missing_path_returns_clear_error(self, tool):
result = await tool.execute()
assert result == "Error reading file: Unknown path"
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_char_budget_trims(self, tool, tmp_path): async def test_char_budget_trims(self, tool, tmp_path):
"""When the selected slice exceeds _MAX_CHARS the output is trimmed.""" """When the selected slice exceeds _MAX_CHARS the output is trimmed."""
@@ -200,6 +205,13 @@ class TestEditFileTool:
assert "Error" in result assert "Error" in result
assert "not found" in result assert "not found" in result
@pytest.mark.asyncio
async def test_missing_new_text_returns_clear_error(self, tool, tmp_path):
f = tmp_path / "a.py"
f.write_text("hello", encoding="utf-8")
result = await tool.execute(path=str(f), old_text="hello")
assert result == "Error editing file: Unknown new_text"
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
# ListDirTool # ListDirTool
@@ -263,6 +275,11 @@ class TestListDirTool:
assert "Error" in result assert "Error" in result
assert "not found" in result assert "not found" in result
@pytest.mark.asyncio
async def test_missing_path_returns_clear_error(self, tool):
result = await tool.execute()
assert result == "Error listing directory: Unknown path"
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
# Workspace restriction + extra_allowed_dirs # Workspace restriction + extra_allowed_dirs