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
impl AgentMemory
Sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Create memory with pre-allocated capacity.
Sourcepub fn get(&self, key: &str) -> Option<Value>
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.
Sourcepub fn get_as<T: for<'de> Deserialize<'de>>(&self, key: &str) -> Option<T>
pub fn get_as<T: for<'de> Deserialize<'de>>(&self, key: &str) -> Option<T>
Get a typed value from memory.
Sourcepub fn set_value<T: Serialize>(&self, key: impl Into<String>, value: &T)
pub fn set_value<T: Serialize>(&self, key: impl Into<String>, value: &T)
Set a typed value in memory.
Sourcepub fn update<F>(&self, key: impl Into<String>, f: F)
pub fn update<F>(&self, key: impl Into<String>, f: F)
Update a value atomically using a closure.
The closure receives the current value (if any) and returns the new value.
Sourcepub fn get_or_insert(&self, key: impl Into<String>, default: Value) -> Value
pub fn get_or_insert(&self, key: impl Into<String>, default: Value) -> Value
Get or insert a value.
Sourcepub fn add_visited_url(&self, url: impl Into<String>)
pub fn add_visited_url(&self, url: impl Into<String>)
Record a visited URL.
Keeps the most recent URLs up to the limit.
Sourcepub fn visited_urls(&self) -> Vec<String>
pub fn visited_urls(&self) -> Vec<String>
Get the list of visited URLs.
Sourcepub fn recent_urls(&self, n: usize) -> Vec<String>
pub fn recent_urls(&self, n: usize) -> Vec<String>
Get the last N visited URLs.
Sourcepub fn has_visited(&self, url: &str) -> bool
pub fn has_visited(&self, url: &str) -> bool
Check if a URL has been visited.
Sourcepub fn clear_urls(&self)
pub fn clear_urls(&self)
Clear URL history.
Sourcepub fn add_action(&self, action: impl Into<String>)
pub fn add_action(&self, action: impl Into<String>)
Record an action summary.
Keeps the most recent actions up to the limit.
Sourcepub fn action_history(&self) -> Vec<String>
pub fn action_history(&self) -> Vec<String>
Get the list of actions.
Sourcepub fn recent_actions(&self, n: usize) -> Vec<String>
pub fn recent_actions(&self, n: usize) -> Vec<String>
Get the last N actions.
Sourcepub fn clear_actions(&self)
pub fn clear_actions(&self)
Clear action history.
Sourcepub fn add_extraction(&self, data: Value)
pub fn add_extraction(&self, data: Value)
Add an extracted value to history.
Keeps the most recent extractions up to the limit.
Sourcepub fn extractions(&self) -> Vec<Value>
pub fn extractions(&self) -> Vec<Value>
Get all extractions.
Sourcepub fn recent_extractions(&self, n: usize) -> Vec<Value>
pub fn recent_extractions(&self, n: usize) -> Vec<Value>
Get the last N extractions.
Sourcepub fn clear_extractions(&self)
pub fn clear_extractions(&self)
Clear extraction history.
Sourcepub fn clear_history(&self)
pub fn clear_history(&self)
Clear all history (URLs, actions, extractions) but keep key-value store.
Sourcepub fn is_all_empty(&self) -> bool
pub fn is_all_empty(&self) -> bool
Check if all memory is empty (store + all history).
Sourcepub fn to_context_string(&self) -> String
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
impl Clone for AgentMemory
Source§fn clone(&self) -> AgentMemory
fn clone(&self) -> AgentMemory
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more