1package provider
2
3import (
4 "encoding/json"
5 "fmt"
6 "os"
7 "strings"
8)
9
10// UserModelsFile is the JSON format for user-defined models.
11// Place a models.json in $ZOT_HOME to add models that aren't in the
12// baked-in catalog or to override catalog entries. Custom providers
13// (not in the built-in set) may specify a baseUrl and api format at
14// the provider level:
15//
16// {
17// "providers": {
18// "my-company": {
19// "baseUrl": "https://llm.mycompany.com/v1",
20// "api": "openai",
21// "models": [
22// {
23// "id": "company-llm-v2",
24// "name": "Company LLM v2",
25// "contextWindow": 128000,
26// "maxTokens": 32000
27// }
28// ]
29// }
30// }
31// }
32type UserModelsFile struct {
33 Providers map[string]UserProvider `json:"providers"`
34}
35
36// UserProvider groups models under a provider key.
37type UserProvider struct {
38 BaseURL string `json:"baseUrl,omitempty"`
39 API string `json:"api,omitempty"` // "openai" (default), "openai-responses", or "anthropic"
40 Models []UserModel `json:"models"`
41}
42
43// CustomProviderConfig holds runtime config for a user-defined provider
44// that isn't part of the built-in catalog.
45type CustomProviderConfig struct {
46 BaseURL string
47 API string // "openai", "openai-responses", or "anthropic"
48}
49
50var customProviders = map[string]CustomProviderConfig{}
51
52// CustomProviders returns the set of user-defined providers loaded from
53// models.json. Keys are provider names; values carry the base URL and
54// wire-format hint.
55func CustomProviders() map[string]CustomProviderConfig { return customProviders }
56
57// UserModel is a single model entry in the user's models.json.
58type UserModel struct {
59 ID string `json:"id"`
60 Name string `json:"name"`
61 Reasoning bool `json:"reasoning"`
62 ContextWindow int `json:"contextWindow"`
63 MaxTokens int `json:"maxTokens"`
64 PriceInput float64 `json:"priceInput"`
65 PriceOutput float64 `json:"priceOutput"`
66 PriceCacheRead float64 `json:"priceCacheRead"`
67 PriceCacheWrite float64 `json:"priceCacheWrite"`
68 BaseURL string `json:"baseUrl,omitempty"`
69 Input []string `json:"input"` // informational only
70 API string `json:"api"` // informational only
71}
72
73// LoadUserModels reads a models.json file and returns the models
74// converted to the internal Model type. Returns nil on any error
75// (missing file, bad JSON, etc.) so the caller can treat it as
76// optional without error handling.
77func LoadUserModels(path string) []Model {
78 models, _ := LoadUserModelsWithWarnings(path)
79 return models
80}
81
82// LoadUserModelsWithWarnings is like LoadUserModels but also returns
83// human-readable warnings about every recoverable issue it found in
84// the file (unknown provider id, empty model id, malformed JSON for a
85// single provider block, etc.). The caller is responsible for
86// surfacing the warnings; the file is never rejected wholesale unless
87// the top-level JSON itself fails to parse.
88func LoadUserModelsWithWarnings(path string) ([]Model, []string) {
89 var warnings []string
90 data, err := os.ReadFile(path)
91 if err != nil {
92 return nil, nil
93 }
94 var file UserModelsFile
95 if err := json.Unmarshal(data, &file); err != nil {
96 warnings = append(warnings, fmt.Sprintf("models.json: parse error: %v (file ignored)", err))
97 return nil, warnings
98 }
99
100 var out []Model
101 // Reset custom providers on each load so removed entries don't linger.
102 customProviders = map[string]CustomProviderConfig{}
103 for providerName, prov := range file.Providers {
104 if providerName == "" {
105 warnings = append(warnings, "models.json: empty provider key skipped")
106 continue
107 }
108 // Normalize legacy transport aliases to their provider names.
109 normalized := providerName
110 switch providerName {
111 case "openai-responses":
112 normalized = "openai"
113 case "anthropic-messages":
114 normalized = "anthropic"
115 case "moonshot", "moonshot-ai", "kimi-code":
116 normalized = "kimi"
117 case "deepseek-chat", "deepseek-ai":
118 normalized = "deepseek"
119 }
120
121 // Register custom providers that carry endpoint metadata either at
122 // the provider level or on any model. Model-level baseUrl-only
123 // configs still need a custom provider entry so Resolve/NewClient
124 // accept the provider name and choose a wire format.
125 hasModelBaseURL := false
126 for _, um := range prov.Models {
127 if um.BaseURL != "" {
128 hasModelBaseURL = true
129 break
130 }
131 }
132 if prov.BaseURL != "" || prov.API != "" || hasModelBaseURL {
133 api := strings.ToLower(strings.TrimSpace(prov.API))
134 if api == "" {
135 api = "openai"
136 }
137 // Normalize common aliases for the wire format.
138 switch api {
139 case "openai-completions", "openai-chat", "chat", "openai":
140 api = "openai"
141 case "openai-responses", "responses":
142 api = APIResponses
143 case "anthropic-messages", "messages", "anthropic":
144 api = "anthropic"
145 default:
146 warnings = append(warnings, fmt.Sprintf("models.json: provider %q has unknown api %q; defaulting to openai", providerName, prov.API))
147 api = "openai"
148 }
149 customProviders[normalized] = CustomProviderConfig{
150 BaseURL: prov.BaseURL,
151 API: api,
152 }
153 }
154
155 for i, um := range prov.Models {
156 if um.ID == "" {
157 warnings = append(warnings, fmt.Sprintf("models.json: provider %q entry #%d has empty id; skipped", providerName, i))
158 continue
159 }
160 if um.ContextWindow < 0 || um.MaxTokens < 0 {
161 warnings = append(warnings, fmt.Sprintf("models.json: %s/%s has negative contextWindow/maxTokens; clamped to 0", normalized, um.ID))
162 if um.ContextWindow < 0 {
163 um.ContextWindow = 0
164 }
165 if um.MaxTokens < 0 {
166 um.MaxTokens = 0
167 }
168 }
169 // Propagate provider-level BaseURL to models without their own.
170 modelBaseURL := um.BaseURL
171 if modelBaseURL == "" {
172 modelBaseURL = prov.BaseURL
173 }
174 m := Model{
175 Provider: normalized,
176 ID: um.ID,
177 DisplayName: um.Name,
178 ContextWindow: um.ContextWindow,
179 MaxOutput: um.MaxTokens,
180 Reasoning: um.Reasoning,
181 PriceInput: um.PriceInput,
182 PriceOutput: um.PriceOutput,
183 PriceCacheRead: um.PriceCacheRead,
184 PriceCacheWrite: um.PriceCacheWrite,
185 BaseURL: modelBaseURL,
186 Source: "user",
187 }
188 if m.DisplayName == "" {
189 m.DisplayName = m.ID
190 }
191 out = append(out, m)
192 }
193 }
194 return out, warnings
195}
196
197// SetUserModels merges user-defined models into the active catalog.
198// User models take precedence over both the baked-in catalog and
199// live-discovered models.
200func SetUserModels(models []Model) {
201 if len(models) == 0 {
202 return
203 }
204 activeMu.Lock()
205 defer activeMu.Unlock()
206
207 // Ensure the active overlay starts from the full built-in catalog
208 // when no live/cache overlay has been applied. Otherwise a fresh
209 // install with only models.json would hide every built-in model.
210 if !activeSet || active == nil {
211 active = append([]Model(nil), Catalog...)
212 activeSet = true
213 }
214
215 // Build index of current active models.
216 byKey := func(p, id string) string { return p + "/" + id }
217 index := make(map[string]int, len(active))
218 for i, m := range active {
219 index[byKey(m.Provider, m.ID)] = i
220 }
221
222 for _, um := range models {
223 k := byKey(um.Provider, um.ID)
224 if idx, ok := index[k]; ok {
225 // Override existing entry but keep prices from user if
226 // they provided them, otherwise keep catalog prices.
227 existing := active[idx]
228 if um.PriceInput > 0 {
229 existing.PriceInput = um.PriceInput
230 }
231 if um.PriceOutput > 0 {
232 existing.PriceOutput = um.PriceOutput
233 }
234 if um.PriceCacheRead > 0 {
235 existing.PriceCacheRead = um.PriceCacheRead
236 }
237 if um.PriceCacheWrite > 0 {
238 existing.PriceCacheWrite = um.PriceCacheWrite
239 }
240 if um.DisplayName != "" {
241 existing.DisplayName = um.DisplayName
242 }
243 if um.ContextWindow > 0 {
244 existing.ContextWindow = um.ContextWindow
245 }
246 if um.MaxOutput > 0 {
247 existing.MaxOutput = um.MaxOutput
248 }
249 existing.Reasoning = um.Reasoning
250 if um.BaseURL != "" {
251 existing.BaseURL = um.BaseURL
252 }
253 existing.Source = "user"
254 existing.Speculative = false
255 active[idx] = existing
256 } else {
257 // New model not in catalog.
258 active = append(active, um)
259 }
260 }
261}