|
| 1 | +// Package registry — extra_models.go injects synthetic Codex models declared |
| 2 | +// in user configuration. Each spec clones an existing Codex model's parameters, |
| 3 | +// only changing the ID and Version. Clones are applied to every Codex tier |
| 4 | +// (free/team/plus/pro) where the inherit-from base model is present. |
| 5 | +// |
| 6 | +// Injected entries carry the ModelInfo.Injected marker so the registry can |
| 7 | +// safely re-apply or remove them on configuration changes and remote refreshes |
| 8 | +// without affecting upstream-defined data. When the upstream catalog later |
| 9 | +// ships a model with the same ID, the upstream entry takes priority and the |
| 10 | +// clone is silently superseded. |
| 11 | +package registry |
| 12 | + |
| 13 | +import ( |
| 14 | + "strings" |
| 15 | + "sync" |
| 16 | +) |
| 17 | + |
| 18 | +// CodexExtraModelSpec describes a synthetic Codex model that clones its |
| 19 | +// parameters from an existing Codex model. |
| 20 | +type CodexExtraModelSpec struct { |
| 21 | + ID string |
| 22 | + InheritFrom string |
| 23 | +} |
| 24 | + |
| 25 | +var ( |
| 26 | + codexExtrasMu sync.RWMutex |
| 27 | + codexExtras []CodexExtraModelSpec |
| 28 | +) |
| 29 | + |
| 30 | +// SetCodexExtraModels replaces the active spec list and immediately re-applies |
| 31 | +// the resulting clones to the catalog store. Safe to call at startup and on |
| 32 | +// configuration reload. |
| 33 | +func SetCodexExtraModels(specs []CodexExtraModelSpec) { |
| 34 | + sanitized := sanitizeCodexExtraSpecs(specs) |
| 35 | + codexExtrasMu.Lock() |
| 36 | + codexExtras = sanitized |
| 37 | + codexExtrasMu.Unlock() |
| 38 | + |
| 39 | + modelsCatalogStore.mu.Lock() |
| 40 | + applyCodexExtrasLocked() |
| 41 | + modelsCatalogStore.mu.Unlock() |
| 42 | +} |
| 43 | + |
| 44 | +// sanitizeCodexExtraSpecs trims fields, drops invalid/self-referential entries, |
| 45 | +// and removes duplicate IDs (case-insensitive). |
| 46 | +func sanitizeCodexExtraSpecs(specs []CodexExtraModelSpec) []CodexExtraModelSpec { |
| 47 | + if len(specs) == 0 { |
| 48 | + return nil |
| 49 | + } |
| 50 | + seen := make(map[string]struct{}, len(specs)) |
| 51 | + out := make([]CodexExtraModelSpec, 0, len(specs)) |
| 52 | + for _, s := range specs { |
| 53 | + id := strings.TrimSpace(s.ID) |
| 54 | + base := strings.TrimSpace(s.InheritFrom) |
| 55 | + if id == "" || base == "" || strings.EqualFold(id, base) { |
| 56 | + continue |
| 57 | + } |
| 58 | + key := strings.ToLower(id) |
| 59 | + if _, ok := seen[key]; ok { |
| 60 | + continue |
| 61 | + } |
| 62 | + seen[key] = struct{}{} |
| 63 | + out = append(out, CodexExtraModelSpec{ID: id, InheritFrom: base}) |
| 64 | + } |
| 65 | + return out |
| 66 | +} |
| 67 | + |
| 68 | +// applyCodexExtrasLocked rebuilds the codex tiers in the catalog store: it |
| 69 | +// first removes any previously-injected clones (identified by the Injected |
| 70 | +// marker), then injects clones for the current spec list. The caller MUST hold |
| 71 | +// modelsCatalogStore.mu (write lock). |
| 72 | +func applyCodexExtrasLocked() { |
| 73 | + if modelsCatalogStore.data == nil { |
| 74 | + return |
| 75 | + } |
| 76 | + data := modelsCatalogStore.data |
| 77 | + data.CodexFree = stripInjectedFromTier(data.CodexFree) |
| 78 | + data.CodexTeam = stripInjectedFromTier(data.CodexTeam) |
| 79 | + data.CodexPlus = stripInjectedFromTier(data.CodexPlus) |
| 80 | + data.CodexPro = stripInjectedFromTier(data.CodexPro) |
| 81 | + |
| 82 | + codexExtrasMu.RLock() |
| 83 | + specs := append([]CodexExtraModelSpec(nil), codexExtras...) |
| 84 | + codexExtrasMu.RUnlock() |
| 85 | + if len(specs) == 0 { |
| 86 | + return |
| 87 | + } |
| 88 | + data.CodexFree = applyExtrasToTier(data.CodexFree, specs) |
| 89 | + data.CodexTeam = applyExtrasToTier(data.CodexTeam, specs) |
| 90 | + data.CodexPlus = applyExtrasToTier(data.CodexPlus, specs) |
| 91 | + data.CodexPro = applyExtrasToTier(data.CodexPro, specs) |
| 92 | +} |
| 93 | + |
| 94 | +// applyExtrasToTier appends a clone for every spec whose base is present and |
| 95 | +// whose target ID does NOT yet exist in the tier (the upstream entry always |
| 96 | +// wins). Each clone is marked with Injected = true so future rebuilds can |
| 97 | +// strip it cleanly. |
| 98 | +func applyExtrasToTier(tier []*ModelInfo, specs []CodexExtraModelSpec) []*ModelInfo { |
| 99 | + if len(specs) == 0 || len(tier) == 0 { |
| 100 | + return tier |
| 101 | + } |
| 102 | + byID := make(map[string]*ModelInfo, len(tier)) |
| 103 | + for _, m := range tier { |
| 104 | + if m == nil { |
| 105 | + continue |
| 106 | + } |
| 107 | + id := strings.TrimSpace(m.ID) |
| 108 | + if id == "" { |
| 109 | + continue |
| 110 | + } |
| 111 | + byID[strings.ToLower(id)] = m |
| 112 | + } |
| 113 | + clones := make([]*ModelInfo, 0, len(specs)) |
| 114 | + for _, spec := range specs { |
| 115 | + if _, exists := byID[strings.ToLower(spec.ID)]; exists { |
| 116 | + continue |
| 117 | + } |
| 118 | + base, ok := byID[strings.ToLower(spec.InheritFrom)] |
| 119 | + if !ok { |
| 120 | + continue |
| 121 | + } |
| 122 | + clone := cloneModelInfo(base) |
| 123 | + clone.ID = spec.ID |
| 124 | + clone.Version = spec.ID |
| 125 | + clone.Injected = true |
| 126 | + clones = append(clones, clone) |
| 127 | + } |
| 128 | + if len(clones) == 0 { |
| 129 | + return tier |
| 130 | + } |
| 131 | + out := make([]*ModelInfo, 0, len(tier)+len(clones)) |
| 132 | + out = append(out, tier...) |
| 133 | + out = append(out, clones...) |
| 134 | + return out |
| 135 | +} |
| 136 | + |
| 137 | +// stripInjectedFromTier returns a tier with all Injected entries removed. |
| 138 | +// When no entries are marked Injected the original slice is returned without |
| 139 | +// allocation. |
| 140 | +func stripInjectedFromTier(tier []*ModelInfo) []*ModelInfo { |
| 141 | + if len(tier) == 0 { |
| 142 | + return tier |
| 143 | + } |
| 144 | + hasInjected := false |
| 145 | + for _, m := range tier { |
| 146 | + if m != nil && m.Injected { |
| 147 | + hasInjected = true |
| 148 | + break |
| 149 | + } |
| 150 | + } |
| 151 | + if !hasInjected { |
| 152 | + return tier |
| 153 | + } |
| 154 | + out := make([]*ModelInfo, 0, len(tier)) |
| 155 | + for _, m := range tier { |
| 156 | + if m == nil || m.Injected { |
| 157 | + continue |
| 158 | + } |
| 159 | + out = append(out, m) |
| 160 | + } |
| 161 | + return out |
| 162 | +} |
| 163 | + |
| 164 | +// stripCodexExtras returns a shallow-cloned catalog with all Injected entries |
| 165 | +// removed from the four Codex tiers. Used by the remote refresh path so |
| 166 | +// change-detection compares apples-to-apples (untouched upstream payload vs |
| 167 | +// untouched cached payload). When no entries are marked Injected, the original |
| 168 | +// pointer is returned without any allocation. |
| 169 | +func stripCodexExtras(data *staticModelsJSON) *staticModelsJSON { |
| 170 | + if data == nil { |
| 171 | + return nil |
| 172 | + } |
| 173 | + free := stripInjectedFromTier(data.CodexFree) |
| 174 | + team := stripInjectedFromTier(data.CodexTeam) |
| 175 | + plus := stripInjectedFromTier(data.CodexPlus) |
| 176 | + pro := stripInjectedFromTier(data.CodexPro) |
| 177 | + if sameSlice(free, data.CodexFree) && |
| 178 | + sameSlice(team, data.CodexTeam) && |
| 179 | + sameSlice(plus, data.CodexPlus) && |
| 180 | + sameSlice(pro, data.CodexPro) { |
| 181 | + return data |
| 182 | + } |
| 183 | + out := *data |
| 184 | + out.CodexFree = free |
| 185 | + out.CodexTeam = team |
| 186 | + out.CodexPlus = plus |
| 187 | + out.CodexPro = pro |
| 188 | + return &out |
| 189 | +} |
| 190 | + |
| 191 | +// sameSlice reports whether two slices share the same underlying array head. |
| 192 | +// Used as a cheap check to avoid a header-copy when stripInjectedFromTier |
| 193 | +// returned the input slice unchanged. |
| 194 | +func sameSlice(a, b []*ModelInfo) bool { |
| 195 | + return len(a) == len(b) && (len(a) == 0 || &a[0] == &b[0]) |
| 196 | +} |
0 commit comments