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 legacy transport aliases to their provider names.
74 normalized := providerName
75 switch providerName {
76 case "openai-responses":
77 normalized = "openai"
78 case "anthropic-messages":
79 normalized = "anthropic"
80 case "moonshot", "moonshot-ai", "kimi-code":
81 normalized = "kimi"
82 case "deepseek-chat", "deepseek-ai":
83 normalized = "deepseek"
84 }
85
86 for _, um := range prov.Models {
87 m := Model{
88 Provider: normalized,
89 ID: um.ID,
90 DisplayName: um.Name,
91 ContextWindow: um.ContextWindow,
92 MaxOutput: um.MaxTokens,
93 Reasoning: um.Reasoning,
94 PriceInput: um.PriceInput,
95 PriceOutput: um.PriceOutput,
96 PriceCacheRead: um.PriceCacheRead,
97 PriceCacheWrite: um.PriceCacheWrite,
98 BaseURL: um.BaseURL,
99 Source: "user",
100 }
101 if m.DisplayName == "" {
102 m.DisplayName = m.ID
103 }
104 out = append(out, m)
105 }
106 }
107 return out
108}
109
110// SetUserModels merges user-defined models into the active catalog.
111// User models take precedence over both the baked-in catalog and
112// live-discovered models.
113func SetUserModels(models []Model) {
114 if len(models) == 0 {
115 return
116 }
117 activeMu.Lock()
118 defer activeMu.Unlock()
119
120 // Build index of current active models.
121 byKey := func(p, id string) string { return p + "/" + id }
122 index := make(map[string]int, len(active))
123 for i, m := range active {
124 index[byKey(m.Provider, m.ID)] = i
125 }
126
127 for _, um := range models {
128 k := byKey(um.Provider, um.ID)
129 if idx, ok := index[k]; ok {
130 // Override existing entry but keep prices from user if
131 // they provided them, otherwise keep catalog prices.
132 existing := active[idx]
133 if um.PriceInput > 0 {
134 existing.PriceInput = um.PriceInput
135 }
136 if um.PriceOutput > 0 {
137 existing.PriceOutput = um.PriceOutput
138 }
139 if um.PriceCacheRead > 0 {
140 existing.PriceCacheRead = um.PriceCacheRead
141 }
142 if um.PriceCacheWrite > 0 {
143 existing.PriceCacheWrite = um.PriceCacheWrite
144 }
145 if um.DisplayName != "" {
146 existing.DisplayName = um.DisplayName
147 }
148 if um.ContextWindow > 0 {
149 existing.ContextWindow = um.ContextWindow
150 }
151 if um.MaxOutput > 0 {
152 existing.MaxOutput = um.MaxOutput
153 }
154 existing.Reasoning = um.Reasoning
155 if um.BaseURL != "" {
156 existing.BaseURL = um.BaseURL
157 }
158 existing.Source = "user"
159 existing.Speculative = false
160 active[idx] = existing
161 } else {
162 // New model not in catalog.
163 active = append(active, um)
164 }
165 }
166}