Class: Clacky::Tools::WebSearch

Inherits:
Base
  • Object
show all
Defined in:
lib/clacky/tools/web_search.rb

Constant Summary collapse

PROVIDERS =

Ordered list of search providers to try in sequence. cn.bing.com is accessible in mainland China without VPN.

%i[duckduckgo bing].freeze
SEARCHER_TIMEOUT =
30
BING_ENDPOINTS =

── Bing ───────────────────────────────────────────────────────────────

[
  ["cn.bing.com", "zh-CN,zh;q=0.9,en;q=0.8"],
  ["www.bing.com", "en-US,en;q=0.9"]
].freeze
USER_AGENTS =

── Shared HTTP helper ─────────────────────────────────────────────────

[
  "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36",
  "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36",
  "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36",
  "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36",
  "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36"
].freeze

Instance Method Summary collapse

Methods inherited from Base

#category, #description, #name, #parameters, #to_function_definition

Instance Method Details

#execute(query:, max_results: 10, working_dir: nil) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/clacky/tools/web_search.rb', line 38

def execute(query:, max_results: 10, working_dir: nil)
  if query.nil? || query.strip.empty?
    return { error: "Query cannot be empty" }
  end

  last_error = nil

  providers = active_providers
  providers.each do |provider|
    begin
      results = send(:"search_#{provider}", query, max_results)
      # Consider it a success only if we got real results
      next if results.empty? || results.first[:url].include?("duckduckgo.com") && results.first[:title] == "Web search results"

      return {
        query: query,
        results: results,
        count: results.length,
        provider: provider == :custom ? Clacky::SearchConfig.load["provider"] : provider.to_s,
        error: nil
      }
    rescue StandardError => e
      # DuckDuckGo failed — suppress it for 10 minutes
      @ddg_unavailable_until = Time.now + 600 if provider == :duckduckgo
      last_error = e
      next
    end
  end

  # All providers failed
  {
    query: query,
    results: [],
    count: 0,
    provider: nil,
    error: "All search providers failed. Last error: #{last_error&.message}"
  }
end

#format_call(args) ⇒ Object

── Formatting ─────────────────────────────────────────────────────────



321
322
323
324
325
# File 'lib/clacky/tools/web_search.rb', line 321

def format_call(args)
  query = args[:query] || args["query"] || ""
  display_query = query.length > 40 ? "#{query[0..37]}..." : query
  "web_search(\"#{display_query}\")"
end

#format_result(result) ⇒ Object



327
328
329
330
331
332
333
334
335
# File 'lib/clacky/tools/web_search.rb', line 327

def format_result(result)
  if result[:error]
    "[Error] #{result[:error]}"
  else
    count = result[:count] || 0
    provider = result[:provider] ? " via #{result[:provider]}" : ""
    "[OK] Found #{count} results#{provider}"
  end
end