Class: Foobara::Agent::CliRunner

Inherits:
Object
  • Object
show all
Defined in:
foobara-agent-cli-0.1.0/src/foobara/cli_runner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#agentObject

Returns the value of attribute agent.



4
5
6
# File 'src/foobara/cli_runner.rb', line 4

def agent
  @agent
end

#io_errObject

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_inObject

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_outObject

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_nameObject



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


81
82
83
84
# File 'src/foobara/cli_runner.rb', line 81

def print_agent_message(message, stream = io_out)
  name = agent_name || "Agent"
  Util.pipe_writeline(stream, "\n#{name} says: #{message}\n")
end


69
70
71
72
73
74
75
76
77
78
79
# File 'src/foobara/cli_runner.rb', line 69

def print_outcome(outcome)
  message, stream = if outcome.success?
                      [outcome.result[:message_to_user], io_out]
                    else
                      # :nocov:
                      ["ERROR: #{outcome.errors_hash}", io_err]
                      # :nocov:
                    end

  print_agent_message(message, stream)
end


65
66
67
# File 'src/foobara/cli_runner.rb', line 65

def print_prompt
  Util.pipe_write_with_flush(io_out, "\n> ")
end


53
54
55
56
57
58
59
60
61
62
63
# File 'src/foobara/cli_runner.rb', line 53

def print_welcome_message
  welcome_message = if agent_name
                      "Welcome! I am #{agent_name}!"
                    else
                      "Welcome to the Foobara Agent CLI!"
                    end

  welcome_message << " What would you like me to attempt to accomplish?"

  Util.pipe_writeline(io_out, "\n#{welcome_message}\n")
end

#runObject



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_welcome_message
  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)
      print_agent_message("Goodbye for now!\n")
      agent.kill!
      break
    end

    next if goal.empty?

    begin
      print_agent_message("On it...")
      outcome = agent.accomplish_goal(goal)
      print_outcome(outcome)
      print_prompt
    rescue => e
      # :nocov:
      Util.pipe_writeline(io_err, e.message)
      Util.pipe_writeline(io_err, e.backtrace)
      # :nocov:
    end
  end
end

#user_wants_to_quit?(goal) ⇒ Boolean

Returns:

  • (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