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>
This commit is contained in:
2026-09-29 12:58:26 +08:00
co-authored by Claude Code
commit d56c699ce6
224 changed files with 33392 additions and 0 deletions
+23
View File
@@ -0,0 +1,23 @@
"""Core Tools factory — returns all 5 tools for the Agent."""
from pathlib import Path
from typing import List
from langchain_core.tools import BaseTool
from .terminal_tool import create_terminal_tool
from .python_repl_tool import create_python_repl_tool
from .fetch_url_tool import create_fetch_url_tool
from .read_file_tool import create_read_file_tool
from .search_knowledge_tool import create_search_knowledge_tool
def get_all_tools(base_dir: Path) -> List[BaseTool]:
"""Create and return all 5 core tools, sandboxed to base_dir."""
return [
create_terminal_tool(base_dir),
create_python_repl_tool(),
create_fetch_url_tool(),
create_read_file_tool(base_dir),
create_search_knowledge_tool(base_dir),
]
+59
View File
@@ -0,0 +1,59 @@
"""FetchURLTool — Fetch a URL and return cleaned Markdown content."""
from typing import Type
import html2text
import requests
from langchain_core.tools import BaseTool
from pydantic import BaseModel, Field
class FetchURLInput(BaseModel):
url: str = Field(description="The URL to fetch content from")
class FetchURLTool(BaseTool):
name: str = "fetch_url"
description: str = (
"Fetch the content of a web page and return it as cleaned Markdown text. "
"Use this to retrieve information from the internet. "
"Input should be a valid URL (starting with http:// or https://)."
)
args_schema: Type[BaseModel] = FetchURLInput
def _run(self, url: str) -> str:
try:
headers = {
"User-Agent": "Mozilla/5.0 (compatible; MiniOpenClaw/0.1)"
}
resp = requests.get(url, headers=headers, timeout=15)
resp.raise_for_status()
content_type = resp.headers.get("content-type", "")
# If JSON, return directly
if "application/json" in content_type:
text = resp.text
if len(text) > 5000:
text = text[:5000] + "\n...[truncated]"
return text
# Convert HTML to Markdown
converter = html2text.HTML2Text()
converter.ignore_links = False
converter.ignore_images = True
converter.body_width = 0
markdown = converter.handle(resp.text)
if len(markdown) > 5000:
markdown = markdown[:5000] + "\n...[truncated]"
return markdown
except requests.Timeout:
return "❌ Request timed out (15s limit)"
except requests.RequestException as e:
return f"❌ Fetch error: {str(e)}"
def create_fetch_url_tool() -> FetchURLTool:
return FetchURLTool()
+17
View File
@@ -0,0 +1,17 @@
"""Python REPL Tool — wraps LangChain experimental PythonREPLTool."""
from langchain_core.tools import BaseTool
from langchain_experimental.tools import PythonREPLTool
def create_python_repl_tool() -> BaseTool:
tool = PythonREPLTool()
tool.name = "python_repl"
tool.description = (
"Execute Python code in an interactive REPL environment. "
"Use this for calculations, data processing, running scripts, "
"and any task that benefits from programmatic execution. "
"Input should be valid Python code. "
"Use print() to see output."
)
return tool
+53
View File
@@ -0,0 +1,53 @@
"""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))
+35
View File
@@ -0,0 +1,35 @@
"""Tools scoped to an immutable authorized risk run, not the production database."""
import json
from copy import deepcopy
from langchain_core.tools import tool
def create_risk_tools(context, evidence, native_skills):
snapshot = deepcopy(context)
sources = deepcopy(evidence)
skills = deepcopy(native_skills)
@tool
def list_data_cards() -> str:
"""List authorized peer data-card snapshots available for this risk task."""
return json.dumps([{"index":i,"name":d["card"],"version":d["version"],"records":len(d["records"])} for i,d in enumerate(snapshot.get("peer_datasets",[]))],ensure_ascii=False)
@tool
def read_data_card(index: int) -> str:
"""Read one authorized data-card snapshot, including periods and raw evidence."""
cards = snapshot.get("peer_datasets", [])
if index < 0 or index >= len(cards):
return json.dumps({"error":"数据卡不在本次授权范围"},ensure_ascii=False)
return json.dumps(cards[index],ensure_ascii=False)
@tool
def read_checks_and_rules() -> str:
"""Read deterministic Skill outputs, human rules and rule findings for this run."""
return json.dumps({"values":snapshot.get("scalar_values",{}),"human_rules":snapshot.get("human_rules",[]),"findings":snapshot.get("rule_findings",[]),"evidence":sources},ensure_ascii=False)
@tool
def read_selected_skill(skill_id: str) -> str:
"""Read only a native SKILL.md explicitly selected in the published plan."""
return skills.get(skill_id,"此 Skill 未被所选方案授权")
return [list_data_cards, read_data_card, read_checks_and_rules, read_selected_skill]
+102
View File
@@ -0,0 +1,102 @@
"""SearchKnowledgeBaseTool — LlamaIndex hybrid search (BM25 + Vector)."""
import os
from pathlib import Path
from typing import Type, Optional
from langchain_core.tools import BaseTool
from pydantic import BaseModel, Field
class SearchKnowledgeInput(BaseModel):
query: str = Field(description="The search query to find relevant knowledge")
class SearchKnowledgeBaseTool(BaseTool):
name: str = "search_knowledge_base"
description: str = (
"Search the local knowledge base using hybrid retrieval (keyword + semantic). "
"Use this when the user asks about specific knowledge or documents. "
"Returns the most relevant passages from the knowledge base."
)
args_schema: Type[BaseModel] = SearchKnowledgeInput
base_dir: str = ""
_index: Optional[object] = None
class Config:
arbitrary_types_allowed = True
def _build_index(self):
"""Build or load LlamaIndex index from knowledge/ directory."""
knowledge_dir = Path(self.base_dir) / "knowledge"
storage_dir = Path(self.base_dir) / "storage"
if not knowledge_dir.exists() or not any(knowledge_dir.iterdir()):
return None
try:
from llama_index.core import (
SimpleDirectoryReader,
StorageContext,
VectorStoreIndex,
load_index_from_storage,
)
from llama_index.core.settings import Settings
from llama_index.embeddings.openai import OpenAIEmbedding
Settings.embed_model = OpenAIEmbedding(
model=os.getenv("EMBEDDING_MODEL", "text-embedding-3-small"),
api_key=os.getenv("OPENAI_API_KEY"),
api_base=os.getenv("OPENAI_BASE_URL", "https://ai.devtool.tech/proxy/v1"),
)
# Try loading persisted index
if storage_dir.exists() and any(storage_dir.iterdir()):
try:
storage_context = StorageContext.from_defaults(
persist_dir=str(storage_dir)
)
return load_index_from_storage(storage_context)
except Exception:
pass
# Build fresh index
documents = SimpleDirectoryReader(
str(knowledge_dir), recursive=True
).load_data()
if not documents:
return None
index = VectorStoreIndex.from_documents(documents)
storage_dir.mkdir(parents=True, exist_ok=True)
index.storage_context.persist(persist_dir=str(storage_dir))
return index
except ImportError as e:
print(f"⚠️ LlamaIndex not fully installed: {e}")
return None
except Exception as e:
print(f"⚠️ Index build error: {e}")
return None
def _run(self, query: str) -> str:
if self._index is None:
self._index = self._build_index()
if self._index is None:
return "📭 Knowledge base is empty. Add documents to backend/knowledge/ to enable search."
try:
query_engine = self._index.as_query_engine(similarity_top_k=3)
response = query_engine.query(query)
result = str(response)
if len(result) > 5000:
result = result[:5000] + "\n...[truncated]"
return result
except Exception as e:
return f"❌ Search error: {str(e)}"
def create_search_knowledge_tool(base_dir: Path) -> SearchKnowledgeBaseTool:
return SearchKnowledgeBaseTool(base_dir=str(base_dir))
+118
View File
@@ -0,0 +1,118 @@
"""Skills Scanner — Scan skills/ directory and generate SKILLS_SNAPSHOT.md."""
from hashlib import sha256
from pathlib import Path
import re
import yaml
MAX_SKILL_BYTES = 64 * 1024
def parse_skill(content: str) -> dict:
"""Validate the small, declarative SKILL.md format used by the workbench."""
if not isinstance(content, str) or not content.startswith("---") or len(content.encode("utf-8")) > MAX_SKILL_BYTES:
raise ValueError("SKILL.md 缺少 YAML 头或超过 64 KB")
parts = content.split("---", 2)
meta = yaml.safe_load(parts[1]) if len(parts) == 3 else None
if not isinstance(meta, dict) or not isinstance(meta.get("name"), str) or not meta["name"].strip():
raise ValueError("SKILL.md 必须声明 name")
if not isinstance(meta.get("description"), str) or not meta["description"].strip():
raise ValueError("SKILL.md 必须声明 description")
details = meta.get("metadata", {})
if not isinstance(details, dict):
raise ValueError("SKILL.md 的 metadata 必须是对象")
for field in ("inputs", "audience"):
value = meta.get(field, details.get(field))
if value is not None and (not isinstance(value, list) or not all(isinstance(item, str) for item in value)):
raise ValueError(f"SKILL.md 的 {field} 必须是文本列表")
return meta
def skill_attributes(meta: dict) -> dict:
return {**meta, **meta.get("metadata", {})}
def skill_slug(meta: dict) -> str:
slug = re.sub(r"[^a-z0-9-]+", "-", meta["name"].strip().lower().replace("_", "-")).strip("-")
if not slug or len(slug) > 64:
raise ValueError("Skill 名称需使用 1–64 位英文小写字母、数字或连字符")
return slug
def install_skill(base_dir: Path, content: str) -> dict:
"""Install one uploaded SKILL.md under the managed imported-skills directory."""
meta = parse_skill(content)
slug = skill_slug(meta)
root = (base_dir / "skills").resolve()
target = (root / "imported" / slug / "SKILL.md").resolve()
if not target.is_relative_to(root):
raise ValueError("Skill 目录不在允许范围内")
if target.exists():
raise FileExistsError("同名 Skill 已存在,请先归档旧版本或修改 name")
target.parent.mkdir(parents=True, exist_ok=False)
temporary = target.with_suffix(".tmp")
temporary.write_text(content, encoding="utf-8")
temporary.replace(target)
legacy_id = target.relative_to(root).as_posix()
return {"id": legacy_id, "legacy_id": legacy_id, "name": meta["name"], "description": meta["description"], "meta": skill_attributes(meta)}
def discover_skills(base_dir: Path) -> list[dict]:
"""Read the native Skills catalog without changing its generated snapshot."""
root = (base_dir / "skills").resolve()
entries = []
for file in sorted(root.rglob("SKILL.md")):
resolved = file.resolve()
if not resolved.is_relative_to(root):
continue
try:
content = file.read_text(encoding="utf-8")
meta = parse_skill(content)
attributes = skill_attributes(meta)
if str(attributes.get("status", "published")) == "archived":
continue
legacy_id = file.relative_to(root).as_posix()
entries.append({
"id": legacy_id,
"component_id": str(attributes.get("id", legacy_id)),
"legacy_id": legacy_id,
"kind": str(attributes.get("kind", "skill")),
"version": str(attributes.get("version", "legacy")),
"schema_version": int(attributes.get("schema_version", 1)),
"status": str(attributes.get("status", "published")),
"name": str(meta.get("name", file.parent.name)),
"description": str(meta.get("description", "")),
"content_hash": sha256(content.encode("utf-8")).hexdigest(),
"content": content,
})
except (OSError, ValueError, TypeError, yaml.YAMLError):
continue
return entries
def scan_skills(base_dir: Path) -> str:
"""Scan all SKILL.md files and generate SKILLS_SNAPSHOT.md."""
skills_dir = base_dir / "skills"
snapshot_path = base_dir / "SKILLS_SNAPSHOT.md"
if not skills_dir.exists():
skills_dir.mkdir(parents=True)
skills = [{**s, "location":f"./backend/skills/{s['legacy_id']}"} for s in discover_skills(base_dir)]
# Build XML-style snapshot
lines = ["<available_skills>"]
for s in skills:
lines.append(" <skill>")
lines.append(f" <name>{s['name']}</name>")
lines.append(f" <description>{s['description']}</description>")
lines.append(f" <location>{s['location']}</location>")
lines.append(" </skill>")
lines.append("</available_skills>")
snapshot = "\n".join(lines)
snapshot_path.write_text(snapshot, encoding="utf-8")
print(f"📋 Skills snapshot: {len(skills)} skills found")
return snapshot
+78
View File
@@ -0,0 +1,78 @@
"""SafeTerminalTool — sandboxed shell execution with command blacklist."""
import subprocess
from pathlib import Path
from typing import Type
from langchain_core.tools import BaseTool
from pydantic import BaseModel, Field
BLACKLISTED_COMMANDS = [
"rm -rf /",
"rm -rf /*",
"mkfs",
"dd if=",
":(){:|:&};:",
"chmod -R 777 /",
"shutdown",
"reboot",
"halt",
"poweroff",
"format c:",
"del /f /s /q c:",
]
class TerminalInput(BaseModel):
command: str = Field(description="The shell command to execute")
class SafeTerminalTool(BaseTool):
name: str = "terminal"
description: str = (
"Execute shell commands in a sandboxed environment. "
"The working directory is restricted to the project root. "
"Use this for file operations, installing packages, running scripts, etc."
)
args_schema: Type[BaseModel] = TerminalInput
root_dir: str = ""
def _is_safe(self, command: str) -> bool:
cmd_lower = command.lower().strip()
for blocked in BLACKLISTED_COMMANDS:
if blocked in cmd_lower:
return False
return True
def _run(self, command: str) -> str:
if not self._is_safe(command):
return f"❌ Command blocked for safety: {command}"
try:
result = subprocess.run(
command,
shell=True,
cwd=self.root_dir,
capture_output=True,
text=True,
timeout=30,
encoding="utf-8",
errors="replace",
)
output = result.stdout
if result.stderr:
output += f"\n[stderr]: {result.stderr}"
if not output.strip():
output = "(command completed with no output)"
# Truncate very long output
if len(output) > 5000:
output = output[:5000] + "\n...[truncated]"
return output
except subprocess.TimeoutExpired:
return "❌ Command timed out (30s limit)"
except Exception as e:
return f"❌ Error: {str(e)}"
def create_terminal_tool(base_dir: Path) -> SafeTerminalTool:
return SafeTerminalTool(root_dir=str(base_dir))