@@ -3,6 +3,13 @@ local M = {
33 handlers = {},
44}
55
6+ local core = require (' opencode.core' )
7+ --- @type OpencodeState
8+ local state = require (' opencode.state' )
9+ local session_store = require (' opencode.session' )
10+ local Promise = require (' opencode.promise' )
11+ local window_actions = require (' opencode.commands.handlers.window' ).actions
12+
613--- @param message string
714local function invalid_arguments (message )
815 error ({
@@ -11,57 +18,37 @@ local function invalid_arguments(message)
1118 }, 0 )
1219end
1320
14- local function core ()
15- return require (' opencode.core' )
16- end
17-
18- local function state ()
19- return require (' opencode.state' )
20- end
21-
22- local function session_store ()
23- return require (' opencode.session' )
24- end
25-
26- local function Promise ()
27- return require (' opencode.promise' )
28- end
29-
30- local function window_actions ()
31- return require (' opencode.commands.handlers.window' ).actions
32- end
33-
3421function M .actions .open_input_new_session ()
35- return core () .open ({ new_session = true , focus = ' input' , start_insert = true })
22+ return core .open ({ new_session = true , focus = ' input' , start_insert = true })
3623end
3724
3825--- @param title string
3926function M .actions .open_input_new_session_with_title (title )
40- return Promise () .async (function (session_title )
41- local new_session = core () .create_new_session (session_title ):await ()
27+ return Promise .async (function (session_title )
28+ local new_session = core .create_new_session (session_title ):await ()
4229 if not new_session then
4330 vim .notify (' Failed to create new session' , vim .log .levels .ERROR )
4431 return
4532 end
4633
47- state () .session .set_active (new_session )
48- return window_actions () .open_input ()
34+ state .session .set_active (new_session )
35+ return window_actions .open_input ()
4936 end )(title )
5037end
5138
5239--- @param parent_id ? string
5340function M .actions .select_session (parent_id )
54- core () .select_session (parent_id )
41+ core .select_session (parent_id )
5542end
5643
5744function M .actions .select_child_session ()
58- local active = state () .active_session
59- core () .select_session (active and active .id or nil )
45+ local active = state .active_session
46+ core .select_session (active and active .id or nil )
6047end
6148
6249--- @param current_session ? Session
6350function M .actions .compact_session (current_session )
64- local state_obj = state ()
51+ local state_obj = state
6552 current_session = current_session or state_obj .active_session
6653 if not current_session then
6754 vim .notify (' No active session to compact' , vim .log .levels .WARN )
@@ -98,7 +85,7 @@ function M.actions.compact_session(current_session)
9885end
9986
10087function M .actions .share ()
101- local state_obj = state ()
88+ local state_obj = state
10289 if not state_obj .active_session then
10390 vim .notify (' No active session to share' , vim .log .levels .WARN )
10491 return
@@ -124,7 +111,7 @@ function M.actions.share()
124111end
125112
126113function M .actions .unshare ()
127- local state_obj = state ()
114+ local state_obj = state
128115 if not state_obj .active_session then
129116 vim .notify (' No active session to unshare' , vim .log .levels .WARN )
130117 return
@@ -145,10 +132,10 @@ function M.actions.unshare()
145132end
146133
147134function M .actions .initialize ()
148- return Promise () .async (function ()
135+ return Promise .async (function ()
149136 local id = require (' opencode.id' )
150- local state_obj = state ()
151- local core_obj = core ()
137+ local state_obj = state
138+ local core_obj = core
152139
153140 local new_session = core_obj .create_new_session (' AGENTS.md Initialization' ):await ()
154141 if not new_session then
@@ -168,7 +155,7 @@ function M.actions.initialize()
168155 end
169156
170157 state_obj .session .set_active (new_session )
171- window_actions () .open_input ()
158+ window_actions .open_input ()
172159 state_obj .api_client :init_session (state_obj .active_session .id , {
173160 providerID = providerId ,
174161 modelID = modelId ,
180167--- @param current_session ? Session
181168--- @param new_title ? string
182169function M .actions .rename_session (current_session , new_title )
183- return Promise () .async (function (session_obj , requested_title )
184- local promise = Promise () .new ()
185- local state_obj = state ()
170+ return Promise .async (function (session_obj , requested_title )
171+ local promise = Promise .new ()
172+ local state_obj = state
186173 session_obj = session_obj or (state_obj .active_session and vim .deepcopy (state_obj .active_session ) or nil ) --[[ @as Session]]
187174 if not session_obj then
188175 vim .notify (' No active session to rename' , vim .log .levels .WARN )
@@ -198,10 +185,10 @@ function M.actions.rename_session(current_session, new_title)
198185 vim .notify (' Failed to rename session: ' .. vim .inspect (err ), vim .log .levels .ERROR )
199186 end )
200187 end )
201- :and_then (Promise () .async (function ()
188+ :and_then (Promise .async (function ()
202189 session_obj .title = title
203190 if state_obj .active_session and state_obj .active_session .id == session_obj .id then
204- local persisted_session = session_store () .get_by_id (session_obj .id ):await ()
191+ local persisted_session = session_store .get_by_id (session_obj .id ):await ()
205192 if persisted_session then
206193 persisted_session .title = title
207194 state_obj .session .set_active (vim .deepcopy (persisted_session ))
0 commit comments