Skip to content

Commit dce4b3c

Browse files
author
Zhe Yu
committed
feat(nvim): Deduplicate tool results using chunk_id (wip)
1 parent 62b561d commit dce4b3c

3 files changed

Lines changed: 64 additions & 5 deletions

File tree

lua/vectorcode/integrations/codecompanion/common.lua

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
---@module "codecompanion"
2+
13
local job_runner
24
local vc_config = require("vectorcode.config")
35
local notify_opts = vc_config.notify_opts
@@ -15,8 +17,10 @@ local default_options = {
1517
chunk_mode = false,
1618
}
1719

20+
local TOOL_RESULT_SOURCE = "VectorCodeToolResult"
21+
1822
return {
19-
tool_result_source = "VectorCodeToolResult",
23+
tool_result_source = TOOL_RESULT_SOURCE,
2024
---@param t table|string
2125
---@return string
2226
flatten_table_to_string = function(t)
@@ -88,6 +92,7 @@ return {
8892
end
8993
return llm_message
9094
end,
95+
9196
---@param use_lsp boolean
9297
---@return VectorCode.JobRunner
9398
initialise_runner = function(use_lsp)
@@ -111,4 +116,53 @@ return {
111116
end
112117
return job_runner
113118
end,
119+
120+
---@param results VectorCode.Result[]
121+
---@param chat CodeCompanion.Chat
122+
---@return VectorCode.Result[]
123+
filter_results = function(results, chat)
124+
local existing_refs = chat.refs
125+
if existing_refs == nil then
126+
return results
127+
end
128+
existing_refs = vim
129+
.iter(existing_refs)
130+
:filter(
131+
---@param ref CodeCompanion.Chat.Ref
132+
function(ref)
133+
return ref.source == TOOL_RESULT_SOURCE or ref.path or ref.bufnr
134+
end
135+
)
136+
:map(
137+
---@param ref CodeCompanion.Chat.Ref
138+
function(ref)
139+
if ref.source == TOOL_RESULT_SOURCE then
140+
return ref.id
141+
elseif ref.path then
142+
return ref.path
143+
elseif ref.bufnr then
144+
return vim.api.nvim_buf_get_name(ref.bufnr)
145+
end
146+
end
147+
)
148+
:totable()
149+
150+
return vim
151+
.iter(results)
152+
:filter(
153+
---@param res VectorCode.Result
154+
function(res)
155+
-- return true if res is not in refs
156+
if res.chunk then
157+
if res.chunk_id == nil then
158+
return true
159+
end
160+
return not vim.tbl_contains(existing_refs, res.chunk_id)
161+
else
162+
return not vim.tbl_contains(existing_refs, res.path)
163+
end
164+
end
165+
)
166+
:totable()
167+
end,
114168
}

lua/vectorcode/integrations/codecompanion/func_calling_tool.lua

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,8 @@ return check_cli_wrap(function(opts)
273273
)
274274
local user_message
275275
if cmd.command == "query" then
276+
---@cast stdout +VectorCode.Result[]
277+
stdout = cc_common.filter_results(stdout, agent.chat)
276278
local max_result = #stdout
277279
if opts.max_num > 0 then
278280
max_result = math.min(opts.max_num or 1, max_result)
@@ -297,15 +299,17 @@ return check_cli_wrap(function(opts)
297299
cc_common.process_result(file),
298300
user_message
299301
)
300-
if not opts.chunk_mode then
302+
303+
if (not opts.chunk_mode) or file.chunk_id ~= nil then
301304
-- skip referencing because there will be multiple chunks with the same path (id).
302305
-- TODO: figure out a way to deduplicate.
303-
agent.chat.references:add({
306+
local ref = {
304307
source = cc_common.tool_result_source,
305-
id = file.path,
308+
id = file.chunk_id or file.path,
306309
path = file.path,
307310
opts = { visible = false },
308-
})
311+
}
312+
agent.chat.references:add(ref)
309313
end
310314
end
311315
end

lua/vectorcode/types.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
---@field chunk string?
66
---@field start_line integer?
77
---@field end_line integer?
8+
---@field chunk_id string?
89

910
---Type definitions for the cache of a buffer.
1011
---@class VectorCode.Cache

0 commit comments

Comments
 (0)