pub struct ToolCallExtensions { /* private fields */ }Expand description
Per-call runtime extensions supplied to a tool by its caller.
A type-map that allows callers to attach arbitrary typed values and tools to extract them. Tools that don’t need any extensions ignore them.
Inspired by http::Extensions.
Backed by the shared internal TypeMap, so empty extensions (the common case when no
caller-provided values are needed) require zero allocation.
Tools receive these by shared reference, so a tool reads values with
get / require / contains;
the mutating accessors (insert, get_mut,
remove) are owner-side, used by the caller that builds the
extensions before a run.
Values are keyed by TypeId, so get returns None both
when nothing was inserted under that type and when a different type was
inserted. For tools that genuinely require a value, prefer
require, which returns a descriptive error instead of a
silent None.
The result-side counterpart — metadata a tool attaches to its output — is
ToolResultExtensions.
§Example
use rig_core::tool::ToolCallExtensions;
let mut extensions = ToolCallExtensions::new();
assert_eq!(extensions.insert(42u32), None); // no prior value
assert_eq!(extensions.get::<u32>(), Some(&42));
assert_eq!(extensions.insert(7u32), Some(42)); // returns the displaced valueImplementations§
Source§impl ToolCallExtensions
impl ToolCallExtensions
Sourcepub fn insert<T: Clone + WasmCompatSend + WasmCompatSync + 'static>(
&mut self,
val: T,
) -> Option<T>
pub fn insert<T: Clone + WasmCompatSend + WasmCompatSync + 'static>( &mut self, val: T, ) -> Option<T>
Insert a typed value, returning the previous value of the same type if
one was present (mirroring http::Extensions::insert and
HashMap::insert).
Sourcepub fn get<T: WasmCompatSend + WasmCompatSync + 'static>(&self) -> Option<&T>
pub fn get<T: WasmCompatSend + WasmCompatSync + 'static>(&self) -> Option<&T>
Get a reference to a value by type. Returns None if not present.
Sourcepub fn require<T: WasmCompatSend + WasmCompatSync + 'static>(
&self,
) -> Result<&T, MissingExtension>
pub fn require<T: WasmCompatSend + WasmCompatSync + 'static>( &self, ) -> Result<&T, MissingExtension>
Get a reference to a value by type, returning a descriptive error instead
of None when it is absent.
Prefer this over get for tools that require an extension
value (auth tokens, session IDs, …): the error names the missing type,
turning a silent None into an actionable failure.
Sourcepub fn get_mut<T: WasmCompatSend + WasmCompatSync + 'static>(
&mut self,
) -> Option<&mut T>
pub fn get_mut<T: WasmCompatSend + WasmCompatSync + 'static>( &mut self, ) -> Option<&mut T>
Get a mutable reference to a value by type. Returns None if not present.
Sourcepub fn remove<T: WasmCompatSend + WasmCompatSync + 'static>(
&mut self,
) -> Option<T>
pub fn remove<T: WasmCompatSend + WasmCompatSync + 'static>( &mut self, ) -> Option<T>
Remove a value by type, returning it if present.
Sourcepub fn contains<T: WasmCompatSend + WasmCompatSync + 'static>(&self) -> bool
pub fn contains<T: WasmCompatSend + WasmCompatSync + 'static>(&self) -> bool
Check if a value of the given type is present.
Trait Implementations§
Source§impl Clone for ToolCallExtensions
impl Clone for ToolCallExtensions
Source§fn clone(&self) -> ToolCallExtensions
fn clone(&self) -> ToolCallExtensions
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 ToolCallExtensions
impl Debug for ToolCallExtensions
Source§impl Default for ToolCallExtensions
impl Default for ToolCallExtensions
Source§fn default() -> ToolCallExtensions
fn default() -> ToolCallExtensions
Auto Trait Implementations§
impl !RefUnwindSafe for ToolCallExtensions
impl !UnwindSafe for ToolCallExtensions
impl Freeze for ToolCallExtensions
impl Send for ToolCallExtensions
impl Sync for ToolCallExtensions
impl Unpin for ToolCallExtensions
impl UnsafeUnpin for ToolCallExtensions
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneDebuggableStorage for Twhere
T: DebuggableStorage + Clone,
impl<T> CloneDebuggableStorage for Twhere
T: DebuggableStorage + Clone,
fn clone_storage(&self) -> Box<dyn CloneDebuggableStorage>
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> CloneableStorage for T
impl<T> CloneableStorage for T
fn clone_storage(&self) -> Box<dyn CloneableStorage>
impl<T> DebuggableStorage for T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more