Replies: 2 comments
-
|
This is a cool idea, I just think it's too niche for me to merge. I'm looking at ways I can support these types of ideas without committing them into |
Beta Was this translation helpful? Give feedback.
-
|
This is a great point about keeping the core clean and general.
To address this, I'd pivot the proposal from a hardcoded feature to a general-purpose hook, allowing users to implement placeholders (or any other input transformation) without bloating the core. By adding a Proposed ImplementationI suggest adding In -- ...existing code...
opts = {
blank_prompt = "", -- The prompt to use when the user doesn't provide a prompt
-- ...existing code...
---@param ctx CodeCompanion.SystemPrompt.Context
---@return string
system_prompt = function(ctx)
-- ...existing code...
)
end,
---Function to resolve user input from vim.ui.input into the messages
---@param messages table List of message objects {role: string, content: string}
---@param input string|nil The raw string retrieved from vim.ui.input
resolve_input = function(messages, input)
if not input or input == "" then
return
end
table.insert(messages, {
role = constants.USER_ROLE,
content = input,
})
end,
},
-- ...existing code...In -- ...existing code...
local function create_chat(input)
if input then
if type(config.interactions.chat.opts.resolve_input) == "function" then
config.interactions.chat.opts.resolve_input(messages, input)
end
end
if type(opts.pre_hook) == "function" then
-- ...existing code...User-Land Example: Placeholder SubstitutionWith this hook, a user can easily implement the positional arguments logic in their own configuration: local cc_config = require("codecompanion.config")
local original_resolve_input = cc_config.interactions.chat.opts.resolve_input
require("codecompanion").setup({
interactions = {
chat = {
opts = {
resolve_input = function(messages, input)
if not input or input == "" then
return
end
-- positional arguments
local args = {}
for word in input:gmatch("%S+") do
table.insert(args, word)
end
-- replacements map
local replacements = {
["%$ARGUMENTS"] = input, -- Represents the full plural string
}
for i, val in ipairs(args) do
replacements["%$" .. i] = val
end
local replaced_any = false
-- iterate through existing messages to perform string substitution
for _, message in ipairs(messages) do
for pattern, replacement in pairs(replacements) do
local new_content, n = message.content:gsub(pattern, replacement)
if n > 0 then
message.content = new_content
replaced_any = true
end
end
end
-- if no placeholders were detected, fall back to default behavior
if not replaced_any then
original_resolve_input(messages, input)
end
end,
},
},
},
}) |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Motivation
Currently, when using Chat Interactions (user_prompt: true), user input from
vim.ui.inputis always appended as a new message at the end of themessagestable. While this works for simple prompts, it limits the ability to create complex "templates" where the user's input needs to be injected into a specific part of a pre-defined system or user prompt.I'm proposing a way to use placeholders within the interaction's messages to allow for more flexible templating.
It is similar to how Claude Code uses parameters in slash commands.
Proposed Change
The idea is to scan the existing messages for specific placeholders and replace them with the user's input:
$ARGUMENTS: Replaces with the full input string.$1,$2, etc.: Replaces with specific positional words from the input.Key Logic:
Example Usage
With this change, a user could define a prompt like this:
You could then for example use /gh-issue and introduce 1234 to have CC fix issue 1234.
Implementation Details
We could create a
process_argumentsfunction to handle this withinlua/codecompanion/interactions/init.lua:and integrated it into the chat interaction flow:
@olimorris , I have searched for similar discussions in the repository and found none, so I believe this is a good opportunity to enhance the flexibility of chat interactions. Or maybe there is already a way to do this and I don't know about it.
Beta Was this translation helpful? Give feedback.
All reactions