Skip to main content

AgentMemory

Struct AgentMemory 

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

Session memory for storing state across operations.

Uses DashMap internally for lock-free concurrent reads and writes. This is optimal for high-concurrency scenarios.

§Example

use spider_agent::AgentMemory;

let memory = AgentMemory::new();

// Key-value storage
memory.set("user_id", serde_json::json!("12345"));

// URL tracking
memory.add_visited_url("https://example.com");

// Action history
memory.add_action("Searched for 'rust frameworks'");

// Extraction history
memory.add_extraction(serde_json::json!({"title": "Example"}));

// Generate context for LLM
let context = memory.to_context_string();

Implementations§

Source§

impl AgentMemory

Source

pub fn new() -> Self

Create a new empty memory.

Source

pub fn with_capacity(capacity: usize) -> Self

Create memory with pre-allocated capacity.

Source

pub fn get(&self, key: &str) -> Option<Value>

Get a value from memory.

Returns a clone of the value to avoid holding refs across await points.

Source

pub fn set(&self, key: impl Into<String>, value: Value)

Set a value in memory.

Source

pub fn remove(&self, key: &str) -> Option<Value>

Remove a value from memory.

Source

pub fn clear(&self)

Clear all key-value data.

Source

pub fn contains(&self, key: &str) -> bool

Check if memory contains a key.

Source

pub fn len(&self) -> usize

Get number of key-value entries.

Source

pub fn is_empty(&self) -> bool

Check if key-value store is empty.

Source

pub fn get_as<T: for<'de> Deserialize<'de>>(&self, key: &str) -> Option<T>

Get a typed value from memory.

Source

pub fn set_value<T: Serialize>(&self, key: impl Into<String>, value: &T)

Set a typed value in memory.

Source

pub fn update<F>(&self, key: impl Into<String>, f: F)
where F: FnOnce(Option<&Value>) -> Value,

Update a value atomically using a closure.

The closure receives the current value (if any) and returns the new value.

Source

pub fn get_or_insert(&self, key: impl Into<String>, default: Value) -> Value

Get or insert a value.

Source

pub fn add_visited_url(&self, url: impl Into<String>)

Record a visited URL.

Keeps the most recent URLs up to the limit.

Source

pub fn visited_urls(&self) -> Vec<String>

Get the list of visited URLs.

Source

pub fn recent_urls(&self, n: usize) -> Vec<String>

Get the last N visited URLs.

Source

pub fn has_visited(&self, url: &str) -> bool

Check if a URL has been visited.

Source

pub fn clear_urls(&self)

Clear URL history.

Source

pub fn add_action(&self, action: impl Into<String>)

Record an action summary.

Keeps the most recent actions up to the limit.

Source

pub fn action_history(&self) -> Vec<String>

Get the list of actions.

Source

pub fn recent_actions(&self, n: usize) -> Vec<String>

Get the last N actions.

Source

pub fn clear_actions(&self)

Clear action history.

Source

pub fn add_extraction(&self, data: Value)

Add an extracted value to history.

Keeps the most recent extractions up to the limit.

Source

pub fn extractions(&self) -> Vec<Value>

Get all extractions.

Source

pub fn recent_extractions(&self, n: usize) -> Vec<Value>

Get the last N extractions.

Source

pub fn clear_extractions(&self)

Clear extraction history.

Source

pub fn clear_history(&self)

Clear all history (URLs, actions, extractions) but keep key-value store.

Source

pub fn clear_all(&self)

Clear everything including key-value store and all history.

Source

pub fn is_all_empty(&self) -> bool

Check if all memory is empty (store + all history).

Source

pub fn to_context_string(&self) -> String

Generate a context string for inclusion in LLM prompts.

This provides the LLM with session context including:

  • Key-value store contents
  • Recent URLs visited
  • Recent actions taken
  • Recent extractions

Trait Implementations§

Source§

impl Clone for AgentMemory

Source§

fn clone(&self) -> AgentMemory

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for AgentMemory

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for AgentMemory

Source§

fn default() -> AgentMemory

Returns the “default value” for a type. Read more

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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