DockerSandbox + LangChain file/shell tools extracted into a standalone package. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
41 lines
1.2 KiB
Python
41 lines
1.2 KiB
Python
"""search_files.py – tool for finding files by name pattern inside the sandbox."""
|
||
|
||
from __future__ import annotations
|
||
|
||
from shlex import quote
|
||
from typing import TYPE_CHECKING
|
||
|
||
from langchain_core.tools import BaseTool, tool
|
||
from loguru import logger
|
||
|
||
if TYPE_CHECKING:
|
||
from docker_agent_sandbox.sandbox import DockerSandbox
|
||
|
||
|
||
def make_search_files_tool(sandbox: "DockerSandbox") -> BaseTool:
|
||
"""Return a search_files tool bound to *sandbox*."""
|
||
|
||
@tool
|
||
def search_files(pattern: str, directory: str = ".") -> str:
|
||
"""
|
||
Find files whose names match *pattern* (shell glob) under *directory*.
|
||
|
||
Examples::
|
||
|
||
search_files("*.so", "/tmp/re-agent")
|
||
search_files("main", "/usr/bin")
|
||
|
||
Returns a newline-separated list of matching paths, or an error message.
|
||
"""
|
||
logger.debug(
|
||
"Searching files inside sandbox: pattern={!r} dir={!r}", pattern, directory
|
||
)
|
||
exit_code, output = sandbox.exec(
|
||
f"find {quote(directory)} -name {quote(pattern)} -print 2>/dev/null"
|
||
)
|
||
if exit_code != 0:
|
||
return f"[ERROR searching {directory!r}] {output.strip()}"
|
||
return output.strip() or "[no matches found]"
|
||
|
||
return search_files
|