Class: Foobara::Agent::CliRunner
- Inherits:
-
Object
- Object
- Foobara::Agent::CliRunner
- Defined in:
- foobara-agent-cli-0.1.0/src/foobara/cli_runner.rb
Instance Attribute Summary collapse
-
#agent ⇒ Object
Returns the value of attribute agent.
-
#io_err ⇒ Object
Returns the value of attribute io_err.
-
#io_in ⇒ Object
Returns the value of attribute io_in.
-
#io_out ⇒ Object
Returns the value of attribute io_out.
Instance Method Summary collapse
- #agent_name ⇒ Object
-
#initialize(agent, io_in: $stdin, io_out: $stdout, io_err: $stderr) ⇒ CliRunner
constructor
A new instance of CliRunner.
- #print_agent_message(message, stream = io_out) ⇒ Object
- #print_outcome(outcome) ⇒ Object
- #print_prompt ⇒ Object
- #print_welcome_message ⇒ Object
- #run ⇒ Object
- #user_wants_to_quit?(goal) ⇒ Boolean
Constructor Details
#initialize(agent, io_in: $stdin, io_out: $stdout, io_err: $stderr) ⇒ CliRunner
Returns a new instance of CliRunner.
6 7 8 9 10 11 |
# File 'src/foobara/cli_runner.rb', line 6 def initialize(agent, io_in: $stdin, io_out: $stdout, io_err: $stderr) self.agent = agent self.io_in = io_in self.io_out = io_out self.io_err = io_err end |
Instance Attribute Details
#agent ⇒ Object
Returns the value of attribute agent.
4 5 6 |
# File 'src/foobara/cli_runner.rb', line 4 def agent @agent end |
#io_err ⇒ Object
Returns the value of attribute io_err.
4 5 6 |
# File 'src/foobara/cli_runner.rb', line 4 def io_err @io_err end |
#io_in ⇒ Object
Returns the value of attribute io_in.
4 5 6 |
# File 'src/foobara/cli_runner.rb', line 4 def io_in @io_in end |
#io_out ⇒ Object
Returns the value of attribute io_out.
4 5 6 |
# File 'src/foobara/cli_runner.rb', line 4 def io_out @io_out end |
Instance Method Details
#agent_name ⇒ Object
100 101 102 103 104 105 106 |
# File 'src/foobara/cli_runner.rb', line 100 def agent_name name = agent.agent_name if name && !name.empty? name end end |
#print_agent_message(message, stream = io_out) ⇒ Object
81 82 83 84 |
# File 'src/foobara/cli_runner.rb', line 81 def (, stream = io_out) name = agent_name || "Agent" Util.pipe_writeline(stream, "\n#{name} says: #{}\n") end |
#print_outcome(outcome) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 |
# File 'src/foobara/cli_runner.rb', line 69 def print_outcome(outcome) , stream = if outcome.success? [outcome.result[:message_to_user], io_out] else # :nocov: ["ERROR: #{outcome.errors_hash}", io_err] # :nocov: end (, stream) end |
#print_prompt ⇒ Object
65 66 67 |
# File 'src/foobara/cli_runner.rb', line 65 def print_prompt Util.pipe_write_with_flush(io_out, "\n> ") end |
#print_welcome_message ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 |
# File 'src/foobara/cli_runner.rb', line 53 def = if agent_name "Welcome! I am #{agent_name}!" else "Welcome to the Foobara Agent CLI!" end << " What would you like me to attempt to accomplish?" Util.pipe_writeline(io_out, "\n#{}\n") end |
#run ⇒ Object
13 14 15 16 17 18 19 20 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 |
# File 'src/foobara/cli_runner.rb', line 13 def run print_prompt loop do ready = Util.pipe_wait_readable(io_in, 1) break if agent.killed? break if io_in.closed? next unless ready line = Util.pipe_readline(io_in) break if line.nil? || io_in.closed? || agent.killed? goal = line.strip if user_wants_to_quit?(goal) ("Goodbye for now!\n") agent.kill! break end next if goal.empty? begin ("On it...") outcome = agent.accomplish_goal(goal) print_outcome(outcome) print_prompt rescue => e # :nocov: Util.pipe_writeline(io_err, e.) Util.pipe_writeline(io_err, e.backtrace) # :nocov: end end end |
#user_wants_to_quit?(goal) ⇒ Boolean
86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'src/foobara/cli_runner.rb', line 86 def user_wants_to_quit?(goal) if goal =~ /\/?(exit|quit|(good)?.?bye)/i remainder = "#{::Regexp.last_match.pre_match}#{::Regexp.last_match.post_match}." remainder.strip! remainder.gsub!(/thanks?.?(you)?[.!]*/i, "") remainder.gsub!(/\A\s*\w+[.!]*/i, "") remainder.gsub!(/[.!]*\z/i, "") remainder.strip! remainder.empty? end end |