75 lines
2.7 KiB
Python
75 lines
2.7 KiB
Python
"""
|
|
Memory layer entities for AI Service.
|
|
[AC-AISVC-13] SQLModel entities for chat sessions and messages with tenant isolation.
|
|
"""
|
|
|
|
import uuid
|
|
from datetime import datetime
|
|
from typing import Any
|
|
|
|
from sqlalchemy import Column, JSON
|
|
from sqlmodel import Field, Index, SQLModel
|
|
|
|
|
|
class ChatSession(SQLModel, table=True):
|
|
"""
|
|
[AC-AISVC-13] Chat session entity with tenant isolation.
|
|
Primary key: (tenant_id, session_id) composite unique constraint.
|
|
"""
|
|
|
|
__tablename__ = "chat_sessions"
|
|
__table_args__ = (
|
|
Index("ix_chat_sessions_tenant_session", "tenant_id", "session_id", unique=True),
|
|
Index("ix_chat_sessions_tenant_id", "tenant_id"),
|
|
)
|
|
|
|
id: uuid.UUID = Field(default_factory=uuid.uuid4, primary_key=True)
|
|
tenant_id: str = Field(..., description="Tenant ID for multi-tenant isolation", index=True)
|
|
session_id: str = Field(..., description="Session ID for conversation tracking")
|
|
channel_type: str | None = Field(default=None, description="Channel type: wechat, douyin, jd")
|
|
metadata_: dict[str, Any] | None = Field(
|
|
default=None,
|
|
sa_column=Column("metadata", JSON, nullable=True),
|
|
description="Session metadata"
|
|
)
|
|
created_at: datetime = Field(default_factory=datetime.utcnow, description="Session creation time")
|
|
updated_at: datetime = Field(default_factory=datetime.utcnow, description="Last update time")
|
|
|
|
|
|
class ChatMessage(SQLModel, table=True):
|
|
"""
|
|
[AC-AISVC-13] Chat message entity with tenant isolation.
|
|
Messages are scoped by (tenant_id, session_id) for multi-tenant security.
|
|
"""
|
|
|
|
__tablename__ = "chat_messages"
|
|
__table_args__ = (
|
|
Index("ix_chat_messages_tenant_session", "tenant_id", "session_id"),
|
|
Index("ix_chat_messages_tenant_session_created", "tenant_id", "session_id", "created_at"),
|
|
)
|
|
|
|
id: uuid.UUID = Field(default_factory=uuid.uuid4, primary_key=True)
|
|
tenant_id: str = Field(..., description="Tenant ID for multi-tenant isolation", index=True)
|
|
session_id: str = Field(..., description="Session ID for conversation tracking", index=True)
|
|
role: str = Field(..., description="Message role: user or assistant")
|
|
content: str = Field(..., description="Message content")
|
|
created_at: datetime = Field(default_factory=datetime.utcnow, description="Message creation time")
|
|
|
|
|
|
class ChatSessionCreate(SQLModel):
|
|
"""Schema for creating a new chat session."""
|
|
|
|
tenant_id: str
|
|
session_id: str
|
|
channel_type: str | None = None
|
|
metadata_: dict[str, Any] | None = None
|
|
|
|
|
|
class ChatMessageCreate(SQLModel):
|
|
"""Schema for creating a new chat message."""
|
|
|
|
tenant_id: str
|
|
session_id: str
|
|
role: str
|
|
content: str
|