mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-11-25 07:22:18 +00:00
Align gateway Grok media/voice paths and model lists, harden upstream failure and quota handling, clear non-Grok video generation config migration, and polish temp-unsched/status indicators with model whitelist updates.
87 lines
2.9 KiB
Go
87 lines
2.9 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"log/slog"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
// Spending-limit is recoverable at the end of the observed billing period.
|
|
// When no billing snapshot is available, use a short probe rather than
|
|
// fabricating a 24h boundary from the error arrival time.
|
|
const grokSpendingLimitCooldown = 24 * time.Hour
|
|
const grokSpendingLimitProbeCooldown = 10 * time.Minute
|
|
|
|
const grokSpendingLimitErrorMessage = "Grok spending limit reached; reauthorize or wait for billing reset"
|
|
|
|
func grokSpendingLimitResetAt(account *Account, now time.Time) time.Time {
|
|
if account != nil {
|
|
if billing, err := grokBillingSnapshotFromExtra(account.Extra); err == nil && billing != nil {
|
|
for _, raw := range []string{billing.PeriodEnd, billing.BillingPeriodEnd} {
|
|
if resetAt, err := time.Parse(time.RFC3339, strings.TrimSpace(raw)); err == nil && resetAt.After(now) {
|
|
return resetAt
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return now.Add(grokSpendingLimitProbeCooldown)
|
|
}
|
|
|
|
// markGrokSpendingLimitReauth applies a long temp-unsched cool and durable
|
|
// SetError so ops sees reauth-required without wiping OAuth credentials.
|
|
func (s *OpenAIGatewayService) markGrokSpendingLimitReauth(ctx context.Context, account *Account) {
|
|
if s == nil || account == nil || account.IsPoolMode() {
|
|
return
|
|
}
|
|
s.tempUnscheduleGrok(ctx, account, grokSpendingLimitCooldown, "grok spending limit")
|
|
if s.accountRepo == nil {
|
|
return
|
|
}
|
|
stateCtx, cancel := openAIAccountStateContext(ctx)
|
|
defer cancel()
|
|
if err := s.accountRepo.SetError(stateCtx, account.ID, grokSpendingLimitErrorMessage); err != nil {
|
|
slog.Warn("grok_spending_limit_set_error_failed", "account_id", account.ID, "error", err)
|
|
}
|
|
// Soft flag in extra for UI / usage fetcher without requiring status poll.
|
|
_ = s.accountRepo.UpdateExtra(stateCtx, account.ID, map[string]any{
|
|
"grok_needs_reauth": true,
|
|
"grok_needs_reauth_reason": "spending_limit",
|
|
"grok_needs_reauth_at": time.Now().UTC().Format(time.RFC3339),
|
|
})
|
|
}
|
|
|
|
// clearGrokNeedsReauthExtra drops the soft reauth flag after successful refresh
|
|
// or reauth. Best-effort; never fails the request path.
|
|
func clearGrokNeedsReauthExtra(ctx context.Context, repo AccountRepository, accountID int64) {
|
|
if repo == nil || accountID <= 0 {
|
|
return
|
|
}
|
|
stateCtx, cancel := openAIAccountStateContext(ctx)
|
|
defer cancel()
|
|
_ = repo.UpdateExtra(stateCtx, accountID, map[string]any{
|
|
"grok_needs_reauth": false,
|
|
"grok_needs_reauth_reason": "",
|
|
"grok_needs_reauth_at": "",
|
|
})
|
|
}
|
|
|
|
func accountGrokNeedsReauth(account *Account) bool {
|
|
if account == nil {
|
|
return false
|
|
}
|
|
if account.Status == StatusError {
|
|
msg := strings.ToLower(account.ErrorMessage)
|
|
if strings.Contains(msg, "spending limit") || strings.Contains(msg, "reauthorize") {
|
|
return true
|
|
}
|
|
}
|
|
if v, ok := account.Extra["grok_needs_reauth"].(bool); ok && v {
|
|
return true
|
|
}
|
|
if s, ok := account.Extra["grok_needs_reauth"].(string); ok {
|
|
return strings.EqualFold(s, "true") || s == "1"
|
|
}
|
|
return false
|
|
}
|