Skip to main content

ContextStrategy

Trait ContextStrategy 

Source
pub trait ContextStrategy:
    Send
    + Sync
    + 'static {
    // Required methods
    fn name(&self) -> &str;
    fn apply<'life0, 'life1, 'life2, 'async_trait>(
        &'life0 self,
        context: &'life1 mut ContextState<'life2>,
    ) -> Pin<Box<dyn Future<Output = Result<bool>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait,
             'life2: 'async_trait;

    // Provided method
    fn init(
        &self,
        _agent: &mut LocalAgent,
        _archive: Option<&Archive>,
    ) -> Result<()> { ... }
}
Expand description

A context reduction strategy: offloads, summarizes or otherwise shrinks the conversation (ContextStrategy).

The context manager applies its strategies in order; each decides from the ContextState whether to act.

use strands::Result;
use strands::context_manager::{ContextState, ContextStrategy};

struct DropOldest;

#[strands::async_trait]
impl ContextStrategy for DropOldest {
    fn name(&self) -> &str {
        "drop-oldest"
    }

    async fn apply(&self, context: &mut ContextState<'_>) -> Result<bool> {
        if context.utilization < 1.0 || context.messages().len() < 3 {
            return Ok(false);
        }
        context.messages_mut().remove(1);
        Ok(true)
    }
}

Required Methods§

Source

fn name(&self) -> &str

A stable identifier for logging (name).

Source

fn apply<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, context: &'life1 mut ContextState<'life2>, ) -> Pin<Box<dyn Future<Output = Result<bool>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

Tries to reduce the context; returns whether it changed anything (apply).

§Errors

A failed reduction; the context manager logs it and continues with the next strategy.

Provided Methods§

Source

fn init( &self, _agent: &mut LocalAgent, _archive: Option<&Archive>, ) -> Result<()>

Called once when the context manager is attached to an agent, for example to register hooks that offload content as it arrives (init).

§Errors

A strategy that cannot run on this agent; attaching the context manager fails.

Dyn Compatibility§

This trait is dyn compatible.

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

Implementors§