Repository navigation
Expand file tree
/
Copy pathcopilot-chat.lua
More file actions
357 lines (349 loc) · 9.68 KB
/
Copy pathcopilot-chat.lua
File metadata and controls
357 lines (349 loc) · 9.68 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
local function moonshot_api_key()
local key = vim.env.NVIM_AI_API_KEY
if type(key) ~= "string" or key == "" then
error("未设置环境变量 NVIM_AI_API_KEY")
end
return key:gsub("^%s*[Bb]earer%s+", ""):gsub("%s+$", "")
end
local function volcano_api_key()
local key = vim.env.NVIM_VOLCE_AI_API_KEY
if type(key) ~= "string" or key == "" then
error("未设置环境变量 NVIM_VOLCE_AI_API_KEY")
end
return key:gsub("^%s*[Bb]earer%s+", ""):gsub("%s+$", "")
end
return {
{
"zbirenbaum/copilot.lua",
cmd = "Copilot",
enabled = true,
-- 认证由用户显式执行 :Copilot auth,安装/更新插件不打开授权流程。
event = "BufReadPost",
opts = {
suggestion = {
enabled = false,
auto_trigger = true,
hide_during_completion = true,
keymap = {
accept = false,
},
},
panel = { enabled = false },
filetypes = {
markdown = false,
help = true,
},
},
},
{
"CopilotC-Nvim/CopilotChat.nvim",
branch = "main",
cmd = "CopilotChat",
build = "make tiktoken",
dependencies = {
{ "nvim-lua/plenary.nvim", branch = "master" },
},
opts = function()
local provider_helpers = require("CopilotChat.config.providers")
return {
model = "glm-5.3",
providers = {
-- Copilot 仅用于代码补全;CopilotChat 通过第三方 OpenAI 兼容 API 请求模型。
copilot = { disabled = true },
moonshot = {
get_url = function()
return "https://api.moonshot.cn/v1/chat/completions"
end,
get_headers = function()
return {
Authorization = "Bearer " .. moonshot_api_key(),
["Content-Type"] = "application/json",
}
end,
get_models = function()
return {
{
id = "kimi-k2.7-code",
name = "Kimi K2.7 Code",
streaming = true,
tools = true,
},
}
end,
prepare_input = function(inputs, opts)
local request, headers = provider_helpers.copilot.prepare_input(inputs, opts)
-- kimi-k2.7-code 仅接受以下固定采样参数。
request.temperature = 1
request.top_p = 0.95
return request, headers
end,
prepare_output = function(output, opts)
local response = provider_helpers.copilot.prepare_output(output, opts)
-- Kimi 的思考过程不写入 CopilotChat,只显示最终答案。
response.reasoning = nil
return response
end,
},
-- 火山方舟 Coding Plan:必须使用 /api/coding/v3 端点与套餐模型名,按订阅额度抵扣。
volcano = {
get_url = function()
return "https://ark.cn-beijing.volces.com/api/coding/v3/chat/completions"
end,
get_headers = function()
return {
Authorization = "Bearer " .. volcano_api_key(),
["Content-Type"] = "application/json",
}
end,
get_models = function()
return {
{
id = "glm-5.3-flash",
name = "GLM-5.3-Flash (Coding Plan)",
streaming = true,
tools = true,
},
{
id = "glm-5.3",
name = "GLM-5.3 (Coding Plan)",
streaming = true,
tools = true,
},
}
end,
prepare_input = function(inputs, opts)
return provider_helpers.copilot.prepare_input(inputs, opts)
end,
prepare_output = function(output, opts)
local response = provider_helpers.copilot.prepare_output(output, opts)
-- GLM 的思考过程不写入 CopilotChat,只显示最终答案。
response.reasoning = nil
return response
end,
},
},
resources = "selection",
language = "Chinese",
auto_insert_mode = false,
insert_at_end = true,
headers = {
user = GlobalUtil.icons.kinds.User,
assistant = GlobalUtil.icons.kinds.Copilot,
tool = GlobalUtil.icons.kinds.Tool,
},
separator = "——",
window = {
border = "double",
},
prompts = {
Translate = {
prompt = "Translate selected code comments into Chinese.",
mapping = "<leader>at",
},
},
mappings = {
close = {
normal = "q",
insert = "<c-a>",
},
complete = {
detail = "Use @<Tab> or /<Tab> for options.",
insert = "<Tab>",
},
reset = {
normal = "<C-x>",
insert = "<C-x>",
},
submit_prompt = {
normal = "<CR>",
insert = "<C-s>",
},
toggle_sticky = {
normal = "grr",
},
clear_stickies = {
normal = "grx",
},
accept_diff = {
normal = "<C-y>",
insert = "<C-y>",
},
jump_to_diff = {
normal = "gj",
},
quickfix_answers = {
normal = "gqa",
},
quickfix_diffs = {
normal = "gqd",
},
yank_diff = {
normal = "gy",
register = '"',
},
show_diff = {
normal = "gd",
full_diff = false,
},
show_info = {
normal = "gi",
},
show_context = {
normal = "gc",
},
show_help = {
normal = "gh",
},
},
}
end,
keys = {
{ "<c-s>", "<CR>", ft = "copilot-chat", desc = "Submit Prompt", remap = true },
{
"<C-a>",
function()
local select = require("CopilotChat.select")
local mode = vim.api.nvim_get_mode().mode
return require("CopilotChat").toggle(
(mode == "v" or mode == "V") and { selection = select.visual } or nil
)
end,
desc = "Toggle (CopilotChat)",
remap = true,
mode = { "n", "v", "i" },
},
{ "<leader>a", "", desc = "+ai", mode = { "n", "v" } },
{
"<leader>aa",
function()
local select = require("CopilotChat.select")
local mode = vim.api.nvim_get_mode().mode
return require("CopilotChat").toggle(
(mode == "v" or mode == "V") and { selection = select.visual } or nil
)
end,
desc = "Toggle (CopilotChat)",
mode = { "n", "v" },
},
{
"<leader>ai",
function()
local chat = require("CopilotChat")
local select = require("CopilotChat.select")
local win = vim.api.nvim_get_current_win()
local win_height = vim.api.nvim_win_get_height(win)
local cursor = vim.api.nvim_win_get_cursor(win)
local textoff = vim.fn.getwininfo(win)[1].textoff
-- inline 浮窗按当前窗口实际尺寸定位,避开侧边栏与行号列;
-- 光标贴近底部时改为在光标上方展开,保证输入栏可见。
local height = math.max(10, math.floor(win_height * 0.4))
local row = cursor[1]
if row + height > win_height then
row = math.max(1, cursor[1] - height - 1)
end
chat.ask("解释这部分代码", {
selection = select.visual,
window = {
layout = "float",
relative = "win",
width = vim.api.nvim_win_get_width(win) - textoff,
height = height,
row = row,
col = textoff,
},
})
end,
mode = { "v", "n" },
desc = "CopilotChat - Inline chat",
},
{
"<leader>ax",
function()
require("CopilotChat").reset()
vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes("<Esc>", true, false, true), "n", false)
end,
desc = "Clear (CopilotChat)",
mode = { "n", "v" },
},
{
"<leader>aq",
function()
local input = vim.fn.input("Quick Chat: ")
if input ~= "" then
local select = require("CopilotChat.select")
local mode = vim.api.nvim_get_mode().mode
require("CopilotChat").ask(
input,
(mode == "v" or mode == "V") and { selection = select.visual } or nil
)
end
end,
desc = "Quick Chat (CopilotChat)",
mode = { "n", "v" },
},
{
"<leader>ap",
function()
local chat = require("CopilotChat")
local select = require("CopilotChat.select")
local mode = vim.api.nvim_get_mode().mode
chat.select_prompt((mode == "v" or mode == "V") and { selection = select.visual } or nil)
end,
desc = "Prompt Actions (CopilotChat)",
mode = { "n", "v" },
},
{
"<leader>am",
function()
return require("CopilotChat").select_model()
end,
desc = "Select Models (CopilotChat)",
mode = { "n", "v" },
},
},
config = function(_, opts)
local chat = require("CopilotChat")
chat.setup(opts)
-- 原生补全被确认后,CopilotChat 的 TextChangedI 会再次触发同一个精确匹配项。
-- 取消这次防抖任务,让 <Tab>/<CR> 的“确认”表现为插入候选并关闭菜单。
local function stop_completion_retrigger()
vim.schedule(function()
local ok, utils = pcall(require, "CopilotChat.utils")
if not ok then
return
end
local timer = utils.timers and utils.timers.copilot_chat_complete
if timer then
pcall(timer.stop, timer)
pcall(timer.close, timer)
utils.timers.copilot_chat_complete = nil
end
end)
end
vim.api.nvim_create_autocmd("BufEnter", {
pattern = "copilot-chat",
callback = function(event)
vim.opt_local.relativenumber = false
vim.opt_local.number = false
vim.opt_local.completeopt:remove("noselect")
vim.opt_local.completeopt:append({ "menuone", "noinsert" })
-- MiniPairs 的全局 <CR> 映射不会确认原生补全,因此在聊天缓冲区单独覆盖。
vim.keymap.set("i", "<CR>", function()
if vim.fn.pumvisible() == 1 then
vim.api.nvim_feedkeys(vim.keycode("<C-y>"), "n", false)
return
end
vim.api.nvim_feedkeys(vim.keycode("<CR>"), "n", false)
end, { buffer = event.buf, desc = "CopilotChat confirm completion" })
if not vim.b[event.buf].copilot_chat_completion_done then
vim.b[event.buf].copilot_chat_completion_done = true
vim.api.nvim_create_autocmd("CompleteDone", {
buffer = event.buf,
callback = stop_completion_retrigger,
})
end
end,
})
end,
},
}