feat: show error if quota is exceeded

This commit is contained in:
Abhishek Kumar 2025-11-26 17:40:11 +07:00
parent 3d710cafb1
commit b804daff77
5 changed files with 219 additions and 27 deletions

View file

@ -20,6 +20,8 @@ from loguru import logger
from api.db import db_client
from api.db.models import UserModel
from api.services.auth.depends import get_user_ws
from api.services.configuration.registry import ServiceProviders
from api.services.mps_service_key_client import mps_service_key_client
from api.services.pipecat.run_pipeline import run_pipeline_smallwebrtc
from pipecat.transports.smallwebrtc.connection import SmallWebRTCConnection
from pipecat.utils.context import set_current_run_id
@ -37,6 +39,75 @@ class SignalingManager:
self._connections: Dict[str, WebSocket] = {}
self._peer_connections: Dict[str, SmallWebRTCConnection] = {}
async def _check_dograh_quota(self, user: UserModel) -> tuple[bool, str]:
"""Check if user has sufficient Dograh quota for making a call.
Args:
user_id: The user ID to check quota for
Returns:
Tuple of (has_quota, error_message)
- has_quota: True if user has sufficient quota or not using Dograh
- error_message: Error message if quota check fails, empty string otherwise
"""
try:
# Get user configurations
user_config = await db_client.get_user_configurations(user.id)
# Check if user is using any Dograh service
using_dograh = False
dograh_api_keys = set()
if user_config.llm and user_config.llm.provider == ServiceProviders.DOGRAH:
using_dograh = True
dograh_api_keys.add(user_config.llm.api_key)
if user_config.stt and user_config.stt.provider == ServiceProviders.DOGRAH:
using_dograh = True
dograh_api_keys.add(user_config.stt.api_key)
if user_config.tts and user_config.tts.provider == ServiceProviders.DOGRAH:
using_dograh = True
dograh_api_keys.add(user_config.tts.api_key)
# If not using Dograh, quota check passes
if not using_dograh:
return True, ""
# Check quota for ALL Dograh keys
for api_key in dograh_api_keys:
try:
usage = await mps_service_key_client.check_service_key_usage(
api_key, created_by=user.provider_id
)
remaining = usage.get("remaining_credits", 0.0)
# Require at least $0.10 for a short call
if remaining < 0.10:
logger.warning(
f"Insufficient Dograh credits for key ...{api_key[-8:]}: "
f"${remaining:.2f} remaining"
)
return False, (
"You have exhausted your trial credits."
"Please email founders@dograh.com for additional credits."
)
logger.info(
f"Dograh quota check passed for key ...{api_key[-8:]}: "
f"${remaining:.2f} remaining"
)
except Exception as e:
logger.error(f"Failed to check quota for Dograh key: {str(e)}")
return False, "Could not verify Dograh credits. Please try again."
return True, ""
except Exception as e:
logger.error(f"Error during quota check: {str(e)}")
# On unexpected error, allow the call to proceed
return True, ""
async def handle_websocket(
self,
websocket: WebSocket,
@ -110,6 +181,21 @@ class SignalingManager:
# Set run context for logging
set_current_run_id(workflow_run_id)
# Check Dograh quota before initiating the call
has_quota, error_message = await self._check_dograh_quota(user)
if not has_quota:
# Send error response for quota issues
await ws.send_json(
{
"type": "error",
"payload": {
"error_type": "quota_exceeded",
"message": error_message,
},
}
)
return
if pc_id and pc_id in self._peer_connections:
# Reuse existing connection
logger.info(f"Reusing existing connection for pc_id: {pc_id}")