Skip to main content

ContextSource

Trait ContextSource 

Source
pub trait ContextSource:
    Send
    + Sync
    + 'static {
    // Required methods
    fn id(&self) -> SourceId;
    fn blocks(
        &mut self,
        moment: Moment,
    ) -> impl Future<Output = Vec<ContextBlock>> + Send;
    fn peek(
        &self,
        moment: Moment,
    ) -> impl Future<Output = Vec<ContextBlock>> + Send;

    // Provided method
    fn committed(&mut self, tail: &[Block]) -> impl Future<Output = ()> + Send { ... }
}
Expand description

Supplies context to the requests the agent sends.

Implementations are asked at a request boundary, so they may read files or query registries; they must not block.

§Example

use aither_agent::{ContextBlock, ContextSource, Moment, SourceId};

struct Clock;

impl ContextSource for Clock {
    fn id(&self) -> SourceId {
        SourceId::new("clock")
    }

    async fn blocks(&mut self, moment: Moment) -> Vec<ContextBlock> {
        self.peek(moment).await
    }

    async fn peek(&self, moment: Moment) -> Vec<ContextBlock> {
        match moment {
            Moment::Request { .. } => vec![ContextBlock::tail("now", "2025-01-01T00:00:00Z")],
            _ => Vec::new(),
        }
    }
}

Required Methods§

Source

fn id(&self) -> SourceId

Names the source in AgentEvent::ContextInjected.

Source

fn blocks( &mut self, moment: Moment, ) -> impl Future<Output = Vec<ContextBlock>> + Send

The blocks this source contributes at moment.

An empty list is the right answer when there is nothing to say, since an empty block still costs tokens and still invites the model to reason about it. A Placement::Stable block is consumed by being injected: the source does not return it again unless the window was rewritten.

Source

fn peek(&self, moment: Moment) -> impl Future<Output = Vec<ContextBlock>> + Send

The blocks blocks would return now at moment, without consuming any of them.

A preview of the next request asks this instead of blocks, so that it shows exactly what the request would carry while a one-shot block is still waiting to be delivered by the request itself. It takes &self because nothing may change.

Provided Methods§

Source

fn committed(&mut self, tail: &[Block]) -> impl Future<Output = ()> + Send

Reports the Placement::Tail blocks that were actually sent.

Called once per request, after the request has been assembled, which is the only point at which a source can know what the model has seen and therefore what counts as a change next time.

Dyn Compatibility§

This trait is not dyn compatible.

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

Implementors§