v0.2.95 b3586dd

usermodels.go RawHistoryFolder

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) 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" 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 "anthropic-messages", "messages", "anthropic":
142				api = "anthropic"
143			default:
144				warnings = append(warnings, fmt.Sprintf("models.json: provider %q has unknown api %q; defaulting to openai", providerName, prov.API))
145				api = "openai"
146			}
147			customProviders[normalized] = CustomProviderConfig{
148				BaseURL: prov.BaseURL,
149				API:     api,
150			}
151		}
152
153		for i, um := range prov.Models {
154			if um.ID == "" {
155				warnings = append(warnings, fmt.Sprintf("models.json: provider %q entry #%d has empty id; skipped", providerName, i))
156				continue
157			}
158			if um.ContextWindow < 0 || um.MaxTokens < 0 {
159				warnings = append(warnings, fmt.Sprintf("models.json: %s/%s has negative contextWindow/maxTokens; clamped to 0", normalized, um.ID))
160				if um.ContextWindow < 0 {
161					um.ContextWindow = 0
162				}
163				if um.MaxTokens < 0 {
164					um.MaxTokens = 0
165				}
166			}
167			// Propagate provider-level BaseURL to models without their own.
168			modelBaseURL := um.BaseURL
169			if modelBaseURL == "" {
170				modelBaseURL = prov.BaseURL
171			}
172			m := Model{
173				Provider:        normalized,
174				ID:              um.ID,
175				DisplayName:     um.Name,
176				ContextWindow:   um.ContextWindow,
177				MaxOutput:       um.MaxTokens,
178				Reasoning:       um.Reasoning,
179				PriceInput:      um.PriceInput,
180				PriceOutput:     um.PriceOutput,
181				PriceCacheRead:  um.PriceCacheRead,
182				PriceCacheWrite: um.PriceCacheWrite,
183				BaseURL:         modelBaseURL,
184				Source:          "user",
185			}
186			if m.DisplayName == "" {
187				m.DisplayName = m.ID
188			}
189			out = append(out, m)
190		}
191	}
192	return out, warnings
193}
194
195// SetUserModels merges user-defined models into the active catalog.
196// User models take precedence over both the baked-in catalog and
197// live-discovered models.
198func SetUserModels(models []Model) {
199	if len(models) == 0 {
200		return
201	}
202	activeMu.Lock()
203	defer activeMu.Unlock()
204
205	// Ensure the active overlay starts from the full built-in catalog
206	// when no live/cache overlay has been applied. Otherwise a fresh
207	// install with only models.json would hide every built-in model.
208	if !activeSet || active == nil {
209		active = append([]Model(nil), Catalog...)
210		activeSet = true
211	}
212
213	// Build index of current active models.
214	byKey := func(p, id string) string { return p + "/" + id }
215	index := make(map[string]int, len(active))
216	for i, m := range active {
217		index[byKey(m.Provider, m.ID)] = i
218	}
219
220	for _, um := range models {
221		k := byKey(um.Provider, um.ID)
222		if idx, ok := index[k]; ok {
223			// Override existing entry but keep prices from user if
224			// they provided them, otherwise keep catalog prices.
225			existing := active[idx]
226			if um.PriceInput > 0 {
227				existing.PriceInput = um.PriceInput
228			}
229			if um.PriceOutput > 0 {
230				existing.PriceOutput = um.PriceOutput
231			}
232			if um.PriceCacheRead > 0 {
233				existing.PriceCacheRead = um.PriceCacheRead
234			}
235			if um.PriceCacheWrite > 0 {
236				existing.PriceCacheWrite = um.PriceCacheWrite
237			}
238			if um.DisplayName != "" {
239				existing.DisplayName = um.DisplayName
240			}
241			if um.ContextWindow > 0 {
242				existing.ContextWindow = um.ContextWindow
243			}
244			if um.MaxOutput > 0 {
245				existing.MaxOutput = um.MaxOutput
246			}
247			existing.Reasoning = um.Reasoning
248			if um.BaseURL != "" {
249				existing.BaseURL = um.BaseURL
250			}
251			existing.Source = "user"
252			existing.Speculative = false
253			active[idx] = existing
254		} else {
255			// New model not in catalog.
256			active = append(active, um)
257		}
258	}
259}

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 1

Tags 476

↑↓ moveenter openesc close