Class: SharedTools::Tools::BrowserTool

Inherits:
RubyLLM::Tool
  • Object
show all
Defined in:
lib/shared_tools/tools/browser_tool.rb

Overview

A tool for controller a browser.

Defined Under Namespace

Modules: Action

Constant Summary collapse

ACTIONS =
[
  Action::VISIT,
  Action::PAGE_INSPECT,
  Action::UI_INSPECT,
  Action::SELECTOR_INSPECT,
  Action::CLICK,
  Action::TEXT_FIELD_SET,
  Action::SCREENSHOT,
].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logger: nil, driver: nil) ⇒ BrowserTool

Returns a new instance of BrowserTool.

Parameters:

  • logger (Logger) (defaults to: nil)

    optional logger

  • driver (SharedTools::Tools::Browser::BaseDriver) (defaults to: nil)

    optional, will attempt to create WatirDriver when execute is called



150
151
152
153
# File 'lib/shared_tools/tools/browser_tool.rb', line 150

def initialize(logger: nil, driver: nil)
  @logger = logger || RubyLLM.logger
  @driver = driver  # Defer default_driver to execute time to support RubyLLM tool discovery
end

Instance Attribute Details

#driver=(value) ⇒ Object (writeonly)

Set driver after instantiation (useful when tool is discovered by RubyLLM)



156
157
158
# File 'lib/shared_tools/tools/browser_tool.rb', line 156

def driver=(value)
  @driver = value
end

Class Method Details

.nameObject



9
# File 'lib/shared_tools/tools/browser_tool.rb', line 9

def self.name = 'browser_tool'

Instance Method Details

#cleanup!Object



158
159
160
# File 'lib/shared_tools/tools/browser_tool.rb', line 158

def cleanup!
  @driver.close
end

#execute(action:, url: nil, selector: nil, value: nil, context_size: 2, full_html: false, text_content: nil) ⇒ String

Parameters:

  • action (String)
  • url (String, nil) (defaults to: nil)
  • selector (String, nil) (defaults to: nil)

    e.g. “button”, “div#parent > span.child”, etc

  • value (String, nil) (defaults to: nil)
  • context_size (Integer) (defaults to: 2)
  • full_html (Boolean) (defaults to: false)
  • text_content (String, nil) (defaults to: nil)

Returns:

  • (String)


171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/shared_tools/tools/browser_tool.rb', line 171

def execute(action:, url: nil, selector: nil, value: nil, context_size: 2, full_html: false, text_content: nil)
  # Lazily resolve driver at execute time
  @driver ||= default_driver

  case action.to_s.downcase
  when Action::VISIT
    require_param!(:url, url)
    visit_tool.execute(url:)
  when Action::PAGE_INSPECT
    if full_html
      page_inspect_tool.execute
    else
      page_inspect_tool.execute(summarize: true)
    end
  when Action::UI_INSPECT
    require_param!(:text_content, text_content)
    inspect_tool.execute(text_content:, selector:, context_size:)
  when Action::SELECTOR_INSPECT
    require_param!(:selector, selector)
    selector_inspect_tool.execute(selector:, context_size:)
  when Action::CLICK
    require_param!(:selector, selector)
    click_tool.execute(selector:)
  when Action::TEXT_FIELD_SET
    require_param!(:selector, selector)
    require_param!(:value, value)
    text_field_area_set_tool.execute(selector:, text: value)
  when Action::SCREENSHOT
    page_screenshot_tool.execute
  else
    { error: "Unsupported action: #{action}. Supported actions are: #{ACTIONS.join(', ')}" }
  end
end