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 lua/CopilotChat/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ return {

-- Shared config starts here (can be passed to functions at runtime and configured via setup function)

system_prompt = 'COPILOT_INSTRUCTIONS', -- System prompt to use (can be specified manually in prompt via /).
system_prompt = '{COPILOT_INSTRUCTIONS}', -- System prompt to use (can be specified manually in prompt via /).

model = 'gpt-4.1', -- Default model to use, see ':CopilotChatModels' for available models (can be specified manually in prompt via $).
tools = nil, -- Default tool or array of tools (or groups) to share with LLM (can be specified manually in prompt via @).
Expand Down
24 changes: 18 additions & 6 deletions lua/CopilotChat/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -520,19 +520,31 @@ function M.resolve_prompt(prompt, config)
return inner_config, inner_prompt
end

local function expand_system_prompt(system_prompt, prompts_to_use)
local prev, curr = nil, system_prompt
local depth = 0
repeat
prev = curr
curr = curr:gsub('{([%w_]+)}', function(name)
local prompt = prompts_to_use[name]
if prompt and prompt.system_prompt then
return prompt.system_prompt
end
return '{' .. name .. '}'
end)
depth = depth + 1
until prev == curr or depth >= MAX_DEPTH
return curr
end

config = vim.tbl_deep_extend('force', M.config, config or {})
config, prompt = resolve(config, prompt or '')

-- Resolve system prompt (handle functions)
config.system_prompt = resolve_system_prompt(config.system_prompt, state.source)

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 = expand_system_prompt(config.system_prompt, prompts_to_use)
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
Loading