1package agent
2
3import (
4 "context"
5 "fmt"
6 "os"
7 "path/filepath"
8 "strings"
9 "time"
10
11 "github.com/patriceckhart/zot/packages/provider"
12)
13
14// ModelCachePath returns the on-disk location of the merged model cache.
15func ModelCachePath() string {
16 return filepath.Join(ZotHome(), "models-cache.json")
17}
18
19// UserModelsPath returns the path to the user's models.json override.
20func UserModelsPath() string {
21 return filepath.Join(ZotHome(), "models.json")
22}
23
24// LoadCachedModels loads the cache file and applies it to the provider
25// package so FindModel / ModelsForProvider see live ids immediately.
26// Safe to call before any credentials are known.
27func LoadCachedModels() {
28 c, err := provider.LoadCache(ModelCachePath())
29 if err != nil {
30 return
31 }
32 if len(c.Models) > 0 {
33 provider.SetLiveModels(c.Models)
34 }
35}
36
37// LoadUserModels reads $ZOT_HOME/models.json and merges any user-defined
38// models into the active catalog. User models take highest precedence.
39// Any validation issues (bad provider id, empty model id, malformed
40// JSON, negative widths) are surfaced as one warning per line on stderr;
41// the well-formed entries from the rest of the file are still loaded.
42func LoadUserModels() {
43 models, warnings := provider.LoadUserModelsWithWarnings(UserModelsPath())
44 for _, w := range warnings {
45 fmt.Fprintln(os.Stderr, "zot:", w)
46 }
47 provider.SetUserModels(models)
48}
49
50// isGatewayProvider returns true for providers whose OpenAI-compatible
51// endpoint can accept routed model IDs that are not present in zot's local
52// catalog. Vercel AI Gateway is intentionally not listed here: zot currently
53// talks to it through the Anthropic-compatible client, which still requires
54// catalog metadata for request shaping.
55func isGatewayProvider(prov string) bool {
56 switch prov {
57 case "openrouter", "cloudflare-ai-gateway":
58 return true
59 default:
60 return false
61 }
62}
63
64// isGatewayRoutedModelID reports whether a model looks like the routed IDs
65// used by gateway providers, for example "deepseek/deepseek-v4-flash".
66// Non-routed typos like "deepseek-v4-flashh" should still be repaired to a
67// known default instead of being silently accepted.
68func isGatewayRoutedModelID(model string) bool {
69 return strings.Contains(strings.TrimSpace(model), "/")
70}
71
72// ValidateAndRepairConfig checks the persisted config.json's
73// (Provider, Model) pair against the active catalog and repairs any
74// mismatch in-place (and on disk) before any UI renders. Three failure
75// modes are handled:
76//
77// - cfg.Provider is empty or unknown -> reset to "anthropic".
78// - cfg.Model is empty -> set to the provider's default.
79// - cfg.Model belongs to a different provider than cfg.Provider
80// (e.g. provider=anthropic + model=kimi-for-coding from a stale
81// half-applied switch) -> reset model to the provider's default.
82//
83// Gateway providers are exempt from the cross-provider model check for routed
84// model IDs because those IDs can be valid even when absent from zot's catalog.
85//
86// Silent on success; one stderr line per repair. Errors loading or
87// saving the file are non-fatal — the caller continues with defaults.
88func ValidateAndRepairConfig() {
89 cfg, err := LoadConfig()
90 if err != nil {
91 fmt.Fprintf(os.Stderr, "zot: config.json: %v (using defaults)\n", err)
92 return
93 }
94 changed := false
95
96 if cfg.Provider != "" && !isKnownProvider(cfg.Provider) {
97 fmt.Fprintf(os.Stderr, "zot: config.json: unknown provider %q reset to \"anthropic\"\n", cfg.Provider)
98 cfg.Provider = "anthropic"
99 cfg.Model = ""
100 changed = true
101 }
102
103 if cfg.Provider != "" && cfg.Model != "" {
104 if _, err := provider.FindModel(cfg.Provider, cfg.Model); err != nil {
105 // Gateway providers can serve routed model ids like
106 // "deepseek/deepseek-v4-flash" even when the local catalog does not
107 // know them. Preserve only routed ids; plain typos are still repaired.
108 if cfg.Provider == provider.LMStudioProviderID || isDiscoverableCustomProvider(cfg.Provider) || (isGatewayProvider(cfg.Provider) && isGatewayRoutedModelID(cfg.Model)) {
109 // Local discovery is transient. Preserve the selected LM Studio
110 // or discovery-enabled custom provider ID even if another
111 // provider has an identically named model. Routed gateway IDs
112 // are also valid without a catalog entry.
113 } else if m, err := provider.FindModel("", cfg.Model); err == nil {
114 fix := defaultModelForProvider(cfg.Provider)
115 fmt.Fprintf(os.Stderr,
116 "zot: config.json: model %q belongs to provider %q (config has provider=%q); switched model to %q\n",
117 cfg.Model, m.Provider, cfg.Provider, fix)
118 cfg.Model = fix
119 changed = true
120 } else if cfg.Provider != "ollama" && cfg.Provider != provider.LlamaCPPProviderID && cfg.Provider != provider.LMStudioProviderID {
121 // Model id not in any catalog. Reset to provider's default.
122 fix := defaultModelForProvider(cfg.Provider)
123 fmt.Fprintf(os.Stderr,
124 "zot: config.json: model %q not found in the active catalog; switched to %q\n",
125 cfg.Model, fix)
126 cfg.Model = fix
127 changed = true
128 }
129 }
130 }
131
132 if changed {
133 if err := SaveConfig(cfg); err != nil {
134 fmt.Fprintf(os.Stderr, "zot: config.json: failed to persist repair: %v\n", err)
135 }
136 }
137}
138
139// RefreshModelsAsync kicks a background discovery for every provider
140// we have credentials for. Refreshed results are merged into the
141// active catalog and persisted to the on-disk cache.
142//
143// Silent on error: discovery is a nice-to-have. Callers can still use
144// the baked-in catalog if this fails.
145func RefreshModelsAsync() {
146 go refreshModels()
147 go refreshCopilotModelAvailability()
148 go func() {
149 ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
150 defer cancel()
151 _ = refreshLMStudioModels(ctx, apiKeyCommandSkip)
152 }()
153 go func() {
154 ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
155 defer cancel()
156 _ = refreshLlamaCPPModels(ctx, apiKeyCommandSkip)
157 }()
158 go func() {
159 ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
160 defer cancel()
161 _ = refreshCustomProviderModels(ctx, apiKeyCommandSkip)
162 }()
163}
164
165// Copilot availability is account-specific and must not use the shared disk cache.
166func refreshCopilotModelAvailability() {
167 ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
168 defer cancel()
169 cred, _, err := resolveCredentialForBackground(ctx, "github-copilot")
170 if err != nil {
171 provider.SetModelAvailability("github-copilot", nil)
172 return
173 }
174 ids, err := provider.DiscoverCopilotAvailableModels(ctx, cred)
175 if err != nil {
176 return // Keep the previous snapshot, or the catalog on first-load failure.
177 }
178 provider.SetModelAvailability("github-copilot", ids)
179}
180
181// RefreshLlamaCPPModels adds the router's currently loaded models to the
182// active catalog. Unloaded models remain in the management UI and cannot be
183// selected for inference until they are loaded.
184func RefreshLlamaCPPModels(ctx context.Context) error {
185 return refreshLlamaCPPModels(ctx, apiKeyCommandExecute)
186}
187
188func refreshLlamaCPPModels(ctx context.Context, commandMode apiKeyCommandMode) error {
189 baseURL, apiKey, err := resolveLlamaCPPConfig(ctx, commandMode)
190 if err != nil || baseURL == "" {
191 return err
192 }
193 client, err := provider.NewLlamaCPPClient(baseURL, apiKey)
194 if err != nil {
195 return err
196 }
197 models, err := client.List(ctx, false)
198 if err != nil {
199 return err
200 }
201 provider.SetManagedModelsForProvider(provider.LlamaCPPProviderID, provider.LlamaCPPModels(models, client.ServerURL))
202 return nil
203}
204
205func refreshModels() {
206 cached, _ := provider.LoadCache(ModelCachePath())
207 cacheFresh := cached.IsFresh()
208
209 // Refresh per-key limits even while the public catalog cache is fresh.
210 // Use a separate deadline so slow public discovery cannot starve this call.
211 yoloCtx, yoloCancel := context.WithTimeout(context.Background(), 15*time.Second)
212 var yoloModels []provider.Model
213 if cred, _, err := resolveCredentialForBackground(yoloCtx, "yolo-auto"); err == nil {
214 yoloModels, _ = provider.DiscoverYoloAuto(yoloCtx, cred, "")
215 }
216 yoloCancel()
217
218 ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
219 defer cancel()
220 var all []provider.Model
221 var openrouterCred string
222 var haveOpenRouter bool
223 if cred, _, err := resolveCredentialForBackground(ctx, "openrouter"); err == nil {
224 openrouterCred = cred
225 haveOpenRouter = true
226 }
227
228 if cacheFresh {
229 all = append(all, cached.Models...)
230 } else {
231 if cred, method, err := resolveCredentialForBackground(ctx, "anthropic"); err == nil && method == "apikey" {
232 // /v1/models on Anthropic is API-key only; OAuth tokens can
233 // also list models via the bearer header, but we skip OAuth
234 // here to avoid surprise rate-limit hits on subscription keys.
235 if live, err := provider.DiscoverAnthropic(ctx, cred, ""); err == nil {
236 all = append(all, live...)
237 }
238 }
239 if cred, method, err := resolveCredentialForBackground(ctx, "openai"); err == nil && method == "apikey" {
240 if live, err := provider.DiscoverOpenAI(ctx, cred, ""); err == nil {
241 all = append(all, live...)
242 }
243 }
244 if cred, method, err := resolveCredentialForBackground(ctx, "kimi"); err == nil && method == "apikey" {
245 if live, err := provider.DiscoverOpenAI(ctx, cred, "https://api.kimi.com/coding/v1"); err == nil {
246 for i := range live {
247 live[i].Provider = "kimi"
248 live[i].Source = "live"
249 }
250 all = append(all, live...)
251 }
252 }
253 if cred, method, err := resolveCredentialForBackground(ctx, "google"); err == nil && method == "apikey" {
254 if live, err := provider.DiscoverGoogle(ctx, cred, ""); err == nil {
255 all = append(all, live...)
256 }
257 }
258 // Bedrock resolves SigV4 credentials independently of inference auth.
259 // A default AWS CLI profile can be usable even when zot has no
260 // Bedrock credential registered with its general resolver.
261 if live, err := provider.DiscoverBedrock(ctx, ""); err == nil {
262 all = append(all, live...)
263 }
264 if haveOpenRouter {
265 // /models is public; gate on a credential so the picker only
266 // fills with OpenRouter's hundreds of routes for users who use it.
267 if live, err := provider.DiscoverOpenRouter(ctx, ""); err == nil {
268 all = append(all, live...)
269 }
270 }
271 if _, _, err := resolveCredentialForBackground(ctx, "gondola"); err == nil {
272 // Gondola's catalog is also public. Gate discovery on a credential so
273 // its text models only fill the picker for users of the provider.
274 if live, err := provider.DiscoverGondola(ctx, ""); err == nil {
275 all = append(all, live...)
276 }
277 }
278 }
279
280 // Presets are per-account and require auth. Fetch them even when the
281 // public /models cache is still fresh so @preset/{slug} shows up in
282 // the picker without waiting out CacheTTL.
283 if haveOpenRouter {
284 if presets, err := provider.DiscoverOpenRouterPresets(ctx, openrouterCred, ""); err == nil {
285 all = mergeOpenRouterPresets(all, presets)
286 }
287 }
288
289 // Do not carry forward another key's models or limits, even on failure.
290 all = mergeYoloAutoModels(all, yoloModels)
291 provider.SetLiveModels(all)
292 // Keep explicit metadata overrides above refreshed server catalogs.
293 LoadUserModels()
294 if len(all) == 0 {
295 // Clear old account entries without marking an empty catalog fresh.
296 _ = provider.SaveCache(ModelCachePath(), provider.ModelCache{})
297 return
298 }
299 _ = provider.SaveCache(ModelCachePath(), provider.ModelCache{
300 FetchedAt: modelCacheFetchedAt(cached, !cacheFresh, time.Now().UTC()),
301 Models: all,
302 })
303}
304
305// mergeYoloAutoModels replaces account metadata without disturbing the public
306// catalog. Cached entries aid offline resolution, but never suppress discovery.
307func mergeYoloAutoModels(existing, live []provider.Model) []provider.Model {
308 out := make([]provider.Model, 0, len(existing)+len(live))
309 for _, model := range existing {
310 if model.Provider != "yolo-auto" {
311 out = append(out, model)
312 }
313 }
314 return append(out, live...)
315}
316
317func modelCacheFetchedAt(cached provider.ModelCache, catalogRefreshed bool, now time.Time) time.Time {
318 if catalogRefreshed {
319 return now
320 }
321 return cached.FetchedAt
322}
323
324// mergeOpenRouterPresets replaces any previously cached OpenRouter preset
325// models with the freshly listed set, keeping every non-preset entry.
326func mergeOpenRouterPresets(existing, presets []provider.Model) []provider.Model {
327 out := make([]provider.Model, 0, len(existing)+len(presets))
328 for _, m := range existing {
329 if m.Provider == "openrouter" && strings.HasPrefix(m.ID, "@preset/") {
330 continue
331 }
332 out = append(out, m)
333 }
334 return append(out, presets...)
335}