Skip to content

Commit 80e3be4

Browse files
authored
Merge pull request #50 from editor-code-assistant/revert-revert-async-server-download
Revert "Merge pull request #49 from editor-code-assistant/revert-asyn…
2 parents ad26f36 + b22b3d4 commit 80e3be4

18 files changed

+602
-125
lines changed

docs/configuration.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@ require("eca").setup({
2121
usage_string_format = "{messageCost} / {sessionCost}",
2222

2323
-- === BEHAVIOR ===
24-
behaviour = {
24+
behavior = {
2525
-- Set keymaps automatically
2626
auto_set_keymaps = true,
2727

2828
-- Focus sidebar automatically when opening
2929
auto_focus_sidebar = true,
3030

3131
-- Start server automatically
32-
auto_start_server = true,
32+
auto_start_server = false,
3333

3434
-- Download server automatically if not found
3535
auto_download = true,
@@ -114,7 +114,7 @@ require("eca").setup({
114114
### Minimalist
115115
```lua
116116
require("eca").setup({
117-
behaviour = { show_status_updates = false },
117+
behavior = { show_status_updates = false },
118118
windows = { width = 30 },
119119
chat = {
120120
headers = {
@@ -128,7 +128,7 @@ require("eca").setup({
128128
### Visual/UX focused
129129
```lua
130130
require("eca").setup({
131-
behaviour = { auto_focus_sidebar = true },
131+
behavior = { auto_focus_sidebar = true },
132132
windows = {
133133
width = 50,
134134
wrap = true,
@@ -149,7 +149,7 @@ require("eca").setup({
149149
require("eca").setup({
150150
debug = true,
151151
server_args = "--log-level debug",
152-
behaviour = {
152+
behavior = {
153153
auto_start_server = true,
154154
show_status_updates = true,
155155
},
@@ -164,7 +164,7 @@ require("eca").setup({
164164
### Performance-oriented
165165
```lua
166166
require("eca").setup({
167-
behaviour = {
167+
behavior = {
168168
auto_focus_sidebar = false,
169169
show_status_updates = false,
170170
},

docs/installation.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ Advanced setup example:
5353
opts = {
5454
debug = false,
5555
server_path = "",
56-
behaviour = {
56+
behavior = {
5757
auto_set_keymaps = true,
5858
auto_focus_sidebar = true,
5959
},

docs/troubleshooting.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ Solutions:
3636
Symptoms: `<leader>ec` doesn't open chat
3737

3838
Solutions:
39-
- Ensure `behaviour.auto_set_keymaps = true`
39+
- Ensure `behavior.auto_set_keymaps = true`
4040
- Confirm your `<leader>` key (default: `\`)
4141
- Configure shortcuts manually:
4242

@@ -61,5 +61,5 @@ Symptoms: Lag when typing, slow responses
6161

6262
Solutions:
6363
- Reduce window width: `windows.width = 25`
64-
- Disable visual updates: `behaviour.show_status_updates = false`
64+
- Disable visual updates: `behavior.show_status_updates = false`
6565
- Use the minimalist configuration preset

lua/eca/api.lua

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ local M = {}
1111
function M.chat(opts)
1212
opts = opts or {}
1313
local eca = require("eca")
14+
15+
if not M.is_server_running() then
16+
M.start_server()
17+
end
18+
1419
eca.open_sidebar(opts)
1520
end
1621

lua/eca/commands.lua

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,50 @@ function M.setup()
343343
desc = "Emergency fix for treesitter issues in ECA chat",
344344
})
345345

346+
vim.api.nvim_create_user_command("EcaChatSelectModel", function()
347+
local eca = require("eca")
348+
349+
if not eca or not eca.current or not eca.current.sidebar then
350+
Logger.notify("No active ECA sidebar found", vim.log.levels.WARN)
351+
return
352+
end
353+
354+
local chat = eca.current.sidebar
355+
local models = chat.mediator:models()
356+
357+
vim.ui.select(models, {
358+
prompt = "Select ECA Chat Model:",
359+
}, function(choice)
360+
if choice then
361+
chat.mediator:update_selected_model(choice)
362+
end
363+
end)
364+
end, {
365+
desc = "Select current ECA Chat model",
366+
})
367+
368+
vim.api.nvim_create_user_command("EcaChatSelectBehavior", function()
369+
local eca = require("eca")
370+
371+
if not eca or not eca.current or not eca.current.sidebar then
372+
Logger.notify("No active ECA sidebar found", vim.log.levels.WARN)
373+
return
374+
end
375+
376+
local chat = eca.current.sidebar
377+
local behaviors = chat.mediator:behaviors()
378+
379+
vim.ui.select(behaviors, {
380+
prompt = "Select ECA Chat Behavior:",
381+
}, function(choice)
382+
if choice then
383+
chat.mediator:update_selected_behavior(choice)
384+
end
385+
end)
386+
end, {
387+
desc = "Select current ECA Chat behavior",
388+
})
389+
346390
Logger.debug("ECA commands registered")
347391
end
348392

lua/eca/config.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ M._defaults = {
2020
file = "",
2121
max_file_size_mb = 10, -- Maximum log file size in MB before warning
2222
},
23-
behaviour = {
23+
behavior = {
2424
auto_set_keymaps = true,
2525
auto_focus_sidebar = true,
26-
auto_start_server = true, -- Automatically start server on setup
26+
auto_start_server = false, -- Automatically start server on setup
2727
auto_download = true, -- Automatically download server if not found
2828
show_status_updates = true, -- Show status updates in notifications
2929
},

lua/eca/init.lua

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ function H.keymaps()
3232
require("eca.api").focus()
3333
end, { noremap = true })
3434

35-
if Config.behaviour.auto_set_keymaps then
35+
if Config.behavior and Config.behavior.auto_set_keymaps then
3636
Utils.safe_keymap_set({ "n", "v" }, Config.mappings.chat, function()
3737
require("eca.api").chat()
3838
end, { desc = "eca: open chat" })
@@ -245,10 +245,12 @@ function M.setup(opts)
245245
M.state = require("eca.state").new()
246246
M.server = Server.new()
247247
M.mediator = require("eca.mediator").new(M.server, M.state)
248-
-- Start server automatically in background
249-
vim.defer_fn(function()
250-
M.server:start()
251-
end, 100) -- Small delay to ensure everything is loaded
248+
249+
if Config.behavior and Config.behavior.auto_start_server then
250+
vim.defer_fn(function()
251+
M.server:start()
252+
end, 100) -- Small delay to ensure everything is loaded
253+
end
252254

253255
M.did_setup = true
254256
end

lua/eca/mediator.lua

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,30 @@ function mediator:send(method, params, callback)
2626
self.server:send_request(method, params, callback)
2727
end
2828

29+
function mediator:behaviors()
30+
return self.state.config.behaviors.list
31+
end
32+
2933
function mediator:selected_behavior()
3034
return self.state.config.behaviors.selected
3135
end
3236

37+
function mediator:update_selected_behavior(behavior)
38+
self.state:update_selected_behavior(behavior)
39+
end
40+
41+
function mediator:models()
42+
return self.state.config.models.list
43+
end
44+
3345
function mediator:selected_model()
3446
return self.state.config.models.selected
3547
end
3648

49+
function mediator:update_selected_model(model)
50+
self.state:update_selected_model(model)
51+
end
52+
3753
function mediator:tokens_session()
3854
return self.state.usage.tokens.session
3955
end

lua/eca/path_finder.lua

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ function M:_write_version_file(version)
100100
file:write(version)
101101
file:close()
102102
else
103-
Logger.notify("Could not write version file: " .. self._version_file, vim.log.levels.WARN)
103+
Logger.warn("Could not write version file: " .. self._version_file)
104104
end
105105
end
106106

@@ -136,7 +136,7 @@ function M:_download_latest_server(server_path, version)
136136

137137
local download_path = self._cache_dir .. "/" .. artifact_name
138138

139-
Logger.notify("Downloading latest ECA server version from: " .. download_url, vim.log.levels.INFO)
139+
Logger.debug("Downloading latest ECA server version from: " .. download_url)
140140

141141
-- Ensure cache directory exists
142142
vim.fn.mkdir(self._cache_dir, "p")
@@ -150,8 +150,7 @@ function M:_download_latest_server(server_path, version)
150150

151151
local download_result = os.execute(download_cmd)
152152
if download_result ~= 0 then
153-
Logger.notify("Failed to download ECA server from: " .. download_url, vim.log.levels.ERROR)
154-
return false
153+
error("Failed to download ECA server from: " .. download_url)
155154
end
156155

157156
-- Extract if it's a zip file
@@ -161,44 +160,32 @@ function M:_download_latest_server(server_path, version)
161160

162161
local extract_result = os.execute(extract_cmd)
163162
if extract_result ~= 0 then
164-
Logger.notify("Failed to extract ECA server", vim.log.levels.ERROR)
165-
return false
163+
error("Failed to extract ECA server")
166164
end
167165

168166
-- Remove the zip file after extraction
169167
os.remove(download_path)
170168
end
171169

172170
-- Make executable (if not Windows)
173-
if not vim.loop.os_uname().sysname:lower():match("windows") then
171+
if not uv.os_uname().sysname:lower():match("windows") then
174172
os.execute("chmod +x " .. vim.fn.shellescape(server_path))
175173
end
176174

177175
if not Utils.file_exists(server_path) then
178-
Logger.notify("ECA server binary not found after download and extraction", vim.log.levels.ERROR)
179-
return false
176+
error("ECA server binary not found after download and extraction")
180177
end
181178

182179
-- Write version file
183180
self:_write_version_file(version)
184181

185-
Logger.notify("ECA server downloaded successfully", vim.log.levels.INFO)
182+
Logger.debug("ECA server downloaded successfully")
183+
186184
return true
187185
end
188186

189187
---@return string
190188
function M:find()
191-
-- Check for custom server path first
192-
local custom_path = Config.server_path
193-
if custom_path and custom_path:gsub("%s+", "") ~= "" then
194-
if Utils.file_exists(custom_path) then
195-
Logger.debug("Using custom server path: " .. custom_path)
196-
return custom_path
197-
else
198-
Logger.notify("Custom server path does not exist: " .. custom_path, vim.log.levels.WARN)
199-
end
200-
end
201-
202189
local server_path = self:_get_extension_server_path()
203190
local latest_version = self:_get_latest_version()
204191
local current_version = self:_read_version_file()
@@ -215,13 +202,18 @@ function M:find()
215202
-- Download if server doesn't exist or version is outdated
216203
if not server_exists or (latest_version and current_version ~= latest_version) then
217204
if not latest_version then
218-
Logger.notify("Could not check for latest version, using existing server", vim.log.levels.WARN)
205+
Logger.warn("Could not check for latest version, using existing server")
219206
return server_path
220207
end
221208

222-
local success = self:_download_latest_server(server_path, latest_version)
223-
if not success then
224-
error("Failed to download ECA server")
209+
local success
210+
211+
local ok, err = pcall(function()
212+
success = self:_download_latest_server(server_path, latest_version)
213+
end)
214+
215+
if not ok or not success then
216+
error((err and tostring(err)) or "Failed to download ECA server")
225217
end
226218
end
227219

0 commit comments

Comments
 (0)