pub struct ToolContext { /* private fields */ }Expand description
Context passed to every tool execution.
Callers insert typed inbound values with insert. Tools read
those values with get or require, and attach
host-only result metadata with insert_result. Result
hooks inspect that metadata through result. Neither inbound
values nor result metadata are sent to the model.
Every value is data: inserting serializes it, reading deserializes it,
and the whole context is itself Serialize + Deserialize. Explicit scene
serialization can therefore contain sensitive inbound values and must be
protected by the host. Effect logs record only ToolResultContext:
values explicitly published through insert_result, never inbound slots
or runtime scopes. Replay uses the current dispatch’s inbound context;
code must not treat mutations to inbound slots as durable output. Values with
interior sharing (Arc<Mutex<_>>, atomics, channels) do not belong here;
they belong to the tool instance.
Registry, server, and agent dispatch clone inbound values once per call. Map-level mutations (inserting, replacing, or removing typed slots) affect only that execution. The dispatch surface returns result metadata without replacing the caller’s inbound slots.
Slots use the value type’s declared ContextValue::KEY. Accessors need
no key argument; types must use distinct keys to occupy distinct slots.
Implementations§
Source§impl ToolContext
impl ToolContext
Sourcepub fn insert<T: ContextValue>(
&mut self,
value: T,
) -> Result<Option<T>, ToolContextError>
pub fn insert<T: ContextValue>( &mut self, value: T, ) -> Result<Option<T>, ToolContextError>
Insert an inbound typed value, returning the displaced value if it decodes
as T. A differently shaped displaced value is replaced successfully and
returns None; decoding the previous value cannot undo a successful write.
Returns an encoding error if serialization fails, leaving the slot unchanged.
Sourcepub fn get<T: ContextValue>(&self) -> Result<Option<T>, ToolContextError>
pub fn get<T: ContextValue>(&self) -> Result<Option<T>, ToolContextError>
Read an inbound typed value: Ok(None) when absent, a decode error when
the stored value does not match T.
Sourcepub fn require<T: ContextValue>(&self) -> Result<T, ToolContextError>
pub fn require<T: ContextValue>(&self) -> Result<T, ToolContextError>
Require an inbound typed value.
Sourcepub fn remove<T: ContextValue>(&mut self) -> Result<Option<T>, ToolContextError>
pub fn remove<T: ContextValue>(&mut self) -> Result<Option<T>, ToolContextError>
Remove an inbound typed value. The slot is removed even if decoding its former value fails.
Sourcepub fn insert_result<T: ContextValue>(
&mut self,
value: T,
) -> Result<Option<T>, ToolContextError>
pub fn insert_result<T: ContextValue>( &mut self, value: T, ) -> Result<Option<T>, ToolContextError>
Attach host-only metadata to this execution’s result.
Sourcepub fn result<T: ContextValue>(&self) -> Result<Option<T>, ToolContextError>
pub fn result<T: ContextValue>(&self) -> Result<Option<T>, ToolContextError>
Read host-only result metadata.
Sourcepub fn require_result<T: ContextValue>(&self) -> Result<T, ToolContextError>
pub fn require_result<T: ContextValue>(&self) -> Result<T, ToolContextError>
Require host-only result metadata.
Sourcepub fn contains<T: ContextValue>(&self) -> bool
pub fn contains<T: ContextValue>(&self) -> bool
Whether this context contains the inbound type T.
Sourcepub fn for_dispatch(&self) -> Self
pub fn for_dispatch(&self) -> Self
Build the snapshot one dispatch runs against: the same inbound values and no result metadata. Runtimes call this per tool call so map-level mutations inside the call cannot leak into the caller’s context.
Sourcepub fn with_scope(self, scope: Arc<dyn Any + Send + Sync>) -> Self
pub fn with_scope(self, scope: Arc<dyn Any + Send + Sync>) -> Self
Attach one of the driver’s scopes for the call (see the field).
Sourcepub fn with_scopes(self, scopes: Vec<Arc<dyn Any + Send + Sync>>) -> Self
pub fn with_scopes(self, scopes: Vec<Arc<dyn Any + Send + Sync>>) -> Self
Attach the driver’s scopes for the call, as the sink carries them.
Sourcepub fn scope<T: Any + Send + Sync>(&self) -> Option<Arc<T>>
pub fn scope<T: Any + Send + Sync>(&self) -> Option<Arc<T>>
Returns the first attached scope of type T, or None if absent.
Sourcepub fn clear_scope(&mut self)
pub fn clear_scope(&mut self)
Drop the scopes: the call is over and the context is data again.
Sourcepub fn accept_dispatch_result(&mut self, dispatched: Self)
pub fn accept_dispatch_result(&mut self, dispatched: Self)
Publish the result metadata a dispatch produced (see
Self::for_dispatch) while keeping the caller’s inbound values.
Sourcepub fn result_context(&self) -> ToolResultContext
pub fn result_context(&self) -> ToolResultContext
Snapshot only the values explicitly published with insert_result.
Sourcepub fn with_result_context(self, result: ToolResultContext) -> Self
pub fn with_result_context(self, result: ToolResultContext) -> Self
Restore durable output while preserving this dispatch’s inbound values.
Sourcepub fn clear_dispatch_result(&mut self)
pub fn clear_dispatch_result(&mut self)
Drop the previous dispatch’s result metadata before starting another.
Trait Implementations§
Source§impl Clone for ToolContext
impl Clone for ToolContext
Source§impl Debug for ToolContext
impl Debug for ToolContext
Source§impl Default for ToolContext
impl Default for ToolContext
Source§impl<'de> Deserialize<'de> for ToolContext
impl<'de> Deserialize<'de> for ToolContext
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>,
impl Eq for ToolContext
Source§impl PartialEq for ToolContext
impl PartialEq for ToolContext
Auto Trait Implementations§
impl !RefUnwindSafe for ToolContext
impl !UnwindSafe for ToolContext
impl Freeze for ToolContext
impl Send for ToolContext
impl Sync for ToolContext
impl Unpin for ToolContext
impl UnsafeUnpin for ToolContext
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> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.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