111 行
3.5 KiB
Python
111 行
3.5 KiB
Python
"""Cost and usage route handlers."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
from typing import Any
|
|
|
|
from backend.routes.common import handle_request
|
|
from backend.services.cost_guard_service import CostGuardService
|
|
from backend.services.cost_service import CostService
|
|
from core.identity import RequestContext
|
|
from core.security import require_authenticated
|
|
from database.db_helper import Database, load_database_settings
|
|
|
|
|
|
def get_cost_usage(
|
|
ctx: RequestContext,
|
|
query: dict[str, Any] | None = None,
|
|
service: CostService | None = None,
|
|
) -> dict:
|
|
params = query or {}
|
|
cost_service = service or CostService(Database(load_database_settings()))
|
|
|
|
def action() -> dict[str, Any]:
|
|
require_authenticated(ctx)
|
|
return cost_service.usage_summary(
|
|
profile_id=_optional_str(params.get("profile_id")),
|
|
workflow_id=_optional_str(params.get("workflow_id")),
|
|
node_id=_optional_str(params.get("node_id")),
|
|
provider_id=_optional_str(params.get("provider_id")),
|
|
days=_optional_int(params.get("days")),
|
|
unit_prices=_unit_prices(params.get("unit_prices")),
|
|
)
|
|
|
|
return handle_request(ctx, action)
|
|
|
|
|
|
def check_cost_guard(
|
|
ctx: RequestContext,
|
|
payload: dict[str, Any] | None = None,
|
|
service: CostGuardService | None = None,
|
|
) -> dict:
|
|
params = payload or {}
|
|
guard_service = service or CostGuardService(CostService(Database(load_database_settings())))
|
|
|
|
def action() -> dict[str, Any]:
|
|
require_authenticated(ctx)
|
|
return guard_service.check_generation_budget(
|
|
provider_id=_optional_str(params.get("provider_id")),
|
|
provider_model=_optional_str(params.get("provider_model")),
|
|
model_route=_optional_str(params.get("model_route")),
|
|
planned_task_count=_int_or_default(params.get("planned_task_count"), 1),
|
|
planned_image_count=_int_or_default(params.get("planned_image_count"), 1),
|
|
planned_total_tokens=_int_or_default(params.get("planned_total_tokens"), 0),
|
|
max_task_count=_optional_int(params.get("max_task_count")),
|
|
max_image_count=_optional_int(params.get("max_image_count")),
|
|
max_estimated_cost=_optional_float(params.get("max_estimated_cost")),
|
|
days=_optional_int(params.get("days")),
|
|
unit_prices=_unit_prices(params.get("unit_prices")),
|
|
)
|
|
|
|
return handle_request(ctx, action)
|
|
|
|
|
|
def _unit_prices(value: Any) -> dict[str, Any]:
|
|
if value is None:
|
|
return {}
|
|
if isinstance(value, dict):
|
|
return value
|
|
if isinstance(value, str):
|
|
text = value.strip()
|
|
if not text:
|
|
return {}
|
|
try:
|
|
parsed = json.loads(text)
|
|
except json.JSONDecodeError:
|
|
return {}
|
|
return parsed if isinstance(parsed, dict) else {}
|
|
return {}
|
|
|
|
|
|
def _optional_str(value: Any) -> str | None:
|
|
if value is None:
|
|
return None
|
|
text = str(value).strip()
|
|
return text or None
|
|
|
|
|
|
def _optional_int(value: Any) -> int | None:
|
|
if value is None or value == "":
|
|
return None
|
|
try:
|
|
return int(value)
|
|
except (TypeError, ValueError):
|
|
return None
|
|
|
|
|
|
def _optional_float(value: Any) -> float | None:
|
|
if value is None or value == "":
|
|
return None
|
|
try:
|
|
return float(value)
|
|
except (TypeError, ValueError):
|
|
return None
|
|
|
|
|
|
def _int_or_default(value: Any, default: int) -> int:
|
|
try:
|
|
return int(value)
|
|
except (TypeError, ValueError):
|
|
return default
|