Class: AIHype::PTYController

Inherits:
Object
  • Object
show all
Defined in:
lib/aihype/pty_controller.rb

Constant Summary collapse

PROMPT_TIMEOUT =
0.5
BUFFER_LINES =
20

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command:, args: [], config_path:, log_path:, verbose: false) ⇒ PTYController

Returns a new instance of PTYController.



17
18
19
20
21
22
23
24
25
# File 'lib/aihype/pty_controller.rb', line 17

def initialize(command:, args: [], config_path:, log_path:, verbose: false)
  @command = command
  @args = args
  @config_path = config_path
  @log_path = log_path
  @verbose = verbose
  @line_buffer = String.new
  @recent_lines = []
end

Instance Attribute Details

#ai_matcherObject (readonly)

Returns the value of attribute ai_matcher.



12
13
14
# File 'lib/aihype/pty_controller.rb', line 12

def ai_matcher
  @ai_matcher
end

#argsObject (readonly)

Returns the value of attribute args.



12
13
14
# File 'lib/aihype/pty_controller.rb', line 12

def args
  @args
end

#blacklistObject (readonly)

Returns the value of attribute blacklist.



12
13
14
# File 'lib/aihype/pty_controller.rb', line 12

def blacklist
  @blacklist
end

#commandObject (readonly)

Returns the value of attribute command.



12
13
14
# File 'lib/aihype/pty_controller.rb', line 12

def command
  @command
end

#loggerObject (readonly)

Returns the value of attribute logger.



12
13
14
# File 'lib/aihype/pty_controller.rb', line 12

def logger
  @logger
end

Instance Method Details

#runObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/aihype/pty_controller.rb', line 27

def run
  # Load components
  @blacklist = Blacklist.from_file(@config_path)
  @ai_matcher = AIMatcher.new
  @logger = Logger.new(log_file_path: @log_path, verbose: @verbose)
  @core = Core.new(blacklist: @blacklist, ai_matcher: @ai_matcher, logger: @logger)

  @logger.log_startup(@blacklist.enabled_rules.length, @config_path)

  # Spawn child process via PTY
  PTY.spawn(@command, *@args) do |pty_out, pty_in, pid|
    exit_code = process_pty_io(pty_out, pty_in, pid)
    return exit_code
  end

  0
rescue InvalidMemoryFileError, Errno::ENOENT => e
  $stderr.puts "Error: Invalid or missing config file: #{e.message}"
  2
rescue StandardError => e
  $stderr.puts "Error: #{e.message}"
  $stderr.puts e.backtrace.join("\n") if @verbose
  1
end