Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion agent/tools/web.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Web tools: search and fetch for online operations."""

import json
import socket
import urllib.request
import urllib.parse
import urllib.error
Expand Down Expand Up @@ -70,6 +71,8 @@ def execute(self, query: str, max_results: int = 5, **kw) -> ToolResult:
lines.append("")

return ToolResult(True, "\n".join(lines))
except socket.timeout:
return ToolResult(False, "", f"Search timeout: server took too long to respond")
except urllib.error.URLError as e:
return ToolResult(False, "", f"Search failed (network error): {e}")
except Exception as e:
Expand Down Expand Up @@ -167,6 +170,8 @@ def execute(self, url: str, max_length: int = 10000, **kw) -> ToolResult:
text = text[:max_length] + "\n\n... [content truncated]"

return ToolResult(True, f"Content from {url}:\n\n{text}")
except socket.timeout:
return ToolResult(False, "", f"Fetch timeout: server took too long to respond")
except urllib.error.HTTPError as e:
return ToolResult(False, "", f"HTTP {e.code}: {e.reason}")
except urllib.error.URLError as e:
Expand All @@ -180,7 +185,7 @@ def _html_to_text(self, html_content: str) -> str:
text = re.sub(r"<script[^>]*>.*?</script>", "", html_content, flags=re.DOTALL)
text = re.sub(r"<style[^>]*>.*?</style>", "", text, flags=re.DOTALL)
# Convert common tags
text = re.sub(r"<br\s*/?>", "\n", text)
text = re.sub(r"<br\s*/?>\n", text)
text = re.sub(r"</(p|div|h[1-6]|li|tr)>", "\n", text)
text = re.sub(r"<h([1-6])[^>]*>", lambda m: "\n" + "#" * int(m.group(1)) + " ", text)
text = re.sub(r"<li[^>]*>", " - ", text)
Expand Down
Loading