Skip to content

Commit cb8fb0f

Browse files
authored
refactor(chat)!: move prompt resolution helpers to prompts module (#1470)
Moves the prompt resolution functions (`resolve_prompt`, `resolve_tools`, `resolve_model`, and `resolve_functions`) from the main CopilotChat module to the `CopilotChat.prompts` module. Updates all references and documentation to use the new location. This improves code organization by separating prompt parsing logic from the main chat interface. BREAKING CHANGE: The prompt resolution functions have been moved to `require('CopilotChat.prompts')`
1 parent 5d46a69 commit cb8fb0f

File tree

3 files changed

+62
-72
lines changed

3 files changed

+62
-72
lines changed

README.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -397,9 +397,6 @@ local chat = require("CopilotChat")
397397
-- Basic Chat Functions
398398
chat.ask(prompt, config) -- Ask a question with optional config
399399
chat.response() -- Get the last response text
400-
chat.resolve_prompt() -- Resolve prompt references
401-
chat.resolve_tools() -- Resolve tools that are available for automatic use by LLM
402-
chat.resolve_model() -- Resolve model from prompt (WARN: async, requires plenary.async.run)
403400

404401
-- Window Management
405402
chat.open(config) -- Open chat window with optional config
@@ -456,6 +453,16 @@ window:focus() -- Focus the chat window
456453
window:overlay(opts) -- Show overlay with specified options
457454
```
458455

456+
## Prompt parser
457+
458+
```lua
459+
local parser = require("CopilotChat.prompts")
460+
461+
parser.resolve_prompt() -- Resolve prompt references
462+
parser.resolve_tools() -- Resolve tools that are available for automatic use by LLM
463+
parser.resolve_model() -- Resolve model from prompt (WARN: async, requires plenary.async.run)
464+
```
465+
459466
## Example Usage
460467

461468
```lua

lua/CopilotChat/config/mappings.lua

Lines changed: 48 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
local async = require('plenary.async')
2-
local copilot = require('CopilotChat')
32
local client = require('CopilotChat.client')
43
local constants = require('CopilotChat.constants')
54
local select = require('CopilotChat.select')
65
local utils = require('CopilotChat.utils')
7-
local diff = require('CopilotChat.utils.diff')
86
local files = require('CopilotChat.utils.files')
97

108
--- Prepare a buffer for applying a diff
@@ -77,22 +75,23 @@ return {
7775
normal = 'q',
7876
insert = '<C-c>',
7977
callback = function()
80-
copilot.close()
78+
require('CopilotChat').close()
8179
end,
8280
},
8381

8482
reset = {
8583
normal = '<C-l>',
8684
insert = '<C-l>',
8785
callback = function()
88-
copilot.reset()
86+
require('CopilotChat').reset()
8987
end,
9088
},
9189

9290
submit_prompt = {
9391
normal = '<CR>',
9492
insert = '<C-s>',
9593
callback = function()
94+
local copilot = require('CopilotChat')
9695
local message = copilot.chat:get_message(constants.ROLE.USER, true)
9796
if not message then
9897
return
@@ -106,7 +105,10 @@ return {
106105
normal = '<C-y>',
107106
insert = '<C-y>',
108107
callback = function(source)
109-
local block = copilot.chat:get_block(constants.ROLE.ASSISTANT, true)
108+
local chat = require('CopilotChat').chat
109+
local diff = require('CopilotChat.utils.diff')
110+
111+
local block = chat:get_block(constants.ROLE.ASSISTANT, true)
110112
if not block then
111113
return
112114
end
@@ -127,7 +129,10 @@ return {
127129
jump_to_diff = {
128130
normal = 'gj',
129131
callback = function(source)
130-
local block = copilot.chat:get_block(constants.ROLE.ASSISTANT, true)
132+
local chat = require('CopilotChat').chat
133+
local diff = require('CopilotChat.utils.diff')
134+
135+
local block = chat:get_block(constants.ROLE.ASSISTANT, true)
131136
if not block then
132137
return
133138
end
@@ -147,19 +152,24 @@ return {
147152
normal = 'gy',
148153
register = '"', -- Default register to use for yanking
149154
callback = function()
150-
local block = copilot.chat:get_block(constants.ROLE.ASSISTANT, true)
155+
local config = require('CopilotChat.config')
156+
local chat = require('CopilotChat').chat
157+
local block = chat:get_block(constants.ROLE.ASSISTANT, true)
151158
if not block then
152159
return
153160
end
154161

155-
vim.fn.setreg(copilot.config.mappings.yank_diff.register, block.content)
162+
vim.fn.setreg(config.mappings.yank_diff.register, block.content)
156163
end,
157164
},
158165

159166
show_diff = {
160167
normal = 'gd',
161168
callback = function(source)
162-
local block = copilot.chat:get_block(constants.ROLE.ASSISTANT, true)
169+
local chat = require('CopilotChat').chat
170+
local diff = require('CopilotChat.utils.diff')
171+
172+
local block = chat:get_block(constants.ROLE.ASSISTANT, true)
163173
if not block then
164174
return
165175
end
@@ -168,7 +178,7 @@ return {
168178
local bufnr = prepare_diff_buffer(path, source)
169179

170180
-- Collect all blocks for the same filename
171-
local message = copilot.chat:get_message(constants.ROLE.ASSISTANT, true)
181+
local message = chat:get_message(constants.ROLE.ASSISTANT, true)
172182
local blocks = {}
173183
if message and message.section and message.section.blocks then
174184
for _, b in ipairs(message.section.blocks) do
@@ -196,26 +206,27 @@ return {
196206
vim.cmd('diffthis')
197207
end)
198208

199-
vim.api.nvim_win_call(copilot.chat.winnr, function()
209+
vim.api.nvim_win_call(chat.winnr, function()
200210
vim.cmd('diffthis')
201211
end)
202212
end
203213

204214
opts.on_hide = function()
205-
vim.api.nvim_win_call(copilot.chat.winnr, function()
215+
vim.api.nvim_win_call(chat.winnr, function()
206216
vim.cmd('diffoff')
207217
end)
208218
end
209219

210-
copilot.chat:overlay(opts)
220+
chat:overlay(opts)
211221
end,
212222
},
213223

214224
quickfix_diffs = {
215225
normal = 'gqd',
216226
callback = function()
227+
local chat = require('CopilotChat').chat
217228
local items = {}
218-
local messages = copilot.chat:get_messages()
229+
local messages = chat:get_messages()
219230
for _, message in ipairs(messages) do
220231
if message.section then
221232
for _, block in ipairs(message.section.blocks) do
@@ -225,7 +236,7 @@ return {
225236
end
226237

227238
table.insert(items, {
228-
bufnr = copilot.chat.bufnr,
239+
bufnr = chat.bufnr,
229240
lnum = block.start_line,
230241
end_lnum = block.end_line,
231242
text = text,
@@ -242,8 +253,9 @@ return {
242253
quickfix_answers = {
243254
normal = 'gqa',
244255
callback = function()
256+
local chat = require('CopilotChat').chat
245257
local items = {}
246-
local messages = copilot.chat:get_messages()
258+
local messages = chat:get_messages()
247259
for i, message in ipairs(messages) do
248260
if message.section and message.role == constants.ROLE.ASSISTANT then
249261
local prev_message = messages[i - 1]
@@ -253,7 +265,7 @@ return {
253265
end
254266

255267
table.insert(items, {
256-
bufnr = copilot.chat.bufnr,
268+
bufnr = chat.bufnr,
257269
lnum = message.section.start_line,
258270
end_lnum = message.section.end_line,
259271
text = text,
@@ -269,28 +281,31 @@ return {
269281
show_info = {
270282
normal = 'gc',
271283
callback = function(source)
272-
local message = copilot.chat:get_message(constants.ROLE.USER, true)
284+
local chat = require('CopilotChat').chat
285+
local prompts = require('CopilotChat.prompts')
286+
287+
local message = chat:get_message(constants.ROLE.USER, true)
273288
if not message then
274289
return
275290
end
276291

277292
local lines = {}
278293

279294
async.run(function()
280-
local config, prompt = copilot.resolve_prompt(message.content)
295+
local config, prompt = prompts.resolve_prompt(message.content)
281296
local system_prompt = config.system_prompt
282-
local resolved_resources = copilot.resolve_functions(prompt, config)
283-
local selected_tools = copilot.resolve_tools(prompt, config)
284-
local selected_model = copilot.resolve_model(prompt, config)
297+
local resolved_resources = prompts.resolve_functions(prompt, config)
298+
local selected_tools = prompts.resolve_tools(prompt, config)
299+
local selected_model = prompts.resolve_model(prompt, config)
285300
local infos = client:info()
286301

287302
selected_tools = vim.tbl_map(function(tool)
288303
return tool.name
289304
end, selected_tools)
290305

291306
utils.schedule_main()
292-
table.insert(lines, '**Logs**: `' .. copilot.config.log_path .. '`')
293-
table.insert(lines, '**History**: `' .. copilot.config.history_path .. '`')
307+
table.insert(lines, '**Logs**: `' .. config.log_path .. '`')
308+
table.insert(lines, '**History**: `' .. config.history_path .. '`')
294309
table.insert(lines, '')
295310

296311
for provider, infolines in pairs(infos) do
@@ -368,7 +383,7 @@ return {
368383
table.insert(lines, '')
369384
end
370385

371-
copilot.chat:overlay({
386+
chat:overlay({
372387
text = vim.trim(table.concat(lines, '\n')) .. '\n',
373388
})
374389
end)
@@ -378,6 +393,9 @@ return {
378393
show_help = {
379394
normal = 'gh',
380395
callback = function()
396+
local config = require('CopilotChat.config')
397+
local chat = require('CopilotChat').chat
398+
381399
local chat_help = '**`Special tokens`**\n'
382400
chat_help = chat_help .. '`@<function>` to share function\n'
383401
chat_help = chat_help .. '`#<function>` to add resource\n'
@@ -387,22 +405,22 @@ return {
387405
chat_help = chat_help .. '`> <text>` to make a sticky prompt (copied to next prompt)\n'
388406

389407
chat_help = chat_help .. '\n**`Mappings`**\n'
390-
local chat_keys = vim.tbl_keys(copilot.config.mappings)
408+
local chat_keys = vim.tbl_keys(config.mappings)
391409
table.sort(chat_keys, function(a, b)
392-
a = copilot.config.mappings[a]
410+
a = config.mappings[a]
393411
a = a and (a.normal or a.insert) or ''
394-
b = copilot.config.mappings[b]
412+
b = config.mappings[b]
395413
b = b and (b.normal or b.insert) or ''
396414
return a < b
397415
end)
398416
for _, name in ipairs(chat_keys) do
399-
local info = utils.key_to_info(name, copilot.config.mappings[name], '`')
417+
local info = utils.key_to_info(name, config.mappings[name], '`')
400418
if info ~= '' then
401419
chat_help = chat_help .. info .. '\n'
402420
end
403421
end
404422

405-
copilot.chat:overlay({
423+
chat:overlay({
406424
text = chat_help,
407425
})
408426
end,

lua/CopilotChat/init.lua

Lines changed: 4 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -230,41 +230,6 @@ local function update_source()
230230
M.chat:set_source(use_prev_window and vim.fn.win_getid(vim.fn.winnr('#')) or vim.api.nvim_get_current_win())
231231
end
232232

233-
--- Resolve enabled tools from the prompt.
234-
---@param prompt string?
235-
---@param config CopilotChat.config.Shared?
236-
---@return table<CopilotChat.client.Tool>, string
237-
function M.resolve_tools(prompt, config)
238-
return prompts.resolve_tools(prompt, config)
239-
end
240-
241-
--- Call and resolve function calls from the prompt.
242-
---@param prompt string?
243-
---@param config CopilotChat.config.Shared?
244-
---@return table<CopilotChat.client.Resource>, table<string>, table<string>, string
245-
---@async
246-
function M.resolve_functions(prompt, config)
247-
return prompts.resolve_functions(prompt, config)
248-
end
249-
250-
--- Resolve the final prompt and config from prompt template.
251-
---@param prompt string?
252-
---@param config CopilotChat.config.Shared?
253-
---@return CopilotChat.config.prompts.Prompt, string
254-
---@async
255-
function M.resolve_prompt(prompt, config)
256-
return prompts.resolve_prompt(prompt, config)
257-
end
258-
259-
--- Resolve the model from the prompt.
260-
---@param prompt string?
261-
---@param config CopilotChat.config.Shared?
262-
---@return string, string
263-
---@async
264-
function M.resolve_model(prompt, config)
265-
return prompts.resolve_model(prompt, config)
266-
end
267-
268233
--- Open the chat window.
269234
---@param config CopilotChat.config.Shared?
270235
function M.open(config)
@@ -449,11 +414,11 @@ function M.ask(prompt, config)
449414
end
450415

451416
async.run(handle_error(config, function()
452-
config, prompt = M.resolve_prompt(prompt, config)
417+
config, prompt = prompts.resolve_prompt(prompt, config)
453418
local system_prompt = config.system_prompt or ''
454-
local selected_tools, prompt = M.resolve_tools(prompt, config)
455-
local resolved_resources, resolved_tools, resolved_stickies, prompt = M.resolve_functions(prompt, config)
456-
local selected_model, prompt = M.resolve_model(prompt, config)
419+
local selected_tools, prompt = prompts.resolve_tools(prompt, config)
420+
local resolved_resources, resolved_tools, resolved_stickies, prompt = prompts.resolve_functions(prompt, config)
421+
local selected_model, prompt = prompts.resolve_model(prompt, config)
457422

458423
-- Remove sticky prefix
459424
prompt = table.concat(

0 commit comments

Comments
 (0)