-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathsession.lua
More file actions
62 lines (52 loc) · 1.68 KB
/
session.lua
File metadata and controls
62 lines (52 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
local store = require('opencode.state.store')
---@class OpencodeSessionStateMutations
local M = {}
---@param session Session|nil
function M.set_active(session)
return store.batch(function()
store.set('restore_points', {})
store.set('last_sent_context', nil)
store.set('user_message_count', {})
return store.set('active_session', session)
end)
end
function M.clear_active()
return store.batch(function()
store.set('restore_points', {})
store.set('last_sent_context', nil)
store.set('user_message_count', {})
return store.set('active_session', nil)
end)
end
---@param points RestorePoint[]
function M.set_restore_points(points)
return store.set('restore_points', points)
end
function M.reset_restore_points()
return store.set('restore_points', {})
end
---@param context OpencodeContext|nil
function M.set_last_sent_context(context)
return store.set('last_sent_context', context)
end
---@param count table<string, number>
function M.set_user_message_count(count)
return store.set('user_message_count', count)
end
---Increment/decrement the message count for a session, clamped to >= 0
---@param session_id string
---@param delta integer
function M.increment_user_message_count(session_id, delta)
store.mutate('user_message_count', function(counts)
local new_value = (counts[session_id] or 0) + delta
counts[session_id] = new_value >= 0 and new_value or 0
end)
end
---Update active_session without emitting a change event, used when a silent
---in-place update is needed (e.g. session metadata refresh that must not
---trigger a re-render)
---@param session Session
function M.update_silently(session)
store.set_raw('active_session', session)
end
return M