From 116573311fda7bac340981ca0d14c1b2085ae4aa Mon Sep 17 00:00:00 2001 From: hkfires <10558748+hkfires@users.noreply.github.com> Date: Wed, 4 Feb 2026 14:02:58 +0800 Subject: [PATCH] fix(cliproxy): update auth before model registration --- sdk/cliproxy/service.go | 39 +++++++++++++++++++++++++++------------ 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/sdk/cliproxy/service.go b/sdk/cliproxy/service.go index d08f5027..4223b5b2 100644 --- a/sdk/cliproxy/service.go +++ b/sdk/cliproxy/service.go @@ -273,27 +273,42 @@ func (s *Service) wsOnDisconnected(channelID string, reason error) { } func (s *Service) applyCoreAuthAddOrUpdate(ctx context.Context, auth *coreauth.Auth) { - if s == nil || auth == nil || auth.ID == "" { - return - } - if s.coreManager == nil { + if s == nil || s.coreManager == nil || auth == nil || auth.ID == "" { return } auth = auth.Clone() s.ensureExecutorsForAuth(auth) - s.registerModelsForAuth(auth) - if existing, ok := s.coreManager.GetByID(auth.ID); ok && existing != nil { + + // IMPORTANT: Update coreManager FIRST, before model registration. + // This ensures that configuration changes (proxy_url, prefix, etc.) take effect + // immediately for API calls, rather than waiting for model registration to complete. + // Model registration may involve network calls (e.g., FetchAntigravityModels) that + // could timeout if the new proxy_url is unreachable. + op := "register" + var err error + if existing, ok := s.coreManager.GetByID(auth.ID); ok { auth.CreatedAt = existing.CreatedAt auth.LastRefreshedAt = existing.LastRefreshedAt auth.NextRefreshAfter = existing.NextRefreshAfter - if _, err := s.coreManager.Update(ctx, auth); err != nil { - log.Errorf("failed to update auth %s: %v", auth.ID, err) + op = "update" + _, err = s.coreManager.Update(ctx, auth) + } else { + _, err = s.coreManager.Register(ctx, auth) + } + if err != nil { + log.Errorf("failed to %s auth %s: %v", op, auth.ID, err) + current, ok := s.coreManager.GetByID(auth.ID) + if !ok || current.Disabled { + GlobalModelRegistry().UnregisterClient(auth.ID) + return } - return - } - if _, err := s.coreManager.Register(ctx, auth); err != nil { - log.Errorf("failed to register auth %s: %v", auth.ID, err) + auth = current } + + // Register models after auth is updated in coreManager. + // This operation may block on network calls, but the auth configuration + // is already effective at this point. + s.registerModelsForAuth(auth) } func (s *Service) applyCoreAuthRemoval(ctx context.Context, id string) {