forked from editor-code-assistant/eca-nvim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommands.lua
More file actions
563 lines (483 loc) · 17.7 KB
/
Copy pathcommands.lua
File metadata and controls
563 lines (483 loc) · 17.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
local Utils = require("eca.utils")
local Logger = require("eca.logger")
local Picker = require("eca.ui.picker")
local M = {}
--- Setup ECA commands
function M.setup()
-- Define ECA commands
vim.api.nvim_create_user_command("EcaChat", function(opts)
require("eca.api").chat(opts.args and { message = opts.args } or {})
end, {
desc = "Open ECA chat",
nargs = "?",
})
vim.api.nvim_create_user_command("EcaToggle", function()
require("eca.api").toggle()
end, {
desc = "Toggle ECA sidebar",
})
vim.api.nvim_create_user_command("EcaFocus", function()
require("eca.api").focus()
end, {
desc = "Focus ECA sidebar",
})
vim.api.nvim_create_user_command("EcaClose", function()
require("eca.api").close()
end, {
desc = "Close ECA sidebar",
})
vim.api.nvim_create_user_command("EcaAddFile", function(opts)
Logger.notify("EcaAddFile is deprecated. Use EcaChatAddFile instead.", vim.log.levels.WARN)
if opts.args and opts.args ~= "" and type(opts.args) == "string" then
require("eca.api").add_file_context(vim.fn.fnamemodify(opts.args, ":p"))
else
require("eca.api").add_current_file_context()
end
end, {
desc = "Add file as context to ECA",
nargs = "?",
complete = "file",
})
vim.api.nvim_create_user_command("EcaChatAddFile", function(opts)
if opts.args and opts.args ~= "" and type(opts.args) == "string" then
require("eca.api").add_file_context(vim.fn.fnamemodify(opts.args, ":p"))
else
require("eca.api").add_current_file_context()
end
end, {
desc = "Add file as context to ECA",
nargs = "?",
complete = "file",
})
vim.api.nvim_create_user_command("EcaRemoveContext", function(opts)
Logger.notify("EcaRemoveContext is deprecated. Use EcaChatRemoveFile instead.", vim.log.levels.WARN)
if opts.args and opts.args ~= "" and type(opts.args) == "string" then
require("eca.api").remove_file_context(vim.fn.fnamemodify(opts.args, ":p"))
else
require("eca.api").remove_current_file_context()
end
end, {
desc = "Remove specific file context from ECA",
nargs = "?",
complete = "file",
})
vim.api.nvim_create_user_command("EcaChatRemoveFile", function(opts)
if opts.args and opts.args ~= "" and type(opts.args) == "string" then
require("eca.api").remove_file_context(vim.fn.fnamemodify(opts.args, ":p"))
else
require("eca.api").remove_current_file_context()
end
end, {
desc = "Remove specific file context from ECA",
nargs = "?",
complete = "file",
})
vim.api.nvim_create_user_command("EcaAddSelection", function()
Logger.notify("EcaAddSelection is deprecated. Use EcaChatAddSelection instead.", vim.log.levels.WARN)
-- Force exit visual mode and set marks
vim.cmd("normal! \\<Esc>")
vim.defer_fn(function()
require("eca.api").add_selection_context()
end, 50) -- Small delay to ensure marks are set
end, {
desc = "Add current selection as context to ECA",
range = true,
})
vim.api.nvim_create_user_command("EcaChatAddSelection", function()
-- Force exit visual mode and set marks
vim.cmd("normal! \\<Esc>")
vim.defer_fn(function()
require("eca.api").add_selection_context()
end, 50) -- Small delay to ensure marks are set
end, {
desc = "Add current selection as context to ECA",
range = true,
})
vim.api.nvim_create_user_command("EcaChatAddUrl", function()
vim.ui.input({ prompt = "Enter URL to add as context: " }, function(input)
if not input or input == "" then
return
end
local url = vim.fn.trim(input)
if url == "" then
return
end
require("eca.api").add_web_context(url)
end)
end, {
desc = "Add URL as web context to ECA",
})
vim.api.nvim_create_user_command("EcaListContexts", function()
Logger.notify("EcaListContexts is deprecated. Use EcaChatListContexts instead.", vim.log.levels.WARN)
require("eca.api").list_contexts()
end, {
desc = "List active contexts in ECA",
})
vim.api.nvim_create_user_command("EcaChatListContexts", function()
require("eca.api").list_contexts()
end, {
desc = "List active contexts in ECA",
})
vim.api.nvim_create_user_command("EcaClearContexts", function()
Logger.notify("EcaClearContexts is deprecated. Use EcaChatClearContexts instead.", vim.log.levels.WARN)
require("eca.api").clear_contexts()
end, {
desc = "Clear all contexts from ECA",
})
vim.api.nvim_create_user_command("EcaChatClearContexts", function()
require("eca.api").clear_contexts()
end, {
desc = "Clear all contexts from ECA",
})
-- ===== Server Commands =====
vim.api.nvim_create_user_command("EcaServerStart", function()
require("eca.api").start_server()
end, {
desc = "Start ECA server",
})
vim.api.nvim_create_user_command("EcaServerStop", function()
require("eca.api").stop_server()
end, {
desc = "Stop ECA server",
})
vim.api.nvim_create_user_command("EcaServerRestart", function()
require("eca.api").restart_server()
end, {
desc = "Restart ECA server",
})
vim.api.nvim_create_user_command("EcaServerMessages", function()
Picker.pick(
{
source = "eca messages",
finder = function(opts, _)
local items = {}
local eca = require("eca")
if not eca or not eca.server then
Logger.notify("ECA plugin is not available", vim.log.levels.ERROR)
return items
end
-- First pass: collect messages so we can render them with
-- a fixed header width and keep the separator in a constant column.
local entries = {}
for msg in vim.iter(eca.server.messages) do
local ok, decoded = pcall(vim.json.decode, msg.content)
if ok and type(decoded) == "table" then
local parts = {}
if msg.direction then
table.insert(parts, string.format("[%s]", tostring(msg.direction)))
end
if decoded.method then
table.insert(parts, tostring(decoded.method))
end
if decoded.id ~= nil then
table.insert(parts, string.format("#%s", tostring(decoded.id)))
end
local header = table.concat(parts, " ")
local preview_text = vim.inspect(decoded) or ""
-- Flatten whitespace so searching works on a single line
local flat_preview = ""
if preview_text ~= "" then
flat_preview = preview_text:gsub("%s+", " ")
end
local json_text = ""
local ok_json, encoded = pcall(vim.json.encode, decoded)
if ok_json and type(encoded) == "string" and encoded ~= "" then
json_text = encoded
end
table.insert(entries, {
header = header,
flat_preview = flat_preview,
preview_text = preview_text,
json_text = json_text,
id = decoded.id or msg.id,
})
end
end
if #entries == 0 then
return items
end
-- Second pass: build display items with a fixed header width so that
-- the separator and body always start in the same column.
local separator = " | "
local header_width = 40 -- column where the header area ends
for idx, entry in ipairs(entries) do
local header = entry.header or ""
local preview_text = entry.preview_text or ""
local flat_preview = entry.flat_preview or ""
-- Truncate overly long headers so the separator stays fixed.
--
-- NOTE: We truncate to exactly `header_width` characters so that the
-- padding logic below can reliably keep the separator aligned.
local display_header = header
if #display_header > header_width then
display_header = display_header:sub(1, header_width)
end
-- Pad headers (or empty ones) up to header_width so the
-- first character of the separator is always in the same column.
local padding = math.max(0, header_width - #display_header)
local padded_header = display_header .. string.rep(" ", padding)
local text = padded_header
if flat_preview ~= "" then
text = padded_header .. separator .. flat_preview
end
if text == "" then
if preview_text ~= "" then
text = preview_text:gsub("%s+", " ")
elseif entry.json_text and entry.json_text ~= "" then
text = entry.json_text
else
text = "<empty message>"
end
end
table.insert(items, {
text = text,
idx = entry.id or idx,
preview = {
text = preview_text,
ft = "lua",
},
})
end
return items
end,
preview = "preview",
format = "text",
confirm = function(self, item, _)
vim.fn.setreg("", item.preview.text)
self:close()
end,
}
)
end, {
desc = "Display Messages Sent to and Received by ECA server",
})
vim.api.nvim_create_user_command("EcaLogs", function(opts)
local Api = require("eca.api")
local subcommand = opts.args and opts.args:match("%S+") or "show"
if subcommand == "show" then
Api.show_logs()
elseif subcommand == "log_path" then
local log_path = Logger.get_log_path()
Logger.notify("ECA log file: " .. log_path, vim.log.levels.INFO)
elseif subcommand == "clear" then
if Logger.clear_log() then
Logger.notify("ECA log file cleared", vim.log.levels.INFO)
else
Logger.notify("Failed to clear log file", vim.log.levels.WARN)
end
elseif subcommand == "stats" then
local stats = Logger.get_log_stats()
if stats then
Logger.notify(string.format("Log file: %s", stats.path), vim.log.levels.INFO)
Logger.notify(string.format("Size: %.2fMB (%d bytes)", stats.size_mb, stats.size), vim.log.levels.INFO)
Logger.notify(string.format("Last modified: %s", stats.modified), vim.log.levels.INFO)
else
Logger.notify("No log file stats available", vim.log.levels.INFO)
end
else
Logger.notify("Unknown EcaLogs subcommand: " .. subcommand, vim.log.levels.WARN)
Logger.notify("Available subcommands: show, log_path, clear, stats", vim.log.levels.INFO)
end
end, {
desc = "ECA logging commands (show, log_path, clear, stats)",
nargs = "?",
complete = function(ArgLead, CmdLine, CursorPos)
local subcommands = { "show", "log_path", "clear", "stats" }
return vim.tbl_filter(function(cmd)
return cmd:match("^" .. ArgLead)
end, subcommands)
end,
})
vim.api.nvim_create_user_command("EcaSend", function(opts)
if opts.args and opts.args ~= "" then
require("eca.api").send_message(opts.args)
else
Logger.notify("Please provide a message to send", vim.log.levels.WARN)
end
end, {
desc = "Send a message to ECA",
nargs = "+",
})
vim.api.nvim_create_user_command("EcaDebugWidth", function()
local Config = require("eca.config")
local width = Config.get_window_width()
local columns = vim.o.columns
local percentage = Config.options.windows.width
Logger.notify(
string.format("Width: %d columns (%.1f%% of %d total columns)", width, percentage, columns),
vim.log.levels.INFO
)
end, {
desc = "Debug window width calculation",
})
vim.api.nvim_create_user_command("EcaRedownload", function()
local eca = require("eca")
local Utils = require("eca.utils")
if eca.server and eca.server:is_running() then
Logger.notify("Stopping server before re-download...", vim.log.levels.INFO)
eca.server:stop()
end
-- Remove existing version file to force re-download
local cache_dir = Utils.get_cache_dir()
local version_file = cache_dir .. "/eca-version"
os.remove(version_file)
-- Remove existing binary
local server_binary = cache_dir .. "/eca"
os.remove(server_binary)
Logger.notify("Removed cached ECA server. Will re-download on next start.", vim.log.levels.INFO)
-- Restart server
vim.defer_fn(function()
if eca.server then
eca.server:start()
end
end, 1000)
end, {
desc = "Force re-download of ECA server",
})
vim.api.nvim_create_user_command("EcaStopResponse", function()
local eca = require("eca")
local Utils = require("eca.utils")
-- Force stop any ongoing streaming response
if eca.sidebar then
eca.sidebar:_finalize_streaming_response()
Logger.notify("Forced stop of streaming response", vim.log.levels.INFO)
else
Logger.notify("No active sidebar to stop", vim.log.levels.WARN)
end
end, {
desc = "Emergency stop for infinite loops or runaway responses",
})
vim.api.nvim_create_user_command("EcaFixTreesitter", function()
-- Emergency treesitter fix for chat buffer
vim.schedule(function()
local eca = require("eca")
local sidebar = eca.get()
if sidebar and sidebar.containers and sidebar.containers.chat then
local bufnr = sidebar.containers.chat.bufnr
if bufnr and vim.api.nvim_buf_is_valid(bufnr) then
-- Disable all highlighting for this buffer
pcall(vim.api.nvim_set_option_value, "syntax", "off", { buf = bufnr })
-- Destroy treesitter highlighter if it exists
pcall(function()
if vim.treesitter.highlighter.active[bufnr] then
vim.treesitter.highlighter.active[bufnr]:destroy()
vim.treesitter.highlighter.active[bufnr] = nil
end
end)
Logger.notify("Disabled treesitter highlighting for ECA chat buffer", vim.log.levels.INFO)
Logger.notify("Buffer " .. bufnr .. " highlighting disabled", vim.log.levels.INFO)
else
Logger.notify("No valid chat buffer found", vim.log.levels.WARN)
end
else
Logger.notify("No active ECA sidebar found", vim.log.levels.WARN)
end
end)
end, {
desc = "Emergency fix for treesitter issues in ECA chat",
})
vim.api.nvim_create_user_command("EcaChatSelectModel", function()
local eca = require("eca")
local chat = eca.get()
if not chat or not chat.mediator then
Logger.notify("No active ECA chat found", vim.log.levels.WARN)
return
end
local models = chat.mediator:models()
vim.ui.select(models, {
prompt = "Select ECA Chat Model:",
}, function(choice)
if choice then
chat.mediator:update_selected_model(choice)
chat.mediator:send("chat/selectedModelChanged", { model = choice, variant = vim.NIL }, nil)
end
end)
end, {
desc = "Select current ECA Chat model",
})
vim.api.nvim_create_user_command("EcaChatSelectBehavior", function()
local eca = require("eca")
local chat = eca.get()
if not chat or not chat.mediator then
Logger.notify("No active ECA chat found", vim.log.levels.WARN)
return
end
local behaviors = chat.mediator:behaviors()
vim.ui.select(behaviors, {
prompt = "Select ECA Chat Behavior:",
}, function(choice)
if choice then
chat.mediator:update_selected_behavior(choice)
end
end)
end, {
desc = "Select current ECA Chat behavior",
})
vim.api.nvim_create_user_command("EcaServerTools", function()
Picker.pick(
{
source = "eca tools",
finder = function(_, _)
local items = {}
local eca = require("eca")
if not eca or not eca.state then
Logger.notify("ECA state is not available", vim.log.levels.ERROR)
return items
end
local tools = eca.state.tools or {}
if not tools or vim.tbl_isempty(tools) then
Logger.notify("No tools registered in server state", vim.log.levels.INFO)
return items
end
-- Collect and sort tool names for stable ordering
local names = vim.tbl_keys(tools)
table.sort(names)
for _, name in ipairs(names) do
local tool = tools[name] or {}
-- Build a human-readable preview string that always includes the
-- tool name and its primary "kind" field (when available),
-- followed by a full vim.inspect dump for debugging.
local preview_text
if next(tool) ~= nil then
local kind_value = tool.kind ~= nil and tostring(tool.kind) or "(unknown)"
preview_text = string.format("name: %s\nkind: %s", name, kind_value)
local inspected = vim.inspect(tool)
if inspected and inspected ~= "" then
preview_text = preview_text .. "\n" .. inspected
end
else
preview_text = string.format("name: %s\n<empty tool>", name)
end
table.insert(items, {
text = name,
idx = name,
preview = {
text = preview_text,
ft = "lua",
},
})
end
return items
end,
preview = "preview",
format = "text",
confirm = function(self, item, _)
vim.fn.setreg("", item.preview.text)
self:close()
end,
}
)
end, {
desc = "Display ECA server tools (yank preview on confirm)",
})
vim.api.nvim_create_user_command("EcaChatClear", function()
local sidebar = require("eca").get()
if sidebar then
sidebar:clear_chat()
end
end, {
desc = "Clear ECA chat buffer",
})
Logger.debug("ECA commands registered")
end
return M