From 75cfaf54a4b5468555603ba2bc8a7e823683806d Mon Sep 17 00:00:00 2001 From: aleidk Date: Mon, 25 Nov 2024 16:23:52 -0300 Subject: [PATCH] create custom prompt for AI chat --- config/nvim/lua/aleidk/plugins-ide/ai.lua | 149 ++++++++++++++-------- 1 file changed, 99 insertions(+), 50 deletions(-) diff --git a/config/nvim/lua/aleidk/plugins-ide/ai.lua b/config/nvim/lua/aleidk/plugins-ide/ai.lua index aa9a4c1..a4f2f00 100644 --- a/config/nvim/lua/aleidk/plugins-ide/ai.lua +++ b/config/nvim/lua/aleidk/plugins-ide/ai.lua @@ -24,52 +24,76 @@ return { end, }, }, - opts = function() - local user = vim.env.USER or "User" - user = user:sub(1, 1):upper() .. user:sub(2) + opts = + ---@type CopilotChat.config + { + ---@type table + prompts = { + HowDoI = { + system_prompt = [[ + > /COPILOT_EXPLAIN - return { - prompts = { - Explain = { - prompt = '> /COPILOT_EXPLAIN\n\nWrite an explanation for the selected code as paragraphs of text.', - }, - Review = { - prompt = '> /COPILOT_REVIEW\n\nReview the selected code.', - -- see config.lua for implementation - }, - Fix = { - prompt = - '> /COPILOT_GENERATE\n\nThere is a problem in this code. Rewrite the code to show it with the bug fixed.', - }, - Optimize = { - prompt = '> /COPILOT_GENERATE\n\nOptimize the selected code to improve performance and readability.', - }, - Docs = { - prompt = '> /COPILOT_GENERATE\n\nPlease add documentation comments to the selected code.', - }, - Tests = { - prompt = '> /COPILOT_GENERATE\n\nPlease generate tests for my code.', - }, - Commit = { - prompt = - '> #git:staged\n\nWrite commit message for the change with commitizen convention. Make sure the title has maximum 50 characters and message is wrapped at 72 characters. Wrap the whole message in code block with language gitcommit.', - }, - }, - question_header = " " .. user .. " ", - answer_header = " Copilot ", - window = { - layout = 'float', -- 'vertical', 'horizontal', 'float', 'replace' - width = 0.8, -- fractional width of parent, or absolute width in columns when > 1 - height = 0.8, -- fractional height of parent, or absolute height in rows when > 1 - -- Options below only apply to floating windows - relative = 'editor', -- 'editor', 'win', 'cursor', 'mouse' - border = 'rounded', -- 'none', single', 'double', 'rounded', 'solid', 'shadow' - title = ' ', -- title of chat window - zindex = 1, -- determines if window is on top or below other floating windows - }, - } - end, + You are gonna be asked for guidance about how to solve a problem or archeive a goal. For that your answers need to follow these rules: + 1. Provide brief, consice and unambiguos responses. + 2. You need to response with an step by step list of actions needed to complete the task. + 3. **You never have to give an explicit answer**, instead provide a high level overview of how to complete the task. + 4. Unless explicity asked by the user, **you never have to provide code snippets** in your responses. Only provide code snippets when the user says so in each question. + 5. If the user ask for a code snippet, **prefer to respond in pseudo code**, unless the user explicity asked for a concrete language. + 6. You need to have into consideration good and clean code practices, code performance and other standards, comunicate in your response this considerations and common pitfalls to avoid. + 7. If possible, include how the user can test the task is completed successfully with the title "## Testing". + 8. Use markdawn syntax highlighting when possible. + 9. You are allowed to reference the following elements but without giving examples of usage (unless the user ask for it): + - Elements from the language syntaxis. + - Standard library of the lenguage. + - External libraries and/or frameworks. + - The idiomatic way of acomplish something in the language. + - Data structures and algorithms that could help complete the task. + + If you have multiple aproaches of how the task can be solved, insert the title "## Options", then generate separate list of task, each in the form of "### Option A", "### Option B", etc so he/her can have it into consideration. + The numbering of each list needs to be independen of each other, also provide only 1 list per aproach. + + The user can ask follow up question, which needs to follow the same rules. + ]] + }, + Explain = { + prompt = '> /COPILOT_EXPLAIN\n\nWrite an explanation for the selected code as paragraphs of text.', + }, + Review = { + prompt = '> /COPILOT_REVIEW\n\nReview the selected code.', + -- see config.lua for implementation + }, + Fix = { + prompt = + '> /COPILOT_GENERATE\n\nThere is a problem in this code. Rewrite the code to show it with the bug fixed.', + }, + Optimize = { + prompt = '> /COPILOT_GENERATE\n\nOptimize the selected code to improve performance and readability.', + }, + Docs = { + prompt = '> /COPILOT_GENERATE\n\nPlease add documentation comments to the selected code.', + }, + Tests = { + prompt = '> /COPILOT_GENERATE\n\nPlease generate tests for my code.', + }, + Commit = { + prompt = + '> #git:staged\n\nWrite commit message for the change with commitizen convention. Make sure the title has maximum 50 characters and message is wrapped at 72 characters. Wrap the whole message in code block with language gitcommit.', + }, + }, + question_header = " " .. vim.env.USER or "User" .. " ", + answer_header = " Copilot ", + window = { + layout = 'float', -- 'vertical', 'horizontal', 'float', 'replace' + width = 0.8, -- fractional width of parent, or absolute width in columns when > 1 + height = 0.8, -- fractional height of parent, or absolute height in rows when > 1 + -- Options below only apply to floating windows + relative = 'editor', -- 'editor', 'win', 'cursor', 'mouse' + border = 'rounded', -- 'none', single', 'double', 'rounded', 'solid', 'shadow' + title = ' ', -- title of chat window + zindex = 1, -- determines if window is on top or below other floating windows + }, + }, keys = { { "at", @@ -82,14 +106,31 @@ return { { "aa", function() - local input = vim.fn.input("Quick Chat: ") - if input ~= "" then - require("CopilotChat").ask(input) + local input = vim.fn.input(" Quick Chat: ") + if input == "" then + return end + require("CopilotChat").ask(input) end, desc = "Quick chat", mode = { "n", "v" }, }, + { + "ah", + function() + local input = vim.fn.input(" How do I...") + if input == "" then + return + end + + local chat = require("CopilotChat") + + local promptConfig = chat.prompts()["HowDoI"] + chat.ask(input, promptConfig) + end, + desc = "How do I...", + mode = { "n", "v" }, + }, { "aA", function() @@ -97,9 +138,10 @@ return { local actions = require("CopilotChat.actions") -- Pick prompt actions - actions.pick(actions.prompt_actions({ - selection = require("CopilotChat.select").visual, - })) + actions.pick(actions.prompt_actions({}), { + prompt = " Prompts:", + } + ) end, desc = "Select action", mode = { "n", "v" }, @@ -112,5 +154,12 @@ return { desc = "Clear chat", mode = { "n", "v" }, }, + { + "al", + function() + return require("CopilotChat").log_level("debug") + end, + desc = "Set debug level", + }, } }