-
-
Notifications
You must be signed in to change notification settings - Fork 170
Expand file tree
/
Copy pathproviders.lua
More file actions
801 lines (701 loc) · 24 KB
/
providers.lua
File metadata and controls
801 lines (701 loc) · 24 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
local log = require('plenary.log')
local plenary_utils = require('plenary.async.util')
local constants = require('CopilotChat.constants')
local notify = require('CopilotChat.utils.notify')
local utils = require('CopilotChat.utils')
local curl = require('CopilotChat.utils.curl')
local files = require('CopilotChat.utils.files')
local EDITOR_VERSION = 'Neovim/' .. vim.version().major .. '.' .. vim.version().minor .. '.' .. vim.version().patch
local token_cache = nil
local unsaved_token_cache = {}
local function load_tokens()
if token_cache then
return token_cache
end
local config_path = vim.fs.normalize(vim.fn.stdpath('data') .. '/copilot_chat')
local cache_file = config_path .. '/tokens.json'
local file = files.read_file(cache_file)
if file then
token_cache = vim.json.decode(file)
else
token_cache = {}
end
return token_cache
end
local function get_token(tag)
if unsaved_token_cache[tag] then
return unsaved_token_cache[tag]
end
local tokens = load_tokens()
return tokens[tag]
end
local function set_token(tag, token, save)
if not save then
unsaved_token_cache[tag] = token
return token
end
utils.schedule_main()
local tokens = load_tokens()
tokens[tag] = token
local config_path = vim.fs.normalize(vim.fn.stdpath('data') .. '/copilot_chat')
local file_path = config_path .. '/tokens.json'
vim.fn.mkdir(vim.fn.fnamemodify(file_path, ':p:h'), 'p')
files.write_file(file_path, vim.json.encode(tokens))
log.info('Token for ' .. tag .. ' saved to ' .. file_path)
return token
end
--- Get the github token using device flow
---@return string
local function github_device_flow(tag, client_id, scope)
local function request_device_code()
local res = curl.post('https://github.com/login/device/code', {
body = {
client_id = client_id,
scope = scope,
},
headers = { ['Accept'] = 'application/json' },
})
local data = vim.json.decode(res.body)
return data
end
local function poll_for_token(device_code, interval)
plenary_utils.sleep(interval * 1000)
local res = curl.post('https://github.com/login/oauth/access_token', {
json_response = true,
body = {
client_id = client_id,
device_code = device_code,
grant_type = 'urn:ietf:params:oauth:grant-type:device_code',
},
headers = { ['Accept'] = 'application/json' },
})
local data = res.body
if data.access_token then
return data.access_token
elseif data.error ~= 'authorization_pending' then
error('Auth error: ' .. (data.error or 'unknown'))
else
return poll_for_token(device_code, interval)
end
end
local token = get_token(tag)
if token then
return token
end
local code_data = request_device_code()
notify.publish(
notify.MESSAGE,
'[' .. tag .. '] Visit ' .. code_data.verification_uri .. ' and enter code: ' .. code_data.user_code
)
notify.publish(notify.STATUS, '[' .. tag .. '] Waiting for authorization...')
token = poll_for_token(code_data.device_code, code_data.interval)
notify.publish(notify.MESSAGE, '')
notify.publish(notify.STATUS, '')
return set_token(tag, token, true)
end
--- Get the github copilot oauth cached token (gu_ token)
---@return string
local function get_github_copilot_token(tag)
local function config_path()
local config = vim.fs.normalize('$XDG_CONFIG_HOME')
if config and vim.uv.fs_stat(config) then
return config
end
if vim.fn.has('win32') > 0 then
config = vim.fs.normalize('$LOCALAPPDATA')
if not config or not vim.uv.fs_stat(config) then
config = vim.fs.normalize('$HOME/AppData/Local')
end
else
config = vim.fs.normalize('$HOME/.config')
end
if config and vim.uv.fs_stat(config) then
return config
end
end
local token = get_token(tag)
if token then
return token
end
-- loading token from the environment only in GitHub Codespaces
local codespaces = os.getenv('CODESPACES')
token = os.getenv('GITHUB_TOKEN')
if token and codespaces then
return set_token(tag, token, false)
end
-- loading token from the file
local config_path = config_path()
if config_path then
-- token can be sometimes in apps.json sometimes in hosts.json
local file_paths = {
config_path .. '/github-copilot/hosts.json',
config_path .. '/github-copilot/apps.json',
}
for _, file_path in ipairs(file_paths) do
local file_data = files.read_file(file_path)
if file_data then
local parsed_data = utils.json_decode(file_data)
if parsed_data then
for key, value in pairs(parsed_data) do
if string.find(key, 'github.com') and value and value.oauth_token then
return set_token(tag, value.oauth_token, false)
end
end
end
end
end
end
return github_device_flow(tag, 'Iv1.b507a08c87ecfe98', '')
end
local function get_github_models_token(tag)
local token = get_token(tag)
if token then
return token
end
-- loading token from the environment only in GitHub Codespaces
local codespaces = os.getenv('CODESPACES')
token = os.getenv('GITHUB_TOKEN')
if token and codespaces then
return set_token(tag, token, false)
end
-- loading token from gh cli if available
if vim.fn.executable('gh') == 1 then
local result = utils.system({ 'gh', 'auth', 'token', '-h', 'github.com' })
if result and result.code == 0 and result.stdout then
local gh_token = vim.trim(result.stdout)
if gh_token ~= '' and not gh_token:find('no oauth token') then
return set_token(tag, gh_token, false)
end
end
end
return github_device_flow(tag, '178c6fc778ccc68e1d6a', 'read:user copilot')
end
--- Resolve the Copilot API base URL from token endpoint response.
--- Falls back to the default api.githubcopilot.com if no business endpoint is found.
---@param token_body table The decoded JSON body from the token endpoint
---@return string base_url The base URL (no trailing slash)
local function resolve_copilot_base_url(token_body)
-- The token response may include an `endpoints` table with an `api` field
-- pointing to the correct base URL for business/enterprise accounts,
-- e.g. https://api.business.githubcopilot.com
if token_body and token_body.endpoints and token_body.endpoints.api then
local url = token_body.endpoints.api
-- Strip trailing slash if present
return url:gsub('/$', '')
end
return 'https://api.githubcopilot.com'
end
--- Prepare input for Responses API
---@param inputs CopilotChat.client.Message[]
---@param opts CopilotChat.config.providers.Options
---@return table
local function prepare_responses_input(inputs, opts)
local instructions = nil
local input_messages = {}
for _, msg in ipairs(inputs) do
if msg.role == constants.ROLE.SYSTEM then
instructions = instructions and (instructions .. '\n\n' .. msg.content) or msg.content
elseif msg.role == constants.ROLE.TOOL then
table.insert(input_messages, {
type = 'function_call_output',
call_id = msg.tool_call_id,
output = msg.content,
})
else
table.insert(input_messages, {
role = msg.role,
content = msg.content,
})
if msg.tool_calls then
for _, tool_call in ipairs(msg.tool_calls) do
table.insert(input_messages, {
type = 'function_call',
call_id = tool_call.id,
name = tool_call.name,
arguments = tool_call.arguments or '',
})
end
end
end
end
local out = {
model = opts.model.id,
stream = opts.model.streaming ~= false,
input = input_messages,
}
if instructions then
out.instructions = instructions
end
if opts.tools and opts.model.tools then
out.tools = vim.tbl_map(function(tool)
return {
type = 'function',
name = tool.name,
description = tool.description,
parameters = tool.schema,
}
end, opts.tools)
end
return out
end
--- Prepare input for Chat Completions API
---@param inputs CopilotChat.client.Message[]
---@param opts CopilotChat.config.providers.Options
---@return table
local function prepare_chat_input(inputs, opts)
local is_o1 = vim.startswith(opts.model.id, 'o1')
local is_codex = opts.model.id:find('codex') ~= nil
inputs = vim.tbl_map(function(input)
local output = {
role = (is_o1 and input.role == constants.ROLE.SYSTEM) and constants.ROLE.USER or input.role,
content = input.content,
}
if input.tool_call_id then
output.tool_call_id = input.tool_call_id
end
if input.tool_calls then
output.tool_calls = vim.tbl_map(function(tool_call)
return {
id = tool_call.id,
type = 'function',
['function'] = {
name = tool_call.name,
arguments = tool_call.arguments or nil,
},
}
end, input.tool_calls)
end
return output
end, inputs)
local out = {
messages = inputs,
model = opts.model.id,
stream = opts.model.streaming or false,
}
if opts.tools and opts.model.tools then
out.tools = vim.tbl_map(function(tool)
return {
type = 'function',
['function'] = {
name = tool.name,
description = tool.description,
parameters = tool.schema,
},
}
end, opts.tools)
end
if not is_o1 and not is_codex then
out.n = 1
out.top_p = 1
out.temperature = opts.temperature
end
if opts.model.max_output_tokens then
out.max_tokens = opts.model.max_output_tokens
end
return out
end
---@param parts table Array of content parts
---@return string The concatenated text content
local function extract_text_from_parts(parts)
if not parts or type(parts) ~= 'table' then
return ''
end
local content = ''
for _, part in ipairs(parts) do
if type(part) == 'string' then
content = content .. part
elseif type(part) == 'table' then
-- Responses API: parts have type field
if part.type == 'text' or part.type == 'output_text' or part.type == 'input_text' then
content = content .. (part.text or '')
-- Fallback for simpler structures
elseif part.text then
content = content .. part.text
end
end
end
return content
end
--- Parse Responses API output (both streaming and non-streaming)
---@param output table Raw API response
---@return CopilotChat.config.providers.Output
local function prepare_responses_output(output)
local content = ''
local reasoning = ''
local finish_reason = nil
local total_tokens = nil
local tool_calls = {}
local model = nil
-- Handle errors
local error_msg = output.error or (output.response and output.response.error)
if error_msg then
if type(error_msg) == 'table' then
error_msg = error_msg.message or vim.inspect(error_msg)
end
return {
content = '',
reasoning = '',
finish_reason = 'error: ' .. tostring(error_msg),
total_tokens = nil,
tool_calls = {},
model = nil,
}
end
-- Handle streaming events
if output.type then
if output.type == 'response.output_text.delta' then
-- Streaming text delta
if output.delta and type(output.delta) == 'string' then
content = output.delta
elseif output.delta and output.delta.text then
content = output.delta.text
end
elseif output.type == 'response.output_item.done' then
local item = output.item
if item and item.type == 'function_call' then
table.insert(tool_calls, {
id = item.call_id,
index = output.output_index,
name = item.name,
arguments = item.arguments or '',
})
end
elseif output.type == 'response.completed' or output.type == 'response.done' then
local response = output.response
if response then
if response.reasoning and response.reasoning.summary then
reasoning = response.reasoning.summary
end
if response.usage then
total_tokens = response.usage.total_tokens
end
if response.model then
model = response.model
end
finish_reason = 'stop'
end
elseif output.type == 'response.failed' then
finish_reason = 'error: ' .. (output.error and output.error.message or 'unknown error')
end
-- Handle non-streaming response
elseif output.response then
local response = output.response
if response.output and #response.output > 0 then
for _, msg in ipairs(response.output) do
if msg.content then
content = content .. extract_text_from_parts(msg.content)
end
if msg.tool_calls then
for i, tool_call in ipairs(msg.tool_calls) do
table.insert(tool_calls, {
id = tool_call.call_id,
index = i,
name = tool_call.name,
arguments = tool_call.arguments or '',
})
end
end
end
end
if response.reasoning and response.reasoning.summary then
reasoning = response.reasoning.summary
end
if response.usage then
total_tokens = response.usage.total_tokens
end
if response.model then
model = response.model
end
finish_reason = response.status == 'completed' and 'stop' or nil
end
return {
content = content,
reasoning = reasoning,
finish_reason = finish_reason,
total_tokens = total_tokens,
tool_calls = tool_calls,
model = model,
}
end
--- Parse Chat Completions API output (both streaming and non-streaming)
---@param output table Raw API response
---@return CopilotChat.config.providers.Output
local function prepare_chat_output(output)
local tool_calls = {}
local choice
if output.choices and #output.choices > 0 then
for _, c in ipairs(output.choices) do
local message = c.message or c.delta
if message and message.tool_calls then
for i, tool_call in ipairs(message.tool_calls) do
local fn = tool_call['function']
if fn then
table.insert(tool_calls, {
id = tool_call.id,
index = tool_call.index or i,
name = fn.name,
arguments = fn.arguments or '',
})
end
end
end
end
choice = output.choices[1]
else
choice = output
end
local message = choice.message or choice.delta
local content = message and message.content
local reasoning = message and (message.reasoning or message.reasoning_content)
local usage = choice.usage and choice.usage.total_tokens or output.usage and output.usage.total_tokens
local finish_reason = choice.finish_reason or choice.done_reason or output.finish_reason or output.done_reason
local model = choice.model or output.model
return {
content = content,
reasoning = reasoning,
finish_reason = finish_reason,
total_tokens = usage,
tool_calls = tool_calls,
model = model,
}
end
---@class CopilotChat.config.providers.Options
---@field model CopilotChat.client.Model
---@field temperature number?
---@field tools table<CopilotChat.client.Tool>?
---@class CopilotChat.config.providers.Output
---@field content string
---@field reasoning string?
---@field finish_reason string?
---@field total_tokens number?
---@field tool_calls table<CopilotChat.client.ToolCall>
---@field model string?
---@class CopilotChat.config.providers.Provider
---@field disabled nil|boolean
---@field get_headers nil|fun():table<string, string>,number?
---@field get_info nil|fun(headers:table):string[]
---@field get_models nil|fun(headers:table):table<CopilotChat.client.Model>
---@field resolve_model nil|fun(headers:table, model: string):string
---@field prepare_input nil|fun(inputs:CopilotChat.client.Message[], opts:CopilotChat.config.providers.Options):table,table?
---@field prepare_output nil|fun(output:table, opts:CopilotChat.config.providers.Options):CopilotChat.config.providers.Output
---@field get_url nil|fun(opts:CopilotChat.config.providers.Options):string
---@type table<string, CopilotChat.config.providers.Provider>
local M = {}
M.copilot = {
get_headers = function()
local response, err = curl.get('https://api.github.com/copilot_internal/v2/token', {
json_response = true,
headers = {
['Authorization'] = 'Token ' .. get_github_copilot_token('github_copilot'),
},
})
if err then
error(err)
end
-- Resolve the base URL from the token response so that business/enterprise
-- accounts using *.business.githubcopilot.com are handled automatically.
local base_url = resolve_copilot_base_url(response.body)
return {
['Authorization'] = 'Bearer ' .. response.body.token,
['Editor-Version'] = EDITOR_VERSION,
['Editor-Plugin-Version'] = 'CopilotChat.nvim/*',
['Copilot-Integration-Id'] = 'vscode-chat',
['x-github-api-version'] = '2025-10-01',
-- Store the resolved base URL in a custom header so that get_models,
-- resolve_model, and get_url can read it without making another request.
['x-copilot-base-url'] = base_url,
},
response.body.expires_at
end,
get_info = function()
local response, err = curl.get('https://api.github.com/copilot_internal/user', {
json_response = true,
headers = {
['Authorization'] = 'Token ' .. get_github_copilot_token('github_copilot'),
},
})
if err then
error(err)
end
local stats = response.body
local lines = {}
if not stats or not stats.quota_snapshots then
return { 'No Copilot stats available.' }
end
local function usage_line(name, snap)
if not snap then
return
end
table.insert(lines, string.format(' **%s**', name))
if snap.unlimited then
table.insert(lines, ' Usage: Unlimited')
else
local used = snap.entitlement - snap.remaining
local percent = snap.entitlement > 0 and (used / snap.entitlement * 100) or 0
table.insert(lines, string.format(' Usage: %d / %d (%.1f%%)', used, snap.entitlement, percent))
table.insert(lines, string.format(' Remaining: %d', snap.remaining))
if snap.overage_permitted ~= nil then
table.insert(lines, ' Overage: ' .. (snap.overage_permitted and 'Permitted' or 'Not Permitted'))
end
end
end
usage_line('Premium requests', stats.quota_snapshots.premium_interactions)
usage_line('Chat', stats.quota_snapshots.chat)
usage_line('Completions', stats.quota_snapshots.completions)
if stats.quota_reset_date then
table.insert(lines, string.format(' **Quota** resets on: %s', stats.quota_reset_date))
end
return lines
end,
get_models = function(headers)
-- Use the resolved base URL carried in the custom header, falling back to
-- the default if it is absent (e.g. during tests or manual calls).
local base_url = headers['x-copilot-base-url'] or 'https://api.githubcopilot.com'
-- Build request headers without our internal routing header.
local request_headers = vim.tbl_extend('force', headers, { ['x-copilot-base-url'] = nil })
local response, err = curl.get(base_url .. '/models', {
json_response = true,
headers = request_headers,
})
if err then
error(err)
end
local models = vim
.iter(response.body.data)
:filter(function(model)
return model.capabilities.type == 'chat' and model.model_picker_enabled
end)
:map(function(model)
local supported_endpoints = model.supported_endpoints or {}
-- Pre-compute whether this model uses the Responses API
local use_responses = vim.tbl_contains(supported_endpoints, '/responses')
return {
id = model.id,
name = model.name,
tokenizer = model.capabilities.tokenizer,
max_input_tokens = model.capabilities.limits.max_prompt_tokens,
max_output_tokens = model.capabilities.limits.max_output_tokens,
streaming = model.capabilities.supports.streaming,
tools = model.capabilities.supports.tool_calls,
policy = not model['policy'] or model['policy']['state'] == 'enabled',
version = model.version,
use_responses = use_responses,
-- Carry the base URL into the model so get_url and resolve_model
-- can use it without needing access to the headers again.
base_url = base_url,
}
end)
:totable()
local name_map = {}
for _, model in ipairs(models) do
if not name_map[model.name] or model.version > name_map[model.name].version then
name_map[model.name] = model
end
end
models = vim.tbl_values(name_map)
for _, model in ipairs(models) do
if not model.policy then
pcall(curl.post, base_url .. '/models/' .. model.id .. '/policy', {
headers = request_headers,
json_request = true,
body = { state = 'enabled' },
})
end
end
-- Auto model selector
table.insert(models, {
id = 'auto',
name = 'Auto (Copilot)',
description = 'Auto selects the best model for your request.',
base_url = base_url,
})
return models
end,
resolve_model = function(headers, model)
if model ~= 'auto' then
return model
end
local base_url = headers['x-copilot-base-url'] or 'https://api.githubcopilot.com'
local request_headers = vim.tbl_extend('force', headers, { ['x-copilot-base-url'] = nil })
local url = base_url .. '/models/session'
local response, err = curl.post(url, {
headers = request_headers,
body = { auto_mode = { model_hints = { 'auto' } } },
json_response = true,
json_request = true,
})
if err then
error(err)
end
return response.body.selected_model
end,
prepare_input = function(inputs, opts)
local request
if opts.model.use_responses then
request = prepare_responses_input(inputs, opts)
else
request = prepare_chat_input(inputs, opts)
end
if inputs and #inputs > 0 then
local last_msg = inputs[#inputs]
if last_msg.role == constants.ROLE.TOOL then
return request, { ['x-initiator'] = 'agent' }
end
end
return request
end,
prepare_output = function(output, opts)
if opts and opts.model and opts.model.use_responses then
return prepare_responses_output(output)
end
return prepare_chat_output(output)
end,
get_url = function(opts)
-- Use the base URL stored on the model (populated by get_models), falling
-- back to the default for backwards compatibility.
local base_url = (opts and opts.model and opts.model.base_url) or 'https://api.githubcopilot.com'
if opts and opts.model and opts.model.use_responses then
return base_url .. '/responses'
end
return base_url .. '/chat/completions'
end,
}
M.github_models = {
disabled = true,
get_headers = function()
return {
['Authorization'] = 'Bearer ' .. get_github_models_token('github_models'),
}
end,
get_models = function(headers)
local response, err = curl.get('https://models.github.ai/catalog/models', {
json_response = true,
headers = headers,
})
if err then
error(err)
end
return vim
.iter(response.body)
:map(function(model)
return {
id = model.id,
name = model.name,
tokenizer = 'o200k_base', -- GitHub Models doesn't expose tokenizer info
max_input_tokens = model.limits and model.limits.max_input_tokens,
max_output_tokens = model.limits and model.limits.max_output_tokens,
streaming = model.capabilities and vim.tbl_contains(model.capabilities, 'streaming') or false,
tools = model.capabilities and vim.tbl_contains(model.capabilities, 'tool-calling') or false,
reasoning = model.capabilities and vim.tbl_contains(model.capabilities, 'reasoning') or false,
version = model.version,
}
end)
:totable()
end,
prepare_input = M.copilot.prepare_input,
prepare_output = M.copilot.prepare_output,
get_url = function()
return 'https://models.github.ai/inference/chat/completions'
end,
}
return M