Class: RubynCode::Tools::WebSearch

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

Constant Summary collapse

TOOL_NAME =
'web_search'
DESCRIPTION =
'Search the web for information. Returns search results with titles, URLs, and snippets.'
PARAMETERS =
{
  query: { type: :string, required: true, description: 'The search query string' },
  num_results: {
    type: :integer, required: false, default: 5,
    description: 'Number of results (default: 5)'
  }
}.freeze
RISK_LEVEL =
:external
REQUIRES_CONFIRMATION =
true
ADAPTERS =

Adapter registry – add new providers here

{
  'duckduckgo' => :search_duckduckgo,
  'brave' => :search_brave,
  'serpapi' => :search_serpapi,
  'tavily' => :search_tavily,
  'google' => :search_google
}.freeze

Instance Attribute Summary

Attributes inherited from Base

#project_root

Instance Method Summary collapse

Methods inherited from Base

description, #initialize, parameters, requires_confirmation?, risk_level, #safe_path, summarize, to_schema, tool_name, #truncate

Constructor Details

This class inherits a constructor from RubynCode::Tools::Base

Instance Method Details

#execute(query:, num_results: 5) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/rubyn_code/tools/web_search.rb', line 34

def execute(query:, num_results: 5)
  num_results = num_results.to_i.clamp(1, 20)
  provider = detect_provider

  results = send(ADAPTERS[provider], query, num_results)

  if results.empty?
    "No results found for: #{query}"
  else
    format_results(query, results, provider)
  end
rescue StandardError => e
  "Search failed (#{detect_provider}): #{e.message}"
end