-- lua/chatgpt_nvim/config.lua -- Modified to: -- 1) Determine the Git root based on the currently opened file. -- 2) If no file is open or not in Git repo, fallback to current working directory. -- 3) Add support for configuring a list of default prompt blocks ("go-development", "typo3-development", "basic-prompt") that can override the initial prompt if provided. -- 4) Add support for configuring a token limit. -- 5) Load a project_name from the .chatgpt_config.yaml for project verification. local M = {} local uv = vim.loop local ok_yaml, lyaml = pcall(require, "lyaml") local function get_project_root() -- Get the directory of the currently opened file. -- If no file is open, we fallback to current working directory. local current_file = vim.fn.expand("%:p") local root_dir if current_file == "" then -- No file opened, fallback to cwd root_dir = vim.fn.getcwd() else -- Extract directory from current file path local file_dir = current_file:match("(.*)/") if not file_dir then -- If something went wrong extracting the directory, fallback to cwd root_dir = vim.fn.getcwd() else -- Attempt to find git root from the file's directory local cmd = string.format("cd %s && git rev-parse --show-toplevel 2>/dev/null", vim.fn.shellescape(file_dir)) local git_root = vim.fn.systemlist(cmd) if vim.v.shell_error == 0 and git_root and #git_root > 0 then root_dir = git_root[1] else -- Not a git repo or failed to find git root, fallback to the file's directory root_dir = file_dir end end end return root_dir end local function get_config_path() local root = get_project_root() return root .. "/.chatgpt_config.yaml" end local prompt_blocks = { ["go-development"] = "You are a coding assistant specialized in Go development. You will receive a project’s context and user instructions related to Go code, and you must return the requested modifications or guidance. When returning modifications, follow the specified YAML structure. Keep your suggestions aligned with Go best practices and idiomatic Go.", ["typo3-development"] = "You are a coding assistant specialized in TYPO3 development. You have access to the project’s context and the user’s instructions. Your answers should focus on TYPO3 coding guidelines, extension development best practices, and TSconfig or TypoScript recommendations. When returning modifications, follow the YAML structure given.", ["basic-prompt"] = "You are a coding assistant who receives a project’s context and user instructions. The user will provide a prompt, and you will return modifications to the project in a YAML structure. The YAML must have a top-level key named files, which should be a list of file changes. Each element in files must be a mapping with the keys path and content. The path should be the file path relative to the project’s root directory, and content should contain the new file content as a multiline string. Do not include additional commentary outside of the YAML.\nAlso include a top-level key named project_name that must match the project's configured name." } function M.load() local path = get_config_path() local fd = uv.fs_open(path, "r", 438) local config = { initial_prompt = "", directories = { "." }, default_prompt_blocks = {}, token_limit = 128000, project_name = "" } if fd then local stat = uv.fs_fstat(fd) local data = uv.fs_read(fd, stat.size, 0) uv.fs_close(fd) if data and ok_yaml then local ok, result = pcall(lyaml.load, data) if ok and type(result) == "table" then if type(result.initial_prompt) == "string" then config.initial_prompt = result.initial_prompt end if type(result.directories) == "table" then config.directories = result.directories end if type(result.default_prompt_blocks) == "table" then config.default_prompt_blocks = result.default_prompt_blocks end if type(result.token_limit) == "number" then config.token_limit = result.token_limit end if type(result.project_name) == "string" then config.project_name = result.project_name end end end else -- Default fallback configuration config.initial_prompt = "You are a coding assistant who receives a project’s context and user instructions. The user will provide a prompt, and you will return modifications to the project in a YAML structure. This YAML must have a top-level key named files, which should be a list of file changes, and a top-level key named project_name that must match the project's configured name. Each element in files must be a mapping with the keys path and content. The path should be the file path relative to the project’s root directory, and content should contain the new file content as a multiline string (using the YAML | literal style). Do not include additional commentary outside of the YAML.\n Here is the structure expected in your final answer:\n project_name: \n files:\n - path: \"relative/path/to/file1.ext\"\n content: |\n \n - path: \"relative/path/to/file2.ext\"\n content: |\n \n Based on the prompt and project context provided, you must only return the YAML structure shown above (with actual file paths and contents substituted in). Any file that should be created or modified must appear as one of the files entries. The content should contain the complete and final code for that file, reflecting all changes requested by the user." end -- If default_prompt_blocks are specified, concatenate all matching prompts if type(config.default_prompt_blocks) == "table" and #config.default_prompt_blocks > 0 then local merged_prompt = {} for _, block_name in ipairs(config.default_prompt_blocks) do if prompt_blocks[block_name] then table.insert(merged_prompt, prompt_blocks[block_name]) end end if #merged_prompt > 0 then config.initial_prompt = table.concat(merged_prompt, "\n\n") end end return config end return M