cal-core 0.2.158

Callable core lib
Documentation
// File: cal-core/src/websocket.rs

use crate::{agent::{AgentStatus, AgentStatusUpdate}, RecordReference};
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", content = "data")]
pub enum WebSocketMessage {
    // Authentication (required for WebSocket security)
    Auth(AuthMessage),
    AuthResponse(AuthResponse),

    // Agent Status
    AgentStatus(AgentStatus),
    AgentStatusUpdate(AgentStatusUpdate),

    // Typing Indicators
    IsTyping(IsTypingMessage),
    IsTypingUpdate(IsTypingUpdate),

    // Queue
    QueueGroup(crate::queue::QueueGroup),

    // Conversation
    ConversationNotification(crate::conversation::ConversationNotification),

    // System
    Ping,
    Pong,
    Error(ErrorMessage),
    Reconnect(ReconnectMessage),
}

// ==================== Authentication Messages ====================

#[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>,
}

// ==================== Typing Messages ====================

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct IsTypingMessage {
    pub account: RecordReference,
    pub user: RecordReference,
    pub agent_name: String,
    pub from: String,
    pub action: String, // "typing" or "stopped"
    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,
}

// ==================== System Messages ====================

#[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, // seconds
}

// ==================== Helper Implementations ====================

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)
    }
}