kwaak 0.8.2

Run a team of autonomous agents on your code, right from your terminal
Documentation
use anyhow::Result;
use derive_builder::Builder;
use std::sync::Arc;
use swiftide::traits::{AgentContext, ToolExecutor};

use swiftide::agents::Agent;
use tokio::sync::Mutex;
use tokio_util::sync::CancellationToken;

use super::env_setup::AgentEnvironment;

/// Defines any agent that is running
#[derive(Clone, Builder)]
#[builder(build_fn(error = anyhow::Error))]
pub struct RunningAgent {
    /// The agent that is running
    #[builder(setter(custom))]
    pub agent: Arc<Mutex<Agent>>,
    /// A copy of the running tool executor the agent is using
    pub executor: Arc<dyn ToolExecutor>,
    /// The content the agent is running with
    #[builder(setter(into))]
    pub agent_context: Arc<dyn AgentContext>,
    /// Used to kill the agent
    #[builder(default)]
    pub cancel_token: CancellationToken,
    /// Information about the environment the agent is running in
    #[builder(setter(into))]
    pub agent_environment: Arc<AgentEnvironment>,
}

impl RunningAgent {
    #[must_use]
    pub fn builder() -> RunningAgentBuilder {
        RunningAgentBuilder::default()
    }

    pub async fn query(&self, query: &str) -> Result<()> {
        self.agent.lock().await.query(query).await
    }

    pub async fn run(&self) -> Result<()> {
        self.agent.lock().await.run().await
    }

    pub async fn stop(&self) {
        self.cancel_token.cancel();
        self.agent.lock().await.stop();
    }
}

impl RunningAgentBuilder {
    pub fn agent(&mut self, agent: Agent) -> &mut Self {
        self.agent = Some(Arc::new(Mutex::new(agent)));
        self
    }
}