fix: URL-encode search query and detect empty results as failure (fixes #274)

- Use urllib.parse.urlencode to properly encode the POST body, so queries
  containing special characters like '&', '+', '=' are sent correctly
- Extend execution_failure_check to also detect "No search results" as a
  failure, preventing the agent from silently looping when all SearXNG
  engines are rate-limited and return no results
This commit is contained in:
octo-patch
2026-04-10 10:07:06 +08:00
parent 31205febfe
commit a976afdbd7
+10 -2
View File
@@ -1,6 +1,7 @@
import requests import requests
from bs4 import BeautifulSoup from bs4 import BeautifulSoup
import os import os
from urllib.parse import urlencode
if __name__ == "__main__": # if running as a script for individual testing if __name__ == "__main__": # if running as a script for individual testing
sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
@@ -77,7 +78,14 @@ class searxSearch(Tools):
'Upgrade-Insecure-Requests': '1', 'Upgrade-Insecure-Requests': '1',
'User-Agent': self.user_agent 'User-Agent': self.user_agent
} }
data = f"q={query}&categories=general&language=auto&time_range=&safesearch=0&theme=simple".encode('utf-8') data = urlencode({
'q': query,
'categories': 'general',
'language': 'auto',
'time_range': '',
'safesearch': '0',
'theme': 'simple'
}).encode('utf-8')
try: try:
response = requests.post(search_url, headers=headers, data=data, verify=False) response = requests.post(search_url, headers=headers, data=data, verify=False)
response.raise_for_status() response.raise_for_status()
@@ -101,7 +109,7 @@ class searxSearch(Tools):
""" """
Checks if the execution failed based on the output. Checks if the execution failed based on the output.
""" """
return "Error" in output return "Error" in output or "No search results" in output
def interpreter_feedback(self, output: str) -> str: def interpreter_feedback(self, output: str) -> str:
""" """