Class: Octo::Channel::ChannelUIController

Inherits:
Object
  • Object
show all
Includes:
UIInterface
Defined in:
lib/octo/server/channel/channel_ui_controller.rb

Overview

ChannelUIController implements UIInterface for IM platform sessions. It is registered as a subscriber on WebUIController so that every agent output event is forwarded here and sent back to the IM platform.

Design notes:

  • Tool calls / results / diffs / token usage are intentionally suppressed to keep IM chat clean. Only high-signal events are forwarded.

  • Buffering: file/shell previews accumulate in a buffer and are flushed as one message before the next assistant message, avoiding flooding.

  • request_confirmation is not invoked directly on this class — the Web UI handles the blocking wait and only sends show_warning notifications.

Constant Summary collapse

BUFFER_FLUSH_SIZE =

flush early when buffer is large

5

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from UIInterface

#hide_next_message_suggestion, #show_background_task_notice, #show_next_message_suggestion, #show_tool_stdout, #start_progress, #stream_thinking_progress, #update_background_tasks, #update_user_message_queue_status, #with_progress

Constructor Details

#initialize(event, adapter) ⇒ ChannelUIController

Returns a new instance of ChannelUIController.



25
26
27
28
29
30
31
32
# File 'lib/octo/server/channel/channel_ui_controller.rb', line 25

def initialize(event, adapter)
  @platform   = event[:platform]
  @chat_id    = event[:chat_id]
  @message_id = event[:message_id]  # original message to reply under
  @adapter    = adapter
  @buffer     = []
  @mutex      = Mutex.new
end

Instance Attribute Details

#chat_idObject (readonly)

Returns the value of attribute chat_id.



23
24
25
# File 'lib/octo/server/channel/channel_ui_controller.rb', line 23

def chat_id
  @chat_id
end

#platformObject (readonly)

Returns the value of attribute platform.



23
24
25
# File 'lib/octo/server/channel/channel_ui_controller.rb', line 23

def platform
  @platform
end

Instance Method Details

#append_output(content) ⇒ Object



122
123
124
125
126
# File 'lib/octo/server/channel/channel_ui_controller.rb', line 122

def append_output(content)
  return if content.nil? || content.to_s.strip.empty?

  send_text(content)
end

#buffer_line(line) ⇒ Object



201
202
203
204
205
206
# File 'lib/octo/server/channel/channel_ui_controller.rb', line 201

def buffer_line(line)
  @mutex.synchronize do
    @buffer << line
    flush_buffer_unlocked if @buffer.size >= BUFFER_FLUSH_SIZE
  end
end

#clear_inputObject

Input control / lifecycle (no-ops) ===



174
# File 'lib/octo/server/channel/channel_ui_controller.rb', line 174

def clear_input; end

#flush_adapter_pendingObject



219
220
221
# File 'lib/octo/server/channel/channel_ui_controller.rb', line 219

def flush_adapter_pending
  @adapter.flush_pending(@chat_id) if @adapter.respond_to?(:flush_pending)
end

#flush_bufferObject



208
209
210
# File 'lib/octo/server/channel/channel_ui_controller.rb', line 208

def flush_buffer
  @mutex.synchronize { flush_buffer_unlocked }
end

#flush_buffer_unlockedObject



212
213
214
215
216
217
# File 'lib/octo/server/channel/channel_ui_controller.rb', line 212

def flush_buffer_unlocked
  return if @buffer.empty?

  send_text(@buffer.join("\n"))
  @buffer.clear
end

#log(message, level: :info) ⇒ Object



146
147
148
# File 'lib/octo/server/channel/channel_ui_controller.rb', line 146

def log(message, level: :info)
  # Suppress
end

#request_confirmation(message, default: true) ⇒ Object

Blocking interaction ===

Not called directly — WebUIController handles the blocking wait and only notifies IM via show_warning. Implemented as auto-approve as a safety fallback in case this is ever called directly.



167
168
169
170
# File 'lib/octo/server/channel/channel_ui_controller.rb', line 167

def request_confirmation(message, default: true)
  send_text("Confirmation requested (auto-approved): #{message}")
  default
end

#send_file(path, name = nil) ⇒ Object



189
190
191
192
193
194
195
196
197
198
199
# File 'lib/octo/server/channel/channel_ui_controller.rb', line 189

def send_file(path, name = nil)
  if @adapter.respond_to?(:send_file)
    @adapter.send_file(@chat_id, path, name: name)
  else
    # Fallback for adapters that don't support file sending
    send_text("File: #{name || File.basename(path)}\n#{path}")
  end
rescue StandardError => e
  Octo::Logger.warn("[ChannelUI] send_file failed (#{@platform}/#{@chat_id}): #{e.message}")
  send_text("Failed to send file: #{File.basename(path)}\nError: #{e.message}")
end

#send_text(text) ⇒ Object



179
180
181
182
183
184
185
186
187
# File 'lib/octo/server/channel/channel_ui_controller.rb', line 179

def send_text(text)
  text = text.to_s.gsub(/<think>[\s\S]*?<\/think>\n*/i, "").strip
  return if text.empty?

  @adapter.send_text(@chat_id, text, reply_to: @message_id)
rescue StandardError => e
  Octo::Logger.warn("[ChannelUI] send_text failed", platform: @platform, chat_id: @chat_id, error: e)
  nil
end

#set_idle_statusObject



161
# File 'lib/octo/server/channel/channel_ui_controller.rb', line 161

def set_idle_status; end

#set_input_tips(message, type: :info) ⇒ Object



175
# File 'lib/octo/server/channel/channel_ui_controller.rb', line 175

def set_input_tips(message, type: :info); end

#set_working_statusObject



160
# File 'lib/octo/server/channel/channel_ui_controller.rb', line 160

def set_working_status; end

#show_assistant_message(content, files:) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/octo/server/channel/channel_ui_controller.rb', line 57

def show_assistant_message(content, files:)
  flush_buffer
  Octo::Logger.info("[ChannelUI] show_assistant_message files=#{files.size} content_len=#{content.to_s.length}")
  # Strip file:// markdown links from the text sent to IM channels —
  # the actual files are delivered via send_file() below, so the
  # raw markdown links would just be noise in the chat.
  text = content.to_s.gsub(/!?\[[^\]]*\]\(file:\/\/[^)]+\)/, "").strip
  send_text(text) unless text.empty?
  flush_adapter_pending
  files.each do |f|
    Octo::Logger.info("[ChannelUI] sending file path=#{f[:path].inspect} name=#{f[:name].inspect}")
    send_file(f[:path], f[:name])
  end
end

#show_complete(iterations:, duration: nil, cache_stats: nil, awaiting_user_feedback: false) ⇒ Object



114
115
116
117
118
119
120
# File 'lib/octo/server/channel/channel_ui_controller.rb', line 114

def show_complete(iterations:, duration: nil, cache_stats: nil, awaiting_user_feedback: false)
  flush_buffer
  parts = ["Done", "#{iterations} step#{"s" if iterations != 1}"]
  parts << "#{duration.round(1)}s" if duration
  send_text(parts.join(" · "))
  flush_adapter_pending
end

#show_diff(old_content, new_content, max_lines: 50) ⇒ Object



106
107
108
# File 'lib/octo/server/channel/channel_ui_controller.rb', line 106

def show_diff(old_content, new_content, max_lines: 50)
  # Diffs are too verbose for IM — suppress
end

#show_error(message) ⇒ Object



138
139
140
# File 'lib/octo/server/channel/channel_ui_controller.rb', line 138

def show_error(message)
  send_text("Error: #{message}")
end

#show_file_edit_preview(path) ⇒ Object



94
95
96
# File 'lib/octo/server/channel/channel_ui_controller.rb', line 94

def show_file_edit_preview(path)
  buffer_line("edit: #{path}")
end

#show_file_error(error_message) ⇒ Object



102
103
104
# File 'lib/octo/server/channel/channel_ui_controller.rb', line 102

def show_file_error(error_message)
  send_text("File error: #{error_message}")
end

#show_file_write_preview(path, is_new_file:) ⇒ Object



89
90
91
92
# File 'lib/octo/server/channel/channel_ui_controller.rb', line 89

def show_file_write_preview(path, is_new_file:)
  action = is_new_file ? "create" : "overwrite"
  buffer_line("#{action}: #{path}")
end

#show_info(message, prefix_newline: true) ⇒ Object

Status messages ===



130
131
132
# File 'lib/octo/server/channel/channel_ui_controller.rb', line 130

def show_info(message, prefix_newline: true)
  # Suppress informational noise in IM
end

#show_progress(message = nil, prefix_newline: true, output_buffer: nil) ⇒ Object

Progress ===



152
153
154
# File 'lib/octo/server/channel/channel_ui_controller.rb', line 152

def show_progress(message = nil, prefix_newline: true, output_buffer: nil)
  # Suppress — progress spinner has no IM equivalent
end

#show_shell_preview(command) ⇒ Object



98
99
100
# File 'lib/octo/server/channel/channel_ui_controller.rb', line 98

def show_shell_preview(command)
  buffer_line("$ #{command}")
end

#show_success(message) ⇒ Object



142
143
144
# File 'lib/octo/server/channel/channel_ui_controller.rb', line 142

def show_success(message)
  send_text(message)
end

#show_token_usage(token_data) ⇒ Object



110
111
112
# File 'lib/octo/server/channel/channel_ui_controller.rb', line 110

def show_token_usage(token_data)
  # Suppress
end

#show_tool_args(formatted_args) ⇒ Object



85
86
87
# File 'lib/octo/server/channel/channel_ui_controller.rb', line 85

def show_tool_args(formatted_args)
  # Suppress
end

#show_tool_call(name, args) ⇒ Object



72
73
74
# File 'lib/octo/server/channel/channel_ui_controller.rb', line 72

def show_tool_call(name, args)
  # Suppress — too noisy for IM
end

#show_tool_error(error) ⇒ Object



80
81
82
83
# File 'lib/octo/server/channel/channel_ui_controller.rb', line 80

def show_tool_error(error)
  msg = error.is_a?(Exception) ? error.message : error.to_s
  send_text("Tool error: #{msg}")
end

#show_tool_result(result) ⇒ Object



76
77
78
# File 'lib/octo/server/channel/channel_ui_controller.rb', line 76

def show_tool_result(result)
  # Suppress — too noisy for IM
end

#show_user_message(content) ⇒ Object

Forward WebUI user messages to the IM channel so both sides stay in sync. Prefixed with the product/user context so it’s clear who sent it.



51
52
53
54
55
# File 'lib/octo/server/channel/channel_ui_controller.rb', line 51

def show_user_message(content)
  return if content.nil? || content.to_s.strip.empty?

  send_text("[USER] #{content}")
end

#show_warning(message) ⇒ Object



134
135
136
# File 'lib/octo/server/channel/channel_ui_controller.rb', line 134

def show_warning(message)
  send_text("Warning: #{message}")
end

#stopObject



176
# File 'lib/octo/server/channel/channel_ui_controller.rb', line 176

def stop; end

#update_message_context(event) ⇒ Object

Update the reply context for the current inbound message. Called at the start of each route_message so replies are threaded correctly. Also updates chat_id — a session may span multiple chats (e.g. same user in both a direct message and a group), and each inbound event dictates where outbound replies should be routed.

Parameters:

  • event (Hash)

    inbound event with :message_id and :chat_id



40
41
42
43
44
45
# File 'lib/octo/server/channel/channel_ui_controller.rb', line 40

def update_message_context(event)
  @mutex.synchronize do
    @message_id = event[:message_id]
    @chat_id    = event[:chat_id] if event[:chat_id]
  end
end

#update_sessionbar(tasks: nil, status: nil, latency: nil) ⇒ Object

State updates (no-ops for IM) ===



158
# File 'lib/octo/server/channel/channel_ui_controller.rb', line 158

def update_sessionbar(tasks: nil, status: nil, latency: nil); end

#update_todos(todos) ⇒ Object



159
# File 'lib/octo/server/channel/channel_ui_controller.rb', line 159

def update_todos(todos); end