-
Notifications
You must be signed in to change notification settings - Fork 72
Expand file tree
/
Copy pathweb.py
More file actions
464 lines (416 loc) · 18.5 KB
/
Copy pathweb.py
File metadata and controls
464 lines (416 loc) · 18.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
# Tencent is pleased to support the open source community by making tRPC-Agent-Python available.
#
# Copyright (C) 2026 Tencent. All rights reserved.
#
# This file is part of tRPC-Agent-Python and is licensed under Apache-2.0.
#
# Portions of this file are derived from HKUDS/nanobot (MIT License):
# https://github.com/HKUDS/nanobot.git
#
# Copyright (c) 2025 nanobot contributors
#
# See the project LICENSE / third-party attribution notices for details.
#
"""Web tools: web_search and web_fetch."""
from __future__ import annotations
import asyncio
import html
import json
import os
import re
from typing import Any
from typing import List
from typing import Optional
from urllib.parse import urlparse
import httpx
from nanobot.config.schema import WebSearchConfig
from trpc_agent_sdk.context import InvocationContext
from trpc_agent_sdk.filter import BaseFilter
from trpc_agent_sdk.log import logger
from trpc_agent_sdk.tools import BaseTool
from trpc_agent_sdk.types import FunctionDeclaration
from trpc_agent_sdk.types import Schema
from trpc_agent_sdk.types import Type
# ---------------------------------------------------------------------------
# Shared constants
# ---------------------------------------------------------------------------
USER_AGENT = "Mozilla/5.0 (Macintosh; Intel Mac OS X 14_7_2) AppleWebKit/537.36"
MAX_REDIRECTS = 5
# ---------------------------------------------------------------------------
# Shared helpers
# ---------------------------------------------------------------------------
def _strip_tags(text: str) -> str:
"""Remove HTML tags and decode entities."""
text = re.sub(r'<script[\s\S]*?</script>', '', text, flags=re.I)
text = re.sub(r'<style[\s\S]*?</style>', '', text, flags=re.I)
text = re.sub(r'<[^>]+>', '', text)
return html.unescape(text).strip()
def _normalize(text: str) -> str:
"""Normalize whitespace."""
text = re.sub(r'[ \t]+', ' ', text)
return re.sub(r'\n{3,}', '\n\n', text).strip()
def _validate_url(url: str) -> tuple[bool, str]:
"""Return (True, '') or (False, reason) for an http(s) URL."""
try:
p = urlparse(url)
if p.scheme not in ('http', 'https'):
return False, f"Only http/https allowed, got '{p.scheme or 'none'}'"
if not p.netloc:
return False, "Missing domain"
return True, ""
except Exception as e:
return False, str(e)
def _format_results(query: str, items: list[dict[str, Any]], n: int) -> str:
"""Format provider results into shared plaintext output."""
if not items:
return f"No results for: {query}"
lines = [f"Results for: {query}\n"]
for i, item in enumerate(items[:n], 1):
title = _normalize(_strip_tags(item.get("title", "")))
snippet = _normalize(_strip_tags(item.get("content", "")))
lines.append(f"{i}. {title}\n {item.get('url', '')}")
if snippet:
lines.append(f" {snippet}")
return "\n".join(lines)
# ---------------------------------------------------------------------------
# WebSearchTool
# ---------------------------------------------------------------------------
class WebSearchTool(BaseTool):
"""trpc_agent_sdk tool to search the web using a configured provider.
Args:
config: :class:`WebSearchConfig` instance; defaults to a fresh
one if *None*.
proxy: Optional HTTP proxy URL forwarded to httpx.
filters_name: Filter names forwarded to
:class:`~trpc_agent_sdk.tools.BaseTool`.
filters: Filter instances forwarded to
:class:`~trpc_agent_sdk.tools.BaseTool`.
"""
def __init__(
self,
config: Optional["WebSearchConfig"] = None,
proxy: Optional[str] = None,
filters_name: Optional[List[str]] = None,
filters: Optional[List[BaseFilter]] = None,
) -> None:
super().__init__(
name="web_search",
description="Search the web. Returns titles, URLs, and snippets.",
filters_name=filters_name,
filters=filters,
)
self._config = config if config is not None else WebSearchConfig()
self._proxy = proxy
# ------------------------------------------------------------------
# BaseTool interface
# ------------------------------------------------------------------
def _get_declaration(self) -> FunctionDeclaration:
return FunctionDeclaration(
name="web_search",
description="Search the web. Returns titles, URLs, and snippets.",
parameters=Schema(
type=Type.OBJECT,
properties={
"query":
Schema(type=Type.STRING, description="Search query"),
"count":
Schema(
type=Type.INTEGER,
description="Number of results to return (1-10)",
minimum=1,
maximum=10,
),
},
required=["query"],
),
)
async def _run_async_impl(self, *, tool_context: InvocationContext, args: dict[str, Any]) -> Any:
query: str = args.get("query", "")
count: Optional[int] = args.get("count")
provider = getattr(self._config, "provider", "brave").strip().lower()
n = min(max(count or self._config.max_results, 1), 10)
if provider == "duckduckgo":
return await self._search_duckduckgo(query, n)
if provider == "tavily":
return await self._search_tavily(query, n)
if provider == "searxng":
return await self._search_searxng(query, n)
if provider == "jina":
return await self._search_jina(query, n)
if provider == "brave":
return await self._search_brave(query, n)
return f"Error: unknown search provider '{provider}'"
# ------------------------------------------------------------------
# Private provider backends
# ------------------------------------------------------------------
async def _search_brave(self, query: str, n: int) -> str:
api_key = self._config.api_key or os.environ.get("BRAVE_API_KEY", "")
if not api_key:
logger.warning("BRAVE_API_KEY not set, falling back to DuckDuckGo")
return await self._search_duckduckgo(query, n)
try:
async with httpx.AsyncClient(proxy=self._proxy) as client:
r = await client.get(
"https://api.search.brave.com/res/v1/web/search",
params={
"q": query,
"count": n
},
headers={
"Accept": "application/json",
"X-Subscription-Token": api_key
},
timeout=10.0,
)
r.raise_for_status()
items = [{
"title": x.get("title", ""),
"url": x.get("url", ""),
"content": x.get("description", "")
} for x in r.json().get("web", {}).get("results", [])]
return _format_results(query, items, n)
except Exception as e: # pylint: disable=broad-except
return f"Error: {e}"
async def _search_tavily(self, query: str, n: int) -> str:
api_key = self._config.api_key or os.environ.get("TAVILY_API_KEY", "")
if not api_key:
logger.warning("TAVILY_API_KEY not set, falling back to DuckDuckGo")
return await self._search_duckduckgo(query, n)
try:
async with httpx.AsyncClient(proxy=self._proxy) as client:
r = await client.post(
"https://api.tavily.com/search",
headers={"Authorization": f"Bearer {api_key}"},
json={
"query": query,
"max_results": n
},
timeout=15.0,
)
r.raise_for_status()
return _format_results(query, r.json().get("results", []), n)
except Exception as e: # pylint: disable=broad-except
return f"Error: {e}"
async def _search_searxng(self, query: str, n: int) -> str:
base_url = (self._config.base_url or os.environ.get("SEARXNG_BASE_URL", "")).strip()
if not base_url:
logger.warning("SEARXNG_BASE_URL not set, falling back to DuckDuckGo")
return await self._search_duckduckgo(query, n)
endpoint = f"{base_url.rstrip('/')}/search"
is_valid, error_msg = _validate_url(endpoint)
if not is_valid:
return f"Error: invalid SearXNG URL: {error_msg}"
try:
async with httpx.AsyncClient(proxy=self._proxy) as client:
r = await client.get(
endpoint,
params={
"q": query,
"format": "json"
},
headers={"User-Agent": USER_AGENT},
timeout=10.0,
)
r.raise_for_status()
return _format_results(query, r.json().get("results", []), n)
except Exception as e: # pylint: disable=broad-except
return f"Error: {e}"
async def _search_jina(self, query: str, n: int) -> str:
api_key = self._config.api_key or os.environ.get("JINA_API_KEY", "")
if not api_key:
logger.warning("JINA_API_KEY not set, falling back to DuckDuckGo")
return await self._search_duckduckgo(query, n)
try:
headers = {"Accept": "application/json", "Authorization": f"Bearer {api_key}"}
async with httpx.AsyncClient(proxy=self._proxy) as client:
r = await client.get(
"https://s.jina.ai/",
params={"q": query},
headers=headers,
timeout=15.0,
)
r.raise_for_status()
data = r.json().get("data", [])[:n]
items = [{
"title": d.get("title", ""),
"url": d.get("url", ""),
"content": d.get("content", "")[:500]
} for d in data]
return _format_results(query, items, n)
except Exception as e: # pylint: disable=broad-except
return f"Error: {e}"
async def _search_duckduckgo(self, query: str, n: int) -> str:
try:
from ddgs import DDGS
ddgs = DDGS(timeout=10)
raw = await asyncio.to_thread(ddgs.text, query, max_results=n)
if not raw:
return f"No results for: {query}"
items = [{"title": r.get("title", ""), "url": r.get("href", ""), "content": r.get("body", "")} for r in raw]
return _format_results(query, items, n)
except Exception as e: # pylint: disable=broad-except
logger.warning("DuckDuckGo search failed: %s", e)
return f"Error: DuckDuckGo search failed ({e})"
# ---------------------------------------------------------------------------
# WebFetchTool
# ---------------------------------------------------------------------------
class WebFetchTool(BaseTool):
"""trpc_agent_sdk tool to fetch and extract readable content from a URL.
Args:
max_chars: Character limit for the returned text.
proxy: Optional HTTP proxy URL forwarded to httpx.
filters_name: Filter names forwarded to
:class:`~trpc_agent_sdk.tools.BaseTool`.
filters: Filter instances forwarded to
:class:`~trpc_agent_sdk.tools.BaseTool`.
"""
def __init__(
self,
max_chars: int = 50_000,
proxy: Optional[str] = None,
filters_name: Optional[List[str]] = None,
filters: Optional[List[BaseFilter]] = None,
) -> None:
super().__init__(
name="web_fetch",
description="Fetch URL and extract readable content (HTML → markdown/text).",
filters_name=filters_name,
filters=filters,
)
self._max_chars = max_chars
self._proxy = proxy
# ------------------------------------------------------------------
# BaseTool interface
# ------------------------------------------------------------------
def _get_declaration(self) -> FunctionDeclaration:
return FunctionDeclaration(
name="web_fetch",
description="Fetch URL and extract readable content (HTML → markdown/text).",
parameters=Schema(
type=Type.OBJECT,
properties={
"url":
Schema(type=Type.STRING, description="URL to fetch"),
"extractMode":
Schema(
type=Type.STRING,
enum=["markdown", "text"],
description="Extraction mode (default: markdown)",
),
"maxChars":
Schema(
type=Type.INTEGER,
description="Maximum characters to return",
minimum=100,
),
},
required=["url"],
),
)
async def _run_async_impl(self, *, tool_context: InvocationContext, args: dict[str, Any]) -> Any:
url: str = args.get("url", "")
extract_mode: str = args.get("extractMode", "markdown")
max_chars: int = int(args.get("maxChars") or self._max_chars)
is_valid, error_msg = _validate_url(url)
if not is_valid:
return json.dumps({"error": f"URL validation failed: {error_msg}", "url": url}, ensure_ascii=False)
result = await self._fetch_jina(url, max_chars)
if result is None:
result = await self._fetch_readability(url, extract_mode, max_chars)
return result
# ------------------------------------------------------------------
# Private fetch backends
# ------------------------------------------------------------------
async def _fetch_jina(self, url: str, max_chars: int) -> Optional[str]:
"""Try Jina Reader API; return None on failure."""
try:
headers = {"Accept": "application/json", "User-Agent": USER_AGENT}
jina_key = os.environ.get("JINA_API_KEY", "")
if jina_key:
headers["Authorization"] = f"Bearer {jina_key}"
async with httpx.AsyncClient(proxy=self._proxy, timeout=20.0) as client:
r = await client.get(f"https://r.jina.ai/{url}", headers=headers)
if r.status_code == 429:
logger.debug("Jina Reader rate limited, falling back to readability")
return None
r.raise_for_status()
data = r.json().get("data", {})
text = data.get("content", "")
if not text:
return None
title = data.get("title", "")
if title:
text = f"# {title}\n\n{text}"
truncated = len(text) > max_chars
if truncated:
text = text[:max_chars]
return json.dumps(
{
"url": url,
"finalUrl": data.get("url", url),
"status": r.status_code,
"extractor": "jina",
"truncated": truncated,
"length": len(text),
"text": text,
},
ensure_ascii=False)
except Exception as e: # pylint: disable=broad-except
logger.debug("Jina Reader failed for %s, falling back to readability: %s", url, e)
return None
async def _fetch_readability(self, url: str, extract_mode: str, max_chars: int) -> str:
"""Local fallback using readability-lxml."""
from readability import Document
try:
async with httpx.AsyncClient(
follow_redirects=True,
max_redirects=MAX_REDIRECTS,
timeout=30.0,
proxy=self._proxy,
) as client:
r = await client.get(url, headers={"User-Agent": USER_AGENT})
r.raise_for_status()
ctype = r.headers.get("content-type", "")
if "application/json" in ctype:
text, extractor = json.dumps(r.json(), indent=2, ensure_ascii=False), "json"
elif "text/html" in ctype or r.text[:256].lower().startswith(("<!doctype", "<html")):
doc = Document(r.text)
content = self._to_markdown(doc.summary()) if extract_mode == "markdown" else _strip_tags(doc.summary())
text = f"# {doc.title()}\n\n{content}" if doc.title() else content
extractor = "readability"
else:
text, extractor = r.text, "raw"
truncated = len(text) > max_chars
if truncated:
text = text[:max_chars]
return json.dumps(
{
"url": url,
"finalUrl": str(r.url),
"status": r.status_code,
"extractor": extractor,
"truncated": truncated,
"length": len(text),
"text": text,
},
ensure_ascii=False)
except httpx.ProxyError as e:
logger.error("WebFetch proxy error for %s: %s", url, e)
return json.dumps({"error": f"Proxy error: {e}", "url": url}, ensure_ascii=False)
except Exception as e: # pylint: disable=broad-except
logger.error("WebFetch error for %s: %s", url, e)
return json.dumps({"error": str(e), "url": url}, ensure_ascii=False)
def _to_markdown(self, html_content: str) -> str:
"""Convert HTML to markdown."""
text = re.sub(r'<a\s+[^>]*href=["\']([^"\']+)["\'][^>]*>([\s\S]*?)</a>',
lambda m: f'[{_strip_tags(m[2])}]({m[1]})',
html_content,
flags=re.I)
text = re.sub(r'<h([1-6])[^>]*>([\s\S]*?)</h\1>',
lambda m: f'\n{"#" * int(m[1])} {_strip_tags(m[2])}\n',
text,
flags=re.I)
text = re.sub(r'<li[^>]*>([\s\S]*?)</li>', lambda m: f'\n- {_strip_tags(m[1])}', text, flags=re.I)
text = re.sub(r'</(p|div|section|article)>', '\n\n', text, flags=re.I)
text = re.sub(r'<(br|hr)\s*/?>', '\n', text, flags=re.I)
return _normalize(_strip_tags(text))