repositories / dotfiles
dotfiles
bugabingas dorkfiles
owned by bugabinga
neovim/lua/bugabinga/pivi/help.lua
Rawlocal rpc = require 'bugabinga.pivi.rpc'
local transcript = require 'bugabinga.pivi.transcript'
local context = require 'bugabinga.pivi.context'
local runtime = require 'bugabinga.pivi.runtime'
local BUFFER_NAME = 'help'
--- Read-only tools. A help run answers about the editor; it never changes it.
local TOOLS = 'read,grep,find,ls,nvim_help,nvim_state,nvim_lua'
local ORIENTATION = table.concat( {
'You answer one short question about Neovim, about pivi, or about Pi itself.',
'The question carries what the user currently sees in Neovim.',
'Use nvim_help for Neovim and plugin documentation instead of recalling it.',
'Answer directly and briefly, and change nothing.',
}, ' ' )
local executable = function()
return vim.g.pivi_executable or 'pi'
end
local build_command = function( options )
options = options or {}
local command = {
executable(), '--mode', 'rpc',
'--no-session',
'--tools', TOOLS,
'--append-system-prompt', ORIENTATION,
}
local extension = options.extension ~= false and runtime.extension_path() or nil
if extension then
table.insert( command, '-e' )
table.insert( command, extension )
end
local skill = options.skill ~= false and runtime.skill_path() or nil
if skill then
table.insert( command, '--skill' )
table.insert( command, skill )
end
if options.model then
table.insert( command, '--model' )
table.insert( command, options.model )
end
for _, argument in ipairs( options.arguments or {} ) do
table.insert( command, argument )
end
return command
end
--- Builds the question sent to the ephemeral run.
--- The preamble describes the visible editor, not the whole project.
local compose = function( question, options )
if options and options.view == false then return question end
local ok, view = pcall( context.visible )
if not ok or not view then return question end
return context.render_visible( view ) .. question
end
--- Answers a question in an independent ephemeral run.
--- It owns its own process, buffer, and lifetime, so no working session's
--- history, queue, or status is touched.
--- @param question string
--- @param options table? model, arguments, spawn, view, on_done
local ask = function( question, options )
vim.validate( 'question', question, 'string' )
options = options or {}
local bufnr = transcript.ensure( BUFFER_NAME )
transcript.clear( bufnr )
transcript.line( bufnr, '? ' .. question )
transcript.line( bufnr, '' )
local run = { buffer = bufnr, done = false, answer = '', }
run.client = rpc.start {
command = build_command( options ),
cwd = options.cwd or vim.uv.cwd(),
env = options.env or runtime.bridge_environment(),
spawn = options.spawn,
on_event = function( event )
if event.type == 'message_update' then
local delta = event.assistantMessageEvent or {}
if delta.type == 'text_delta' then
run.answer = run.answer .. ( delta.delta or '' )
transcript.append( bufnr, delta.delta or '' )
end
elseif event.type == 'tool_execution_start' then
transcript.line( bufnr, '' )
transcript.line( bufnr, ' · ' .. tostring( event.toolName ) )
elseif event.type == 'message_end' and event.message and event.message.errorMessage then
transcript.line( bufnr, '! ' .. tostring( event.message.errorMessage ) )
elseif event.type == 'agent_settled' then
run.done = true
run.client.stop()
if options.on_done then options.on_done( run ) end
end
end,
on_exit = function()
run.done = true
end,
}
run.client.request( { type = 'prompt', message = compose( question, options ), }, function( response )
if response.success then return end
transcript.line( bufnr, '! ' .. tostring( response.error or 'help run rejected' ) )
run.done = true
run.client.stop()
if options.on_done then options.on_done( run ) end
end )
transcript.show( bufnr )
return run
end
return {
buffer_name = BUFFER_NAME,
tools = TOOLS,
orientation = ORIENTATION,
build_command = build_command,
compose = compose,
ask = ask,
}