diff --git a/.chatgpt_config.yaml b/chatgpt_config.yaml similarity index 100% rename from .chatgpt_config.yaml rename to chatgpt_config.yaml diff --git a/lua/chatgpt_nvim/prompts.lua b/lua/chatgpt_nvim/prompts.lua index 3bc0a82..3b6ad1d 100644 --- a/lua/chatgpt_nvim/prompts.lua +++ b/lua/chatgpt_nvim/prompts.lua @@ -292,6 +292,59 @@ local M = { - Never do write operations on a file which you have not read before. Its contents must be in your context before writing. This does not apply if you create a new file. - The plugin will verify the `project_name` is correct before running any tools. - If the response grows too large, I'll guide you to break it into smaller steps. + + You are assisting me in a coding workflow for a project (e.g., "my_project"). + + 1. **Gather Context / Ask Questions** + - If you need more information or something is unclear, **ask me directly** in plain text, without calling any tools. + + 2. **Inspect Files** + - When you need to check a file’s content, use: + ```yaml + project_name: "my_project" + tools: + - tool: "readFile" + path: "relative/path/to/file" + ``` + - Read the file before deciding on any modifications. + + 3. **Make Changes** + - If you need to modify an existing file (after reading it), use: + ```yaml + project_name: "my_project" + tools: + - tool: "editFile" + path: "relative/path/to/file" + content: | + # Full updated file content + ``` + - Or perform incremental text replacements with: + ```yaml + project_name: "my_project" + tools: + - tool: "replace_in_file" + path: "relative/path/to/file" + replacements: + - search: "old text" + replace: "new text" + ``` + + 4. **Run Commands (Optional)** + - To run tests, list files, or do other checks, use: + ```yaml + project_name: "my_project" + tools: + - tool: "executeCommand" + command: "shell command here" + ``` + + 5. **Important Rules** + - Always start with 1 or 2, but afterwards you can mix 1, 2, 3, and 4 as needed. + - Include `project_name: "my_project"` whenever you call `tools`. + - Keep each tool call in the `tools` array (multiple if needed). + - **Never write to a file you haven’t read** in this session (unless creating a new file). + - Follow secure coding guidelines (input validation, least privilege, no sensitive info in logs, etc.). + - When done, provide a final answer **without** calling any tools. ]], ["secure-coding"] = [[ ### Secure Coding Guidelines diff --git a/lua/chatgpt_nvim/tools/replace_in_file.lua b/lua/chatgpt_nvim/tools/replace_in_file.lua index c49fa46..6a9f6d7 100644 --- a/lua/chatgpt_nvim/tools/replace_in_file.lua +++ b/lua/chatgpt_nvim/tools/replace_in_file.lua @@ -3,8 +3,11 @@ local robust_lsp = require("chatgpt_nvim.tools.lsp_robust_diagnostics") local M = {} --- Enhanced search_and_replace to track if a string was found. We use Lua’s gsub return value --- (updatedString, replacementCount) to see if any replacements occurred. +-- Function to escape all Lua pattern magic characters: +local function escape_lua_pattern(s) + return s:gsub("([%^%$%(%)%%%.%[%]%*%+%-%?])", "%%%1") +end + local function search_and_replace(original, replacements) local updated = original local info_msgs = {} @@ -13,8 +16,11 @@ local function search_and_replace(original, replacements) local search_str = r.search or "" local replace_str = r.replace or "" + -- Escape special pattern chars to ensure literal matching: + local escaped_search = escape_lua_pattern(search_str) + local replacement_count = 0 - updated, replacement_count = updated:gsub(search_str, replace_str) + updated, replacement_count = updated:gsub(escaped_search, replace_str) -- If the string was not found, append an info message if replacement_count == 0 then @@ -41,8 +47,8 @@ M.run = function(tool_call, conf, prompt_user_tool_accept, is_subpath, read_file if not path or #replacements == 0 then return "[replace_in_file] Missing 'path' or 'replacements'." end - local root = vim.fn.getcwd() + local root = vim.fn.getcwd() if not is_subpath(root, path) then return string.format("Tool [replace_in_file for '%s'] REJECTED. Path outside project root.", path) end