apiRefreshAccountModels POST /admin/api/accounts/{id}/models/refresh 立即为指定账号拉取并更新模型路由缓存。
(w http.ResponseWriter, r *http.Request, id string)
| 642 | modelIDs := make([]string, 0, len(models)) |
| 643 | for _, m := range models { |
| 644 | modelIDs = append(modelIDs, m.ModelId) |
| 645 | } |
| 646 | h.pool.SetModelList(account.ID, modelIDs) |
| 647 | aggregated = mergeUniqueModels(aggregated, models) |
| 648 | } |
| 649 | |
| 650 | if len(aggregated) > 0 { |
| 651 | h.modelsCacheMu.Lock() |
| 652 | h.cachedModels = aggregated |
| 653 | h.modelsCacheTime = time.Now().Unix() |
| 654 | h.modelsCacheMu.Unlock() |
| 655 | logger.Infof("[ModelsCache] Cached %d models", len(aggregated)) |
| 656 | } |
| 657 | } |
| 658 | |
| 659 | // fetchAndCacheAccountModels 为单个账号拉取并写入模型缓存。 |
| 660 | // 同时更新 pool 的路由缓存与全局聚合模型列表。 |
| 661 | func (h *Handler) fetchAndCacheAccountModels(account *config.Account) error { |
| 662 | if err := h.ensureValidToken(account); err != nil { |
| 663 | return fmt.Errorf("token refresh failed: %w", err) |
| 664 | } |
| 665 | models, err := ListAvailableModels(account) |
| 666 | if err != nil { |
| 667 | return err |
| 668 | } |
| 669 | modelIDs := make([]string, 0, len(models)) |
| 670 | for _, m := range models { |
| 671 | modelIDs = append(modelIDs, m.ModelId) |
| 672 | } |
| 673 | h.pool.SetModelList(account.ID, modelIDs) |
| 674 | |
| 675 | // 合并到聚合缓存 |
| 676 | h.modelsCacheMu.Lock() |
| 677 | h.cachedModels = mergeUniqueModels(h.cachedModels, models) |
no test coverage detected