1use serde::{Deserialize, Serialize};
4
5use crate::error::Result;
6use crate::memory::{RecalledMemory, Session};
7use crate::DakeraClient;
8
9#[derive(Debug, Clone, Serialize, Deserialize)]
15pub struct AgentSummary {
16 pub agent_id: String,
17 pub memory_count: i64,
18 pub session_count: i64,
19 pub active_sessions: i64,
20}
21
22#[derive(Debug, Clone, Serialize, Deserialize)]
24pub struct AgentStats {
25 pub agent_id: String,
26 pub total_memories: i64,
27 #[serde(default)]
28 pub memories_by_type: std::collections::HashMap<String, i64>,
29 pub total_sessions: i64,
30 pub active_sessions: i64,
31 #[serde(skip_serializing_if = "Option::is_none")]
32 pub avg_importance: Option<f32>,
33 #[serde(skip_serializing_if = "Option::is_none")]
34 pub oldest_memory_at: Option<String>,
35 #[serde(skip_serializing_if = "Option::is_none")]
36 pub newest_memory_at: Option<String>,
37}
38
39impl DakeraClient {
44 pub async fn list_agents(&self) -> Result<Vec<AgentSummary>> {
46 let url = format!("{}/v1/agents", self.base_url);
47 let response = self.client.get(&url).send().await?;
48 self.handle_response(response).await
49 }
50
51 pub async fn agent_memories(
53 &self,
54 agent_id: &str,
55 memory_type: Option<&str>,
56 limit: Option<u32>,
57 ) -> Result<Vec<RecalledMemory>> {
58 let mut url = format!("{}/v1/agents/{}/memories", self.base_url, agent_id);
59 let mut params = Vec::new();
60 if let Some(t) = memory_type {
61 params.push(format!("memory_type={}", t));
62 }
63 if let Some(l) = limit {
64 params.push(format!("limit={}", l));
65 }
66 if !params.is_empty() {
67 url.push('?');
68 url.push_str(¶ms.join("&"));
69 }
70
71 let response = self.client.get(&url).send().await?;
72 self.handle_response(response).await
73 }
74
75 pub async fn agent_stats(&self, agent_id: &str) -> Result<AgentStats> {
77 let url = format!("{}/v1/agents/{}/stats", self.base_url, agent_id);
78 let response = self.client.get(&url).send().await?;
79 self.handle_response(response).await
80 }
81
82 pub async fn subscribe_agent_events(
111 &self,
112 agent_id: &str,
113 tags: Option<Vec<String>>,
114 ) -> crate::error::Result<
115 tokio::sync::mpsc::Receiver<crate::error::Result<crate::events::MemoryEvent>>,
116 > {
117 let (tx, rx) = tokio::sync::mpsc::channel(64);
118 let client = self.clone();
119 let agent_id = agent_id.to_owned();
120
121 tokio::spawn(async move {
122 loop {
123 match client.stream_memory_events().await {
124 Err(_) => {
125 tokio::time::sleep(std::time::Duration::from_secs(1)).await;
126 continue;
127 }
128 Ok(mut inner_rx) => {
129 while let Some(result) = inner_rx.recv().await {
130 match result {
131 Err(e) => {
132 let _ = tx.send(Err(e)).await;
134 break;
135 }
136 Ok(event) => {
137 if event.event_type == "connected" {
138 continue;
139 }
140 if event.agent_id != agent_id {
141 continue;
142 }
143 if let Some(ref filter_tags) = tags {
144 let event_tags = event.tags.as_deref().unwrap_or(&[]);
145 if !filter_tags.iter().any(|t| event_tags.contains(t)) {
146 continue;
147 }
148 }
149 if tx.send(Ok(event)).await.is_err() {
150 return; }
152 }
153 }
154 }
155 }
156 }
157 tokio::time::sleep(std::time::Duration::from_secs(1)).await;
159 }
160 });
161
162 Ok(rx)
163 }
164
165 pub async fn agent_sessions(
167 &self,
168 agent_id: &str,
169 active_only: Option<bool>,
170 limit: Option<u32>,
171 ) -> Result<Vec<Session>> {
172 let mut url = format!("{}/v1/agents/{}/sessions", self.base_url, agent_id);
173 let mut params = Vec::new();
174 if let Some(active) = active_only {
175 params.push(format!("active_only={}", active));
176 }
177 if let Some(l) = limit {
178 params.push(format!("limit={}", l));
179 }
180 if !params.is_empty() {
181 url.push('?');
182 url.push_str(¶ms.join("&"));
183 }
184
185 let response = self.client.get(&url).send().await?;
186 self.handle_response(response).await
187 }
188}