Class: TouringTest::Agent
- Inherits:
-
Object
- Object
- TouringTest::Agent
- Defined in:
- lib/touring_test/agent.rb
Instance Attribute Summary collapse
-
#driver ⇒ Object
readonly
Returns the value of attribute driver.
-
#instruction ⇒ Object
readonly
Returns the value of attribute instruction.
Instance Method Summary collapse
-
#initialize(driver, instruction, max_steps: 15) ⇒ Agent
constructor
A new instance of Agent.
- #run ⇒ Object
Constructor Details
#initialize(driver, instruction, max_steps: 15) ⇒ Agent
Returns a new instance of Agent.
12 13 14 15 16 17 18 19 |
# File 'lib/touring_test/agent.rb', line 12 def initialize(driver, instruction, max_steps: 15) @driver = driver @instruction = instruction @api_key = ENV.fetch("GEMINI_API_KEY") @max_steps = max_steps @conversation_history = [] @log_file = setup_log_file end |
Instance Attribute Details
#driver ⇒ Object (readonly)
Returns the value of attribute driver.
10 11 12 |
# File 'lib/touring_test/agent.rb', line 10 def driver @driver end |
#instruction ⇒ Object (readonly)
Returns the value of attribute instruction.
10 11 12 |
# File 'lib/touring_test/agent.rb', line 10 def instruction @instruction end |
Instance Method Details
#run ⇒ Object
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 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 |
# File 'lib/touring_test/agent.rb', line 21 def run # Initial turn puts "[User] #{instruction}" screenshot_path, url = driver.capture_screenshot_and_url user_turn = build_user_turn(instruction, screenshot_path, url) @conversation_history << user_turn step_count = 0 loop do if step_count >= @max_steps raise "Agent exceeded maximum steps (#{@max_steps}). Halting execution to prevent infinite loop." end step_count += 1 puts "[Debug] === API Request Loop Iteration #{step_count}/#{@max_steps} ===" if debug? response = make_api_request(@conversation_history) model_turn = response.dig("candidates", 0, "content") @conversation_history << model_turn parts = model_turn["parts"] function_calls = parts.select { |part| part["functionCall"] } puts "[Debug] Found #{function_calls.size} function calls in response" if debug? break if function_calls.empty? function_responses = [] function_calls.each_with_index do |part, idx| function_call = part["functionCall"] next if function_call.nil? puts "[Debug] Processing function call #{idx + 1}/#{function_calls.size}: #{function_call['name']}" if debug? driver.execute_action(function_call) screenshot_path, url = driver.capture_screenshot_and_url puts "[Debug] Screenshot saved to: #{screenshot_path}" if debug? # Each function response includes its own screenshot function_responses << { "functionResponse" => { "name" => function_call["name"], "response" => { "url" => url } } } function_responses << { "inline_data" => { "mime_type" => "image/png", "data" => encode_image(screenshot_path) } } end user_turn = { "role" => "user", "parts" => function_responses } @conversation_history << user_turn end end |