Skip to main content

ToolContext

Struct ToolContext 

Source
pub struct ToolContext { /* private fields */ }
Expand description

Context passed to Rust tools during dispatch, mirroring the Python SDK’s ToolContext.

Provides access to the current conversation ID, a shared key-value state store that persists across tool calls within the same agent turn, and an idle flag.

The state is backed by Arc<Mutex<HashMap>> so it can be cheaply cloned and shared across concurrent tool invocations.

§get_state vs set_state error handling

These two methods intentionally handle mutex poisoning differently:

  • get_state returns the caller-supplied default when the lock is poisoned. Reads are best-effort — a missing value is indistinguishable from a default, so returning default keeps the tool running without surfacing infrastructure errors to the model.

  • set_state returns Err when the lock is poisoned. Writes that silently vanish can cause subtle logic bugs, so callers must handle the failure explicitly.

Implementations§

Source§

impl ToolContext

Source

pub fn new(conversation_id: Option<String>) -> Self

Create a new context with the given conversation ID.

Source

pub fn with_shared_state( conversation_id: Option<String>, state: Arc<Mutex<HashMap<String, Value>>>, ) -> Self

Create a context that shares an externally-provided state map.

Use this when multiple ToolContext instances (e.g. successive tool calls within the same agent) must read/write the same state store.

Source

pub fn conversation_id(&self) -> Option<&str>

Return the conversation ID, if one has been set.

Source

pub fn get_state(&self, key: &str, default: Value) -> Value

Retrieve a value from the shared state, returning default if the key is absent or the lock is poisoned.

This method never fails — on a poisoned lock it logs a warning and returns default. See the struct-level docs for rationale.

Source

pub fn set_state(&self, key: &str, value: Value) -> Result<(), ToolError>

Insert or update a value in the shared state.

Unlike get_state, this method returns Err on a poisoned lock because silently dropping a write can cause subtle bugs. See the struct-level docs for rationale.

§Errors

Returns ToolError if the mutex is poisoned.

Source

pub const fn is_idle(&self) -> bool

Whether the agent is currently idle.

Auto Trait Implementations§

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