Skip to content
Closed
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
6 changes: 6 additions & 0 deletions astrbot/core/computer/file_read_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,12 @@ async def read_file_tool_result(
workspace_dir: str | None = None,
) -> ToolExecResult:
if local_mode:
if Path(path).is_dir():

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

In an asyncio event loop, performing synchronous I/O operations like Path.is_dir() directly on the main thread can block the event loop and degrade the responsiveness of the entire application. Since to_thread is already imported in this file, it is highly recommended to run this check asynchronously in a separate thread.

Suggested change
if Path(path).is_dir():
if await to_thread(Path(path).is_dir):

return (
f"Error: `{path}` is a directory, not a file. "
"Provide a file path instead, or use `astrbot_execute_shell` "
"to list directory contents."
)
probe_payload = await _probe_local_file(path)
else:
probe_payload = await _exec_python_json(
Expand Down
19 changes: 19 additions & 0 deletions tests/test_computer_fs_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,25 @@ async def test_file_read_tool_allows_partial_read_for_large_text_file(
assert result == "".join(lines[1000:1003])


@pytest.mark.asyncio
async def test_file_read_tool_reports_directory_clearly(
monkeypatch: pytest.MonkeyPatch,
tmp_path,
):
workspace = _setup_local_fs_tools(monkeypatch, tmp_path)
(workspace / "some_dir").mkdir()

result = await fs_tools.FileReadTool().call(
_make_context(),
path="some_dir",
)

assert isinstance(result, str)
assert "is a directory" in result
assert "astrbot_execute_shell" in result
assert "Permission denied" not in result


@pytest.mark.asyncio
async def test_file_read_tool_returns_image_call_tool_result_for_images(
monkeypatch: pytest.MonkeyPatch,
Expand Down