82 lines
2.4 KiB
Python
82 lines
2.4 KiB
Python
"""Participant model - unified abstraction for users and agents in chat rooms."""
|
|
from typing import Optional, Dict, Any, TYPE_CHECKING
|
|
from dataclasses import dataclass
|
|
from enum import Enum
|
|
|
|
if TYPE_CHECKING:
|
|
from luxx.models.user import User
|
|
|
|
|
|
class ParticipantType(Enum):
|
|
USER = "user"
|
|
AGENT = "agent"
|
|
|
|
|
|
@dataclass
|
|
class Participant:
|
|
"""Unified participant abstraction for users and agents."""
|
|
participant_id: str
|
|
name: str
|
|
participant_type: ParticipantType
|
|
avatar: Optional[str] = None
|
|
agent_config_id: Optional[str] = None
|
|
role: Optional[str] = None
|
|
auto_response: bool = False
|
|
mention_trigger: bool = False
|
|
priority: int = 5
|
|
|
|
@property
|
|
def is_agent(self) -> bool:
|
|
return self.participant_type == ParticipantType.AGENT
|
|
|
|
@property
|
|
def is_user(self) -> bool:
|
|
return self.participant_type == ParticipantType.USER
|
|
|
|
def to_dict(self) -> Dict[str, Any]:
|
|
return {
|
|
"id": self.participant_id,
|
|
"name": self.name,
|
|
"type": self.participant_type.value,
|
|
"avatar": self.avatar,
|
|
"role": self.role,
|
|
"is_agent": self.is_agent
|
|
}
|
|
|
|
# ==================== Factory Methods ====================
|
|
|
|
@classmethod
|
|
def from_user(cls, user: "User") -> "Participant":
|
|
return cls(
|
|
participant_id=str(user.id),
|
|
name=user.username,
|
|
participant_type=ParticipantType.USER
|
|
)
|
|
|
|
@classmethod
|
|
def from_agent(
|
|
cls, agent_id: str, name: str, role: str,
|
|
avatar: Optional[str] = None, auto_response: bool = True,
|
|
mention_trigger: bool = False, priority: int = 5
|
|
) -> "Participant":
|
|
return cls(
|
|
participant_id=agent_id,
|
|
name=name,
|
|
participant_type=ParticipantType.AGENT,
|
|
avatar=avatar,
|
|
agent_config_id=agent_id,
|
|
role=role,
|
|
auto_response=auto_response,
|
|
mention_trigger=mention_trigger,
|
|
priority=priority
|
|
)
|
|
|
|
@classmethod
|
|
def from_participant_id(cls, participant_id: str, name: str = None) -> "Participant":
|
|
ptype, pid = (participant_id.split(":", 1) + [None])[:2] if ":" in participant_id else ("user", participant_id)
|
|
return cls(
|
|
participant_id=participant_id,
|
|
name=name or pid,
|
|
participant_type=ParticipantType.USER if ptype == "user" else ParticipantType.AGENT
|
|
)
|