@@ -69,47 +69,64 @@ function M.open(opts)
6969 state .windows = ui .create_windows ()
7070 end
7171
72- server_job .ensure_server ():and_then (function (server )
73- state .opencode_server = server
72+ if opts .focus == ' input' then
73+ ui .focus_input ({ restore_position = are_windows_closed , start_insert = opts .start_insert == true })
74+ elseif opts .focus == ' output' then
75+ ui .focus_output ({ restore_position = are_windows_closed })
76+ end
7477
75- M .ensure_current_mode ()
78+ server_job
79+ .ensure_server ()
80+ :and_then (function (server )
81+ local ok , err = pcall (function ()
82+ state .opencode_server = server
7683
77- if opts .new_session then
78- state .active_session = nil
79- state .last_sent_context = nil
84+ M .ensure_current_mode ()
8085
81- state .current_model = nil
82- state .current_mode = nil
83- M .ensure_current_mode ()
86+ if opts .new_session then
87+ state .active_session = nil
88+ state .last_sent_context = nil
89+
90+ state .current_model = nil
91+ state .current_mode = nil
92+ M .ensure_current_mode ()
8493
85- state .active_session = M .create_new_session ()
86- else
87- if not state .active_session then
88- state .active_session = session .get_last_workspace_session ()
89- if not state .active_session then
9094 state .active_session = M .create_new_session ()
95+ else
96+ if not state .active_session then
97+ state .active_session = session .get_last_workspace_session ()
98+ if not state .active_session then
99+ state .active_session = M .create_new_session ()
100+ end
101+ else
102+ if not state .display_route and are_windows_closed then
103+ -- We're not displaying /help or something like that but we have an active session
104+ -- and the windows were closed so we need to do a full refresh. This mostly happens
105+ -- when opening the window after having closed it since we're not currently clearing
106+ -- the session on api.close()
107+ ui .render_output ()
108+ end
109+ end
91110 end
92- else
93- if not state .display_route and are_windows_closed then
94- -- We're not displaying /help or something like that but we have an active session
95- -- and the windows were closed so we need to do a full refresh. This mostly happens
96- -- when opening the window after having closed it since we're not currently clearing
97- -- the session on api.close()
98- ui .render_output ()
99- end
100- end
101- end
102- promise :resolve ()
103- state .is_opening = false
104111
105- if opts .focus == ' input' then
106- ui .focus_input ({ restore_position = are_windows_closed , start_insert = opts .start_insert == true })
107- elseif opts .focus == ' output' then
108- ui .focus_output ({ restore_position = are_windows_closed })
109- end
112+ state .is_opencode_focused = true
113+ end )
110114
111- state .is_opencode_focused = true
112- end )
115+ state .is_opening = false
116+
117+ if not ok then
118+ promise :reject (err )
119+ vim .notify (' Error opening panel: ' .. tostring (err ), vim .log .levels .ERROR )
120+ return
121+ end
122+
123+ promise :resolve ()
124+ end )
125+ :catch (function (err )
126+ state .is_opening = false
127+ promise :reject (err )
128+ vim .notify (' Error ensuring server: ' .. tostring (err ), vim .log .levels .ERROR )
129+ end )
113130 return promise
114131end
115132
0 commit comments