on main help sign in
chore: search improvements (#1632)
69d3ac9Kujtim Hoxha
internal/agent/tools/search.go+8−2
@@ -38,6 +38,10 @@ func searchDuckDuckGo(ctx context.Context, client *http.Client, query string, ma
38
38
39 req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
39 req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
40 req.Header.Set("User-Agent", BrowserUserAgent)
40 req.Header.Set("User-Agent", BrowserUserAgent)
41+ req.Header.Set("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
42+ req.Header.Set("Accept-Language", "en-US,en;q=0.5")
43+ req.Header.Set("Accept-Encoding", "gzip, deflate")
44+ req.Header.Set("Referer", "https://duckduckgo.com/")
41
45
42 resp, err := client.Do(req)
46 resp, err := client.Do(req)
43 if err != nil {
47 if err != nil {
@@ -45,8 +49,10 @@ func searchDuckDuckGo(ctx context.Context, client *http.Client, query string, ma
45 }
49 }
46 defer resp.Body.Close()
50 defer resp.Body.Close()
47
51
52+ // Accept both 200 (OK) and 202 (Accepted).
53+ // DuckDuckGo may still return 202 for rate limiting or bot detection.
48− if resp.StatusCode != http.StatusOK {
54+ if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusAccepted {
49− return nil, fmt.Errorf("search failed with status code: %d", resp.StatusCode)
55+ return nil, fmt.Errorf("search failed with status code: %d (DuckDuckGo may be rate limiting requests)", resp.StatusCode)
50 }
56 }
51
57
52 body, err := io.ReadAll(resp.Body)
58 body, err := io.ReadAll(resp.Body)