pub struct InvocationState { /* private fields */ }Expand description
Per-invocation state threaded through hooks and tools for one agent invocation
(InvocationState).
A typed map that holds one value per type, following http::Extensions, so callers can pass
live values (clients, pools, request ids) without serializing or downcasting them. Clones share
the same map: a value one hook inserts is visible to later hooks, tools and
AgentResult::invocation_state.
The state belongs to the caller; the SDK never stores its own data in it. It is never serialized.
use strands::InvocationState;
#[derive(Clone, Debug, PartialEq)]
struct RequestId(String);
let state = InvocationState::new();
let shared = state.clone();
shared.insert(RequestId("req-1".into()));
assert_eq!(state.get::<RequestId>(), Some(RequestId("req-1".into())));Implementations§
Source§impl InvocationState
impl InvocationState
Sourcepub fn insert<T: Clone + Send + Sync + 'static>(&self, value: T) -> Option<T>
pub fn insert<T: Clone + Send + Sync + 'static>(&self, value: T) -> Option<T>
Stores value, returning the previous value of the same type.
Sourcepub fn get<T: Clone + Send + Sync + 'static>(&self) -> Option<T>
pub fn get<T: Clone + Send + Sync + 'static>(&self) -> Option<T>
Returns a clone of the value of type T.
Sourcepub fn contains<T: Clone + Send + Sync + 'static>(&self) -> bool
pub fn contains<T: Clone + Send + Sync + 'static>(&self) -> bool
Whether a value of type T is stored.
Sourcepub fn update<T, R>(&self, update: impl FnOnce(&mut T) -> R) -> R
pub fn update<T, R>(&self, update: impl FnOnce(&mut T) -> R) -> R
Updates the value of type T in place, inserting T::default() first when it is absent,
and returns what update returns. The map stays locked while update runs, so concurrent
tools see each other’s updates.
Trait Implementations§
Source§impl Clone for InvocationState
impl Clone for InvocationState
Source§impl Debug for InvocationState
impl Debug for InvocationState
Auto Trait Implementations§
impl Freeze for InvocationState
impl RefUnwindSafe for InvocationState
impl Send for InvocationState
impl Sync for InvocationState
impl Unpin for InvocationState
impl UnsafeUnpin for InvocationState
impl UnwindSafe for InvocationState
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> FutureExt for T
impl<T> FutureExt for T
§fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
§fn with_current_context(self) -> WithContext<Self> ⓘ
fn with_current_context(self) -> WithContext<Self> ⓘ
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> 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