Skip to content

Commit fc56770

Browse files
committed
feat: add diagnostic actions and modernize prompts for multi-provider support
- Add {{diagnostic}} and {{diagnostics}} template variables for actions - Add new actions: fix_diagnostic, explain_diagnostic, fix_diagnostics - Remove deprecated `stop` parameter from all actions (not supported by modern LLMs) - Remove `frequency_penalty` from keywords action - Rewrite prompts to explicitly request raw code output without markdown fences - Update README with new actions and template variable documentation
1 parent bd7d704 commit fc56770

4 files changed

Lines changed: 100 additions & 34 deletions

File tree

README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,9 @@ list. Available actions are:
294294
9. `explain_code`
295295
10. `roxygen_edit`
296296
11. `code_readability_analysis` -- see [demo](https://youtu.be/zlU3YGGv2zY)
297+
12. `fix_diagnostic` -- fix error under cursor
298+
13. `explain_diagnostic` -- explain error under cursor
299+
14. `fix_diagnostics` -- fix all errors in selection
297300

298301
All the above actions are using `gpt-5-mini` model.
299302

@@ -306,7 +309,7 @@ An example of custom action may look like this: (`#` marks comments)
306309
"action_name": {
307310
"type": "chat", # or "completion" or "edit"
308311
"opts": {
309-
"template": "A template using possible variable: {{filetype}} (neovim filetype), {{input}} (the selected text) an {{argument}} (provided on the command line), {{filepath}} (the relative path to the file)",
312+
"template": "A template using possible variables",
310313
"strategy": "replace", # or "display" or "append" or "edit"
311314
"params": { # parameters according to the official OpenAI API
312315
"model": "gpt-5-mini", # or any other model supported by `"type"` in the OpenAI API, use the playground for reference
@@ -326,6 +329,14 @@ An example of custom action may look like this: (`#` marks comments)
326329
}
327330
```
328331

332+
Available template variables:
333+
- `{{input}}` - the selected text
334+
- `{{filetype}}` - neovim filetype
335+
- `{{filepath}}` - relative path to the file
336+
- `{{argument}}` - provided on the command line
337+
- `{{diagnostic}}` - LSP diagnostic under cursor (format: `[SEVERITY] message (line N)`)
338+
- `{{diagnostics}}` - all LSP diagnostics in selection (format: `Line N [SEVERITY]: message`)
339+
329340
The `edit` strategy consists in showing the output side by side with the input and
330341
available for further editing requests.
331342
For now, `edit` strategy is implemented for `chat` type only.

lua/chatgpt/flows/actions/actions.json

Lines changed: 44 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,10 @@
22
"complete_code": {
33
"type": "chat",
44
"opts": {
5-
"template": "Complete the following code written in {{lang}} by pasting the existing code and continuing it.\n\nExisting code:\n```{{filetype}}\n{{input}}\n```\n\n```{{filetype}}\n",
5+
"template": "Complete the following {{filetype}} code by continuing it. Respond with the COMPLETE code (existing + new). No markdown fences, no explanations.\n\n{{input}}",
66
"strategy": "replace",
77
"params": {
8-
"model": "gpt-5-mini",
9-
"stop": [
10-
"```"
11-
]
8+
"model": "gpt-5-mini"
129
}
1310
}
1411
},
@@ -53,48 +50,37 @@
5350
"template": "Extract the main keywords from the following text.\n\n{{input}}",
5451
"strategy": "replace",
5552
"params": {
56-
"model": "gpt-5-mini",
57-
"temperature": 0.5,
58-
"frequency_penalty": 0.8
53+
"model": "gpt-5-mini"
5954
}
6055
}
6156
},
6257
"docstring": {
6358
"type": "chat",
6459
"opts": {
65-
"template": "# An elaborate, high quality docstring for the above function:\n# Writing a good docstring\n\nThis is an example of writing a really good docstring that follows a best practice for the given language. Attention is paid to detailing things like\n* parameter and return types (if applicable)\n* any errors that might be raised or returned, depending on the language\n\nI received the following code:\n\n```{{filetype}}\n{{input}}\n```\n\nThe code with a really good docstring added is below:\n\n```{{filetype}}",
60+
"template": "Add a high quality docstring to the following {{filetype}} code. Include parameter types, return types, and any errors that might be raised.\n\nCode:\n{{input}}\n\nRespond with ONLY the complete code including the docstring. No markdown fences, no explanations.",
6661
"strategy": "edit",
6762
"params": {
68-
"model": "gpt-5-mini",
69-
"stop": [
70-
"```"
71-
]
63+
"model": "gpt-5-mini"
7264
}
7365
}
7466
},
7567
"add_tests": {
7668
"type": "chat",
7769
"opts": {
78-
"template": "Implement tests for the following code.\n\nCode:\n```{{filetype}}\n{{input}}\n```\n\nTests:\n```{{filetype}}",
70+
"template": "Implement tests for the following {{filetype}} code.\n\nCode:\n{{input}}\n\nRespond with ONLY the test code. No markdown fences, no explanations.",
7971
"strategy": "append",
8072
"params": {
81-
"model": "gpt-5-mini",
82-
"stop": [
83-
"```"
84-
]
73+
"model": "gpt-5-mini"
8574
}
8675
}
8776
},
8877
"optimize_code": {
8978
"type": "chat",
9079
"opts": {
91-
"template": "Optimize the following code.\n\nCode:\n```{{filetype}}\n{{input}}\n```\n\nOptimized version:\n```{{filetype}}",
80+
"template": "Optimize the following {{filetype}} code for better performance and readability.\n\nCode:\n{{input}}\n\nRespond with ONLY the optimized code. No markdown fences, no explanations.",
9281
"strategy": "edit",
9382
"params": {
94-
"model": "gpt-5-mini",
95-
"stop": [
96-
"```"
97-
]
83+
"model": "gpt-5-mini"
9884
}
9985
}
10086
},
@@ -111,27 +97,21 @@
11197
"fix_bugs": {
11298
"type": "chat",
11399
"opts": {
114-
"template": "Fix bugs in the below code\n\nCode:\n```{{filetype}}\n{{input}}\n```\n\nFixed code:\n```{{filetype}}",
100+
"template": "Fix bugs in the following {{filetype}} code.\n\nCode:\n{{input}}\n\nRespond with ONLY the fixed code. No markdown fences, no explanations.",
115101
"strategy": "edit",
116102
"params": {
117-
"model": "gpt-5-mini",
118-
"stop": [
119-
"```"
120-
]
103+
"model": "gpt-5-mini"
121104
}
122105
}
123106
},
124107
"explain_code": {
125108
"type": "chat",
126109
"opts": {
127110
"title": " Explain Code",
128-
"template": "Explain the following code:\n\nCode:\n```{{filetype}}\n{{input}}\n```\n\nUse markdown format.\nHere's what the above code is doing:\n```",
111+
"template": "Explain the following {{filetype}} code. Use markdown format with clear sections.\n\nCode:\n{{input}}",
129112
"strategy": "display",
130113
"params": {
131-
"model": "gpt-5-mini",
132-
"stop": [
133-
"```"
134-
]
114+
"model": "gpt-5-mini"
135115
}
136116
}
137117
},
@@ -187,5 +167,36 @@
187167
]
188168
}
189169
}
170+
},
171+
"fix_diagnostic": {
172+
"type": "chat",
173+
"opts": {
174+
"template": "Fix the following error in the {{filetype}} code.\n\nError:\n{{diagnostic}}\n\nCode:\n{{input}}\n\nRespond with ONLY the fixed code. No markdown fences, no explanations.",
175+
"strategy": "edit",
176+
"params": {
177+
"model": "gpt-5-mini"
178+
}
179+
}
180+
},
181+
"explain_diagnostic": {
182+
"type": "chat",
183+
"opts": {
184+
"title": " Explain Error",
185+
"template": "Explain the following error and how to fix it. Use markdown format.\n\nError:\n{{diagnostic}}\n\nCode:\n{{input}}",
186+
"strategy": "display",
187+
"params": {
188+
"model": "gpt-5-mini"
189+
}
190+
}
191+
},
192+
"fix_diagnostics": {
193+
"type": "chat",
194+
"opts": {
195+
"template": "Fix all the following errors in the {{filetype}} code.\n\nErrors:\n{{diagnostics}}\n\nCode:\n{{input}}\n\nRespond with ONLY the fixed code. No markdown fences, no explanations.",
196+
"strategy": "edit",
197+
"params": {
198+
"model": "gpt-5-mini"
199+
}
200+
}
190201
}
191202
}

lua/chatgpt/flows/actions/base.lua

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,48 @@ function BaseAction:get_selected_text_with_line_numbers()
7373
return table.concat(lines_with_numbers, "\n")
7474
end
7575

76+
function BaseAction:get_diagnostic()
77+
local bufnr = self:get_bufnr()
78+
local cursor = vim.api.nvim_win_get_cursor(0)
79+
local row = cursor[1] - 1
80+
81+
local diagnostics = vim.diagnostic.get(bufnr, { lnum = row })
82+
if #diagnostics == 0 then
83+
return ""
84+
end
85+
86+
local diag = diagnostics[1]
87+
local severity = vim.diagnostic.severity[diag.severity] or "UNKNOWN"
88+
return string.format("[%s] %s (line %d)", severity, diag.message, diag.lnum + 1)
89+
end
90+
91+
function BaseAction:get_diagnostics()
92+
local bufnr = self:get_bufnr()
93+
local _, start_row, _, end_row, _ = self:get_visual_selection()
94+
95+
local diagnostics = vim.diagnostic.get(bufnr)
96+
local filtered = {}
97+
98+
for _, diag in ipairs(diagnostics) do
99+
local line = diag.lnum + 1
100+
if line >= start_row and line <= end_row then
101+
table.insert(filtered, diag)
102+
end
103+
end
104+
105+
if #filtered == 0 then
106+
return ""
107+
end
108+
109+
local result = {}
110+
for _, diag in ipairs(filtered) do
111+
local severity = vim.diagnostic.severity[diag.severity] or "UNKNOWN"
112+
table.insert(result, string.format("Line %d [%s]: %s", diag.lnum + 1, severity, diag.message))
113+
end
114+
115+
return table.concat(result, "\n")
116+
end
117+
76118
function BaseAction:mark_selection_with_signs()
77119
local bufnr = self:get_bufnr()
78120
local _, start_row, _, end_row, _ = self:get_visual_selection()

lua/chatgpt/flows/actions/chat/init.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ function ChatAction:render_template()
4646
filetype = self:get_filetype(),
4747
filepath = self:get_filepath(),
4848
input = input,
49+
diagnostic = self:get_diagnostic(),
50+
diagnostics = self:get_diagnostics(),
4951
}
5052
data = vim.tbl_extend("force", {}, data, self.variables)
5153
local result = self.template

0 commit comments

Comments
 (0)