diff --git a/lua/CopilotChat/copilot.lua b/lua/CopilotChat/copilot.lua index f20103fe..18b8072e 100644 --- a/lua/CopilotChat/copilot.lua +++ b/lua/CopilotChat/copilot.lua @@ -32,6 +32,7 @@ ---@field save fun(self: CopilotChat.Copilot, name: string, path: string):nil ---@field load fun(self: CopilotChat.Copilot, name: string, path: string):table ---@field running fun(self: CopilotChat.Copilot):boolean +---@field select_model fun(self: CopilotChat.Copilot, callback: fun(string):nil):nil local log = require('plenary.log') local curl = require('plenary.curl') @@ -499,6 +500,40 @@ function Copilot:ask(prompt, opts) end, on_error) end +--- Fetch & allow model selection +---@param callback fun(string):nil +function Copilot:select_model(callback) + local url = 'https://api.githubcopilot.com/models' + local headers = generate_headers(self.token.token, self.sessionid, self.machineid) + curl.get(url, { + headers = headers, + proxy = self.proxy, + insecure = self.allow_insecure, + on_error = function(err) + err = 'Failed to get response: ' .. vim.inspect(err) + log.error(err) + end, + callback = function(response) + if response.status ~= 200 then + local msg = 'Failed to fetch models: ' .. tostring(response.status) + log.error(msg) + return + end + + local models = vim.json.decode(response.body)['data'] + local selections = {} + for _, model in ipairs(models) do + table.insert(selections, model['version']) + end + vim.ui.select(selections, { + prompt = 'Select a model', + }, function(choice) + callback(choice) + end) + end, + }) +end + --- Generate embeddings for the given inputs ---@param inputs table: The inputs to embed ---@param opts CopilotChat.copilot.embed.opts: Options for the request diff --git a/lua/CopilotChat/init.lua b/lua/CopilotChat/init.lua index b1d95fa6..8f24f7c1 100644 --- a/lua/CopilotChat/init.lua +++ b/lua/CopilotChat/init.lua @@ -354,11 +354,17 @@ function M.toggle(config, source) end end --- @returns string +--- @returns string function M.response() return state.response end +function M.select_model() + state.copilot:select_model(function(model) + state.config.model = model + end) +end + --- Ask a question to the Copilot model. ---@param prompt string ---@param config CopilotChat.config|CopilotChat.config.prompt|nil @@ -859,6 +865,10 @@ function M.setup(config) range = true, }) + vim.api.nvim_create_user_command('CopilotChatModels', function() + M.select_model() + end, { force = true }) + vim.api.nvim_create_user_command('CopilotChatOpen', function() M.open() end, { force = true })