v0.2.11 498b769

usermodels.go RawHistoryFolder

1package provider
2
3import (
4	"encoding/json"
5	"fmt"
6	"os"
7)
8
9// UserModelsFile is the JSON format for user-defined models.
10// Place a models.json in $ZOT_HOME to add models that aren't in the
11// baked-in catalog or to override catalog entries.
12//
13// Example:
14//
15//	{
16//	  "providers": {
17//	    "openai": {
18//	      "models": [
19//	        {
20//	          "id": "gpt-5.5",
21//	          "name": "GPT-5.5",
22//	          "reasoning": true,
23//	          "contextWindow": 400000,
24//	          "maxTokens": 128000,
25//	          "priceInput": 2.50,
26//	          "priceOutput": 15.00,
27//	          "priceCacheRead": 0.25
28//	        }
29//	      ]
30//	    }
31//	  }
32//	}
33type UserModelsFile struct {
34	Providers map[string]UserProvider `json:"providers"`
35}
36
37// UserProvider groups models under a provider key.
38type UserProvider struct {
39	Models []UserModel `json:"models"`
40}
41
42// UserModel is a single model entry in the user's models.json.
43type UserModel struct {
44	ID              string   `json:"id"`
45	Name            string   `json:"name"`
46	Reasoning       bool     `json:"reasoning"`
47	ContextWindow   int      `json:"contextWindow"`
48	MaxTokens       int      `json:"maxTokens"`
49	PriceInput      float64  `json:"priceInput"`
50	PriceOutput     float64  `json:"priceOutput"`
51	PriceCacheRead  float64  `json:"priceCacheRead"`
52	PriceCacheWrite float64  `json:"priceCacheWrite"`
53	BaseURL         string   `json:"baseUrl,omitempty"`
54	Input           []string `json:"input"` // informational only
55	API             string   `json:"api"`   // informational only
56}
57
58// LoadUserModels reads a models.json file and returns the models
59// converted to the internal Model type. Returns nil on any error
60// (missing file, bad JSON, etc.) so the caller can treat it as
61// optional without error handling.
62func LoadUserModels(path string) []Model {
63	models, _ := LoadUserModelsWithWarnings(path)
64	return models
65}
66
67// LoadUserModelsWithWarnings is like LoadUserModels but also returns
68// human-readable warnings about every recoverable issue it found in
69// the file (unknown provider id, empty model id, malformed JSON for a
70// single provider block, etc.). The caller is responsible for
71// surfacing the warnings; the file is never rejected wholesale unless
72// the top-level JSON itself fails to parse.
73func LoadUserModelsWithWarnings(path string) ([]Model, []string) {
74	var warnings []string
75	data, err := os.ReadFile(path)
76	if err != nil {
77		return nil, nil
78	}
79	var file UserModelsFile
80	if err := json.Unmarshal(data, &file); err != nil {
81		warnings = append(warnings, fmt.Sprintf("models.json: parse error: %v (file ignored)", err))
82		return nil, warnings
83	}
84
85	var out []Model
86	for providerName, prov := range file.Providers {
87		if providerName == "" {
88			warnings = append(warnings, "models.json: empty provider key skipped")
89			continue
90		}
91		// Normalize legacy transport aliases to their provider names.
92		normalized := providerName
93		switch providerName {
94		case "openai-responses":
95			normalized = "openai"
96		case "anthropic-messages":
97			normalized = "anthropic"
98		case "moonshot", "moonshot-ai", "kimi-code":
99			normalized = "kimi"
100		case "deepseek-chat", "deepseek-ai":
101			normalized = "deepseek"
102		}
103
104		for i, um := range prov.Models {
105			if um.ID == "" {
106				warnings = append(warnings, fmt.Sprintf("models.json: provider %q entry #%d has empty id; skipped", providerName, i))
107				continue
108			}
109			if um.ContextWindow < 0 || um.MaxTokens < 0 {
110				warnings = append(warnings, fmt.Sprintf("models.json: %s/%s has negative contextWindow/maxTokens; clamped to 0", normalized, um.ID))
111				if um.ContextWindow < 0 {
112					um.ContextWindow = 0
113				}
114				if um.MaxTokens < 0 {
115					um.MaxTokens = 0
116				}
117			}
118			m := Model{
119				Provider:        normalized,
120				ID:              um.ID,
121				DisplayName:     um.Name,
122				ContextWindow:   um.ContextWindow,
123				MaxOutput:       um.MaxTokens,
124				Reasoning:       um.Reasoning,
125				PriceInput:      um.PriceInput,
126				PriceOutput:     um.PriceOutput,
127				PriceCacheRead:  um.PriceCacheRead,
128				PriceCacheWrite: um.PriceCacheWrite,
129				BaseURL:         um.BaseURL,
130				Source:          "user",
131			}
132			if m.DisplayName == "" {
133				m.DisplayName = m.ID
134			}
135			out = append(out, m)
136		}
137	}
138	return out, warnings
139}
140
141// SetUserModels merges user-defined models into the active catalog.
142// User models take precedence over both the baked-in catalog and
143// live-discovered models.
144func SetUserModels(models []Model) {
145	if len(models) == 0 {
146		return
147	}
148	activeMu.Lock()
149	defer activeMu.Unlock()
150
151	// Build index of current active models.
152	byKey := func(p, id string) string { return p + "/" + id }
153	index := make(map[string]int, len(active))
154	for i, m := range active {
155		index[byKey(m.Provider, m.ID)] = i
156	}
157
158	for _, um := range models {
159		k := byKey(um.Provider, um.ID)
160		if idx, ok := index[k]; ok {
161			// Override existing entry but keep prices from user if
162			// they provided them, otherwise keep catalog prices.
163			existing := active[idx]
164			if um.PriceInput > 0 {
165				existing.PriceInput = um.PriceInput
166			}
167			if um.PriceOutput > 0 {
168				existing.PriceOutput = um.PriceOutput
169			}
170			if um.PriceCacheRead > 0 {
171				existing.PriceCacheRead = um.PriceCacheRead
172			}
173			if um.PriceCacheWrite > 0 {
174				existing.PriceCacheWrite = um.PriceCacheWrite
175			}
176			if um.DisplayName != "" {
177				existing.DisplayName = um.DisplayName
178			}
179			if um.ContextWindow > 0 {
180				existing.ContextWindow = um.ContextWindow
181			}
182			if um.MaxOutput > 0 {
183				existing.MaxOutput = um.MaxOutput
184			}
185			existing.Reasoning = um.Reasoning
186			if um.BaseURL != "" {
187				existing.BaseURL = um.BaseURL
188			}
189			existing.Source = "user"
190			existing.Speculative = false
191			active[idx] = existing
192		} else {
193			// New model not in catalog.
194			active = append(active, um)
195		}
196	}
197}

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