-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.lua
More file actions
961 lines (905 loc) · 31 KB
/
Copy pathinit.lua
File metadata and controls
961 lines (905 loc) · 31 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
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
-- mod-version: 3.11
local core = require "core"
local common = require "core.common"
local command = require "core.command"
local config = require "core.config"
local keymap = require "core.keymap"
local HAS_NET = rawget(_G, "net") ~= nil
local Conversation = require "plugins.assistant.conversation"
local PromptView = require "plugins.assistant.promptview"
local tools = require "plugins.assistant.tools"
local agent_config = require "plugins.assistant.agent_config"
local ConversationsList = require "plugins.assistant.ui.conversationslist"
local MemoriesList = require "plugins.assistant.ui.memorieslist"
local CliBackend = require "plugins.assistant.backend.cli"
local AppServerBackend = require "plugins.assistant.backend.appserver"
local AcpBackend = require "plugins.assistant.backend.acp"
local Codex = require "plugins.assistant.agent.codex"
local Acp = require "plugins.assistant.agent.acp"
local Copilot = require "plugins.assistant.agent.copilot"
local HttpBackend = HAS_NET and require "plugins.assistant.backend.http" or nil
local AnthropicBackend = HAS_NET and require "plugins.assistant.backend.anthropic" or nil
local Ollama = HAS_NET and require "plugins.assistant.agent.ollama" or nil
local LlamaCpp = HAS_NET and require "plugins.assistant.agent.llamacpp" or nil
local Lms = HAS_NET and require "plugins.assistant.agent.lms" or nil
local OpenAI = HAS_NET and require "plugins.assistant.agent.openai" or nil
local Anthropic = HAS_NET and require "plugins.assistant.agent.anthropic" or nil
local DeepSeek = HAS_NET and require "plugins.assistant.agent.deepseek" or nil
local DeepSeekAnthropic = HAS_NET and require "plugins.assistant.agent.deepseek_anthropic" or nil
if not HAS_NET then
core.warn(
"Assistant: Pragtical net module is disabled; HTTP providers and web tools are unavailable."
)
end
local AGENT_CHOICES = {
{ "Codex", "codex" },
{ "ACP", "acp" },
{ "GitHub Copilot", "copilot" }
}
if HAS_NET then
AGENT_CHOICES = {
{ "Ollama", "ollama" },
{ "llama.cpp", "llamacpp" },
{ "LM Studio", "lms" },
{ "OpenAI", "openai" },
{ "Codex", "codex" },
{ "ACP", "acp" },
{ "GitHub Copilot", "copilot" },
{ "Anthropic", "anthropic" },
{ "DeepSeek", "deepseek" },
{ "DeepSeek Anthropic", "deepseek_anthropic" }
}
end
config.plugins.assistant = common.merge({
agent = HAS_NET and "ollama" or "codex",
backend = HAS_NET and "http" or "appserver",
agents = agent_config.defaults(),
debug = false,
log_protocol = false,
log_raw_messages = true,
verbose_tool_calling = false,
verbose_activity = false,
reasoning_activity_messages = true,
persist_reasoning_content = true,
raw_markdown_line_wrapping = true,
compact_tool_results = false,
compact_tool_history = false,
generate_conversation_titles = true,
stream = false,
request_timeout_ms = 1800000,
max_tool_call_rounds = 0,
max_repeated_tool_calls = 4,
send_max_tokens = false,
send_max_tokens_amount = 65536,
reasoning_effort = "low",
fetch_model_metadata = true,
auto_compact = true,
auto_compact_threshold = 0.85,
auto_compact_min_input_tokens = 50000,
auto_compact_max_input_tokens = 200000,
auto_compact_min_new_messages = 4,
auto_save = true,
prompt_height = 140,
confirm_writes = true,
allow_any_read_path = false,
web_search_url = "https://search.brave.com/search",
web_search_query_param = "q",
web_search_results_path = "",
web_timeout_ms = 10000,
web_allow_hosts = {},
config_spec = {
name = "Assistant",
{
label = "Agent",
description = "Default assistant provider.",
path = "agent",
type = "selection",
default = HAS_NET and "ollama" or "codex",
values = AGENT_CHOICES
},
{
label = "Agent Settings",
description = "Configure provider-specific model, endpoint, command, and reasoning options.",
type = "subconfig",
title = "Assistant Agent Settings",
spec = agent_config.config_spec()
},
{
label = "Stream Responses",
description = "Use Server-Sent Events streaming when supported.",
path = "stream",
type = "toggle",
default = false
},
{
label = "Allow Any Read Path",
description = "Allow read-only assistant tools to read outside loaded project roots without asking. Writes, patches, deletes, and commands remain project-root restricted.",
path = "allow_any_read_path",
type = "toggle",
default = false
},
{
label = "Web Search URL",
description = "Optional search page or JSON endpoint used by the assistant web_search tool. Leave empty to disable web_search.",
path = "web_search_url",
type = "string",
default = "https://search.brave.com/search"
},
{
label = "Web Search Query Parameter",
description = "Query parameter name used when calling the configured web search endpoint.",
path = "web_search_query_param",
type = "string",
default = "q"
},
{
label = "Request Timeout",
description = "Timeout in milliseconds for assistant provider chat requests.",
path = "request_timeout_ms",
type = "number",
default = 1800000
},
{
label = "Max Tool Call Rounds",
description = "Maximum model/tool continuation rounds allowed for a single assistant turn. Set to 0 to rely on manual cancellation instead of a round cap.",
path = "max_tool_call_rounds",
type = "number",
default = 0,
min = 0,
max = 512
},
{
label = "Max Repeated Tool Calls",
description = "Maximum identical tool calls allowed in one assistant turn before stopping the turn as a loop.",
path = "max_repeated_tool_calls",
type = "number",
default = 4,
min = 1,
max = 64
},
{
label = "Send Max Tokens",
description = "Send a max_tokens limit with OpenAI-compatible chat requests. Disabled by default so providers use their own output limits.",
path = "send_max_tokens",
type = "toggle",
default = false
},
{
label = "Max Tokens Amount",
description = "The max_tokens value to send when Send Max Tokens is enabled.",
path = "send_max_tokens_amount",
type = "number",
default = 65536,
min = 1
},
{
label = "Compact Tool Results",
description = "Compact tool results before sending them back to HTTP/OpenAI-compatible models - experimental.",
path = "compact_tool_results",
type = "toggle",
default = false
},
{
label = "Compact Tool History",
description = "Compact historical tool call/result messages before provider requests - experimental.",
path = "compact_tool_history",
type = "toggle",
default = false
},
{
label = "Reasoning Effort",
description = "Reasoning effort sent to OpenAI Responses and supported OpenAI-compatible chat providers.",
path = "reasoning_effort",
type = "selection",
default = "low",
values = {
{ "None", "none" },
{ "Low", "low" },
{ "Medium", "medium" },
{ "High", "high" }
}
},
{
label = "Auto Compact",
description = "Automatically compact local HTTP conversations before sending when reported context usage is near the model window.",
path = "auto_compact",
type = "toggle",
default = true
},
{
label = "Auto Compact Threshold",
description = "Fraction of the model context window used before automatic local compaction runs.",
path = "auto_compact_threshold",
type = "number",
default = 0.85,
min = 0.01,
max = 0.98
},
{
label = "Auto Compact Min Input Tokens",
description = "Minimum reported input/context tokens required before automatic local compaction can run.",
path = "auto_compact_min_input_tokens",
type = "number",
default = 50000,
min = 0
},
{
label = "Auto Compact Max Input Tokens",
description = "Reported input/context token ceiling that triggers automatic local compaction even before the context threshold is reached. Set 0 to disable.",
path = "auto_compact_max_input_tokens",
type = "number",
default = 200000,
min = 0
},
{
label = "Web Timeout",
description = "Timeout in milliseconds for assistant web tools.",
path = "web_timeout_ms",
type = "number",
default = 10000,
min = 1000,
max = 60000
},
{
label = "Debug Logging",
description = "Log general assistant backend diagnostics for troubleshooting.",
path = "debug",
type = "toggle",
default = false
},
{
label = "Raw Message Logging",
description = "Record per-conversation raw client requests and provider responses for View Raw Responses. Disable to reduce memory and disk usage.",
path = "log_raw_messages",
type = "toggle",
default = true
},
{
label = "Verbose Tool Calling",
description = "Show full tool call and tool result sections in conversation transcripts.",
path = "verbose_tool_calling",
type = "toggle",
default = false
},
{
label = "Verbose Activity",
description = "Show activity messages as full Activity sections instead of compact one-line summaries.",
path = "verbose_activity",
type = "toggle",
default = false
},
{
label = "Reasoning Activity Messages",
description = "Show streamed reasoning and thought text as activity messages in conversation transcripts.",
path = "reasoning_activity_messages",
type = "toggle",
default = true
},
{
label = "Persist Reasoning Content",
description = "Persist and resend provider reasoning_content for HTTP/OpenAI-compatible chat agents.",
path = "persist_reasoning_content",
type = "toggle",
default = true
},
{
label = "Raw Markdown Line Wrapping",
description = "Visually wrap the raw markdown transcript using Pragtical's linewrapping plugin.",
path = "raw_markdown_line_wrapping",
type = "toggle",
default = true
},
{
label = "Generate Conversation Titles",
description = "Generate a concise title from the first user prompt without adding the title request to conversation context.",
path = "generate_conversation_titles",
type = "toggle",
default = true
},
{
label = "Protocol Logging",
description = "Log raw assistant backend requests and responses for troubleshooting.",
path = "log_protocol",
type = "toggle",
default = false
},
{
label = "Prompt Height",
description = "Height in pixels for the prompt editor.",
path = "prompt_height",
type = "number",
default = 140,
min = 80,
max = 500
}
}
}, config.plugins.assistant)
---Assistant plugin module.
---@alias assistant.AgentClass fun(options?: table): assistant.Agent
---@alias assistant.BackendClass fun(name?: string): assistant.Backend
---
---@class assistant
---@field agents table<string, assistant.AgentClass>
---@field backends table<string, assistant.BackendClass>
local assistant = {
agents = {},
backends = {}
}
---Register an assistant agent class.
---@param name string
---@param agent assistant.AgentClass
function assistant.register_agent(name, agent)
assistant.agents[name] = agent
PromptView.register_agent(name, agent)
end
---Remove a registered assistant agent class.
---@param name string
function assistant.unregister_agent(name)
assistant.agents[name] = nil
PromptView.unregister_agent(name)
end
---Configure provider-specific defaults for an assistant agent.
---@param name string Agent id, such as `"ollama"` or `"openai"`.
---@param options assistant.agent.config Agent-specific configuration values.
---@return boolean ok True when the configuration was merged.
function assistant.configure_agent(name, options)
return agent_config.configure(name, options)
end
---Return the resolved configuration for an assistant agent.
---@param name string Agent id.
---@return assistant.agent.config? config Effective configuration, or nil for invalid names.
function assistant.get_agent_config(name)
return agent_config.get(name)
end
---Return an agent class by name or the configured default.
---@param name string?
---@return assistant.AgentClass?
function assistant.get_agent(name)
return assistant.agents[name or config.plugins.assistant.agent]
end
---Return the preferred available agent name.
---@return string?
local function default_agent_name()
local preferred = config.plugins.assistant.agent
if assistant.agents[preferred] then return preferred end
for _, name in ipairs({ "codex", "copilot", "acp" }) do
if assistant.agents[name] then return name end
end
for name in pairs(assistant.agents) do
return name
end
end
---Return registered assistant agents for UI selection.
---@return table[] agents Agent choices with `name`, `label`, and `default` fields.
function assistant.list_agents()
local configured_name = default_agent_name()
local choices = {}
for name, cls in pairs(assistant.agents) do
local label = name
if type(cls) == "table" and type(cls.display_name) == "string" then
label = cls.display_name
elseif type(cls) == "function" or type(cls) == "table" then
local ok, agent = pcall(function() return cls() end)
if ok and type(agent) == "table" and type(agent.display_name) == "string" then
label = agent.display_name
end
end
table.insert(choices, {
name = name,
label = label,
default = name == configured_name
})
end
table.sort(choices, function(a, b)
if a.default ~= b.default then return a.default end
return tostring(a.label):lower() < tostring(b.label):lower()
end)
return choices
end
---Register a communication backend class.
---@param name string
---@param backend assistant.BackendClass
function assistant.register_backend(name, backend)
assistant.backends[name] = backend
PromptView.register_backend(name, backend)
end
---Return a backend class by name or the configured default.
---@param name string?
---@return assistant.BackendClass?
function assistant.get_backend(name)
return assistant.backends[name or config.plugins.assistant.backend]
end
---Handle configured agent.
---@param name string?
---@return assistant.Agent?
local function configured_agent(name)
local requested = name or config.plugins.assistant.agent
local cls = assistant.get_agent(requested)
if not cls then
if name then
core.error(
"Assistant: agent '%s' is unavailable%s",
tostring(name),
HAS_NET and "" or " because the Pragtical net module is disabled"
)
return nil
end
requested = default_agent_name()
cls = requested and assistant.agents[requested] or nil
end
if not cls then
core.error("Assistant: no assistant agents are registered")
return nil
end
local agent = cls()
agent_config.apply(agent, config.plugins.assistant)
return tools.register_agent_tools(agent)
end
---Handle configured backend.
---@param name string?
---@return assistant.Backend?
local function configured_backend(name)
local cls = assistant.get_backend(name)
if not cls then
core.error(
"Assistant: backend '%s' is unavailable%s",
tostring(name or config.plugins.assistant.backend),
HAS_NET and "" or " because the Pragtical net module is disabled"
)
return nil
end
return cls()
end
---Register an assistant tool contributed by another plugin.
---
---The spec uses the same fields as `plugins.assistant.tool`, including
---callback/build functions, schema params, approval hooks, compaction hooks,
---activity rendering hooks, and history-success detection.
---@param name string Tool name exposed to models.
---@param spec assistant.ToolSpec Full assistant tool specification.
---@return boolean ok True when the tool was registered.
function assistant.register_tool(name, spec)
if type(name) ~= "string" or name == "" then
core.error("Assistant: register_tool requires a tool name")
return false
end
if type(spec) ~= "table" then
core.error("Assistant: register_tool requires a tool spec for %s", name)
return false
end
spec.name = name
local ok, err = tools.register_external_tool(name, spec)
if not ok then
core.error("Assistant: could not register tool %s: %s", name, tostring(err))
return false
end
return true
end
---Remove an assistant tool contributed by another plugin.
---@param name string Tool name previously passed to `assistant.register_tool`.
---@return boolean removed True when a registered external tool was removed.
function assistant.unregister_tool(name)
if type(name) ~= "string" or name == "" then
core.error("Assistant: unregister_tool requires a tool name")
return false
end
return tools.unregister_external_tool(name)
end
---Open prompt view.
---@param view assistant.PromptView
---@return assistant.PromptView
local function open_prompt_view(view)
local node = core.root_view:get_active_node_default()
node:add_view(view)
core.root_view.root_node:update_layout()
view.focused_child = view.prompt
core.set_active_view(view.prompt)
return view
end
---Open a new assistant conversation view.
---@param agent_name string?
---@return assistant.PromptView
function assistant.start_conversation(agent_name)
local agent = configured_agent(agent_name)
if not agent then return end
local backend = configured_backend(agent.backend)
if not backend then return end
return open_prompt_view(PromptView({ agent = agent, backend = backend }))
end
---Open a new assistant conversation after selecting an agent.
---@return assistant.PromptView?
function assistant.select_agent_new_conversation()
local choices = assistant.list_agents()
if #choices == 0 then
core.error("Assistant: no agents are registered")
return
end
if #choices == 1 then
return assistant.start_conversation(choices[1].name)
end
local suggestions = {}
local by_text = {}
for _, choice in ipairs(choices) do
local item = {
text = choice.label,
name = choice.name,
info = choice.default and "Default agent" or choice.name
}
table.insert(suggestions, item)
by_text[choice.name] = item
by_text[choice.label] = item
end
local function matching_suggestion(text, suggestion)
return suggestion or by_text[tostring(text or "")]
end
core.command_view:enter("Assistant Agent", {
show_suggestions = true,
typeahead = true,
suggest = function()
return suggestions
end,
validate = function(text, suggestion)
return matching_suggestion(text, suggestion) ~= nil
end,
submit = function(text, suggestion)
local item = matching_suggestion(text, suggestion)
if item then
assistant.start_conversation(item.name)
end
end
})
end
local function conversation_suggestion_text(item)
local title = tostring(item.title or item.name or item.preview or "Assistant Session")
local updated = tostring(item.updated_at or item.created_at or "")
if updated ~= "" then
return title .. " - " .. updated
end
return title
end
local function conversation_suggestion_info(item)
local parts = {}
if item.agent and item.agent ~= "" then table.insert(parts, item.agent) end
if item.model and item.model ~= "" then table.insert(parts, item.model) end
if item.id and item.id ~= "" then table.insert(parts, item.id) end
return table.concat(parts, " ")
end
local function conversation_picker_items(project_dir)
local suggestions = {}
local captions = {}
local by_text = {}
local by_id = {}
for index, item in ipairs(Conversation.list(project_dir)) do
local text = conversation_suggestion_text(item)
if by_text[text] then
text = text .. " - " .. tostring(item.id or index)
end
local suggestion = {
text = text,
info = conversation_suggestion_info(item),
id = item.id,
item = item
}
table.insert(suggestions, suggestion)
table.insert(captions, text)
by_text[text] = suggestion
if item.id and item.id ~= "" then by_id[tostring(item.id)] = suggestion end
end
return suggestions, captions, by_text, by_id
end
---Prompt for a saved assistant conversation and resume it.
---@param project_dir string?
function assistant.select_resume_conversation(project_dir)
project_dir = project_dir or core.root_project().path
local suggestions, captions, by_text, by_id = conversation_picker_items(project_dir)
if #suggestions == 0 then
core.warn("Assistant: no saved conversations for %s", project_dir)
return
end
local function matching_suggestion(text, suggestion)
text = tostring(text or "")
return suggestion or by_id[text] or by_text[text]
end
core.command_view:enter("Assistant Session", {
show_suggestions = true,
typeahead = true,
suggest = function(text)
text = tostring(text or "")
if text == "" then return suggestions end
local result = {}
for _, caption in ipairs(common.fuzzy_match(captions, text, true)) do
table.insert(result, by_text[caption])
end
return result
end,
validate = function(text, suggestion)
return matching_suggestion(text, suggestion) ~= nil
end,
submit = function(text, suggestion)
local selected = matching_suggestion(text, suggestion)
if selected then
assistant.resume_conversation(selected.id, project_dir)
end
end
})
end
---Open a saved assistant conversation.
---@param id string
---@param project_dir string?
---@return assistant.PromptView?
function assistant.resume_conversation(id, project_dir)
local conversation = Conversation.load(id, project_dir or core.root_project().path)
if not conversation then
core.error("Assistant: could not load conversation %s", tostring(id))
return
end
local agent = configured_agent(conversation.agent)
if not agent then return end
agent.model = conversation.model or agent.model
conversation.backend = agent.backend
local backend = configured_backend(agent.backend)
if not backend then return end
return open_prompt_view(PromptView({
conversation = conversation,
agent = agent,
backend = backend
}))
end
---Open a conversation list item.
---@param item table
---@param project_dir string?
---@return assistant.PromptView?
function assistant.open_conversation_item(item, project_dir)
return assistant.resume_conversation(item.id, project_dir)
end
---Persist a conversation to disk.
---@param conversation assistant.Conversation?
---@return boolean?
function assistant.save_conversation(conversation)
return conversation and conversation:save()
end
---Delete a saved conversation from disk.
---@param id string
---@param project_dir string?
---@param callback fun(deleted: boolean)?
---@return boolean deleted
function assistant.delete_conversation(id, project_dir, callback)
project_dir = project_dir or core.root_project().path
local deleted = Conversation.delete(id, project_dir)
if deleted then
core.log("Assistant: deleted conversation %s", id)
else
core.warn("Assistant: conversation not found: %s", id)
end
if callback then callback(deleted) end
return deleted
end
---Open the saved conversation list.
---@param project_dir string?
---@return assistant.ui.ConversationsList
function assistant.list_conversations(project_dir)
project_dir = project_dir or core.root_project().path
local view = ConversationsList(project_dir, function(item)
assistant.open_conversation_item(item, project_dir)
end, function(item, delete_project_dir, callback)
assistant.delete_conversation(item.id, delete_project_dir, callback)
end)
local node = core.root_view:get_active_node_default()
node:add_view(view)
core.root_view.root_node:update_layout()
return view
end
---Open the saved memories list.
---@param project_dir string?
---@return assistant.ui.MemoriesList
function assistant.list_memories(project_dir)
project_dir = project_dir or core.root_project().path
local view = MemoriesList(project_dir, function(item, list_view)
local editor = MemoriesList.MemoryEditor(project_dir, item, function()
list_view:refresh()
end)
local node = core.root_view:get_active_node_default()
node:add_view(editor)
core.root_view.root_node:update_layout()
end, function(item, delete_project_dir, callback)
local deleted = Conversation.delete_memory(delete_project_dir, item.id)
if deleted then
core.log("Assistant: deleted memory %s", item.id)
else
core.warn("Assistant: memory not found: %s", item.id)
end
if callback then callback(deleted) end
end)
local node = core.root_view:get_active_node_default()
node:add_view(view)
core.root_view.root_node:update_layout()
return view
end
---Return path relative to the active project when possible.
---@param filename string
---@return string
local function prompt_file_path(filename)
local project = core.current_project(filename) or core.root_project()
local info = system.get_file_info(filename)
local is_dir = info and info.type == "dir"
local trailing_sep = is_dir and PATHSEP or ""
if project and common.path_belongs_to(filename, project.path) then
return common.relative_path(project.path, filename):gsub(PATHSEP .. "$", "") .. trailing_sep
end
return common.home_encode(filename):gsub(PATHSEP .. "$", "") .. trailing_sep
end
---Insert selected project file path into a conversation prompt.
---@param view assistant.PromptView
---@param filename string
---@param line? integer
local function insert_project_file_path(view, filename, line)
local text = prompt_file_path(filename)
if line then
text = string.format("%s:%d", text, line)
end
view.prompt_doc:text_input(text)
core.set_active_view(view.prompt)
end
---Log the model list for an agent.
---@param agent_name string?
function assistant.list_models(agent_name)
local agent = configured_agent(agent_name)
if not agent then return end
local backend = configured_backend(agent.backend)
if not backend then return end
backend:list_models(agent, function(ok, err, models)
if not ok then
core.error("Assistant: could not list models: %s", err or "unknown error")
return
end
if not models or #models == 0 then
core.log("Assistant: no models reported by %s", agent.display_name or agent.name)
return
end
core.log("Assistant models from %s:", agent.display_name or agent.name)
for _, model in ipairs(models) do
core.log(" %s", model)
end
end)
end
if HAS_NET then
assistant.register_agent("ollama", Ollama)
assistant.register_agent("llamacpp", LlamaCpp)
assistant.register_agent("lms", Lms)
assistant.register_agent("openai", OpenAI)
end
assistant.register_agent("codex", Codex)
assistant.register_agent("acp", Acp)
assistant.register_agent("copilot", Copilot)
if HAS_NET then
assistant.register_agent("anthropic", Anthropic)
assistant.register_agent("deepseek", DeepSeek)
assistant.register_agent("deepseek_anthropic", DeepSeekAnthropic)
assistant.register_backend("http", HttpBackend)
assistant.register_backend("anthropic", AnthropicBackend)
end
assistant.register_backend("cli", CliBackend)
assistant.register_backend("appserver", AppServerBackend)
assistant.register_backend("acp", AcpBackend)
command.add(nil, {
["assistant:new-conversation"] = function()
assistant.start_conversation()
end,
["assistant:select-agent-new-conversation"] = function()
assistant.select_agent_new_conversation()
end,
["assistant:list-conversations"] = function()
assistant.list_conversations()
end,
["assistant:list-models"] = function()
assistant.list_models()
end,
["assistant:resume-conversation"] = function()
assistant.select_resume_conversation()
end,
["assistant:delete-conversation"] = function()
core.command_view:enter("Delete Assistant Session ID", {
submit = function(id)
assistant.delete_conversation(id, core.root_project().path)
end
})
end,
["assistant:add-memory"] = function()
core.command_view:enter("Assistant Memory", {
submit = function(text)
local item = Conversation.add_memory(core.root_project().path, "Memory", text)
if item then
core.log("Assistant: added memory %s", item.id)
end
end
})
end,
["assistant:list-memories"] = function()
assistant.list_memories()
end,
["assistant:delete-memory"] = function()
core.command_view:enter("Delete Assistant Memory ID", {
submit = function(id)
if Conversation.delete_memory(core.root_project().path, id) then
core.log("Assistant: deleted memory %s", id)
else
core.warn("Assistant: memory not found: %s", id)
end
end
})
end
})
command.add(PromptView.active_predicate, {
["assistant-conversation:send"] = function(view)
view:submit()
end
})
command.add(PromptView.active_predicate, {
["assistant-conversation:select-model"] = function(view)
view:open_model_dialog()
end,
["assistant-conversation:configure-agent"] = function(view)
view:configure_agent()
end,
["assistant-conversation:cancel"] = function(view)
view:cancel()
end,
["assistant-conversation:save"] = function(view)
view:save_conversation()
end,
["assistant-conversation:view-raw-responses"] = function(view)
view:view_raw_responses()
end,
["assistant-conversation:view-raw-markdown"] = function(view)
view:view_raw_markdown()
end,
["assistant-conversation:view-rendered-markdown"] = function(view)
view:view_rendered_markdown()
end,
["assistant-conversation:clear-prompt"] = function(view)
view:clear_prompt()
end,
["assistant-conversation:cycle-mode"] = function(view)
view:cycle_collaboration_mode()
end,
["assistant-conversation:respond-to-request"] = function(view)
view:respond_to_pending_request()
end,
["assistant-conversation:insert-file"] = function(view)
command.perform("core:open-file", "Insert File", function(filename)
insert_project_file_path(view, filename)
end, true)
end,
["assistant-conversation:insert-project-file"] = function(view)
command.perform("core:find-file", "Insert Project File", function(filename, line)
insert_project_file_path(view, filename, line)
end)
end,
["assistant-conversation:rename"] = function(view)
core.command_view:enter("Conversation Title", {
submit = function(title)
view:rename_conversation(title)
end
})
end
})
command.add(PromptView.compact_predicate, {
["assistant-conversation:compact"] = function(view)
view:compact()
end
})
keymap.add {
["ctrl+alt+a"] = "assistant:new-conversation",
["ctrl+shift+alt+a"] = "assistant:select-agent-new-conversation",
["ctrl+enter"] = "assistant-conversation:send",
["ctrl+return"] = "assistant-conversation:send",
["ctrl+m"] = "assistant-conversation:select-model",
["shift+tab"] = "assistant-conversation:cycle-mode",
["escape"] = "assistant-conversation:cancel",
["ctrl+alt+enter"] = "assistant-conversation:respond-to-request",
["ctrl+alt+return"] = "assistant-conversation:respond-to-request",
["ctrl+alt+u"] = "assistant-conversation:insert-file",
["ctrl+shift+u"] = "assistant-conversation:insert-project-file",
["ctrl+backspace"] = "assistant-conversation:clear-prompt"
}
return assistant