Skip to content

Commit 9b3d6bd

Browse files
committed
make commands use current chat instead of state directly
1 parent a21fac3 commit 9b3d6bd

File tree

3 files changed

+69
-32
lines changed

3 files changed

+69
-32
lines changed

lua/eca/commands.lua

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -344,29 +344,43 @@ function M.setup()
344344
})
345345

346346
vim.api.nvim_create_user_command("EcaChatSelectModel", function()
347-
local state = require("eca.state"):new()
348-
local models = state.config.models.list
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()
349356

350357
vim.ui.select(models, {
351358
prompt = "Select ECA Chat Model:",
352359
}, function(choice)
353360
if choice then
354-
state:update_selected_model(choice)
361+
chat.mediator:update_selected_model(choice)
355362
end
356363
end)
357364
end, {
358365
desc = "Select current ECA Chat model",
359366
})
360367

361368
vim.api.nvim_create_user_command("EcaChatSelectBehavior", function()
362-
local state = require("eca.state"):new()
363-
local behaviors = state.config.behaviors.list
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()
364378

365379
vim.ui.select(behaviors, {
366380
prompt = "Select ECA Chat Behavior:",
367381
}, function(choice)
368382
if choice then
369-
state:update_selected_behavior(choice)
383+
chat.mediator:update_selected_behavior(choice)
370384
end
371385
end)
372386
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

tests/test_select_commands.lua

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,43 @@ local MiniTest = require("mini.test")
22
local eq = MiniTest.expect.equality
33
local child = MiniTest.new_child_neovim()
44

5+
local function setup_test_environment()
6+
-- Setup commands
7+
require('eca.commands').setup()
8+
9+
-- Initialize everything
10+
_G.Server = require('eca.server').new()
11+
_G.State = require('eca.state').new()
12+
_G.Mediator = require('eca.mediator').new(_G.Server, _G.State)
13+
_G.Sidebar = require('eca.sidebar').new(1, _G.Mediator)
14+
_G.Eca = require('eca')
15+
_G.Eca.current = { sidebar = _G.Sidebar }
16+
17+
-- Mock vim.ui.select for testing
18+
_G.selected_choice = nil
19+
_G.shown_items = nil
20+
_G.shown_prompt = nil
21+
_G.original_select = vim.ui.select
22+
23+
_G.mock_select = function(choice)
24+
_G.selected_choice = choice
25+
vim.ui.select = function(items, opts, on_choice)
26+
_G.shown_items = items
27+
_G.shown_prompt = opts.prompt
28+
on_choice(choice)
29+
end
30+
end
31+
32+
_G.restore_select = function()
33+
vim.ui.select = _G.original_select
34+
end
35+
end
36+
537
local T = MiniTest.new_set({
638
hooks = {
739
pre_case = function()
840
child.restart({ "-u", "scripts/minimal_init.lua" })
9-
child.lua([[
10-
-- Setup commands
11-
require('eca.commands').setup()
12-
13-
-- Instantiate state singleton
14-
_G.State = require('eca.state').new()
15-
16-
-- Mock vim.ui.select for testing
17-
_G.selected_choice = nil
18-
_G.shown_items = nil
19-
_G.shown_prompt = nil
20-
_G.original_select = vim.ui.select
21-
22-
_G.mock_select = function(choice)
23-
_G.selected_choice = choice
24-
vim.ui.select = function(items, opts, on_choice)
25-
_G.shown_items = items
26-
_G.shown_prompt = opts.prompt
27-
on_choice(choice)
28-
end
29-
end
30-
31-
_G.restore_select = function()
32-
vim.ui.select = _G.original_select
33-
end
34-
]])
41+
child.lua_func(setup_test_environment)
3542
end,
3643
post_case = function()
3744
child.lua([[_G.restore_select()]])

0 commit comments

Comments
 (0)