fix(MAT-13): Add DNS fallback via web search for browse_url
When browse_url fails with DNS resolution error (common with STT-misrecognized domain names like "klicksports" instead of "clicksports"), automatically try a web search to find the correct domain and retry. Applied to both text and voice bot. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
18
voice.py
18
voice.py
@@ -893,6 +893,24 @@ class VoiceSession:
|
||||
Returns the page text content."""
|
||||
logger.info("BROWSE: %s", url)
|
||||
result = await _fetch_webpage(url)
|
||||
# On DNS failure, try to find the correct URL via web search
|
||||
if "Name or service not known" in result or "Failed to fetch" in result:
|
||||
from urllib.parse import urlparse
|
||||
domain = urlparse(url).netloc or url
|
||||
logger.info("BROWSE_DNS_FAIL: %s — trying web search for correct domain", domain)
|
||||
search_result = await _brave_search(f"{domain} website")
|
||||
if search_result and "No results" not in search_result:
|
||||
# Extract first URL from search results
|
||||
import re as _re
|
||||
url_match = _re.search(r'https?://[^\s\)]+', search_result)
|
||||
if url_match:
|
||||
corrected_url = url_match.group(0).rstrip('.,;')
|
||||
logger.info("BROWSE_RETRY: trying %s (from search)", corrected_url)
|
||||
result = await _fetch_webpage(corrected_url)
|
||||
if "Failed to fetch" not in result:
|
||||
logger.info("BROWSE_OK: %d chars from %s (corrected)", len(result), corrected_url)
|
||||
return f"[Note: Original URL {url} was unreachable. Found correct site at {corrected_url}]\n\n{result}"
|
||||
return f"Could not reach {url} and web search did not find an alternative. The domain name may be misspelled — ask the user to clarify."
|
||||
logger.info("BROWSE_OK: %d chars from %s", len(result), url)
|
||||
return result
|
||||
|
||||
|
||||
Reference in New Issue
Block a user