Skip to content

Commit c2b16eb

Browse files
committed
refactor: add render_state WIP
Building on MessageMap idea, encapsulate the rendered state into render_state, along with easy look ups
1 parent bd144ab commit c2b16eb

File tree

6 files changed

+525
-176
lines changed

6 files changed

+525
-176
lines changed

lua/opencode/server_job.lua

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,20 @@ local function handle_api_response(response, cb)
1717
end
1818
end
1919

20+
M.requests = {}
21+
22+
function M.get_unresolved_requests()
23+
local unresolved = {}
24+
25+
for _, data in ipairs(M.requests) do
26+
if data[2]._resolved ~= true then
27+
table.insert(unresolved, data)
28+
end
29+
end
30+
31+
return unresolved
32+
end
33+
2034
--- Make an HTTP API call to the opencode server.
2135
--- @generic T
2236
--- @param url string The API endpoint URL
@@ -61,6 +75,9 @@ function M.call_api(url, method, body)
6175
opts.body = body and vim.json.encode(body) or '{}'
6276
end
6377

78+
-- FIXME: remove tracking code when thinking bug is fixed
79+
table.insert(M.requests, { opts, call_promise })
80+
6481
curl.request(opts)
6582
return call_promise
6683
end

lua/opencode/ui/debug_helper.lua

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,14 @@ function M.debug_output()
2626
end
2727

2828
function M.debug_message()
29-
local session_formatter = require('opencode.ui.formatter')
29+
local renderer = require('opencode.ui.renderer')
3030
local current_line = vim.api.nvim_win_get_cursor(state.windows.output_win)[1]
31-
local metadata = session_formatter.get_message_at_line(current_line) or {}
32-
M.open_json_file(metadata.message)
31+
local message_data = renderer._render_state:get_message_at_line(current_line)
32+
if message_data and message_data.message then
33+
M.open_json_file(message_data.message)
34+
else
35+
vim.notify('No message found at current line', vim.log.levels.WARN)
36+
end
3337
end
3438

3539
function M.debug_session()

lua/opencode/ui/formatter.lua

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -53,24 +53,9 @@ end
5353
---@param line number Buffer line number
5454
---@param output Output Output object to query
5555
---@return {message: MessageInfo, part: MessagePart, msg_idx: number, part_idx: number}|nil
56+
---@deprecated Use RenderState:get_message_at_line() instead
5657
function M.get_message_at_line(line, output)
57-
local metadata = output:get_nearest_metadata(line)
58-
if metadata and metadata.msg_idx and metadata.part_idx then
59-
local msg = state.messages and state.messages[metadata.msg_idx]
60-
if not msg or not msg.parts then
61-
return nil
62-
end
63-
local part = msg.parts[metadata.part_idx]
64-
if not part then
65-
return nil
66-
end
67-
return {
68-
message = msg,
69-
part = part,
70-
msg_idx = metadata.msg_idx,
71-
part_idx = metadata.part_idx,
72-
}
73-
end
58+
return nil
7459
end
7560

7661
---Calculate statistics for reverted messages and tool calls
@@ -667,22 +652,10 @@ end
667652
---Formats a single message part and returns the resulting output object
668653
---@param part MessagePart The part to format
669654
---@param role 'user'|'assistant'|'system' The role, user or assistant, that created this part
670-
---@param msg_idx integer The index of the message in state.messages
671-
---@param part_idx integer The index of the part in state.messages[msg_idx].parts
672655
---@return Output
673-
function M.format_part(part, role, msg_idx, part_idx)
656+
function M.format_part(part, role)
674657
local output = Output.new()
675658

676-
-- FIXME: do we need metadata? it looks like maybe only for snapshots?
677-
local metadata = {
678-
msg_idx = msg_idx,
679-
part_idx = part_idx,
680-
role = role,
681-
type = part.type,
682-
snapshot = part.snapshot,
683-
}
684-
output:add_metadata(metadata)
685-
686659
local content_added = false
687660

688661
if role == 'user' then

0 commit comments

Comments
 (0)