Skip to content

Commit 10d146f

Browse files
committed
feat: add PrtReloadCache command to reload cached models
- Add ReloadCache hook in config.lua to clear and refetch model caches - Support reloading all providers or a specific provider with validation - Include autocompletion for provider names in the command - Update README.md to document the new command - Reuses existing caching and fetching logic with spinner and logging
1 parent 820cc7d commit 10d146f

3 files changed

Lines changed: 42 additions & 2 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ Additional useful commands are implemented through hooks (see below).
208208
| `PrtProvider <provider>` | Switch the provider (empty arg triggers fzf) |
209209
| `PrtModel <model>` | Switch the model (empty arg triggers fzf) |
210210
| `PrtStatus` | Prints current provider and model selection |
211+
| `PrtReloadCache <optional provider>` | Reload cached models for all or specific provider |
211212
| `PrtCmd <optional prompt>` | Directly generate executable Neovim commands (requires explicit Return to execute) |
212213
| __Interactive__ | |
213214
| `PrtRewrite <optional prompt>` | Rewrites the visual selection based on a provided prompt (direct input, input dialog or from collection) |

lua/parrot/config.lua

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,39 @@ local defaults = {
210210
parrot.logger.info("Asking model: " .. model_obj.name)
211211
parrot.Prompt(params, parrot.ui.Target.popup, model_obj, "🤖 Ask ~ ", template)
212212
end,
213+
-- PrtReloadCache reloads cached models for all or specific providers
214+
ReloadCache = function(parrot, params)
215+
local provider = params.args -- Optional provider name from command args
216+
local state = parrot.chat_handler.state
217+
local spinner = parrot.options.enable_spinner and require("parrot.spinner"):new(parrot.options.spinner_type) or nil
218+
219+
-- Validate provider if specified
220+
if provider and not vim.tbl_contains(parrot.available_providers, provider) then
221+
parrot.logger.error("Provider '" .. provider .. "' is not configured or available")
222+
return
223+
end
224+
225+
-- Clear cache
226+
state:clear_cache(provider)
227+
228+
-- Determine providers to reload (only available ones)
229+
local providers_to_reload = provider and {provider} or parrot.available_providers
230+
231+
-- Refetch models (similar to setup logic, only for available providers)
232+
for _, prov_name in ipairs(providers_to_reload) do
233+
local _prov = require("parrot.provider").init_provider(vim.tbl_deep_extend("force", {name = prov_name}, parrot.providers[prov_name]))
234+
if _prov:online_model_fetching() and parrot.options.model_cache_expiry_hours > 0 then
235+
local endpoint_hash = require("parrot.utils").generate_endpoint_hash(_prov)
236+
parrot.logger.info("Reloading model cache for " .. prov_name)
237+
local fresh_models = _prov:get_available_models_cached(state, parrot.options.model_cache_expiry_hours, spinner)
238+
parrot.available_models[prov_name] = fresh_models
239+
end
240+
end
241+
242+
-- Refresh state with updated models
243+
state:refresh(parrot.available_providers, parrot.available_models)
244+
parrot.logger.info("Model cache reloaded" .. (provider and " for " .. provider or " for all providers"))
245+
end,
213246
},
214247
prompts = {
215248
["ProofReader"] = "You are a professional proofreader looking for spell and grammar errors",
@@ -382,9 +415,15 @@ end
382415
M.register_hooks = function(hooks, options)
383416
-- register user commands
384417
for hook, _ in pairs(hooks) do
418+
local complete_func = nil
419+
if hook == "ReloadCache" then
420+
complete_func = function()
421+
return M.available_providers
422+
end
423+
end
385424
vim.api.nvim_create_user_command(options.cmd_prefix .. hook, function(params)
386425
M.call_hook(hook, params)
387-
end, { nargs = "?", range = true, desc = "Parrot LLM plugin" })
426+
end, { nargs = "?", range = true, desc = "Parrot LLM plugin", complete = complete_func })
388427
end
389428
end
390429

lua/parrot/logger.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ local M = {
33
_logfile = vim.fn.stdpath("state") .. "/parrot.nvim.log",
44
_max_log_lines = 10000,
55
_debug_enabled = vim.env.DEBUG_PARROT ~= nil,
6-
notify = vim.notify
6+
notify = vim.notify,
77
}
88

99
-- Use pcall to safely require the notify plugin

0 commit comments

Comments
 (0)