Skip to main content

dakera_client/
agents.rs

1//! Agent management for the Dakera client.
2
3use serde::{Deserialize, Serialize};
4
5use crate::error::Result;
6use crate::memory::{RecalledMemory, Session};
7use crate::DakeraClient;
8
9// ============================================================================
10// Agent Types
11// ============================================================================
12
13/// Summary of an agent
14#[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/// Detailed stats for an agent
23#[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
39// ============================================================================
40// Agent Client Methods
41// ============================================================================
42
43impl DakeraClient {
44    /// List all agents
45    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    /// Get memories for an agent
52    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(&params.join("&"));
69        }
70
71        let response = self.client.get(&url).send().await?;
72        self.handle_response(response).await
73    }
74
75    /// Get stats for an agent
76    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    /// Get sessions for an agent
83    pub async fn agent_sessions(
84        &self,
85        agent_id: &str,
86        active_only: Option<bool>,
87        limit: Option<u32>,
88    ) -> Result<Vec<Session>> {
89        let mut url = format!("{}/v1/agents/{}/sessions", self.base_url, agent_id);
90        let mut params = Vec::new();
91        if let Some(active) = active_only {
92            params.push(format!("active_only={}", active));
93        }
94        if let Some(l) = limit {
95            params.push(format!("limit={}", l));
96        }
97        if !params.is_empty() {
98            url.push('?');
99            url.push_str(&params.join("&"));
100        }
101
102        let response = self.client.get(&url).send().await?;
103        self.handle_response(response).await
104    }
105}