Skip to content

Commit 6ac77aa

Browse files
authored
feat(resources)!: add option to enable resource processing (#1202)
This adds option to enable resource processing, disabled by default. BREAKING CHANGE: intelligent resource processing is now disabled by default, use config.resource_processing: true to reenable Signed-off-by: Tomas Slusny <slusnucky@gmail.com>
1 parent 946069a commit 6ac77aa

4 files changed

Lines changed: 19 additions & 24 deletions

File tree

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,10 +445,12 @@ Below are all available configuration options with their default values:
445445
tools = nil, -- Default tool or array of tools (or groups) to share with LLM (can be specified manually in prompt via @).
446446
sticky = nil, -- Default sticky prompt or array of sticky prompts to use at start of every new chat (can be specified manually in prompt via >).
447447

448+
resource_processing = false, -- Enable intelligent resource processing (skips unnecessary resources to save tokens)
449+
448450
temperature = 0.1, -- Result temperature
449451
headless = false, -- Do not write to chat buffer and use history (useful for using custom processing)
450452
callback = nil, -- Function called when full response is received
451-
remember_as_sticky = true, -- Remember model as sticky prompts when asking questions
453+
remember_as_sticky = true, -- Remember config as sticky prompts when asking questions
452454

453455
-- default selection
454456
-- see select.lua for implementation

lua/CopilotChat/client.lua

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,7 @@ local class = utils.class
6161
--- Constants
6262
local RESOURCE_FORMAT = '# %s\n```%s\n%s\n```'
6363
local LINE_CHARACTERS = 100
64-
local BIG_FILE_THRESHOLD = 1000 * LINE_CHARACTERS
6564
local BIG_EMBED_THRESHOLD = 200 * LINE_CHARACTERS
66-
local TRUNCATED = '... (truncated)'
6765

6866
--- Resolve provider function
6967
---@param model string
@@ -103,16 +101,9 @@ end
103101

104102
--- Generate content block with line numbers, truncating if necessary
105103
---@param content string
106-
---@param threshold number: The threshold for truncation
107104
---@param start_line number?: The starting line number
108105
---@return string
109-
local function generate_content_block(content, threshold, start_line)
110-
local total_chars = #content
111-
if total_chars > threshold then
112-
content = content:sub(1, threshold)
113-
content = content .. '\n' .. TRUNCATED
114-
end
115-
106+
local function generate_content_block(content, start_line)
116107
if start_line ~= nil then
117108
local lines = vim.split(content, '\n')
118109
local total_lines = #lines
@@ -144,12 +135,7 @@ local function generate_selection_message(selection)
144135
if selection.start_line and selection.end_line then
145136
out = out .. string.format('Excerpt from %s, lines %s to %s:\n', filename, selection.start_line, selection.end_line)
146137
end
147-
out = out
148-
.. string.format(
149-
'```%s\n%s\n```',
150-
filetype,
151-
generate_content_block(content, BIG_FILE_THRESHOLD, selection.start_line)
152-
)
138+
out = out .. string.format('```%s\n%s\n```', filetype, generate_content_block(content, selection.start_line))
153139

154140
return {
155141
content = out,
@@ -167,7 +153,7 @@ local function generate_resource_messages(resources)
167153
return resource.data and resource.data ~= ''
168154
end)
169155
:map(function(resource)
170-
local content = generate_content_block(resource.data, BIG_FILE_THRESHOLD, 1)
156+
local content = generate_content_block(resource.data, 1)
171157

172158
return {
173159
content = string.format(RESOURCE_FORMAT, resource.name, resource.type, content),

lua/CopilotChat/config.lua

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
---@field model string?
1818
---@field tools string|table<string>|nil
1919
---@field sticky string|table<string>|nil
20+
---@field resource_processing boolean?
2021
---@field temperature number?
2122
---@field headless boolean?
2223
---@field callback nil|fun(response: string, source: CopilotChat.source)
@@ -57,10 +58,12 @@ return {
5758
tools = nil, -- Default tool or array of tools (or groups) to share with LLM (can be specified manually in prompt via @).
5859
sticky = nil, -- Default sticky prompt or array of sticky prompts to use at start of every new chat (can be specified manually in prompt via >).
5960

61+
resource_processing = false, -- Enable intelligent resource processing (skips unnecessary resources to save tokens)
62+
6063
temperature = 0.1, -- Result temperature
6164
headless = false, -- Do not write to chat buffer and use history (useful for using custom processing)
6265
callback = nil, -- Function called when full response is received
63-
remember_as_sticky = true, -- Remember model as sticky prompts when asking questions
66+
remember_as_sticky = true, -- Remember config as sticky prompts when asking questions
6467

6568
-- default selection
6669
selection = require('CopilotChat.select').visual,

lua/CopilotChat/init.lua

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -903,11 +903,15 @@ function M.ask(prompt, config)
903903
local ok, err = pcall(async.run, function()
904904
local selected_tools, resolved_resources, resolved_tools, prompt = M.resolve_functions(prompt, config)
905905
local selected_model, prompt = M.resolve_model(prompt, config)
906-
local query_ok, processed_resources = pcall(resources.process_resources, prompt, selected_model, resolved_resources)
907-
if query_ok then
908-
resolved_resources = processed_resources
909-
else
910-
log.warn('Failed to process resources', processed_resources)
906+
907+
if config.resource_processing then
908+
local query_ok, processed_resources =
909+
pcall(resources.process_resources, prompt, selected_model, resolved_resources)
910+
if query_ok then
911+
resolved_resources = processed_resources
912+
else
913+
log.warn('Failed to process resources', processed_resources)
914+
end
911915
end
912916

913917
prompt = vim.trim(prompt)

0 commit comments

Comments
 (0)