Skip to content

Commit 0a39c01

Browse files
committed
refactor: move stats to renderer
Trying to have formatter not have side effects
1 parent 50de5b3 commit 0a39c01

File tree

2 files changed

+41
-34
lines changed

2 files changed

+41
-34
lines changed

lua/opencode/ui/formatter.lua

Lines changed: 7 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -40,25 +40,12 @@ function M._format_messages(session)
4040
output:add_lines(M.separator)
4141
state.current_message = msg
4242

43-
if not state.current_model and msg.info.providerID and msg.info.providerID ~= '' then
44-
state.current_model = msg.info.providerID .. '/' .. msg.info.modelID
45-
end
46-
47-
if msg.info.tokens and msg.info.tokens.input > 0 then
48-
state.tokens_count = msg.info.tokens.input
49-
+ msg.info.tokens.output
50-
+ msg.info.tokens.cache.read
51-
+ msg.info.tokens.cache.write
52-
end
53-
54-
if msg.info.cost and type(msg.info.cost) == 'number' then
55-
state.cost = msg.info.cost
56-
end
57-
5843
if session.revert and session.revert.messageID == msg.info.id then
5944
---@type {messages: number, tool_calls: number, files: table<string, {additions: number, deletions: number}>}
6045
local revert_stats = M._calculate_revert_stats(state.messages, i, session.revert)
6146
M._format_revert_message(output, revert_stats)
47+
48+
-- FIXME: how does reverting work? why is it breaking out of the message reading loop?
6249
break
6350
end
6451

@@ -69,6 +56,7 @@ function M._format_messages(session)
6956
end
7057

7158
if msg.info.error and msg.info.error ~= '' then
59+
vim.notify('calling _format_error')
7260
M._format_error(output, msg.info)
7361
end
7462
end
@@ -788,27 +776,12 @@ end
788776
---@param msg_idx number
789777
---@return Output
790778
function M.format_message_header_single(message, msg_idx)
791-
local temp_output = Output.new()
792-
793-
if not state.current_model and message.info.providerID and message.info.providerID ~= '' then
794-
state.current_model = message.info.providerID .. '/' .. message.info.modelID
795-
end
796-
797-
if message.info.tokens and message.info.tokens.input > 0 then
798-
state.tokens_count = message.info.tokens.input
799-
+ message.info.tokens.output
800-
+ message.info.tokens.cache.read
801-
+ message.info.tokens.cache.write
802-
end
803-
804-
if message.info.cost and type(message.info.cost) == 'number' then
805-
state.cost = message.info.cost
806-
end
779+
local output = Output.new()
807780

808-
temp_output:add_lines(M.separator)
809-
M._format_message_header(temp_output, message.info, msg_idx)
781+
output:add_lines(M.separator)
782+
M._format_message_header(output, message.info, msg_idx)
810783

811-
return temp_output
784+
return output
812785
end
813786

814787
---@param error_text string

lua/opencode/ui/renderer.lua

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,13 @@ function M._render_full_session_data(session_data)
9797
state.messages = session_data
9898
M._message_map:hydrate(state.messages)
9999

100+
M._update_stats_from_messages(state.messages)
101+
100102
local output_data = formatter._format_messages(state.active_session)
101103

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
106+
102107
M.write_output(output_data)
103108
M._scroll_to_bottom()
104109
end
@@ -374,10 +379,12 @@ function M.on_message_updated(event)
374379
M._message_map:add_message(message.info.id, found_idx)
375380

376381
M._write_message_header(message, found_idx)
382+
377383
if message.info.role == 'user' then
378384
state.last_user_message = message
379385
end
380386
end
387+
M._update_stats_from_message(message)
381388

382389
M._scroll_to_bottom()
383390
end
@@ -636,4 +643,31 @@ function M.get_actions_for_line(line)
636643
return actions
637644
end
638645

646+
---Update stats from all messages in session
647+
---@param messages OpencodeMessage[]
648+
function M._update_stats_from_messages(messages)
649+
for _, msg in ipairs(messages) do
650+
M._update_stats_from_message(msg)
651+
end
652+
end
653+
654+
---Update display stats from a single message
655+
---@param message OpencodeMessage
656+
function M._update_stats_from_message(message)
657+
if not state.current_model and message.info.providerID and message.info.providerID ~= '' then
658+
state.current_model = message.info.providerID .. '/' .. message.info.modelID
659+
end
660+
661+
if message.info.tokens and message.info.tokens.input > 0 then
662+
state.tokens_count = message.info.tokens.input
663+
+ message.info.tokens.output
664+
+ message.info.tokens.cache.read
665+
+ message.info.tokens.cache.write
666+
end
667+
668+
if message.info.cost and type(message.info.cost) == 'number' then
669+
state.cost = message.info.cost
670+
end
671+
end
672+
639673
return M

0 commit comments

Comments
 (0)