Class: RailsConsoleAi::Channel::Api

Inherits:
Base
  • Object
show all
Defined in:
lib/rails_console_ai/channel/api.rb

Overview

Non-interactive channel used by the AgentRunner. It owns its own buffers — there is no parent channel and no user to prompt. The runner reads ‘captured_output` back as the agent’s ‘result`.

Mirrors Channel::Slack’s pattern of logging every display event to STDOUT (tagged) so the rake task log shows progress in real time, while ALSO buffering display output into ‘captured_output` for the runner to compose the final result.

Constant Summary collapse

ANSI_REGEX =
/\e\[[0-9;]*[a-zA-Z]/.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#edit_code

Constructor Details

#initialize(user_name: nil) ⇒ Api

Returns a new instance of Api.



18
19
20
21
22
# File 'lib/rails_console_ai/channel/api.rb', line 18

def initialize(user_name: nil)
  @user_name = user_name
  @captured_output = +''
  @status_log = []
end

Instance Attribute Details

#captured_outputObject (readonly)

Returns the value of attribute captured_output.



16
17
18
# File 'lib/rails_console_ai/channel/api.rb', line 16

def captured_output
  @captured_output
end

#status_logObject (readonly)

Returns the value of attribute status_log.



16
17
18
# File 'lib/rails_console_ai/channel/api.rb', line 16

def status_log
  @status_log
end

Instance Method Details

#cancelled?Boolean

Returns:

  • (Boolean)


101
102
103
# File 'lib/rails_console_ai/channel/api.rb', line 101

def cancelled?
  false
end

#confirm(_text) ⇒ Object



86
87
88
89
90
91
# File 'lib/rails_console_ai/channel/api.rb', line 86

def confirm(_text)
  # No human present to confirm. Auto-yes matches Channel::SubAgent's
  # behavior — actual safety comes from the safety guards plus
  # supports_danger? = false below, not from this prompt.
  'y'
end

#display(text) ⇒ Object



24
25
26
27
28
# File 'lib/rails_console_ai/channel/api.rb', line 24

def display(text)
  stripped = strip_ansi(text)
  @captured_output << stripped << "\n"
  log_prefixed(">>", stripped)
end

#display_code(code) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/rails_console_ai/channel/api.rb', line 42

def display_code(code)
  # Don't add to captured_output — code itself is persisted on the
  # session row as code_executed. But DO log it to STDOUT so the
  # rake task log shows what was generated.
  log_prefixed("(code)", '')
  code.to_s.each_line { |line| log_prefixed("(code)", line.rstrip) }
end

#display_error(text) ⇒ Object



76
77
78
79
80
# File 'lib/rails_console_ai/channel/api.rb', line 76

def display_error(text)
  stripped = strip_ansi(text)
  @status_log << "ERROR: #{stripped}"
  log_prefixed("(error)", stripped)
end

#display_result(text) ⇒ Object



30
31
32
33
34
# File 'lib/rails_console_ai/channel/api.rb', line 30

def display_result(text)
  stripped = strip_ansi(text)
  @captured_output << stripped << "\n"
  log_prefixed(">>", stripped)
end

#display_result_output(text) ⇒ Object



36
37
38
39
40
# File 'lib/rails_console_ai/channel/api.rb', line 36

def display_result_output(text)
  stripped = strip_ansi(text)
  @captured_output << stripped << "\n"
  log_prefixed(">>", stripped)
end

#display_status(text) ⇒ Object



57
58
59
60
61
62
# File 'lib/rails_console_ai/channel/api.rb', line 57

def display_status(text)
  stripped = strip_ansi(text).strip
  return if stripped.empty?
  @status_log << stripped
  log_prefixed("(status)", stripped)
end

#display_thinking(text) ⇒ Object



50
51
52
53
54
55
# File 'lib/rails_console_ai/channel/api.rb', line 50

def display_thinking(text)
  stripped = strip_ansi(text).strip
  return if stripped.empty?
  @status_log << stripped
  log_prefixed("(thinking)", stripped)
end

#display_tool_call(text) ⇒ Object



64
65
66
67
68
# File 'lib/rails_console_ai/channel/api.rb', line 64

def display_tool_call(text)
  stripped = strip_ansi(text)
  @status_log << stripped
  log_prefixed("->", stripped)
end

#display_warning(text) ⇒ Object



70
71
72
73
74
# File 'lib/rails_console_ai/channel/api.rb', line 70

def display_warning(text)
  stripped = strip_ansi(text)
  @status_log << "WARN: #{stripped}"
  log_prefixed("(warn)", stripped)
end

#modeObject



97
98
99
# File 'lib/rails_console_ai/channel/api.rb', line 97

def mode
  'api'
end

#prompt(_text) ⇒ Object



82
83
84
# File 'lib/rails_console_ai/channel/api.rb', line 82

def prompt(_text)
  ''  # non-interactive — no user to ask
end

#supports_danger?Boolean

Returns:

  • (Boolean)


105
106
107
# File 'lib/rails_console_ai/channel/api.rb', line 105

def supports_danger?
  false  # like sub_agent: never bypass safety guards without a human
end

#supports_editing?Boolean

Returns:

  • (Boolean)


109
110
111
# File 'lib/rails_console_ai/channel/api.rb', line 109

def supports_editing?
  false
end

#system_instructionsObject



117
118
119
# File 'lib/rails_console_ai/channel/api.rb', line 117

def system_instructions
  nil
end

#user_identityObject



93
94
95
# File 'lib/rails_console_ai/channel/api.rb', line 93

def user_identity
  @user_name
end

#wrap_llm_call(&block) ⇒ Object



113
114
115
# File 'lib/rails_console_ai/channel/api.rb', line 113

def wrap_llm_call(&block)
  yield
end