Class: Octo::Server::WebUIController
- Inherits:
-
Object
- Object
- Octo::Server::WebUIController
show all
- Includes:
- UIInterface
- Defined in:
- lib/octo/server/web_ui_controller.rb
Overview
WebUIController implements UIInterface for the web server mode. Instead of writing to stdout, it broadcasts JSON events over WebSocket connections. Multiple browser tabs can subscribe to the same session_id.
request_confirmation blocks the calling thread until the browser sends a response, mirroring the behaviour of JsonUIController (which reads from stdin).
Constant Summary
collapse
- CONFIRMATION_TIMEOUT =
Blocking interaction ===
Emits a request_confirmation event and blocks until the browser responds. Timeout after 5 minutes to avoid hanging threads forever.
300
Instance Attribute Summary collapse
Instance Method Summary
collapse
-
#append_output(content) ⇒ Object
-
#channel_subscribed? ⇒ Boolean
True if any channel subscribers are registered.
-
#clear_input ⇒ Object
-
#deliver_confirmation(conf_id, result) ⇒ Object
Deliver a confirmation answer received from the browser.
-
#emit(type, **data) ⇒ Object
-
#forward_to_subscribers(&block) ⇒ Object
Forward a UIInterface call to all registered channel subscribers.
-
#initialize(session_id, broadcaster) ⇒ WebUIController
constructor
A new instance of WebUIController.
-
#log(message, level: :info) ⇒ Object
-
#replay_live_state ⇒ Object
Replay in-progress command state to a newly (re-)subscribing browser tab.
-
#request_confirmation(message, default: true) ⇒ Object
-
#set_idle_status ⇒ Object
-
#set_input_tips(message, type: :info) ⇒ Object
-
#set_working_status ⇒ Object
-
#show_assistant_message(content, files:) ⇒ Object
-
#show_background_task_notice(command: nil, handle_id: nil, status: "success") ⇒ Object
-
#show_complete(iterations:, duration: nil, cache_stats: nil, awaiting_user_feedback: false) ⇒ 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_next_message_suggestion(text) ⇒ Object
Predicted next-user-message ghost text.
-
#show_progress(message = nil, prefix_newline: true, progress_type: "thinking", phase: "active", metadata: {}) ⇒ 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, ui_payload: nil) ⇒ Object
-
#show_tool_stdout(lines) ⇒ Object
Stream shell stdout/stderr lines to the browser while a command is running.
-
#show_user_message(content, created_at: nil, files: [], source: :web) ⇒ Object
Output display ===.
-
#show_warning(message) ⇒ Object
-
#stop ⇒ Object
Lifecycle ===.
-
#subscribe_channel(subscriber) ⇒ void
Register a channel subscriber (e.g. ChannelUIController).
-
#tool_call_summary(name, args) ⇒ Object
Generate a short human-readable summary for a tool call display.
-
#unsubscribe_channel(subscriber) ⇒ void
Remove a previously registered channel subscriber.
-
#update_background_tasks(running: 0, tasks: []) ⇒ Object
-
#update_sessionbar(tasks: nil, status: nil, latency: nil) ⇒ Object
State updates ===.
-
#update_todos(todos) ⇒ Object
-
#update_user_message_queue_status(pending: 0) ⇒ Object
#hide_next_message_suggestion, #start_progress, #stream_thinking_progress, #with_progress
Constructor Details
#initialize(session_id, broadcaster) ⇒ WebUIController
Returns a new instance of WebUIController.
20
21
22
23
24
25
26
27
28
29
30
31
32
|
# File 'lib/octo/server/web_ui_controller.rb', line 20
def initialize(session_id, broadcaster)
@session_id = session_id
@broadcaster = broadcaster @mutex = Mutex.new
@pending_confirmations = {}
@channel_subscribers = []
@subscribers_mutex = Mutex.new
end
|
Instance Attribute Details
#session_id ⇒ Object
Returns the value of attribute session_id.
18
19
20
|
# File 'lib/octo/server/web_ui_controller.rb', line 18
def session_id
@session_id
end
|
Instance Method Details
#append_output(content) ⇒ Object
219
220
221
222
|
# File 'lib/octo/server/web_ui_controller.rb', line 219
def append_output(content)
emit("output", content: content)
forward_to_subscribers { |sub| sub.append_output(content) }
end
|
#channel_subscribed? ⇒ Boolean
Returns true if any channel subscribers are registered.
50
51
52
|
# File 'lib/octo/server/web_ui_controller.rb', line 50
def channel_subscribed?
@subscribers_mutex.synchronize { !@channel_subscribers.empty? }
end
|
436
|
# File 'lib/octo/server/web_ui_controller.rb', line 436
def clear_input; end
|
#deliver_confirmation(conf_id, result) ⇒ Object
Deliver a confirmation answer received from the browser. Called by the HTTP server when a confirmation message arrives over WebSocket.
56
57
58
59
60
61
62
63
64
|
# File 'lib/octo/server/web_ui_controller.rb', line 56
def deliver_confirmation(conf_id, result)
@mutex.synchronize do
pending = @pending_confirmations[conf_id]
return unless pending
pending[:result] = result
pending[:cond].signal
end
end
|
#emit(type, **data) ⇒ Object
466
467
468
469
|
# File 'lib/octo/server/web_ui_controller.rb', line 466
def emit(type, **data)
event = { type: type, session_id: @session_id }.merge(data)
@broadcaster.call(@session_id, event)
end
|
#forward_to_subscribers(&block) ⇒ Object
Forward a UIInterface call to all registered channel subscribers. Each subscriber is called in the same thread as the caller (Agent thread). Errors in individual subscribers are rescued and logged so they never interrupt the main agent execution.
475
476
477
478
479
480
481
482
483
484
|
# File 'lib/octo/server/web_ui_controller.rb', line 475
def forward_to_subscribers(&block)
subscribers = @subscribers_mutex.synchronize { @channel_subscribers.dup }
return if subscribers.empty?
subscribers.each do |sub|
block.call(sub)
rescue StandardError => e
Octo::Logger.error("[WebUIController] channel subscriber error", error: e)
end
end
|
#log(message, level: :info) ⇒ Object
246
247
248
249
|
# File 'lib/octo/server/web_ui_controller.rb', line 246
def log(message, level: :info)
emit("log", level: level.to_s, message: message)
end
|
#replay_live_state ⇒ Object
Replay in-progress command state to a newly (re-)subscribing browser tab. all tool_stdout lines that fired while the user was away are lost. Replay live state when a client re-subscribes (e.g. after switching sessions).
Plan C: we do NOT re-emit tool_call here. The tool-item is already rendered in the DOM via the normal flow. We only replay:
1. progress(start) — restores the spinner / progress bar
2. tool_stdout — fills in all stdout received so far
The frontend’s appendToolStdout will attach to the last visible .tool-item even when _liveLastToolItem is null (after the tab re-loaded).
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
|
# File 'lib/octo/server/web_ui_controller.rb', line 318
def replay_live_state
return unless @live_progress_state
state = @live_progress_state
started_at_ms = @progress_start_time ? (@progress_start_time.to_f * 1000).round : nil
emit("progress",
message: state[:message],
progress_type: state[:progress_type],
phase: "active",
status: "start",
metadata: state[:metadata] || {},
started_at: started_at_ms
)
buf = @live_stdout_buffer
emit("tool_stdout", lines: buf) if buf && !buf.empty?
end
|
#request_confirmation(message, default: true) ⇒ Object
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
|
# File 'lib/octo/server/web_ui_controller.rb', line 403
def request_confirmation(message, default: true)
conf_id = "conf_#{SecureRandom.hex(4)}"
cond = ConditionVariable.new
pending = { cond: cond, result: nil }
@mutex.synchronize { @pending_confirmations[conf_id] = pending }
emit("request_confirmation", id: conf_id, message: message, default: default)
forward_to_subscribers { |sub| sub.show_warning("⏳ Confirmation requested: #{message}") }
@mutex.synchronize do
cond.wait(@mutex, CONFIRMATION_TIMEOUT)
@pending_confirmations.delete(conf_id)
result = pending[:result]
return default if result.nil?
case result.to_s.downcase
when "yes", "y" then true
when "no", "n" then false
else result.to_s
end
end
end
|
#set_idle_status ⇒ Object
383
384
385
386
387
388
389
390
391
392
|
# File 'lib/octo/server/web_ui_controller.rb', line 383
def set_idle_status
if @live_progress_state
emit("progress", phase: "done", status: "stop")
@live_progress_state = nil
@progress_start_time = nil
end
emit("session_update", status: "idle")
forward_to_subscribers { |sub| sub.set_idle_status }
end
|
437
|
# File 'lib/octo/server/web_ui_controller.rb', line 437
def set_input_tips(message, type: :info); end
|
#set_working_status ⇒ Object
378
379
380
381
|
# File 'lib/octo/server/web_ui_controller.rb', line 378
def set_working_status
emit("session_update", status: "working")
forward_to_subscribers { |sub| sub.set_working_status }
end
|
#show_assistant_message(content, files:) ⇒ Object
107
108
109
110
111
112
113
114
115
116
117
118
|
# File 'lib/octo/server/web_ui_controller.rb', line 107
def show_assistant_message(content, files:)
return if (content.nil? || content.to_s.strip.empty?) && files.empty?
web_content = Octo::Utils::FileProcessor.rewrite_local_image_urls(content.to_s)
emit("assistant_message", content: web_content, files: files)
forward_to_subscribers { |sub| sub.show_assistant_message(content, files: files) }
end
|
#show_background_task_notice(command: nil, handle_id: nil, status: "success") ⇒ Object
369
370
371
372
373
374
375
376
|
# File 'lib/octo/server/web_ui_controller.rb', line 369
def show_background_task_notice(command: nil, handle_id: nil, status: "success")
cmd = command.to_s
cmd = "#{cmd[0, 60]}…" if cmd.length > 60
emit("background_task_notice",
command: cmd,
handle_id: handle_id.to_s,
status: status.to_s)
end
|
#show_complete(iterations:, duration: nil, cache_stats: nil, awaiting_user_feedback: false) ⇒ Object
207
208
209
210
211
212
213
214
215
216
217
|
# File 'lib/octo/server/web_ui_controller.rb', line 207
def show_complete(iterations:, duration: nil, cache_stats: nil, awaiting_user_feedback: false)
data = { iterations: iterations }
data[:duration] = duration if duration
data[:cache_stats] = cache_stats if cache_stats
data[:awaiting_user_feedback] = awaiting_user_feedback if awaiting_user_feedback
emit("complete", **data)
forward_to_subscribers do |sub|
sub.show_complete(iterations: iterations, duration: duration,
cache_stats: cache_stats, awaiting_user_feedback: awaiting_user_feedback)
end
end
|
#show_diff(old_content, new_content, max_lines: 50) ⇒ Object
188
189
190
191
192
193
194
195
196
197
198
199
200
|
# File 'lib/octo/server/web_ui_controller.rb', line 188
def show_diff(old_content, new_content, max_lines: 50)
require "diffy"
diff = Diffy::Diff.new(old_content, new_content, context: 3).to_s
diff_lines = diff.lines
emit("diff",
old_size: old_content.bytesize,
new_size: new_content.bytesize,
diff: diff_lines.take(max_lines).join,
truncated: diff_lines.size > max_lines)
rescue LoadError
emit("diff", old_size: old_content.bytesize, new_size: new_content.bytesize)
end
|
#show_error(message) ⇒ Object
236
237
238
239
|
# File 'lib/octo/server/web_ui_controller.rb', line 236
def show_error(message)
emit("error", message: message)
forward_to_subscribers { |sub| sub.show_error(message) }
end
|
#show_file_edit_preview(path) ⇒ Object
173
174
175
176
|
# File 'lib/octo/server/web_ui_controller.rb', line 173
def show_file_edit_preview(path)
emit("file_preview", path: path, operation: "edit")
forward_to_subscribers { |sub| sub.show_file_edit_preview(path) }
end
|
#show_file_error(error_message) ⇒ Object
178
179
180
181
|
# File 'lib/octo/server/web_ui_controller.rb', line 178
def show_file_error(error_message)
emit("file_error", error: error_message)
forward_to_subscribers { |sub| sub.show_file_error(error_message) }
end
|
#show_file_write_preview(path, is_new_file:) ⇒ Object
168
169
170
171
|
# File 'lib/octo/server/web_ui_controller.rb', line 168
def show_file_write_preview(path, is_new_file:)
emit("file_preview", path: path, operation: "write", is_new_file: is_new_file)
forward_to_subscribers { |sub| sub.show_file_write_preview(path, is_new_file: is_new_file) }
end
|
#show_info(message, prefix_newline: true) ⇒ Object
226
227
228
229
|
# File 'lib/octo/server/web_ui_controller.rb', line 226
def show_info(message, prefix_newline: true)
emit("info", message: message)
forward_to_subscribers { |sub| sub.show_info(message) }
end
|
#show_next_message_suggestion(text) ⇒ Object
Predicted next-user-message ghost text. Web renders as placeholder + Tab-to-accept. Channel subscribers (Feishu/WeCom) intentionally do not forward — there’s no input box to ghost-text into.
442
443
444
|
# File 'lib/octo/server/web_ui_controller.rb', line 442
def show_next_message_suggestion(text)
emit("next_message_suggestion", text: text.to_s)
end
|
#show_progress(message = nil, prefix_newline: true, progress_type: "thinking", phase: "active", metadata: {}) ⇒ Object
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
|
# File 'lib/octo/server/web_ui_controller.rb', line 253
def show_progress(message = nil, prefix_newline: true, progress_type: "thinking", phase: "active", metadata: {})
if phase == "active"
if @live_progress_state.nil? || @live_progress_state[:progress_type] != progress_type
@progress_start_time = Time.now
@live_stdout_buffer = []
end
@live_progress_state = {
message: message,
progress_type: progress_type,
metadata: metadata
}
elsif phase == "done"
@live_tool_call = nil @live_progress_state = nil
@progress_start_time = nil
end
data = {
message: message,
progress_type: progress_type,
phase: phase,
status: phase == "active" ? "start" : "stop" }
data[:metadata] = metadata unless metadata.empty?
if phase == "active" && @progress_start_time
data[:started_at] = (@progress_start_time.to_f * 1000).round
end
data[:elapsed] = (Time.now - @progress_start_time).round(1) if phase == "done" && @progress_start_time
emit("progress", **data)
forward_to_subscribers { |sub| sub.show_progress(message) }
end
|
#show_shell_preview(command) ⇒ Object
183
184
185
186
|
# File 'lib/octo/server/web_ui_controller.rb', line 183
def show_shell_preview(command)
emit("shell_preview", command: command)
forward_to_subscribers { |sub| sub.show_shell_preview(command) }
end
|
#show_success(message) ⇒ Object
241
242
243
244
|
# File 'lib/octo/server/web_ui_controller.rb', line 241
def show_success(message)
emit("success", message: message)
forward_to_subscribers { |sub| sub.show_success(message) }
end
|
#show_token_usage(token_data) ⇒ Object
202
203
204
205
|
# File 'lib/octo/server/web_ui_controller.rb', line 202
def show_token_usage(token_data)
emit("token_usage", **token_data)
end
|
163
164
165
166
|
# File 'lib/octo/server/web_ui_controller.rb', line 163
def show_tool_args(formatted_args)
emit("tool_args", args: formatted_args)
forward_to_subscribers { |sub| sub.show_tool_args(formatted_args) }
end
|
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
|
# File 'lib/octo/server/web_ui_controller.rb', line 120
def show_tool_call(name, args)
args_data = args.is_a?(String) ? (JSON.parse(args) rescue args) : args
if name.to_s == "request_user_feedback"
question = args_data.is_a?(Hash) ? (args_data[:question] || args_data["question"]).to_s : ""
context = args_data.is_a?(Hash) ? (args_data[:context] || args_data["context"]).to_s : ""
options = args_data.is_a?(Hash) ? (args_data[:options] || args_data["options"]) : nil
options = Array(options) if options && !options.is_a?(Array)
emit("request_feedback",
question: question,
context: context,
options: options || [])
return
end
summary = tool_call_summary(name, args_data)
@live_tool_call = { name: name, args: args_data, summary: summary }
emit("tool_call", name: name, args: args_data, summary: summary)
forward_to_subscribers { |sub| sub.show_tool_call(name, args_data) }
end
|
157
158
159
160
161
|
# File 'lib/octo/server/web_ui_controller.rb', line 157
def show_tool_error(error)
error_msg = error.is_a?(Exception) ? error.message : error.to_s
emit("tool_error", error: error_msg)
forward_to_subscribers { |sub| sub.show_tool_error(error) }
end
|
151
152
153
154
155
|
# File 'lib/octo/server/web_ui_controller.rb', line 151
def show_tool_result(result, ui_payload: nil)
@live_tool_call = nil emit("tool_result", result: result, ui_payload: ui_payload)
forward_to_subscribers { |sub| sub.show_tool_result(result) }
end
|
Stream shell stdout/stderr lines to the browser while a command is running. Called immediately via on_output callback from shell.rb — no polling delay. Lines are also buffered in @live_stdout_buffer so late-joining subscribers (e.g. user switches away and back) can receive a replay of what they missed.
298
299
300
301
302
303
304
|
# File 'lib/octo/server/web_ui_controller.rb', line 298
def show_tool_stdout(lines)
return if lines.nil? || lines.empty?
@live_stdout_buffer ||= []
@live_stdout_buffer.concat(lines)
emit("tool_stdout", lines: lines)
end
|
#show_user_message(content, created_at: nil, files: [], source: :web) ⇒ Object
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
# File 'lib/octo/server/web_ui_controller.rb', line 68
def show_user_message(content, created_at: nil, files: [], source: :web)
images = []
if content.is_a?(Array)
text_parts = []
content.each do |block|
next unless block.is_a?(Hash)
case block[:type]
when "text"
text_parts << block[:text]
when "image_url"
url = block.dig(:image_url, :url)
images << url if url
end
end
content = text_parts.join("\n")
end
data = { content: content }
data[:created_at] = created_at if created_at
rendered = Array(files).filter_map do |f|
url = f[:data_url] || f["data_url"]
name = f[:name] || f["name"]
url || (name ? "pdf:#{name}" : nil)
end
images.concat(rendered)
data[:images] = images unless images.empty?
emit("history_user_message", **data)
return unless source == :web
forward_to_subscribers { |sub| sub.show_user_message(content) if sub.respond_to?(:show_user_message) }
end
|
#show_warning(message) ⇒ Object
231
232
233
234
|
# File 'lib/octo/server/web_ui_controller.rb', line 231
def show_warning(message)
emit("warning", message: message)
forward_to_subscribers { |sub| sub.show_warning(message) }
end
|
#stop ⇒ Object
448
449
450
|
# File 'lib/octo/server/web_ui_controller.rb', line 448
def stop
emit("server_stop")
end
|
#subscribe_channel(subscriber) ⇒ void
This method returns an undefined value.
Register a channel subscriber (e.g. ChannelUIController). The subscriber will receive every UIInterface call that this controller handles.
38
39
40
|
# File 'lib/octo/server/web_ui_controller.rb', line 38
def subscribe_channel(subscriber)
@subscribers_mutex.synchronize { @channel_subscribers << subscriber }
end
|
Generate a short human-readable summary for a tool call display. Delegates to each tool’s own format_call method when available.
455
456
457
458
459
460
461
462
463
464
|
# File 'lib/octo/server/web_ui_controller.rb', line 455
def tool_call_summary(name, args)
class_name = name.to_s.split("_").map(&:capitalize).join
return nil unless Octo::Tools.const_defined?(class_name)
tool = Octo::Tools.const_get(class_name).new
args_sym = args.is_a?(Hash) ? args.transform_keys(&:to_sym) : {}
tool.format_call(args_sym)
rescue StandardError
nil
end
|
#unsubscribe_channel(subscriber) ⇒ void
This method returns an undefined value.
Remove a previously registered channel subscriber.
45
46
47
|
# File 'lib/octo/server/web_ui_controller.rb', line 45
def unsubscribe_channel(subscriber)
@subscribers_mutex.synchronize { @channel_subscribers.delete(subscriber) }
end
|
#update_background_tasks(running: 0, tasks: []) ⇒ Object
356
357
358
359
360
361
362
363
364
365
366
367
|
# File 'lib/octo/server/web_ui_controller.rb', line 356
def update_background_tasks(running: 0, tasks: [])
safe_tasks = Array(tasks).map do |t|
cmd = (t[:command] || t["command"]).to_s
cmd = "#{cmd[0, 80]}…" if cmd.length > 80
{
handle_id: (t[:handle_id] || t["handle_id"]).to_s,
command: cmd,
elapsed: (t[:elapsed] || t["elapsed"]).to_i
}
end
emit("background_tasks_update", running: running.to_i, tasks: safe_tasks)
end
|
#update_sessionbar(tasks: nil, status: nil, latency: nil) ⇒ Object
342
343
344
345
346
347
348
349
|
# File 'lib/octo/server/web_ui_controller.rb', line 342
def update_sessionbar(tasks: nil, status: nil, latency: nil)
data = {}
data[:tasks] = tasks if tasks
data[:status] = status if status
data[:latency] = latency if latency
emit("session_update", **data) unless data.empty?
forward_to_subscribers { |sub| sub.update_sessionbar(tasks: tasks, status: status, latency: latency) }
end
|
#update_todos(todos) ⇒ Object
351
352
353
354
|
# File 'lib/octo/server/web_ui_controller.rb', line 351
def update_todos(todos)
emit("todo_update", todos: todos)
forward_to_subscribers { |sub| sub.update_todos(todos) }
end
|
#update_user_message_queue_status(pending: 0) ⇒ Object
394
395
396
|
# File 'lib/octo/server/web_ui_controller.rb', line 394
def update_user_message_queue_status(pending: 0)
emit("user_message_queue_status", pending: pending.to_i)
end
|