Skip to main content

alice_runtime/agent_backend/
bob_backend.rs

1//! Bob agent backend implementation.
2//!
3//! Wraps [`bob_runtime::Agent`] and [`bob_runtime::Session`] behind the
4//! [`AgentBackend`](super::AgentBackend) / [`AgentSession`](super::AgentSession) traits.
5
6use std::sync::Arc;
7
8use bob_core::types::RequestContext;
9use bob_runtime::{Agent, AgentResponse, Session};
10
11use super::{AgentBackend, AgentSession};
12
13/// Bob-runtime backed session.
14#[derive(Debug)]
15pub struct BobAgentSession {
16    session: Session,
17}
18
19#[async_trait::async_trait]
20impl AgentSession for BobAgentSession {
21    async fn chat(&self, input: &str, context: RequestContext) -> eyre::Result<AgentResponse> {
22        let response = self.session.chat_with_context(input, context).await?;
23        Ok(response)
24    }
25}
26
27/// Bob-runtime backed agent backend.
28///
29/// Holds a pre-built [`Agent`] and produces [`Session`] instances on demand.
30#[derive(Debug, Clone)]
31pub struct BobAgentBackend {
32    agent: Agent,
33}
34
35impl BobAgentBackend {
36    /// Create a new backend from a pre-built agent.
37    #[must_use]
38    pub const fn new(agent: Agent) -> Self {
39        Self { agent }
40    }
41}
42
43impl AgentBackend for BobAgentBackend {
44    fn create_session(&self) -> Arc<dyn AgentSession> {
45        let session = self.agent.start_session();
46        Arc::new(BobAgentSession { session })
47    }
48
49    fn create_session_with_id(&self, session_id: &str) -> Arc<dyn AgentSession> {
50        let session = self.agent.start_session_with_id(session_id);
51        Arc::new(BobAgentSession { session })
52    }
53}