Files
nvim/lua/ai.lua
T
jannikdonkerandClaude Opus 5.5 fb8cdc0e1c start codecompanion chats and CLI agents in auto mode
Claude Code and Antigravity chats open in the agent's own auto mode,
with CodeCompanion's approval mode on auto too. The CLI terminals run
`claude --permission-mode auto` and `agy --mode auto`. Inline stays as
is: it never prompts, and the agent's permission requests are what keep
it editing only the buffer copy.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 17:26:13 +01:00

210 lines
10 KiB
Lua

-- CodeCompanion: AI chat / inline edits backed by agents over ACP (Agent
-- Client Protocol), so both use their own CLI login. No API keys needed.
-- claude_code -> `claude-agent-acp` (npm i -g @agentclientprotocol/claude-agent-acp),
-- which reuses the `claude` CLI login.
-- antigravity -> `agy acp` (Antigravity CLI: Gemini, plus Claude/GPT-OSS via
-- Google). Uses the `agy` Google login.
-- Chat uses Claude Code, inline uses Antigravity (Gemini 4 Hydrogen, high).
-- Agents read their own CLAUDE.md / GEMINI.md / AGENTS.md and settings, so
-- model, permissions and MCP servers come from the CLI configs unless pinned
-- here (only inline is).
-- In a chat: `ga` switches agent/model, `ge` effort, `?` lists all keymaps.
-- The winbar shows the current agent · model · effort.
-- The ACP module directly: the generic adapters.extend() decides http vs acp
-- by looking the name up in the config, and gemini_cli is hidden from it.
local acp = require("codecompanion.adapters.acp")
-- Antigravity (`agy acp`). There's no built-in adapter for it yet; its ACP
-- server speaks the same dialect as Gemini CLI (same auth method ids), so
-- reuse that adapter with a different command. `session_options` pins
-- model/effort/mode for new sessions (values match ids or display names).
local function antigravity(name, session_options)
return acp.extend("gemini_cli", {
name = name, -- must equal the key under adapters.acp
formatted_name = "Antigravity",
commands = {
default = { "agy", "acp" },
yolo = { "agy", "acp", "--dangerously-skip-permissions" },
},
defaults = {
auth_method = "oauth-personal", -- Google login
session_config_options = session_options,
},
})
end
-- Effort ("thought_level" in ACP terms; both agents report it under that
-- category). Opens a picker with just the effort levels of the current model,
-- the same one `/acp_session_options` shows after you pick "Effort" there.
-- Uses that slash command's module, which isn't public API (fine while
-- pinned to ^20).
local function pick_effort(chat)
local conn = chat.acp_connection
if not conn then
return vim.notify("Effort: only agent (ACP) chats have it", vim.log.levels.WARN)
end
for _, opt in ipairs(conn:get_config_options()) do
if opt.category == "thought_level" then
return require("codecompanion.interactions.chat.slash_commands.builtin.acp_session_options")
.new({ Chat = chat })
:show_values(opt)
end
end
vim.notify("Effort: the agent hasn't reported one (still connecting, or this model has none)",
vim.log.levels.WARN)
end
-- Display name of an ACP chat's current value for an option category, read
-- live from the agent connection. (The metadata CodeCompanion hands the
-- winbar caches its value->name map before the agent has sent its options,
-- so it only has raw ids there, e.g. Claude's model is just "default".)
local function acp_option_name(md, category)
local bufnr
for b, m in pairs(_G.codecompanion_chat_metadata or {}) do
if m == md then bufnr = b end
end
local chat = bufnr and require("codecompanion").buf_get_chat(bufnr)
local conn = chat and chat.acp_connection
if not conn then return nil end
for _, opt in ipairs(conn:get_config_options()) do
if opt.category == category and opt.currentValue then
for _, item in ipairs(opt.options or {}) do
for _, v in ipairs(item.options or { item }) do -- items may be grouped
if v.value == opt.currentValue then return v.name end
end
end
return opt.currentValue
end
end
end
-- Chat winbar: title on the left, `agent · model · effort` on the right.
local function chat_winbar(md)
local title = (md.title or "CodeCompanion"):gsub("%%", "%%%%")
local parts = { md.adapter.name }
local model = acp_option_name(md, "model") or md.adapter.model -- HTTP: model id
if model then table.insert(parts, model) end
local effort = acp_option_name(md, "thought_level")
if effort then table.insert(parts, "effort " .. effort:lower()) end
local right = table.concat(parts, " · "):gsub("%%", "%%%%")
return string.format("%%#CodeCompanionChatHeader# %s%%*%%=%%#Comment#%s ", title, right)
end
require("codecompanion").setup({
adapters = {
acp = {
-- List only the two agents below in the adapter picker.
opts = { show_presets = false },
-- Chat agents start in their own auto mode: the agent approves
-- routine actions itself and only asks about risky ones.
-- (Claude falls back to its default if a model lacks auto.)
claude_code = function()
return acp.extend("claude_code", {
defaults = { session_config_options = { mode = "auto" } },
})
end,
antigravity = function() return antigravity("antigravity", { mode = "auto" }) end,
-- Inline edits: Gemini 4 Hydrogen at high effort. A separate
-- adapter because inline can only pin a model, not effort, and
-- setting effort on `antigravity` would change chats too. Hidden
-- from the `ga` picker below. No auto mode here on purpose:
-- inline never prompts you anyway (CodeCompanion answers the
-- agent's permission requests itself), and those requests are
-- how it keeps the agent to editing only the buffer copy. An
-- agent in auto mode would skip them.
antigravity_inline = function()
return antigravity("antigravity_inline", {
model = "gemini-4-hydrogen",
thought_level = "high",
})
end,
},
-- Merged into the default hidden list. All HTTP adapters get hidden
-- after setup() below; this one is an agent, so it's listed here.
http = { opts = { hidden = { antigravity_inline = true } } },
},
interactions = {
chat = {
adapter = "claude_code",
-- CodeCompanion's own approval layer (`gty` to change per chat).
-- Auto: the agent's reads, searches, edits and fetches go through;
-- shell commands outside tools.run_command.opts.safe_commands
-- (git status, ls, pwd) and anything else (e.g. deletes) still ask.
tools = { opts = { approval_mode = "auto" } },
keymaps = {
-- `ga` already switches agent + model; `ge` (normally "end of
-- previous word", only overridden in the chat buffer) switches
-- effort. Everything else: `/acp_session_options` (mode etc.).
change_effort = {
modes = { n = "ge" },
callback = pick_effort,
description = "Change effort (reasoning level)",
},
},
},
-- ACP agents edit a temp copy of the buffer and hand back a diff.
-- `ga` in the :CodeCompanion input box overrides this per buffer.
inline = { adapter = "antigravity_inline" },
-- These two only work with HTTP adapters (plain API calls), not
-- agents. They need ANTHROPIC_API_KEY; without it they stay unused
-- (background tasks are off by default; :CodeCompanionCmd errors).
-- The default would be Copilot, which isn't set up here.
cmd = { adapter = { name = "anthropic", model = "claude-haiku-5-5" } },
background = { adapter = { name = "anthropic", model = "claude-haiku-5-5" } },
-- The full CLIs in a terminal split, with context sharing from
-- buffers (`:CodeCompanionCLI`, `:CodeCompanionCLI agent=antigravity`).
cli = {
agent = "claude_code",
agents = {
claude_code = {
cmd = "claude", -- must stay "claude" for :CodeCompanionCLI Install hooks
args = { "--permission-mode", "auto" },
description = "Claude Code CLI",
},
antigravity = { cmd = "agy", args = { "--mode", "auto" }, description = "Antigravity CLI" },
},
},
},
display = {
chat = {
window = { position = "right", width = 0.4 },
-- Winbar instead of a heading in the buffer, so it stays visible.
title = { position = "winbar", text = { winbar = chat_winbar } },
},
},
})
-- The `ga` picker shows only Claude Code and Antigravity: hide every
-- built-in HTTP (API-key) provider, including ones added in later releases.
-- The hidden list is read whenever the picker opens, and hidden adapters
-- still work by name (cmd/background use anthropic, web tools duckduckgo).
local http_adapters = require("codecompanion.config").adapters.http
for name in pairs(http_adapters) do
if name ~= "opts" and name ~= "extend" then http_adapters.opts.hidden[name] = true end
end
-- Progress float while an inline edit runs (see lua/ai_status.lua).
require("ai_status").setup()
-- Keymaps: everything under <leader>a. Visual-mode maps use `:` rather than
-- <cmd> so the selection is passed as a range ('<,'>).
local map = vim.keymap.set
map("n", "<leader>aa", "<cmd>CodeCompanionChat Toggle<cr>", { desc = "AI: toggle chat" })
map("x", "<leader>aa", ":CodeCompanionChat Add<cr>", { silent = true, desc = "AI: add selection to chat" })
map("n", "<leader>ac", "<cmd>CodeCompanionChat adapter=claude_code<cr>", { desc = "AI: new Claude Code chat" })
map("n", "<leader>ag", "<cmd>CodeCompanionChat adapter=antigravity<cr>", { desc = "AI: new Antigravity chat" })
map({ "n", "x" }, "<leader>ai", ":CodeCompanion ", { desc = "AI: inline edit (type prompt)" })
map("n", "<leader>ap", "<cmd>CodeCompanionActions<cr>", { desc = "AI: action palette" })
map("x", "<leader>ap", ":CodeCompanionActions<cr>", { silent = true, desc = "AI: action palette (selection)" })
map("n", "<leader>at", "<cmd>CodeCompanionCLI<cr>", { desc = "AI: CLI agent in terminal" })
map("x", "<leader>at", ":CodeCompanionCLI<cr>", { silent = true, desc = "AI: send selection to CLI agent" })
map("n", "<leader>ar", "<cmd>CodeCompanionCodeReview<cr>", { desc = "AI: review agent changes" })