Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions lua/avante/llm.lua
Original file line number Diff line number Diff line change
Expand Up @@ -262,11 +262,14 @@ function M.generate_prompts(opts)
local project_root = Utils.root.get()
local instruction_file_path = PPath:new(project_root, project_instruction_file)

if instruction_file_path:exists() then
if instruction_file_path:exists() and not opts._instructions_loaded then
local lines = Utils.read_file_from_buf_or_disk(instruction_file_path:absolute())
local instruction_content = lines and table.concat(lines, "\n") or ""

if instruction_content then opts.instructions = (opts.instructions or "") .. "\n" .. instruction_content end
if instruction_content then
opts.instructions = (opts.instructions or "") .. "\n" .. instruction_content
end
opts._instructions_loaded = true
end

local mode = opts.mode or Config.mode
Expand Down Expand Up @@ -438,8 +441,9 @@ function M.generate_prompts(opts)
:filter(function(msg) return type(msg.content) ~= "string" or msg.content ~= "" end)
:totable()

if opts.instructions ~= nil and opts.instructions ~= "" then
if opts.instructions ~= nil and opts.instructions ~= "" and not opts._instructions_added_to_messages then
messages = vim.list_extend(messages, { { role = "user", content = opts.instructions } })
opts._instructions_added_to_messages = true
end

opts.session_ctx = opts.session_ctx or {}
Expand Down
43 changes: 43 additions & 0 deletions tests/llm_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,47 @@ describe("generate_prompts", function()
assert.are.same(#result.tools, 1)
assert.are.same(result.tools[1].name, "test_tool")
end)

it("should not duplicate instruction file content when called multiple times with same opts", function()
local opts = {}
llm.generate_prompts(opts)
local first_instructions = opts.instructions

-- Call again with the same opts object
llm.generate_prompts(opts)
local second_instructions = opts.instructions

-- Instructions should be the same, not duplicated
assert.are.same(first_instructions, second_instructions)
-- Verify that mock content is present (more flexible than hardcoded exact match)
assert.truthy(string.find(opts.instructions, "Mock Instructions"))
end)

it("should not duplicate instructions in messages when called multiple times with same opts", function()
local opts = {
instructions = "Test instructions"
}

-- First call
local result1 = llm.generate_prompts(opts)
local instruction_message_count1 = 0
for _, msg in ipairs(result1.session_ctx.messages) do
if msg.role == "user" and msg.content == "Test instructions" then
instruction_message_count1 = instruction_message_count1 + 1
end
end

-- Second call with same opts
local result2 = llm.generate_prompts(opts)
local instruction_message_count2 = 0
for _, msg in ipairs(result2.session_ctx.messages) do
if msg.role == "user" and msg.content == "Test instructions" then
instruction_message_count2 = instruction_message_count2 + 1
end
end

-- Should have instructions message only once in both calls
assert.are.same(1, instruction_message_count1)
assert.are.same(1, instruction_message_count2)
end)
end)