Skip to main content

stasis/application/use_cases/
register_agent.rs

1use crate::application::dto::RegisterAgentRequest;
2use crate::domain::entities::agent::Agent;
3use crate::domain::errors::{Result, StasisError};
4use crate::ports::outbound::agent_repository::AgentRepository;
5
6#[derive(Clone)]
7pub struct RegisterAgent<R>
8where
9    R: AgentRepository,
10{
11    repository: R,
12}
13
14impl<R> RegisterAgent<R>
15where
16    R: AgentRepository,
17{
18    pub fn new(repository: R) -> Self {
19        Self { repository }
20    }
21
22    pub async fn execute(&self, request: RegisterAgentRequest) -> Result<()> {
23        if self.repository.find_by_id(&request.id).await?.is_some() {
24            return Err(StasisError::AgentAlreadyExists(request.id));
25        }
26
27        let agent = Agent::new(request.id, request.name, request.system_prompt)?;
28        self.repository.save(agent).await
29    }
30}