@@ -89,7 +89,8 @@ type AntigravityTokenInfo struct {
8989 TokenType string `json:"token_type"`
9090 Email string `json:"email,omitempty"`
9191 ProjectID string `json:"project_id,omitempty"`
92- ProjectIDMissing bool `json:"-"` // LoadCodeAssist 未返回 project_id
92+ ProjectIDMissing bool `json:"-"`
93+ PlanType string `json:"-"`
9394}
9495
9596// ExchangeCode 用 authorization code 交换 token
@@ -145,13 +146,17 @@ func (s *AntigravityOAuthService) ExchangeCode(ctx context.Context, input *Antig
145146 result .Email = userInfo .Email
146147 }
147148
148- // 获取 project_id(部分账户类型可能没有),失败时重试
149- projectID , loadErr := s .loadProjectIDWithRetry (ctx , tokenResp .AccessToken , proxyURL , 3 )
149+ // 获取 project_id + plan_type (部分账户类型可能没有),失败时重试
150+ loadResult , loadErr := s .loadProjectIDWithRetry (ctx , tokenResp .AccessToken , proxyURL , 3 )
150151 if loadErr != nil {
151152 fmt .Printf ("[AntigravityOAuth] 警告: 获取 project_id 失败(重试后): %v\n " , loadErr )
152153 result .ProjectIDMissing = true
153- } else {
154- result .ProjectID = projectID
154+ }
155+ if loadResult != nil {
156+ result .ProjectID = loadResult .ProjectID
157+ if loadResult .Subscription != nil {
158+ result .PlanType = loadResult .Subscription .PlanType
159+ }
155160 }
156161
157162 return result , nil
@@ -230,13 +235,17 @@ func (s *AntigravityOAuthService) ValidateRefreshToken(ctx context.Context, refr
230235 tokenInfo .Email = userInfo .Email
231236 }
232237
233- // 获取 project_id(容错,失败不阻塞)
234- projectID , loadErr := s .loadProjectIDWithRetry (ctx , tokenInfo .AccessToken , proxyURL , 3 )
238+ // 获取 project_id + plan_type (容错,失败不阻塞)
239+ loadResult , loadErr := s .loadProjectIDWithRetry (ctx , tokenInfo .AccessToken , proxyURL , 3 )
235240 if loadErr != nil {
236241 fmt .Printf ("[AntigravityOAuth] 警告: 获取 project_id 失败(重试后): %v\n " , loadErr )
237242 tokenInfo .ProjectIDMissing = true
238- } else {
239- tokenInfo .ProjectID = projectID
243+ }
244+ if loadResult != nil {
245+ tokenInfo .ProjectID = loadResult .ProjectID
246+ if loadResult .Subscription != nil {
247+ tokenInfo .PlanType = loadResult .Subscription .PlanType
248+ }
240249 }
241250
242251 return tokenInfo , nil
@@ -288,33 +297,42 @@ func (s *AntigravityOAuthService) RefreshAccountToken(ctx context.Context, accou
288297 tokenInfo .Email = existingEmail
289298 }
290299
291- // 每次刷新都调用 LoadCodeAssist 获取 project_id,失败时重试
300+ // 每次刷新都调用 LoadCodeAssist 获取 project_id + plan_type ,失败时重试
292301 existingProjectID := strings .TrimSpace (account .GetCredential ("project_id" ))
293- projectID , loadErr := s .loadProjectIDWithRetry (ctx , tokenInfo .AccessToken , proxyURL , 3 )
302+ loadResult , loadErr := s .loadProjectIDWithRetry (ctx , tokenInfo .AccessToken , proxyURL , 3 )
294303
295304 if loadErr != nil {
296- // LoadCodeAssist 失败,保留原有 project_id
297305 tokenInfo .ProjectID = existingProjectID
298- // 只有从未获取过 project_id 且本次也获取失败时,才标记为真正缺失
299- // 如果之前有 project_id,本次只是临时故障,不应标记为错误
300306 if existingProjectID == "" {
301307 tokenInfo .ProjectIDMissing = true
302308 }
303- } else {
304- tokenInfo .ProjectID = projectID
309+ }
310+ if loadResult != nil {
311+ if loadResult .ProjectID != "" {
312+ tokenInfo .ProjectID = loadResult .ProjectID
313+ }
314+ if loadResult .Subscription != nil {
315+ tokenInfo .PlanType = loadResult .Subscription .PlanType
316+ }
305317 }
306318
307319 return tokenInfo , nil
308320}
309321
310- // loadProjectIDWithRetry 带重试机制获取 project_id
311- // 返回 project_id 和错误,失败时会重试指定次数
312- func (s * AntigravityOAuthService ) loadProjectIDWithRetry (ctx context.Context , accessToken , proxyURL string , maxRetries int ) (string , error ) {
322+ // loadCodeAssistResult 封装 loadProjectIDWithRetry 的返回结果,
323+ // 同时携带从 LoadCodeAssist 响应中提取的 plan_type 信息。
324+ type loadCodeAssistResult struct {
325+ ProjectID string
326+ Subscription * AntigravitySubscriptionResult
327+ }
328+
329+ // loadProjectIDWithRetry 带重试机制获取 project_id,同时从响应中提取 plan_type。
330+ func (s * AntigravityOAuthService ) loadProjectIDWithRetry (ctx context.Context , accessToken , proxyURL string , maxRetries int ) (* loadCodeAssistResult , error ) {
313331 var lastErr error
332+ var lastSubscription * AntigravitySubscriptionResult
314333
315334 for attempt := 0 ; attempt <= maxRetries ; attempt ++ {
316335 if attempt > 0 {
317- // 指数退避:1s, 2s, 4s
318336 backoff := time .Duration (1 << uint (attempt - 1 )) * time .Second
319337 if backoff > 8 * time .Second {
320338 backoff = 8 * time .Second
@@ -324,24 +342,34 @@ func (s *AntigravityOAuthService) loadProjectIDWithRetry(ctx context.Context, ac
324342
325343 client , err := antigravity .NewClient (proxyURL )
326344 if err != nil {
327- return "" , fmt .Errorf ("create antigravity client failed: %w" , err )
345+ return nil , fmt .Errorf ("create antigravity client failed: %w" , err )
328346 }
329347 loadResp , loadRaw , err := client .LoadCodeAssist (ctx , accessToken )
330348
349+ if loadResp != nil {
350+ sub := NormalizeAntigravitySubscription (loadResp )
351+ lastSubscription = & sub
352+ }
353+
331354 if err == nil && loadResp != nil && loadResp .CloudAICompanionProject != "" {
332- return loadResp .CloudAICompanionProject , nil
355+ return & loadCodeAssistResult {
356+ ProjectID : loadResp .CloudAICompanionProject ,
357+ Subscription : lastSubscription ,
358+ }, nil
333359 }
334360
335361 if err == nil {
336362 if projectID , onboardErr := tryOnboardProjectID (ctx , client , accessToken , loadRaw ); onboardErr == nil && projectID != "" {
337- return projectID , nil
363+ return & loadCodeAssistResult {
364+ ProjectID : projectID ,
365+ Subscription : lastSubscription ,
366+ }, nil
338367 } else if onboardErr != nil {
339368 lastErr = onboardErr
340369 continue
341370 }
342371 }
343372
344- // 记录错误
345373 if err != nil {
346374 lastErr = err
347375 } else if loadResp == nil {
@@ -351,7 +379,10 @@ func (s *AntigravityOAuthService) loadProjectIDWithRetry(ctx context.Context, ac
351379 }
352380 }
353381
354- return "" , fmt .Errorf ("获取 project_id 失败 (重试 %d 次后): %w" , maxRetries , lastErr )
382+ if lastSubscription != nil {
383+ return & loadCodeAssistResult {Subscription : lastSubscription }, fmt .Errorf ("获取 project_id 失败 (重试 %d 次后): %w" , maxRetries , lastErr )
384+ }
385+ return nil , fmt .Errorf ("获取 project_id 失败 (重试 %d 次后): %w" , maxRetries , lastErr )
355386}
356387
357388func tryOnboardProjectID (ctx context.Context , client * antigravity.Client , accessToken string , loadRaw map [string ]any ) (string , error ) {
@@ -410,7 +441,11 @@ func (s *AntigravityOAuthService) FillProjectID(ctx context.Context, account *Ac
410441 proxyURL = proxy .URL ()
411442 }
412443 }
413- return s .loadProjectIDWithRetry (ctx , accessToken , proxyURL , 3 )
444+ result , err := s .loadProjectIDWithRetry (ctx , accessToken , proxyURL , 3 )
445+ if result != nil {
446+ return result .ProjectID , err
447+ }
448+ return "" , err
414449}
415450
416451// BuildAccountCredentials 构建账户凭证
@@ -431,6 +466,9 @@ func (s *AntigravityOAuthService) BuildAccountCredentials(tokenInfo *Antigravity
431466 if tokenInfo .ProjectID != "" {
432467 creds ["project_id" ] = tokenInfo .ProjectID
433468 }
469+ if tokenInfo .PlanType != "" {
470+ creds ["plan_type" ] = tokenInfo .PlanType
471+ }
434472 return creds
435473}
436474
0 commit comments