-
-
Notifications
You must be signed in to change notification settings - Fork 406
Expand file tree
/
Copy pathinit.lua
More file actions
378 lines (322 loc) · 10.1 KB
/
init.lua
File metadata and controls
378 lines (322 loc) · 10.1 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
local config = require("codecompanion.config")
local Path = require("plenary.path")
local buf_utils = require("codecompanion.utils.buffers")
local log = require("codecompanion.utils.log")
local M = {}
local api = vim.api
local fmt = string.format
---Establishes the connection, authenticates, creates a session and links the buffer.
---@param chat CodeCompanion.Chat
---@param cb? function
---@return nil
function M.create_acp_connection(chat, cb)
---Run async so as not to block the UI
local async_utils = require("codecompanion.utils.async")
local function call_cb()
if cb then
vim.schedule(cb)
end
end
async_utils.sync(function()
local ACPHandler = require("codecompanion.interactions.chat.acp.handler")
local handler = ACPHandler.new(chat)
if not handler:ensure_connection() then
return call_cb()
end
handler:ensure_session()
call_cb()
end)()
end
---Format the given role without any separator
---@param role string
---@return string
function M.format_role(role)
if config.display.chat.show_header_separator then
role = vim.trim(role:gsub(config.display.chat.separator, ""))
end
return role
end
---Strip any context from the messages - The LLM doesn't need to see this
---@param messages table
---@return table
function M.strip_context(messages)
local i = 1
while messages[i] and messages[i]:sub(1, 1) == ">" do
table.remove(messages, i)
-- we do not increment i, since removing shifts everything down
end
return messages
end
---Get the keymaps for the slash commands
---@param slash_commands table
---@return table
function M.slash_command_keymaps(slash_commands)
local keymaps = {}
for k, v in pairs(slash_commands) do
if v.keymaps then
keymaps[k] = {}
keymaps[k].description = v.description
keymaps[k].callback = "keymaps." .. k
keymaps[k].modes = v.keymaps.modes
end
end
return keymaps
end
---Check if the messages contain any user messages
---@param messages table The list of messages to check
---@return boolean
function M.has_user_messages(messages)
return vim.iter(messages):any(function(msg)
return msg.role == config.constants.USER_ROLE
end)
end
---Helper function to update the chat settings and model if changed
---@param chat CodeCompanion.Chat
---@param settings table The new settings to apply
---@return nil
function M.apply_settings_and_model(chat, settings)
local old_model = chat.settings.model
chat:apply_settings(settings)
if old_model and old_model ~= settings.model then
chat:change_model({ model = settings.model })
end
end
---Determine if a tag exists in the messages table
---@param tag string
---@param messages CodeCompanion.Chat.Messages
---@return boolean
function M.has_tag(tag, messages)
return vim.tbl_contains(
vim.tbl_map(function(msg)
return msg._meta and msg._meta.tag
end, messages),
tag
)
end
---Resolve which MCP servers should be added to new chat buffers
---@return table<string> server_names List of server names to add to chat
function M.mcp_servers_to_add_to_chat()
local mcp_cfg = config.mcp
local default_servers = mcp_cfg.opts and mcp_cfg.opts.default_servers
if type(default_servers) == "table" then
return vim.deepcopy(default_servers)
end
return {}
end
---Start MCP servers and add their tools to the chat buffer
---@param chat CodeCompanion.Chat
---@param server_names table<string> List of MCP server names
---@return nil
function M.start_mcp_servers(chat, server_names)
local mcp = require("codecompanion.mcp")
---Add an MCP server's tool group to the chat buffer
---@param name string
local function add_tools(name)
chat.tools:refresh({ adapter = chat.adapter })
chat.tool_registry:add(mcp.tool_prefix() .. name, { config = chat.tools.tools_config })
log:debug("Added MCP server tools for `%s` to chat %d", name, chat.id)
end
for _, name in ipairs(server_names) do
local status = mcp.get_status()
local server_status = status[name]
if server_status and server_status.ready and server_status.tool_count > 0 then
add_tools(name)
else
mcp.enable_server(name, {
on_tools_loaded = function()
add_tools(name)
end,
})
end
end
end
---Remove all MCP tool groups from the chat's tool registry
---@param chat CodeCompanion.Chat
---@return nil
function M.remove_mcp_tools(chat)
local mcp = require("codecompanion.mcp")
local prefix = mcp.tool_prefix()
for group_name, _ in pairs(chat.tool_registry.groups) do
if group_name:sub(1, #prefix) == prefix then
chat.tool_registry:remove_group(group_name)
end
end
end
---Determine if context has already been added to the messages stack
---@param context string
---@param messages CodeCompanion.Chat.Messages
---@return boolean
function M.has_context(context, messages)
return vim.tbl_contains(
vim.tbl_map(function(msg)
return msg.context and msg.context.id
end, messages),
context
)
end
---Format buffer content with XML wrapper for LLM consumption
---@param bufnr number
---@param path string
---@param opts? { message?: string, range?: table }
---@return string content The XML-wrapped content
---@return string id The buffer context ID
---@return string filename The buffer filename
function M.format_buffer_for_llm(bufnr, path, opts)
opts = opts or {}
-- Handle unloaded buffers
local content
if not api.nvim_buf_is_loaded(bufnr) then
local file_content = Path.new(path):read()
if file_content == "" then
error("Could not read the file: " .. path)
end
content = fmt(
[[````%s
%s
````]],
vim.filetype.match({ filename = path }),
buf_utils.add_line_numbers(vim.trim(file_content))
)
else
content = fmt(
[[````%s
%s
````]],
buf_utils.get_info(bufnr).filetype,
buf_utils.add_line_numbers(buf_utils.get_content(bufnr, opts.range))
)
end
local filename = vim.fn.fnamemodify(path, ":t")
-- Generate consistent ID using relative path for conciseness
local id = "<buf>" .. vim.fn.fnamemodify(path, ":.") .. "</buf>"
local message = opts.message or "File content"
local formatted_content = fmt(
[[<attachment filepath="%s" buffer_number="%s">%s:
%s</attachment>]],
path,
bufnr,
message,
content
)
return formatted_content, id, filename
end
---Format buffer content with XML wrapper for LLM consumption
---@param path string
---@param opts? { message?: string, range?: table }
---@return string file_contents
---@return string id The context ID
---@return string path The file path
---@return string ft The filetype
---@return string file_contents The raw file contents
function M.format_file_for_llm(path, opts)
opts = opts or {}
local file_contents = Path.new(path):read()
local ft = vim.filetype.match({ filename = path })
local id = "<file>" .. vim.fn.fnamemodify(path, ":.") .. "</file>"
local content
if opts.message then
content = fmt(
[[%s
````%s
%s
````]],
opts.message,
ft,
file_contents
)
else
content = fmt(
[[<attachment filepath="%s">%s:
````%s
%s
````
</attachment>]],
path,
"Here is the content from the file",
ft,
file_contents
)
end
return content, id, path, ft, file_contents
end
---Add line numbers with an offset to content
---@param content string
---@param start_line number The starting line number
---@return string
local function add_line_numbers_from(content, start_line)
local formatted = {}
local lines = vim.split(content, "\n")
for i, line in ipairs(lines) do
table.insert(formatted, fmt("%d |%s", start_line + i - 1, line))
end
return table.concat(formatted, "\n")
end
---Format a single viewport range for LLM consumption
---@param bufnr number
---@param range table {start_line, end_line}
---@return string content The XML-wrapped content
---@return string id The context ID
function M.format_viewport_range_for_llm(bufnr, range)
local info = buf_utils.get_info(bufnr)
local filepath = info.path
local start_line, end_line = range[1], range[2]
local buffer_content = buf_utils.get_content(bufnr, { start_line - 1, end_line })
local numbered_content = add_line_numbers_from(buffer_content, start_line)
local content = fmt(
[[````%s
%s
````]],
info.filetype,
numbered_content
)
local excerpt_info = fmt("Excerpt from %s, lines %d to %d", filepath, start_line, end_line)
local formatted_content = fmt(
[[<attachment filepath="%s" buffer_number="%s">%s:
%s</attachment>]],
filepath,
bufnr,
excerpt_info,
content
)
local id = fmt("<viewport>%s:%d-%d</viewport>", vim.fn.fnamemodify(filepath, ":."), start_line, end_line)
return formatted_content, id
end
---Format viewport content with XML wrapper for LLM consumption
---@param buf_lines table Buffer lines from get_visible_lines()
---@return string content The XML-wrapped content for all visible buffers
function M.format_viewport_for_llm(buf_lines)
local formatted = {}
for bufnr, ranges in pairs(buf_lines) do
for _, range in ipairs(ranges) do
local content, _ = M.format_viewport_range_for_llm(bufnr, range)
table.insert(formatted, content)
end
end
return table.concat(formatted, "\n\n")
end
---Returns the number of tokens that trigger context management for a given operation
---@param adapter CodeCompanion.HTTPAdapter
---@param opts? { operation?: "editing"|"compaction" } defaults to "compaction"
---@return number
function M.trigger_context_management(adapter, opts)
opts = opts or {}
local operation = opts.operation or "compaction"
local context_management = config.interactions.chat.opts.context_management
local settings = context_management and context_management[operation]
local trigger = settings and settings.trigger
if trigger == nil then
return 0
end
if trigger < 1 then
local ok
ok, trigger = pcall(function()
return math.floor(trigger * adapter.schema.model.choices[adapter.schema.model.default].meta.context_window)
end)
if not ok then
log:error("Could not evaluate the %s trigger for context management in the `%s` adapter", operation, adapter.name)
return 0
end
end
return trigger
end
return M