Class: Clacky::Channel::ChannelUIController
- Inherits:
-
Object
- Object
- Clacky::Channel::ChannelUIController
show all
- Includes:
- UIInterface
- Defined in:
- lib/clacky/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
-
#append_output(content) ⇒ Object
-
#buffer_line(line) ⇒ Object
-
#clear_input ⇒ Object
-
#flush_adapter_pending ⇒ Object
-
#flush_buffer ⇒ Object
-
#flush_buffer_unlocked ⇒ Object
-
#initialize(event, adapter) ⇒ ChannelUIController
constructor
A new instance of ChannelUIController.
-
#log(message, level: :info) ⇒ Object
-
#request_confirmation(message, default: true) ⇒ Object
Blocking interaction === Not called directly — WebUIController handles the blocking wait and only notifies IM via show_warning.
-
#send_file(path, name = nil) ⇒ Object
-
#send_text(text) ⇒ Object
-
#set_idle_status ⇒ Object
-
#set_input_tips(message, type: :info) ⇒ Object
-
#set_working_status ⇒ Object
-
#show_assistant_message(content, files:) ⇒ Object
-
#show_complete(iterations:, cost:, duration: nil, cache_stats: nil, awaiting_user_feedback: false, cost_source: nil) ⇒ Object
-
#show_diff(old_content, new_content, max_lines: 50) ⇒ Object
-
#show_error(message) ⇒ Object
-
#show_file_edit_preview(path) ⇒ Object
-
#show_file_error(error_message) ⇒ Object
-
#show_file_write_preview(path, is_new_file:) ⇒ Object
-
#show_info(message, prefix_newline: true) ⇒ Object
Status messages ===.
-
#show_progress(message = nil, prefix_newline: true, output_buffer: nil) ⇒ Object
Progress ===.
-
#show_shell_preview(command) ⇒ Object
-
#show_success(message) ⇒ Object
-
#show_token_usage(token_data) ⇒ Object
-
#show_tool_args(formatted_args) ⇒ Object
-
#show_tool_call(name, args) ⇒ Object
-
#show_tool_error(error) ⇒ Object
-
#show_tool_result(result) ⇒ Object
-
#show_user_message(content) ⇒ Object
Forward WebUI user messages to the IM channel so both sides stay in sync.
-
#show_warning(message) ⇒ Object
-
#stop ⇒ Object
-
#update_message_context(event) ⇒ Object
Update the reply context for the current inbound message.
-
#update_sessionbar(tasks: nil, cost: nil, cost_source: nil, status: nil, latency: nil) ⇒ Object
State updates (no-ops for IM) ===.
-
#update_todos(todos) ⇒ Object
#show_tool_stdout, #start_progress, #with_progress
Constructor Details
Returns a new instance of ChannelUIController.
25
26
27
28
29
30
31
32
|
# File 'lib/clacky/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] @adapter = adapter
@buffer = []
@mutex = Mutex.new
end
|
Instance Attribute Details
#chat_id ⇒ Object
Returns the value of attribute chat_id.
23
24
25
|
# File 'lib/clacky/server/channel/channel_ui_controller.rb', line 23
def chat_id
@chat_id
end
|
Returns the value of attribute platform.
23
24
25
|
# File 'lib/clacky/server/channel/channel_ui_controller.rb', line 23
def platform
@platform
end
|
Instance Method Details
#append_output(content) ⇒ Object
127
128
129
130
131
|
# File 'lib/clacky/server/channel/channel_ui_controller.rb', line 127
def append_output(content)
return if content.nil? || content.to_s.strip.empty?
send_text(content)
end
|
#buffer_line(line) ⇒ Object
206
207
208
209
210
211
|
# File 'lib/clacky/server/channel/channel_ui_controller.rb', line 206
def buffer_line(line)
@mutex.synchronize do
@buffer << line
flush_buffer_unlocked if @buffer.size >= BUFFER_FLUSH_SIZE
end
end
|
179
|
# File 'lib/clacky/server/channel/channel_ui_controller.rb', line 179
def clear_input; end
|
#flush_adapter_pending ⇒ Object
224
225
226
|
# File 'lib/clacky/server/channel/channel_ui_controller.rb', line 224
def flush_adapter_pending
@adapter.flush_pending(@chat_id) if @adapter.respond_to?(:flush_pending)
end
|
#flush_buffer ⇒ Object
213
214
215
|
# File 'lib/clacky/server/channel/channel_ui_controller.rb', line 213
def flush_buffer
@mutex.synchronize { flush_buffer_unlocked }
end
|
#flush_buffer_unlocked ⇒ Object
217
218
219
220
221
222
|
# File 'lib/clacky/server/channel/channel_ui_controller.rb', line 217
def flush_buffer_unlocked
return if @buffer.empty?
send_text(@buffer.join("\n"))
@buffer.clear
end
|
#log(message, level: :info) ⇒ Object
151
152
153
|
# File 'lib/clacky/server/channel/channel_ui_controller.rb', line 151
def log(message, level: :info)
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.
172
173
174
175
|
# File 'lib/clacky/server/channel/channel_ui_controller.rb', line 172
def request_confirmation(message, default: true)
send_text("Confirmation requested (auto-approved): #{message}")
default
end
|
#send_file(path, name = nil) ⇒ Object
194
195
196
197
198
199
200
201
202
203
204
|
# File 'lib/clacky/server/channel/channel_ui_controller.rb', line 194
def send_file(path, name = nil)
if @adapter.respond_to?(:send_file)
@adapter.send_file(@chat_id, path, name: name)
else
send_text("File: #{name || File.basename(path)}\n#{path}")
end
rescue StandardError => e
Clacky::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
184
185
186
187
188
189
190
191
192
|
# File 'lib/clacky/server/channel/channel_ui_controller.rb', line 184
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
Clacky::Logger.warn("[ChannelUI] send_text failed", platform: @platform, chat_id: @chat_id, error: e)
nil
end
|
#set_idle_status ⇒ Object
166
|
# File 'lib/clacky/server/channel/channel_ui_controller.rb', line 166
def set_idle_status; end
|
180
|
# File 'lib/clacky/server/channel/channel_ui_controller.rb', line 180
def set_input_tips(message, type: :info); end
|
#set_working_status ⇒ Object
165
|
# File 'lib/clacky/server/channel/channel_ui_controller.rb', line 165
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/clacky/server/channel/channel_ui_controller.rb', line 57
def show_assistant_message(content, files:)
flush_buffer
Clacky::Logger.info("[ChannelUI] show_assistant_message files=#{files.size} content_len=#{content.to_s.length}")
text = content.to_s.gsub(/!?\[[^\]]*\]\(file:\/\/[^)]+\)/, "").strip
send_text(text) unless text.empty?
flush_adapter_pending
files.each do |f|
Clacky::Logger.info("[ChannelUI] sending file path=#{f[:path].inspect} name=#{f[:name].inspect}")
send_file(f[:path], f[:name])
end
end
|
#show_complete(iterations:, cost:, duration: nil, cache_stats: nil, awaiting_user_feedback: false, cost_source: nil) ⇒ Object
114
115
116
117
118
119
120
121
122
123
124
125
|
# File 'lib/clacky/server/channel/channel_ui_controller.rb', line 114
def show_complete(iterations:, cost:, duration: nil, cache_stats: nil, awaiting_user_feedback: false, cost_source: nil)
flush_buffer
parts = ["Done", "#{iterations} step#{"s" if iterations != 1}"]
if cost && cost > 0 && cost_source
parts << "$#{cost.round(4)}"
end
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/clacky/server/channel/channel_ui_controller.rb', line 106
def show_diff(old_content, new_content, max_lines: 50)
end
|
#show_error(message) ⇒ Object
143
144
145
|
# File 'lib/clacky/server/channel/channel_ui_controller.rb', line 143
def show_error(message)
send_text("Error: #{message}")
end
|
#show_file_edit_preview(path) ⇒ Object
94
95
96
|
# File 'lib/clacky/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/clacky/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/clacky/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
135
136
137
|
# File 'lib/clacky/server/channel/channel_ui_controller.rb', line 135
def show_info(message, prefix_newline: true)
end
|
#show_progress(message = nil, prefix_newline: true, output_buffer: nil) ⇒ Object
157
158
159
|
# File 'lib/clacky/server/channel/channel_ui_controller.rb', line 157
def show_progress(message = nil, prefix_newline: true, output_buffer: nil)
end
|
#show_shell_preview(command) ⇒ Object
98
99
100
|
# File 'lib/clacky/server/channel/channel_ui_controller.rb', line 98
def show_shell_preview(command)
buffer_line("$ #{command}")
end
|
#show_success(message) ⇒ Object
147
148
149
|
# File 'lib/clacky/server/channel/channel_ui_controller.rb', line 147
def show_success(message)
send_text(message)
end
|
#show_token_usage(token_data) ⇒ Object
110
111
112
|
# File 'lib/clacky/server/channel/channel_ui_controller.rb', line 110
def show_token_usage(token_data)
end
|
85
86
87
|
# File 'lib/clacky/server/channel/channel_ui_controller.rb', line 85
def show_tool_args(formatted_args)
end
|
72
73
74
|
# File 'lib/clacky/server/channel/channel_ui_controller.rb', line 72
def show_tool_call(name, args)
end
|
80
81
82
83
|
# File 'lib/clacky/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
|
76
77
78
|
# File 'lib/clacky/server/channel/channel_ui_controller.rb', line 76
def show_tool_result(result)
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/clacky/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
139
140
141
|
# File 'lib/clacky/server/channel/channel_ui_controller.rb', line 139
def show_warning(message)
send_text("Warning: #{message}")
end
|
#stop ⇒ Object
181
|
# File 'lib/clacky/server/channel/channel_ui_controller.rb', line 181
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.
40
41
42
43
44
45
|
# File 'lib/clacky/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, cost: nil, cost_source: nil, status: nil, latency: nil) ⇒ Object
State updates (no-ops for IM) ===
163
|
# File 'lib/clacky/server/channel/channel_ui_controller.rb', line 163
def update_sessionbar(tasks: nil, cost: nil, cost_source: nil, status: nil, latency: nil); end
|
#update_todos(todos) ⇒ Object
164
|
# File 'lib/clacky/server/channel/channel_ui_controller.rb', line 164
def update_todos(todos); end
|