Class: Botiasloop::Channels::CLI

Inherits:
Base
  • Object
show all
Defined in:
lib/botiasloop/channels/cli.rb

Constant Summary collapse

EXIT_COMMANDS =
%w[exit quit \q].freeze
SOURCE_ID =
"cli"

Instance Method Summary collapse

Methods inherited from Base

#after_process, #before_process, #channel_config, channel_name, #channel_type, #chat_for, #extract_user_id, #format_message, #handle_unauthorized, #process_message, required_config_keys, requires_config, #send_message

Constructor Details

#initializeCLI

Initialize CLI channel



12
13
14
15
# File 'lib/botiasloop/channels/cli.rb', line 12

def initialize
  super
  @running = false
end

Instance Method Details

#authorized?(_source_id) ⇒ Boolean

Check if source is authorized (CLI is always authorized)

Parameters:

  • source_id (String)

    Source identifier to check

Returns:

  • (Boolean)

    Always true for CLI



69
70
71
# File 'lib/botiasloop/channels/cli.rb', line 69

def authorized?(_source_id)
  true
end

#deliver_message(_source_id, formatted_content) ⇒ Object

Deliver a formatted message to the CLI

Parameters:

  • source_id (String)

    Source identifier

  • formatted_content (String)

    Formatted message content



88
89
90
91
# File 'lib/botiasloop/channels/cli.rb', line 88

def deliver_message(_source_id, formatted_content)
  puts "Agent: #{formatted_content}"
  puts
end

#extract_content(raw_message) ⇒ String

Extract content from raw message For CLI, the raw message is already the content string

Parameters:

  • raw_message (String)

    Raw message (already a string)

Returns:

  • (String)

    The content



61
62
63
# File 'lib/botiasloop/channels/cli.rb', line 61

def extract_content(raw_message)
  raw_message
end

#handle_error(source_id, _user_id, error, _raw_message) ⇒ Object

Handle errors by sending error message to user

Parameters:

  • source_id (String)

    Source identifier

  • user_id (String)

    User ID

  • error (Exception)

    The error that occurred

  • raw_message (Object)

    Raw message object



79
80
81
82
# File 'lib/botiasloop/channels/cli.rb', line 79

def handle_error(source_id, _user_id, error, _raw_message)
  Logger.error "[CLI] Error processing message: #{error.message}"
  send_message(source_id, "Error: #{error.message}")
end

#running?Boolean

Check if CLI channel is running

Returns:

  • (Boolean)

    True if running



52
53
54
# File 'lib/botiasloop/channels/cli.rb', line 52

def running?
  @running
end

#start_listeningObject

Start the CLI interactive mode



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/botiasloop/channels/cli.rb', line 18

def start_listening
  @running = true
  Logger.info "[CLI] Starting interactive mode..."

  puts "botiasloop v#{VERSION} - Interactive Mode"
  puts "Type 'exit', 'quit', or '\\q' to exit"
  puts

  while @running
    print "You: "
    input = $stdin.gets&.chomp
    break if input.nil? || EXIT_COMMANDS.include?(input.downcase)

    puts
    process_message(SOURCE_ID, input)
  end

  @running = false
  Logger.info "[CLI] Interactive mode ended"
rescue Interrupt
  @running = false
  puts "\nGoodbye!"
  Logger.info "[CLI] Interrupted by user"
end

#stop_listeningObject

Stop the CLI channel



44
45
46
47
# File 'lib/botiasloop/channels/cli.rb', line 44

def stop_listening
  @running = false
  Logger.info "[CLI] Stopping..."
end