Skip to main content

agent_relay/
client.rs

1//! HTTP client for connecting to a remote relay server.
2
3use crate::{AgentRegistration, Message};
4
5pub struct RemoteRelay {
6    base_url: String,
7}
8
9impl RemoteRelay {
10    pub fn new(server_url: &str) -> Self {
11        let base_url = server_url.trim_end_matches('/').to_string();
12        Self { base_url }
13    }
14
15    pub fn health(&self) -> Result<String, String> {
16        let resp = ureq::get(&format!("{}/health", self.base_url))
17            .call()
18            .map_err(|e| format!("Connection failed: {}", e))?;
19        resp.into_string().map_err(|e| format!("Read error: {}", e))
20    }
21
22    pub fn register(
23        &self,
24        agent_id: &str,
25        session_id: &str,
26        pid: u32,
27    ) -> Result<AgentRegistration, String> {
28        let resp = ureq::post(&format!("{}/agents/register", self.base_url))
29            .send_json(serde_json::json!({
30                "session_id": session_id,
31                "agent_id": agent_id,
32                "pid": pid,
33            }))
34            .map_err(|e| format!("Request failed: {}", e))?;
35
36        let body: AgentRegistration = resp
37            .into_json()
38            .map_err(|e| format!("Parse error: {}", e))?;
39        Ok(body)
40    }
41
42    pub fn unregister(&self, session_id: &str) -> Result<(), String> {
43        ureq::post(&format!("{}/agents/unregister", self.base_url))
44            .send_json(serde_json::json!({"session_id": session_id}))
45            .map_err(|e| format!("Request failed: {}", e))?;
46        Ok(())
47    }
48
49    pub fn agents(&self) -> Result<Vec<AgentRegistration>, String> {
50        let resp = ureq::get(&format!("{}/agents", self.base_url))
51            .call()
52            .map_err(|e| format!("Request failed: {}", e))?;
53
54        let body: Vec<AgentRegistration> = resp
55            .into_json()
56            .map_err(|e| format!("Parse error: {}", e))?;
57        Ok(body)
58    }
59
60    pub fn send(
61        &self,
62        from_session: &str,
63        from_agent: &str,
64        to_session: Option<&str>,
65        content: &str,
66    ) -> Result<Message, String> {
67        let resp = ureq::post(&format!("{}/messages/send", self.base_url))
68            .send_json(serde_json::json!({
69                "from_session": from_session,
70                "from_agent": from_agent,
71                "to_session": to_session,
72                "content": content,
73            }))
74            .map_err(|e| format!("Request failed: {}", e))?;
75
76        let body: Message = resp
77            .into_json()
78            .map_err(|e| format!("Parse error: {}", e))?;
79        Ok(body)
80    }
81
82    pub fn inbox(&self, session_id: &str, limit: usize) -> Result<Vec<Message>, String> {
83        let resp = ureq::get(&format!(
84            "{}/messages/inbox?session={}&limit={}",
85            self.base_url, session_id, limit
86        ))
87        .call()
88        .map_err(|e| format!("Request failed: {}", e))?;
89
90        let body: Vec<Message> = resp
91            .into_json()
92            .map_err(|e| format!("Parse error: {}", e))?;
93        Ok(body)
94    }
95
96    pub fn unread_count(&self, session_id: &str) -> Result<u64, String> {
97        let resp = ureq::get(&format!(
98            "{}/messages/unread?session={}",
99            self.base_url, session_id
100        ))
101        .call()
102        .map_err(|e| format!("Request failed: {}", e))?;
103
104        let body: serde_json::Value = resp
105            .into_json()
106            .map_err(|e| format!("Parse error: {}", e))?;
107        Ok(body["count"].as_u64().unwrap_or(0))
108    }
109}