@@ -10,6 +10,7 @@ local config = require('opencode.config')
1010local image_handler = require (' opencode.image_handler' )
1111local Promise = require (' opencode.promise' )
1212local permission_window = require (' opencode.ui.permission_window' )
13+ local log = require (' opencode.log' )
1314
1415local M = {}
1516M ._abort_count = 0
@@ -57,6 +58,27 @@ M.open_if_closed = Promise.async(function(opts)
5758 end
5859end )
5960
61+ M .is_prompting_allowed = function ()
62+ local mentioned_files = context .get_context ().mentioned_files or {}
63+ local allowed , err_msg = util .check_prompt_allowed (config .prompt_guard , mentioned_files )
64+ if not allowed then
65+ vim .notify (err_msg or ' Prompt denied by prompt_guard' , vim .log .levels .ERROR )
66+ end
67+ return allowed
68+ end
69+
70+ M .check_cwd = function ()
71+ if state .current_cwd ~= vim .fn .getcwd () then
72+ log .debug (
73+ ' CWD changed since last check, resetting session and context' ,
74+ { current_cwd = state .current_cwd , new_cwd = vim .fn .getcwd () }
75+ )
76+ state .current_cwd = vim .fn .getcwd ()
77+ state .active_session = nil
78+ context .unload_attachments ()
79+ end
80+ end
81+
6082--- @param opts ? OpenOpts
6183M .open = Promise .async (function (opts )
6284 opts = opts or { focus = ' input' , new_session = false }
@@ -69,13 +91,7 @@ M.open = Promise.async(function(opts)
6991
7092 local are_windows_closed = state .windows == nil
7193 if are_windows_closed then
72- -- Check if whether prompting will be allowed
73- local mentioned_files = context .get_context ().mentioned_files or {}
74- local allowed , err_msg = util .check_prompt_allowed (config .prompt_guard , mentioned_files )
75- if not allowed then
76- vim .notify (err_msg or ' Prompts will be denied by prompt_guard' , vim .log .levels .WARN )
77- end
78-
94+ M .is_prompting_allowed ()
7995 state .windows = ui .create_windows ()
8096 end
8197
@@ -85,22 +101,16 @@ M.open = Promise.async(function(opts)
85101 ui .focus_output ({ restore_position = are_windows_closed })
86102 end
87103
88- local server
89- local server_ok , server_err = pcall (function ()
90- server = server_job .ensure_server ():await ()
91- end )
104+ local server = server_job .ensure_server ():await ()
92105
93- if not server_ok or not server then
106+ if not server then
94107 state .is_opening = false
95- vim .notify (' Failed to start opencode server: ' .. tostring (server_err or ' Unknown error' ), vim .log .levels .ERROR )
96- return Promise .new ():reject (server_err or ' Server failed to start' )
108+ return Promise .new ():reject (' Server failed to start' )
97109 end
98110
99- state . opencode_server = server
111+ M . check_cwd ()
100112
101113 local ok , err = pcall (function ()
102- state .opencode_server = server
103-
104114 if opts .new_session then
105115 state .active_session = nil
106116 state .last_sent_context = nil
@@ -109,6 +119,7 @@ M.open = Promise.async(function(opts)
109119 M .ensure_current_mode ():await ()
110120
111121 state .active_session = M .create_new_session ():await ()
122+ log .debug (' Created new session on open' , { session = state .active_session .id })
112123 else
113124 M .ensure_current_mode ():await ()
114125 if not state .active_session then
@@ -549,32 +560,22 @@ end
549560M .handle_directory_change = Promise .async (function ()
550561 local log = require (' opencode.log' )
551562 if not state .active_session then
552- is_new = true
553563 state .active_session = M .create_new_session ():await ()
554564 end
555565
556- if state .opencode_server then
557- vim .notify (' Working directory changed.' , vim .log .levels .INFO )
558- log .debug (' Working directory change %s' , vim .inspect ({ cwd = vim .fn .getcwd () }))
566+ vim .notify (' Working directory changed.' , vim .log .levels .INFO )
567+ log .debug (' Working directory change %s' , vim .inspect ({ cwd = vim .fn .getcwd () }))
559568
560- vim .defer_fn (
561- Promise .async (function ()
562- vim .notify (' Loading last session for new working dir [' .. vim .fn .getcwd () .. ' ]' , vim .log .levels .INFO )
569+ vim .notify (' Loading last session for new working dir [' .. vim .fn .getcwd () .. ' ]' , vim .log .levels .INFO )
563570
564- state .active_session = nil
565- state .last_sent_context = nil
566- context .unload_attachments ()
571+ state .active_session = nil
572+ state .last_sent_context = nil
573+ context .unload_attachments ()
567574
568- local is_new = false
569- state .active_session = session .get_last_workspace_session ():await () or M .create_new_session ():await ()
575+ local is_new = false
576+ state .active_session = session .get_last_workspace_session ():await () or M .create_new_session ():await ()
570577
571- log .debug (
572- ' Loaded session for new working dir' .. vim .inspect ({ session = state .active_session , is_new = is_new })
573- )
574- end ),
575- 200
576- )
577- end
578+ log .debug (' Loaded session for new working dir' .. vim .inspect ({ session = state .active_session , is_new = is_new }))
578579end )
579580
580581function M .setup ()
0 commit comments