Skip to content

Commit dd06a3d

Browse files
committed
refactor(renderer): load session data same as events
Change full session loading to process messages / parts the same as if they were events. That means we have a single code path for processing events which should be less error-prone over time.
1 parent 2f5b983 commit dd06a3d

File tree

1 file changed

+32
-10
lines changed

1 file changed

+32
-10
lines changed

lua/opencode/ui/renderer.lua

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -94,17 +94,36 @@ end
9494
function M._render_full_session_data(session_data)
9595
M.reset()
9696

97-
state.messages = session_data
98-
M._message_map:hydrate(state.messages)
97+
for i, msg in ipairs(session_data) do
98+
-- output:add_lines(M.separator)
99+
-- state.current_message = msg
100+
101+
if state.active_session.revert and state.active_session.revert.messageID == msg.info.id then
102+
---@type {messages: number, tool_calls: number, files: table<string, {additions: number, deletions: number}>}
103+
local revert_stats = M._calculate_revert_stats(state.messages, i, state.active_session.revert)
104+
local output = require('opencode.ui.output'):new()
105+
formatter._format_revert_message(output, revert_stats)
106+
M.write_output(output)
107+
108+
-- FIXME: how does reverting work? why is it breaking out of the message reading loop?
109+
break
110+
end
99111

100-
M._update_stats_from_messages(state.messages)
112+
-- only pass in the info so, the parts will be processed as part of the loop
113+
-- TODO: remove part processing code in formatter
114+
M.on_message_updated({ info = msg.info })
101115

102-
local output_data = formatter._format_messages(state.active_session)
116+
for j, part in ipairs(msg.parts or {}) do
117+
M.on_part_updated({ part = part })
118+
end
103119

104-
-- FIXME: I think this should be setting state.last_user_message
105-
-- Maybe it'd be better to move iterating over messages to renderer
120+
-- FIXME: not sure how this error rendering code works when streaming
121+
-- if msg.info.error and msg.info.error ~= '' then
122+
-- vim.notify('calling _format_error')
123+
-- M._format_error(output, msg.info)
124+
-- end
125+
end
106126

107-
M.write_output(output_data)
108127
M._scroll_to_bottom()
109128
end
110129

@@ -373,20 +392,23 @@ function M.on_message_updated(properties)
373392

374393
if found_idx then
375394
state.messages[found_idx].info = message.info
376-
M._update_stats_from_message(message)
377395
else
378396
table.insert(state.messages, message)
379397
found_idx = #state.messages
380398
M._message_map:add_message(message.info.id, found_idx)
381399

382-
M._update_stats_from_message(message)
400+
local header_data = formatter.format_message_header_single(message, found_idx)
401+
M._write_formatted_data(header_data)
402+
403+
state.current_message = message
383404

384-
M._write_message_header(message, found_idx)
385405
if message.info.role == 'user' then
386406
state.last_user_message = message
387407
end
388408
end
389409

410+
M._update_stats_from_message(message)
411+
390412
M._scroll_to_bottom()
391413
end
392414

0 commit comments

Comments
 (0)