local rpc = require 'bugabinga.pivi.rpc' local status = require 'bugabinga.pivi.status' local publish = require 'bugabinga.pivi.publish' local transcript = require 'bugabinga.pivi.transcript' local assemble = require 'bugabinga.pivi.assemble' local context = require 'bugabinga.pivi.context' local dialog = require 'bugabinga.pivi.dialog' local partial = require 'bugabinga.pivi.partial' local activity = require 'bugabinga.pivi.activity' local follow = require 'bugabinga.pivi.follow' local edit = require 'bugabinga.pivi.edit' local registry = {} local counter = 0 local executable = function() return vim.g.pivi_executable or 'pi' end local runtime_file = function( relative ) local found = vim.api.nvim_get_runtime_file( relative, false )[1] if found then return vim.fs.normalize( found ) end return nil end local extension_path = function() return runtime_file 'lua/bugabinga/pivi/extension/index.ts' end --- Environment the companion extension needs to reach this Neovim instance. local bridge_environment = function() local address = vim.v.servername if address == nil or address == '' then return nil end return { PIVI_NVIM_ADDRESS = address, PIVI_NVIM_EXE = vim.v.progpath, PIVI_NVIM_CLIENT = runtime_file 'lua/bugabinga/pivi/extension/client.lua', } end local build_command = function( options ) options = options or {} local command = { executable(), '--mode', 'rpc', } local extension = options.extension ~= false and extension_path() or nil if extension then table.insert( command, '-e' ) table.insert( command, extension ) end if options.model then table.insert( command, '--model' ) table.insert( command, options.model ) end if options.session then table.insert( command, '--session' ) table.insert( command, options.session ) end if options.name then table.insert( command, '--name' ) table.insert( command, options.name ) end for _, argument in ipairs( options.arguments or {} ) do table.insert( command, argument ) end return command end local unique_name = function( cwd ) counter = counter + 1 local base = vim.fs.basename( cwd or vim.uv.cwd() or 'pi' ) local name = base while registry[name] do name = base .. '-' .. tostring( counter ) counter = counter + 1 end return name end local render_event = function( self, event ) local bufnr = self.buffer if event.type == 'message_start' and event.message and event.message.role == 'assistant' then transcript.line( bufnr, '' ) transcript.line( bufnr, '# pi' ) elseif event.type == 'message_update' then local delta = event.assistantMessageEvent or {} if delta.type == 'text_delta' then 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 == 'agent_settled' then transcript.line( bufnr, '' ) elseif event.type == 'message_end' and event.message and event.message.errorMessage then transcript.line( bufnr, '' ) transcript.line( bufnr, ' ! ' .. tostring( event.message.errorMessage ) ) end end local publish_status = function( self ) publish.session_status( self.name, self.status ) end local located = function( arguments ) if type( arguments ) ~= 'table' then return nil end local path = arguments.path or arguments.file_path or arguments.filePath if type( path ) ~= 'string' or path == '' then return nil end return { path = path, line = arguments.line or arguments.lnum, } end --- Renders streaming edit intent as inert ghost text. --- Nothing here mutates a buffer: complete arguments are required for that. local ghost_intent = function( self, call ) local intent = partial.intent( call.name, call.arguments_text ) if not intent.path or not intent.text then return end local bufnr = edit.buffer_for( intent.path ) if not bufnr then return end self.ghost_buffer = bufnr activity.ghost( bufnr, 0, intent.text ) end local clear_ghost = function( self ) if self.ghost_buffer then activity.ghost_clear( self.ghost_buffer ) self.ghost_buffer = nil end end local handle_event = function( self, event ) if event.type == 'extension_ui_request' then dialog.handle( event, function( response ) self.client.notify( response ) end ) return end self.status = status.reduce( self.status, event ) self.message = assemble.apply( self.message, event ) if event.type == 'message_update' then local delta = event.assistantMessageEvent or {} if delta.type == 'toolcall_delta' then for _, call in ipairs( assemble.tool_calls( self.message ) ) do if not call.complete then ghost_intent( self, call ) end end elseif delta.type == 'toolcall_end' then clear_ghost( self ) end elseif event.type == 'tool_execution_start' then clear_ghost( self ) local location = located( event.args ) if location then follow.to( location ) end elseif event.type == 'agent_settled' then clear_ghost( self ) end render_event( self, event ) for _, listener in ipairs( self.listeners ) do pcall( listener, event, self ) end publish_status( self ) end --- Creates a session and starts its Pi process. --- @param options table? name, cwd, model, session, arguments, spawn local new = function( options ) options = options or {} local cwd = options.cwd or vim.uv.cwd() local name = options.name or unique_name( cwd ) local self = { name = name, cwd = cwd, status = status.initial(), message = assemble.new(), listeners = {}, buffer = transcript.ensure( name ), } self.client = rpc.start { command = build_command { model = options.model, session = options.session, name = options.name, arguments = options.arguments, extension = options.extension, }, cwd = cwd, env = options.env or bridge_environment(), spawn = options.spawn, on_event = function( event ) handle_event( self, event ) end, on_stderr = function( data ) self.last_stderr = data end, on_exit = function( code ) self.status = status.stopped( self.status, code ) publish_status( self ) end, } self.status.connection = 'running' publish_status( self ) registry[name] = self return self end local get = function( name ) return registry[name] end local list = function() local names = {} for key in pairs( registry ) do table.insert( names, key ) end table.sort( names ) return names end local active = function() local value = publish.read() if value.active and registry[value.active] then return registry[value.active] end local names = list() if #names == 0 then return nil end return registry[names[1]] end local set_active = function( name ) if not registry[name] then return false end publish.set_active( name ) return true end --- Submits text to the session. --- A submission during an active run must choose its delivery explicitly; --- rejection is returned, never silently swallowed. --- @return boolean accepted --- @return string? rejection local submit = function( self, text, options ) options = options or {} vim.validate( 'text', text, 'string' ) if not self.client.is_running() then return false, 'pi process is not running' end local busy = status.is_busy( self.status ) local delivery = options.delivery if busy and not delivery then return false, 'pi is running: choose steer or follow_up delivery' end if delivery and delivery ~= 'steer' and delivery ~= 'follow_up' then return false, 'unknown delivery: ' .. tostring( delivery ) end local snapshot = options.snapshot if snapshot == nil and options.capture ~= false then snapshot = context.snapshot( options.source ) end local message = text if snapshot then message = context.render( snapshot ) .. text end transcript.line( self.buffer, '' ) transcript.line( self.buffer, '> ' .. text ) local command if delivery == 'steer' then command = { type = 'steer', message = message, } elseif delivery == 'follow_up' then command = { type = 'follow_up', message = message, } else command = { type = 'prompt', message = message, } end table.insert( self.submissions, { text = text, delivery = delivery, snapshot = snapshot, } ) self.client.request( command, function( response ) if response.success then return end transcript.line( self.buffer, ' ! ' .. tostring( response.error or 'submission rejected' ) ) self.status = vim.tbl_extend( 'force', self.status, { error = response.error, } ) publish_status( self ) end ) return true, nil end local request = function( self, command, callback ) return self.client.request( command, callback ) end local abort = function( self, callback ) return self.client.request( { type = 'abort', }, callback ) end local clear_queue = function( self, callback ) return self.client.request( { type = 'clear_queue', }, function( response ) if response.success and response.data then self.status = status.reduce( self.status, { type = 'queue_update', steering = {}, followUp = {}, } ) publish_status( self ) end if callback then callback( response ) end end ) end local refresh = function( self, callback ) return self.client.request( { type = 'get_state', }, function( response ) if response.success then self.status = status.reduce_state( self.status, response.data ) publish_status( self ) end if callback then callback( response ) end end ) end local stop = function( self ) self.client.stop() registry[self.name] = nil publish.remove( self.name ) end local on = function( self, listener ) table.insert( self.listeners, listener ) return function() for index, candidate in ipairs( self.listeners ) do if candidate == listener then table.remove( self.listeners, index ) return end end end end local stop_all = function() for _, name in ipairs( list() ) do local live = registry[name] if live then stop( live ) end end end local wrap = function( self ) self.submissions = self.submissions or {} self.submit = function( text, options ) return submit( self, text, options ) end self.request = function( command, callback ) return request( self, command, callback ) end self.abort = function( callback ) return abort( self, callback ) end self.clear_queue = function( callback ) return clear_queue( self, callback ) end self.refresh = function( callback ) return refresh( self, callback ) end self.stop = function() return stop( self ) end self.on = function( listener ) return on( self, listener ) end return self end local create = function( options ) return wrap( new( options ) ) end return { create = create, get = get, list = list, active = active, set_active = set_active, stop_all = stop_all, build_command = build_command, }