feat: add the possibility to delete files. and restrict the editing of files to inside the project

This commit is contained in:
2024-12-13 02:30:54 +01:00
parent 2eb79c2b1a
commit cc37c8505c
3 changed files with 38 additions and 3 deletions

View File

@@ -50,7 +50,7 @@ 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 projects 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 projects 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."
["basic-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. 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 either content (for file creation or modification) or delete: true (if the file should be deleted). The path should be the file path relative to the projects root directory. If content is provided, it should contain the new file content as a multiline string. If delete: true is used, do not include content. If more context is needed, tell it outside of the YAML.\nAlso include a top-level key named project_name that must match the project's configured name."
}
function M.load()

View File

@@ -32,6 +32,18 @@ function M.write_file(filepath, content)
uv.fs_close(fd)
end
function M.delete_file(filepath)
local st = uv.fs_stat(filepath)
if st then
local success, err = uv.fs_unlink(filepath)
if not success then
vim.api.nvim_err_writeln("Could not delete file: " .. filepath .. " - " .. (err or "unknown error"))
end
else
vim.api.nvim_err_writeln("File not found, cannot delete: " .. filepath)
end
end
function M.finish()
print("Finished processing files.")
end

View File

@@ -31,6 +31,12 @@ local function estimate_tokens(text)
return math.floor(length / approx_chars_per_token)
end
local function is_subpath(root, path)
local root_abs = vim.fn.fnamemodify(root, ":p")
local target_abs = vim.fn.fnamemodify(path, ":p")
return target_abs:sub(1, #root_abs) == root_abs
end
function M.run_chatgpt_command()
local conf = config.load()
local user_input = vim.fn.input("Message for O1 Model: ")
@@ -90,13 +96,30 @@ function M.run_chatgpt_paste_command()
return
end
local root = vim.fn.getcwd()
for _, fileinfo in ipairs(data.files) do
if fileinfo.path and fileinfo.content then
if not fileinfo.path then
vim.api.nvim_err_writeln("Invalid file entry. Must have 'path'.")
goto continue
end
-- Ensure the path is within the project root
if not is_subpath(root, fileinfo.path) then
vim.api.nvim_err_writeln("Invalid file path outside project root: " .. fileinfo.path)
goto continue
end
if fileinfo.delete == true then
handler.delete_file(fileinfo.path)
print("Deleted file: " .. fileinfo.path)
elseif fileinfo.content then
handler.write_file(fileinfo.path, fileinfo.content)
print("Wrote file: " .. fileinfo.path)
else
vim.api.nvim_err_writeln("Invalid file entry. Must have 'path' and 'content'.")
vim.api.nvim_err_writeln("Invalid file entry. Must have 'content' or 'delete' set to true.")
end
::continue::
end
end