Merge pull request #16 from xpack-ai/feature/0.3-backend

tmp commit
This commit is contained in:
Dot.L authored and GitHub committed 2025-07-30 13:37:50 +08:00
commit 3012d27aae
4 files changed
+53 -13

No files matched your search

@@ -23,6 +23,7 @@ from services.admin_service.constants.sys_config_key import (
KEY_FAQ,
KEY_EMBEDED_HTML,
KEY_TOP_NAVIGATION,
KEY_MCP_SERVER_PREFIX
)
from services.admin_service.services.payment_channel_service import PaymentChannelService
@@ -61,6 +62,7 @@ def get_config(
# Convert to boolean
email_is_enabled = email_is_enabled_raw.lower() in ("true", "t", "yes", "y", "1")
email_mode = sys_config_service.get_value_by_key(KEY_LOGIN_EMAIL_MODE) or "password"
mcp_server_prefix = sys_config_service.get_value_by_key(KEY_MCP_SERVER_PREFIX) or ""
# Get homepage config
faq = sys_config_service.get_value_by_key(KEY_FAQ) or "[]"
@@ -93,6 +95,7 @@ def get_config(
"language": language,
"theme": theme,
"about_page": about_page,
"mcp_server_prefix": mcp_server_prefix,
},
"faq": faq,
"embeded_html": embeded_html,
@@ -50,14 +50,26 @@ class BillingService:
"""
try:
# Get service price information
service_price, charge_type = await self._get_service_price(service_id)
price, input_token_price, output_token_price, charge_type = await self._get_service_price(service_id)
service_price = price
match charge_type:
case ChargeType.FREE:
logger.info(f"Free service, skip billing check - User ID: {user_id}, Service ID: {service_id}")
return PreDeductResult(
success=True,
message="Free service",
service_price=Decimal("0"),
user_balance=Decimal("0"),
input_token_price=input_token_price,
output_token_price=output_token_price,
charge_type=charge_type.value
)
case ChargeType.PER_TOKEN:
# 计算预估费用(按每百万Token计费)
estimated_input_cost = (Decimal(100) / Decimal("1000000")) * input_token_price
estimated_output_cost = (Decimal(500) / Decimal("1000000")) * output_token_price
service_price = estimated_input_cost + estimated_output_cost
# Free service passes directly
if charge_type == ChargeType.FREE:
logger.info(f"Free service, skip billing check - User ID: {user_id}, Service ID: {service_id}")
return PreDeductResult(success=True, message="Free service", service_price=Decimal("0"), user_balance=Decimal("0"))
# Acquire distributed lock for pre-deduction
async with self._acquire_billing_lock(user_id):
# Get user balance
user_balance = await self._get_user_wallet_balance(user_id)
@@ -70,6 +82,9 @@ class BillingService:
message=f"Insufficient balance, current balance: {user_balance}, required: {service_price}",
service_price=service_price,
user_balance=user_balance,
input_token_price=input_token_price,
output_token_price=output_token_price,
charge_type=charge_type.value
)
# Pre-deduct amount in Redis
@@ -77,7 +92,15 @@ class BillingService:
await self._update_wallet_cache(user_id, new_balance)
logger.info(f"Pre-deduction successful - User ID: {user_id}, Deduction: {service_price}, Balance: {user_balance} -> {new_balance}")
return PreDeductResult(success=True, message="Pre-deduction successful", service_price=service_price, user_balance=new_balance)
return PreDeductResult(
success=True,
message="Pre-deduction successful",
service_price=service_price,
user_balance=new_balance,
input_token_price=input_token_price,
output_token_price=output_token_price,
charge_type=charge_type.value
)
except Exception as e:
logger.error(f"Pre-deduction check failed - User ID: {user_id}, Service ID: {service_id}: {str(e)}", exc_info=True)
@@ -130,7 +153,7 @@ class BillingService:
logger.error(f"Failed to send billing message: {str(e)}", exc_info=True)
# Consider saving failed messages to local queue for retry
async def _get_service_price(self, service_id: str) -> Tuple[Decimal, ChargeType]:
async def _get_service_price(self, service_id: str) -> Tuple[Decimal, Decimal, Decimal, ChargeType]:
"""
Get service price and charge type
@@ -147,7 +170,7 @@ class BillingService:
if cached_data:
try:
data = json.loads(cached_data)
return Decimal(data["price"]), ChargeType(data["charge_type"])
return Decimal(data["price"]), Decimal(data["input_token_price"]), Decimal(data["output_token_price"]), ChargeType(data["charge_type"])
except (json.JSONDecodeError, KeyError, ValueError):
logger.warning(f"Service price cache data anomaly, re-fetch from database - Service ID: {service_id}")
@@ -161,13 +184,20 @@ class BillingService:
raise ValueError(f"Service not found: {service_id}")
price = Decimal(str(service.price))
input_token_price = Decimal(str(service.input_token_price))
output_token_price = Decimal(str(service.output_token_price))
charge_type = service.charge_type
# Cache to Redis
cache_data = {"price": str(price), "charge_type": charge_type.value}
cache_data = {
"price": str(price),
"input_token_price": str(input_token_price),
"output_token_price":str(output_token_price),
"charge_type": charge_type.value
}
self.redis.set(cache_key, json.dumps(cache_data), ex=self.SERVICE_CACHE_EXPIRE)
return price, charge_type
return price,input_token_price,output_token_price, charge_type
finally:
db.close()
@@ -186,7 +186,9 @@ class McpServerFactory:
api_id=call_log_id,
tool_name=name,
input_params=json.dumps(arguments),
unit_price=pre_deduct_result.service_price,
# unit_price=pre_deduct_result.service_price,
input_token_amount=0,
output_token_amount=0,
call_start_time=call_start_time,
call_end_time=call_end_time,
apikey_id=apikey_id,
+5
View File
@@ -29,6 +29,8 @@ class ApiCallLogInfo:
tool_name: str
input_params: str
unit_price: Decimal
input_token_amount: Decimal
output_token_amount: Decimal
call_start_time: datetime
call_end_time: Optional[datetime] = None
apikey_id: Optional[str] = None
@@ -40,3 +42,6 @@ class PreDeductResult:
message: str
service_price: Decimal
user_balance: Decimal
input_token_price: Decimal
output_token_price: Decimal
charge_type: str