Class: Tidewave::Tools::BrowserEval

Inherits:
Tidewave::Tool show all
Defined in:
lib/tidewave/tools/browser_eval.rb

Constant Summary collapse

DESCRIPTION =
<<~DESCRIPTION
  Runs JavaScript in a real browser to interact with the application.

  You MUST use "help" action first to learn the full API.
DESCRIPTION
BROADCAST_TIMEOUT_MS =
5_000

Instance Method Summary collapse

Methods inherited from Tidewave::Tool

descendants, inherited, #validate_and_call

Constructor Details

#initialize(options = {}) ⇒ BrowserEval

Returns a new instance of BrowserEval.



12
13
14
15
# File 'lib/tidewave/tools/browser_eval.rb', line 12

def initialize(options = {})
  super
  @browser_control = options[:browser_control]
end

Instance Method Details

#browser_tool?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/tidewave/tools/browser_eval.rb', line 17

def browser_tool?
  true
end

#call(arguments, context = {}) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/tidewave/tools/browser_eval.rb', line 48

def call(arguments, context = {})
  url = context[:url]
  sid = arguments["sid"]

  if sid.is_a?(String) && !sid.empty?
    result = @browser_control.run(sid, "browser_eval", arguments, nil)
    direct_result(result, sid, url)
  else
    # The broadcast case is only expected to run for initial discovery.
    # We can safely retry once if the first attempt times out.
    result = @browser_control.broadcast_run("browser_eval", arguments, BROADCAST_TIMEOUT_MS)
    result = @browser_control.broadcast_run("browser_eval", arguments, BROADCAST_TIMEOUT_MS) if result == [ :error, :timeout ]
    broadcast_result(result, url)
  end
end

#definitionObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/tidewave/tools/browser_eval.rb', line 21

def definition
  return nil unless @browser_control

  {
    "name" => "browser_eval",
    "description" => DESCRIPTION,
    "inputSchema" => {
      "type" => "object",
      "properties" => {
        "action" => {
          "type" => "string"
        },
        "sid" => {
          "description" => 'The session to target, e.g. "nice-cactus#1".',
          "type" => "string"
        },
        "args" => {
          "description" => 'Parameters for the action, as documented by "help".',
          "type" => "object",
          "additionalProperties" => true
        }
      },
      "required" => [ "action" ]
    }
  }
end