Skip to content

Commit 3ba9296

Browse files
committed
feat(nvim): use pre_hook to prepare the embeddings.
1 parent e51cb24 commit 3ba9296

File tree

4 files changed

+67
-8
lines changed

4 files changed

+67
-8
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
local M = {}
2+
3+
local vc_config = require("vectorcode.config")
4+
5+
---@param path string[]|string path to files or wildcards.
6+
---@param project_root? string
7+
---@param callback? VectorCode.JobRunner.Callback
8+
function M.vectorise_files(path, project_root, callback)
9+
if type(path) == "string" then
10+
path = { path }
11+
end
12+
local jobrunner =
13+
require("vectorcode.integrations.codecompanion.common").initialise_runner(
14+
vc_config.get_user_config().async_backend == "lsp"
15+
)
16+
17+
local args = { "vectorise", "--pipe" }
18+
if project_root then
19+
vim.list_extend(args, { "--project_root", project_root })
20+
end
21+
vim.list_extend(args, path)
22+
jobrunner.run_async(args, function(result, error, code, signal)
23+
if type(callback) == "function" then
24+
callback(result, error, code, signal)
25+
end
26+
end, 0)
27+
end
28+
return M

lua/vectorcode/integrations/codecompanion/prompts/nvim.lua

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,26 @@
11
---@module "codecompanion"
22

3+
local config = require("vectorcode.config")
4+
local prepare = function(rtp)
5+
vim.notify(
6+
"Vectorising neovim runtime files...",
7+
vim.log.levels.INFO,
8+
config.notify_opts
9+
)
10+
require("vectorcode.integrations.codecompanion.prompts").vectorise_files(
11+
vim.fs.joinpath(rtp, "**/*.lua"),
12+
rtp,
13+
function(result, _, _, _)
14+
if result ~= nil and not vim.tbl_isempty(result) then
15+
vim.schedule_wrap(vim.notify)(
16+
string.format("Added %d files to the database!", result.add or 0),
17+
vim.log.levels.INFO,
18+
config.notify_opts
19+
)
20+
end
21+
end
22+
)
23+
end
324
return require("vectorcode.config").check_cli_wrap(function()
425
local constants = require("codecompanion.config").config.constants
526
local rtp = vim.fs.normalize(vim.env.VIMRUNTIME)
@@ -17,23 +38,29 @@ return require("vectorcode.config").check_cli_wrap(function()
1738
vim.log.levels.ERROR
1839
)
1940
end
41+
2042
return {
2143
name = "Neovim Assistant",
2244
prompts = {
2345
strategy = "chat",
2446
description = "Use VectorCode to index and query from neovim documentation and lua runtime.",
25-
47+
opts = {
48+
pre_hook = function()
49+
prepare(rtp)
50+
end,
51+
},
2652
prompts = {
2753
{
2854
role = constants.SYSTEM_ROLE,
2955
content = string.format(
3056
[[You are an neovim expert.
3157
You will be given tools to index and query from the neovim runtime library.
3258
This will include lua APIs for the neovim runtime and documentation for the neovim build that the user is running.
33-
Use the tools to explain the user's questions related to neovim.
3459
The runtime files are stored in `%s`.
3560
You can ONLY use the vectorcode tools to interact with these files or directory.
3661
DO NOT attempt to read from or write into this directory.
62+
When the user asked a question that is not part of a previous query tool call, make a new query using new keywords that are directly relevant to the new question.
63+
If the tool returns an error that says the collection doesn't exist, it's because the files are still being indexed, and you should ask the user to wait for it to finish.
3764
]],
3865
rtp
3966
),
@@ -42,13 +69,15 @@ DO NOT attempt to read from or write into this directory.
4269
role = constants.USER_ROLE,
4370
content = string.format(
4471
[[
45-
You are given the @{vectorcode_vectorise} and @{vectorcode_query} tools.
46-
Vectorrise all lua files and `*.txt` files under this directory using the `vectorcode_vectorise` tool. Use wildcards to match all lua and `txt` files. Use absolute paths when supplying paths to the vectorcode_vectorise tool;
72+
You are given the @{vectorcode_query} tool.
4773
Use `%s` as the value of the `project_root` argument;
48-
When you're done, I'll be asking you questions related to these documents.
74+
I'll be asking you questions related to these documents.
4975
Use the vectorcode_query tool to query from the project root and answer my question.
76+
77+
Here's my question:
78+
79+
-
5080
]],
51-
rtp,
5281
rtp
5382
),
5483
},

lua/vectorcode/integrations/codecompanion/vectorise_tool.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ The paths should be accurate (DO NOT ASSUME A PATH EXIST) and case case-sensitiv
5252
paths = {
5353
type = "array",
5454
items = { type = "string" },
55-
description = "Paths to the files to be vectorised. DO NOT use directories for this parameter.",
55+
description = "Paths to the files to be vectorised. DO NOT use directories for this parameter. You may use wildcard here if the user instructed to do so.",
5656
},
5757
project_root = {
5858
type = "string",

lua/vectorcode/jobrunner/init.lua

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
local utils = require("vectorcode.utils")
22

3+
---@alias VectorCode.JobRunner.Callback fun(result: table, error: table, code:integer, signal: integer?)
4+
35
--- A class for calling vectorcode commands that aims at providing a unified API for both LSP and command-line backend.
46
--- Implementations exist for both direct command-line execution (`cmd.lua`) and LSP (`lsp.lua`).
57
--- For the format of the `result`, see https://github.com/Davidyz/VectorCode/blob/main/docs/cli.md#for-developers
@@ -13,7 +15,7 @@ local utils = require("vectorcode.utils")
1315
--- - `signal`: _for cmd runner only_, the shell signal sent to the process.
1416
--- The `bufnr` is used for context, potentially to find the project root or attach LSP clients.
1517
--- Returns a job handle (e.g., PID or LSP request ID) or nil if the job couldn't be started.
16-
---@field run_async fun(args: string[], callback:fun(result: table, error: table, code:integer, signal: integer?)?, bufnr: integer):(job_handle:integer?)
18+
---@field run_async fun(args: string[], callback:VectorCode.JobRunner.Callback?, bufnr: integer):(job_handle:integer?)
1719
--- Runs a vectorcode command synchronously, blocking until completion or timeout.
1820
--- Executes the command specified by `args`. Waits for up to `timeout_ms` milliseconds.
1921
--- The `bufnr` is used for context, potentially to find the project root or attach LSP clients.

0 commit comments

Comments
 (0)