Class: RobotLab::AskUser

Inherits:
Tool
  • Object
show all
Defined in:
lib/robot_lab/ask_user.rb

Overview

Tool that lets a robot ask the user a question via the terminal.

The LLM decides when human input is needed and calls this tool with a question. Supports open-ended text, multiple choice, and default values for confirmation-style prompts.

IO is sourced from the owning robot’s input / output accessors, falling back to $stdin / $stdout.

Examples:

Open-ended question

# LLM calls: ask_user(question: "What is your name?")
# Terminal shows:
#   [helper] What is your name?
#   >

Multiple choice

# LLM calls: ask_user(question: "Pick a language:", choices: ["Ruby", "Python", "Go"])
# Terminal shows:
#   [helper] Pick a language:
#     1. Ruby
#     2. Python
#     3. Go
#   >

With default

# LLM calls: ask_user(question: "Continue?", default: "yes")
# Terminal shows:
#   [helper] Continue?
#   > [yes]

Instance Attribute Summary

Attributes inherited from Tool

#mcp, #robot

Instance Method Summary collapse

Methods inherited from Tool

#call, create, #initialize, #mcp?, #name, ractor_safe, raise_on_error?, #to_h, #to_json, #to_json_schema

Constructor Details

This class inherits a constructor from RobotLab::Tool

Instance Method Details

#execute(question:, choices: nil, default: nil) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/robot_lab/ask_user.rb', line 40

def execute(question:, choices: nil, default: nil)
  out = output_io
  label = robot&.name || "Robot"

  out.puts "\n[#{label}] #{question}"

  if choices.is_a?(Array) && choices.any?
    choices.each_with_index { |c, i| out.puts "  #{i + 1}. #{c}" }
  end

  prompt = default ? "> [#{default}] " : "> "
  out.print prompt
  out.flush

  response = input_io.gets&.chomp || ""
  response = default if response.empty? && default

  if choices.is_a?(Array) && choices.any? && response.match?(/\A\d+\z/)
    idx = response.to_i - 1
    response = choices[idx] if idx >= 0 && idx < choices.size
  end

  response
end