forked from Davidyz/VectorCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcopilotchat.lua
More file actions
109 lines (94 loc) · 3.42 KB
/
Copy pathcopilotchat.lua
File metadata and controls
109 lines (94 loc) · 3.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
---@module "CopilotChat"
---@class VectorCode.CopilotChat.ContextOpts
---@field max_num number?
---@field use_lsp boolean?
local async = require("plenary.async")
local vc_config = require("vectorcode.config")
local notify_opts = vc_config.notify_opts
local check_cli_wrap = vc_config.check_cli_wrap
local job_runner = nil
---@param use_lsp boolean
local function get_runner(use_lsp)
if job_runner == nil then
if use_lsp then
job_runner = require("vectorcode.jobrunner.lsp")
end
if job_runner == nil then
job_runner = require("vectorcode.jobrunner.cmd")
if use_lsp then
vim.schedule_wrap(vim.notify)(
"Failed to initialise the LSP runner. Falling back to cmd runner.",
vim.log.levels.WARN,
notify_opts
)
end
end
end
return job_runner
end
---@param args string[]
---@param use_lsp boolean
---@param bufnr integer
---@async
local run_job = async.wrap(function(args, use_lsp, bufnr, callback)
local runner = get_runner(use_lsp)
assert(runner ~= nil)
runner.run_async(args, callback, bufnr)
end, 4)
---@param opts VectorCode.CopilotChat.ContextOpts?
---@return CopilotChat.config.context
local make_context_provider = check_cli_wrap(function(opts)
opts = vim.tbl_deep_extend("force", {
max_num = 5,
use_lsp = vc_config.get_user_config().async_backend == "lsp",
}, opts or {})
local utils = require("CopilotChat.utils")
return {
description = [[This gives you the ability to access the repository to find information that you may need to assist the user. Supports input (query).
- **Use at your discretion** when you feel you don't have enough information about the repository or project.
- **Don't escape** special characters.
- If a class, type or function has been imported from another file, this context may be able to find its source. Add the name of the imported symbol to the query.
- The embeddings are mostly generated from source code, so using keywords that may be present in source code may help with the retrieval.
- Avoid retrieving one single file because the retrieval mechanism may not be very accurate.
= If a query failed to retrieve desired results, a new attempt should use different keywords that are orthogonal to the previous ones but with similar meanings
- Do not use exact query keywords that you have used in a previous context call in the conversation, unless the user instructed otherwise
]],
input = function(callback)
vim.ui.input({
prompt = "Enter query> ",
}, callback)
end,
resolve = function(input, source, prompt)
if not input or input == "" then
input = prompt
end
local args = {
"query",
"--pipe",
"-n",
tostring(opts.max_num),
'"' .. input .. '"',
}
local cwd = source.cwd()
local try_root = vim.fs.root(cwd, ".vectorcode") or vim.fs.root(cwd, ".git")
if try_root ~= nil then
vim.list_extend(args, { "--project_root", try_root })
end
local result, err = run_job(args, opts.use_lsp, source.bufnr)
if utils.empty(result) and err then
error(utils.make_string(err))
end
utils.schedule_main()
return vim.tbl_map(function(item)
return {
content = item.document,
filename = item.path,
filetype = utils.filetype(item.path),
}
end, result)
end,
}
end)
return {
make_context_provider = make_context_provider,
}