agentlink_sdk/services/
message_service.rs1use std::sync::Arc;
4
5use serde_json::Value;
6use tokio::sync::RwLock;
7
8use crate::error::SdkResult;
9use crate::http::HttpClient;
10use crate::mqtt::client::MqttClient;
11use crate::protocols::message::{Message, MessagesResponse, SendMessageRequest};
12
13pub struct MessageService {
15 http: Arc<HttpClient>,
16 mqtt: Option<Arc<RwLock<MqttClient>>>,
17}
18
19impl MessageService {
20 pub fn new(http: Arc<HttpClient>) -> Self {
22 Self { http, mqtt: None }
23 }
24
25 pub fn with_mqtt(http: Arc<HttpClient>, mqtt: Arc<RwLock<MqttClient>>) -> Self {
27 Self {
28 http,
29 mqtt: Some(mqtt),
30 }
31 }
32
33 pub async fn get_conversation_messages(
43 &self,
44 conversation_id: &str,
45 cursor: Option<&str>,
46 limit: Option<i32>,
47 ) -> SdkResult<MessagesResponse> {
48 let mut path = format!(
49 "/conversations/{}/messages?limit={}",
50 conversation_id,
51 limit.unwrap_or(20)
52 );
53
54 if let Some(c) = cursor {
55 path.push_str(&format!("&cursor={}", c));
56 }
57
58 self.http.get(&path).await
59 }
60
61 pub async fn send_message(
73 &self,
74 conversation_id: &str,
75 content: &str,
76 message_type: Option<&str>,
77 reply_to_id: Option<&str>,
78 data: Option<Value>,
79 ) -> SdkResult<Message> {
80 let request = SendMessageRequest {
81 conversation_id: conversation_id.to_string(),
82 content: content.to_string(),
83 message_type: message_type.unwrap_or("text").to_string(),
84 reply_to_id: reply_to_id.map(|s| s.to_string()),
85 data,
86 };
87
88 self.http.post("/messages", &request).await
91 }
92
93 pub async fn delete_message(
99 &self,
100 conversation_id: &str,
101 message_id: &str,
102 ) -> SdkResult<()> {
103 let path = format!("/conversations/{}/messages/{}", conversation_id, message_id);
104 self.http.delete(&path).await
105 }
106
107 pub async fn mark_message_as_read(
113 &self,
114 conversation_id: &str,
115 message_id: &str,
116 ) -> SdkResult<()> {
117 let path = format!(
118 "/conversations/{}/messages/{}/read",
119 conversation_id, message_id
120 );
121 let _: serde_json::Value = self.http.post(&path, &serde_json::json!({})).await?;
122 Ok(())
123 }
124
125 pub async fn trigger_sync(&self, conversation_id: Option<&str>) -> SdkResult<()> {
132 let body = if let Some(cid) = conversation_id {
133 serde_json::json!({ "conversation_id": cid })
134 } else {
135 serde_json::json!({})
136 };
137
138 let _: serde_json::Value = self.http.post("/sync/messages", &body).await?;
139 Ok(())
140 }
141
142 pub async fn publish_message(&self, conversation_id: &str, payload: &[u8]) -> SdkResult<()> {
148 if let Some(ref mqtt) = self.mqtt {
149 let topic = format!("conversations/{}/messages", conversation_id);
150 let mqtt = mqtt.read().await;
151 mqtt.publish(&topic, payload).await
152 } else {
153 Err(crate::error::SdkError::NotConnected)
154 }
155 }
156}