2026-01-16 13:03:04 +08:00
|
|
|
|
package service
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
2026-02-07 12:31:10 +08:00
|
|
|
|
"context"
|
2026-01-16 13:03:04 +08:00
|
|
|
|
"strings"
|
|
|
|
|
|
"time"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
const modelRateLimitsKey = "model_rate_limits"
|
|
|
|
|
|
|
2026-02-07 12:31:10 +08:00
|
|
|
|
// isRateLimitActiveForKey 检查指定 key 的限流是否生效
|
|
|
|
|
|
func (a *Account) isRateLimitActiveForKey(key string) bool {
|
|
|
|
|
|
resetAt := a.modelRateLimitResetAt(key)
|
|
|
|
|
|
return resetAt != nil && time.Now().Before(*resetAt)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// getRateLimitRemainingForKey 获取指定 key 的限流剩余时间,0 表示未限流或已过期
|
|
|
|
|
|
func (a *Account) getRateLimitRemainingForKey(key string) time.Duration {
|
|
|
|
|
|
resetAt := a.modelRateLimitResetAt(key)
|
|
|
|
|
|
if resetAt == nil {
|
|
|
|
|
|
return 0
|
2026-01-16 13:03:04 +08:00
|
|
|
|
}
|
2026-02-07 12:31:10 +08:00
|
|
|
|
remaining := time.Until(*resetAt)
|
|
|
|
|
|
if remaining > 0 {
|
|
|
|
|
|
return remaining
|
2026-01-16 13:03:04 +08:00
|
|
|
|
}
|
2026-02-07 12:31:10 +08:00
|
|
|
|
return 0
|
2026-01-16 13:03:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-07 12:31:10 +08:00
|
|
|
|
func (a *Account) isModelRateLimitedWithContext(ctx context.Context, requestedModel string) bool {
|
|
|
|
|
|
if a == nil {
|
2026-01-16 13:03:04 +08:00
|
|
|
|
return false
|
|
|
|
|
|
}
|
2026-02-07 12:31:10 +08:00
|
|
|
|
|
|
|
|
|
|
modelKey := a.GetMappedModel(requestedModel)
|
|
|
|
|
|
if a.Platform == PlatformAntigravity {
|
|
|
|
|
|
modelKey = resolveFinalAntigravityModelKey(ctx, a, requestedModel)
|
|
|
|
|
|
}
|
|
|
|
|
|
modelKey = strings.TrimSpace(modelKey)
|
|
|
|
|
|
if modelKey == "" {
|
2026-01-16 13:03:04 +08:00
|
|
|
|
return false
|
|
|
|
|
|
}
|
2026-02-07 12:31:10 +08:00
|
|
|
|
return a.isRateLimitActiveForKey(modelKey)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// GetModelRateLimitRemainingTime 获取模型限流剩余时间
|
|
|
|
|
|
// 返回 0 表示未限流或已过期
|
|
|
|
|
|
func (a *Account) GetModelRateLimitRemainingTime(requestedModel string) time.Duration {
|
|
|
|
|
|
return a.GetModelRateLimitRemainingTimeWithContext(context.Background(), requestedModel)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (a *Account) GetModelRateLimitRemainingTimeWithContext(ctx context.Context, requestedModel string) time.Duration {
|
|
|
|
|
|
if a == nil {
|
|
|
|
|
|
return 0
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
modelKey := a.GetMappedModel(requestedModel)
|
|
|
|
|
|
if a.Platform == PlatformAntigravity {
|
|
|
|
|
|
modelKey = resolveFinalAntigravityModelKey(ctx, a, requestedModel)
|
|
|
|
|
|
}
|
|
|
|
|
|
modelKey = strings.TrimSpace(modelKey)
|
|
|
|
|
|
if modelKey == "" {
|
|
|
|
|
|
return 0
|
|
|
|
|
|
}
|
|
|
|
|
|
return a.getRateLimitRemainingForKey(modelKey)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func resolveFinalAntigravityModelKey(ctx context.Context, account *Account, requestedModel string) string {
|
|
|
|
|
|
modelKey := mapAntigravityModel(account, requestedModel)
|
|
|
|
|
|
if modelKey == "" {
|
|
|
|
|
|
return ""
|
|
|
|
|
|
}
|
|
|
|
|
|
// thinking 会影响 Antigravity 最终模型名(例如 claude-sonnet-4-5 -> claude-sonnet-4-5-thinking)
|
2026-02-28 15:01:20 +08:00
|
|
|
|
if enabled, ok := ThinkingEnabledFromContext(ctx); ok {
|
2026-02-07 12:31:10 +08:00
|
|
|
|
modelKey = applyThinkingModelSuffix(modelKey, enabled)
|
|
|
|
|
|
}
|
|
|
|
|
|
return modelKey
|
2026-01-16 13:03:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (a *Account) modelRateLimitResetAt(scope string) *time.Time {
|
|
|
|
|
|
if a == nil || a.Extra == nil || scope == "" {
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
rawLimits, ok := a.Extra[modelRateLimitsKey].(map[string]any)
|
|
|
|
|
|
if !ok {
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
rawLimit, ok := rawLimits[scope].(map[string]any)
|
|
|
|
|
|
if !ok {
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
resetAtRaw, ok := rawLimit["rate_limit_reset_at"].(string)
|
|
|
|
|
|
if !ok || strings.TrimSpace(resetAtRaw) == "" {
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
resetAt, err := time.Parse(time.RFC3339, resetAtRaw)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
return &resetAt
|
|
|
|
|
|
}
|