v0.1.13 d53fbf4

usermodels.go RawHistoryFolder

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

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