1use serde::{Deserialize, Serialize};
10
11#[derive(Debug, Clone, Serialize, Deserialize)]
13#[serde(tag = "type", rename_all = "snake_case")]
14pub enum MessageType {
15 Text { content: String },
17
18 Task {
20 task_id: String,
21 description: String,
22 payload: serde_json::Value,
24 },
25
26 TaskResult {
28 task_id: String,
29 success: bool,
30 result: serde_json::Value,
31 },
32
33 Heartbeat { sequence: u64 },
35
36 Command { command: ActorCommand },
38
39 Alert {
41 severity: AlertSeverity,
42 message: String,
43 context: serde_json::Value,
44 },
45
46 SpawnRequest {
48 agent_type: String,
49 agent_name: String,
50 config: serde_json::Value,
51 },
52
53 SpawnResult {
55 agent_name: String,
56 agent_id: String,
57 success: bool,
58 error: Option<String>,
59 },
60}
61
62#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
64#[serde(rename_all = "snake_case")]
65pub enum ActorCommand {
66 Stop,
68 Pause,
70 Resume,
72 Status,
74}
75
76#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
78#[serde(rename_all = "lowercase")]
79pub enum AlertSeverity {
80 Info,
81 Warning,
82 Error,
83 Critical,
84}
85
86#[derive(Debug, Clone, Serialize, Deserialize)]
88pub struct Message {
89 pub id: String,
91 pub from: Option<String>,
93 pub to: Option<String>,
95 pub timestamp_ms: u64,
97 pub payload: MessageType,
99}
100
101impl Message {
102 pub fn new(from: Option<String>, to: Option<String>, payload: MessageType) -> Self {
104 let id = wid::HLCWidGen::new("msg".to_string(), 4, 0)
105 .expect("HLCWidGen init failed")
106 .next_hlc_wid();
107 let timestamp_ms = std::time::SystemTime::now()
108 .duration_since(std::time::UNIX_EPOCH)
109 .unwrap_or_default()
110 .as_millis() as u64;
111 Self {
112 id,
113 from,
114 to,
115 timestamp_ms,
116 payload,
117 }
118 }
119
120 pub fn text(from: Option<String>, to: Option<String>, content: impl Into<String>) -> Self {
122 Self::new(
123 from,
124 to,
125 MessageType::Text {
126 content: content.into(),
127 },
128 )
129 }
130
131 pub fn command(to: String, cmd: ActorCommand) -> Self {
133 Self::new(None, Some(to), MessageType::Command { command: cmd })
134 }
135}