File size: 7,366 Bytes
ddebc3e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
"""Default quota windows and pool inference logic.

Canonical quota totals per pool type (from upstream rate-limits API):

              auto    fast    expert    heavy    grok_4_3
  basic          —      30       —        —         —        window: 86400 s
  super         50     140      50        —        50        window: 7200 s
  heavy        150     400     150       20       150        window: 7200 s

Pool inference uses ``auto.total`` as the primary signal for super/heavy
accounts; basic accounts no longer expose auto/expert windows locally.
"""

from typing import TYPE_CHECKING

from .enums import QuotaSource
from .models import AccountQuotaSet, QuotaWindow

if TYPE_CHECKING:
    pass


# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------


def _w(remaining: int, total: int, window_seconds: int) -> QuotaWindow:
    return QuotaWindow(
        remaining=remaining,
        total=total,
        window_seconds=window_seconds,
        reset_at=None,
        synced_at=None,
        source=QuotaSource.DEFAULT,
    )


# ---------------------------------------------------------------------------
# Per-pool default quota sets
# ---------------------------------------------------------------------------

BASIC_FAST_LIMIT = 30
BASIC_FAST_WINDOW_SECONDS = 86_400

BASIC_CONSOLE_LIMIT = 20
BASIC_CONSOLE_WINDOW_SECONDS = 3600

BASIC_QUOTA_DEFAULTS = AccountQuotaSet(
    auto=_w(0, 0, 0),  # unsupported on basic accounts
    fast=_w(BASIC_FAST_LIMIT, BASIC_FAST_LIMIT, BASIC_FAST_WINDOW_SECONDS),
    expert=_w(0, 0, 0),  # unsupported on basic accounts
    console=_w(BASIC_CONSOLE_LIMIT, BASIC_CONSOLE_LIMIT, BASIC_CONSOLE_WINDOW_SECONDS),
)

SUPER_QUOTA_DEFAULTS = AccountQuotaSet(
    auto=_w(50, 50, 7_200),  # 50  queries / 2 h
    fast=_w(140, 140, 7_200),  # 140 queries / 2 h
    expert=_w(50, 50, 7_200),  # 50  queries / 2 h
    grok_4_3=_w(50, 50, 7_200),  # 50  queries / 2 h
)

HEAVY_QUOTA_DEFAULTS = AccountQuotaSet(
    auto=_w(150, 150, 7_200),  # 150 queries / 2 h
    fast=_w(400, 400, 7_200),  # 400 queries / 2 h
    expert=_w(150, 150, 7_200),  # 150 queries / 2 h
    heavy=_w(20, 20, 7_200),  # 20  queries / 2 h
    grok_4_3=_w(150, 150, 7_200),  # 150 queries / 2 h
)

# Map pool name → defaults object (used by backends on upsert).
_POOL_DEFAULTS: dict[str, AccountQuotaSet] = {
    "basic": BASIC_QUOTA_DEFAULTS,
    "super": SUPER_QUOTA_DEFAULTS,
    "heavy": HEAVY_QUOTA_DEFAULTS,
}

_SUPPORTED_MODE_IDS_BY_POOL: dict[str, frozenset[int]] = {
    "basic": frozenset((1, 5)),
    "super": frozenset((0, 1, 2, 4, 5)),
    "heavy": frozenset((0, 1, 2, 3, 4, 5)),
}

# ---------------------------------------------------------------------------
# Pool inference — keyed on auto.total (unique across pool types)
# ---------------------------------------------------------------------------

_AUTO_TOTAL_TO_POOL: dict[int, str] = {
    20: "basic",
    50: "super",
    150: "heavy",
}


def default_quota_set(pool: str) -> AccountQuotaSet:
    """Return a fresh copy of the default quota set for *pool*."""
    src = _POOL_DEFAULTS.get(pool, BASIC_QUOTA_DEFAULTS)
    qs = AccountQuotaSet(
        auto=_w(src.auto.remaining, src.auto.total, src.auto.window_seconds),
        fast=_w(src.fast.remaining, src.fast.total, src.fast.window_seconds),
        expert=_w(src.expert.remaining, src.expert.total, src.expert.window_seconds),
    )
    if src.heavy is not None:
        qs.heavy = _w(src.heavy.remaining, src.heavy.total, src.heavy.window_seconds)
    if src.grok_4_3 is not None:
        qs.grok_4_3 = _w(
            src.grok_4_3.remaining, src.grok_4_3.total, src.grok_4_3.window_seconds
        )
    if src.console is not None:
        qs.console = _w(
            src.console.remaining, src.console.total, src.console.window_seconds
        )
    return qs


def supports_mode(pool: str, mode_id: int) -> bool:
    """Return whether *pool* has a default quota window for *mode_id*."""
    return mode_id in _SUPPORTED_MODE_IDS_BY_POOL.get(
        pool, _SUPPORTED_MODE_IDS_BY_POOL["basic"]
    )


def supported_mode_ids(pool: str) -> tuple[int, ...]:
    """Return the supported mode IDs for *pool* in stable request order."""
    supported = _SUPPORTED_MODE_IDS_BY_POOL.get(
        pool, _SUPPORTED_MODE_IDS_BY_POOL["basic"]
    )
    return tuple(mode_id for mode_id in (0, 1, 2, 3, 4, 5) if mode_id in supported)


def default_quota_window(pool: str, mode_id: int) -> QuotaWindow | None:
    """Return the default quota window for *(pool, mode_id)*, if supported."""
    if not supports_mode(pool, mode_id):
        return None
    return default_quota_set(pool).get(mode_id)


def normalize_quota_window(
    pool: str, mode_id: int, window: QuotaWindow | None
) -> QuotaWindow | None:
    """Apply product-level quota policy for one pool/mode window."""
    if window is None or not supports_mode(pool, mode_id):
        return None
    if pool == "basic" and mode_id == 1:
        return QuotaWindow(
            remaining=max(0, min(int(window.remaining), BASIC_FAST_LIMIT)),
            total=BASIC_FAST_LIMIT,
            window_seconds=BASIC_FAST_WINDOW_SECONDS,
            reset_at=window.reset_at,
            synced_at=window.synced_at,
            source=window.source,
        )
    if pool == "basic" and mode_id == 5:
        # L1 修复:矫正历史 console 配额数据到当前常量值
        # 历史值如 200/24h、70/5min、100/1min、30/15min、30/30min 等均归一到 20/60min
        return QuotaWindow(
            remaining=max(0, min(int(window.remaining), BASIC_CONSOLE_LIMIT)),
            total=BASIC_CONSOLE_LIMIT,
            window_seconds=BASIC_CONSOLE_WINDOW_SECONDS,
            reset_at=window.reset_at,
            synced_at=window.synced_at,
            source=window.source,
        )
    return window


def normalize_quota_set(pool: str, quota_set: AccountQuotaSet) -> AccountQuotaSet:
    """Return a quota set normalized to the supported modes for *pool*."""
    defaults = default_quota_set(pool)

    auto = normalize_quota_window(pool, 0, quota_set.auto) or defaults.auto
    fast = normalize_quota_window(pool, 1, quota_set.fast) or defaults.fast
    expert = normalize_quota_window(pool, 2, quota_set.expert) or defaults.expert

    qs = AccountQuotaSet(auto=auto, fast=fast, expert=expert)
    qs.heavy = normalize_quota_window(pool, 3, quota_set.heavy)
    qs.grok_4_3 = normalize_quota_window(pool, 4, quota_set.grok_4_3)
    qs.console = normalize_quota_window(pool, 5, quota_set.console) or defaults.console
    return qs


def infer_pool(windows: dict[int, QuotaWindow]) -> str:
    """Infer pool type from live quota windows returned by the rate-limits API.

    Uses ``auto.total`` (mode_id=0) as the discriminating signal.
    Falls back to ``"basic"`` when the value is absent or unrecognised.
    """
    auto_win = windows.get(0)
    if auto_win is None:
        return "basic"
    return _AUTO_TOTAL_TO_POOL.get(auto_win.total, "basic")


__all__ = [
    "BASIC_QUOTA_DEFAULTS",
    "SUPER_QUOTA_DEFAULTS",
    "HEAVY_QUOTA_DEFAULTS",
    "default_quota_set",
    "default_quota_window",
    "infer_pool",
    "normalize_quota_set",
    "normalize_quota_window",
    "supported_mode_ids",
    "supports_mode",
]