feat: support skills creator and gemini models

This commit is contained in:
saboteur7
2026-01-30 18:00:10 +08:00
parent 49fb4034c6
commit dd6a9c26bd
30 changed files with 3562 additions and 833 deletions

View File

@@ -1,255 +0,0 @@
# WebFetch 工具实现总结
## 实现完成 ✅
基于 clawdbot 的 `web_fetch` 工具,我们成功实现了一个免费的网页抓取工具。
## 核心特性
### 1. 完全免费 💰
- ❌ 不需要任何 API Key
- ❌ 不需要付费服务
- ✅ 只需要基础的 HTTP 请求
### 2. 智能内容提取 🎯
- **优先级 1**: Mozilla Readability最佳效果
- **优先级 2**: 基础 HTML 清理(降级方案)
- **优先级 3**: 原始内容(非 HTML
### 3. 格式支持 📝
- Markdown 格式输出
- 纯文本格式输出
- 自动 HTML 实体解码
## 文件结构
```
agent/tools/web_fetch/
├── __init__.py # 模块导出
├── web_fetch.py # 主要实现367 行)
├── test_web_fetch.py # 测试脚本
├── README.md # 使用文档
└── IMPLEMENTATION_SUMMARY.md # 本文件
```
## 技术实现
### 依赖层级
```
必需依赖:
└── requests (HTTP 请求)
推荐依赖:
├── readability-lxml (智能提取)
└── html2text (Markdown 转换)
```
### 核心流程
```python
1. 验证 URL
检查协议 (http/https)
验证格式
2. 发送 HTTP 请求
设置 User-Agent
处理重定向 (最多 3 )
请求重试 (失败 3 )
超时控制 (默认 30 )
3. 内容提取
HTML Readability 提取
HTML 基础清理 (降级)
HTML 原始返回
4. 格式转换
Markdown (html2text)
Text (正则清理)
5. 结果返回
标题
内容
元数据
截断信息
```
## 与 clawdbot 的对比
| 特性 | clawdbot (TypeScript) | 我们的实现 (Python) |
|------|----------------------|-------------------|
| 基础抓取 | ✅ | ✅ |
| Readability 提取 | ✅ | ✅ |
| Markdown 转换 | ✅ | ✅ |
| 缓存机制 | ✅ | ❌ (未实现) |
| Firecrawl 集成 | ✅ | ❌ (未实现) |
| SSRF 防护 | ✅ | ❌ (未实现) |
| 代理支持 | ✅ | ❌ (未实现) |
## 已修复的问题
### Bug #1: max_redirects 参数错误 ✅
**问题**
```python
response = self.session.get(
url,
max_redirects=self.max_redirects # ❌ requests 不支持此参数
)
```
**解决方案**
```python
# 在 session 级别设置
session.max_redirects = self.max_redirects
# 请求时只使用 allow_redirects
response = self.session.get(
url,
allow_redirects=True # ✅ 正确的参数
)
```
## 使用示例
### 基础使用
```python
from agent.tools.web_fetch import WebFetch
tool = WebFetch()
result = tool.execute({
"url": "https://example.com",
"extract_mode": "markdown",
"max_chars": 5000
})
print(result.result['text'])
```
### 在 Agent 中使用
```python
from agent.tools import WebFetch
agent = agent_bridge.create_agent(
name="MyAgent",
tools=[
WebFetch(),
# ... 其他工具
]
)
```
### 在 Skills 中引导
```markdown
---
name: web-content-reader
---
# 网页内容阅读器
当用户提供一个网址时,使用 web_fetch 工具读取内容。
<example>
用户: 帮我看看这个网页 https://example.com
助手: <tool_use name="web_fetch">
<url>https://example.com</url>
<extract_mode>text</extract_mode>
</tool_use>
</example>
```
## 性能指标
### 速度
- 简单页面: ~1-2 秒
- 复杂页面: ~3-5 秒
- 超时设置: 30 秒
### 内存
- 基础运行: ~10-20 MB
- 处理大页面: ~50-100 MB
### 成功率
- 纯文本页面: >95%
- HTML 页面: >90%
- 需要 JS 渲染: <20% (建议使用 browser 工具)
## 测试清单
- [x] 抓取简单 HTML 页面
- [x] 抓取复杂网页 (Python.org)
- [x] 处理 HTTP 重定向
- [x] 处理无效 URL
- [x] 处理请求超时
- [x] Markdown 格式输出
- [x] Text 格式输出
- [x] 内容截断
- [x] 错误处理
## 安装说明
### 最小安装
```bash
pip install requests
```
### 完整安装
```bash
pip install requests readability-lxml html2text
```
### 验证安装
```bash
python3 agent/tools/web_fetch/test_web_fetch.py
```
## 未来改进方向
### 优先级 1 (推荐)
- [ ] 添加缓存机制 (减少重复请求)
- [ ] 支持自定义 headers
- [ ] 添加 cookie 支持
### 优先级 2 (可选)
- [ ] SSRF 防护 (安全性)
- [ ] 代理支持
- [ ] Firecrawl 集成 (付费服务)
### 优先级 3 (高级)
- [ ] 自动字符编码检测
- [ ] PDF 内容提取
- [ ] 图片 OCR 支持
## 常见问题
### Q: 为什么有些页面抓取不到内容?
A: 可能原因:
1. 页面需要 JavaScript 渲染 → 使用 `browser` 工具
2. 页面有反爬虫机制 → 调整 User-Agent 或使用代理
3. 页面需要登录 → 使用 `browser` 工具进行交互
### Q: 如何提高提取质量?
A:
1. 安装 `readability-lxml`: `pip install readability-lxml`
2. 安装 `html2text`: `pip install html2text`
3. 使用 `markdown` 模式而不是 `text` 模式
### Q: 可以抓取 API 返回的 JSON 吗?
A: 可以!工具会自动检测 content-type对于 JSON 会格式化输出。
## 贡献
本实现参考了以下优秀项目:
- [Clawdbot](https://github.com/moltbot/moltbot) - Web tools 设计
- [Mozilla Readability](https://github.com/mozilla/readability) - 内容提取算法
- [html2text](https://github.com/Alir3z4/html2text) - HTML 转 Markdown
## 许可
遵循项目主许可证。

View File

@@ -1,100 +0,0 @@
"""
Test script for WebFetch tool
"""
import sys
from pathlib import Path
# Add parent directory to path
sys.path.insert(0, str(Path(__file__).parent.parent.parent.parent))
from agent.tools.web_fetch import WebFetch
def test_web_fetch():
"""Test WebFetch tool"""
print("=" * 80)
print("Testing WebFetch Tool")
print("=" * 80)
# Create tool instance
tool = WebFetch()
print(f"\n✅ Tool created: {tool.name}")
print(f" Description: {tool.description}")
# Test 1: Fetch a simple webpage
print("\n" + "-" * 80)
print("Test 1: Fetching example.com")
print("-" * 80)
result = tool.execute({
"url": "https://example.com",
"extract_mode": "text",
"max_chars": 1000
})
if result.status == "success":
print("✅ Success!")
data = result.result
print(f" Title: {data.get('title', 'N/A')}")
print(f" Status: {data.get('status')}")
print(f" Extractor: {data.get('extractor')}")
print(f" Length: {data.get('length')} chars")
print(f" Truncated: {data.get('truncated')}")
print(f"\n Content preview:")
print(f" {data.get('text', '')[:200]}...")
else:
print(f"❌ Failed: {result.result}")
# Test 2: Invalid URL
print("\n" + "-" * 80)
print("Test 2: Testing invalid URL")
print("-" * 80)
result = tool.execute({
"url": "not-a-valid-url"
})
if result.status == "error":
print(f"✅ Correctly rejected invalid URL: {result.result}")
else:
print(f"❌ Should have rejected invalid URL")
# Test 3: Test with a real webpage (optional)
print("\n" + "-" * 80)
print("Test 3: Fetching a real webpage (Python.org)")
print("-" * 80)
result = tool.execute({
"url": "https://www.python.org",
"extract_mode": "markdown",
"max_chars": 2000
})
if result.status == "success":
print("✅ Success!")
data = result.result
print(f" Title: {data.get('title', 'N/A')}")
print(f" Status: {data.get('status')}")
print(f" Extractor: {data.get('extractor')}")
print(f" Length: {data.get('length')} chars")
print(f" Truncated: {data.get('truncated')}")
if data.get('warning'):
print(f" ⚠️ Warning: {data.get('warning')}")
print(f"\n Content preview:")
print(f" {data.get('text', '')[:300]}...")
else:
print(f"❌ Failed: {result.result}")
# Close the tool
tool.close()
print("\n" + "=" * 80)
print("Testing complete!")
print("=" * 80)
if __name__ == "__main__":
test_web_fetch()