Fix preflight NameError, clean up debug logs, remove redundant voice button, fix Tooltip warning

This commit is contained in:
ajaysi
2026-04-22 16:10:27 +05:30
parent fbbfe81ed7
commit 8b79099b15
7 changed files with 51 additions and 111 deletions

View File

@@ -19,6 +19,15 @@ if TYPE_CHECKING:
from .pricing_service import PricingService
def _should_enforce_limit(limit_value: int, tier: str) -> bool:
"""
Determine if a limit should be enforced.
- Free tier: 0 means DISABLED (not unlimited)
- Basic/Pro/Enterprise: 0 means UNLIMITED
"""
return limit_value > 0
class LimitValidator:
"""Validates subscription limits for API usage."""
@@ -107,20 +116,6 @@ class LimitValidator:
}
return result
# Helper: Check if a limit should be enforced based on tier
def should_enforce_limit(limit_value: int, tier: str) -> bool:
"""
Determine if a limit should be enforced.
- Free tier: 0 means DISABLED (not unlimited)
- Basic/Pro/Enterprise: 0 means UNLIMITED
"""
if tier == 'free':
# Free tier: 0 means disabled
return limit_value > 0
else:
# Basic/Pro/Enterprise: 0 means unlimited
return limit_value > 0
# Get user limits with error handling (STRICT: fail on errors)
# CRITICAL: Expire SQLAlchemy objects to ensure we get fresh plan data after renewal
try:
@@ -263,7 +258,7 @@ class LimitValidator:
)
# Enforce limit based on tier (Free: 0=disabled, others: 0=unlimited)
if should_enforce_limit(ai_text_gen_limit, user_tier) and current_total_llm_calls >= ai_text_gen_limit:
if _should_enforce_limit(ai_text_gen_limit, user_tier) and current_total_llm_calls >= ai_text_gen_limit:
logger.error(f"[Subscription Check] AI text generation call limit exceeded for user {user_id}: {current_total_llm_calls}/{ai_text_gen_limit} (provider: {display_provider_name})")
result = (False, f"AI text generation call limit reached. Used {current_total_llm_calls} of {ai_text_gen_limit} total AI text generation calls this billing period.", {
'current_calls': current_total_llm_calls,
@@ -296,7 +291,7 @@ class LimitValidator:
call_limit = limits['limits'].get(f"{provider_name}_calls", 0) or 0
# Enforce limit based on tier (Free: 0=disabled, others: 0=unlimited)
if should_enforce_limit(call_limit, user_tier) and current_calls >= call_limit:
if _should_enforce_limit(call_limit, user_tier) and current_calls >= call_limit:
logger.error(f"[Subscription Check] Call limit exceeded for user {user_id}, provider {display_provider_name}: {current_calls}/{call_limit}")
result = (False, f"API call limit reached for {display_provider_name}. Used {current_calls} of {call_limit} calls this billing period.", {
'current_calls': current_calls,
@@ -329,7 +324,7 @@ class LimitValidator:
token_limit = limits['limits'].get(f"{provider_name}_tokens", 0) or 0
# Enforce limit based on tier (Free: 0=disabled, others: 0=unlimited)
if should_enforce_limit(token_limit, user_tier) and (current_tokens + tokens_requested) > token_limit:
if _should_enforce_limit(token_limit, user_tier) and (current_tokens + tokens_requested) > token_limit:
result = (False, f"Token limit would be exceeded for {display_provider_name}. Current: {current_tokens}, Requested: {tokens_requested}, Limit: {token_limit}", {
'current_tokens': current_tokens,
'requested_tokens': tokens_requested,
@@ -363,7 +358,7 @@ class LimitValidator:
try:
cost_limit = limits['limits'].get('monthly_cost', 0) or 0
# Enforce limit based on tier (Free: 0=disabled, others: 0=unlimited)
if should_enforce_limit(cost_limit, user_tier) and usage.total_cost >= cost_limit:
if _should_enforce_limit(cost_limit, user_tier) and usage.total_cost >= cost_limit:
result = (False, f"Monthly cost limit reached. Current cost: ${usage.total_cost:.2f}, Limit: ${cost_limit:.2f}", {
'current_cost': usage.total_cost,
'limit': cost_limit,
@@ -583,7 +578,7 @@ class LimitValidator:
projected_total_llm_calls = total_llm_calls + 1
# Enforce limit based on tier (Free: 0=disabled, others: 0=unlimited)
if should_enforce_limit(ai_text_gen_limit, tier) and projected_total_llm_calls > ai_text_gen_limit:
if _should_enforce_limit(ai_text_gen_limit, tier) and projected_total_llm_calls > ai_text_gen_limit:
error_info = {
'current_calls': total_llm_calls,
'limit': ai_text_gen_limit,
@@ -691,7 +686,7 @@ class LimitValidator:
token_limit = limits.get(provider_tokens_key, 0) or 0
# Enforce limit based on tier (Free: 0=disabled, others: 0=unlimited)
if should_enforce_limit(token_limit, tier) and tokens_requested > 0:
if _should_enforce_limit(token_limit, tier) and tokens_requested > 0:
projected_tokens = current_provider_tokens + tokens_requested
logger.info(f" └─ Token Check: {current_provider_tokens} (current) + {tokens_requested} (requested) = {projected_tokens} (total) / {token_limit} (limit)")
@@ -754,7 +749,7 @@ class LimitValidator:
projected_images = total_images + 1
# Enforce limit based on tier (Free: 0=disabled, others: 0=unlimited)
if should_enforce_limit(image_limit, tier) and projected_images > image_limit:
if _should_enforce_limit(image_limit, tier) and projected_images > image_limit:
error_info = {
'current_images': total_images,
'limit': image_limit,
@@ -776,7 +771,7 @@ class LimitValidator:
projected_video_calls = total_video_calls + 1
# Enforce limit based on tier (Free: 0=disabled, others: 0=unlimited)
if should_enforce_limit(video_limit, tier) and projected_video_calls > video_limit:
if _should_enforce_limit(video_limit, tier) and projected_video_calls > video_limit:
error_info = {
'current_calls': total_video_calls,
'limit': video_limit,
@@ -796,7 +791,7 @@ class LimitValidator:
projected_image_edit_calls = total_image_edit_calls + 1
# Enforce limit based on tier (Free: 0=disabled, others: 0=unlimited)
if should_enforce_limit(image_edit_limit, tier) and projected_image_edit_calls > image_edit_limit:
if _should_enforce_limit(image_edit_limit, tier) and projected_image_edit_calls > image_edit_limit:
error_info = {
'current_calls': total_image_edit_calls,
'limit': image_edit_limit,
@@ -833,7 +828,7 @@ class LimitValidator:
# Check WaveSpeed combined limit if actual_provider is WaveSpeed
if actual_provider_name == 'wavespeed':
wavespeed_limit = limits.get('wavespeed_calls', 0) or 0
if should_enforce_limit(wavespeed_limit, tier):
if _should_enforce_limit(wavespeed_limit, tier):
wavespeed_usage = usage.wavespeed_calls or 0
projected_wavespeed = wavespeed_usage + 1
if projected_wavespeed > wavespeed_limit: