Skip to content

Commit

Permalink
refactor: optimize context handling and content blocks
Browse files Browse the repository at this point in the history
Improve memory usage and performance by:
- Replace original field with outline in context struct
- Increase TOP_SYMBOLS limit from 64 to 100
- Optimize content block generation to avoid unnecessary string operations
- Simplify embedding messages generation by removing file grouping
- Use outline content when available for more efficient context handling
  • Loading branch information
deathbeam committed Dec 4, 2024
1 parent 7753236 commit 55c1991
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 53 deletions.
13 changes: 5 additions & 8 deletions lua/CopilotChat/context.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
---@field content string
---@field filename string
---@field filetype string
---@field original string?
---@field outline string?
---@field symbols table<string, CopilotChat.context.symbol>?
---@field embedding table<number>?

Expand Down Expand Up @@ -64,7 +64,7 @@ local OFF_SIDE_RULE_LANGUAGES = {
'fsharp',
}

local TOP_SYMBOLS = 64
local TOP_SYMBOLS = 100
local TOP_RELATED = 20
local MULTI_FILE_THRESHOLD = 5

Expand Down Expand Up @@ -204,6 +204,7 @@ end
---@param ft string
---@return CopilotChat.context.embed
local function build_outline(content, filename, ft)
---@type CopilotChat.context.embed
local output = {
filename = filename,
filetype = ft,
Expand Down Expand Up @@ -269,8 +270,7 @@ local function build_outline(content, filename, ft)
parse_node(root)

if #outline_lines > 0 then
output.original = content
output.content = table.concat(outline_lines, '\n')
output.outline = table.concat(outline_lines, '\n')
output.symbols = symbols
end

Expand Down Expand Up @@ -571,10 +571,7 @@ function M.filter_embeddings(copilot, prompt, embeddings)
log.debug(string.format('%s: %s - %s', i, item.score, item.filename))
end

-- Return embeddings with original content
return vim.tbl_map(function(item)
return vim.tbl_extend('force', item, { content = item.original or item.content })
end, embeddings)
return embeddings
end

return M
69 changes: 25 additions & 44 deletions lua/CopilotChat/copilot.lua
Original file line number Diff line number Diff line change
Expand Up @@ -78,33 +78,35 @@ end

--- Generate content block with line numbers, truncating if necessary
---@param content string: The content
---@param outline string?: The outline
---@param threshold number: The threshold for truncation
---@param start_line number|nil: The starting line number
---@return string
local function generate_content_block(content, threshold, start_line)
local lines = vim.split(content, '\n')
local total_chars = 0

for i, line in ipairs(lines) do
total_chars = total_chars + #line
if total_chars > threshold then
lines = vim.list_slice(lines, 1, i)
table.insert(lines, TRUNCATED)
break
end
local function generate_content_block(content, outline, threshold, start_line)
local total_chars = #content
if total_chars > threshold and outline then
content = outline
total_chars = #content
end
if total_chars > threshold then
content = content:sub(1, threshold)
content = content .. '\n' .. TRUNCATED
end

if start_line ~= -1 then
local lines = vim.split(content, '\n')
local total_lines = #lines
local max_length = #tostring(total_lines)
for i, line in ipairs(lines) do
local formatted_line_number =
string.format('%' .. max_length .. 'd', i - 1 + (start_line or 1))
lines[i] = formatted_line_number .. ': ' .. line
end

return table.concat(lines, '\n')
end

return table.concat(lines, '\n')
return content
end

--- Generate messages for the given selection
Expand Down Expand Up @@ -133,7 +135,7 @@ local function generate_selection_messages(selection)
.. string.format(
'```%s\n%s\n```',
filetype,
generate_content_block(content, BIG_FILE_THRESHOLD, selection.start_line)
generate_content_block(content, nil, BIG_FILE_THRESHOLD, selection.start_line)
)

if selection.diagnostics then
Expand Down Expand Up @@ -170,40 +172,18 @@ end
--- Generate messages for the given embeddings
--- @param embeddings table<CopilotChat.context.embed>
local function generate_embeddings_messages(embeddings)
local files = {}
for _, embedding in ipairs(embeddings) do
local filename = embedding.filename or 'unknown'
if not files[filename] then
files[filename] = {}
end
table.insert(files[filename], embedding)
end

local out = {}

for filename, group in pairs(files) do
local filetype = group[1].filetype or 'text'
table.insert(out, {
context = string.format(CONTEXT_FORMAT, filename, filename),
return vim.tbl_map(function(embedding)
return {
context = string.format(CONTEXT_FORMAT, embedding.filename, embedding.filename),
content = string.format(
'# FILE:%s CONTEXT\n```%s\n%s\n```',
filename:upper(),
filetype,
generate_content_block(
table.concat(
vim.tbl_map(function(e)
return vim.trim(e.content)
end, group),
'\n'
),
BIG_FILE_THRESHOLD
)
embedding.filename:upper(),
embedding.filetype or 'text',
generate_content_block(embedding.content, embedding.outline, BIG_FILE_THRESHOLD)
),
role = 'user',
})
end

return out
}
end, embeddings)
end

local function generate_ask_request(
Expand Down Expand Up @@ -275,7 +255,8 @@ local function generate_embedding_request(inputs, model, threshold)
return {
dimensions = 512,
input = vim.tbl_map(function(embedding)
local content = generate_content_block(embedding.content, threshold, -1)
local content =
generate_content_block(embedding.outline or embedding.content, nil, threshold, -1)
if embedding.filetype == 'raw' then
return content
else
Expand Down
2 changes: 1 addition & 1 deletion lua/CopilotChat/ui/debug.lua
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ local function build_debug_info()
table.insert(lines, 'Current buffer outline:')
table.insert(lines, '`' .. buf.filename .. '`')
table.insert(lines, '```' .. buf.filetype)
local outline_lines = vim.split(buf.content, '\n')
local outline_lines = vim.split(buf.outline or buf.content, '\n')
for _, line in ipairs(outline_lines) do
table.insert(lines, line)
end
Expand Down

0 comments on commit 55c1991

Please sign in to comment.