Skip to content

Commit

Permalink
fix: inline actions in the action palette
Browse files Browse the repository at this point in the history
  • Loading branch information
olimorris committed Mar 13, 2024
1 parent 6235c42 commit b38b3dd
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 52 deletions.
25 changes: 9 additions & 16 deletions lua/codecompanion/actions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -341,13 +341,11 @@ M.static.actions = {
role = "user",
contains_code = true,
content = function(context)
return send_code(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"
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",
},
},
},
{
Expand All @@ -367,18 +365,15 @@ M.static.actions = {
.. " language"
end,
},

{
role = "user",
contains_code = true,
content = function(context)
return send_code(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"
end,
},
{
role = "user",
content = "Please optimize the provided code. Please just respond with the code only and no explanation or markdown block syntax",
},
},
},
{
Expand All @@ -402,13 +397,11 @@ M.static.actions = {
role = "user",
contains_code = true,
content = function(context)
return send_code(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"
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",
},
},
},
},
Expand Down
70 changes: 34 additions & 36 deletions lua/codecompanion/strategies/inline.lua
Original file line number Diff line number Diff line change
Expand Up @@ -258,51 +258,49 @@ end
---@param user_input string|nil
function Inline:execute(user_input)
if not adapter then
vim.notify("No adapter found for inline requests", vim.log.levels.ERROR)
return
end

local prompt = build_prompt(self, user_input)

local action = {
{
role = "system",
content = 'I am writing a prompt from within the Neovim text editor. This prompt will go to a generative AI model which will return a response. The response will then be streamed into Neovim. However, depending on the nature of the prompt, how Neovim handles the output will vary. For instance, if the user references the words "refactor" or "update" in their prompt, they will likely want the response to replace a visual selection they\'ve made in the editor. However, if they include words such as "after" or "before", it may be insinuated that they wish the response to be placed after or before the cursor position. They may also ask for the response to be placed in a new buffer. Finally, if you they don\'t specify what they wish to do, then they likely want to stream the response into where the cursor is currently. What I\'d like you to do is analyse the following prompt and determine whether the response should be one of: 1) after 2) before 3) replace 4) new 5) cursor. We\'ll call this the "placement" and please only respond with a single word. The user may not wish for their original code to be returned back to them from the generative AI model as part of the response. An example would be if they\'ve asked the model to generate comments or documentation. However if they\'ve asked for some refactoring/modification, then the original code should be returned. Please can you determine whether the code should be returned or not by responding with a boolean flag. Can you append this to the "placement" from earlier and seperate them with a "|" character? An example would be "cursor|true".',
},
{
role = "user",
content = 'The prompt to analyse is: "' .. user_input .. '"',
},
}

-- Assume the placement should be after the cursor
vim.api.nvim_buf_set_lines(self.context.bufnr, self.context.end_line, self.context.end_line, false, { "" })

local placement = ""
fire_autocmd("started")
client.new():stream(adapter:set_params(), action, self.context.bufnr, function(err, data, done)
if err then
fire_autocmd("finished")
return
end

if done then
log:trace("Placement: %s", placement)
get_inline_output(self, placement, prompt, {})
return
end
if not self.opts.placement then
local action = {
{
role = "system",
content = 'I am writing a prompt from within the Neovim text editor. This prompt will go to a generative AI model which will return a response. The response will then be streamed into Neovim. However, depending on the nature of the prompt, how Neovim handles the output will vary. For instance, if the user references the words "refactor" or "update" in their prompt, they will likely want the response to replace a visual selection they\'ve made in the editor. However, if they include words such as "after" or "before", it may be insinuated that they wish the response to be placed after or before the cursor position. They may also ask for the response to be placed in a new buffer. Finally, if you they don\'t specify what they wish to do, then they likely want to stream the response into where the cursor is currently. What I\'d like you to do is analyse the following prompt and determine whether the response should be one of: 1) after 2) before 3) replace 4) new 5) cursor. We\'ll call this the "placement" and please only respond with a single word. The user may not wish for their original code to be returned back to them from the generative AI model as part of the response. An example would be if they\'ve asked the model to generate comments or documentation. However if they\'ve asked for some refactoring/modification, then the original code should be returned. Please can you determine whether the code should be returned or not by responding with a boolean flag. Can you append this to the "placement" from earlier and seperate them with a "|" character? An example would be "cursor|true".',
},
{
role = "user",
content = 'The prompt to analyse is: "' .. user_input .. '"',
},
}

-- Assume the placement should be after the cursor
vim.api.nvim_buf_set_lines(self.context.bufnr, self.context.end_line, self.context.end_line, false, { "" })

local placement = ""
fire_autocmd("started")
client.new():stream(adapter:set_params(), action, self.context.bufnr, function(err, data, done)
if err then
fire_autocmd("finished")
return
end

if data then
placement = placement .. (adapter.callbacks.inline_output(data) or "")
end
end)
if done then
log:trace("Placement: %s", placement)
get_inline_output(self, placement, prompt, {})
return
end

if not adapter then
vim.notify("No adapter found for inline requests", vim.log.levels.ERROR)
if data then
placement = placement .. (adapter.callbacks.inline_output(data) or "")
end
end)
else
get_inline_output(self, self.opts.placement, prompt, {})
return
end

--Make initial call to determine the action to take:
--Within this then call the stream function
end

---@param user_input? string
Expand Down

0 comments on commit b38b3dd

Please sign in to comment.