@@ -10,6 +10,7 @@ local icons = require('opencode.ui.icons')
1010local git_review = require (' opencode.git_review' )
1111local history = require (' opencode.history' )
1212local config = require (' opencode.config' )
13+ local Promise = require (' opencode.promise' )
1314
1415local M = {}
1516
@@ -49,14 +50,14 @@ function M.paste_image()
4950 core .paste_image_from_clipboard ()
5051end
5152
52- function M .toggle (new_session )
53+ M .toggle = Promise .async (function (new_session )
54+ local focus = state .last_focused_opencode_window or ' input' --- @cast focus ' input' | ' output'
5355 if state .windows == nil then
54- local focus = state .last_focused_opencode_window or ' input' --- @cast focus ' input' | ' output'
55- core .open ({ new_session = new_session == true , focus = focus , start_insert = false })
56+ core .open ({ new_session = new_session == true , focus = focus , start_insert = false }):await ()
5657 else
5758 M .close ()
5859 end
59- end
60+ end )
6061
6162function M .toggle_focus (new_session )
6263 if not ui .is_opencode_focused () then
115116--- @param from_snapshot_id ? string
116117--- @param to_snapshot_id ? string | number
117118function M .diff_open (from_snapshot_id , to_snapshot_id )
118- core .open ({ new_session = false , focus = ' output' }):and_then (function ()
119+ core .open_if_closed ({ new_session = false , focus = ' output' }):and_then (function ()
119120 git_review .review (from_snapshot_id )
120121 end )
121122end
@@ -333,15 +334,15 @@ function M.debug_session()
333334 debug_helper .debug_session ()
334335end
335336
336- function M .initialize ()
337+ M .initialize = Promise . async ( function ()
337338 local id = require (' opencode.id' )
338339
339- local new_session = core .create_new_session (' AGENTS.md Initialization' )
340+ local new_session = core .create_new_session (' AGENTS.md Initialization' ): await ()
340341 if not new_session then
341342 vim .notify (' Failed to create new session' , vim .log .levels .ERROR )
342343 return
343344 end
344- if not core .initialize_current_model () or not state .current_model then
345+ if not core .initialize_current_model (): await () or not state .current_model then
345346 vim .notify (' No model selected' , vim .log .levels .ERROR )
346347 return
347348 end
@@ -357,7 +358,7 @@ function M.initialize()
357358 modelID = modelId ,
358359 messageID = id .ascending (' message' ),
359360 })
360- end
361+ end )
361362
362363function M .agent_plan ()
363364 require (' opencode.core' ).switch_to_mode (' plan' )
@@ -463,8 +464,8 @@ function M.help()
463464 ui .render_lines (msg )
464465end
465466
466- function M .mcp ()
467- local mcp = config_file .get_mcp_servers ()
467+ M .mcp = Promise . async ( function ()
468+ local mcp = config_file .get_mcp_servers (): await ()
468469 if not mcp then
469470 vim .notify (' No MCP configuration found. Please check your opencode config file.' , vim .log .levels .WARN )
470471 return
@@ -502,7 +503,7 @@ function M.mcp()
502503
503504 table.insert (msg , ' ' )
504505 ui .render_lines (msg )
505- end
506+ end )
506507
507508function M .commands_list ()
508509 local commands = config_file .get_user_commands ()
@@ -530,14 +531,14 @@ function M.commands_list()
530531 ui .render_lines (msg )
531532end
532533
533- function M .current_model ()
534+ M .current_model = Promise . async ( function ()
534535 return core .initialize_current_model ()
535- end
536+ end )
536537
537538--- Runs a user-defined command by name.
538539--- @param name string The name of the user command to run.
539540--- @param args ? string[] Additional arguments to pass to the command.
540- function M .run_user_command (name , args )
541+ M .run_user_command = Promise . async ( function (name , args )
541542 return M .open_input ():and_then (function ()
542543 local user_commands = config_file .get_user_commands ()
543544 local command_cfg = user_commands and user_commands [name ]
@@ -566,7 +567,7 @@ function M.run_user_command(name, args)
566567 end )
567568 end )
568569 end )
569- end
570+ end )
570571
571572--- Compacts the current session by removing unnecessary data.
572573--- @param current_session ? Session The session to compact. Defaults to the active session.
737738
738739--- @param current_session ? Session
739740--- @param new_title ? string
740- function M .rename_session (current_session , new_title )
741+ M .rename_session = Promise . async ( function (current_session , new_title )
741742 local promise = require (' opencode.promise' ).new ()
742- current_session = current_session or vim .deepcopy (state .active_session ) --[[ @as Session]]
743+ current_session = current_session or ( state . active_session and vim .deepcopy (state .active_session ) or nil ) --[[ @as Session]]
743744 if not current_session then
744745 vim .notify (' No active session to rename' , vim .log .levels .WARN )
745746 promise :resolve (nil )
@@ -756,7 +757,7 @@ function M.rename_session(current_session, new_title)
756757 :and_then (function ()
757758 current_session .title = title
758759 if state .active_session and state .active_session .id == current_session .id then
759- local session_obj = session .get_by_id (current_session .id )
760+ local session_obj = session .get_by_id (current_session .id ): await ()
760761 if session_obj then
761762 session_obj .title = title
762763 state .active_session = vim .deepcopy (session_obj )
@@ -781,7 +782,7 @@ function M.rename_session(current_session, new_title)
781782 end )
782783 end )
783784 return promise
784- end
785+ end )
785786
786787-- Returns the ID of the next user message after the current undo point
787788-- This is a port of the opencode tui logic
@@ -906,6 +907,7 @@ function M.toggle_tool_output()
906907 ui .render_output ()
907908end
908909
910+ --- @type table<string , OpencodeUICommand>
909911M .commands = {
910912 open = {
911913 desc = ' Open opencode window (input/output)' ,
@@ -972,7 +974,7 @@ M.commands = {
972974 vim .notify (' Failed to create new session' , vim .log .levels .ERROR )
973975 return
974976 end
975- state .active_session = new_session
977+ state .active_session = new_session : await ()
976978 M .open_input ()
977979 else
978980 M .open_input_new_session ()
@@ -1351,7 +1353,7 @@ function M.setup_legacy_commands()
13511353 end
13521354end
13531355
1354- function M .get_slash_commands ()
1356+ M .get_slash_commands = Promise . async ( function ()
13551357 local result = {}
13561358 for slash_cmd , def in pairs (M .slash_commands_map ) do
13571359 table.insert (result , {
@@ -1361,7 +1363,7 @@ function M.get_slash_commands()
13611363 })
13621364 end
13631365
1364- local user_commands = config_file .get_user_commands ()
1366+ local user_commands = config_file .get_user_commands (): await ()
13651367 if user_commands then
13661368 for name , def in pairs (user_commands ) do
13671369 table.insert (result , {
@@ -1376,7 +1378,7 @@ function M.get_slash_commands()
13761378 end
13771379
13781380 return result
1379- end
1381+ end )
13801382
13811383function M .setup ()
13821384 vim .api .nvim_create_user_command (' Opencode' , M .route_command , {
0 commit comments