@@ -31,7 +31,6 @@ type Store struct {
3131 cacheFile string
3232 mu sync.Mutex
3333 db * Database
34- etag string // ETag from last successful fetch, used for conditional requests
3534}
3635
3736// NewStore returns the process-wide singleton Store.
@@ -72,18 +71,17 @@ func (s *Store) GetDatabase(ctx context.Context) (*Database, error) {
7271 return s .db , nil
7372 }
7473
75- db , etag , err := loadDatabase (ctx , s .cacheFile )
74+ db , err := loadDatabase (ctx , s .cacheFile )
7675 if err != nil {
7776 return nil , err
7877 }
7978
8079 s .db = db
81- s .etag = etag
8280 return db , nil
8381}
8482
85- // GetProvider returns a specific provider by ID.
86- func (s * Store ) GetProvider (ctx context.Context , providerID string ) (* Provider , error ) {
83+ // getProvider returns a specific provider by ID.
84+ func (s * Store ) getProvider (ctx context.Context , providerID string ) (* Provider , error ) {
8785 db , err := s .GetDatabase (ctx )
8886 if err != nil {
8987 return nil , err
@@ -106,30 +104,23 @@ func (s *Store) GetModel(ctx context.Context, id string) (*Model, error) {
106104 providerID := parts [0 ]
107105 modelID := parts [1 ]
108106
109- provider , err := s .GetProvider (ctx , providerID )
107+ provider , err := s .getProvider (ctx , providerID )
110108 if err != nil {
111109 return nil , err
112110 }
113111
114112 model , exists := provider .Models [modelID ]
115- if ! exists {
116- // For amazon-bedrock, try stripping region/inference profile prefixes
117- // Bedrock uses prefixes for cross-region inference profiles,
118- // but models.dev stores models without these prefixes.
119- //
120- // Strip known region prefixes and retry lookup.
121- if providerID == "amazon-bedrock" {
122- if before , after , ok := strings .Cut (modelID , "." ); ok {
123- possibleRegionPrefix := before
124- if isBedrockRegionPrefix (possibleRegionPrefix ) {
125- normalizedModelID := after
126- model , exists = provider .Models [normalizedModelID ]
127- if exists {
128- return & model , nil
129- }
130- }
131- }
113+
114+ // For amazon-bedrock, try stripping region/inference profile prefixes.
115+ // Bedrock uses prefixes for cross-region inference profiles,
116+ // but models.dev stores models without these prefixes.
117+ if ! exists && providerID == "amazon-bedrock" {
118+ if prefix , after , ok := strings .Cut (modelID , "." ); ok && bedrockRegionPrefixes [prefix ] {
119+ model , exists = provider .Models [after ]
132120 }
121+ }
122+
123+ if ! exists {
133124 return nil , fmt .Errorf ("model %q not found in provider %q" , modelID , providerID )
134125 }
135126
@@ -138,12 +129,11 @@ func (s *Store) GetModel(ctx context.Context, id string) (*Model, error) {
138129
139130// loadDatabase loads the database from the local cache file or
140131// falls back to fetching from the models.dev API.
141- // It returns the database and the ETag associated with the data.
142- func loadDatabase (ctx context.Context , cacheFile string ) (* Database , string , error ) {
132+ func loadDatabase (ctx context.Context , cacheFile string ) (* Database , error ) {
143133 // Try to load from cache first
144134 cached , err := loadFromCache (cacheFile )
145135 if err == nil && time .Since (cached .LastRefresh ) < refreshInterval {
146- return & cached .Database , cached . ETag , nil
136+ return & cached .Database , nil
147137 }
148138
149139 // Cache is stale or doesn't exist — try a conditional fetch with the ETag.
@@ -157,9 +147,9 @@ func loadDatabase(ctx context.Context, cacheFile string) (*Database, string, err
157147 // If API fetch fails but we have cached data, use it regardless of age.
158148 if cached != nil {
159149 slog .Debug ("API fetch failed, using stale cache" , "error" , fetchErr )
160- return & cached .Database , cached . ETag , nil
150+ return & cached .Database , nil
161151 }
162- return nil , "" , fmt .Errorf ("failed to fetch from API and no cached data available: %w" , fetchErr )
152+ return nil , fmt .Errorf ("failed to fetch from API and no cached data available: %w" , fetchErr )
163153 }
164154
165155 // database is nil when the server returned 304 Not Modified.
@@ -169,15 +159,15 @@ func loadDatabase(ctx context.Context, cacheFile string) (*Database, string, err
169159 if saveErr := saveToCache (cacheFile , & cached .Database , cached .ETag ); saveErr != nil {
170160 slog .Warn ("Failed to update cache timestamp" , "error" , saveErr )
171161 }
172- return & cached .Database , cached . ETag , nil
162+ return & cached .Database , nil
173163 }
174164
175165 // Save the fresh data to cache.
176166 if saveErr := saveToCache (cacheFile , database , newETag ); saveErr != nil {
177167 slog .Warn ("Failed to save to cache" , "error" , saveErr )
178168 }
179169
180- return database , newETag , nil
170+ return database , nil
181171}
182172
183173// fetchFromAPI fetches the models.dev database.
@@ -224,7 +214,6 @@ func fetchFromAPI(ctx context.Context, etag string) (*Database, string, error) {
224214
225215 return & Database {
226216 Providers : providers ,
227- UpdatedAt : time .Now (),
228217 }, newETag , nil
229218}
230219
@@ -243,11 +232,9 @@ func loadFromCache(cacheFile string) (*CachedData, error) {
243232}
244233
245234func saveToCache (cacheFile string , database * Database , etag string ) error {
246- now := time .Now ()
247235 cached := CachedData {
248236 Database : * database ,
249- CachedAt : now ,
250- LastRefresh : now ,
237+ LastRefresh : time .Now (),
251238 ETag : etag ,
252239 }
253240
@@ -280,8 +267,7 @@ func (s *Store) ResolveModelAlias(ctx context.Context, providerID, modelName str
280267 return modelName
281268 }
282269
283- // Get the provider from the database
284- provider , err := s .GetProvider (ctx , providerID )
270+ provider , err := s .getProvider (ctx , providerID )
285271 if err != nil {
286272 return modelName
287273 }
@@ -313,13 +299,8 @@ func (s *Store) ResolveModelAlias(ctx context.Context, providerID, modelName str
313299// stores models without regional prefixes. AWS uses these for cross-region inference profiles.
314300// See: https://docs.aws.amazon.com/bedrock/latest/userguide/cross-region-inference.html
315301var bedrockRegionPrefixes = map [string ]bool {
316- "us" : true , // US region inference profile
317- "eu" : true , // EU region inference profile
318- "apac" : true , // Asia Pacific region inference profile
319- "global" : true , // Global inference profile (routes to any available region)
320- }
321-
322- // isBedrockRegionPrefix returns true if the prefix is a known Bedrock regional/inference profile prefix.
323- func isBedrockRegionPrefix (prefix string ) bool {
324- return bedrockRegionPrefixes [prefix ]
302+ "us" : true ,
303+ "eu" : true ,
304+ "apac" : true ,
305+ "global" : true ,
325306}
0 commit comments