|
504 | 504 | ---@field get_headers nil|fun():table<string, string>,number? |
505 | 505 | ---@field get_info nil|fun(headers:table):string[] |
506 | 506 | ---@field get_models nil|fun(headers:table):table<CopilotChat.client.Model> |
| 507 | +---@field select_model nil|fun(headers:table, hints:table?):string?,string? |
507 | 508 | ---@field prepare_input nil|fun(inputs:table<CopilotChat.client.Message>, opts:CopilotChat.config.providers.Options):table |
508 | 509 | ---@field prepare_output nil|fun(output:table, opts:CopilotChat.config.providers.Options):CopilotChat.config.providers.Output |
509 | 510 | ---@field get_url nil|fun(opts:CopilotChat.config.providers.Options):string |
|
512 | 513 | local M = {} |
513 | 514 |
|
514 | 515 | 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, |
515 | 550 | get_headers = function() |
516 | 551 | local response, err = curl.get('https://api.github.com/copilot_internal/v2/token', { |
517 | 552 | json_response = true, |
|
0 commit comments