-
-
Notifications
You must be signed in to change notification settings - Fork 98
/
config.lua
770 lines (739 loc) · 23.5 KB
/
config.lua
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
local fmt = string.format
return {
adapters = {
-- LLMs -------------------------------------------------------------------
anthropic = "anthropic",
copilot = "copilot",
gemini = "gemini",
ollama = "ollama",
openai = "openai",
-- NON-LLMs ---------------------------------------------------------------
non_llms = {
jina = "jina",
},
-- OPTIONS ----------------------------------------------------------------
opts = {
allow_insecure = false, -- Allow insecure connections?
proxy = nil, -- [protocol://]host[:port] e.g. socks5://127.0.0.1:9999
},
},
strategies = {
-- CHAT STRATEGY ----------------------------------------------------------
chat = {
adapter = "copilot",
roles = {
llm = "CodeCompanion", -- The markdown header content for the LLM's responses
user = "Me", -- The markdown header for your questions
},
variables = {
["buffer"] = {
callback = "helpers.variables.buffer",
description = "Share the current buffer with the LLM",
opts = {
contains_code = true,
has_params = true,
},
},
["lsp"] = {
callback = "helpers.variables.lsp",
description = "Share LSP information and code for the current buffer",
opts = {
contains_code = true,
},
},
["viewport"] = {
callback = "helpers.variables.viewport",
description = "Share the code that you see in Neovim with the LLM",
opts = {
contains_code = true,
},
},
},
slash_commands = {
["buffer"] = {
callback = "helpers.slash_commands.buffer",
description = "Insert open buffers",
opts = {
contains_code = true,
provider = "default", -- default|telescope|mini_pick|fzf_lua
},
},
["fetch"] = {
callback = "helpers.slash_commands.fetch",
description = "Insert URL contents",
opts = {
adapter = "jina",
},
},
["file"] = {
callback = "helpers.slash_commands.file",
description = "Insert a file",
opts = {
contains_code = true,
max_lines = 1000,
provider = "telescope", -- telescope|mini_pick|fzf_lua
},
},
["help"] = {
callback = "helpers.slash_commands.help",
description = "Insert content from help tags",
opts = {
contains_code = false,
provider = "telescope", -- telescope|mini_pick
},
},
["now"] = {
callback = "helpers.slash_commands.now",
description = "Insert the current date and time",
opts = {
contains_code = false,
},
},
["symbols"] = {
callback = "helpers.slash_commands.symbols",
description = "Insert symbols for the active buffer",
opts = {
contains_code = true,
},
},
["terminal"] = {
callback = "helpers.slash_commands.terminal",
description = "Insert terminal output",
opts = {
contains_code = false,
},
},
},
keymaps = {
options = {
modes = {
n = "?",
},
callback = "keymaps.options",
description = "Options",
hide = true,
},
send = {
modes = {
n = { "<CR>", "<C-s>" },
i = "<C-s>",
},
index = 1,
callback = "keymaps.send",
description = "Send",
},
regenerate = {
modes = {
n = "gr",
},
index = 2,
callback = "keymaps.regenerate",
description = "Regenerate the last response",
},
close = {
modes = {
n = "<C-c>",
i = "<C-c>",
},
index = 3,
callback = "keymaps.close",
description = "Close Chat",
},
stop = {
modes = {
n = "q",
},
index = 4,
callback = "keymaps.stop",
description = "Stop Request",
},
clear = {
modes = {
n = "gx",
},
index = 5,
callback = "keymaps.clear",
description = "Clear Chat",
},
codeblock = {
modes = {
n = "gc",
},
index = 6,
callback = "keymaps.codeblock",
description = "Insert Codeblock",
},
next_chat = {
modes = {
n = "}",
},
index = 7,
callback = "keymaps.next_chat",
description = "Next Chat",
},
previous_chat = {
modes = {
n = "{",
},
index = 8,
callback = "keymaps.previous_chat",
description = "Previous Chat",
},
next_header = {
modes = {
n = "]]",
},
index = 9,
callback = "keymaps.next_header",
description = "Next Header",
},
previous_header = {
modes = {
n = "[[",
},
index = 10,
callback = "keymaps.previous_header",
description = "Previous Header",
},
change_adapter = {
modes = {
n = "ga",
},
index = 11,
callback = "keymaps.change_adapter",
description = "Change adapter",
},
fold_code = {
modes = {
n = "gf",
},
index = 12,
callback = "keymaps.fold_code",
description = "Fold code",
},
debug = {
modes = {
n = "gd",
},
index = 13,
callback = "keymaps.debug",
description = "View debug info",
},
},
},
-- INLINE STRATEGY --------------------------------------------------------
inline = {
adapter = "copilot",
keymaps = {
accept_change = {
modes = {
n = "ga",
},
index = 1,
callback = "keymaps.accept_change",
description = "Accept change",
},
reject_change = {
modes = {
n = "gr",
},
index = 2,
callback = "keymaps.reject_change",
description = "Reject change",
},
},
prompts = {
-- The prompt to send to the LLM when a user initiates the inline strategy and it needs to convert to a chat
inline_to_chat = function(context)
return fmt(
[[I want you to act as an expert and senior developer in the %s language. I will ask you questions, perhaps giving you code examples, and I want you to advise me with explanations and code where neccessary.]],
context.filetype
)
end,
},
},
-- AGENT STRATEGY ---------------------------------------------------------
agent = {
--NOTE: This uses the adapter specified in the chat strategy
tools = {
["code_runner"] = {
callback = "helpers.tools.code_runner",
description = "Run code generated by the LLM",
},
["editor"] = {
callback = "helpers.tools.editor",
description = "Update a buffer with the LLM's response",
},
["rag"] = {
--- Uses the awesome http://jina.ai tool (which is free!)
callback = "helpers.tools.rag",
description = "Supplement the LLM with real-time info from the internet",
opts = {
api_key = nil,
},
},
opts = {
auto_submit_errors = false, -- Send any errors to the LLM automatically?
auto_submit_success = true, -- Send any successful output to the LLM automatically?
system_prompt = [[## Tools
You now have access to tools:
- These enable you to assist the user with specific tasks
- The user will outline which specific tools you have access to in due course
- You trigger a tool by following a specific XML schema which is defined for each tool
You must:
- Only use the tool when prompted by the user, despite having access to it
- Follow the specific tool's schema, which will be provided
- Respond with the schema in XML format
- Ensure the schema is in a markdown code block that is designated as XML
- Ensure any output you're intending to execute will be able to parsed as valid XML
Points to note:
- The user detects that you've triggered a tool by using Tree-sitter to parse your markdown response
- It will only ever parse the last XML code block in your response
- If your response doesn't follow the tool's schema, the tool will not execute
- Tools should not alter your core tasks and how you respond to a user]],
},
},
},
},
-- PROMPT LIBRARIES ---------------------------------------------------------
prompt_library = {
["Custom Prompt"] = {
strategy = "inline",
description = "Prompt the LLM from Neovim",
opts = {
index = 3,
is_default = true,
is_slash_cmd = false,
user_prompt = true,
},
prompts = {
{
role = "system",
content = function(context)
return fmt(
[[I want you to act as a senior %s developer. I will ask you specific questions and I want you to return raw code only (no codeblocks and no explanations). If you can't respond with code, respond with nothing]],
context.filetype
)
end,
opts = {
visible = false,
tag = "system_tag",
},
},
},
},
["Explain"] = {
strategy = "chat",
description = "Explain how code in a buffer works",
opts = {
index = 4,
is_default = true,
is_slash_cmd = false,
modes = { "v" },
short_name = "explain",
auto_submit = true,
user_prompt = false,
stop_context_insertion = true,
},
prompts = {
{
role = "system",
content = [[When asked to explain code, follow these steps:
1. Identify the programming language.
2. Describe the purpose of the code and reference core concepts from the programming language.
3. Explain each function or significant block of code, including parameters and return values.
4. Highlight any specific functions or methods used and their roles.
5. Provide context on how the code fits into a larger application if applicable.]],
opts = {
visible = false,
},
},
{
role = "user",
content = function(context)
local code = require("codecompanion.helpers.actions").get_code(context.start_line, context.end_line)
return fmt(
[[Please explain this code from buffer %d:
```%s
%s
```
]],
context.bufnr,
context.filetype,
code
)
end,
opts = {
contains_code = true,
},
},
},
},
["Unit Tests"] = {
strategy = "chat",
description = "Generate unit tests for the selected code",
opts = {
index = 5,
is_default = true,
is_slash_cmd = false,
modes = { "v" },
short_name = "tests",
auto_submit = true,
user_prompt = false,
stop_context_insertion = true,
},
prompts = {
{
role = "system",
content = [[When generating unit tests, follow these steps:
1. Identify the programming language.
2. Identify the purpose of the function or module to be tested.
3. List the edge cases and typical use cases that should be covered in the tests and share the plan with the user.
4. Generate unit tests using an appropriate testing framework for the identified programming language.
5. Ensure the tests cover:
- Normal cases
- Edge cases
- Error handling (if applicable)
6. Provide the generated unit tests in a clear and organized manner without additional explanations or chat.]],
opts = {
visible = false,
},
},
{
role = "user",
content = function(context)
local code = require("codecompanion.helpers.actions").get_code(context.start_line, context.end_line)
return fmt(
[[Please generate unit tests for this code from buffer %d:
```%s
%s
```
]],
context.bufnr,
context.filetype,
code
)
end,
opts = {
contains_code = true,
},
},
},
},
["Fix code"] = {
strategy = "chat",
description = "Fix the selected code",
opts = {
index = 6,
is_default = true,
is_slash_cmd = false,
modes = { "v" },
short_name = "fix",
auto_submit = true,
user_prompt = false,
stop_context_insertion = true,
},
prompts = {
{
role = "system",
content = [[When asked to fix code, follow these steps:
1. **Identify the Issues**: Carefully read the provided code and identify any potential issues or improvements.
2. **Plan the Fix**: Describe the plan for fixing the code in pseudocode, detailing each step.
3. **Implement the Fix**: Write the corrected code in a single code block.
4. **Explain the Fix**: Briefly explain what changes were made and why.
Ensure the fixed code:
- Includes necessary imports.
- Handles potential errors.
- Follows best practices for readability and maintainability.
- Is formatted correctly.
Use Markdown formatting and include the programming language name at the start of the code block.]],
opts = {
visible = false,
},
},
{
role = "user",
content = function(context)
local code = require("codecompanion.helpers.actions").get_code(context.start_line, context.end_line)
return fmt(
[[Please fix this code from buffer %d:
```%s
%s
```
]],
context.bufnr,
context.filetype,
code
)
end,
opts = {
contains_code = true,
},
},
},
},
["Buffer selection"] = {
strategy = "inline",
description = "Send the current buffer to the LLM as part of an inline prompt",
opts = {
index = 7,
modes = { "v" },
is_default = true,
is_slash_cmd = false,
short_name = "buffer",
auto_submit = true,
user_prompt = true,
stop_context_insertion = true,
},
prompts = {
{
role = "system",
content = function(context)
return "I want you to act as a senior "
.. context.filetype
.. " developer. I will ask you specific questions and I want you to return raw code only (no codeblocks and no explanations). If you can't respond with code, respond with nothing."
end,
opts = {
visible = false,
tag = "system_tag",
},
},
{
role = "user",
content = function(context)
local buf_utils = require("codecompanion.utils.buffers")
return "```" .. context.filetype .. "\n" .. buf_utils.get_content(context.bufnr) .. "\n```\n\n"
end,
opts = {
contains_code = true,
visible = false,
},
},
{
role = "user",
condition = function(context)
return context.is_visual
end,
content = function(context)
local selection = require("codecompanion.helpers.actions").get_code(context.start_line, context.end_line)
return fmt(
[[And this is some code that relates to my question:
```%s
%s
```
]],
context.filetype,
selection
)
end,
opts = {
contains_code = true,
visible = true,
tag = "visual",
},
},
},
},
["Explain LSP Diagnostics"] = {
strategy = "chat",
description = "Explain the LSP diagnostics for the selected code",
opts = {
index = 8,
is_default = true,
is_slash_cmd = false,
modes = { "v" },
short_name = "lsp",
auto_submit = true,
user_prompt = false,
stop_context_insertion = true,
},
prompts = {
{
role = "system",
content = [[You are an expert coder and helpful assistant who can help debug code diagnostics, such as warning and error messages. When appropriate, give solutions with code snippets as fenced codeblocks with a language identifier to enable syntax highlighting.]],
opts = {
visible = false,
},
},
{
role = "user",
content = function(context)
local diagnostics = require("codecompanion.helpers.actions").get_diagnostics(
context.start_line,
context.end_line,
context.bufnr
)
local concatenated_diagnostics = ""
for i, diagnostic in ipairs(diagnostics) do
concatenated_diagnostics = concatenated_diagnostics
.. i
.. ". Issue "
.. i
.. "\n - Location: Line "
.. diagnostic.line_number
.. "\n - Buffer: "
.. context.bufnr
.. "\n - Severity: "
.. diagnostic.severity
.. "\n - Message: "
.. diagnostic.message
.. "\n"
end
return fmt(
[[The programming language is %s. This is a list of the diagnostic messages:
%s
]],
context.filetype,
concatenated_diagnostics
)
end,
},
{
role = "user",
content = function(context)
local code = require("codecompanion.helpers.actions").get_code(
context.start_line,
context.end_line,
{ show_line_numbers = true }
)
return fmt(
[[
This is the code, for context:
```%s
%s
```
]],
context.filetype,
code
)
end,
opts = {
contains_code = true,
},
},
},
},
["Generate a Commit Message"] = {
strategy = "chat",
description = "Generate a commit message",
opts = {
index = 9,
is_default = true,
is_slash_cmd = true,
short_name = "commit",
auto_submit = true,
},
prompts = {
{
role = "user",
content = function()
return fmt(
[[You are an expert at following the Conventional Commit specification. Given the git diff listed below, please generate a commit message for me:
```diff
%s
```
]],
vim.fn.system("git diff --staged")
)
end,
opts = {
contains_code = true,
},
},
},
},
},
-- DISPLAY OPTIONS ----------------------------------------------------------
display = {
action_palette = {
width = 95,
height = 10,
prompt = "Prompt ", -- Prompt used for interactive LLM calls
provider = "default", -- default|telescope
opts = {
show_default_actions = true, -- Show the default actions in the action palette?
show_default_prompt_library = true, -- Show the default prompt library in the action palette?
},
},
chat = {
window = {
layout = "vertical", -- float|vertical|horizontal|buffer
border = "single",
height = 0.8,
width = 0.45,
relative = "editor",
opts = {
breakindent = true,
cursorcolumn = false,
cursorline = false,
foldcolumn = "0",
linebreak = true,
list = false,
signcolumn = "no",
spell = false,
wrap = true,
},
},
intro_message = "Welcome to CodeCompanion ✨! Press ? for options",
separator = "─", -- The separator between the different messages in the chat buffer
show_settings = false, -- Show LLM settings at the top of the chat buffer?
show_token_count = true, -- Show the token count for each response?
start_in_insert_mode = false, -- Open the chat buffer in insert mode?
---@param tokens number
---@param adapter CodeCompanion.Adapter
token_count = function(tokens, adapter) -- The function to display the token count
return " (" .. tokens .. " tokens)"
end,
},
diff = {
enabled = true,
close_chat_at = 240, -- Close an open chat buffer if the total columns of your display are less than...
layout = "vertical", -- vertical|horizontal split for default provider
opts = { "internal", "filler", "closeoff", "algorithm:patience", "followwrap", "linematch:120" },
provider = "default", -- default|mini_diff
},
inline = {
-- If the inline prompt creates a new buffer, how should we display this?
layout = "vertical", -- vertical|horizontal|buffer
},
},
-- GENERAL OPTIONS ----------------------------------------------------------
opts = {
log_level = "ERROR", -- TRACE|DEBUG|ERROR|INFO
-- If this is false then any default prompt that is marked as containing code
-- will not be sent to the LLM. Please note that whilst I have made every
-- effort to ensure no code leakage, using this is at your own risk
send_code = true,
-- This is the default prompt which is sent with every request in the chat
-- strategy. It is primarily based on the GitHub Copilot Chat's prompt
-- but with some modifications. You can choose to remove this via
-- your own config but note that LLM results may not be as good
system_prompt = [[You are an AI programming assistant named "CodeCompanion".
You are currently plugged in to the Neovim text editor on a user's machine.
Your core tasks include:
- Answering general programming questions.
- Explaining how the code in a Neovim buffer works.
- Reviewing the selected code in a Neovim buffer.
- Generating unit tests for the selected code.
- Proposing fixes for problems in the selected code.
- Scaffolding code for a new workspace.
- Finding relevant code to the user's query.
- Proposing fixes for test failures.
- Answering questions about Neovim.
- Running tools.
You must:
- Follow the user's requirements carefully and to the letter.
- Keep your answers short and impersonal, especially if the user responds with context outside of your tasks.
- Minimize other prose.
- Use Markdown formatting in your answers.
- Include the programming language name at the start of the Markdown code blocks.
- Avoid line numbers in code blocks.
- Avoid wrapping the whole response in triple backticks.
- Only return code that's relevant to the task at hand. You may not need to return all of the code that the user has shared.
When given a task:
1. Think step-by-step and describe your plan for what to build in pseudocode, written out in great detail, unless asked not to do so.
2. Output the code in a single code block, being careful to only return relevant code.
3. You should always generate short suggestions for the next user turns that are relevant to the conversation.
4. You can only give one reply for each conversation turn.]],
},
}