Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897local api = vim.apilocal M = {}
local blockwise = "\22"local edit_namespace = api.nvim_create_namespace("pi.edits")local read_namespace = api.nvim_create_namespace("pi.reads")
local state = { buf = nil, job = nil, cwd = nil, source_win = nil, event_channel = nil, event_connecting = false, event_subscribed = false, event_remainder = "", tools = {}, edit_snapshots = {}, edited_buffers = {}, read_buffers = {},}
local socket_path = vim.fs.joinpath(vim.fn.stdpath("state"), "pi-" .. vim.fn.getpid() .. ".sock")local extension_path = vim.fs.joinpath(vim.fn.stdpath("config"), "lua", "pi", "bridge.ts")local present = require("present")
local function notify(message, level) vim.notify(message, level or vim.log.levels.ERROR, { title = "Pi" })end
local function clear_highlights(buffers, namespace) for buf in pairs(buffers) do if api.nvim_buf_is_valid(buf) then api.nvim_buf_clear_namespace(buf, namespace, 0, -1) end end return {}end
local editor_tools = {}local editor_tool_names = {}
local function load_editor_tools() local tools = {} for _, path in ipairs(api.nvim_get_runtime_file("tool/*.lua", true)) do local name = vim.fs.basename(path):sub(1, -5) if not name:match("^[a-z][a-z0-9_]*$") or #name > 64 then error("Invalid editor tool filename: " .. path, 0) end local definition = assert(loadfile(path))() if type(definition) ~= "table" then error(path .. ": expected a table", 0) end tools[name] = vim.tbl_deep_extend("force", tools[name] or {}, definition) end
local names = vim.tbl_keys(tools) table.sort(names) for _, name in ipairs(names) do if type(tools[name].execute) ~= "function" then error("Editor tool has no execute function: " .. name, 0) end if tools[name].cleanup ~= nil and type(tools[name].cleanup) ~= "function" then error("Editor tool has an invalid cleanup function: " .. name, 0) end end editor_tools = tools editor_tool_names = namesend
local function list_editor_tools() load_editor_tools() local tools = {} for _, name in ipairs(editor_tool_names) do local tool = editor_tools[name] table.insert(tools, { name = name, label = tool.label, description = tool.description, parameters = tool.parameters, retrySafe = tool.retrySafe, }) end return { tools = tools }end
local function execute_editor_tool(request) if type(request.tool) ~= "string" or not editor_tools[request.tool] then error("Unknown editor tool: " .. tostring(request.tool), 0) end if type(request.arguments) ~= "table" then error("Editor tool arguments must be an object", 0) end if type(request.cwd) ~= "string" then error("Editor tool working directory must be a string", 0) end local result = editor_tools[request.tool].execute(request.arguments, request.cwd) if type(result) ~= "table" or type(result.text) ~= "string" then error("Editor tool returned an invalid result: " .. request.tool, 0) end return resultend
local function cleanup_editor_tools() for _, name in ipairs(editor_tool_names) do local cleanup = editor_tools[name].cleanup if cleanup then pcall(cleanup) end end editor_tools = {} editor_tool_names = {}end
local function focus_terminal(win) api.nvim_set_current_win(win) vim.wo[win].statusline = " %{b:term_title} " vim.cmd("startinsert") return winend
local function terminal_window() if state.job and (not state.buf or not api.nvim_buf_is_valid(state.buf)) then notify("Pi terminal is closing", vim.log.levels.WARN) return end if state.buf and api.nvim_buf_is_valid(state.buf) and not state.job then api.nvim_buf_delete(state.buf, { force = true }) state.buf = nil end if state.buf then for _, win in ipairs(vim.fn.win_findbuf(state.buf)) do if api.nvim_win_is_valid(win) then return focus_terminal(win) end end vim.cmd("botright vsplit") api.nvim_win_set_buf(0, state.buf) return focus_terminal(api.nvim_get_current_win()) end
vim.cmd("botright vsplit") vim.cmd("enew") local win = api.nvim_get_current_win() state.buf = api.nvim_get_current_buf() state.cwd = vim.fn.getcwd() api.nvim_win_set_width(win, math.max(40, math.floor(vim.o.columns * 0.4)))
vim.fn.delete(socket_path) local job = vim.fn.jobstart({ "pi", "--continue", "--tui-mode", "regular", "-e", extension_path }, { term = true, cwd = state.cwd, env = { PI_NVIM_SOCKET = socket_path, PI_DEBUG_REDRAW = "1" }, on_exit = function(exited_job, code) vim.schedule(function() if state.job ~= exited_job then return end state.job = nil state.cwd = nil if state.event_channel then pcall(vim.fn.chanclose, state.event_channel) state.event_channel = nil end state.event_connecting = false state.event_subscribed = false state.event_remainder = "" state.tools = {} state.edit_snapshots = {} cleanup_editor_tools() local buf = state.buf state.buf = nil if buf and api.nvim_buf_is_valid(buf) then for _, terminal_win in ipairs(vim.fn.win_findbuf(buf)) do pcall(api.nvim_win_close, terminal_win, true) end pcall(api.nvim_buf_delete, buf, { force = true }) end vim.fn.delete(socket_path) if code ~= 0 then notify("Pi exited with status " .. code) end end) end, }) if job <= 0 then state.job = nil state.cwd = nil notify("Could not start Pi") return end
state.job = job vim.bo[state.buf].bufhidden = "hide" vim.keymap.set("t", "<Esc>", "<Esc>", { buffer = state.buf }) api.nvim_create_autocmd("TermLeave", { buffer = state.buf, callback = function(event) local current = api.nvim_get_current_win() if api.nvim_win_get_buf(current) == event.buf then api.nvim_win_set_cursor(current, { api.nvim_buf_line_count(event.buf), 0 }) end end, }) return focus_terminal(win)end
local function source_file(bufnr) if not api.nvim_buf_is_valid(bufnr) or vim.bo[bufnr].buftype ~= "" then return nil, "the source must be a normal buffer" end if vim.bo[bufnr].modified then return nil, "save or discard source changes before using Pi" end
local name = api.nvim_buf_get_name(bufnr) if name == "" then return nil, "the source buffer has no filename" end return vim.fs.abspath(name)end
local function visual_selection(opts, bufnr) local kind = vim.fn.visualmode() if opts.range ~= 2 or (kind ~= "v" and kind ~= "V" and kind ~= blockwise) then return end
local first = vim.fn.getpos("'<") local last = vim.fn.getpos("'>") local region = vim.fn.getregionpos(first, last, { type = kind }) if #region == 0 or opts.line1 ~= region[1][1][2] or opts.line2 ~= region[#region][2][2] then return end
return { lines = { start = math.min(first[2], last[2]), ["end"] = math.max(first[2], last[2]) }, text = table.concat(vim.fn.getregion(first, last, { type = kind }), "\n"), }end
local function capture_selection(opts, bufnr) if opts.range == 0 then return end
local file, err = source_file(bufnr) if not file then return nil, err end local selection = visual_selection(opts, bufnr) if not selection then local first = math.min(opts.line1, opts.line2) local last = math.max(opts.line1, opts.line2) selection = { lines = { start = first, ["end"] = last }, text = table.concat(api.nvim_buf_get_lines(bufnr, first - 1, last, false), "\n"), } end selection.file = file return selectionend
local function buffer_content(buf) local newline = ({ dos = "\r\n", mac = "\r", unix = "\n" })[vim.bo[buf].fileformat] local content = table.concat(api.nvim_buf_get_lines(buf, 0, -1, false), newline) if vim.bo[buf].endofline then content = content .. newline end if vim.bo[buf].bomb then content = "\239\187\191" .. content end return contentend
local function content_lines(content, revision) local has_bom = content:sub(1, 3) == "\239\187\191" if has_bom ~= revision.bomb then error("Pi edits cannot change the byte-order mark", 0) end if has_bom then content = content:sub(4) end
if revision.fileformat == "dos" then local remainder = content:gsub("\r\n", "") if remainder:find("\r", 1, true) or remainder:find("\n", 1, true) then error("Pi edit changed the file line endings", 0) end content = content:gsub("\r\n", "\n") elseif revision.fileformat == "unix" then if content:find("\r", 1, true) then error("Pi edit changed the file line endings", 0) end else error("Unsupported file format: " .. tostring(revision.fileformat), 0) end
local endofline = content:sub(-1) == "\n" if endofline ~= revision.endofline then error("Pi edits cannot change the final line ending", 0) end local lines = vim.split(content, "\n", { plain = true }) if endofline then table.remove(lines) end if #lines == 0 then lines = { "" } end return linesend
local function edit_changes(old_lines, new_lines, old_endofline, new_endofline) local old_text = table.concat(old_lines, "\n") .. (old_endofline and "\n" or "") local new_text = table.concat(new_lines, "\n") .. (new_endofline and "\n" or "") local changes = { added = {}, removed = {}, hunks = {} } for _, hunk in ipairs(vim.text.diff(old_text, new_text, { result_type = "indices" })) do local old_start, old_count, new_start, new_count = unpack(hunk) local at = math.max(1, new_start + (new_count == 0 and 1 or 0)) table.insert(changes.hunks, { first = at, last = new_count == 0 and at or new_start + new_count - 1, }) if new_count > 0 then table.insert(changes.added, { first = new_start, last = new_start + new_count - 1 }) end if old_count > 0 then table.insert(changes.removed, { at = at, lines = vim.list_slice(old_lines, old_start, old_start + old_count - 1), }) end end return changesend
local function edit_read(request) if type(request.path) ~= "string" or type(request.toolCallId) ~= "string" then error("Invalid Neovim edit read request", 0) end local path = vim.fs.abspath(request.path) local buf = vim.fn.bufadd(path) vim.fn.bufload(buf) if not api.nvim_buf_is_valid(buf) or vim.bo[buf].buftype ~= "" then error("Pi can only edit normal file buffers", 0) end api.nvim_buf_call(buf, function() vim.cmd("silent checktime") end) local encoding = vim.bo[buf].fileencoding:lower() if encoding ~= "" and encoding ~= "utf-8" and encoding ~= "utf8" then error("Pi can only edit UTF-8 buffers", 0) end if vim.bo[buf].binary then error("Pi cannot edit binary buffers", 0) end if vim.bo[buf].fileformat == "mac" then error("Pi cannot edit Mac-format line endings", 0) end if vim.bo[buf].modified then error("Pi cannot edit a buffer with unsaved changes: " .. path, 0) end if not vim.bo[buf].modifiable or vim.bo[buf].readonly then error("Pi cannot edit a readonly buffer: " .. path, 0) end
local revision = { buffer = buf, changedtick = api.nvim_buf_get_changedtick(buf), fileformat = vim.bo[buf].fileformat, fileencoding = vim.bo[buf].fileencoding, binary = vim.bo[buf].binary, bomb = vim.bo[buf].bomb, endofline = vim.bo[buf].endofline, fixendofline = vim.bo[buf].fixendofline, } local content = buffer_content(buf) if not vim.deep_equal(content_lines(content, revision), api.nvim_buf_get_lines(buf, 0, -1, false)) then error("Pi cannot represent this buffer without changing its bytes", 0) end state.edit_snapshots[request.toolCallId] = { path = path, content = content, lines = api.nvim_buf_get_lines(buf, 0, -1, false), revision = revision, } return { content = content, revision = revision }end
local function disk_content(path) local fd, open_error = vim.uv.fs_open(path, "r", 438) if not fd then return nil, open_error end local stat, stat_error = vim.uv.fs_fstat(fd) if not stat then vim.uv.fs_close(fd) return nil, stat_error end local content, read_error = stat.size == 0 and "" or vim.uv.fs_read(fd, stat.size, 0) vim.uv.fs_close(fd) return content, read_errorend
local function restore_snapshot(buf, snapshot, undo_sequence) if not api.nvim_buf_is_valid(buf) then return end local ok = pcall(api.nvim_buf_call, buf, function() vim.cmd("silent undo " .. undo_sequence) end) vim.bo[buf].binary = snapshot.revision.binary vim.bo[buf].fileencoding = snapshot.revision.fileencoding vim.bo[buf].fileformat = snapshot.revision.fileformat vim.bo[buf].bomb = snapshot.revision.bomb vim.bo[buf].endofline = snapshot.revision.endofline vim.bo[buf].fixendofline = snapshot.revision.fixendofline if not ok or buffer_content(buf) ~= snapshot.content then error("Neovim could not restore the buffer after the failed write", 0) end vim.bo[buf].modified = falseend
local function edit_write(request) if type(request.path) ~= "string" or type(request.toolCallId) ~= "string" or type(request.content) ~= "string" or type(request.revision) ~= "table" then error("Invalid Neovim edit write request", 0) end
local snapshot = state.edit_snapshots[request.toolCallId] if not snapshot or snapshot.path ~= vim.fs.abspath(request.path) then error("Neovim edit snapshot is missing", 0) end local revision = snapshot.revision if not vim.deep_equal(request.revision, revision) then error("Neovim edit snapshot does not match the write request", 0) end
local buf = revision.buffer if not api.nvim_buf_is_valid(buf) or api.nvim_buf_get_name(buf) ~= snapshot.path or vim.bo[buf].buftype ~= "" or not vim.bo[buf].modifiable or vim.bo[buf].readonly or vim.bo[buf].modified or api.nvim_buf_get_changedtick(buf) ~= revision.changedtick or vim.bo[buf].fileformat ~= revision.fileformat or vim.bo[buf].fileencoding ~= revision.fileencoding or vim.bo[buf].binary ~= revision.binary or vim.bo[buf].bomb ~= revision.bomb or vim.bo[buf].endofline ~= revision.endofline or vim.bo[buf].fixendofline ~= revision.fixendofline or buffer_content(buf) ~= snapshot.content then error("Neovim buffer changed while Pi prepared the edit: " .. snapshot.path, 0) end local disk, disk_error = disk_content(snapshot.path) if disk ~= snapshot.content then error("File changed while Pi prepared the edit: " .. tostring(disk_error or snapshot.path), 0) end
local lines = content_lines(request.content, revision) local undo_sequence = api.nvim_buf_call(buf, function() return vim.fn.undotree().seq_cur end) api.nvim_buf_set_lines(buf, 0, -1, false, lines) vim.bo[buf].fixendofline = false local write_ok, write_error = pcall(api.nvim_buf_call, buf, function() vim.cmd("silent write") end) if api.nvim_buf_is_valid(buf) then vim.bo[buf].fixendofline = revision.fixendofline end if not write_ok then local disk = disk_content(snapshot.path) if disk == snapshot.content then restore_snapshot(buf, snapshot, undo_sequence) error("Neovim did not save the Pi edit: " .. tostring(write_error), 0) end error("Neovim write failed after the file changed on disk. Do not retry this edit: " .. tostring(write_error), 0) end if not api.nvim_buf_is_valid(buf) or vim.bo[buf].modified then error("Neovim saved the Pi edit, but a write hook left unsaved changes. Do not retry this edit", 0) end
local actual_lines = api.nvim_buf_get_lines(buf, 0, -1, false) local actual = buffer_content(buf) local disk, read_error = disk_content(snapshot.path) if disk ~= actual then error("Neovim saved the Pi edit, but the file changed again. Do not retry this edit: " .. tostring(read_error or snapshot.path), 0) end local changedtick = api.nvim_buf_get_changedtick(buf) local tool = state.tools[request.toolCallId] if tool then tool.changedtick = changedtick tool.modified = false tool.changes = edit_changes(snapshot.lines, actual_lines, revision.endofline, vim.bo[buf].endofline) end return { digest = vim.fn.sha256(actual), changedtick = changedtick }end
local function handle_nvim_request(channel, request) if channel ~= state.event_channel then return end local handler = ({ ["tools/list"] = list_editor_tools, ["tools/execute"] = execute_editor_tool, read = edit_read, write = edit_write, })[request.operation] local ok, result = pcall(handler or function() error("Unknown Neovim operation", 0) end, request) if type(request.toolCallId) == "string" and (request.operation == "write" or not ok) then state.edit_snapshots[request.toolCallId] = nil end if channel == state.event_channel then local response = ok and result or { error = tostring(result) } response.type = "nvim_response" response.requestId = request.requestId response.ok = ok vim.fn.chansend(channel, vim.json.encode(response) .. "\n") endend
local function buffer_conflict(tool, buf) return tool.modified or vim.bo[buf].modified or (tool.changedtick and api.nvim_buf_get_changedtick(buf) ~= tool.changedtick)end
local function prepare_buffer(tool) local buf = vim.fn.bufnr(tool.path) if buf < 0 or not api.nvim_buf_is_loaded(buf) then return true end if buffer_conflict(tool, buf) then notify("Pi changed " .. tool.path .. " while the buffer has unsaved changes", vim.log.levels.WARN) return false end api.nvim_buf_call(buf, function() vim.cmd("silent checktime") end) return trueend
local function show_read(tool, event) if not tool.show or event.isImage or not vim.uv.fs_stat(tool.path) then return end local buf = vim.fn.bufadd(tool.path) if buffer_conflict(tool, buf) then notify("Cannot show Pi read because the buffer changed", vim.log.levels.WARN) return end vim.fn.bufload(buf) api.nvim_buf_call(buf, function() vim.cmd("silent checktime") end)
local line_count = api.nvim_buf_line_count(buf) local first = math.max(1, math.floor(tonumber(tool.args.offset) or 1)) if first > line_count then return end local limit = tonumber(tool.args.limit) local truncation = event.details and event.details.truncation if type(truncation) == "table" and type(truncation.outputLines) == "number" then limit = limit and math.min(limit, truncation.outputLines) or truncation.outputLines end if limit and limit < 1 then return end local last = limit and math.min(line_count, first + math.floor(limit) - 1) or line_count vim.hl.range(buf, read_namespace, "Visual", { first - 1, 0 }, { last - 1, -1 }, { regtype = "V", inclusive = true, }) state.read_buffers[buf] = true pcall(present.buffer, buf, { line = math.floor((first + last) / 2), anchor_win = tool.terminal_win, preferred_win = tool.preferred_win, })end
local function show_edit(tool, event) if not prepare_buffer(tool) then return end local changes = tool.changes or { added = {}, removed = {}, hunks = {} } if #changes.added == 0 and #changes.removed == 0 then return end
if not tool.show then return end local buf = vim.fn.bufadd(tool.path) vim.fn.bufload(buf) api.nvim_buf_call(buf, function() vim.cmd("silent checktime") end)
local line_count = api.nvim_buf_line_count(buf) for _, hunk in ipairs(changes.hunks) do local first = math.max(1, math.min(hunk.first, line_count)) local last = math.max(first, math.min(hunk.last, line_count)) for line = first, last do local row = line - 1 for _, mark in ipairs(api.nvim_buf_get_extmarks( buf, edit_namespace, { row, 0 }, { row, -1 }, { type = "sign" } )) do api.nvim_buf_del_extmark(buf, edit_namespace, mark[1]) end api.nvim_buf_set_extmark(buf, edit_namespace, row, 0, { sign_text = "│", sign_hl_group = "Changed", }) end end for _, range in ipairs(changes.added) do local first = math.max(1, math.min(range.first, line_count)) local last = math.max(first, math.min(range.last, line_count)) vim.hl.range(buf, edit_namespace, "Added", { first - 1, 0 }, { last - 1, -1 }, { regtype = "V", inclusive = true, }) end for _, removed in ipairs(changes.removed) do local lines = {} for _, line in ipairs(removed.lines) do table.insert(lines, { { line, "Removed" } }) end local at_end = removed.at > line_count api.nvim_buf_set_extmark(buf, edit_namespace, at_end and line_count - 1 or removed.at - 1, 0, { virt_lines = lines, virt_lines_above = not at_end, virt_lines_overflow = "scroll", }) end state.edited_buffers[buf] = true
local added = changes.added[1] and changes.added[1].first local removed = changes.removed[1] and changes.removed[1].at local first = added and removed and math.min(added, removed) or added or removed pcall(present.buffer, buf, { line = math.max(1, math.min(first, line_count)), anchor_win = tool.terminal_win, preferred_win = tool.preferred_win, })end
local function bridge_event(event) if event.type == "subscribed" then state.event_subscribed = true return elseif event.type == "subscription_error" then state.event_subscribed = false notify(type(event.error) == "string" and event.error or "Pi rejected the editor tool catalog") return elseif event.type == "prompt_start" then state.edited_buffers = clear_highlights(state.edited_buffers, edit_namespace) if not event.external then state.source_win = nil end elseif event.type == "assistant_start" or event.type == "tool_start" then state.read_buffers = clear_highlights(state.read_buffers, read_namespace) elseif event.type == "agent_settled" then state.source_win = nil end
if event.type == "tool_start" then local args = event.args or {} if (event.tool == "read" or event.tool == "edit" or event.tool == "write") and type(args.path) == "string" then local path = vim.fs.abspath(args.path) local buf = vim.fn.bufnr(path) local current = api.nvim_get_current_win() state.tools[event.id] = { name = event.tool, args = args, path = vim.fs.abspath(path), modified = buf >= 0 and vim.bo[buf].modified or false, changedtick = buf >= 0 and api.nvim_buf_is_loaded(buf) and api.nvim_buf_get_changedtick(buf) or nil, preferred_win = state.source_win, show = state.buf and api.nvim_win_get_buf(current) == state.buf, terminal_win = current, } end elseif event.type == "tool_end" then local tool = state.tools[event.id] state.tools[event.id] = nil state.edit_snapshots[event.id] = nil if not tool or event.isError then return end if type(event.changedtick) == "number" then tool.changedtick = event.changedtick tool.modified = false end if type(event.path) == "string" and vim.fs.abspath(event.path) ~= tool.path then tool.path = vim.fs.abspath(event.path) local buf = vim.fn.bufnr(tool.path) tool.modified = buf >= 0 and vim.bo[buf].modified or false tool.changedtick = nil end if tool.name == "read" then show_read(tool, event) elseif tool.name == "edit" then show_edit(tool, event) else prepare_buffer(tool) end endend
local function read_events(channel, data) if state.event_channel ~= channel then return end if #data == 1 and data[1] == "" then state.event_channel = nil state.event_subscribed = false state.event_remainder = "" state.tools = {} state.edit_snapshots = {} cleanup_editor_tools() state.edited_buffers = clear_highlights(state.edited_buffers, edit_namespace) state.read_buffers = clear_highlights(state.read_buffers, read_namespace) if state.job then vim.defer_fn(M.connect_events, 100) end return end
state.event_remainder = state.event_remainder .. table.concat(data, "\n") while true do local newline = state.event_remainder:find("\n", 1, true) if not newline then return end local line = state.event_remainder:sub(1, newline - 1) state.event_remainder = state.event_remainder:sub(newline + 1) local ok, event = pcall(vim.json.decode, line) if ok and type(event) == "table" then vim.schedule(function() if channel ~= state.event_channel then return end if event.type == "nvim_request" then handle_nvim_request(channel, event) else bridge_event(event) end end) end endend
function M.connect_events() if state.event_channel or state.event_connecting or not state.job then return end state.event_connecting = true local job = state.job
local function connect(attempt) if state.job ~= job then state.event_connecting = false return end local ok, channel = pcall(vim.fn.sockconnect, "pipe", socket_path, { on_data = read_events }) if ok and channel > 0 then state.event_channel = channel state.event_connecting = false state.event_subscribed = false vim.fn.chansend(channel, '{"type":"subscribe"}\n') elseif attempt == 100 then state.event_connecting = false notify("Could not subscribe to Pi tool events") else vim.defer_fn(function() connect(attempt + 1) end, 100) end end
connect(1)end
local function send_to_pi(message, job, attempt) if state.job ~= job then return end if not state.event_subscribed then if attempt == 100 then notify("Could not subscribe to Pi tool events") return end vim.defer_fn(function() send_to_pi(message, job, attempt + 1) end, 100) return end
local response = "" local ok, channel = pcall(vim.fn.sockconnect, "pipe", socket_path, { on_data = function(_, data) response = response .. table.concat(data, "\n") while true do local newline = response:find("\n", 1, true) if not newline then return end local line = response:sub(1, newline - 1) response = response:sub(newline + 1) local decoded, value = pcall(vim.json.decode, line) if decoded and type(value) == "table" and value.error then notify(value.error) end end end, }) if ok and channel > 0 then vim.fn.chansend(channel, vim.json.encode(message) .. "\n") return end if attempt == 100 then notify("Could not connect to the Pi bridge") return end vim.defer_fn(function() send_to_pi(message, job, attempt + 1) end, 100)end
local function send(request, selection) local source_win = api.nvim_get_current_win() if not terminal_window() then return end state.source_win = source_win M.connect_events() send_to_pi({ type = "prompt", request = request, selection = selection }, state.job, 1)end
function M.command(opts) local source_buf = api.nvim_get_current_buf() local selection, err = capture_selection(opts, source_buf) if err then notify(err, vim.log.levels.WARN) return end
local request = opts.args or "" if vim.trim(request) ~= "" then send(request, selection) elseif not selection then if terminal_window() then M.connect_events() end else vim.ui.input({ prompt = "Pi: " }, function(input) if input and vim.trim(input) ~= "" then send(input, selection) end end) endend
function M.cleanup() if state.event_channel then pcall(vim.fn.chanclose, state.event_channel) state.event_channel = nil end state.event_subscribed = false state.edit_snapshots = {} cleanup_editor_tools() state.edited_buffers = clear_highlights(state.edited_buffers, edit_namespace) state.read_buffers = clear_highlights(state.read_buffers, read_namespace) if state.job then vim.fn.jobstop(state.job) state.job = nil end vim.fn.delete(socket_path)end
return M