Elevate MiroFish/CrowdSight from single-container dev to a SaaS foundation: - Local memory backend (Zep-compatible): memory services/models, local graph builder + updater, AgentActivity seam, import-boundary isolation; Zep stays default, local is opt-in behind MEMORY_BACKEND. Semantic parity not yet proven. - Durable product persistence: projects/simulations/reports schema (migration 0007) + tenant/owner-scoped ProductRepository + dual-write + scoped_project read-first + ArtifactStore abstraction; durable JobQueue + worker.py. - SaaS hardening: durable RateLimiter (wired to login), UsageService (LLM accounting), redacted AuditService, idempotency, CORS allowlist, safe API errors, single-use PasswordResetService + endpoints (covers invite-pending). - Exactly 3 roles (super_admin/admin/user) with tenant authz policy. - Admin UI: GET/POST/PATCH /api/admin/users + GET/PUT /api/admin/settings (super-admin only, encrypted/masked); AdminView.vue + SettingsView.vue with admin/super-admin route guards, th/en i18n. - Production deploy topology: multi-stage Dockerfile (frontend build + gunicorn wsgi + nginx SPA-proxy + supervisord worker), backend/wsgi.py, gunicorn dep. Backend 197 passed; frontend 10 tests + build green. ruff unavailable (gap). No commit of credentials; secrets handled via env/.env.example. Deferred: Zep semantic A/B parity, object storage cutover, mobile QA, EasyPanel container build of deploy topology.
46 lines
1.7 KiB
Python
46 lines
1.7 KiB
Python
"""Durable LLM usage/cost events."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from datetime import datetime, timezone
|
|
from uuid import uuid4
|
|
|
|
from sqlalchemy import DateTime, Float, ForeignKey, Index, Integer, String, func, text
|
|
from sqlalchemy.orm import Mapped, mapped_column
|
|
|
|
from ..db import Base
|
|
|
|
|
|
def _utc_now() -> datetime:
|
|
return datetime.now(timezone.utc)
|
|
|
|
|
|
class UsageEvent(Base):
|
|
"""One LLM usage record. Never stores prompt content or secrets."""
|
|
|
|
__tablename__ = "usage_events"
|
|
__table_args__ = (
|
|
Index("ix_usage_org_created", "organization_id", "created_at"),
|
|
Index("ix_usage_org_user", "organization_id", "user_id"),
|
|
)
|
|
|
|
id: Mapped[str] = mapped_column(
|
|
String(64), primary_key=True, default=lambda: f"usage_{uuid4().hex}"
|
|
)
|
|
organization_id: Mapped[str] = mapped_column(
|
|
ForeignKey("organizations.id", ondelete="CASCADE"), nullable=False, index=True
|
|
)
|
|
user_id: Mapped[str | None] = mapped_column(
|
|
ForeignKey("users.id", ondelete="SET NULL"), nullable=True, index=True
|
|
)
|
|
operation: Mapped[str] = mapped_column(String(160), nullable=False)
|
|
model: Mapped[str | None] = mapped_column(String(120), nullable=True)
|
|
input_tokens: Mapped[int] = mapped_column(Integer, nullable=False, default=0, server_default=text("0"))
|
|
output_tokens: Mapped[int] = mapped_column(Integer, nullable=False, default=0, server_default=text("0"))
|
|
estimated_cost: Mapped[float] = mapped_column(
|
|
Float, nullable=False, default=0.0, server_default=text("0")
|
|
)
|
|
created_at: Mapped[datetime] = mapped_column(
|
|
DateTime(timezone=True), nullable=False, default=_utc_now, server_default=func.now()
|
|
)
|