use crate::{agent::{AgentStatus, AgentStatusUpdate}, RecordReference};
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", content = "data")]
pub enum WebSocketMessage {
Auth(AuthMessage),
AuthResponse(AuthResponse),
AgentStatus(AgentStatus),
AgentStatusUpdate(AgentStatusUpdate),
IsTyping(IsTypingMessage),
IsTypingUpdate(IsTypingUpdate),
QueueGroup(crate::queue::QueueGroup),
ConversationNotification(crate::conversation::ConversationNotification),
Ping,
Pong,
Error(ErrorMessage),
Reconnect(ReconnectMessage),
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AuthMessage {
pub token: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AuthResponse {
pub success: bool,
pub user_id: Option<String>,
pub agent_id: Option<String>,
pub permissions: Vec<String>,
pub error: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct IsTypingMessage {
pub account: RecordReference,
pub user: RecordReference,
pub agent_name: String,
pub from: String,
pub action: String, pub timestamp: i64,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct IsTypingUpdate {
pub conversation_id: String,
pub user_id: String,
pub is_typing: bool,
pub timestamp: i64,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ErrorMessage {
pub code: String,
pub message: String,
pub details: Option<serde_json::Value>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ReconnectMessage {
pub reason: String,
pub retry_after: u32, }
impl WebSocketMessage {
pub fn ping() -> Self {
WebSocketMessage::Ping
}
pub fn pong() -> Self {
WebSocketMessage::Pong
}
pub fn error(code: String, message: String) -> Self {
WebSocketMessage::Error(ErrorMessage {
code,
message,
details: None,
})
}
pub fn is_ping(&self) -> bool {
matches!(self, WebSocketMessage::Ping)
}
pub fn is_pong(&self) -> bool {
matches!(self, WebSocketMessage::Pong)
}
}