feat: add the possibility to add initial files for the prompt

This commit is contained in:
2024-12-14 15:28:42 +01:00
parent 63832ae0c3
commit 44a8458b97
3 changed files with 64 additions and 34 deletions

View File

@@ -3,29 +3,28 @@ local uv = vim.loop
local ok_yaml, lyaml = pcall(require, "lyaml")
local prompt_blocks = {
["go-development"] = "You are a coding assistant specialized in Go development. You will receive a projects 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 projects context and the users 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 guide them through a workflow:\n1. First, ask them which files from the project are needed to understand and perform the coding task.\n2. If more information or context is needed, ask the user (outside of the YAML) to provide that.\n3. When all necessary information is gathered, provide the final YAML with the project's name and a list of files to be created or modified.\n\nThe final YAML must have a top-level key named 'project_name' that matches the project's configured name, and a top-level key named 'files', which is a list of file changes. Each element in 'files' must be a mapping with:\n- 'path' for the file path relative to the projects root directory.\n- either 'content' with a multiline string for new content, or 'delete: true' if the file should be deleted.\n\nIf more context is needed at any point before providing the final YAML, request it outside of the YAML."
}
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
@@ -39,12 +38,6 @@ local function get_config_path()
return root .. "/.chatgpt_config.yaml"
end
local prompt_blocks = {
["go-development"] = "You are a coding assistant specialized in Go development. You will receive a projects 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 projects context and the users 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 guide them through a workflow:\n1. First, ask them which files from the project are needed to understand and perform the coding task.\n2. If more context or information is needed, ask the user (outside of the YAML) to provide that.\n3. When all necessary information is gathered, provide the final YAML with the project's name and a list of files to be created or modified.\n\nThe final YAML must have a top-level key named 'project_name' that matches the project's configured name, and a top-level key named 'files', which is a list of file changes. Each element in 'files' must be a mapping with:\n- 'path' for the file path relative to the projects root directory.\n- either 'content' with a multiline string for new content, or 'delete: true' if the file should be deleted.\n\nIf more context is needed at any point before providing the final YAML, request it outside of the YAML."
}
function M.load()
local path = get_config_path()
local fd = uv.fs_open(path, "r", 438)
@@ -54,7 +47,8 @@ function M.load()
default_prompt_blocks = {},
token_limit = 128000,
project_name = "",
debug = false
debug = false,
initial_files = {}
}
if fd then
@@ -82,14 +76,15 @@ function M.load()
if type(result.debug) == "boolean" then
config.debug = result.debug
end
if type(result.initial_files) == "table" then
config.initial_files = result.initial_files
end
end
end
else
-- Default fallback configuration
config.initial_prompt = "You are a coding assistant who receives a project's context and user instructions. You will guide the user through a workflow:\n1. First, ask the user which files are needed from the project to understand and perform the coding task.\n2. If more information or context is needed, ask for it outside of the YAML.\n3. Once all information is obtained, return the final YAML with the project_name and the files to be created/modified or deleted.\n\nThe final YAML must have:\nproject_name: <project_name>\nfiles:\n - path: \"relative/path/to/file\"\n content: |\n <file content>\n - path: \"relative/path/to/other_file\"\n delete: true\n\nIf more context is needed at any step before providing the final YAML, request it outside of the YAML."
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