Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 26 additions & 23 deletions lua/CopilotChat/client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -164,14 +164,12 @@ end
---@class CopilotChat.client.Client : Class
---@field private provider_resolver function():table<string, CopilotChat.config.providers.Provider>
---@field private provider_cache table<string, table>
---@field private model_cache table<string, CopilotChat.client.Model>?
---@field private current_job string?
local Client = class(function(self)
self.provider_resolver = nil
self.provider_cache = vim.defaulttable(function()
return {}
end)
self.model_cache = nil
self.current_job = nil
end)

Expand Down Expand Up @@ -211,44 +209,49 @@ end
--- Fetch models from the Copilot API
---@return table<string, CopilotChat.client.Model>
function Client:models()
if self.model_cache then
return self.model_cache
end

local models = {}
local providers = self:get_providers()
local provider_order = vim.tbl_keys(providers)
table.sort(provider_order)
for _, provider_name in ipairs(provider_order) do
local provider = providers[provider_name]
if not provider.disabled and provider.get_models then
notify.publish(notify.STATUS, 'Fetching models from ' .. provider_name)
local ok, headers = pcall(self.authenticate, self, provider_name)
if not ok then
log.warn('Failed to authenticate with ' .. provider_name .. ': ' .. headers)
goto continue
end
local ok, provider_models = pcall(provider.get_models, headers)
if not ok then
log.warn('Failed to fetch models from ' .. provider_name .. ': ' .. provider_models)
goto continue
local cache = self.provider_cache[provider_name]
local resolved_models = nil
if cache and cache.models then
resolved_models = cache.models
else
notify.publish(notify.STATUS, 'Fetching models from ' .. provider_name)
local ok, headers = pcall(self.authenticate, self, provider_name)
if not ok then
log.warn('Failed to authenticate with ' .. provider_name .. ': ' .. headers)
goto continue
end
local ok, provider_models = pcall(provider.get_models, headers)
if not ok then
log.warn('Failed to fetch models from ' .. provider_name .. ': ' .. provider_models)
goto continue
end
resolved_models = provider_models
cache.models = resolved_models
end

for _, model in ipairs(provider_models) do
model.provider = provider_name
if models[model.id] then
model.id = model.id .. ':' .. provider_name
if resolved_models then
for _, model in ipairs(resolved_models) do
model.provider = provider_name
if models[model.id] then
model.id = model.id .. ':' .. provider_name
end
models[model.id] = model
end
models[model.id] = model
end

::continue::
end
end

log.debug('Fetched models:', #vim.tbl_keys(models))
self.model_cache = models
return self.model_cache
return models
end

--- Get information about all providers
Expand Down
2 changes: 1 addition & 1 deletion lua/CopilotChat/config/mappings.lua
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ return {
end

table.insert(lines, header)
table.insert(lines, '```' .. resource.type)
table.insert(lines, '```' .. utils.mimetype_to_filetype(resource.mimetype))
for _, line in ipairs(preview) do
table.insert(lines, line)
end
Expand Down
3 changes: 3 additions & 0 deletions lua/CopilotChat/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,9 @@ function M.filetype_to_mimetype(filetype)
if filetype == 'html' or filetype == 'css' then
return 'text/' .. filetype
end
if filetype:find('/') then
return filetype
end
return 'text/x-' .. filetype
end

Expand Down
Loading