use std::collections::VecDeque;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AgentState {
pub short_term: VecDeque<MemoryEntry>,
pub short_term_capacity: usize,
pub long_term: Vec<MemoryEntry>,
pub phase: AgentPhase,
pub last_active: DateTime<Utc>,
pub message_count: u64,
pub token_usage: u64,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MemoryEntry {
pub timestamp: DateTime<Utc>,
pub role: String,
pub content: String,
pub metadata: Option<serde_json::Value>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum AgentPhase {
Idle,
Thinking,
Executing,
AwaitingHuman,
Streaming,
Completed,
Failed,
}
impl AgentState {
pub fn new(short_term_capacity: usize) -> Self {
Self {
short_term: VecDeque::with_capacity(short_term_capacity),
short_term_capacity,
long_term: Vec::new(),
phase: AgentPhase::Idle,
last_active: Utc::now(),
message_count: 0,
token_usage: 0,
}
}
pub fn push_short_term(&mut self, entry: MemoryEntry) {
if self.short_term.len() >= self.short_term_capacity {
self.short_term.pop_front();
}
self.short_term.push_back(entry);
}
pub fn push_long_term(&mut self, entry: MemoryEntry) {
self.long_term.push(entry);
}
pub fn recent_memories(&self, n: usize) -> Vec<&MemoryEntry> {
self.short_term.iter().rev().take(n).collect()
}
pub fn touch(&mut self) {
self.last_active = Utc::now();
}
pub fn add_token_usage(&mut self, tokens: u64) {
self.token_usage += tokens;
}
}
impl Default for AgentState {
fn default() -> Self {
Self::new(50)
}
}