Skip to content
Draft
27 changes: 25 additions & 2 deletions .codecompanion/chat.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Messages in the chat buffer are lua table objects as seen. They contain the role
_meta = {
cycle = 1,
id = 708950413,
estimated_tokens = 20,
tag = "system_prompt_from_config",
},
opts = {
Expand All @@ -25,15 +26,37 @@ Messages in the chat buffer are lua table objects as seen. They contain the role
}, {
_meta = {
cycle = 1,
estimated_tokens = 20,
id = 533315931,
sent = true
},
opts = {
visible = true
},
role = "user"
content = "Are you working?",
}, {
content = "Are you working? Sharing a file with you",
},
{
_meta = {
cycle = 1,
estimated_tokens = 3556,
id = 1048633318,
index = 5,
sent = true,
source = "editor_context",
tag = "file"
},
content = "<attachment filepath=\"/Users/Oli/some_path/some_file.lua\">An example file</attachment>",
context = {
id = "<file>some_path/some_file.lua</file>",
path = "/Users/Oli/some_path/some_file.lua",
},
opts = {
visible = false
},
role = "user"
},
{
_meta = {
cycle = 1,
id = 1141409506,
Expand Down
1 change: 1 addition & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ This is a Neovim plugin written in Lua, which allows developers to code with LLM
- **Naming:** snake_case for files/functions, PascalCase for classes, underscore prefix for private functions
- **Explicit names:** `pattern` not `pat`, `should_include` not `include_ok`
- **Readable code:** names, variables, and control flow should read like clean English. Avoid generic names like `ctx` — use domain-specific names (`permission`, `request`, `source`)
- **Plain language:** avoid jargon shortcuts in code, comments, commit messages, and chat. Don't say "no-op" — say what the code actually does ("returns unchanged", "does nothing", "skipped because already edited")
- **Function params:** prefer a single table argument over positional args
- **Error handling:** `pcall` + `log:error()`, return nil on failure
- **Type annotations:** LuaCATS for public APIs. Keep doc blocks concise — one description line, params should be self-explanatory without inline comments
Expand Down
47 changes: 36 additions & 11 deletions lua/codecompanion/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -694,17 +694,24 @@ If you are providing code changes, use the insert_edit_into_file tool (if availa
},
opts = {
context_management = {
trigger = 0.75, -- Compaction starts at 75% of the context window limit
enabled = function(adapter)
if adapter.type ~= "http" then
return false
end
-- Anthropic and OpenAI have their own server-side compaction
if adapter.vendor and (adapter.vendor == "anthropic" or adapter.vendor == "openai") then
return false
end
return true
end,
---@type boolean|fun(adapter: CodeCompanion.HTTPAdapter|CodeCompanion.ACPAdapter): boolean
enabled = true,

editing = {
trigger = 0.65, -- 65% of the context window
exclude_tools = { "memory" }, -- tools whose result are never edited
keep_cycles = 3, -- preserve tool results from the last N cycles
},

compaction = {
trigger = 0.85, -- 85% of the context window

---The adapter to use for compaction. Defaults to the current chat adapter
---@type nil|string|{ name: string, model:string }
adapter = nil,

fallback_to_chat_adapter = false, -- on failure, retry with the chat adapter?
},
},

blank_prompt = "", -- The prompt to use when the user doesn't provide a prompt
Expand Down Expand Up @@ -1355,6 +1362,24 @@ M.setup = function(args)
M.config.interactions.chat.editor_context = nil
end

-- TODO: Deprecate in v20.0.0 and remove in v21.0.0
-- Legacy `context_management.trigger` migrates to `context_management.compaction.trigger`
local context_management = args.interactions
and args.interactions.chat
and args.interactions.chat.opts
and args.interactions.chat.opts.context_management
if context_management and context_management.trigger ~= nil then
vim.notify(
"[CodeCompanion] `context_management.trigger` is deprecated. Use `context_management.compaction.trigger` instead.",
vim.log.levels.WARN,
{ title = "CodeCompanion" }
)
if not (context_management.compaction and context_management.compaction.trigger ~= nil) then
M.config.interactions.chat.opts.context_management.compaction.trigger = context_management.trigger
end
M.config.interactions.chat.opts.context_management.trigger = nil
end

M.config.interactions.chat.keymaps = remove_disabled_keymaps(M.config.interactions.chat.keymaps)
M.config.interactions.cli.keymaps = remove_disabled_keymaps(M.config.interactions.cli.keymaps)
M.config.interactions.inline.keymaps = remove_disabled_keymaps(M.config.interactions.inline.keymaps)
Expand Down
2 changes: 1 addition & 1 deletion lua/codecompanion/interactions/chat/buffer_diffs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ local diff = vim.text.diff or vim.diff

---@class CodeCompanion.BufferDiffs
---@field buffers table<number, CodeCompanion.BufferDiffs.State> Map of buffer numbers to their states
---@field augroup integer The autocmd group ID
---@field augroup number The autocmd group ID
---@field sync fun(self: CodeCompanion.BufferDiffs, bufnr: number): nil Start syncing a buffer
---@field unsync fun(self: CodeCompanion.BufferDiffs, bufnr: number): nil Stop syncing a buffer
---@field get_changes fun(self: CodeCompanion.BufferDiffs, bufnr: number): boolean, table
Expand Down
Loading
Loading