Skip to content

Commit 20a75e6

Browse files
committed
refactor: move model selector to specific provider
1 parent 17e6f91 commit 20a75e6

2 files changed

Lines changed: 41 additions & 32 deletions

File tree

lua/CopilotChat/client.lua

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ local class = require('CopilotChat.utils.class')
6363
local files = require('CopilotChat.utils.files')
6464
local orderedmap = require('CopilotChat.utils.orderedmap')
6565
local stringbuffer = require('CopilotChat.utils.stringbuffer')
66+
local model = require('fidget.notification.model')
6667

6768
--- Constants
6869
local RESOURCE_SHORT_FORMAT = '# %s\n```%s start_line=% end_line=%s\n%s\n```'
@@ -299,28 +300,6 @@ function Client:ask(opts)
299300
opts = opts or {}
300301
local job_id = utils.uuid()
301302

302-
-- handle auto model selection
303-
if opts.model == 'auto' then
304-
notify.publish(notify.STATUS, 'Auto-selecting model...')
305-
local provider_name = 'copilot'
306-
local provider = self:get_providers():get(provider_name)
307-
308-
if provider and provider.route_model then
309-
local headers = self:authenticate(provider_name)
310-
local selected_model, err = provider.route_model(headers, { 'auto' })
311-
312-
if selected_model then
313-
opts.model = selected_model
314-
else
315-
log.warn('Auto mode failed, falling back to gpt-4o. Error: ' .. tostring(err))
316-
opts.model = 'gpt-4o'
317-
end
318-
else
319-
log.warn('Auto mode not supported, falling back to gpt-4o')
320-
opts.model = 'gpt-4o'
321-
end
322-
end
323-
324303
log.debug('Model:', opts.model)
325304
log.debug('Tools:', #opts.tools)
326305
log.debug('Resources:', #opts.resources)
@@ -341,6 +320,24 @@ function Client:ask(opts)
341320
error('Provider not found: ' .. provider_name)
342321
end
343322

323+
if provider.route_model then
324+
if not opts.headless then
325+
notify.publish(notify.STATUS, 'Routing model...')
326+
end
327+
328+
local headers = self:authenticate(provider_name)
329+
local resolved_model = provider.route_model(headers, opts)
330+
331+
if resolved_model and resolved_model ~= opts.model then
332+
opts.model = resolved_model
333+
334+
model_config = models[opts.model]
335+
if not model_config then
336+
error('Routed model not found: ' .. opts.model)
337+
end
338+
end
339+
end
340+
344341
local options = {
345342
model = vim.tbl_extend('force', model_config, {
346343
id = opts.model:gsub(':' .. provider_name .. '$', ''),

lua/CopilotChat/config/providers.lua

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -649,39 +649,51 @@ M.copilot = {
649649
return models
650650
end,
651651

652-
route_model = function(headers, hints)
653-
hints = hints or { 'auto' }
654-
local token = headers['Authorization'] and headers['Authorization']:gsub('^Bearer%s+', '')
652+
route_model = function(headers, opts)
653+
local model_id = opts.model
654+
if model_id ~= 'auto' then
655+
return model_id
656+
end
657+
658+
local token = headers['Authorization']
655659
if not token then
656-
return nil, 'No authorization token available'
660+
log.warn('No authorization token available for auto model resolution')
661+
return 'gpt-4.1' -- Fallback model
657662
end
658663

659664
local url = 'https://api.individual.githubcopilot.com/models/session'
660665
local response, err = curl.post(url, {
661666
headers = {
662-
['Authorization'] = 'Bearer ' .. token,
667+
['Authorization'] = token,
663668
['editor-version'] = 'vscode/1.109.0-insider',
664669
['user-agent'] = 'GitHubCopilotChat/0.38.0',
665670
['x-github-api-version'] = '2025-10-01',
666671
},
667-
body = { auto_mode = { model_hints = hints } },
672+
body = { auto_mode = { model_hints = { 'auto' } } },
668673
json_response = true,
669674
json_request = true,
670675
})
671676

672677
if err then
673-
return nil, 'Auto selection request failed: ' .. tostring(err)
678+
log.warn('Auto selection request failed: ' .. tostring(err) .. '. Falling back to gpt-4.1.')
679+
return 'gpt-4.1'
674680
end
675681

676682
if not response or response.status ~= 200 then
677-
return nil, 'Auto selection returned status: ' .. tostring(response and response.status or 'unknown')
683+
log.warn(
684+
'Auto selection returned status: '
685+
.. tostring(response and response.status or 'unknown')
686+
.. '. Falling back to gpt-4.1.'
687+
)
688+
return 'gpt-4.1'
678689
end
679690

680691
if not response.body or not response.body.selected_model then
681-
return nil, 'No model selected in response'
692+
log.warn('No model selected in response. Falling back to gpt-4.1.')
693+
return 'gpt-4.1'
682694
end
683695

684-
return response.body.selected_model, nil
696+
return response.body.selected_model
685697
end,
686698

687699
prepare_input = function(inputs, opts)

0 commit comments

Comments
 (0)