Skip to content

Commit fafcd7a

Browse files
authored
feat(nvim): Add prompt library builder for codecompanion.nvim extension (#264)
* feat(nvim): Add VectorCode integration for Neovim help files * feat(nvim): use pre_hook to prepare the embeddings. * feat(nvim): also vectorise help files * feat(nvim): Add toggleable notifications for vectorising help files * fix(nvim): make sure to load the custom system prompt * feat(nvim): Add customisable prompt library for CodeCompanion * Auto generate docs * feat(nvim): Improve vectorisation feedback * fix(nvim): loop require. * Auto generate docs * feat(nvim): allow function type for `project_root` and `file_patterns` * docs(nvim): Vectorise help files notify message * Auto generate docs * docs(nvim): Document customisable prompt library for Neovim * Auto generate docs * refactor(nvim): move prompt library presets to dedicated module * Auto generate docs * feat(nvim): Improve project root and file pattern handling * update documentations. * Auto generate docs
1 parent 4580c45 commit fafcd7a

File tree

9 files changed

+390
-53
lines changed

9 files changed

+390
-53
lines changed

doc/VectorCode.txt

Lines changed: 80 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ Table of Contents *VectorCode-table-of-contents*
2929
- |VectorCode-integrations|
3030
- |VectorCode-milanglacier/minuet-ai.nvim|
3131
- |VectorCode-olimorris/codecompanion.nvim|
32+
- |VectorCode-tools|
33+
- |VectorCode-prompt-library|
3234
- |VectorCode-copilotc-nvim/copilotchat.nvim|
3335
- |VectorCode-setup|
3436
- |VectorCode-configuration-options|
@@ -176,6 +178,9 @@ OLIMORRIS/CODECOMPANION.NVIM ~
176178

177179
<https://asciinema.org/a/8WP8QJHNAR9lEllZSSx3poLPD?t=3>
178180

181+
182+
TOOLS
183+
179184
The following requires VectorCode 0.7+ and a recent version of
180185
CodeCompanion.nvim.
181186

@@ -196,7 +201,7 @@ the `include_in_toolbox` option explained below.
196201

197202
>lua
198203
---@module "vectorcode"
199-
opts = {
204+
require("codecompanion").setup({
200205
extensions = {
201206
vectorcode = {
202207
---@type VectorCode.CodeCompanion.ExtensionOpts
@@ -231,15 +236,15 @@ the `include_in_toolbox` option explained below.
231236
enabled = false,
232237
adapter = nil,
233238
query_augmented = true,
234-
}
239+
},
235240
},
236241
files_ls = {},
237-
files_rm = {}
238-
}
242+
files_rm = {},
243+
},
239244
},
240245
},
241-
}
242-
}
246+
},
247+
})
243248
<
244249

245250
The following are the common options that all tools supports:
@@ -295,6 +300,59 @@ so that when the LLM decide what information to include, it _may_ be able to
295300
avoid omitting stuff related to query.
296301

297302

303+
PROMPT LIBRARY
304+
305+
On VectorCode 0.7.16+ and CodeCompanion.nvim 17.20.0+, VectorCode also provides
306+
a customisable prompt library that helps you RAG local directories. The presets
307+
provided by VectorCode are available here
308+
<../../lua/vectorcode/integrations/codecompanion/prompts/presets.lua>, which
309+
you can refer to if you wish to build local RAG APPs with CodeCompanion.nvim
310+
and VectorCode.
311+
312+
>lua
313+
require("codecompanion").setup({
314+
extensions = {
315+
vectorcode = {
316+
---@type VectorCode.CodeCompanion.ExtensionOpts
317+
opts = {
318+
---@type table<string, VectorCode.CodeCompanion.PromptFactory.Opts>
319+
prompt_library = {
320+
{
321+
["Neovim Tutor"] = {
322+
-- this is for demonstration only.
323+
-- "Neovim Tutor" is shipped with this plugin already,
324+
-- and you don't need to add it in the config
325+
-- unless you're not happy with the defaults.
326+
project_root = vim.env.VIMRUNTIME,
327+
file_patterns = { "lua/**/*.lua", "doc/**/*.txt" },
328+
-- system_prompt = ...,
329+
-- user_prompt = ...,
330+
},
331+
},
332+
},
333+
},
334+
},
335+
},
336+
})
337+
<
338+
339+
The `prompt_library` option is a mapping of prompt name (`string`) to a lua
340+
table (type annotation available) that contains some information used to
341+
generate the embeddings:
342+
343+
- `project_root``string`, the path to the directory (for example,
344+
`/usr/share/nvim/runtime/`);
345+
- `file_patterns``string[]`, file name patterns that defines files to be vectorised.
346+
You should either use absolute paths or relative paths from the project root;
347+
- `system_prompt` and `user_prompt``string|fun(context:table):string|nil`
348+
Theseoptions allow you to customise the prompts. See
349+
codecompanion.nvim documentation <https://codecompanion.olimorris.dev/extending/prompts#recipe-2-using-context-in-your-prompts>
350+
if you want to use a function here that build the prompts from the context.
351+
352+
The first time will take some extra time for computing the embeddings, but the
353+
subsequent runs should be a lot faster.
354+
355+
298356
COPILOTC-NVIM/COPILOTCHAT.NVIM ~
299357

300358
CopilotC-Nvim/CopilotChat.nvim
@@ -310,13 +368,14 @@ contextual information about your codebase to enhance Copilot’s responses.
310368
Add this to your CopilotChat configuration:
311369

312370
>lua
313-
local vectorcode_ctx = require('vectorcode.integrations.copilotchat').make_context_provider({
314-
prompt_header = "Here are relevant files from the repository:", -- Customize header text
315-
prompt_footer = "\nConsider this context when answering:", -- Customize footer text
316-
skip_empty = true, -- Skip adding context when no files are retrieved
317-
})
371+
local vectorcode_ctx =
372+
require("vectorcode.integrations.copilotchat").make_context_provider({
373+
prompt_header = "Here are relevant files from the repository:", -- Customize header text
374+
prompt_footer = "\nConsider this context when answering:", -- Customize footer text
375+
skip_empty = true, -- Skip adding context when no files are retrieved
376+
})
318377

319-
require('CopilotChat').setup({
378+
require("CopilotChat").setup({
320379
-- Your other CopilotChat options...
321380

322381
contexts = {
@@ -328,10 +387,10 @@ Add this to your CopilotChat configuration:
328387
prompts = {
329388
Explain = {
330389
prompt = "Explain the following code in detail:\n$input",
331-
context = {"selection", "vectorcode"}, -- Add vectorcode to the context
390+
context = { "selection", "vectorcode" }, -- Add vectorcode to the context
332391
},
333392
-- Other prompts...
334-
}
393+
},
335394
})
336395
<
337396

@@ -366,7 +425,7 @@ You can configure VectorCode to be part of your sticky prompts, ensuring every
366425
conversation includes relevant codebase context automatically:
367426

368427
>lua
369-
require('CopilotChat').setup({
428+
require("CopilotChat").setup({
370429
-- Your other CopilotChat options...
371430

372431
sticky = {
@@ -392,8 +451,8 @@ cached retrieval results.
392451
>lua
393452
tabline = {
394453
lualine_y = {
395-
require("vectorcode.integrations").lualine(opts)
396-
}
454+
require("vectorcode.integrations").lualine(opts),
455+
},
397456
}
398457
<
399458

@@ -419,7 +478,7 @@ when neovim starts). If this bothers you, you can use the following snippet:
419478
end
420479
end,
421480
},
422-
}
481+
},
423482
}
424483
<
425484

@@ -575,12 +634,9 @@ in an autocmd:
575634
callback = function()
576635
local bufnr = vim.api.nvim_get_current_buf()
577636
cacher.async_check("config", function()
578-
cacher.register_buffer(
579-
bufnr,
580-
{
581-
n_query = 10,
582-
}
583-
)
637+
cacher.register_buffer(bufnr, {
638+
n_query = 10,
639+
})
584640
end, nil)
585641
end,
586642
desc = "Register buffer for VectorCode",

docs/neovim/README.md

Lines changed: 79 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# NeoVim Plugin
2+
23
> [!NOTE]
34
> This plugin depends on the CLI tool. Please go through
45
> [the CLI documentation](../cli/README.md) and make sure the VectorCode CLI is working
@@ -18,6 +19,8 @@
1819
* [Integrations](#integrations)
1920
* [milanglacier/minuet-ai.nvim](#milanglacierminuet-ainvim)
2021
* [olimorris/codecompanion.nvim](#olimorriscodecompanionnvim)
22+
* [Tools](#tools)
23+
* [Prompt Library](#prompt-library)
2124
* [CopilotC-Nvim/CopilotChat.nvim](#copilotc-nvimcopilotchatnvim)
2225
* [Setup](#setup)
2326
* [Configuration Options](#configuration-options)
@@ -155,6 +158,7 @@ or change the value of `async_opts.n_query` in the `setup` function
155158

156159
[![asciicast](https://asciinema.org/a/8WP8QJHNAR9lEllZSSx3poLPD.svg)](https://asciinema.org/a/8WP8QJHNAR9lEllZSSx3poLPD?t=3)
157160

161+
#### Tools
158162
The following requires VectorCode 0.7+ and a recent version of CodeCompanion.nvim.
159163

160164
The CodeCompanion extension will register the following tools:
@@ -176,7 +180,7 @@ option explained below.
176180

177181
```lua
178182
---@module "vectorcode"
179-
opts = {
183+
require("codecompanion").setup({
180184
extensions = {
181185
vectorcode = {
182186
---@type VectorCode.CodeCompanion.ExtensionOpts
@@ -211,15 +215,15 @@ opts = {
211215
enabled = false,
212216
adapter = nil,
213217
query_augmented = true,
214-
}
218+
},
215219
},
216220
files_ls = {},
217-
files_rm = {}
218-
}
221+
files_rm = {},
222+
},
219223
},
220224
},
221-
}
222-
}
225+
},
226+
})
223227
```
224228

225229
The following are the common options that all tools supports:
@@ -277,6 +281,58 @@ The `query` tool contains the following extra config options:
277281
query so that when the LLM decide what information to include, it _may_ be
278282
able to avoid omitting stuff related to query.
279283

284+
#### Prompt Library
285+
286+
On VectorCode 0.7.16+ and CodeCompanion.nvim 17.20.0+, VectorCode also provides a
287+
customisable prompt library that helps you RAG local directories. The presets
288+
provided by VectorCode are available
289+
[here](../../lua/vectorcode/integrations/codecompanion/prompts/presets.lua), which
290+
you can refer to if you wish to build local RAG APPs with CodeCompanion.nvim and
291+
VectorCode.
292+
293+
```lua
294+
require("codecompanion").setup({
295+
extensions = {
296+
vectorcode = {
297+
---@type VectorCode.CodeCompanion.ExtensionOpts
298+
opts = {
299+
---@type table<string, VectorCode.CodeCompanion.PromptFactory.Opts>
300+
prompt_library = {
301+
{
302+
["Neovim Tutor"] = {
303+
-- this is for demonstration only.
304+
-- "Neovim Tutor" is shipped with this plugin already,
305+
-- and you don't need to add it in the config
306+
-- unless you're not happy with the defaults.
307+
project_root = vim.env.VIMRUNTIME,
308+
file_patterns = { "lua/**/*.lua", "doc/**/*.txt" },
309+
-- system_prompt = ...,
310+
-- user_prompt = ...,
311+
},
312+
},
313+
},
314+
},
315+
},
316+
},
317+
})
318+
```
319+
320+
The `prompt_library` option is a mapping of prompt name (`string`) to a lua table
321+
(type annotation available) that contains some information used to generate the
322+
embeddings:
323+
324+
- `project_root`: `string`, the path to the directory (for example,
325+
`/usr/share/nvim/runtime/`);
326+
- `file_patterns`: `string[]`, file name patterns that defines files to be vectorised.
327+
You should either use absolute paths or relative paths from the project root;
328+
- `system_prompt` and `user_prompt`: `string|fun(context:table):string|nil`:
329+
These options allow you to customise the prompts. See
330+
[codecompanion.nvim documentation](https://codecompanion.olimorris.dev/extending/prompts#recipe-2-using-context-in-your-prompts)
331+
if you want to use a function here that build the prompts from the context.
332+
333+
The first time will take some extra time for computing the embeddings, but the
334+
subsequent runs should be a lot faster.
335+
280336
### [CopilotC-Nvim/CopilotChat.nvim](https://github.com/CopilotC-Nvim/CopilotChat.nvim)
281337

282338
[CopilotC-Nvim/CopilotChat.nvim](https://github.com/CopilotC-Nvim/CopilotChat.nvim)
@@ -290,13 +346,14 @@ contextual information about your codebase to enhance Copilot's responses. Add t
290346
to your CopilotChat configuration:
291347

292348
```lua
293-
local vectorcode_ctx = require('vectorcode.integrations.copilotchat').make_context_provider({
294-
prompt_header = "Here are relevant files from the repository:", -- Customize header text
295-
prompt_footer = "\nConsider this context when answering:", -- Customize footer text
296-
skip_empty = true, -- Skip adding context when no files are retrieved
297-
})
298-
299-
require('CopilotChat').setup({
349+
local vectorcode_ctx =
350+
require("vectorcode.integrations.copilotchat").make_context_provider({
351+
prompt_header = "Here are relevant files from the repository:", -- Customize header text
352+
prompt_footer = "\nConsider this context when answering:", -- Customize footer text
353+
skip_empty = true, -- Skip adding context when no files are retrieved
354+
})
355+
356+
require("CopilotChat").setup({
300357
-- Your other CopilotChat options...
301358

302359
contexts = {
@@ -308,10 +365,10 @@ require('CopilotChat').setup({
308365
prompts = {
309366
Explain = {
310367
prompt = "Explain the following code in detail:\n$input",
311-
context = {"selection", "vectorcode"}, -- Add vectorcode to the context
368+
context = { "selection", "vectorcode" }, -- Add vectorcode to the context
312369
},
313370
-- Other prompts...
314-
}
371+
},
315372
})
316373
```
317374

@@ -339,7 +396,7 @@ The integration includes caching to avoid sending duplicate context to the LLM,
339396
You can configure VectorCode to be part of your sticky prompts, ensuring every conversation includes relevant codebase context automatically:
340397

341398
```lua
342-
require('CopilotChat').setup({
399+
require("CopilotChat").setup({
343400
-- Your other CopilotChat options...
344401

345402
sticky = {
@@ -360,8 +417,8 @@ cached retrieval results.
360417
```lua
361418
tabline = {
362419
lualine_y = {
363-
require("vectorcode.integrations").lualine(opts)
364-
}
420+
require("vectorcode.integrations").lualine(opts),
421+
},
365422
}
366423
```
367424
`opts` is a table with the following configuration option:
@@ -386,7 +443,7 @@ tabline = {
386443
end
387444
end,
388445
},
389-
}
446+
},
390447
}
391448
```
392449
This will further delay the loading of VectorCode to the moment you (or one of
@@ -533,12 +590,9 @@ vim.api.nvim_create_autocmd("LspAttach", {
533590
callback = function()
534591
local bufnr = vim.api.nvim_get_current_buf()
535592
cacher.async_check("config", function()
536-
cacher.register_buffer(
537-
bufnr,
538-
{
539-
n_query = 10,
540-
}
541-
)
593+
cacher.register_buffer(bufnr, {
594+
n_query = 10,
595+
})
542596
end, nil)
543597
end,
544598
desc = "Register buffer for VectorCode",

0 commit comments

Comments
 (0)