ba6e7669e8
Features: - FastAPI + SQLAlchemy 2.0 async + PostgreSQL/pgvector + Redis backend - Vue 3 + TypeScript + Element Plus frontend - JWT auth with access/refresh tokens and revocation - Admin/member RBAC - RSS feed CRUD and article listing - Settings management with Fernet encryption for sensitive values - Redis distributed lock service - Alembic initial migration - Docker Compose development environment Fixes from code review: - Fix DB session leak in dependency injection - Restrict registration to admin only - Add default admin password warning - Implement JWT refresh tokens and jti blacklist - Strengthen password policy - Use func.count for pagination totals - Replace NullPool with AsyncAdaptedQueuePool - Remove init_db from lifespan to enforce alembic migrations - Add request_id middleware and logging filter - Fix vite.config.ts env loading - Add frontend token refresh interceptor - Add Vue error handler Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
43 lines
1.7 KiB
Python
43 lines
1.7 KiB
Python
"""Chat models."""
|
|
from sqlalchemy import ForeignKey, JSON, String, Text
|
|
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
|
|
|
from app.models.base import Base, TimestampMixin, UUIDMixin
|
|
|
|
|
|
class ChatSession(Base, UUIDMixin, TimestampMixin):
|
|
"""Chat session."""
|
|
|
|
__tablename__ = "chat_sessions"
|
|
|
|
user_id: Mapped[str | None] = mapped_column(
|
|
ForeignKey("users.id", ondelete="CASCADE"), nullable=True, index=True
|
|
)
|
|
title: Mapped[str | None] = mapped_column(String(256), default="")
|
|
skill_id: Mapped[str | None] = mapped_column(
|
|
ForeignKey("skills.id", ondelete="SET NULL"), nullable=True
|
|
)
|
|
context_window: Mapped[int] = mapped_column(default=10, nullable=False)
|
|
|
|
messages: Mapped[list["ChatMessage"]] = relationship(
|
|
"ChatMessage", back_populates="session", cascade="all, delete-orphan"
|
|
)
|
|
|
|
|
|
class ChatMessage(Base, UUIDMixin, TimestampMixin):
|
|
"""Chat message."""
|
|
|
|
__tablename__ = "chat_messages"
|
|
|
|
session_id: Mapped[str] = mapped_column(
|
|
ForeignKey("chat_sessions.id", ondelete="CASCADE"), nullable=False, index=True
|
|
)
|
|
role: Mapped[str] = mapped_column(String(32), nullable=False, index=True) # user / assistant / tool
|
|
content: Mapped[str | None] = mapped_column(Text, default="")
|
|
tool_calls: Mapped[list] = mapped_column(JSON, default=list, nullable=False)
|
|
tool_results: Mapped[list] = mapped_column(JSON, default=list, nullable=False)
|
|
references: Mapped[list] = mapped_column(JSON, default=list, nullable=False)
|
|
token_usage: Mapped[dict | None] = mapped_column(JSON, nullable=True)
|
|
|
|
session: Mapped["ChatSession"] = relationship("ChatSession", back_populates="messages")
|