sub2api/backend/internal/service/antigravity_warmup.go
win 12ae97b755 fix: Increase maxOutputTokens in Antigravity test request from 1 to 10
The test request was using maxOutputTokens: 1, which caused Google API to
generate only 1 token. When decoded, this single token produced "It" as the
response, making it look like an error.

Changed:
- Content: "." → "Test connection" (more meaningful prompt)
- MaxTokens: 1 → 10 (enough tokens to verify connection is working)

This fixes the issue where account test always showed "It" in the response,
which was actually just the truncated output from the single-token generation.

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
2026-04-11 18:49:53 +08:00

84 lines
2.4 KiB
Go

package service
import (
"context"
"log/slog"
"time"
"github.com/Wei-Shaw/sub2api/internal/pkg/antigravity"
)
// WarmupAntigravityAccount 预热新的 Antigravity 账号
// 在账号创建后立即调用,避免首次请求的 503 延迟
//
// 预热流程:
// 1. GetUserInfo - 验证 token 有效性
// 2. LoadCodeAssist - 初始化项目信息
// 3. FetchAvailableModels - 初始化模型列表
//
// 总耗时通常 4-6 秒,预热期间的失败不影响账号创建结果(非阻塞)
func (s *AntigravityOAuthService) WarmupAntigravityAccount(ctx context.Context, accessToken, projectID, proxyURL string) {
logger := slog.Default()
// 5 秒超时预热(防止卡住其他操作)
warmupCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
client, err := antigravity.NewClient(proxyURL)
if err != nil {
logger.Warn("antigravity_warmup_client_creation_failed", "error", err)
return
}
start := time.Now()
defer func() {
elapsed := time.Since(start)
logger.Info("antigravity_account_warmup_completed", "elapsed_ms", elapsed.Milliseconds())
}()
// Step 1: 验证 token
_, err = client.GetUserInfo(warmupCtx, accessToken)
if err != nil {
logger.Warn("antigravity_warmup_get_user_info_failed", "error", err)
// 继续后续步骤(部分失败不中止)
}
// Step 2: 初始化项目信息
_, _, err = client.LoadCodeAssist(warmupCtx, accessToken)
if err != nil {
logger.Warn("antigravity_warmup_load_code_assist_failed", "error", err)
}
// Step 3: 初始化模型列表
if projectID != "" {
_, _, err := client.FetchAvailableModels(warmupCtx, accessToken, projectID)
if err != nil {
logger.Warn("antigravity_warmup_fetch_available_models_failed", "error", err)
}
}
}
// WarmupOptions 预热选项
type WarmupOptions struct {
// Async 为 true 时在后台预热(推荐)
Async bool
// Timeout 单次预热操作的超时时间
Timeout time.Duration
}
// WarmupAntigravityAccountAsync 异步预热账号(推荐用法)
func (s *AntigravityOAuthService) WarmupAntigravityAccountAsync(ctx context.Context, accessToken, projectID, proxyURL string, opts *WarmupOptions) {
if opts == nil {
opts = &WarmupOptions{
Async: true,
Timeout: 5 * time.Second,
}
}
if opts.Async {
go s.WarmupAntigravityAccount(ctx, accessToken, projectID, proxyURL)
} else {
s.WarmupAntigravityAccount(ctx, accessToken, projectID, proxyURL)
}
}