Skip to content

Commit 407068b

Browse files
author
Zhe Yu
committed
feat(nvim): allow customising the system prompt as a function
1 parent b517c76 commit 407068b

3 files changed

Lines changed: 32 additions & 7 deletions

File tree

lua/vectorcode/integrations/codecompanion/common.lua

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,6 @@ local vc_config = require("vectorcode.config")
55
local notify_opts = vc_config.notify_opts
66
local logger = vc_config.logger
77

8-
---@class VectorCode.CodeCompanion.SummariseOpts
9-
---@field enabled boolean|(fun(chat: CodeCompanion.Chat,results: VectorCode.QueryResult[]):boolean)|nil
10-
---@field adapter string|CodeCompanion.Adapter|nil
11-
---@field system_prompt string
12-
---When set to true, include the query messages so that the LLM may make task-related summarisations.
13-
---@field query_augmented boolean
14-
158
---@type VectorCode.CodeCompanion.QueryToolOpts
169
local default_query_options = {
1710
max_num = { chunk = -1, document = -1 },
@@ -75,6 +68,8 @@ General Guidelines:
7568
Information Source: There will be no extra information available to you. Provide the summary solely based on the provided XML objects.
7669
7770
Omit meaningless results: For an xml object that contains no meaningful information, you're free to omit it, but please leave a sentence in the summary saying that you did this.
71+
72+
No extra reply: Your reply should solely consist of the summary. Do not say anything else.
7873
]],
7974
},
8075
}

lua/vectorcode/integrations/codecompanion/query_tool.lua

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,23 @@ local function generate_summary(result, summarise_opts, cmd, callback)
113113
return cc_common.process_result(res)
114114
end)
115115
:totable())
116+
116117
if summarise_opts.enabled and type(callback) == "function" then
117118
---@type CodeCompanion.Adapter
118119
local adapter =
119120
vim.deepcopy(require("codecompanion.adapters").resolve(summarise_opts.adapter))
120121

121122
local system_prompt = summarise_opts.system_prompt
123+
if type(system_prompt) == "function" then
124+
system_prompt = system_prompt(
125+
cc_common.get_query_tool_opts().summarise.system_prompt --[[@as string]]
126+
)
127+
end
128+
129+
assert(
130+
type(system_prompt) == "string",
131+
"`system_prompt` should have been converted to a string."
132+
)
122133
if summarise_opts.query_augmented then
123134
system_prompt = string.format(
124135
[[%s

lua/vectorcode/types.lua

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,22 @@
113113
---@field raw_results VectorCode.QueryResult[]
114114
---@field count integer
115115
---@field summary string|nil
116+
117+
---@class VectorCode.CodeCompanion.SummariseOpts
118+
---A boolean flag that controls whether summarisation should be enabled.
119+
---This can also be a function that returns a boolean.
120+
---In this case, you can use this option to dynamically control whether summarisation is enabled during a chat.
121+
---
122+
---This function recieves 2 parameters:
123+
--- - `CodeCompanion.Chat`: the chat object;
124+
--- - `VectorCode.QueryResult[]`: a list of query results.
125+
---@field enabled boolean|(fun(chat: CodeCompanion.Chat, results: VectorCode.QueryResult[]):boolean)|nil
126+
---The adapter used for the summarisation task. When set to `nil`, the adapter from the current chat will be used.
127+
---@field adapter string|CodeCompanion.Adapter|nil
128+
---The system prompt sent to the summariser model.
129+
---When set to a function, it'll recieve the default system prompt as the only parameter,
130+
---and should return the new (full) system prompt. This allows you to customise or rewrite the system prompt.
131+
---@field system_prompt string|(fun(original_prompt: string): string)
132+
---When set to true, include the query messages so that the LLM may make task-related summarisations.
133+
---This happens __after__ the `system_prompt` callback processing
134+
---@field query_augmented boolean

0 commit comments

Comments
 (0)