chore: mini-openclaw 初始版本

Next.js 前端 + FastAPI/LangChain 后端透明 agent平台
- 含 .gitignore, 敏感文件(sessions数据/.env/venv等)已排除
- 待后续添加 trajectory 自进化机制
This commit is contained in:
NathanAlex7
2026-09-01 16:40:10 +08:00
committed by NathanAlex7
parent 73796a6abd
commit 42ce992a0f
76 changed files with 14207 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))
+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))
+48
View File
@@ -0,0 +1,48 @@
"""Skills Scanner — Scan skills/ directory and generate SKILLS_SNAPSHOT.md."""
from pathlib import Path
import yaml
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 = []
for skill_md in sorted(skills_dir.rglob("SKILL.md")):
try:
content = skill_md.read_text(encoding="utf-8")
# Parse YAML frontmatter
if content.startswith("---"):
parts = content.split("---", 2)
if len(parts) >= 3:
meta = yaml.safe_load(parts[1])
if meta:
rel_path = f"./backend/skills/{skill_md.parent.name}/SKILL.md"
skills.append({
"name": meta.get("name", skill_md.parent.name),
"description": meta.get("description", ""),
"location": rel_path,
})
except Exception as e:
print(f"⚠️ Error scanning {skill_md}: {e}")
# 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))