From 9671170d8390808924c7ddfbdaf3d15e1b9514f3 Mon Sep 17 00:00:00 2001 From: Mihamina RKTMB Date: Tue, 12 Aug 2025 10:55:01 +0300 Subject: [PATCH 1/3] use a function to dynamically look for sytem instructions --- lua/CopilotChat/config.lua | 3 ++- lua/CopilotChat/init.lua | 25 +++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/lua/CopilotChat/config.lua b/lua/CopilotChat/config.lua index 48f5e96a..062215d1 100644 --- a/lua/CopilotChat/config.lua +++ b/lua/CopilotChat/config.lua @@ -14,7 +14,7 @@ ---@field blend number? ---@class CopilotChat.config.Shared ----@field system_prompt string? +---@field system_prompt string|fun(source: CopilotChat.source):string|nil ---@field model string? ---@field tools string|table|nil ---@field sticky string|table|nil @@ -125,3 +125,4 @@ return { -- default mappings mappings = require('CopilotChat.config.mappings'), } + diff --git a/lua/CopilotChat/init.lua b/lua/CopilotChat/init.lua index c97359dd..90235a5e 100644 --- a/lua/CopilotChat/init.lua +++ b/lua/CopilotChat/init.lua @@ -193,6 +193,27 @@ local function list_prompts() return prompts_to_use end +--- Resolve system prompt - handle both string and function types +---@param system_prompt string|function|nil +---@param source CopilotChat.source? +---@return string? +local function resolve_system_prompt(system_prompt, source) + if not system_prompt then + return nil + end + + if type(system_prompt) == 'function' then + local ok, result = pcall(system_prompt, source) + if not ok then + log.warn('Failed to resolve system prompt function: ' .. result) + return nil + end + return result + end + + return system_prompt +end + --- Finish writing to chat buffer. ---@param start_of_chat boolean? local function finish(start_of_chat) @@ -510,6 +531,9 @@ function M.resolve_prompt(prompt, config) config.system_prompt = prompts_to_use[config.system_prompt].system_prompt end + -- Resolve system prompt (handle functions) + config.system_prompt = resolve_system_prompt(config.system_prompt, state.source) + if config.system_prompt then config.system_prompt = config.system_prompt:gsub('{OS_NAME}', jit.os) config.system_prompt = config.system_prompt:gsub('{LANGUAGE}', config.language) @@ -1126,3 +1150,4 @@ function M.setup(config) end return M + From 32c5cf25fde622522e83742bbc0655f1ac0dc961 Mon Sep 17 00:00:00 2001 From: Mihamina RKTMB Date: Tue, 12 Aug 2025 23:21:31 +0300 Subject: [PATCH 2/3] remove unused parameter --- lua/CopilotChat/init.lua | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lua/CopilotChat/init.lua b/lua/CopilotChat/init.lua index 90235a5e..795b68c8 100644 --- a/lua/CopilotChat/init.lua +++ b/lua/CopilotChat/init.lua @@ -195,15 +195,14 @@ end --- Resolve system prompt - handle both string and function types ---@param system_prompt string|function|nil ----@param source CopilotChat.source? ---@return string? -local function resolve_system_prompt(system_prompt, source) +local function resolve_system_prompt(system_prompt) if not system_prompt then return nil end if type(system_prompt) == 'function' then - local ok, result = pcall(system_prompt, source) + local ok, result = pcall(system_prompt) if not ok then log.warn('Failed to resolve system prompt function: ' .. result) return nil From 8384c5062fb10865dbd481f59bdab8918360985f Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 12 Aug 2025 20:51:38 +0000 Subject: [PATCH 3/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- lua/CopilotChat/config.lua | 1 - lua/CopilotChat/init.lua | 1 - 2 files changed, 2 deletions(-) diff --git a/lua/CopilotChat/config.lua b/lua/CopilotChat/config.lua index fe2f3ec2..1f35f33f 100644 --- a/lua/CopilotChat/config.lua +++ b/lua/CopilotChat/config.lua @@ -125,4 +125,3 @@ return { -- default mappings mappings = require('CopilotChat.config.mappings'), } - diff --git a/lua/CopilotChat/init.lua b/lua/CopilotChat/init.lua index 6b0a5fe1..8a660183 100644 --- a/lua/CopilotChat/init.lua +++ b/lua/CopilotChat/init.lua @@ -1148,4 +1148,3 @@ function M.setup(config) end return M -