2026-07-06 02:34:43 +08:00
|
|
|
"""Cost and usage route handlers."""
|
|
|
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
import json
|
|
|
|
|
from typing import Any
|
|
|
|
|
|
|
|
|
|
from backend.routes.common import handle_request
|
2026-07-11 21:50:34 +08:00
|
|
|
from backend.routes.params import int_or_default, optional_float, optional_int, optional_str
|
2026-07-06 02:40:18 +08:00
|
|
|
from backend.services.cost_guard_service import CostGuardService
|
2026-07-06 02:34:43 +08:00
|
|
|
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(
|
2026-07-11 21:50:34 +08:00
|
|
|
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")),
|
2026-07-06 02:34:43 +08:00
|
|
|
unit_prices=_unit_prices(params.get("unit_prices")),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
return handle_request(ctx, action)
|
|
|
|
|
|
|
|
|
|
|
2026-07-06 02:40:18 +08:00
|
|
|
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(
|
2026-07-11 21:50:34 +08:00
|
|
|
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")),
|
2026-07-06 02:40:18 +08:00
|
|
|
unit_prices=_unit_prices(params.get("unit_prices")),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
return handle_request(ctx, action)
|
|
|
|
|
|
|
|
|
|
|
2026-07-06 02:34:43 +08:00
|
|
|
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 {}
|