173 lines
7.3 KiB
Python
173 lines
7.3 KiB
Python
"""ChatRoom models"""
|
|
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 model - like a group chat for multiple agents"""
|
|
__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)
|
|
|
|
# Relationships
|
|
owner: Mapped["User"] = relationship("User", backref="chat_rooms")
|
|
agents: Mapped[List["ChatRoomAgent"]] = relationship(
|
|
"ChatRoomAgent", back_populates="chat_room", cascade="all, delete-orphan"
|
|
)
|
|
messages: Mapped[List["ChatRoomMessage"]] = relationship(
|
|
"ChatRoomMessage", back_populates="chat_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 and self.agents:
|
|
result["agents"] = [ca.to_dict() for ca in self.agents]
|
|
return result
|
|
|
|
|
|
class Agent(Base):
|
|
"""Agent model - defines an AI agent with specific role"""
|
|
__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 a helpful assistant.")
|
|
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)
|
|
|
|
# Relationships
|
|
provider: Mapped[Optional["LLMProvider"]] = relationship("LLMProvider")
|
|
chat_rooms: Mapped[List["ChatRoomAgent"]] = relationship(
|
|
"ChatRoomAgent", 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
|
|
}
|
|
if self.tools:
|
|
try:
|
|
result["tools"] = json.loads(self.tools)
|
|
except json.JSONDecodeError:
|
|
result["tools"] = []
|
|
else:
|
|
result["tools"] = []
|
|
|
|
if include_secrets and self.provider:
|
|
result["provider"] = self.provider.to_dict(include_key=True)
|
|
return result
|
|
|
|
|
|
class ChatRoomAgent(Base):
|
|
"""Association table for ChatRoom and Agent"""
|
|
__tablename__ = "chat_room_agents"
|
|
|
|
id: Mapped[str] = mapped_column(String(64), primary_key=True)
|
|
chat_room_id: Mapped[str] = mapped_column(String(64), ForeignKey("chat_rooms.id"), nullable=False)
|
|
agent_id: Mapped[str] = mapped_column(String(64), ForeignKey("agents.id"), nullable=False)
|
|
is_active: Mapped[bool] = mapped_column(Boolean, default=True)
|
|
joined_at: Mapped[datetime] = mapped_column(DateTime, default=local_now)
|
|
|
|
# Relationships
|
|
chat_room: Mapped["ChatRoom"] = relationship("ChatRoom", back_populates="agents")
|
|
agent: Mapped["Agent"] = relationship("Agent", back_populates="chat_rooms")
|
|
|
|
def to_dict(self):
|
|
return {
|
|
"id": self.id,
|
|
"chat_room_id": self.chat_room_id,
|
|
"agent_id": self.agent_id,
|
|
"is_active": self.is_active,
|
|
"joined_at": self.joined_at.isoformat() if self.joined_at else None,
|
|
"agent": self.agent.to_dict() if self.agent else None
|
|
}
|
|
|
|
|
|
class ChatRoomMessage(Base):
|
|
"""Chat Room Message model"""
|
|
__tablename__ = "chat_room_messages"
|
|
|
|
id: Mapped[str] = mapped_column(String(64), primary_key=True)
|
|
room_id: Mapped[str] = mapped_column(String(64), ForeignKey("chat_rooms.id"), nullable=False)
|
|
sender_type: Mapped[str] = mapped_column(String(16), nullable=False)
|
|
sender_id: Mapped[str] = mapped_column(String(64), nullable=False)
|
|
sender_name: Mapped[str] = mapped_column(String(50), nullable=False)
|
|
content: Mapped[str] = mapped_column(Text, nullable=False, default="")
|
|
mentions: Mapped[Optional[str]] = mapped_column(Text, nullable=True)
|
|
parent_id: Mapped[Optional[str]] = mapped_column(String(64), nullable=True)
|
|
token_count: Mapped[int] = mapped_column(Integer, default=0)
|
|
created_at: Mapped[datetime] = mapped_column(DateTime, default=local_now)
|
|
|
|
# Relationships
|
|
chat_room: Mapped["ChatRoom"] = relationship("ChatRoom", back_populates="messages")
|
|
|
|
def to_dict(self):
|
|
import json
|
|
result = {
|
|
"id": self.id,
|
|
"room_id": self.room_id,
|
|
"sender_type": self.sender_type,
|
|
"sender_id": self.sender_id,
|
|
"sender_name": self.sender_name,
|
|
"content": self.content,
|
|
"parent_id": self.parent_id,
|
|
"token_count": self.token_count,
|
|
"created_at": self.created_at.isoformat() if self.created_at else None
|
|
}
|
|
if self.mentions:
|
|
try:
|
|
result["mentions"] = json.loads(self.mentions)
|
|
except json.JSONDecodeError:
|
|
result["mentions"] = []
|
|
else:
|
|
result["mentions"] = []
|
|
return result
|