local M = {}
M.bin = @BIN@
M.keys = {
annotate_last = "shift+cmd+o",
annotate_newest = "alt+shift+cmd+o",
annotate_open = "ctrl+cmd+o",
annotate_review_pick = "ctrl+cmd+p",
}
M.actions = {
{ name = "annotate_last", title = "Annotate: Agent's Last Reply", args = { "last" },
description = "Review the agent's recent replies and send feedback back." },
{ name = "annotate_newest", title = "Annotate: Agent's Newest Reply", args = { "last", "--newest" },
description = "Review the agent's newest reply, without the picker." },
{ name = "annotate_open", title = "Annotate: Documents Here", args = { "open" },
description = "Review the Markdown files in the pane's folder and send feedback to its agent." },
{ name = "annotate_gate", title = "Annotate: Documents Here, with Approve", args = { "open", "--gate" },
description = "Review the pane's folder with an Approve button, and send the decision to its agent." },
{ name = "annotate_terminal", title = "Annotate: Pane Output", args = { "terminal" },
description = "Review the pane's recent output and send feedback to its agent." },
{ name = "annotate_find", title = "Annotate: Newest Markdown File Named Here", args = { "find" },
description = "Review the newest Markdown file the pane's output names." },
{ name = "annotate_review", title = "Annotate: Review Changes", args = { "review" },
description = "Review the uncommitted changes in the pane's git repository and send feedback to its agent." },
{ name = "annotate_review_pick", title = "Annotate: Pick Changes to Review", args = { "review", "--pick" },
description = "Pick what to review in the pane's git repository (uncommitted, unstaged, staged, a recent commit) and send feedback to its agent." },
}
local function launch(bin, placement, review, ctx)
if ctx.block_id == nil then error("Annotate: no pane to review", 0) end
local argv = { bin, "rex" }
for _, arg in ipairs(review) do argv[#argv + 1] = arg end
argv[#argv + 1] = "--block"
argv[#argv + 1] = ctx.block_id
if ctx.session_id ~= nil then
argv[#argv + 1] = "--session"
argv[#argv + 1] = ctx.session_id
end
if placement ~= nil then
argv[#argv + 1] = "--placement"
argv[#argv + 1] = placement
end
local r, err = rex.exec(argv, { timeout = 20 })
if r == nil then error("Annotate: cannot run " .. bin .. ": " .. tostring(err), 0) end
if not r.ok then
local message = (r.stderr or ""):gsub("^%s+", ""):gsub("%s+$", "")
if message == "" then message = "Annotate: plannotator-tui exited with status " .. tostring(r.code) end
error(message, 0)
end
end
function M.setup(opts)
opts = opts or {}
local bin = opts.bin or M.bin
local placement = opts.placement
for _, action in ipairs(M.actions) do
local review = action.args
rex.action{
name = action.name,
title = action.title,
description = action.description,
category = "Annotate",
keywords = "plannotator review annotate feedback agent",
run = function(ctx) launch(bin, placement, review, ctx) end,
}
end
if opts.keys == false then return end
local mine = opts.keys or {}
local taken = {}
for _, key in pairs(mine) do
if key then taken[M.key_id(key)] = true end
end
local function sorted(t)
local names = {}
for name in pairs(t) do names[#names + 1] = name end
table.sort(names)
return names
end
for _, name in ipairs(sorted(M.keys)) do
local key = M.keys[name]
if mine[name] == nil and not taken[M.key_id(key)] then rex.bind(key, name) end
end
for _, name in ipairs(sorted(mine)) do
local key = mine[name]
if key then rex.bind(key, name) end
end
end
function M.key_id(key)
local parts = {}
for part in key:lower():gsub("%s+", ""):gmatch("[^+]+") do parts[#parts + 1] = part end
local last = table.remove(parts)
table.sort(parts)
parts[#parts + 1] = last
return table.concat(parts, "+")
end
return M