feat: add project_name for safety, so you dont accidently insert into the wrong project

This commit is contained in:
2024-12-13 00:25:03 +01:00
parent 00a52998b8
commit 2eb79c2b1a
3 changed files with 18 additions and 3 deletions

View File

@@ -1,3 +1,4 @@
project_name: "chatgpt_nvim"
default_prompt_blocks: default_prompt_blocks:
- "basic-prompt" - "basic-prompt"
directories: directories:

View File

@@ -4,6 +4,7 @@
-- 2) If no file is open or not in Git repo, fallback to current working directory. -- 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. -- 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. -- 4) Add support for configuring a token limit.
-- 5) Load a project_name from the .chatgpt_config.yaml for project verification.
local M = {} local M = {}
local uv = vim.loop local uv = vim.loop
@@ -49,7 +50,7 @@ end
local prompt_blocks = { 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.", ["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.", ["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." ["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."
} }
function M.load() function M.load()
@@ -59,7 +60,8 @@ function M.load()
initial_prompt = "", initial_prompt = "",
directories = { "." }, directories = { "." },
default_prompt_blocks = {}, default_prompt_blocks = {},
token_limit = 128000 token_limit = 128000,
project_name = ""
} }
if fd then if fd then
@@ -81,11 +83,14 @@ function M.load()
if type(result.token_limit) == "number" then if type(result.token_limit) == "number" then
config.token_limit = result.token_limit config.token_limit = result.token_limit
end end
if type(result.project_name) == "string" then
config.project_name = result.project_name
end
end end
end end
else else
-- Default fallback configuration -- Default fallback configuration
config.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." config.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, 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 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 project_name: <project_name>\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."
end end
-- If default_prompt_blocks are specified, concatenate all matching prompts -- If default_prompt_blocks are specified, concatenate all matching prompts

View File

@@ -46,6 +46,7 @@ function M.run_chatgpt_command()
local sections = { local sections = {
conf.initial_prompt .. "\n" .. user_input, conf.initial_prompt .. "\n" .. user_input,
"\n\nProject name: " .. (conf.project_name or "") .. "\n",
"\n\nProject Structure:\n", "\n\nProject Structure:\n",
project_structure, project_structure,
"\n\nBelow are the files from the project, each preceded by its filename in backticks and enclosed in triple backticks.\n" "\n\nBelow are the files from the project, each preceded by its filename in backticks and enclosed in triple backticks.\n"
@@ -69,6 +70,7 @@ end
function M.run_chatgpt_paste_command() function M.run_chatgpt_paste_command()
print("Reading ChatGPT YAML response from clipboard...") print("Reading ChatGPT YAML response from clipboard...")
local conf = config.load()
local raw = handler.get_clipboard_content() local raw = handler.get_clipboard_content()
if raw == "" then if raw == "" then
vim.api.nvim_err_writeln("Clipboard is empty. Please copy the YAML response from ChatGPT first.") vim.api.nvim_err_writeln("Clipboard is empty. Please copy the YAML response from ChatGPT first.")
@@ -81,6 +83,13 @@ function M.run_chatgpt_paste_command()
return return
end end
if not data.project_name or data.project_name ~= conf.project_name then
vim.api.nvim_err_writeln("Project name mismatch. The provided changes are for project '" ..
(data.project_name or "unknown") .. "' but current project is '" ..
(conf.project_name or "unconfigured") .. "'. Aborting changes.")
return
end
for _, fileinfo in ipairs(data.files) do for _, fileinfo in ipairs(data.files) do
if fileinfo.path and fileinfo.content then if fileinfo.path and fileinfo.content then
handler.write_file(fileinfo.path, fileinfo.content) handler.write_file(fileinfo.path, fileinfo.content)