127 lines
6.0 KiB
Python
127 lines
6.0 KiB
Python
"""ChatRoom models - unified participant architecture"""
|
|
from datetime import datetime
|
|
from typing import Optional, List
|
|
from sqlalchemy import String, Integer, Boolean, Text, DateTime, ForeignKey
|
|
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
|
|
|
from luxx.core.database import Base
|
|
|
|
|
|
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")
|
|
participants: Mapped[List["RoomParticipant"]] = relationship(
|
|
"RoomParticipant", back_populates="room", cascade="all, delete-orphan"
|
|
)
|
|
# Note: messages are now stored in unified Message table with target_type="room"
|
|
|
|
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 and self.participants:
|
|
result["participants"] = [p.to_dict() for p in self.participants]
|
|
return result
|
|
|
|
|
|
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")
|
|
room_participations: Mapped[List["RoomParticipant"]] = relationship(
|
|
"RoomParticipant", back_populates="agent", cascade="all, delete-orphan"
|
|
)
|
|
|
|
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
|
|
|
|
|
|
class RoomParticipant(Base):
|
|
"""Room participant - unified for users and agents"""
|
|
__tablename__ = "room_participants"
|
|
|
|
id: Mapped[str] = mapped_column(String(64), primary_key=True)
|
|
room_id: Mapped[str] = mapped_column(String(64), ForeignKey("chat_rooms.id"), nullable=False)
|
|
agent_id: Mapped[Optional[str]] = mapped_column(String(64), ForeignKey("agents.id"), nullable=True)
|
|
user_id: Mapped[Optional[int]] = mapped_column(Integer, ForeignKey("users.id"), nullable=True)
|
|
role: Mapped[str] = mapped_column(String(32), default="member")
|
|
is_active: Mapped[bool] = mapped_column(Boolean, default=True)
|
|
joined_at: Mapped[datetime] = mapped_column(DateTime, default=local_now)
|
|
|
|
room: Mapped["ChatRoom"] = relationship("ChatRoom", back_populates="participants")
|
|
agent: Mapped[Optional["Agent"]] = relationship("Agent", back_populates="room_participations")
|
|
|
|
@property
|
|
def participant_id(self) -> str:
|
|
if self.agent_id:
|
|
return f"agent:{self.agent_id}"
|
|
return f"user:{self.user_id}" if self.user_id else ""
|
|
|
|
@property
|
|
def participant_type(self) -> str:
|
|
return "agent" if self.agent_id else "user"
|
|
|
|
@property
|
|
def name(self) -> str:
|
|
return self.agent.name if self.agent else f"user_{self.user_id}"
|
|
|
|
def to_dict(self):
|
|
return {
|
|
"id": self.id, "room_id": self.room_id, "agent_id": self.agent_id, "user_id": self.user_id,
|
|
"participant_id": self.participant_id, "participant_type": self.participant_type,
|
|
"name": self.name, "role": self.role, "is_active": self.is_active,
|
|
"joined_at": self.joined_at.isoformat() if self.joined_at else None
|
|
}
|