pub struct C;Expand description
The C namespace — static factory methods for context policies.
Implementations§
Source§impl C
impl C
Sourcepub fn window(n: usize) -> ContextPolicy
pub fn window(n: usize) -> ContextPolicy
Keep only the last n messages.
Sourcepub fn user_only() -> ContextPolicy
pub fn user_only() -> ContextPolicy
Keep only messages with role “user”.
Sourcepub fn custom(
f: impl Fn(&[Content]) -> Vec<Content> + Send + Sync + 'static,
) -> ContextPolicy
pub fn custom( f: impl Fn(&[Content]) -> Vec<Content> + Send + Sync + 'static, ) -> ContextPolicy
Apply a custom filter function.
Sourcepub fn model_only() -> ContextPolicy
pub fn model_only() -> ContextPolicy
Keep only messages with role “model”.
Sourcepub fn head(n: usize) -> ContextPolicy
pub fn head(n: usize) -> ContextPolicy
Keep the first n messages (head).
Sourcepub fn sample(n: usize) -> ContextPolicy
pub fn sample(n: usize) -> ContextPolicy
Keep every n-th message (sampling).
Sourcepub fn exclude_tools() -> ContextPolicy
pub fn exclude_tools() -> ContextPolicy
Exclude messages that contain tool-related parts (function calls/responses).
Sourcepub fn prepend(content: Content) -> ContextPolicy
pub fn prepend(content: Content) -> ContextPolicy
Prepend a system message to the context.
Sourcepub fn append(content: Content) -> ContextPolicy
pub fn append(content: Content) -> ContextPolicy
Append a content to the context.
Sourcepub fn text_only() -> ContextPolicy
pub fn text_only() -> ContextPolicy
Keep only messages that contain text parts.
Sourcepub fn filter(
f: impl Fn(&Content) -> bool + Send + Sync + 'static,
) -> ContextPolicy
pub fn filter( f: impl Fn(&Content) -> bool + Send + Sync + 'static, ) -> ContextPolicy
Filter messages by a predicate on Content.
Sourcepub fn map(
f: impl Fn(&Content) -> Content + Send + Sync + 'static,
) -> ContextPolicy
pub fn map( f: impl Fn(&Content) -> Content + Send + Sync + 'static, ) -> ContextPolicy
Map/transform each message in the context.
Sourcepub fn truncate(max_chars: usize) -> ContextPolicy
pub fn truncate(max_chars: usize) -> ContextPolicy
Truncate context to approximately max_chars total characters of text.
Sourcepub fn last(n: usize) -> ContextPolicy
pub fn last(n: usize) -> ContextPolicy
Keep messages within a time window (by index offset from end).
Alias for window — provided for API symmetry.
Sourcepub fn empty() -> ContextPolicy
pub fn empty() -> ContextPolicy
Return an empty context (useful for isolated agents).
Sourcepub fn from_state(keys: &[&str]) -> ContextPolicy
pub fn from_state(keys: &[&str]) -> ContextPolicy
Inject state values as context preamble.
Bridges Channel 2 (State) → Channel 1 (Conversation History) by prepending formatted state values as a system context message.
§Example
C::from_state(&["user:name", "app:account_balance", "derived:risk"])
// Produces: "[Context: name=John, account_balance=$5230, risk=0.72]"Sourcepub fn none() -> ContextPolicy
pub fn none() -> ContextPolicy
Alias for empty — matches upstream Python C.none().
Sourcepub fn recent(n: usize) -> ContextPolicy
pub fn recent(n: usize) -> ContextPolicy
Alias for window — matches upstream Python C.recent().
Sourcepub fn template(tpl: &str) -> ContextPolicy
pub fn template(tpl: &str) -> ContextPolicy
Template-based context injection with {key} placeholders.
Replaces placeholders in the template with state key references.
Sourcepub fn when(
predicate: impl Fn() -> bool + Send + Sync + 'static,
inner: ContextPolicy,
) -> ContextPolicy
pub fn when( predicate: impl Fn() -> bool + Send + Sync + 'static, inner: ContextPolicy, ) -> ContextPolicy
Conditional context — applies inner policy only when predicate is true.
Falls back to passing history through unchanged.
Sourcepub fn rolling(n: usize) -> ContextPolicy
pub fn rolling(n: usize) -> ContextPolicy
Rolling window — keeps last N messages (alias with summarization hint).
Sourcepub fn compact() -> ContextPolicy
pub fn compact() -> ContextPolicy
Compact context — removes tool call/response parts to reduce token usage.
Sourcepub fn budget(max_tokens: usize) -> ContextPolicy
pub fn budget(max_tokens: usize) -> ContextPolicy
Budget context — truncate to approximate token count.
Rough estimate: 4 chars per token.
Sourcepub fn fresh(max_entries: usize) -> ContextPolicy
pub fn fresh(max_entries: usize) -> ContextPolicy
Freshness filter — keep only messages within the last N entries.
Sourcepub fn redact(patterns: &[&str]) -> ContextPolicy
pub fn redact(patterns: &[&str]) -> ContextPolicy
Redact patterns from context messages.
Sourcepub fn summarize(prompt: &str) -> ContextPolicy
pub fn summarize(prompt: &str) -> ContextPolicy
LLM-powered context summarization.
Stores a summarization prompt that the runtime uses to condense context via an LLM call before passing it to the agent. The policy prepends a marker so the runtime knows summarization is requested.
§Example
C::summarize("Summarize the conversation focusing on action items")Sourcepub fn relevant(query_key: &str) -> ContextPolicy
pub fn relevant(query_key: &str) -> ContextPolicy
Sourcepub fn extract(keys: &[&str]) -> ContextPolicy
pub fn extract(keys: &[&str]) -> ContextPolicy
Sourcepub fn distill(instruction: &str) -> ContextPolicy
pub fn distill(instruction: &str) -> ContextPolicy
Sourcepub fn priority(weights: &[(&str, f64)]) -> ContextPolicy
pub fn priority(weights: &[(&str, f64)]) -> ContextPolicy
Priority-weighted context selection.
Assigns weights to context entries by role or content pattern. Higher weight entries are kept preferentially when context must be truncated. Weights are encoded as a marker for runtime processing.
§Example
C::priority(&[("user", 1.0), ("model", 0.5), ("tool", 0.2)])Sourcepub fn fit(max_tokens: usize) -> ContextPolicy
pub fn fit(max_tokens: usize) -> ContextPolicy
Sourcepub fn project(fields: &[&str]) -> ContextPolicy
pub fn project(fields: &[&str]) -> ContextPolicy
Sourcepub fn select(
predicate: impl Fn(&Content) -> bool + Send + Sync + 'static,
) -> ContextPolicy
pub fn select( predicate: impl Fn(&Content) -> bool + Send + Sync + 'static, ) -> ContextPolicy
Select context entries matching a predicate.
Similar to filter but with select semantics: entries
matching the predicate are included (positive selection).
Sourcepub fn from_agents(names: &[&str]) -> ContextPolicy
pub fn from_agents(names: &[&str]) -> ContextPolicy
Sourcepub fn exclude_agents(names: &[&str]) -> ContextPolicy
pub fn exclude_agents(names: &[&str]) -> ContextPolicy
Sourcepub fn notes(key: &str) -> ContextPolicy
pub fn notes(key: &str) -> ContextPolicy
Sourcepub fn pipeline_aware() -> ContextPolicy
pub fn pipeline_aware() -> ContextPolicy
Sourcepub fn dedup() -> ContextPolicy
pub fn dedup() -> ContextPolicy
Deduplicate adjacent messages with identical text content.