Files
chatgpt.vim/lua/chatgpt_nvim/config.lua

57 lines
2.6 KiB
Lua
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
-- lua/chatgpt_nvim/config.lua
-- Modified to load the config file from the git root of the currently opened file
-- If not in a git repository, fallback to current working directory
local M = {}
local uv = vim.loop
local ok_yaml, lyaml = pcall(require, "lyaml")
local function get_project_root()
-- Attempt to find git root
local git_root = vim.fn.systemlist('git rev-parse --show-toplevel')
if vim.v.shell_error == 0 and git_root and #git_root > 0 then
return git_root[1]
end
-- Fallback to current working directory if git root not found
return vim.fn.getcwd()
end
local function get_config_path()
local root = get_project_root()
local config_path = root .. "/.chatgpt_config.yaml"
return config_path
end
function M.load()
local path = get_config_path()
local fd = uv.fs_open(path, "r", 438)
if not fd then
return {
initial_prompt = "",
directories = { "." },
}
end
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
local config = result[1]
if type(config) == "table" then
return {
initial_prompt = config.initial_prompt or "",
directories = config.directories or { "." },
}
end
end
end
return {
initial_prompt = " You are a coding assistant who receives a projects 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. Each element in files must be a mapping with the keys path and content. The path should be the file path relative to the projects 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 files:\n - path: \"relative/path/to/file1.ext\"\n content: |\n <full file content here>\n - path: \"relative/path/to/file2.ext\"\n content: |\n <full file content here>\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.",
directories = { "." },
}
end
return M