112 lines
5.3 KiB
Python
112 lines
5.3 KiB
Python
"""ChatRoom models - unified participant architecture"""
|
|
from datetime import datetime
|
|
from typing import Optional, List, TYPE_CHECKING
|
|
from sqlalchemy import String, Integer, Boolean, Text, DateTime, ForeignKey, UniqueConstraint
|
|
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
|
|
|
from luxx.core.database import Base
|
|
|
|
if TYPE_CHECKING:
|
|
from luxx.models.user import User
|
|
|
|
|
|
def local_now():
|
|
return datetime.now()
|
|
|
|
|
|
class ChatRoom(Base):
|
|
"""Chat Room - group chat for multiple participants"""
|
|
__tablename__ = "chat_rooms"
|
|
|
|
id: Mapped[str] = mapped_column(String(64), primary_key=True)
|
|
name: Mapped[str] = mapped_column(String(100), nullable=False)
|
|
description: Mapped[Optional[str]] = mapped_column(Text, nullable=True)
|
|
owner_id: Mapped[int] = mapped_column(Integer, ForeignKey("users.id"), nullable=False)
|
|
is_active: Mapped[bool] = mapped_column(Boolean, default=True)
|
|
created_at: Mapped[datetime] = mapped_column(DateTime, default=local_now)
|
|
updated_at: Mapped[datetime] = mapped_column(DateTime, default=local_now, onupdate=local_now)
|
|
|
|
owner: Mapped["User"] = relationship("User", backref="chat_rooms")
|
|
room_agents: Mapped[List["RoomAgent"]] = relationship("RoomAgent", back_populates="room", cascade="all, delete-orphan")
|
|
|
|
def to_dict(self, include_agents: bool = False):
|
|
result = {
|
|
"id": self.id, "name": self.name, "description": self.description,
|
|
"owner_id": self.owner_id, "is_active": self.is_active,
|
|
"created_at": self.created_at.isoformat() if self.created_at else None,
|
|
"updated_at": self.updated_at.isoformat() if self.updated_at else None
|
|
}
|
|
if include_agents:
|
|
result["agents"] = [ra.to_dict() for ra in self.room_agents]
|
|
return result
|
|
|
|
|
|
class RoomAgent(Base):
|
|
"""ChatRoom 与 Agent 的关联表(替代依赖 Message 表的不稳定方案)"""
|
|
__tablename__ = "room_agents"
|
|
__table_args__ = (
|
|
UniqueConstraint('room_id', 'agent_id', name='uq_room_agent'),
|
|
)
|
|
|
|
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
|
|
room_id: Mapped[str] = mapped_column(String(64), ForeignKey("chat_rooms.id", ondelete="CASCADE"), nullable=False)
|
|
agent_id: Mapped[str] = mapped_column(String(64), ForeignKey("agents.id", ondelete="CASCADE"), nullable=False)
|
|
joined_at: Mapped[datetime] = mapped_column(DateTime, default=local_now)
|
|
is_active: Mapped[bool] = mapped_column(Boolean, default=True)
|
|
|
|
room: Mapped["ChatRoom"] = relationship("ChatRoom", back_populates="room_agents")
|
|
agent: Mapped["Agent"] = relationship("Agent")
|
|
|
|
def to_dict(self):
|
|
return {
|
|
"id": self.agent_id,
|
|
"room_agent_id": self.id,
|
|
"joined_at": self.joined_at.isoformat() if self.joined_at else None,
|
|
"is_active": self.is_active,
|
|
"agent": self.agent.to_dict() if self.agent else None
|
|
}
|
|
|
|
|
|
class Agent(Base):
|
|
"""Agent model - defines an AI agent"""
|
|
__tablename__ = "agents"
|
|
|
|
id: Mapped[str] = mapped_column(String(64), primary_key=True)
|
|
name: Mapped[str] = mapped_column(String(50), nullable=False)
|
|
role: Mapped[str] = mapped_column(String(50), nullable=False, default="helper")
|
|
avatar: Mapped[Optional[str]] = mapped_column(String(500), nullable=True)
|
|
system_prompt: Mapped[str] = mapped_column(Text, nullable=False, default="You are helpful.")
|
|
provider_id: Mapped[Optional[int]] = mapped_column(Integer, ForeignKey("llm_providers.id"), nullable=True)
|
|
model: Mapped[Optional[str]] = mapped_column(String(100), nullable=True)
|
|
tools: Mapped[Optional[str]] = mapped_column(Text, nullable=True)
|
|
is_active: Mapped[bool] = mapped_column(Boolean, default=True)
|
|
priority: Mapped[int] = mapped_column(Integer, default=5)
|
|
auto_response: Mapped[bool] = mapped_column(Boolean, default=True)
|
|
mention_trigger: Mapped[bool] = mapped_column(Boolean, default=False)
|
|
temperature: Mapped[float] = mapped_column(Text, default="0.7")
|
|
max_tokens: Mapped[int] = mapped_column(Integer, default=2048)
|
|
created_at: Mapped[datetime] = mapped_column(DateTime, default=local_now)
|
|
updated_at: Mapped[datetime] = mapped_column(DateTime, default=local_now, onupdate=local_now)
|
|
|
|
provider: Mapped[Optional["LLMProvider"]] = relationship("LLMProvider")
|
|
|
|
def to_dict(self, include_secrets: bool = False):
|
|
import json
|
|
result = {
|
|
"id": self.id, "name": self.name, "role": self.role, "avatar": self.avatar,
|
|
"system_prompt": self.system_prompt, "provider_id": self.provider_id, "model": self.model,
|
|
"is_active": self.is_active, "priority": self.priority,
|
|
"auto_response": self.auto_response, "mention_trigger": self.mention_trigger,
|
|
"temperature": float(self.temperature) if self.temperature else 0.7,
|
|
"max_tokens": self.max_tokens,
|
|
"created_at": self.created_at.isoformat() if self.created_at else None,
|
|
"updated_at": self.updated_at.isoformat() if self.updated_at else None
|
|
}
|
|
try:
|
|
result["tools"] = json.loads(self.tools) if self.tools else []
|
|
except json.JSONDecodeError:
|
|
result["tools"] = []
|
|
if include_secrets and self.provider:
|
|
result["provider"] = self.provider.to_dict(include_key=True)
|
|
return result
|