feat(engine): 添加技能查看工具并优化异步任务管理 - 添加SkillViewTool到引擎加载器中,增强技能管理功能 - 在AgentLoop中引入_active_direct_task来跟踪活跃任务 - 实现直接任务执行时的同步处理逻辑 - 更新工具实例化方式以支持依赖注入 feat(config): 增加智能体运行时参数配置支持 - 扩展AgentDefaultsConfig添加max_tokens和temperature字段 - 实现配置解析函数_first_config_value处理多个配置源 - 支持通过Web API动态更新智能体运行时参数 - 添加前端页面配置表单和验证逻辑 refactor(provider): 统一最大令牌数参数类型为可选整型 - 将所有LLM提供者的max_tokens参数改为int | None类型 - 为AnthropicProvider实现模型特定的最大令牌数默认值 - 调整参数传递逻辑,优先级:调用参数 > 配置文件 > 模型默认值 - 移除硬编码的默认值,改用条件判断 feat(process): 增强事件投影功能 - 添加工具调用开始/结束事件的映射逻辑 - 实现技能激活事件的识别和展示 - 添加辅助函数处理工具调用名称和参数提取 - 优化运行记录关联逻辑,提升事件匹配准确性 fix(web): 更新网络请求客户端信任环境设置 - 将WebFetchTool和WebSearchTool的trust_env参数设为True - 确保HTTP客户端能够正确使用系统代理配置 - 修复可能的网络连接问题 test: 添加配置加载和事件投影相关测试 - 新增智能体默认参数配置测试用例 - 实现API配置持久化和重载测试 - 添加技能卡片和工具事件的投影测试 ```
118 lines
4.4 KiB
Python
118 lines
4.4 KiB
Python
"""No-key web search and fetch tools."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from dataclasses import dataclass, field
|
|
from html import unescape
|
|
import json
|
|
import re
|
|
from typing import Any
|
|
from urllib.parse import quote_plus, urlparse
|
|
|
|
import httpx
|
|
|
|
|
|
def _json_result(success: bool, **payload: Any) -> str:
|
|
return json.dumps({"success": success, **payload}, ensure_ascii=False, indent=2)
|
|
|
|
|
|
def _strip_html(value: str) -> str:
|
|
text = re.sub(r"(?is)<(script|style).*?>.*?</\1>", " ", value)
|
|
text = re.sub(r"(?s)<[^>]+>", " ", text)
|
|
text = unescape(text)
|
|
return re.sub(r"\s+", " ", text).strip()
|
|
|
|
|
|
def _safe_url(url: str) -> str:
|
|
parsed = urlparse(url)
|
|
if parsed.scheme not in {"http", "https"} or not parsed.netloc:
|
|
raise ValueError("url must be an http(s) URL")
|
|
return url
|
|
|
|
|
|
@dataclass(slots=True)
|
|
class WebFetchTool:
|
|
name: str = "web_fetch"
|
|
description: str = "Fetch a public HTTP(S) page and return readable text. No API key required."
|
|
toolset: str = "web"
|
|
always_available: bool = False
|
|
parameters: dict[str, Any] = field(
|
|
default_factory=lambda: {
|
|
"type": "object",
|
|
"properties": {
|
|
"url": {"type": "string", "description": "HTTP(S) URL to fetch."},
|
|
"max_chars": {"type": "integer", "default": 12000, "minimum": 1000, "maximum": 50000},
|
|
},
|
|
"required": ["url"],
|
|
}
|
|
)
|
|
|
|
async def execute(self, *, url: str, max_chars: int = 12000, **_: Any) -> str:
|
|
try:
|
|
safe_url = _safe_url(url)
|
|
limit = max(1000, min(int(max_chars or 12000), 50000))
|
|
async with httpx.AsyncClient(timeout=20, follow_redirects=True, trust_env=True) as client:
|
|
response = await client.get(
|
|
safe_url,
|
|
headers={"User-Agent": "Mozilla/5.0 Beaver/1.0"},
|
|
)
|
|
response.raise_for_status()
|
|
content_type = response.headers.get("content-type", "")
|
|
raw = response.text
|
|
text = _strip_html(raw) if "html" in content_type.lower() else raw
|
|
truncated = len(text) > limit
|
|
return _json_result(
|
|
True,
|
|
url=str(response.url),
|
|
status_code=response.status_code,
|
|
content_type=content_type,
|
|
content=text[:limit],
|
|
truncated=truncated,
|
|
)
|
|
except Exception as exc:
|
|
return _json_result(False, url=url, error=str(exc))
|
|
|
|
|
|
@dataclass(slots=True)
|
|
class WebSearchTool:
|
|
name: str = "web_search"
|
|
description: str = "Search the web using DuckDuckGo HTML results. No API key required."
|
|
toolset: str = "web"
|
|
always_available: bool = False
|
|
parameters: dict[str, Any] = field(
|
|
default_factory=lambda: {
|
|
"type": "object",
|
|
"properties": {
|
|
"query": {"type": "string", "description": "Search query."},
|
|
"limit": {"type": "integer", "default": 5, "minimum": 1, "maximum": 10},
|
|
},
|
|
"required": ["query"],
|
|
}
|
|
)
|
|
|
|
async def execute(self, *, query: str, limit: int = 5, **_: Any) -> str:
|
|
try:
|
|
if not str(query).strip():
|
|
raise ValueError("query is required")
|
|
bounded = max(1, min(int(limit or 5), 10))
|
|
url = f"https://duckduckgo.com/html/?q={quote_plus(query)}"
|
|
async with httpx.AsyncClient(timeout=20, follow_redirects=True, trust_env=True) as client:
|
|
response = await client.get(url, headers={"User-Agent": "Mozilla/5.0 Beaver/1.0"})
|
|
response.raise_for_status()
|
|
html = response.text
|
|
results: list[dict[str, str]] = []
|
|
pattern = re.compile(
|
|
r'<a[^>]+class="result__a"[^>]+href="(?P<url>[^"]+)"[^>]*>(?P<title>.*?)</a>',
|
|
re.I | re.S,
|
|
)
|
|
for match in pattern.finditer(html):
|
|
title = _strip_html(match.group("title"))
|
|
result_url = unescape(match.group("url"))
|
|
if title and result_url:
|
|
results.append({"title": title, "url": result_url, "snippet": ""})
|
|
if len(results) >= bounded:
|
|
break
|
|
return _json_result(True, query=query, results=results)
|
|
except Exception as exc:
|
|
return _json_result(False, query=query, error=str(exc))
|