Compare commits

...

2 Commits

Author SHA1 Message Date
a27e3da769 fix: escaping in replace_in_file tool 2025-01-31 17:45:30 +01:00
84e71dcbef fix: improve the basic prompt 2025-01-31 17:28:20 +01:00
3 changed files with 63 additions and 4 deletions

View File

@@ -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. - 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. - 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. - 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 files 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 havent 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"] = [[
### Secure Coding Guidelines ### Secure Coding Guidelines

View File

@@ -3,8 +3,11 @@ local robust_lsp = require("chatgpt_nvim.tools.lsp_robust_diagnostics")
local M = {} local M = {}
-- Enhanced search_and_replace to track if a string was found. We use Luas gsub return value -- Function to escape all Lua pattern magic characters:
-- (updatedString, replacementCount) to see if any replacements occurred. local function escape_lua_pattern(s)
return s:gsub("([%^%$%(%)%%%.%[%]%*%+%-%?])", "%%%1")
end
local function search_and_replace(original, replacements) local function search_and_replace(original, replacements)
local updated = original local updated = original
local info_msgs = {} local info_msgs = {}
@@ -13,8 +16,11 @@ local function search_and_replace(original, replacements)
local search_str = r.search or "" local search_str = r.search or ""
local replace_str = r.replace 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 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 the string was not found, append an info message
if replacement_count == 0 then 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 if not path or #replacements == 0 then
return "[replace_in_file] Missing 'path' or 'replacements'." return "[replace_in_file] Missing 'path' or 'replacements'."
end end
local root = vim.fn.getcwd()
local root = vim.fn.getcwd()
if not is_subpath(root, path) then if not is_subpath(root, path) then
return string.format("Tool [replace_in_file for '%s'] REJECTED. Path outside project root.", path) return string.format("Tool [replace_in_file for '%s'] REJECTED. Path outside project root.", path)
end end