Skip to main content

ContextPipeline

Struct ContextPipeline 

Source
pub struct ContextPipeline { /* private fields */ }
Expand description

Runtime context pipeline that composes context from multiple sources.

The pipeline:

  1. Loads session history from the store
  2. Applies compaction filter (reorder post-compaction messages)
  3. Invokes registered context adapters (system prompt, RAG, etc.)
  4. Applies token-budget trimming as a safety net
  5. Produces a final ChatRequest or ContextOutput

Implementations§

Source§

impl ContextPipeline

Source

pub fn new() -> Self

Creates a new context pipeline with default settings.

Source

pub fn with_factory(factory: ContextFactory) -> Self

Creates a context pipeline with an existing context factory.

Source

pub fn with_max_history(self, max: usize) -> Self

Sets the maximum number of history messages to include as a fallback limit.

This limit applies before token-based trimming. Default: 50.

Source

pub fn with_max_history_tokens(self, max: usize) -> Self

Sets the maximum token budget for historical messages before trimming.

Older messages are dropped first when the budget is exceeded. Default: 64,000 tokens.

Source

pub fn with_compaction_filter(self, enable: bool) -> Self

Enables or disables the compaction message filter.

When enabled, compacted message pairs are replaced with a synthetic checkpoint system message. Enabled by default.

Source

pub fn with_cache_strategy(self, config: PromptCacheConfig) -> Self

Sets the prompt cache strategy used to auto-place cache breakpoints.

When cache_strategy.enabled is true and cache_strategy.auto_breakpoints is true, the pipeline places up to three cache markers on each ChatRequest:

  1. The end of the last system message (if it has at least min_system_tokens tokens).
  2. The end of the conversation tail (the last message before the current user turn).
  3. Each tool definition in the request.
Source

pub fn register<A>(&mut self, adapter: A)
where A: ContextAdapter + 'static,

Registers a context adapter.

Source

pub fn register_arc(&mut self, adapter: Arc<dyn ContextAdapter>)

Registers an already shared context adapter.

Source

pub fn adapter_names(&self) -> impl Iterator<Item = &str>

Returns adapter names in registration order.

Source

pub async fn build( &self, store: &RuntimeStore, session_id: Uuid, model: ModelName, user_message: Option<&str>, tools: Option<&[ToolSpec]>, ) -> RuntimeResult<ChatRequest>

Builds a ChatRequest from session history, registered adapters, and the current user message.

The pipeline loads session messages, applies compaction filtering, token-budget trimming, runs registered adapters, and assembles the final request with optional tool specs.

§Errors

Returns RuntimeError on adapter failure or storage errors.

Source

pub async fn build_context( &self, store: &RuntimeStore, session_id: Uuid, user_message: Option<&str>, ) -> RuntimeResult<ContextOutput>

Builds context output without creating a chat request.

Equivalent to build but returns the raw ContextOutput instead of wrapping it in a ChatRequest. Useful when the caller needs access to the composed message list without binding to a specific model or tools.

§Errors

Returns RuntimeError on adapter failure or storage errors.

Trait Implementations§

Source§

impl Default for ContextPipeline

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more