-
-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathcommon.lua
More file actions
148 lines (140 loc) · 4.26 KB
/
Copy pathcommon.lua
File metadata and controls
148 lines (140 loc) · 4.26 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
local job_runner
local vc_config = require("vectorcode.config")
local notify_opts = vc_config.notify_opts
local logger = vc_config.logger
---@type VectorCode.CodeCompanion.QueryToolOpts
local default_query_options = {
max_num = { chunk = -1, document = -1 },
default_num = { chunk = 50, document = 10 },
no_duplicate = true,
chunk_mode = false,
}
---@type VectorCode.CodeCompanion.LsToolOpts
local default_ls_options = {}
---@type VectorCode.CodeCompanion.VectoriseToolOpts
local default_vectorise_options = {}
return {
tool_result_source = "VectorCodeToolResult",
---@param t table|string
---@return string
flatten_table_to_string = function(t)
if type(t) == "string" then
return t
end
return table.concat(vim.iter(t):flatten(math.huge):totable(), "\n")
end,
---@param opts VectorCode.CodeCompanion.LsToolOpts|{}|nil
---@return VectorCode.CodeCompanion.LsToolOpts
get_ls_tool_opts = function(opts)
opts = vim.tbl_deep_extend("force", default_ls_options, opts or {})
logger.info(
string.format(
"Loading `vectorcode_ls` with the following opts:\n%s",
vim.inspect(opts)
)
)
return opts
end,
---@param opts VectorCode.CodeCompanion.VectoriseToolOpts|{}|nil
---@return VectorCode.CodeCompanion.VectoriseToolOpts
get_vectorise_tool_opts = function(opts)
opts = vim.tbl_deep_extend("force", default_vectorise_options, opts or {})
logger.info(
string.format(
"Loading `vectorcode_vectorise` with the following opts:\n%s",
vim.inspect(opts)
)
)
return opts
end,
---@param opts VectorCode.CodeCompanion.QueryToolOpts|{}|nil
---@return VectorCode.CodeCompanion.QueryToolOpts
get_query_tool_opts = function(opts)
if opts == nil or opts.use_lsp == nil then
opts = vim.tbl_deep_extend(
"force",
opts or {},
{ use_lsp = vc_config.get_user_config().async_backend == "lsp" }
)
end
opts = vim.tbl_deep_extend("force", default_query_options, opts)
if type(opts.default_num) == "table" then
if opts.chunk_mode then
opts.default_num = opts.default_num.chunk
else
opts.default_num = opts.default_num.document
end
assert(
type(opts.default_num) == "number",
"default_num should be an integer or a table: {chunk: integer, document: integer}"
)
end
if type(opts.max_num) == "table" then
if opts._ then
opts.max_num = opts.max_num.chunk
else
opts.max_num = opts.max_num.document
end
assert(
type(opts.max_num) == "number",
"max_num should be an integer or a table: {chunk: integer, document: integer}"
)
end
logger.info(
string.format(
"Loading `vectorcode_query` with the following opts:\n%s",
vim.inspect(opts)
)
)
return opts
end,
---@param result VectorCode.QueryResult
---@return string
process_result = function(result)
local llm_message
if result.chunk then
-- chunk mode
llm_message =
string.format("<path>%s</path><chunk>%s</chunk>", result.path, result.chunk)
if result.start_line and result.end_line then
llm_message = llm_message
.. string.format(
"<start_line>%d</start_line><end_line>%d</end_line>",
result.start_line,
result.end_line
)
end
else
-- full document mode
llm_message = string.format(
"<path>%s</path><content>%s</content>",
result.path,
result.document
)
end
return llm_message
end,
---@param use_lsp boolean
---@return VectorCode.JobRunner
initialise_runner = function(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")
logger.info("Using cmd runner for CodeCompanion tool.")
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
else
logger.info("Using LSP runner for CodeCompanion tool.")
end
end
return job_runner
end,
}