Skip to main content

ConversationManager

Trait ConversationManager 

Source
pub trait ConversationManager:
    Send
    + Sync
    + 'static {
    // Required methods
    fn name(&self) -> &str;
    fn reduce<'life0, 'life1, 'async_trait>(
        &'life0 self,
        options: ConversationManagerReduceOptions<'life1>,
    ) -> Pin<Box<dyn Future<Output = Result<bool>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;

    // Provided methods
    fn compression_threshold(&self) -> Option<f64> { ... }
    fn init_agent<'life0, 'life1, 'async_trait>(
        &'life0 self,
        _agent: &'life1 mut LocalAgent,
    ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait { ... }
}
Expand description

A conversation history strategy (ConversationManager).

The agent registers it as a plugin: on a context-window overflow it calls reduce with the error and retries when it returns true; before a model call it calls reduce without an error when compression_threshold is set and the projected input reaches it. init_agent adds any other hooks the strategy needs.

use strands::async_trait;
use strands::conversation_manager::{ConversationManager, ConversationManagerReduceOptions};

struct LastTenMessages;

#[async_trait]
impl ConversationManager for LastTenMessages {
    fn name(&self) -> &str {
        "my:last-10-messages"
    }

    async fn reduce(&self, options: ConversationManagerReduceOptions<'_>) -> strands::Result<bool> {
        let messages = options.agent.messages_mut();
        if messages.len() <= 10 {
            return Ok(false);
        }
        let excess = messages.len() - 10;
        messages.drain(..excess);
        Ok(true)
    }
}

Required Methods§

Source

fn name(&self) -> &str

A stable identifier, also the plugin name.

Source

fn reduce<'life0, 'life1, 'async_trait>( &'life0 self, options: ConversationManagerReduceOptions<'life1>, ) -> Pin<Box<dyn Future<Output = Result<bool>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Reduces the history and returns whether it changed (reduce).

With options.error set this is overflow recovery and must make progress, or the overflow leaves the agent loop. Without it, it is best-effort proactive compression.

§Errors

A reduction that failed. During recovery the error replaces the overflow error; during proactive compression it is logged and the call proceeds.

Provided Methods§

Source

fn compression_threshold(&self) -> Option<f64>

The proactive compression threshold, or None when only overflow recovery runs: the ratio of the context window, in (0, 1], at which a model call reduces first. The agent checks it when the manager is initialized and fails otherwise.

Source

fn init_agent<'life0, 'life1, 'async_trait>( &'life0 self, _agent: &'life1 mut LocalAgent, ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Registers the strategy’s own hooks, after the overflow and compression hooks.

§Errors

A failed initialization; the agent’s first invocation reports it.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl<M: ConversationManager + ?Sized> ConversationManager for Arc<M>

A shared manager is a manager, so the builder’s conversation_manager(..) takes one by value or as an Arc.

Source§

fn name(&self) -> &str

Source§

fn reduce<'life0, 'life1, 'async_trait>( &'life0 self, options: ConversationManagerReduceOptions<'life1>, ) -> Pin<Box<dyn Future<Output = Result<bool>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Source§

fn compression_threshold(&self) -> Option<f64>

Source§

fn init_agent<'life0, 'life1, 'async_trait>( &'life0 self, agent: &'life1 mut LocalAgent, ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Implementors§