Skip to main content

stasis/application/use_cases/
invoke_agent.rs

1use crate::application::dto::{InvokeAgentRequest, InvokeAgentResponse};
2use crate::domain::errors::{Result, StasisError};
3use crate::ports::outbound::agent_repository::AgentRepository;
4use crate::ports::outbound::llm_gateway::LlmGateway;
5
6#[derive(Clone)]
7pub struct InvokeAgent<R, L>
8where
9    R: AgentRepository,
10    L: LlmGateway,
11{
12    repository: R,
13    llm: L,
14}
15
16impl<R, L> InvokeAgent<R, L>
17where
18    R: AgentRepository,
19    L: LlmGateway,
20{
21    pub fn new(repository: R, llm: L) -> Self {
22        Self { repository, llm }
23    }
24
25    pub async fn execute(&self, request: InvokeAgentRequest) -> Result<InvokeAgentResponse> {
26        let agent = self
27            .repository
28            .find_by_id(&request.agent_id)
29            .await?
30            .ok_or_else(|| StasisError::AgentNotFound(request.agent_id.clone()))?;
31
32        let prompt = format!(
33            "SYSTEM:\n{}\n\nUSER:\n{}",
34            agent.system_prompt, request.user_prompt
35        );
36
37        let completion = self.llm.complete(&prompt).await?;
38
39        Ok(InvokeAgentResponse {
40            agent_id: agent.id.as_str().to_string(),
41            completion,
42        })
43    }
44}