From 2aadb27ff322941f9029521f6e1f0e6ff5c0e0c5 Mon Sep 17 00:00:00 2001 From: Scott Severance Date: Sat, 8 Aug 2026 16:17:23 +0000 Subject: [PATCH] feat(web): add timeout and error handling for socket timeouts --- agent/tools/web.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/agent/tools/web.py b/agent/tools/web.py index 6213429..0b48325 100644 --- a/agent/tools/web.py +++ b/agent/tools/web.py @@ -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 @@ -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: @@ -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: @@ -180,7 +185,7 @@ def _html_to_text(self, html_content: str) -> str: text = re.sub(r"]*>.*?", "", html_content, flags=re.DOTALL) text = re.sub(r"]*>.*?", "", text, flags=re.DOTALL) # Convert common tags - text = re.sub(r"", "\n", text) + text = re.sub(r"\n", text) text = re.sub(r"", "\n", text) text = re.sub(r"]*>", lambda m: "\n" + "#" * int(m.group(1)) + " ", text) text = re.sub(r"]*>", " - ", text)