summarize_at_limit/
summarize_at_limit.rs

1use espionox::{agents::error::AgentResult, prelude::*};
2
3#[derive(Debug)]
4pub struct SummarizeAtLimit {
5    limit: usize,
6    summarizer: Agent,
7}
8
9impl SummarizeAtLimit {
10    fn new(limit: usize, summarizer: Agent) -> Self {
11        Self { limit, summarizer }
12    }
13}
14
15/// We'll define our own push to cache action method so we can trigger the listener on it
16/// All action methods must be async & return AgentResult, which can be coerced from an
17/// `anyhow::Result`
18async fn push_to_cache_with_limit(
19    agent: &mut Agent,
20    sum: &mut SummarizeAtLimit,
21    m: Message,
22) -> AgentResult<()> {
23    if agent.cache.len() >= sum.limit {
24        let message = Message::new_user(&format!(
25            "Summarize this chat history: {}",
26            agent.cache.to_string()
27        ));
28        sum.summarizer.cache.push(message);
29
30        let summary = sum.summarizer.io_completion().await?;
31
32        agent.cache.mut_filter_by(&MessageRole::System, true);
33        agent.cache.push(Message::new_assistant(&summary));
34    }
35    agent.cache.push(m);
36    Ok(())
37}
38
39#[tokio::main]
40async fn main() {
41    dotenv::dotenv().ok();
42    let api_key = std::env::var("ANTHROPIC_KEY").unwrap();
43    let mut agent = Agent::new(
44        Some("You are jerry!!"),
45        CompletionModel::default_anthropic(&api_key),
46    );
47
48    let summarizer = Agent::new(
49        Some("Your job is to summarize chunks of a conversation"),
50        CompletionModel::default_anthropic(&api_key),
51    );
52    let mut sal = SummarizeAtLimit::new(5usize, summarizer);
53
54    // agent.insert_listener(sal);
55    let message = Message::new_user("im saying things to fill space");
56
57    for _ in 0..=5 {
58        // And now we use our predefined action method
59        push_to_cache_with_limit(&mut agent, &mut sal, message.clone())
60            .await
61            .unwrap();
62    }
63
64    // env.finalize_dispatch().await.unwrap();
65    println!("STACK: {:?}", agent.cache);
66    assert_eq!(agent.cache.len(), 4);
67    assert_eq!(agent.cache.as_ref()[0].role, MessageRole::System);
68    assert_eq!(agent.cache.as_ref()[1].role, MessageRole::Assistant);
69    assert_eq!(agent.cache.as_ref()[2].role, MessageRole::User);
70    println!("All asserts passed, summarize at limit working as expected");
71}