Skip to main content

agentics_persistence/repositories/
agents.rs

1use secrecy::SecretString;
2use sqlx::PgPool;
3
4use crate::db;
5use crate::repositories::{
6    AgentRecord, AuthenticatedAgent, PioneerCodeRegistrationKind, RegisterAgentInput,
7};
8use agentics_error::Result;
9
10#[derive(Debug, Clone, Copy)]
11pub struct AgentsRepository<'a> {
12    pub(super) pool: &'a PgPool,
13}
14
15impl AgentsRepository<'_> {
16    pub async fn register_agent(
17        &self,
18        input: &RegisterAgentInput,
19        max_active_agents: i64,
20    ) -> Result<AgentRecord> {
21        db::agents::register_agent(self.pool, input, max_active_agents).await
22    }
23
24    pub async fn register_agent_with_pioneer_code(
25        &self,
26        input: &RegisterAgentInput,
27        code_hash: &str,
28        max_active_agents: i64,
29        kind: PioneerCodeRegistrationKind,
30    ) -> Result<AgentRecord> {
31        db::agents::register_agent_with_pioneer_code(
32            self.pool,
33            input,
34            code_hash,
35            kind,
36            max_active_agents,
37        )
38        .await
39    }
40
41    pub async fn count_active(&self) -> Result<i64> {
42        db::agents::count_active_agents(self.pool).await
43    }
44
45    pub async fn authenticate_token(
46        &self,
47        token: &SecretString,
48    ) -> Result<Option<AuthenticatedAgent>> {
49        db::agents::authenticate_agent_token(self.pool, token).await
50    }
51
52    pub async fn disable(&self, agent_id: &str) -> Result<()> {
53        db::agents::disable_agent(self.pool, agent_id).await
54    }
55}