antigravity_quota_scope.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package service
  2. import (
  3. "context"
  4. "strings"
  5. "time"
  6. )
  7. func normalizeAntigravityModelName(model string) string {
  8. normalized := strings.ToLower(strings.TrimSpace(model))
  9. if idx := strings.LastIndex(normalized, "/publishers/google/models/"); idx != -1 {
  10. normalized = normalized[idx+len("/publishers/google/models/"):]
  11. } else if idx := strings.LastIndex(normalized, "/publishers/anthropic/models/"); idx != -1 {
  12. normalized = normalized[idx+len("/publishers/anthropic/models/"):]
  13. } else if idx := strings.LastIndex(normalized, "/models/"); idx != -1 {
  14. normalized = normalized[idx+len("/models/"):]
  15. } else {
  16. normalized = strings.TrimPrefix(normalized, "publishers/google/models/")
  17. normalized = strings.TrimPrefix(normalized, "publishers/anthropic/models/")
  18. normalized = strings.TrimPrefix(normalized, "models/")
  19. }
  20. return normalized
  21. }
  22. // resolveAntigravityModelKey 根据请求的模型名解析限流 key
  23. // 返回空字符串表示无法解析
  24. func resolveAntigravityModelKey(requestedModel string) string {
  25. return normalizeAntigravityModelName(requestedModel)
  26. }
  27. // IsSchedulableForModel 结合模型级限流判断是否可调度。
  28. // 保持旧签名以兼容既有调用方;默认使用 context.Background()。
  29. func (a *Account) IsSchedulableForModel(requestedModel string) bool {
  30. return a.IsSchedulableForModelWithContext(context.Background(), requestedModel)
  31. }
  32. func (a *Account) IsSchedulableForModelWithContext(ctx context.Context, requestedModel string) bool {
  33. if a == nil {
  34. return false
  35. }
  36. if !a.IsSchedulable() {
  37. return false
  38. }
  39. if a.isModelRateLimitedWithContext(ctx, requestedModel) {
  40. // Antigravity + overages 启用 + 积分未耗尽 → 放行(有积分可用)
  41. if a.Platform == PlatformAntigravity && a.IsOveragesEnabled() && !a.isCreditsExhausted() {
  42. return true
  43. }
  44. return false
  45. }
  46. return true
  47. }
  48. // GetRateLimitRemainingTime 获取限流剩余时间(模型级限流)
  49. // 返回 0 表示未限流或已过期
  50. func (a *Account) GetRateLimitRemainingTime(requestedModel string) time.Duration {
  51. return a.GetRateLimitRemainingTimeWithContext(context.Background(), requestedModel)
  52. }
  53. // GetRateLimitRemainingTimeWithContext 获取限流剩余时间(模型级限流)
  54. // 返回 0 表示未限流或已过期
  55. func (a *Account) GetRateLimitRemainingTimeWithContext(ctx context.Context, requestedModel string) time.Duration {
  56. if a == nil {
  57. return 0
  58. }
  59. return a.GetModelRateLimitRemainingTimeWithContext(ctx, requestedModel)
  60. }