Skip to main content

TransformContext

Struct TransformContext 

Source
pub struct TransformContext<'a> {
    pub signal: &'a CancellationToken,
    pub model_id: &'a str,
    pub iteration: usize,
    pub last_provider_usage: Option<&'a Usage>,
    pub estimator: &'a dyn TokenEstimator,
}
Expand description

Read-only context handed to a ContextTransform hook.

Carries the cancellation signal plus a few cheap observables that transforms key on (model identity, iteration index, last-turn token usage, the loop’s configured token estimator). Gathering these on the hook context — rather than widening the trait one parameter at a time — keeps the trait stable as later compaction layers (per-tool-result cap, cache-aware microcompact, auto-compact) come online.

New fields are additive: transforms that don’t care can ignore them.

Fields§

§signal: &'a CancellationToken

Cancellation signal for the current run.

§model_id: &'a str

Model identifier the run is targeting (e.g. provider/model). May be empty when the host runtime doesn’t surface one — tests, fixture-replay transports, etc. Plugins that key per-model behavior should treat empty as “unknown”.

§iteration: usize

Zero-indexed iteration within the current run. Same semantics as ToolGateContext::iteration: the very first LLM call of the run is 0.

§last_provider_usage: Option<&'a Usage>

Token usage reported by the provider on the most recent assistant turn that surfaced a Usage block. None on the very first turn or when the provider didn’t surface usage. Useful for cache-aware decisions (read cache_read_input_tokens to see if the prompt prefix actually hit cache last turn).

§estimator: &'a dyn TokenEstimator

Estimator the loop is configured with. Plugins use this to count tokens for budgeting and compaction without duplicating the loop’s tokenizer choice.

Implementations§

Source§

impl<'a> TransformContext<'a>

Source

pub fn for_test(signal: &'a CancellationToken) -> Self

Convenience constructor for tests and ad-hoc callers that don’t have a model id, iteration counter, or usage data. Picks the default char-heuristic estimator.

Auto Trait Implementations§

§

impl<'a> Freeze for TransformContext<'a>

§

impl<'a> !RefUnwindSafe for TransformContext<'a>

§

impl<'a> Send for TransformContext<'a>

§

impl<'a> Sync for TransformContext<'a>

§

impl<'a> Unpin for TransformContext<'a>

§

impl<'a> UnsafeUnpin for TransformContext<'a>

§

impl<'a> !UnwindSafe for TransformContext<'a>

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<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, 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