Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ Define your own prompts in the configuration:
system_prompt = 'You are fascinated by pirates, so please respond in pirate speak.',
},
NiceInstructions = {
system_prompt = 'You are a nice coding tutor, so please respond in a friendly and helpful manner.' .. require('CopilotChat.config.prompts').COPILOT_BASE.system_prompt,
system_prompt = 'You are a nice coding tutor, so please respond in a friendly and helpful manner. {BASE_INSTRUCTIONS}',
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion lua/CopilotChat/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
---@field language string?
---@field temperature number?
---@field headless boolean?
---@field callback nil|fun(response: string, source: CopilotChat.source)
---@field callback nil|fun(response: CopilotChat.client.Message, source: CopilotChat.source)
---@field remember_as_sticky boolean?
---@field selection false|nil|fun(source: CopilotChat.source):CopilotChat.select.Selection?
---@field window CopilotChat.config.Window?
Expand Down
64 changes: 30 additions & 34 deletions lua/CopilotChat/config/prompts.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
local COPILOT_BASE = [[
---@class CopilotChat.config.prompts.Prompt : CopilotChat.config.Shared
---@field prompt string?
---@field description string?
---@field mapping string?

---@type table<string, CopilotChat.config.prompts.Prompt>
return {
COPILOT_BASE = {
system_prompt = [[
When asked for your name, you must respond with "GitHub Copilot".
Follow the user's requirements carefully & to the letter.
Keep your answers short and impersonal.
Expand Down Expand Up @@ -72,15 +80,20 @@ When presenting code changes:
4. Address any diagnostics issues when fixing code.
5. If multiple changes are needed, present them as separate code blocks.
</editFileInstructions>
]]
]],
},

local COPILOT_INSTRUCTIONS = [[
COPILOT_INSTRUCTIONS = {
system_prompt = [[
You are a code-focused AI programming assistant that specializes in practical software engineering solutions.
]] .. COPILOT_BASE

local COPILOT_EXPLAIN = [[
{COPILOT_BASE}
]],
},

COPILOT_EXPLAIN = {
system_prompt = [[
You are a programming instructor focused on clear, practical explanations.
]] .. COPILOT_BASE .. [[

When explaining code:
- Provide concise high-level overview first
Expand All @@ -90,11 +103,16 @@ When explaining code:
- Focus on complex parts rather than basic syntax
- Use short paragraphs with clear structure
- Mention performance considerations where relevant
]]

local COPILOT_REVIEW = [[
{COPILOT_BASE}
]],
},

COPILOT_REVIEW = {
system_prompt = [[
You are a code reviewer focused on improving code quality and maintainability.
]] .. COPILOT_BASE .. [[

{COPILOT_BASE}

Format each issue you find precisely as:
line=<line_number>: <issue_description>
Expand All @@ -117,39 +135,17 @@ Multiple issues on one line should be separated by semicolons.
End with: "**`To clear buffer highlights, please ask a different question.`**"

If no issues found, confirm the code is well-written and explain why.
]]

---@class CopilotChat.config.prompts.Prompt : CopilotChat.config.Shared
---@field prompt string?
---@field description string?
---@field mapping string?

---@type table<string, CopilotChat.config.prompts.Prompt>
return {
COPILOT_BASE = {
system_prompt = COPILOT_BASE,
},

COPILOT_INSTRUCTIONS = {
system_prompt = COPILOT_INSTRUCTIONS,
},

COPILOT_EXPLAIN = {
system_prompt = COPILOT_EXPLAIN,
},

COPILOT_REVIEW = {
system_prompt = COPILOT_REVIEW,
]],
},

Explain = {
prompt = 'Write an explanation for the selected code as paragraphs of text.',
system_prompt = 'COPILOT_EXPLAIN',
system_prompt = '{COPILOT_EXPLAIN}',
},

Review = {
prompt = 'Review the selected code.',
system_prompt = 'COPILOT_REVIEW',
system_prompt = '{COPILOT_REVIEW}',
callback = function(response, source)
local diagnostics = {}
for line in response:gmatch('[^\r\n]+') do
Expand Down
15 changes: 7 additions & 8 deletions lua/CopilotChat/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,6 @@ local function list_prompts()
}
end

if val.system_prompt and M.config.prompts[val.system_prompt] then
val.system_prompt = M.config.prompts[val.system_prompt].system_prompt
end

prompts_to_use[name] = val
end

Expand Down Expand Up @@ -506,11 +502,14 @@ function M.resolve_prompt(prompt, config)

config = vim.tbl_deep_extend('force', M.config, config or {})
config, prompt = resolve(config, prompt or '')
if prompts_to_use[config.system_prompt] then
config.system_prompt = prompts_to_use[config.system_prompt].system_prompt
end

if config.system_prompt then
for name, prompt in pairs(prompts_to_use) do
if prompt.system_prompt then
config.system_prompt = config.system_prompt:gsub('{' .. name .. '}', prompt.system_prompt)
end
end

config.system_prompt = config.system_prompt:gsub('{OS_NAME}', jit.os)
config.system_prompt = config.system_prompt:gsub('{LANGUAGE}', config.language)
if state.source then
Expand Down Expand Up @@ -894,7 +893,7 @@ function M.ask(prompt, config)
-- Call the callback function
if config.callback then
utils.schedule_main()
config.callback(response.content, state.source)
config.callback(response, state.source)
end

if not config.headless then
Expand Down
Loading