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
24 changes: 24 additions & 0 deletions lua/eca/observer.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
local observer = {}

---@type { [integer]: fun(message: table) }
local subscriptions = {}

---@param id integer
---@param on_update fun(message: table)
function observer.subscribe(id, on_update)
subscriptions[id] = on_update
end

function observer.unsubscribe(id)
if subscriptions[id] then
subscriptions[id] = nil
end
end

function observer.notify(message)
for _, fn in pairs(subscriptions) do
fn(message)
end
end

return observer
23 changes: 7 additions & 16 deletions lua/eca/server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ local Logger = require("eca.logger")
---@field on_start? function Callback when the server process starts
---@field on_stop function Callback when the server stops
---Called when a notification is received(message without an ID)
---@field on_notification fun(server: eca.Server, method: string, params: table)
---@field capabilities table Server capabilities
---@field on_notification fun(server: eca.Server, message: table)
---@field capabilities eca.ServerCapabilities Server capabilities
---@field private path_finder eca.PathFinder Server path finder
---@field pending_requests {id: fun(err, data)} -- outgoing requests with callbacks
local M = {}
Expand All @@ -31,10 +31,11 @@ function M.new(opts)
on_stop = function()
require("eca.logger").notify("Server stopped", vim.log.levels.INFO)
end,
---@param server eca.Server
on_notification = function(server, method, params)
---@param _ eca.Server
---@param message table
on_notification = function(_, message)
return vim.schedule(function()
server:handle_content(method, params)
require("eca.observer").notify(message)
end)
end,
path_finder = PathFinder:new(),
Expand Down Expand Up @@ -207,16 +208,6 @@ function M:stop()
self.initialized = false
end

function M:handle_content(method, params)
if method == "chat/contentReceived" then
local eca = require("eca")
local sidebar = eca.get(false)
if sidebar and params then
sidebar:handle_chat_content_received(params)
end
end
end

---@return boolean
function M:is_running()
return self.process and not self.process:is_closing()
Expand All @@ -235,7 +226,7 @@ function M:handle_message(message)
end
elseif message.method and not message.id then
if self.on_notification then
self.on_notification(self, message.method, message.params)
self:on_notification(message)
end
end
end
Expand Down
12 changes: 11 additions & 1 deletion lua/eca/sidebar.lua
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ function M.new(id)
instance._augroup = vim.api.nvim_create_augroup("eca_sidebar_" .. id, { clear = true })
instance._response_start_time = 0
instance._max_response_length = 50000 -- 50KB max response

require("eca.observer").subscribe(id, function(message)
instance:handle_chat_content(message)
end)
return instance
end

Expand Down Expand Up @@ -1160,7 +1164,7 @@ function M:_send_message(message)
-- Include active contexts in the message
local contexts = self:get_contexts()
eca.server:send_request("chat/prompt", {
chatId = nil,
chatId = self.id,
requestId = tostring(os.time()),
message = message,
contexts = contexts or {},
Expand All @@ -1180,6 +1184,12 @@ function M:_send_message(message)
end
end

function M:handle_chat_content(message)
if message.params then
self:handle_chat_content_received(message.params)
end
end

---@param params table Server content notification
function M:handle_chat_content_received(params)
if not params or not params.content then
Expand Down