pub struct StateStore { /* private fields */ }Expand description
Key-value storage for application state outside the conversation context (StateStore).
State is not sent to the model; tools and application code read and write it. Values are JSON,
so a store can be persisted in snapshots and sessions. A StateStore is a cheap handle: clones
share the same state, and StateStore::snapshot makes an independent copy.
use strands::StateStore;
let state = StateStore::new();
state.set("userId", "user-123").unwrap();
let user_id: Option<String> = state.get("userId").unwrap();
assert_eq!(user_id.as_deref(), Some("user-123"));Implementations§
Source§impl StateStore
impl StateStore
Sourcepub fn get<T: DeserializeOwned>(&self, key: &str) -> Result<Option<T>>
pub fn get<T: DeserializeOwned>(&self, key: &str) -> Result<Option<T>>
Reads a value and deserializes it into T; Ok(None) when the key is absent.
§Errors
Returns an error when the stored value does not deserialize into T.
Sourcepub fn get_value(&self, key: &str) -> Option<JsonValue>
pub fn get_value(&self, key: &str) -> Option<JsonValue>
A copy of the raw JSON value stored under key.
Sourcepub fn set(&self, key: impl Into<String>, value: impl Serialize) -> Result<()>
pub fn set(&self, key: impl Into<String>, value: impl Serialize) -> Result<()>
Serializes value and stores it under key, replacing any previous value.
§Errors
Returns a JsonValidationError when value cannot be serialized to JSON.
Sourcepub fn update<T, F>(&self, key: impl Into<String>, f: F) -> Result<()>
pub fn update<T, F>(&self, key: impl Into<String>, f: F) -> Result<()>
Reads, changes and writes back the value under key in one step, holding the store’s lock
throughout, so concurrent tools that update the same key do not lose each other’s writes.
f receives None when the key is absent; keep it short, since it blocks every other
access to the store.
use strands::StateStore;
let state = StateStore::new();
state.update("count", |count: Option<u32>| count.unwrap_or(0) + 1).unwrap();
state.update("count", |count: Option<u32>| count.unwrap_or(0) + 1).unwrap();
assert_eq!(state.get::<u32>("count").unwrap(), Some(2));§Errors
Returns an error when the stored value does not deserialize into T, or the new value
cannot be serialized; the stored value is then unchanged.
Sourcepub fn get_all(&self) -> JsonObject
pub fn get_all(&self) -> JsonObject
A copy of the whole state.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for StateStore
impl RefUnwindSafe for StateStore
impl Send for StateStore
impl Sync for StateStore
impl Unpin for StateStore
impl UnsafeUnpin for StateStore
impl UnwindSafe for StateStore
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