pub struct ContextItem {
pub source: ContextSourceKind,
pub source_id: String,
pub rank: usize,
pub score: f64,
pub text: String,
pub estimated_chars: usize,
pub provenance: Value,
pub metadata: Value,
}Expand description
One ranked piece of context that may be packed into a bounded model window.
ContextItem is intentionally backend-neutral. Memory crates, MCP/resource
adapters, and harnesses can all project their native records into this shape
so tests can assert what context was selected, omitted, and rendered.
use rig_compose::{ContextItem, ContextSourceKind};
let item = ContextItem::new(
ContextSourceKind::Memory,
"profile/alice/location",
"fact alice lives in Berlin",
)
.with_rank(0)
.with_score(9.5);
assert_eq!(item.estimated_chars, item.text.chars().count());Fields§
§source: ContextSourceKindBackend-neutral source category.
source_id: StringStable id inside the source system.
rank: usizeZero-based rank after source-local selection.
score: f64Relevance score used for ordering within the source or planner.
text: StringPrompt-ready text.
estimated_chars: usizeCharacter count estimate for early context packing.
provenance: ValueSource-specific provenance such as frame id, URI, tool call id, or path.
metadata: ValueCaller-defined metadata not required for packing.
Implementations§
Source§impl ContextItem
impl ContextItem
Sourcepub fn new(
source: ContextSourceKind,
source_id: impl Into<String>,
text: impl Into<String>,
) -> Self
pub fn new( source: ContextSourceKind, source_id: impl Into<String>, text: impl Into<String>, ) -> Self
Build a context item with a source, source id, and prompt-ready text.
Sourcepub fn with_rank(self, rank: usize) -> Self
pub fn with_rank(self, rank: usize) -> Self
Set the source-local rank used by ContextPack::pack.
Sourcepub fn with_score(self, score: f64) -> Self
pub fn with_score(self, score: f64) -> Self
Set the relevance score attached by the source or planner.
Sourcepub fn with_estimated_chars(self, estimated_chars: usize) -> Self
pub fn with_estimated_chars(self, estimated_chars: usize) -> Self
Override the character estimate when a caller has a better tokenizer or sizing approximation.
Sourcepub fn with_provenance(self, provenance: Value) -> Self
pub fn with_provenance(self, provenance: Value) -> Self
Attach source-specific provenance.
Sourcepub fn with_metadata(self, metadata: Value) -> Self
pub fn with_metadata(self, metadata: Value) -> Self
Attach caller-defined metadata.
Trait Implementations§
Source§impl Clone for ContextItem
impl Clone for ContextItem
Source§fn clone(&self) -> ContextItem
fn clone(&self) -> ContextItem
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for ContextItem
impl Debug for ContextItem
Source§impl<'de> Deserialize<'de> for ContextItem
impl<'de> Deserialize<'de> for ContextItem
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl PartialEq for ContextItem
impl PartialEq for ContextItem
Source§fn eq(&self, other: &ContextItem) -> bool
fn eq(&self, other: &ContextItem) -> bool
self and other values to be equal, and is used by ==.