diff --git a/lua/codecompanion/actions.lua b/lua/codecompanion/actions.lua index 567ffc69..bbfa721e 100644 --- a/lua/codecompanion/actions.lua +++ b/lua/codecompanion/actions.lua @@ -341,11 +341,13 @@ M.static.actions = { role = "user", contains_code = true, content = function(context) - return "This the code:\n" - .. send_code(context) - .. "\nPlease add a documentation comment to the provided code and reply with just the comment only and no explanation, no codeblocks and do not return the code either. If neccessary add parameter and return types" + return send_code(context) end, }, + { + role = "user", + content = "Please add a documentation comment to the provided code and reply with just the comment only and no explanation, no codeblocks and do not return the code either. If neccessary add parameter and return types", + }, }, }, { @@ -369,11 +371,13 @@ M.static.actions = { role = "user", contains_code = true, content = function(context) - return "This is the code:\n" - .. send_code(context) - .. "\nPlease optimize the provided code. Please just respond with the code only and no explanation and no markdown codeblocks" + return send_code(context) end, }, + { + role = "user", + content = "Please optimize the provided code. Please just respond with the code only and no explanation or markdown block syntax", + }, }, }, { @@ -397,11 +401,13 @@ M.static.actions = { role = "user", contains_code = true, content = function(context) - return "This is the code:\n" - .. send_code(context) - .. "\nPlease create a unit test for the provided code. Please just respond with the code only and no explanation or markdown block syntax" + return send_code(context) end, }, + { + role = "user", + content = "Please create a unit test for the provided code. Please just respond with the code only and no explanation or markdown block syntax", + }, }, }, }, @@ -450,7 +456,6 @@ M.static.actions = { }, { role = "user", - contains_code = true, content = function(context) local diagnostics = require("codecompanion.helpers.lsp").get_diagnostics(context.start_line, context.end_line, context.bufnr) @@ -474,7 +479,13 @@ M.static.actions = { .. context.filetype .. ". This is a list of the diagnostic messages:\n\n" .. concatenated_diagnostics - .. "\n\nThis is the code, for context:\n\n" + end, + }, + { + role = "user", + contains_code = true, + content = function(context) + return "This is the code, for context:\n\n" .. "```" .. context.filetype .. "\n" diff --git a/lua/codecompanion/adapters/anthropic.lua b/lua/codecompanion/adapters/anthropic.lua index 1376f31d..4339ba2c 100644 --- a/lua/codecompanion/adapters/anthropic.lua +++ b/lua/codecompanion/adapters/anthropic.lua @@ -1,8 +1,8 @@ local log = require("codecompanion.utils.log") local function get_system_prompt(tbl) - for i, element in ipairs(tbl) do - if element.role == "system" then + for i = 1, #tbl do + if tbl[i].role == "system" then return i end end @@ -50,11 +50,30 @@ return { ---@param messages table Format is: { { role = "user", content = "Your prompt here" } } ---@return table form_messages = function(messages) + -- Remove any system prompts from the messages array local system_prompt_index = get_system_prompt(messages) if system_prompt_index then table.remove(messages, system_prompt_index) end + -- Combine user prompts into a single prompt + local user_role_content = {} + for i = #messages, 1, -1 do + local message = messages[i] + if message.role == "user" then + table.insert(user_role_content, 1, message.content) + table.remove(messages, i) + end + end + local output = table.concat(user_role_content, " ") + + if output ~= "" then + table.insert(messages, { + role = "user", + content = output, + }) + end + return { messages = messages } end,