Class: OmniAI::Tools::BrowserTool

Inherits:
OmniAI::Tool
  • Object
show all
Defined in:
lib/omniai/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 Method Summary collapse

Constructor Details

#initialize(logger:, driver:) ⇒ BrowserTool

Returns a new instance of BrowserTool.

Parameters:



146
147
148
149
150
# File 'lib/omniai/tools/browser_tool.rb', line 146

def initialize(logger:, driver:)
  super()
  @logger = logger
  @driver = driver
end

Instance Method Details

#cleanup!Object



152
153
154
# File 'lib/omniai/tools/browser_tool.rb', line 152

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)


165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/omniai/tools/browser_tool.rb', line 165

def execute(action:, url: nil, selector: nil, value: nil, context_size: 2, full_html: false, text_content: nil)
  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