1package provider
2
3import (
4 "encoding/json"
5 "os"
6)
7
8// UserModelsFile is the JSON format for user-defined models.
9// Place a models.json in $ZOT_HOME to add models that aren't in the
10// baked-in catalog or to override catalog entries.
11//
12// Example:
13//
14// {
15// "providers": {
16// "openai": {
17// "models": [
18// {
19// "id": "gpt-5.5",
20// "name": "GPT-5.5",
21// "reasoning": true,
22// "contextWindow": 400000,
23// "maxTokens": 128000,
24// "priceInput": 2.50,
25// "priceOutput": 15.00,
26// "priceCacheRead": 0.25
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 Models []UserModel `json:"models"`
39}
40
41// UserModel is a single model entry in the user's models.json.
42type UserModel struct {
43 ID string `json:"id"`
44 Name string `json:"name"`
45 Reasoning bool `json:"reasoning"`
46 ContextWindow int `json:"contextWindow"`
47 MaxTokens int `json:"maxTokens"`
48 PriceInput float64 `json:"priceInput"`
49 PriceOutput float64 `json:"priceOutput"`
50 PriceCacheRead float64 `json:"priceCacheRead"`
51 PriceCacheWrite float64 `json:"priceCacheWrite"`
52 BaseURL string `json:"baseUrl,omitempty"`
53 Input []string `json:"input"` // informational only
54 API string `json:"api"` // informational only
55}
56
57// LoadUserModels reads a models.json file and returns the models
58// converted to the internal Model type. Returns nil on any error
59// (missing file, bad JSON, etc.) so the caller can treat it as
60// optional without error handling.
61func LoadUserModels(path string) []Model {
62 data, err := os.ReadFile(path)
63 if err != nil {
64 return nil
65 }
66 var file UserModelsFile
67 if err := json.Unmarshal(data, &file); err != nil {
68 return nil
69 }
70
71 var out []Model
72 for providerName, prov := range file.Providers {
73 // Normalize provider name: strip suffixes like "-codex" so
74 // "openai-codex" maps to "openai".
75 normalized := providerName
76 switch providerName {
77 case "openai-codex", "openai-responses":
78 normalized = "openai"
79 case "anthropic-messages":
80 normalized = "anthropic"
81 case "moonshot", "moonshot-ai", "kimi-code":
82 normalized = "kimi"
83 }
84
85 for _, um := range prov.Models {
86 m := Model{
87 Provider: normalized,
88 ID: um.ID,
89 DisplayName: um.Name,
90 ContextWindow: um.ContextWindow,
91 MaxOutput: um.MaxTokens,
92 Reasoning: um.Reasoning,
93 PriceInput: um.PriceInput,
94 PriceOutput: um.PriceOutput,
95 PriceCacheRead: um.PriceCacheRead,
96 PriceCacheWrite: um.PriceCacheWrite,
97 BaseURL: um.BaseURL,
98 Source: "user",
99 }
100 if m.DisplayName == "" {
101 m.DisplayName = m.ID
102 }
103 out = append(out, m)
104 }
105 }
106 return out
107}
108
109// SetUserModels merges user-defined models into the active catalog.
110// User models take precedence over both the baked-in catalog and
111// live-discovered models.
112func SetUserModels(models []Model) {
113 if len(models) == 0 {
114 return
115 }
116 activeMu.Lock()
117 defer activeMu.Unlock()
118
119 // Build index of current active models.
120 byKey := func(p, id string) string { return p + "/" + id }
121 index := make(map[string]int, len(active))
122 for i, m := range active {
123 index[byKey(m.Provider, m.ID)] = i
124 }
125
126 for _, um := range models {
127 k := byKey(um.Provider, um.ID)
128 if idx, ok := index[k]; ok {
129 // Override existing entry but keep prices from user if
130 // they provided them, otherwise keep catalog prices.
131 existing := active[idx]
132 if um.PriceInput > 0 {
133 existing.PriceInput = um.PriceInput
134 }
135 if um.PriceOutput > 0 {
136 existing.PriceOutput = um.PriceOutput
137 }
138 if um.PriceCacheRead > 0 {
139 existing.PriceCacheRead = um.PriceCacheRead
140 }
141 if um.PriceCacheWrite > 0 {
142 existing.PriceCacheWrite = um.PriceCacheWrite
143 }
144 if um.DisplayName != "" {
145 existing.DisplayName = um.DisplayName
146 }
147 if um.ContextWindow > 0 {
148 existing.ContextWindow = um.ContextWindow
149 }
150 if um.MaxOutput > 0 {
151 existing.MaxOutput = um.MaxOutput
152 }
153 existing.Reasoning = um.Reasoning
154 if um.BaseURL != "" {
155 existing.BaseURL = um.BaseURL
156 }
157 existing.Source = "user"
158 existing.Speculative = false
159 active[idx] = existing
160 } else {
161 // New model not in catalog.
162 active = append(active, um)
163 }
164 }
165}