Skip to content

Commit 8590144

Browse files
committed
move contexts to state
1 parent b8d0842 commit 8590144

File tree

6 files changed

+270
-75
lines changed

6 files changed

+270
-75
lines changed

lua/eca/api.lua

Lines changed: 65 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -81,23 +81,20 @@ function M.add_file_context(file_path)
8181
-- Create context object
8282
local context = {
8383
type = "file",
84-
path = file_path,
85-
content = content,
84+
data = {
85+
path = file_path,
86+
}
8687
}
8788

88-
-- Get current sidebar and add context
89-
local sidebar = eca.get()
90-
if not sidebar then
91-
Logger.info("Opening ECA sidebar to add context...")
92-
M.chat()
93-
sidebar = eca.get()
94-
end
89+
local chat = eca.get()
9590

96-
if sidebar then
97-
sidebar:add_context(context)
98-
else
99-
Logger.notify("Failed to create ECA sidebar", vim.log.levels.ERROR)
91+
if not chat or not chat.mediator then
92+
Logger.notify("No active ECA Chat to add context", vim.log.levels.WARN)
93+
return
10094
end
95+
96+
chat.mediator:add_context(context)
97+
Logger.info("File context added: " .. vim.inspect(context))
10198
end
10299

103100
---@param directory_path string
@@ -113,12 +110,20 @@ function M.add_directory_context(directory_path)
113110
-- Create context object for directory
114111
local context = {
115112
type = "directory",
116-
path = directory_path,
113+
data = {
114+
path = directory_path,
115+
},
117116
}
118117

119-
-- For now, store it for next message
120-
-- TODO: Implement context management
121-
Logger.debug("Directory context added: " .. directory_path)
118+
local chat = eca.get()
119+
120+
if not chat or not chat.mediator then
121+
Logger.notify("No active ECA Chat to add context", vim.log.levels.WARN)
122+
return
123+
end
124+
125+
chat.mediator:add_context(context)
126+
Logger.info("Directory context added: " .. vim.inspect(context))
122127
end
123128

124129
function M.add_current_file_context()
@@ -131,6 +136,9 @@ function M.add_current_file_context()
131136
end
132137

133138
function M.add_selection_context()
139+
Logger.info("Adding selection context ...")
140+
local eca = require("eca")
141+
134142
-- Get visual selection marks (should be set by the command before calling this)
135143
local start_pos = vim.fn.getpos("'<")
136144
local end_pos = vim.fn.getpos("'>")
@@ -145,97 +153,82 @@ function M.add_selection_context()
145153
local end_line = math.max(start_pos[2], end_pos[2])
146154

147155
local lines = vim.api.nvim_buf_get_lines(0, start_line - 1, end_line, false)
148-
if #lines > 0 then
149-
local selection_text = table.concat(lines, "\n")
150-
local current_file = vim.api.nvim_buf_get_name(0)
151-
local context_path = current_file .. ":" .. start_line .. "-" .. end_line
152-
153-
-- Create context object
154-
local context = {
155-
type = "file",
156+
157+
if #lines <= 0 then
158+
Logger.notify("No lines found in the selection", vim.log.levels.WARN)
159+
return
160+
end
161+
162+
local current_file = vim.api.nvim_buf_get_name(0)
163+
164+
-- Create context object
165+
local context = {
166+
type = "file",
167+
data = {
156168
path = current_file,
157-
linesRange = {
158-
start = start_line,
159-
["end"] = end_line, -- end is a reserved word in Lua
169+
lines_range = {
170+
line_start = start_line,
171+
line_end = end_line,
160172
},
161173
}
174+
}
162175

163-
-- Get current sidebar and add context
164-
local eca = require("eca")
165-
local sidebar = eca.get()
166-
if not sidebar then
167-
Logger.info("Opening ECA sidebar to add context...")
168-
M.chat()
169-
sidebar = eca.get()
170-
end
171-
172-
if sidebar then
173-
sidebar:add_context(context)
174-
175-
-- Also set as selected code for visual display
176-
local selected_code = {
177-
filepath = current_file,
178-
content = selection_text,
179-
start_line = start_line,
180-
end_line = end_line,
181-
filetype = vim.api.nvim_get_option_value("filetype", { buf = 0 }),
182-
}
183-
sidebar:set_selected_code(selected_code)
176+
local chat = eca.get()
184177

185-
Logger.info("Added selection context (" .. #lines .. " lines from lines " .. start_line .. "-" .. end_line .. ")")
186-
else
187-
Logger.notify("Failed to create ECA sidebar", vim.log.levels.ERROR)
188-
end
189-
else
190-
Logger.notify("No lines found in selection", vim.log.levels.WARN)
178+
if not chat or not chat.mediator then
179+
Logger.notify("No active ECA Chat to add context", vim.log.levels.WARN)
180+
return
191181
end
182+
183+
chat.mediator:add_context(context)
184+
Logger.info("Added selection context: " .. vim.inspect(context))
192185
end
193186

194187
function M.list_contexts()
195188
local eca = require("eca")
196-
local sidebar = eca.get()
197-
if not sidebar then
189+
local chat = eca.get()
190+
191+
if not chat or not chat.mediator then
198192
Logger.notify("No active ECA sidebar", vim.log.levels.WARN)
199193
return
200194
end
201195

202-
local contexts = sidebar:get_contexts()
196+
local contexts = chat.mediator:contexts()
203197
if #contexts == 0 then
204198
Logger.notify("No active contexts", vim.log.levels.INFO)
205199
return
206200
end
207201

208202
Logger.info("Active contexts (" .. #contexts .. "):")
209203
for i, context in ipairs(contexts) do
210-
local size_info = ""
211-
if context.content then
212-
local lines = vim.split(context.content, "\n")
213-
size_info = " (" .. #lines .. " lines)"
214-
end
215-
Logger.info(i .. ". " .. context.type .. ": " .. context.path .. size_info)
204+
Logger.info(i .. ". " .. context.type .. ": " .. vim.inspect(context.data))
216205
end
217206
end
218207

219208
function M.clear_contexts()
220209
local eca = require("eca")
221-
local sidebar = eca.get()
222-
if not sidebar then
223-
Logger.notify("No active ECA sidebar", vim.log.levels.WARN)
210+
local chat = eca.get()
211+
212+
if not chat or not chat.mediator then
213+
Logger.notify("No active ECA Chat", vim.log.levels.WARN)
224214
return
225215
end
226216

227-
sidebar:clear_contexts()
217+
chat.mediator:clear_contexts()
218+
Logger.info("Cleared all contexts")
228219
end
229220

230221
function M.remove_context(path)
231222
local eca = require("eca")
232-
local sidebar = eca.get()
233-
if not sidebar then
234-
Logger.notify("No active ECA sidebar", vim.log.levels.WARN)
223+
local chat = eca.get()
224+
225+
if not chat or not chat.mediator then
226+
Logger.notify("No active ECA Chat", vim.log.levels.WARN)
235227
return
236228
end
237229

238-
sidebar:remove_context(path)
230+
chat.mediator:remove_context(path)
231+
Logger.info("Context removed: " .. path)
239232
end
240233

241234
function M.add_repo_map_context()

lua/eca/commands.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ function M.setup()
351351
return
352352
end
353353

354-
local chat = eca.current.sidebar
354+
local chat = eca.get()
355355
local models = chat.mediator:models()
356356

357357
vim.ui.select(models, {
@@ -373,7 +373,7 @@ function M.setup()
373373
return
374374
end
375375

376-
local chat = eca.current.sidebar
376+
local chat = eca.get()
377377
local behaviors = chat.mediator:behaviors()
378378

379379
vim.ui.select(behaviors, {

lua/eca/mediator.lua

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,93 @@ function mediator.new(server, state)
1313
}, { __index = mediator })
1414
end
1515

16+
local function context_adapter(context)
17+
if not context or not context.type or not context.data then
18+
return nil
19+
end
20+
21+
local function is_pos(p)
22+
return type(p) == "table" and type(p.line) == "number" and type(p.character) == "number"
23+
end
24+
25+
local adapters = {
26+
cursor = function(ctx)
27+
local d = ctx.data
28+
29+
if type(d.path) ~= "string" or type(d.position) ~= "table" then
30+
return nil
31+
end
32+
33+
local start_src = d.position.position_start
34+
local end_src = d.position.position_end
35+
36+
if not (is_pos(start_src) and is_pos(end_src)) then
37+
return nil
38+
end
39+
40+
return {
41+
type = ctx.type,
42+
path = d.path,
43+
position = {
44+
start = {
45+
line = start_src.line,
46+
character = start_src.character,
47+
},
48+
["end"] = {
49+
line = end_src.line,
50+
character = end_src.character,
51+
},
52+
},
53+
}
54+
end,
55+
directory = function(ctx)
56+
if type(ctx.data.path) ~= "string" then
57+
return nil
58+
end
59+
return {
60+
type = ctx.type,
61+
path = ctx.data.path,
62+
}
63+
end,
64+
file = function(ctx)
65+
local d = ctx.data
66+
if type(d.path) ~= "string" then
67+
return nil
68+
end
69+
local linesRange = nil
70+
if type(d.lines_range) == "table" and type(d.lines_range.line_start) == "number" and type(d.lines_range.line_end) == "number" then
71+
linesRange = {
72+
start = d.lines_range.line_start,
73+
["end"] = d.lines_range.line_end,
74+
}
75+
end
76+
77+
return {
78+
type = ctx.type,
79+
path = d.path,
80+
linesRange = linesRange,
81+
}
82+
end,
83+
web = function(ctx)
84+
if type(ctx.data.path) ~= "string" then
85+
return nil
86+
end
87+
return {
88+
type = ctx.type,
89+
url = ctx.data.path,
90+
}
91+
end,
92+
}
93+
94+
local adapter = adapters[context.type]
95+
96+
if not adapter then
97+
return nil
98+
end
99+
100+
return adapter(context)
101+
end
102+
16103
---@param method string
17104
---@param params eca.MessageParams
18105
---@param callback? fun(err?: string, result?: table)
@@ -23,6 +110,18 @@ function mediator:send(method, params, callback)
23110
end
24111
require("eca.logger").notify("Server is not rnning, please start the server", vim.log.levels.WARN)
25112
end
113+
114+
local contexts = {}
115+
116+
for _, context in pairs(params.contexts) do
117+
local adapted = context_adapter(context)
118+
if adapted then
119+
table.insert(contexts, adapted)
120+
end
121+
end
122+
123+
params.contexts = contexts
124+
26125
self.server:send_request(method, params, callback)
27126
end
28127

@@ -90,4 +189,32 @@ function mediator:id()
90189
return self.state and self.state.id
91190
end
92191

192+
function mediator:contexts()
193+
return self.state and self.state.contexts
194+
end
195+
196+
function mediator:add_context(context)
197+
if not self.state then
198+
return
199+
end
200+
201+
self.state:add_context(context)
202+
end
203+
204+
function mediator:remove_context(name)
205+
if not self.state then
206+
return
207+
end
208+
209+
self.state:remove_context(name)
210+
end
211+
212+
function mediator:clear_contexts()
213+
if not self.state then
214+
return
215+
end
216+
217+
self.state:clear_contexts()
218+
end
219+
93220
return mediator

lua/eca/sidebar.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1277,7 +1277,7 @@ function M:_send_message(message)
12771277
-- Add user message to chat
12781278
self:_add_message("user", message)
12791279

1280-
local contexts = self:get_contexts()
1280+
local contexts = self.mediator:contexts()
12811281
self.mediator:send("chat/prompt", {
12821282
chatId = self.mediator:id(),
12831283
requestId = tostring(os.time()),

0 commit comments

Comments
 (0)