Skip to content

Commit ab440a5

Browse files
committed
refactor(state): unify model clearing and improve renderer/session reset
1 parent 2a3203f commit ab440a5

5 files changed

Lines changed: 52 additions & 17 deletions

File tree

lua/opencode/core.lua

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,10 @@ end)
3737
M.switch_session = Promise.async(function(session_id)
3838
local selected_session = session.get_by_id(session_id):await()
3939

40-
state.model.clear_model()
41-
state.model.clear_mode()
40+
state.model.clear()
4241
M.ensure_current_mode():await()
4342

4443
state.session.set_active(selected_session)
45-
state.session.reset_restore_points()
4644
if state.ui.is_visible() then
4745
ui.focus_input()
4846
else

lua/opencode/state/model.lua

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
local store = require('opencode.state.store')
22

3+
---@class OpencodeModelStateMutations
4+
---@field set_mode fun(mode: string|nil)
5+
---@field clear_mode fun()
6+
---@field set_model fun(model: string|nil)
7+
---@field clear_model fun()
8+
---@field clear fun()
9+
---@field set_model_info fun(info: table|nil)
10+
---@field set_variant fun(variant: string|nil)
11+
---@field clear_variant fun()
12+
---@field set_mode_model_map fun(mode_map: table<string, string>)
13+
---@field set_mode_model_override fun(mode: string, model: string)
14+
315
local M = {}
416

517
---@param mode string|nil
@@ -20,6 +32,12 @@ function M.clear_model()
2032
return store.set('current_model', nil)
2133
end
2234

35+
function M.clear()
36+
store.set('current_model', nil)
37+
store.set('current_mode', nil)
38+
store.set('current_variant', nil)
39+
end
40+
2341
---@param info table|nil
2442
function M.set_model_info(info)
2543
return store.set('current_model_info', info)
@@ -42,10 +60,11 @@ end
4260
---@param mode string
4361
---@param model string
4462
function M.set_mode_model_override(mode, model)
45-
local state = store.state()
46-
local mode_map = vim.deepcopy(state.user_mode_model_map)
47-
mode_map[mode] = model
48-
return store.set('user_mode_model_map', mode_map)
63+
return store.update('user_mode_model_map', function(current)
64+
local updated = vim.deepcopy(current)
65+
updated[mode] = model
66+
return updated
67+
end)
4968
end
5069

5170
return M

lua/opencode/state/renderer.lua

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ local store = require('opencode.state.store')
88
---@field set_pending_permissions fun(permissions: OpencodePermission[])
99
---@field set_cost fun(cost: number)
1010
---@field set_tokens_count fun(count: number)
11+
---@field set_stats fun(tokens_count: number, cost: number)
12+
---@field reset fun()
1113

1214
local M = {}
1315

@@ -41,4 +43,19 @@ function M.set_tokens_count(count)
4143
return store.set('tokens_count', count)
4244
end
4345

46+
---@param tokens_count number
47+
---@param cost number
48+
function M.set_stats(tokens_count, cost)
49+
store.set('tokens_count', tokens_count)
50+
store.set('cost', cost)
51+
end
52+
53+
function M.reset()
54+
store.set('messages', {})
55+
store.set('last_user_message', nil)
56+
store.set('tokens_count', 0)
57+
store.set('cost', 0)
58+
store.set('pending_permissions', {})
59+
end
60+
4461
return M

lua/opencode/state/session.lua

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ local M = {}
1212

1313
---@param session Session|nil
1414
function M.set_active(session)
15-
M.clear_active()
15+
store.set('restore_points', {})
16+
store.set('last_sent_context', nil)
17+
store.set('user_message_count', {})
1618
return store.set('active_session', session)
1719
end
1820

lua/opencode/ui/renderer.lua

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,14 @@ function M.reset()
4343

4444
output_window.clear()
4545

46-
state.renderer.set_messages({})
47-
state.renderer.set_last_user_message(nil)
48-
state.renderer.set_tokens_count(0)
49-
5046
local permissions = state.pending_permissions or {}
5147
if #permissions > 0 and state.api_client then
5248
for _, permission in ipairs(permissions) do
5349
require('opencode.api').permission_deny(permission)
5450
end
5551
end
5652
permission_window.clear_all()
57-
state.renderer.set_pending_permissions({})
53+
state.renderer.reset()
5854

5955
trigger_on_data_rendered()
6056
end
@@ -1198,11 +1194,14 @@ function M._update_stats_from_message(message)
11981194
end
11991195

12001196
local tokens = message.info.tokens
1201-
if tokens and tokens.input > 0 then
1197+
if tokens and tokens.input > 0 and message.info.cost and type(message.info.cost) == 'number' then
1198+
state.renderer.set_stats(
1199+
tokens.input + tokens.output + tokens.cache.read + tokens.cache.write,
1200+
message.info.cost
1201+
)
1202+
elseif tokens and tokens.input > 0 then
12021203
state.renderer.set_tokens_count(tokens.input + tokens.output + tokens.cache.read + tokens.cache.write)
1203-
end
1204-
1205-
if message.info.cost and type(message.info.cost) == 'number' then
1204+
elseif message.info.cost and type(message.info.cost) == 'number' then
12061205
state.renderer.set_cost(message.info.cost)
12071206
end
12081207
end

0 commit comments

Comments
 (0)