Skip to content

Commit 9223b98

Browse files
committed
perf: open the panel without waiting on session
1 parent 3cc6e77 commit 9223b98

File tree

4 files changed

+139
-146
lines changed

4 files changed

+139
-146
lines changed

lua/opencode/api.lua

Lines changed: 87 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ function M.toggle_zoom()
2222
end
2323

2424
function M.open_input()
25-
core.open({ new_session = false, focus = 'input', start_insert = true })
25+
return core.open({ new_session = false, focus = 'input', start_insert = true })
2626
end
2727

2828
function M.open_input_new_session()
29-
core.open({ new_session = true, focus = 'input', start_insert = true })
29+
return core.open({ new_session = true, focus = 'input', start_insert = true })
3030
end
3131

3232
function M.open_output()
33-
core.open({ new_session = false, focus = 'output' })
33+
return core.open({ new_session = false, focus = 'output' })
3434
end
3535

3636
function M.close()
@@ -78,15 +78,19 @@ end
7878
---@param prompt string
7979
---@param opts? SendMessageOpts
8080
function M.run(prompt, opts)
81-
opts = vim.tbl_deep_extend('force', { new_session = false, focus = 'output' }, opts or {})
82-
core.send_message(prompt, opts)
81+
opts = vim.tbl_deep_extend('force', { new_session = false, focus = 'input' }, opts or {})
82+
return core.open(opts):and_then(function()
83+
return core.send_message(prompt, opts)
84+
end)
8385
end
8486

8587
---@param prompt string
8688
---@param opts? SendMessageOpts
8789
function M.run_new_session(prompt, opts)
88-
opts = vim.tbl_deep_extend('force', { new_session = true, focus = 'output' }, opts or {})
89-
core.send_message(prompt, opts)
90+
opts = vim.tbl_deep_extend('force', { new_session = true, focus = 'input' }, opts or {})
91+
return core.open(opts):and_then(function()
92+
return core.send_message(prompt, opts)
93+
end)
9094
end
9195

9296
---@param parent_id? string
@@ -114,112 +118,92 @@ end
114118
---@param from_snapshot_id? string
115119
---@param to_snapshot_id? string|number
116120
function M.diff_open(from_snapshot_id, to_snapshot_id)
117-
if not state.messages or not state.active_session then
118-
core.open({ new_session = false, focus = 'output' })
119-
end
120-
121-
git_review.review(from_snapshot_id)
121+
core.open({ new_session = false, focus = 'output' }):and_then(function()
122+
git_review.review(from_snapshot_id)
123+
end)
122124
end
123125

124126
function M.diff_next()
125-
if not state.windows then
126-
core.open({ new_session = false, focus = 'output' })
127-
end
128-
129-
git_review.next_diff()
127+
core.open({ new_session = false, focus = 'output' }):and_then(function()
128+
git_review.next_diff()
129+
end)
130130
end
131131

132132
function M.diff_prev()
133-
if not state.windows then
134-
core.open({ new_session = false, focus = 'output' })
135-
end
136-
137-
git_review.prev_diff()
133+
core.open({ new_session = false, focus = 'output' }):and_then(function()
134+
git_review.prev_diff()
135+
end)
138136
end
139137

140138
function M.diff_close()
141-
if not state.windows then
142-
core.open({ new_session = false, focus = 'output' })
143-
end
144-
145-
git_review.close_diff()
139+
core.open({ new_session = false, focus = 'output' }):and_then(function()
140+
git_review.close_diff()
141+
end)
146142
end
147143

148144
---@param from_snapshot_id? string
149145
function M.diff_revert_all(from_snapshot_id)
150-
if not state.windows then
151-
core.open({ new_session = false, focus = 'output' })
152-
end
153-
154-
git_review.revert_all(from_snapshot_id)
146+
core.open({ new_session = false, focus = 'output' }):and_then(function()
147+
git_review.revert_all(from_snapshot_id)
148+
end)
155149
end
156150

157151
---@param from_snapshot_id? string
158152
---@param to_snapshot_id? string
159153
function M.diff_revert_selected_file(from_snapshot_id, to_snapshot_id)
160-
if not state.windows then
161-
core.open({ new_session = false, focus = 'output' })
162-
end
163-
164-
git_review.revert_selected_file(from_snapshot_id)
154+
core.open({ new_session = false, focus = 'output' }):and_then(function()
155+
git_review.revert_selected_file(from_snapshot_id)
156+
end)
165157
end
166158

167159
---@param restore_point_id? string
168160
function M.diff_restore_snapshot_file(restore_point_id)
169-
if not state.windows then
170-
core.open({ new_session = false, focus = 'output' })
171-
end
172-
173-
git_review.restore_snapshot_file(restore_point_id)
161+
core.open({ new_session = false, focus = 'output' }):and_then(function()
162+
git_review.restore_snapshot_file(restore_point_id)
163+
end)
174164
end
175165

176166
---@param restore_point_id? string
177167
function M.diff_restore_snapshot_all(restore_point_id)
178-
if not state.windows then
179-
core.open({ new_session = false, focus = 'output' })
180-
end
181-
182-
git_review.restore_snapshot_all(restore_point_id)
168+
core.open({ new_session = false, focus = 'output' }):and_then(function()
169+
git_review.restore_snapshot_all(restore_point_id)
170+
end)
183171
end
184172

185173
function M.diff_revert_all_last_prompt()
186-
if not state.windows then
187-
core.open({ new_session = false, focus = 'output' })
188-
end
189-
190-
local snapshots = session.get_message_snapshot_ids(state.current_message)
191-
local snapshot_id = snapshots and snapshots[1]
192-
if not snapshot_id then
193-
vim.notify('No snapshots found for the current message', vim.log.levels.WARN)
194-
return
195-
end
196-
git_review.revert_all(snapshot_id)
174+
core.open({ new_session = false, focus = 'output' }):and_then(function()
175+
local snapshots = session.get_message_snapshot_ids(state.current_message)
176+
local snapshot_id = snapshots and snapshots[1]
177+
if not snapshot_id then
178+
vim.notify('No snapshots found for the current message', vim.log.levels.WARN)
179+
return
180+
end
181+
git_review.revert_all(snapshot_id)
182+
end)
197183
end
198184

199185
function M.diff_revert_this(snapshot_id)
200-
if not state.windows then
201-
core.open({ new_session = false, focus = 'output' })
202-
end
203-
204-
git_review.revert_current(snapshot_id)
186+
core.open({ new_session = false, focus = 'output' }):and_then(function()
187+
git_review.revert_current(snapshot_id)
188+
end)
205189
end
206190

207191
function M.diff_revert_this_last_prompt()
208-
if not state.windows then
209-
core.open({ new_session = false, focus = 'output' })
210-
end
211-
212-
local snapshots = session.get_message_snapshot_ids(state.current_message)
213-
local snapshot_id = snapshots and snapshots[1]
214-
if not snapshot_id then
215-
vim.notify('No snapshots found for the current message', vim.log.levels.WARN)
216-
return
217-
end
192+
core.open({ new_session = false, focus = 'output' }):and_then(function()
193+
local snapshots = session.get_message_snapshot_ids(state.current_message)
194+
local snapshot_id = snapshots and snapshots[1]
195+
if not snapshot_id then
196+
vim.notify('No snapshots found for the current message', vim.log.levels.WARN)
197+
return
198+
end
199+
git_review.revert_current(snapshot_id)
200+
end)
218201
end
219202

220203
function M.set_review_breakpoint()
221-
vim.notify('Setting review breakpoint is not implemented yet', vim.log.levels.WARN)
222-
git_review.create_snapshot()
204+
core.open({ new_session = false, focus = 'output' }):and_then(function()
205+
git_review.create_snapshot()
206+
end)
223207
end
224208

225209
function M.prev_history()
@@ -245,7 +229,6 @@ function M.next_history()
245229
end
246230

247231
function M.prev_prompt_history()
248-
local config = require('opencode.config')
249232
local key = config.get_key_for_function('input_window', 'prev_prompt_history')
250233
if key ~= '<up>' then
251234
return M.prev_history()
@@ -260,7 +243,6 @@ function M.prev_prompt_history()
260243
end
261244

262245
function M.next_prompt_history()
263-
local config = require('opencode.config')
264246
local key = config.get_key_for_function('input_window', 'next_prompt_history')
265247
if key ~= '<down>' then
266248
return M.next_history()
@@ -305,22 +287,19 @@ function M.mention_file()
305287
end
306288

307289
function M.mention()
308-
local config = require('opencode.config')
309290
local char = config.get_key_for_function('input_window', 'mention')
310291

311292
ui.focus_input({ restore_position = false, start_insert = true })
312293
require('opencode.ui.completion').trigger_completion(char)()
313294
end
314295

315296
function M.context_items()
316-
local config = require('opencode.config')
317297
local char = config.get_key_for_function('input_window', 'context_items')
318298
ui.focus_input({ restore_position = true, start_insert = true })
319299
require('opencode.ui.completion').trigger_completion(char)()
320300
end
321301

322302
function M.slash_commands()
323-
local config = require('opencode.config')
324303
local char = config.get_key_for_function('input_window', 'slash_commands')
325304
ui.focus_input({ restore_position = false, start_insert = true })
326305
require('opencode.ui.completion').trigger_completion(char)()
@@ -331,7 +310,6 @@ function M.focus_input()
331310
end
332311

333312
function M.debug_output()
334-
local config = require('opencode.config')
335313
if not config.debug.enabled then
336314
vim.notify('Debugging is not enabled in the config', vim.log.levels.WARN)
337315
return
@@ -341,7 +319,6 @@ function M.debug_output()
341319
end
342320

343321
function M.debug_message()
344-
local config = require('opencode.config')
345322
if not config.debug.enabled then
346323
vim.notify('Debugging is not enabled in the config', vim.log.levels.WARN)
347324
return
@@ -351,7 +328,6 @@ function M.debug_message()
351328
end
352329

353330
function M.debug_session()
354-
local config = require('opencode.config')
355331
if not config.debug.enabled then
356332
vim.notify('Debugging is not enabled in the config', vim.log.levels.WARN)
357333
return
@@ -565,33 +541,34 @@ end
565541
--- @param name string The name of the user command to run.
566542
--- @param args? string[] Additional arguments to pass to the command.
567543
function M.run_user_command(name, args)
568-
M.open_input()
569-
local user_commands = config_file.get_user_commands()
570-
local command_cfg = user_commands and user_commands[name]
571-
if not command_cfg then
572-
vim.notify('Unknown user command: ' .. name, vim.log.levels.WARN)
573-
return
574-
end
544+
return M.open_input():and_then(function()
545+
local user_commands = config_file.get_user_commands()
546+
local command_cfg = user_commands and user_commands[name]
547+
if not command_cfg then
548+
vim.notify('Unknown user command: ' .. name, vim.log.levels.WARN)
549+
return
550+
end
575551

576-
local model = command_cfg.model or state.current_model
577-
local agent = command_cfg.agent or state.current_mode
552+
local model = command_cfg.model or state.current_model
553+
local agent = command_cfg.agent or state.current_mode
578554

579-
if not state.active_session then
580-
vim.notify('No active session', vim.log.levels.WARN)
581-
return
582-
end
583-
state.api_client
584-
:send_command(state.active_session.id, {
585-
command = name,
586-
arguments = table.concat(args or {}, ' '),
587-
model = model,
588-
agent = agent,
589-
})
590-
:and_then(function()
591-
vim.schedule(function()
592-
require('opencode.history').write('/' .. name .. ' ' .. table.concat(args or {}, ' '))
555+
if not state.active_session then
556+
vim.notify('No active session', vim.log.levels.WARN)
557+
return
558+
end
559+
state.api_client
560+
:send_command(state.active_session.id, {
561+
command = name,
562+
arguments = table.concat(args or {}, ' '),
563+
model = model,
564+
agent = agent,
565+
})
566+
:and_then(function()
567+
vim.schedule(function()
568+
require('opencode.history').write('/' .. name .. ' ' .. table.concat(args or {}, ' '))
569+
end)
593570
end)
594-
end)
571+
end)
595572
end
596573

597574
--- Compacts the current session by removing unnecessary data.
@@ -1147,7 +1124,7 @@ M.commands = {
11471124
vim.notify('Prompt required', vim.log.levels.ERROR)
11481125
return
11491126
end
1150-
M.run(prompt, opts)
1127+
return M.run(prompt, opts)
11511128
end,
11521129
},
11531130

@@ -1159,7 +1136,7 @@ M.commands = {
11591136
vim.notify('Prompt required', vim.log.levels.ERROR)
11601137
return
11611138
end
1162-
M.run_new_session(prompt, opts)
1139+
return M.run_new_session(prompt, opts)
11631140
end,
11641141
},
11651142

@@ -1359,7 +1336,6 @@ function M.complete_command(arg_lead, cmd_line, cursor_pos)
13591336
end
13601337

13611338
function M.setup_legacy_commands()
1362-
local config = require('opencode.config')
13631339
if not config.legacy_commands then
13641340
return
13651341
end
@@ -1395,7 +1371,7 @@ function M.get_slash_commands()
13951371
slash_cmd = '/' .. name,
13961372
desc = def.description or 'User command',
13971373
fn = function(...)
1398-
M.run_user_command(name, ...)
1374+
return M.run_user_command(name, ...)
13991375
end,
14001376
args = config_file.command_takes_arguments(def),
14011377
})

0 commit comments

Comments
 (0)