Files
mini-openclaw-v2/backend/tools/read_file_tool.py
T
qiaoqichaoandClaude Code d56c699ce6 chore(v2): 从兴邦预审v2白名单抽取独立可运行基座
按 docs/v2/05-implementation-plan.md §1 候选迁移表与 claude_use/T00 白名单,
从 55e2cdb 抽取 224 个文件(2.48 MB)到独立目录 F:/mini-OpenClaw-v2。

- 排除:凭据(.env/server-deploy.conf/旧DELIVERY.md)、运行数据(xingbang_data 等
  4 个 SQLite)、依赖(venv/node_modules 符号链接)、归档(_backup*/test-evidence/
  ori_file)、Office 临时文件
- 目标目录独立 git 仓库,初始恢复点;Gitea 远程与分支待 OQ-06 Q2 定案
- 未部署、未启动服务、未调用模型

Co-Authored-By: Claude Code <noreply@anthropic.com>
2026-09-29 12:58:26 +08:00

54 lines
1.7 KiB
Python

"""ReadFileTool — sandboxed file reading within project directory."""
from pathlib import Path
from typing import Type
from langchain_core.tools import BaseTool
from pydantic import BaseModel, Field
class ReadFileInput(BaseModel):
file_path: str = Field(
description="Relative path of the file to read (relative to project root)"
)
class SandboxedReadFileTool(BaseTool):
name: str = "read_file"
description: str = (
"Read the content of a local file. Path is relative to the project root. "
"Use this to read SKILL.md files, MEMORY.md, configuration files, etc. "
"Example: read_file('skills/get_weather/SKILL.md')"
)
args_schema: Type[BaseModel] = ReadFileInput
root_dir: str = ""
def _run(self, file_path: str) -> str:
try:
root = Path(self.root_dir)
# Normalize path
normalized = file_path.replace("\\", "/").lstrip("./")
full_path = (root / normalized).resolve()
# Sandbox check
if not str(full_path).startswith(str(root.resolve())):
return f"❌ Access denied: path escapes project root"
if not full_path.exists():
return f"❌ File not found: {file_path}"
if not full_path.is_file():
return f"❌ Not a file: {file_path}"
content = full_path.read_text(encoding="utf-8")
if len(content) > 10000:
content = content[:10000] + "\n...[truncated]"
return content
except Exception as e:
return f"❌ Error reading file: {str(e)}"
def create_read_file_tool(base_dir: Path) -> SandboxedReadFileTool:
return SandboxedReadFileTool(root_dir=str(base_dir))