Spaces:
Running
Running
Download core/gateway/models.py from Aynkhn/WorkPilot-AI: direct link, hf CLI and curl.
- Browser
- Download file 3.88 kB
-
https://huggingface.co/spaces/Aynkhn/WorkPilot-AI/resolve/main/core/gateway/models.py
- Command line
-
hf download hf://spaces/Aynkhn/WorkPilot-AI/core/gateway/models.py
-
curl -L -o models.py https://huggingface.co/spaces/Aynkhn/WorkPilot-AI/resolve/main/core/gateway/models.py
3.88 kB
| """Tables owned by the gateway: kill switches, quotas, idempotency keys, audit log.""" | |
| from __future__ import annotations | |
| import uuid | |
| from datetime import datetime | |
| from sqlalchemy import BigInteger, Boolean, DateTime, Float, Integer, String, Text, func | |
| from sqlalchemy.dialects.postgresql import JSONB, UUID | |
| from sqlalchemy.orm import Mapped, mapped_column | |
| from core.db.base import Base, TenantMixin, TimestampMixin, new_id | |
| class KillSwitch(TimestampMixin, Base): | |
| """scope: 'global' (target '*'), 'tenant' (target tenant id), 'tool' (target tool name, | |
| optionally per tenant). Global rows have tenant_id NULL and are managed by the owner role.""" | |
| __tablename__ = "kill_switches" | |
| __rls__ = True # custom policy: global rows readable by all | |
| id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), primary_key=True, default=new_id) | |
| tenant_id: Mapped[uuid.UUID | None] = mapped_column(UUID(as_uuid=True), nullable=True) | |
| scope: Mapped[str] = mapped_column(String(10), nullable=False) | |
| target: Mapped[str] = mapped_column(String(200), nullable=False) | |
| enabled: Mapped[bool] = mapped_column(Boolean, default=True, nullable=False) | |
| reason: Mapped[str] = mapped_column(Text, default="", nullable=False) | |
| class QuotaCounter(TenantMixin, Base): | |
| __tablename__ = "quota_counters" | |
| key: Mapped[str] = mapped_column(String(300), primary_key=True) | |
| window_start: Mapped[datetime] = mapped_column(DateTime(timezone=True), primary_key=True) | |
| count: Mapped[int] = mapped_column(Integer, default=0, nullable=False) | |
| class IdempotencyKey(TenantMixin, TimestampMixin, Base): | |
| __tablename__ = "idempotency_keys" | |
| key: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), primary_key=True) | |
| call_hash: Mapped[str] = mapped_column(String(64), nullable=False) | |
| status: Mapped[str] = mapped_column(String(20), nullable=False) # in_progress | completed | |
| result: Mapped[dict | None] = mapped_column(JSONB, nullable=True) | |
| class AuditLog(TenantMixin, Base): | |
| """Append-only, hash-chained per tenant: row_hash = sha256(prev_hash + canonical(row)).""" | |
| __tablename__ = "audit_log" | |
| seq: Mapped[int] = mapped_column(BigInteger, primary_key=True, autoincrement=True) | |
| id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), default=new_id, unique=True) | |
| at: Mapped[datetime] = mapped_column( | |
| DateTime(timezone=True), server_default=func.now(), nullable=False | |
| ) | |
| run_id: Mapped[uuid.UUID | None] = mapped_column(UUID(as_uuid=True), nullable=True) | |
| actor: Mapped[str] = mapped_column(String(200), nullable=False) | |
| model: Mapped[str | None] = mapped_column(String(100), nullable=True) | |
| prompt_hash: Mapped[str | None] = mapped_column(String(64), nullable=True) | |
| tool: Mapped[str] = mapped_column(String(200), nullable=False) | |
| args_redacted: Mapped[dict] = mapped_column(JSONB, nullable=False) | |
| args_hash: Mapped[str] = mapped_column(String(64), nullable=False) | |
| taint: Mapped[dict] = mapped_column(JSONB, nullable=False) | |
| decision: Mapped[str] = mapped_column(String(30), nullable=False) | |
| reason: Mapped[str | None] = mapped_column(Text, nullable=True) | |
| policy_version: Mapped[str] = mapped_column(String(64), nullable=False) | |
| approval_id: Mapped[uuid.UUID | None] = mapped_column(UUID(as_uuid=True), nullable=True) | |
| idempotency_key: Mapped[uuid.UUID | None] = mapped_column(UUID(as_uuid=True), nullable=True) | |
| result_status: Mapped[str | None] = mapped_column(String(30), nullable=True) | |
| latency_ms: Mapped[int] = mapped_column(Integer, default=0, nullable=False) | |
| cost_usd: Mapped[float] = mapped_column(Float, default=0.0, nullable=False) | |
| prev_hash: Mapped[str] = mapped_column(String(64), nullable=False) | |
| row_hash: Mapped[str] = mapped_column(String(64), nullable=False) | |