Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,43 @@ require('aibo').setup({
-- completion = { claude = true },
-- },
},

-- Live "/" completion sources, one key per tool. Each source probes the
-- agent's own live command/skill list -- no static table, no disk scan --
-- so it needs no manual updates and can't drift out of sync. No prompt is
-- ever sent, so it consumes no tokens.
completion = {
-- Claude does not speak ACP itself; probes `claude` directly. Needs
-- nothing beyond a logged-in `claude` already on PATH -- no adapter,
-- no Node.js, no npm. On by default: there is no static fallback, so
-- setting this to `false` means no "/" completion at all for Claude.
claude = true,
-- claude = {
-- cmd = { "claude" }, -- command to probe, looked up on PATH
-- timeout = 10000, -- probe timeout (ms)
-- },
-- claude = false, -- disable "/" completion for Claude entirely

-- Codex does not speak ACP itself; probes `codex app-server` directly.
-- Needs nothing beyond `codex` already on PATH. On by default: there is
-- no static fallback, so `false` means no "/" completion for Codex.
codex = true,
-- codex = {
-- cmd = { "codex" },
-- timeout = 10000,
-- },
-- codex = false, -- disable "/" completion for Codex entirely

-- Generic Agent Client Protocol (ACP) client, for agents that speak ACP
-- natively. Currently used by Gemini CLI (`gemini --acp`). On by
-- default: same reasoning as Claude/Codex -- no static fallback.
acp = true,
-- acp = {
-- cmd = { "gemini", "--acp" },
-- timeout = 10000,
-- },
-- acp = false, -- disable "/" completion for Gemini entirely
},
})
```

Expand Down Expand Up @@ -404,6 +441,8 @@ require('aibo').setup({
> [!NOTE]
>
> `<C-g><C-o>` enters a special mode where you can press any single key to send it to the terminal. Useful for sending arbitrary keys not mapped by default.
>
> `<C-g>i` (or `<C-g><C-i>`) enters Direct mode, where every key you press is forwarded to the terminal until you press `<Esc>`. `<Esc>` itself is never forwarded — press `<C-c>` while in Direct mode to send ESC to the tool. While active, a 5-line window with a thick red border appears at the top of the console explaining the mode; the prompt window (if shown) is hidden for the duration and reopened in the same state afterwards.

### Console Buffer

Expand All @@ -422,6 +461,7 @@ Most keys use the `<Plug>(aibo-send)<Key>` pattern to send keys directly to the
| `<Down>` | Send down arrow | `<Plug>(aibo-send)<Down>` |
| `<Up>` | Send up arrow | `<Plug>(aibo-send)<Up>` |
| `<C-g><C-o>` | Send any single key (n) | `<Plug>(aibo-send)` |
| `<C-g>i` / `<C-g><C-i>` | Enter Direct mode (n) | `<Plug>(aibo-direct)` |

### Prompt Buffer

Expand All @@ -434,6 +474,7 @@ Most keys use the `<Plug>(aibo-send)<Key>` pattern to send keys directly to the
| `<C-p>` | Previous history or completion (i) | `<Plug>(aibo-history-prev)` |
| `<C-n>` | Next history or completion (i) | `<Plug>(aibo-history-next)` |
| `<C-g><C-o>` | Send any single key (n/i) | `<Plug>(aibo-send)` |
| `<C-g>i` / `<C-g><C-i>` | Enter Direct mode (n/i) | `<Plug>(aibo-direct)` |

> [!NOTE]
> `<C-p>` and `<C-n>` in insert mode intelligently switch between history navigation and completion menu navigation. When the completion popup menu is visible, they navigate the completion menu. When the popup is not visible, they navigate through your prompt history.
Expand Down
23 changes: 22 additions & 1 deletion doc/aibo.txt
Original file line number Diff line number Diff line change
Expand Up @@ -624,17 +624,27 @@ Console buffer mappings:
<Down> Send down arrow (n)
<Up> Send up arrow (n)
<C-g><C-o> Send any single key (n)
<C-g>i Enter Direct mode (n, also <C-g><C-i>)

Prompt buffer mappings:
<CR> Submit content (n)
<Esc> Close prompt window (n)
<C-Enter> Submit and close (n/i)
<F5> Submit and close (n/i)
<C-g><C-o> Send any single key (n/i)
<C-g>i Enter Direct mode (n/i, also <C-g><C-i>)

Note: Press <C-g><C-o> followed by any key to send that key to the terminal.
This is useful for sending keys not mapped by default.

Note: Press <C-g>i (or <C-g><C-i>) to enter Direct mode, where every key you
press is forwarded to the terminal until you press <Esc>. <Esc> itself is
never forwarded to the tool; press <C-c> while in Direct mode if you need to
send ESC. While active, a 5-line window with a thick red border is shown at
the top of the console explaining the mode. If the prompt window is
visible it is hidden for the duration and reopened in the same Normal or
Insert state once Direct mode ends.

Claude tool mappings:
<Tab> Toggle think (n/i)
<S-Tab> Switch mode (n/i)
Expand Down Expand Up @@ -664,10 +674,12 @@ All functionality is exposed through <Plug> mappings defined in ftplugin
files. These can be used to create custom key mappings.

Core <Plug> mappings~
*<Plug>(aibo-send)* *<Plug>(aibo-submit)*
*<Plug>(aibo-send)* *<Plug>(aibo-direct)* *<Plug>(aibo-submit)*
*<Plug>(aibo-jump)* *<Plug>(aibo-jump-or-submit)*

<Plug>(aibo-send) Prefix for sending keys to terminal
<Plug>(aibo-direct) Enter Direct mode: forward every key to the
terminal until <Esc> is pressed
<Plug>(aibo-submit) Submit content to terminal
<Plug>(aibo-jump) Alias for <Plug>(aibo-jump:tabdrop).
Remap this to change the default opener.
Expand All @@ -694,6 +706,15 @@ The `<Plug>(aibo-send)` mapping is designed to be used in two ways:
It automatically converts Vim key notation to terminal escape sequences
and sends them to the underlying process.

The `<Plug>(aibo-direct)` mapping (mapped to <C-g>i / <C-g><C-i> by default)
repeats the same single-key capture used by `<Plug>(aibo-send)`, forwarding
each captured key until <Esc> is pressed. <Esc> is never forwarded to the
tool; use <C-c> from within Direct mode to send ESC instead. While active, a
fixed 5-line window with a thick red border and the title "Direct mode" is
shown at the top of the console, explaining the mode and how to exit it.
If the prompt window is visible, it is hidden for the duration and reopened
in the same Normal or Insert state once Direct mode ends.

Common usage patterns:
<Plug>(aibo-send)<Esc> Send ESC to terminal
<Plug>(aibo-send)<C-c> Send interrupt signal
Expand Down
2 changes: 2 additions & 0 deletions ftplugin/aibo-console.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ local cfg = aibo.get_buffer_config("console")
if not (cfg and cfg.no_default_mappings) then
local opts = { buffer = bufnr, nowait = true, silent = true }
vim.keymap.set("n", "<C-g><C-o>", "<Plug>(aibo-send)", { buffer = bufnr, nowait = true })
vim.keymap.set("n", "<C-g>i", "<Plug>(aibo-direct)", { buffer = bufnr, nowait = true })
vim.keymap.set("n", "<C-g><C-i>", "<Plug>(aibo-direct)", { buffer = bufnr, nowait = true })
vim.keymap.set("n", "<CR>", "<Plug>(aibo-jump-or-submit)", opts)
vim.keymap.set("n", "<C-Enter>", "<Plug>(aibo-submit)", opts)
vim.keymap.set("n", "<F5>", "<Plug>(aibo-submit)", opts)
Expand Down
2 changes: 2 additions & 0 deletions ftplugin/aibo-prompt.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ local cfg = aibo.get_buffer_config("prompt")
if not (cfg and cfg.no_default_mappings) then
local opts = { buffer = bufnr, nowait = true, silent = true }
vim.keymap.set({ "n", "i" }, "<C-g><C-o>", "<Plug>(aibo-send)", { buffer = bufnr, nowait = true })
vim.keymap.set({ "n", "i" }, "<C-g>i", "<Plug>(aibo-direct)", { buffer = bufnr, nowait = true })
vim.keymap.set({ "n", "i" }, "<C-g><C-i>", "<Plug>(aibo-direct)", { buffer = bufnr, nowait = true })
vim.keymap.set("n", "<CR>", "<Plug>(aibo-submit)", opts)
vim.keymap.set("n", "<Esc>", "<Cmd>q<CR>", opts)
vim.keymap.set("n", "<C-Enter>", "<Plug>(aibo-submit)<Cmd>q<CR>", opts)
Expand Down
15 changes: 15 additions & 0 deletions lua/aibo/internal/console_window.lua
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,21 @@ local function setup_mappings(bufnr)
send(key)
end
end)
define("<Plug>(aibo-direct)", "Enter Direct mode: forward every key to the tool until <Esc>", function()
local winid = vim.api.nvim_get_current_win()
local info = M.get_info_by_winid(winid)
if not info then
return
end
-- Resolve the target bufnr up front rather than inside send_fn: Direct
-- mode may close other windows (e.g. hides a visible prompt) while
-- looping, so relying on "current window" per keystroke is unreliable.
local direct_mode = require("aibo.internal.direct_mode")
direct_mode.enter(winid, function(key)
local code = aibo.resolve(key) or key
M.send(info.bufnr, code)
end)
end)
define("<Plug>(aibo-submit)", "Submit to the tool", function()
submit("")
end)
Expand Down
135 changes: 135 additions & 0 deletions lua/aibo/internal/direct_mode.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
--- Direct mode: continuously forward key presses to a console's tool.
---
--- Unlike the one-shot `<Plug>(aibo-send)` capture (which sends a single key
--- and returns immediately), Direct mode repeats the capture until <Esc> is
--- pressed. <Esc> itself is never forwarded -- it always exits Direct mode,
--- consistent with the rest of Aibo where the physical <Esc> key is reserved
--- for Vim and never sent to the tool as a raw byte.
---
--- While active, a fixed 5-line floating window is shown at the top of the
--- console explaining the mode and how to exit it. If a prompt window is
--- currently visible for the console, it is hidden for the duration and
--- reopened (in the same Normal/Insert state) once Direct mode ends.
local M = {}

local TITLE = " Direct mode "
local HEIGHT = 5
local CONTENT = {
"Direct mode forwards every key you press",
"directly to the tool.",
"",
" <Esc> Exit Direct mode",
" <C-c> Send ESC to the tool",

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
fd -a keycode.lua
ast-grep run --pattern 'getchar($$$)' --lang lua lua/aibo/internal/keycode.lua
cat -n lua/aibo/internal/keycode.lua
rg -n 'C-c|pcall' tests/internal/test_direct_mode.lua

Repository: lambdalisue/nvim-aibo

Length of output: 1049


🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo '--- lua/aibo/internal/direct_mode.lua ---'
cat -n lua/aibo/internal/direct_mode.lua

echo
echo '--- tests/internal/test_direct_mode.lua ---'
if [ -f tests/internal/test_direct_mode.lua ]; then
  cat -n tests/internal/test_direct_mode.lua
else
  fd -a 'test_direct_mode.lua'
fi

echo
echo '--- search for direct mode / getchar coverage ---'
rg -n 'get_single_keycode|Keyboard interrupt|C-c|<C-c>|getchar|pcall' lua tests -g '!**/.git/**'

Repository: lambdalisue/nvim-aibo

Length of output: 15981


<C-c> doesn’t send ESC here; it exits Direct mode instead. Line 22 is misleading because keycode.get_single_keycode() wraps vim.fn.getchar() in pcall, so a real Ctrl-C is treated as an interrupt and the loop breaks before anything is forwarded. Update the hint or add a dedicated capture path if ESC forwarding is intended. There’s also no test for the Ctrl-C case.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@lua/aibo/internal/direct_mode.lua` at line 22, The `<C-c>` hint in
`direct_mode.lua` is misleading because the `keycode.get_single_keycode()` path
uses `vim.fn.getchar()` under `pcall`, so Ctrl-C is handled as an interrupt and
exits Direct mode instead of being forwarded as ESC. Update the user-facing hint
near the Direct mode key list, or add a separate capture path in the Direct mode
loop if ESC forwarding is actually intended; also add a test covering the Ctrl-C
behavior in `keycode.get_single_keycode()`/Direct mode handling.

}

-- Thick border, as opposed to the thin "rounded" border used elsewhere.
local BORDER = { "┏", "━", "┓", "┃", "┛", "━", "┗", "┃" }

--- Registry of indicator windows currently shown, keyed by console winid.
--- Exposed for testing only.
local active_indicators = {}

---@param console_winid number
---@return number indicator winid
local function open_indicator(console_winid)
local prompt = require("aibo.internal.prompt_window")
prompt.ensure_highlights()

local width = vim.api.nvim_win_get_width(console_winid)

local bufnr = vim.api.nvim_create_buf(false, true)
vim.bo[bufnr].bufhidden = "wipe"
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, CONTENT)
vim.bo[bufnr].modifiable = false

local winid = vim.api.nvim_open_win(bufnr, false, {
relative = "win",
win = console_winid,
row = 0,
col = 0,
width = math.max(1, width - 2),
height = HEIGHT,
style = "minimal",
border = BORDER,
title = TITLE,
title_pos = "left",
focusable = false,
zindex = 200,
})
vim.wo[winid].winhighlight = "FloatBorder:AiboDirectBorder,FloatTitle:AiboDirectTitle"
return winid
end

---@param winid number?
local function close_indicator(winid)
if winid and vim.api.nvim_win_is_valid(winid) then
pcall(vim.api.nvim_win_close, winid, true)
end
end

--- Hide a console's visible prompt window for the duration of Direct mode.
---@param console_winid number
---@return boolean had_prompt True if a prompt window was visible and hidden
---@return boolean was_insert True if the prompt itself was focused in Insert mode
local function hide_prompt(console_winid)
local prompt = require("aibo.internal.prompt_window")
local info = prompt.get_info_by_console_winid(console_winid)
if not info or not vim.api.nvim_win_is_valid(info.winid) then
return false, false
end
local was_insert = vim.api.nvim_get_current_win() == info.winid and vim.fn.mode():match("^[iR]") ~= nil
pcall(vim.api.nvim_win_close, info.winid, true)
return true, was_insert
end

---@param console_winid number
---@param was_insert boolean
local function restore_prompt(console_winid, was_insert)
local prompt = require("aibo.internal.prompt_window")
prompt.open(console_winid, { startinsert = was_insert })
end

--- Enter Direct mode for a console window.
--- Repeatedly captures a single key and passes it to `send_fn`, until <Esc>
--- is pressed or key capture is interrupted. <Esc> is never forwarded.
---
--- @param console_winid number The console window ID to show the indicator on
--- @param send_fn fun(key: string) Called with each captured key (Vim key notation)
function M.enter(console_winid, send_fn)
local keycode = require("aibo.internal.keycode")

local had_prompt, was_insert = hide_prompt(console_winid)
local indicator_winid = open_indicator(console_winid)
active_indicators[console_winid] = indicator_winid
Comment on lines +101 to +103
-- Paint the indicator before the first (blocking) getchar() call.
vim.cmd("redraw")

local ok, err = pcall(function()
while true do
local key = keycode.get_single_keycode()
if not key or key == "<Esc>" then
break
end
send_fn(key)
-- The tool's terminal echo of what was just sent arrives
-- asynchronously; give it a moment to land, then repaint, otherwise
-- it visibly lags by one keystroke behind what was actually typed.
vim.wait(50)
vim.cmd("redraw!")
end
end)

close_indicator(indicator_winid)
active_indicators[console_winid] = nil
if had_prompt then
restore_prompt(console_winid, was_insert)
end
if not ok then
error(err, 0)
end
end
Comment on lines +98 to +130

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🩺 Stability & Availability | 🟠 Major | ⚡ Quick win

Prompt is never restored if open_indicator throws.

hide_prompt (line 101) and open_indicator (line 102) run before the pcall block. If open_indicator raises (e.g. console_winid becomes invalid, nvim_open_win fails on a very constrained layout), the function aborts immediately — restore_prompt is never reached, permanently leaving the user without their prompt window until they manually reopen it.

🛠️ Proposed fix: wrap the whole sequence so cleanup always runs
 function M.enter(console_winid, send_fn)
   local keycode = require("aibo.internal.keycode")

   local had_prompt, was_insert = hide_prompt(console_winid)
-  local indicator_winid = open_indicator(console_winid)
-  active_indicators[console_winid] = indicator_winid
-  -- Paint the indicator before the first (blocking) getchar() call.
-  vim.cmd("redraw")
-
-  local ok, err = pcall(function()
+  local ok, err = pcall(function()
+    local indicator_winid = open_indicator(console_winid)
+    active_indicators[console_winid] = indicator_winid
+    -- Paint the indicator before the first (blocking) getchar() call.
+    vim.cmd("redraw")
+
     while true do
       local key = keycode.get_single_keycode()
       if not key or key == "<Esc>" then
         break
       end
       send_fn(key)
       vim.wait(50)
       vim.cmd("redraw!")
     end
   end)

-  close_indicator(indicator_winid)
+  close_indicator(active_indicators[console_winid])
   active_indicators[console_winid] = nil
   if had_prompt then
     restore_prompt(console_winid, was_insert)
   end
   if not ok then
     error(err, 0)
   end
 end
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
function M.enter(console_winid, send_fn)
local keycode = require("aibo.internal.keycode")
local had_prompt, was_insert = hide_prompt(console_winid)
local indicator_winid = open_indicator(console_winid)
active_indicators[console_winid] = indicator_winid
-- Paint the indicator before the first (blocking) getchar() call.
vim.cmd("redraw")
local ok, err = pcall(function()
while true do
local key = keycode.get_single_keycode()
if not key or key == "<Esc>" then
break
end
send_fn(key)
-- The tool's terminal echo of what was just sent arrives
-- asynchronously; give it a moment to land, then repaint, otherwise
-- it visibly lags by one keystroke behind what was actually typed.
vim.wait(50)
vim.cmd("redraw!")
end
end)
close_indicator(indicator_winid)
active_indicators[console_winid] = nil
if had_prompt then
restore_prompt(console_winid, was_insert)
end
if not ok then
error(err, 0)
end
end
function M.enter(console_winid, send_fn)
local keycode = require("aibo.internal.keycode")
local had_prompt, was_insert = hide_prompt(console_winid)
local ok, err = pcall(function()
local indicator_winid = open_indicator(console_winid)
active_indicators[console_winid] = indicator_winid
-- Paint the indicator before the first (blocking) getchar() call.
vim.cmd("redraw")
while true do
local key = keycode.get_single_keycode()
if not key or key == "<Esc>" then
break
end
send_fn(key)
-- The tool's terminal echo of what was just sent arrives
-- asynchronously; give it a moment to land, then repaint, otherwise
-- it visibly lags by one keystroke behind what was actually typed.
vim.wait(50)
vim.cmd("redraw!")
end
end)
close_indicator(active_indicators[console_winid])
active_indicators[console_winid] = nil
if had_prompt then
restore_prompt(console_winid, was_insert)
end
if not ok then
error(err, 0)
end
end
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@lua/aibo/internal/direct_mode.lua` around lines 98 - 130, The prompt cleanup
in M.enter is outside the protected block, so if hide_prompt or open_indicator
fails, restore_prompt and indicator cleanup will never run. Wrap the full enter
flow in a protected call and move the cleanup logic for close_indicator,
active_indicators, and restore_prompt into a guaranteed finally-style path so
the console prompt is restored even when open_indicator or later steps throw.


-- Exposed for testing only
M._active_indicators = active_indicators

return M
24 changes: 24 additions & 0 deletions lua/aibo/internal/prompt_window.lua
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ local function setup_highlights()
vim.api.nvim_set_hl(0, "AiboPromptTitle", { default = true, link = "DiagnosticInfo" })
vim.api.nvim_set_hl(0, "AiboPromptInsertBorder", { default = true, link = "DiagnosticWarn" })
vim.api.nvim_set_hl(0, "AiboPromptInsertTitle", { default = true, link = "DiagnosticWarn" })
-- Shared with aibo.internal.direct_mode's console overlay window.
vim.api.nvim_set_hl(0, "AiboDirectBorder", { default = true, link = "DiagnosticError" })
vim.api.nvim_set_hl(0, "AiboDirectTitle", { default = true, link = "DiagnosticError" })
end

--- Ensure prompt highlight groups are defined (once per session).
Expand All @@ -106,6 +109,10 @@ local function ensure_highlights()
})
end

-- Exposed so aibo.internal.direct_mode can ensure the shared highlight
-- groups exist before styling its own overlay window.
M.ensure_highlights = ensure_highlights

--- Apply mode-specific highlights and winblend to prompt window.
--- In Insert mode, uses AiboPromptInsertBorder/Title and lower winblend (more opaque).
--- In Normal mode, uses AiboPromptBorder/Title and higher winblend (more transparent).
Expand Down Expand Up @@ -208,6 +215,23 @@ local function setup_mappings(bufnr)
send(key)
end
end)
define("<Plug>(aibo-direct)", "Enter Direct mode: forward every key to associated console until <Esc>", function()
local winid = vim.api.nvim_get_current_win()
local info = M.get_info_by_winid(winid)
if not info or not info.console_info then
return
end
-- Resolve the target bufnr up front rather than inside send_fn: entering
-- Direct mode from the prompt closes the prompt window itself (to show
-- the indicator instead), so send_fn can no longer rely on "current
-- window" being the prompt by the time it's called from the loop.
local console_bufnr = info.console_info.bufnr
local direct_mode = require("aibo.internal.direct_mode")
direct_mode.enter(info.console_info.winid, function(key)
local code = aibo.resolve(key) or key
console.send(console_bufnr, code)
end)
end)
define("<Plug>(aibo-submit)", "Submit prompt to associated console", function()
vim.cmd("write")
end)
Expand Down
Loading
Loading