@@ -28,6 +28,7 @@ local M = {
2828 vault = require (" gp.vault" ), -- handles secrets
2929 whisper = require (" gp.whisper" ), -- whisper module
3030 macro = require (" gp.macro" ), -- builder for macro completion
31+ buffer_state = require (" gp.buffer_state" ), -- buffer state module
3132}
3233
3334---- ----------------------------------------------------------------------------
@@ -267,6 +268,23 @@ M.setup = function(opts)
267268 end ,
268269 })
269270
271+ vim .api .nvim_create_autocmd (" BufEnter" , {
272+ callback = function (ev )
273+ local buf = ev .buf
274+ local context_dir = M .buffer_state .get_key (buf , " context_dir" )
275+ context_dir = context_dir or M .helpers .find_git_root ()
276+ if context_dir == " " then
277+ context_dir = vim .fn .getcwd ()
278+ end
279+
280+ local full_path = vim .fn .fnamemodify (context_dir , " :p" )
281+ if vim .fn .isdirectory (full_path ) == 1 then
282+ full_path = vim .fn .resolve (full_path )
283+ M .buffer_state .set (buf , " context_dir" , full_path )
284+ end
285+ end ,
286+ })
287+
270288 if vim .fn .executable (" curl" ) == 0 then
271289 M .logger .error (" curl is not installed, run :checkhealth gp" )
272290 end
@@ -695,9 +713,16 @@ M.new_chat = function(params, toggle, system_prompt, agent)
695713 system_prompt = " "
696714 end
697715
716+ local context_dir = M .buffer_state .get_key (vim .api .nvim_get_current_buf (), " context_dir" )
717+ context_dir = context_dir or M .helpers .find_git_root ()
718+ if context_dir == " " then
719+ context_dir = vim .fn .getcwd ()
720+ end
721+ context_dir = " contextDir: " .. context_dir .. " \n "
722+
698723 local template = M .render .template (M .config .chat_template or require (" gp.defaults" ).chat_template , {
699724 [" {{filename}}" ] = string.match (filename , " ([^/]+)$" ),
700- [" {{optional_headers}}" ] = model .. provider .. system_prompt ,
725+ [" {{optional_headers}}" ] = model .. provider .. system_prompt .. context_dir ,
701726 [" {{user_prefix}}" ] = M .config .chat_user_prefix ,
702727 [" {{respond_shortcut}}" ] = M .config .chat_shortcut_respond .shortcut ,
703728 [" {{cmd_prefix}}" ] = M .config .cmd_prefix ,
@@ -1065,8 +1090,6 @@ M.chat_header_lines = function(lines)
10651090 return lines , 0 , 0
10661091 end
10671092
1068- local header_lines = table.concat (vim .list_slice (lines , 0 , header_end + 1 ), " \n " )
1069-
10701093 local help_template = M .render .template (M .defaults .chat_help , {
10711094 [" {{user_prefix}}" ] = M .config .chat_user_prefix ,
10721095 [" {{respond_shortcut}}" ] = M .config .chat_shortcut_respond .shortcut ,
0 commit comments