Skip to content

Commit 8280b51

Browse files
committed
refactor: remove function to provider configs
1 parent 924e94f commit 8280b51

2 files changed

Lines changed: 47 additions & 18 deletions

File tree

lua/CopilotChat/client.lua

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -310,26 +310,20 @@ function Client:ask(opts)
310310
if opts.model == 'auto' then
311311
notify.publish(notify.STATUS, 'Auto-selecting model...')
312312
local provider_name = 'copilot'
313-
local headers = self:authenticate(provider_name)
314-
local token = headers['Authorization'] and headers['Authorization']:gsub('^Bearer%s+', '')
315-
316-
local url = 'https://api.individual.githubcopilot.com/models/session'
317-
local response, err = curl.post(url, {
318-
headers = {
319-
['Authorization'] = 'Bearer ' .. token,
320-
['editor-version'] = 'vscode/1.109.0-insider',
321-
['user-agent'] = 'GitHubCopilotChat/0.38.0',
322-
['x-github-api-version'] = '2025-10-01',
323-
},
324-
body = { auto_mode = { model_hints = { 'auto' } } },
325-
json_response = true,
326-
json_request = true,
327-
})
313+
local provider = self:get_providers():get(provider_name)
314+
315+
if provider and provider.select_model then
316+
local headers = self:authenticate(provider_name)
317+
local selected_model, err = provider.select_model(headers, { 'auto' })
328318

329-
if not err and response and response.status == 200 and response.body.selected_model then
330-
opts.model = response.body.selected_model
319+
if selected_model then
320+
opts.model = selected_model
321+
else
322+
log.warn('Auto mode failed, falling back to gpt-4o. Error: ' .. tostring(err))
323+
opts.model = 'gpt-4o'
324+
end
331325
else
332-
log.warn('Auto mode failed, falling back to gpt-4o. Error: ' .. tostring(err or (response and response.status)))
326+
log.warn('Auto mode not supported, falling back to gpt-4o')
333327
opts.model = 'gpt-4o'
334328
end
335329
end

lua/CopilotChat/config/providers.lua

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,7 @@ end
504504
---@field get_headers nil|fun():table<string, string>,number?
505505
---@field get_info nil|fun(headers:table):string[]
506506
---@field get_models nil|fun(headers:table):table<CopilotChat.client.Model>
507+
---@field select_model nil|fun(headers:table, hints:table?):string?,string?
507508
---@field prepare_input nil|fun(inputs:table<CopilotChat.client.Message>, opts:CopilotChat.config.providers.Options):table
508509
---@field prepare_output nil|fun(output:table, opts:CopilotChat.config.providers.Options):CopilotChat.config.providers.Output
509510
---@field get_url nil|fun(opts:CopilotChat.config.providers.Options):string
@@ -512,6 +513,40 @@ end
512513
local M = {}
513514

514515
M.copilot = {
516+
select_model = function(headers, hints)
517+
hints = hints or { 'auto' }
518+
local token = headers['Authorization'] and headers['Authorization']:gsub('^Bearer%s+', '')
519+
if not token then
520+
return nil, 'No authorization token available'
521+
end
522+
523+
local url = 'https://api.individual.githubcopilot.com/models/session'
524+
local response, err = curl.post(url, {
525+
headers = {
526+
['Authorization'] = 'Bearer ' .. token,
527+
['editor-version'] = 'vscode/1.109.0-insider',
528+
['user-agent'] = 'GitHubCopilotChat/0.38.0',
529+
['x-github-api-version'] = '2025-10-01',
530+
},
531+
body = { auto_mode = { model_hints = hints } },
532+
json_response = true,
533+
json_request = true,
534+
})
535+
536+
if err then
537+
return nil, 'Auto selection request failed: ' .. tostring(err)
538+
end
539+
540+
if not response or response.status ~= 200 then
541+
return nil, 'Auto selection returned status: ' .. tostring(response and response.status or 'unknown')
542+
end
543+
544+
if not response.body or not response.body.selected_model then
545+
return nil, 'No model selected in response'
546+
end
547+
548+
return response.body.selected_model, nil
549+
end,
515550
get_headers = function()
516551
local response, err = curl.get('https://api.github.com/copilot_internal/v2/token', {
517552
json_response = true,

0 commit comments

Comments
 (0)