@@ -197,29 +197,67 @@ func (s *Service) cachedModels() ([]api.ModelInfo, bool) {
197197
198198func (s * Service ) refresh (ctx context.Context ) ([]api.ModelInfo , error ) {
199199 baseURL := s .BaseURL ()
200+ token := s .currentAccessToken ()
201+ models , limits , err := fetchCloudModels (ctx , s .client , baseURL , token )
202+ if err != nil && token != "" && isUnauthorizedStatus (err ) {
203+ models , limits , err = fetchCloudModels (ctx , s .client , baseURL , "" )
204+ }
205+ if err != nil {
206+ return nil , err
207+ }
208+
209+ s .mu .Lock ()
210+ s .cached = cloneModels (models )
211+ s .limits = limits
212+ s .cachedAt = time .Now ()
213+ s .mu .Unlock ()
214+
215+ return models , nil
216+ }
217+
218+ type cloudModelListStatusError struct {
219+ statusCode int
220+ body string
221+ }
222+
223+ func (e cloudModelListStatusError ) Error () string {
224+ return fmt .Sprintf ("cloud model list returned %d: %s" , e .statusCode , e .body )
225+ }
226+
227+ func isUnauthorizedStatus (err error ) bool {
228+ if statusErr , ok := err .(cloudModelListStatusError ); ok {
229+ return statusErr .statusCode == http .StatusUnauthorized || statusErr .statusCode == http .StatusForbidden
230+ }
231+ return false
232+ }
233+
234+ func fetchCloudModels (ctx context.Context , client * http.Client , baseURL , token string ) ([]api.ModelInfo , map [string ]ModelTokenLimits , error ) {
200235 req , err := http .NewRequestWithContext (ctx , http .MethodGet , baseURL + "/v1/models?page=" + cloudModelListPage + "&per=" + cloudModelListPer , nil )
201236 if err != nil {
202- return nil , fmt .Errorf ("creating cloud model request: %w" , err )
237+ return nil , nil , fmt .Errorf ("creating cloud model request: %w" , err )
203238 }
204239 req .Header .Set ("Accept" , "application/json" )
205- if token := s . currentAccessToken (); token != "" {
240+ if token != "" {
206241 req .Header .Set ("Authorization" , "Bearer " + token )
207242 }
208243
209- resp , err := s . client .Do (req )
244+ resp , err := client .Do (req )
210245 if err != nil {
211- return nil , fmt .Errorf ("fetching cloud models: %w" , err )
246+ return nil , nil , fmt .Errorf ("fetching cloud models: %w" , err )
212247 }
213248 defer resp .Body .Close ()
214249
215250 if resp .StatusCode != http .StatusOK {
216251 body , _ := io .ReadAll (io .LimitReader (resp .Body , 4096 ))
217- return nil , fmt .Errorf ("cloud model list returned %d: %s" , resp .StatusCode , strings .TrimSpace (string (body )))
252+ return nil , nil , cloudModelListStatusError {
253+ statusCode : resp .StatusCode ,
254+ body : strings .TrimSpace (string (body )),
255+ }
218256 }
219257
220258 var payload modelListResponse
221259 if err := json .NewDecoder (resp .Body ).Decode (& payload ); err != nil {
222- return nil , fmt .Errorf ("decoding cloud model list: %w" , err )
260+ return nil , nil , fmt .Errorf ("decoding cloud model list: %w" , err )
223261 }
224262
225263 models := make ([]api.ModelInfo , 0 , len (payload .Data ))
@@ -233,13 +271,7 @@ func (s *Service) refresh(ctx context.Context) ([]api.ModelInfo, error) {
233271 limits [strings .TrimSpace (info .Model )] = modelTokenLimitsFromRemote (item )
234272 }
235273
236- s .mu .Lock ()
237- s .cached = cloneModels (models )
238- s .limits = limits
239- s .cachedAt = time .Now ()
240- s .mu .Unlock ()
241-
242- return models , nil
274+ return models , limits , nil
243275}
244276
245277func (s * Service ) currentAccessToken () string {
0 commit comments