Skip to content

Commit

Permalink
refactor: move user and llm names to constants in config
Browse files Browse the repository at this point in the history
  • Loading branch information
olimorris committed Oct 9, 2024
1 parent dba83b9 commit 8f13002
Show file tree
Hide file tree
Showing 14 changed files with 84 additions and 84 deletions.
4 changes: 2 additions & 2 deletions lua/cmp_codecompanion/slash_commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ function source:execute(item, callback)
if item.from_prompt_library then
local prompts = strategy.evaluate_prompts(item.config.prompts, item.context)
vim.iter(prompts):each(function(prompt)
if prompt.role == "system" then
if prompt.role == config.constants.SYSTEM_ROLE then
item.Chat:add_message(prompt, { visible = false })
elseif prompt.role == "user" then
elseif prompt.role == config.constants.USER_ROLE then
item.Chat:append_to_buf(prompt)
end
end)
Expand Down
29 changes: 15 additions & 14 deletions lua/codecompanion/actions/static.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local codecompanion = require("codecompanion")
local config = require("codecompanion.config")

local function send_code(context)
local text = require("codecompanion.helpers.actions").get_code(context.start_line, context.end_line)
Expand All @@ -22,15 +23,15 @@ return {
end,
v = {
{
role = "system",
role = config.constants.SYSTEM_ROLE,
content = function(context)
return "I want you to act as a senior "
.. context.filetype
.. " developer. I will give you specific code examples and ask you questions. I want you to advise me with explanations and code examples."
end,
},
{
role = "user",
role = config.constants.USER_ROLE,
content = function(context)
return send_code(context)
end,
Expand Down Expand Up @@ -95,7 +96,7 @@ return {
})
:workflow({
{
role = "system",
role = config.constants.SYSTEM_ROLE,
content = "You carefully provide accurate, factual, thoughtful, nuanced answers, and are brilliant at reasoning. If you think there might not be a correct answer, you say so. Always spend a few sentences explaining background context, assumptions, and step-by-step thinking BEFORE you try to answer a question. Don't be verbose in your answers, but do provide details and examples where it might help the explanation. You are an expert software engineer for the "
.. context.filetype
.. " language.",
Expand All @@ -107,36 +108,36 @@ return {
condition = function()
return context.is_visual
end,
role = "user",
role = config.constants.USER_ROLE,
content = "Here is some relevant context: " .. send_code(context),
opts = {
contains_code = true,
start = true,
},
},
{
role = "user",
role = config.constants.USER_ROLE,
content = "I want you to help me code a feature. Before we write any code let's outline how we'll architect and implement the feature with the context you already have. The feature I'd like to add is ",
opts = {
start = true,
},
},
{
role = "user",
role = config.constants.USER_ROLE,
content = "Thanks. Now let's draft the code for the feature.",
opts = {
auto_submit = true,
},
},
{
role = "user",
role = config.constants.USER_ROLE,
content = "Great. Now let's consider the code. I'd like you to check it carefully for correctness, style, and efficiency, and give constructive criticism for how to improve it.",
opts = {
auto_submit = true,
},
},
{
role = "user",
role = config.constants.USER_ROLE,
content = "Thanks. Now let's revise the code based on the feedback, without additional explanations.",
opts = {
auto_submit = true,
Expand All @@ -156,7 +157,7 @@ return {
})
:workflow({
{
role = "system",
role = config.constants.SYSTEM_ROLE,
content = "You carefully provide accurate, factual, thoughtful, nuanced answers, and are brilliant at reasoning. If you think there might not be a correct answer, you say so. Always spend a few sentences explaining background context, assumptions, and step-by-step thinking BEFORE you try to answer a question. Don't be verbose in your answers, but do provide details and examples where it might help the explanation. You are an expert software engineer for the "
.. context.filetype
.. " language.",
Expand All @@ -168,36 +169,36 @@ return {
condition = function()
return context.is_visual
end,
role = "user",
role = config.constants.USER_ROLE,
content = "Here is some relevant context: " .. send_code(context),
opts = {
contains_code = true,
start = true,
},
},
{
role = "user",
role = config.constants.USER_ROLE,
content = "I want you to help me with a refactor. Before we write any code let's outline how we'll architect and implement the code with the context you already have. What I'm looking to achieve is ",
opts = {
start = true,
},
},
{
role = "user",
role = config.constants.USER_ROLE,
content = "Thanks. Now let's draft the code for the refactor.",
opts = {
auto_submit = true,
},
},
{
role = "user",
role = config.constants.USER_ROLE,
content = "Great. Now let's consider the code. I'd like you to check it carefully for correctness, style, and efficiency, and give constructive criticism for how to improve it.",
opts = {
auto_submit = true,
},
},
{
role = "user",
role = config.constants.USER_ROLE,
content = "Thanks. Now let's revise the code based on the feedback, without additional explanations.",
opts = {
auto_submit = true,
Expand Down
36 changes: 21 additions & 15 deletions lua/codecompanion/config.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
local fmt = string.format

local constants = {
LLM_ROLE = "llm",
USER_ROLE = "user",
SYSTEM_ROLE = "system",
}

local defaults = {
adapters = {
-- LLMs -------------------------------------------------------------------
Expand Down Expand Up @@ -304,7 +310,7 @@ Points to note:
},
prompts = {
{
role = "system",
role = constants.SYSTEM_ROLE,
content = function(context)
return fmt(
[[I want you to act as a senior %s developer. I will ask you specific questions and I want you to return raw code only (no codeblocks and no explanations). If you can't respond with code, respond with nothing]],
Expand Down Expand Up @@ -333,7 +339,7 @@ Points to note:
},
prompts = {
{
role = "system",
role = constants.SYSTEM_ROLE,
content = [[When asked to explain code, follow these steps:
1. Identify the programming language.
Expand All @@ -346,7 +352,7 @@ Points to note:
},
},
{
role = "user",
role = constants.USER_ROLE,
content = function(context)
local code = require("codecompanion.helpers.actions").get_code(context.start_line, context.end_line)

Expand Down Expand Up @@ -383,7 +389,7 @@ Points to note:
},
prompts = {
{
role = "system",
role = constants.SYSTEM_ROLE,
content = [[When generating unit tests, follow these steps:
1. Identify the programming language.
Expand All @@ -400,7 +406,7 @@ Points to note:
},
},
{
role = "user",
role = constants.USER_ROLE,
content = function(context)
local code = require("codecompanion.helpers.actions").get_code(context.start_line, context.end_line)

Expand Down Expand Up @@ -437,7 +443,7 @@ Points to note:
},
prompts = {
{
role = "system",
role = constants.SYSTEM_ROLE,
content = [[When asked to fix code, follow these steps:
1. **Identify the Issues**: Carefully read the provided code and identify any potential issues or improvements.
Expand All @@ -458,7 +464,7 @@ Use Markdown formatting and include the programming language name at the start o
},
},
{
role = "user",
role = constants.USER_ROLE,
content = function(context)
local code = require("codecompanion.helpers.actions").get_code(context.start_line, context.end_line)

Expand Down Expand Up @@ -495,7 +501,7 @@ Use Markdown formatting and include the programming language name at the start o
},
prompts = {
{
role = "system",
role = constants.SYSTEM_ROLE,
content = function(context)
return "I want you to act as a senior "
.. context.filetype
Expand All @@ -507,7 +513,7 @@ Use Markdown formatting and include the programming language name at the start o
},
},
{
role = "user",
role = constants.USER_ROLE,
content = function(context)
local buf_utils = require("codecompanion.utils.buffers")

Expand All @@ -519,7 +525,7 @@ Use Markdown formatting and include the programming language name at the start o
},
},
{
role = "user",
role = constants.USER_ROLE,
condition = function(context)
return context.is_visual
end,
Expand Down Expand Up @@ -560,14 +566,14 @@ Use Markdown formatting and include the programming language name at the start o
},
prompts = {
{
role = "system",
role = constants.SYSTEM_ROLE,
content = [[You are an expert coder and helpful assistant who can help debug code diagnostics, such as warning and error messages. When appropriate, give solutions with code snippets as fenced codeblocks with a language identifier to enable syntax highlighting.]],
opts = {
visible = false,
},
},
{
role = "user",
role = constants.USER_ROLE,
content = function(context)
local diagnostics = require("codecompanion.helpers.actions").get_diagnostics(
context.start_line,
Expand Down Expand Up @@ -603,7 +609,7 @@ Use Markdown formatting and include the programming language name at the start o
end,
},
{
role = "user",
role = constants.USER_ROLE,
content = function(context)
local code = require("codecompanion.helpers.actions").get_code(
context.start_line,
Expand Down Expand Up @@ -640,7 +646,7 @@ This is the code, for context:
},
prompts = {
{
role = "user",
role = constants.USER_ROLE,
content = function()
return fmt(
[[You are an expert at following the Conventional Commit specification. Given the git diff listed below, please generate a commit message for me:
Expand Down Expand Up @@ -768,7 +774,7 @@ local M = {
---@param args? table
M.setup = function(args)
args = args or {}
M.config = vim.tbl_deep_extend("force", vim.deepcopy(defaults), args)
M.config = vim.tbl_deep_extend("force", vim.deepcopy(defaults), { constants = vim.deepcopy(constants) }, args)
end

return setmetatable(M, {
Expand Down
2 changes: 1 addition & 1 deletion lua/codecompanion/helpers/slash_commands/buffer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ local function output(SlashCommand, selected)

local Chat = SlashCommand.Chat
Chat:add_message({
role = "user",
role = config.constants.USER_ROLE,
content = string.format(
[[Here is the content from %s (which has a buffer number of _%d_ and a filepath of `%s`):
Expand Down
3 changes: 2 additions & 1 deletion lua/codecompanion/helpers/slash_commands/fetch.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
local adapters = require("codecompanion.adapters")
local client = require("codecompanion.http")
local config = require("codecompanion.config")

local log = require("codecompanion.utils.log")
local util = require("codecompanion.utils.util")
Expand Down Expand Up @@ -79,7 +80,7 @@ function SlashCommand:execute()
)

self.Chat:add_message({
role = "user",
role = config.constants.USER_ROLE,
content = content,
}, { visible = false })

Expand Down
2 changes: 1 addition & 1 deletion lua/codecompanion/helpers/slash_commands/file.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ local function output(SlashCommand, selected)
local Chat = SlashCommand.Chat
local relative_path = selected.relative_path or selected[1] or selected.path
Chat:add_message({
role = "user",
role = config.constants.USER_ROLE,
content = string.format(
[[Here is the content from the file `%s`:
Expand Down
2 changes: 1 addition & 1 deletion lua/codecompanion/helpers/slash_commands/help.lua
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ local function output(SlashCommand, selected)

local Chat = SlashCommand.Chat
Chat:add_message({
role = "user",
role = config.constants.USER_ROLE,
content = string.format(
[[Here is some additional context related to the tag `%s`:
Expand Down
2 changes: 1 addition & 1 deletion lua/codecompanion/helpers/slash_commands/symbols.lua
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ function SlashCommand:execute()

local content = table.concat(symbols, "\n")
Chat:add_message({
role = "user",
role = config.constants.USER_ROLE,
content = string.format(
[[Here is a symbolic outline of the file `%s` with filetype `%s`:
Expand Down
3 changes: 2 additions & 1 deletion lua/codecompanion/helpers/slash_commands/terminal.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
local config = require("codecompanion.config")
local util = require("codecompanion.utils.util")

CONSTANTS = {
Expand Down Expand Up @@ -31,7 +32,7 @@ function SlashCommand:execute()

local Chat = self.Chat
Chat:add_message({
role = "user",
role = config.constants.USER_ROLE,
content = string.format(
[[Here is the terminal output for buffer number `%s`:
Expand Down
6 changes: 3 additions & 3 deletions lua/codecompanion/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ M.inline = function(args)
context = context,
prompts = {
{
role = "system",
role = config.constants.SYSTEM_ROLE,
content = function()
return "I want you to act as a senior "
.. context.filetype
Expand Down Expand Up @@ -102,7 +102,7 @@ M.add = function(args)
local content = table.concat(context.lines, "\n")

chat:append_to_buf({
role = "user",
role = config.constants.USER_ROLE,
content = "Here is some code from "
.. context.filename
.. ":\n\n```"
Expand Down Expand Up @@ -134,7 +134,7 @@ M.chat = function(args)
return M.toggle()
else
table.insert(messages, {
role = "user",
role = config.constants.USER_ROLE,
content = args.args,
})
end
Expand Down
2 changes: 1 addition & 1 deletion lua/codecompanion/strategies.lua
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ function Strategies:chat()
local function chat(input)
if input then
table.insert(messages, {
role = "user",
role = config.constants.USER_ROLE,
content = input,
})
end
Expand Down
Loading

0 comments on commit 8f13002

Please sign in to comment.