Files
mini-openclaw-v2/backend/tools/fetch_url_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

60 lines
1.9 KiB
Python

"""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()