@@ -10,7 +10,10 @@ local M = {}
1010M ._subscriptions = {}
1111M ._prev_line_count = 0
1212M ._render_state = RenderState .new ()
13- M ._last_part_formatted = { part_id = nil , formatted_data = nil }
13+ M ._last_part_formatted = {
14+ part_id = nil ,
15+ formatted_data = nil --[[ @as Output|nil]] ,
16+ }
1417
1518local trigger_on_data_rendered = require (' opencode.util' ).debounce (function ()
1619 local cb_type = type (config .ui .output .rendering .on_data_rendered )
@@ -99,6 +102,8 @@ function M.teardown()
99102 M .reset ()
100103end
101104
105+ --- Fetch full session messages from server
106+ --- @return Promise<OpencodeMessage[]> Promise resolving to list of OpencodeMessage
102107local function fetch_session ()
103108 local session = state .active_session
104109 if not session or not session or session == ' ' then
120125function M ._render_full_session_data (session_data )
121126 M .reset ()
122127
123- if not state .active_session then
128+ if not state .active_session or not state . messages then
124129 return
125130 end
126131
215220--- @param part_id ? string Optional part ID to store actions
216221--- @return { line_start : integer , line_end : integer }? Range where data was written
217222function M ._write_formatted_data (formatted_data , part_id )
223+ if not state .windows or not state .windows .output_buf then
224+ return
225+ end
226+
218227 local buf = state .windows .output_buf
219228 local start_line = output_window .get_buf_line_count ()
220229 local new_lines = formatted_data .lines
@@ -288,6 +297,7 @@ function M._replace_part_in_buffer(part_id, formatted_data)
288297 local write_start_line = cached .line_start
289298
290299 if can_optimize then
300+ --- @cast old_formatted { formatted_data : { lines : string[] } }
291301 local old_lines = old_formatted .formatted_data .lines
292302 local first_diff_line = nil
293303
@@ -367,6 +377,17 @@ function M._remove_message_from_buffer(message_id)
367377 M ._render_state :remove_message (message_id )
368378end
369379
380+ --- Adds a message (most likely just a header) to the buffer
381+ --- @param message OpencodeMessage Message to add
382+ function M ._add_message_to_buffer (message )
383+ local header_data = formatter .format_message_header (message )
384+ local range = M ._write_formatted_data (header_data )
385+
386+ if range then
387+ M ._render_state :set_message (message , range .line_start , range .line_end )
388+ end
389+ end
390+
370391--- Replace existing message header in buffer
371392--- @param message_id string Message ID
372393--- @param formatted_data Output Formatted header as Output object
@@ -442,12 +463,7 @@ function M.on_message_updated(message, revert_index)
442463 else
443464 table.insert (state .messages , msg )
444465
445- local header_data = formatter .format_message_header (msg )
446- local range = M ._write_formatted_data (header_data )
447-
448- if range then
449- M ._render_state :set_message (msg , range .line_start , range .line_end )
450- end
466+ M ._add_message_to_buffer (msg )
451467
452468 state .current_message = msg
453469 if message .info .role == ' user' then
0 commit comments