use serde::{Deserialize, Serialize};
use std::any::{Any, TypeId};
use std::collections::HashMap;
use std::sync::Arc;
use uuid::Uuid;
#[derive(Clone, Debug)]
pub struct RunnableConfig {
thread_id: Option<String>,
checkpoint_id: Option<String>,
metadata: HashMap<String, String>,
context: HashMap<TypeId, Arc<dyn Any + Send + Sync>>,
tags: Vec<String>,
max_iterations: Option<usize>,
recursion_limit: usize,
}
impl RunnableConfig {
pub fn new() -> Self {
Self {
thread_id: None,
checkpoint_id: None,
metadata: HashMap::new(),
context: HashMap::new(),
tags: Vec::new(),
max_iterations: None,
recursion_limit: 25,
}
}
pub fn with_random_thread() -> Self {
Self::new().with_thread_id(Uuid::new_v4().to_string())
}
pub fn with_thread_id(mut self, thread_id: impl Into<String>) -> Self {
self.thread_id = Some(thread_id.into());
self
}
pub fn with_checkpoint_id(mut self, checkpoint_id: impl Into<String>) -> Self {
self.checkpoint_id = Some(checkpoint_id.into());
self
}
pub fn with_metadata(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
self.metadata.insert(key.into(), value.into());
self
}
pub fn with_metadata_map(mut self, metadata: HashMap<String, String>) -> Self {
self.metadata.extend(metadata);
self
}
pub fn with_context<T: Send + Sync + 'static>(mut self, value: T) -> Self {
self.context.insert(TypeId::of::<T>(), Arc::new(value));
self
}
pub fn with_tag(mut self, tag: impl Into<String>) -> Self {
self.tags.push(tag.into());
self
}
pub fn with_tags(mut self, tags: impl IntoIterator<Item = impl Into<String>>) -> Self {
self.tags.extend(tags.into_iter().map(Into::into));
self
}
pub fn with_max_iterations(mut self, max: usize) -> Self {
self.max_iterations = Some(max);
self
}
pub fn with_recursion_limit(mut self, limit: usize) -> Self {
self.recursion_limit = limit;
self
}
pub fn thread_id(&self) -> Option<&str> {
self.thread_id.as_deref()
}
pub fn checkpoint_id(&self) -> Option<&str> {
self.checkpoint_id.as_deref()
}
pub fn get_metadata(&self, key: &str) -> Option<&str> {
self.metadata.get(key).map(|s| s.as_str())
}
pub fn metadata(&self) -> &HashMap<String, String> {
&self.metadata
}
pub fn get_context<T: Send + Sync + 'static>(&self) -> Option<Arc<T>> {
self.context
.get(&TypeId::of::<T>())
.and_then(|v| v.clone().downcast::<T>().ok())
}
pub fn require_context<T: Send + Sync + 'static>(&self) -> crate::error::Result<Arc<T>> {
self.get_context::<T>().ok_or_else(|| {
crate::error::RegulaError::ConfigKeyNotFound(std::any::type_name::<T>().to_string())
})
}
pub fn has_tag(&self, tag: &str) -> bool {
self.tags.iter().any(|t| t == tag)
}
pub fn tags(&self) -> &[String] {
&self.tags
}
pub fn max_iterations(&self) -> Option<usize> {
self.max_iterations
}
pub fn recursion_limit(&self) -> usize {
self.recursion_limit
}
pub fn child(&self) -> crate::error::Result<Self> {
if self.recursion_limit == 0 {
return Err(crate::error::RegulaError::MaxIterationsExceeded(0));
}
Ok(Self {
thread_id: self.thread_id.clone(),
checkpoint_id: None, metadata: self.metadata.clone(),
context: self.context.clone(),
tags: self.tags.clone(),
max_iterations: self.max_iterations,
recursion_limit: self.recursion_limit - 1,
})
}
pub fn merge(mut self, other: Self) -> Self {
if other.thread_id.is_some() {
self.thread_id = other.thread_id;
}
if other.checkpoint_id.is_some() {
self.checkpoint_id = other.checkpoint_id;
}
self.metadata.extend(other.metadata);
self.context.extend(other.context);
self.tags.extend(other.tags);
if other.max_iterations.is_some() {
self.max_iterations = other.max_iterations;
}
self.recursion_limit = other.recursion_limit;
self
}
}
impl Default for RunnableConfig {
fn default() -> Self {
Self::new()
}
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct ConfigSnapshot {
pub thread_id: Option<String>,
pub checkpoint_id: Option<String>,
pub metadata: HashMap<String, String>,
pub tags: Vec<String>,
}
impl From<&RunnableConfig> for ConfigSnapshot {
fn from(config: &RunnableConfig) -> Self {
Self {
thread_id: config.thread_id.clone(),
checkpoint_id: config.checkpoint_id.clone(),
metadata: config.metadata.clone(),
tags: config.tags.clone(),
}
}
}
impl From<ConfigSnapshot> for RunnableConfig {
fn from(snapshot: ConfigSnapshot) -> Self {
Self {
thread_id: snapshot.thread_id,
checkpoint_id: snapshot.checkpoint_id,
metadata: snapshot.metadata,
context: HashMap::new(),
tags: snapshot.tags,
max_iterations: None,
recursion_limit: 25,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_config_new() {
let config = RunnableConfig::new();
assert!(config.thread_id().is_none());
assert!(config.metadata().is_empty());
}
#[test]
fn test_config_with_thread_id() {
let config = RunnableConfig::new().with_thread_id("thread-1");
assert_eq!(config.thread_id(), Some("thread-1"));
}
#[test]
fn test_config_with_metadata() {
let config = RunnableConfig::new()
.with_metadata("key1", "value1")
.with_metadata("key2", "value2");
assert_eq!(config.get_metadata("key1"), Some("value1"));
assert_eq!(config.get_metadata("key2"), Some("value2"));
assert_eq!(config.get_metadata("key3"), None);
}
#[test]
fn test_config_with_context() {
#[derive(Clone)]
struct MyContext {
value: i32,
}
let config = RunnableConfig::new().with_context(MyContext { value: 42 });
let ctx = config.get_context::<MyContext>().unwrap();
assert_eq!(ctx.value, 42);
}
#[test]
fn test_config_require_context() {
let config = RunnableConfig::new().with_context(42i32);
let result = config.require_context::<i32>();
assert!(result.is_ok());
assert_eq!(*result.unwrap(), 42);
let result = config.require_context::<String>();
assert!(result.is_err());
}
#[test]
fn test_config_with_tags() {
let config = RunnableConfig::new()
.with_tag("tag1")
.with_tags(vec!["tag2", "tag3"]);
assert!(config.has_tag("tag1"));
assert!(config.has_tag("tag2"));
assert!(config.has_tag("tag3"));
assert!(!config.has_tag("tag4"));
}
#[test]
fn test_config_child() {
let config = RunnableConfig::new()
.with_thread_id("thread-1")
.with_recursion_limit(5);
let child = config.child().unwrap();
assert_eq!(child.thread_id(), Some("thread-1"));
assert_eq!(child.recursion_limit(), 4);
}
#[test]
fn test_config_child_recursion_limit() {
let config = RunnableConfig::new().with_recursion_limit(0);
let result = config.child();
assert!(result.is_err());
}
#[test]
fn test_config_merge() {
let config1 = RunnableConfig::new()
.with_thread_id("thread-1")
.with_metadata("key1", "value1");
let config2 = RunnableConfig::new()
.with_thread_id("thread-2")
.with_metadata("key2", "value2");
let merged = config1.merge(config2);
assert_eq!(merged.thread_id(), Some("thread-2")); assert_eq!(merged.get_metadata("key1"), Some("value1"));
assert_eq!(merged.get_metadata("key2"), Some("value2"));
}
#[test]
fn test_config_snapshot_roundtrip() {
let config = RunnableConfig::new()
.with_thread_id("thread-1")
.with_metadata("key", "value")
.with_tag("tag1");
let snapshot = ConfigSnapshot::from(&config);
let restored = RunnableConfig::from(snapshot);
assert_eq!(restored.thread_id(), Some("thread-1"));
assert_eq!(restored.get_metadata("key"), Some("value"));
assert!(restored.has_tag("tag1"));
}
#[test]
fn test_config_with_random_thread() {
let config = RunnableConfig::with_random_thread();
assert!(config.thread_id().is_some());
assert!(!config.thread_id().unwrap().is_empty());
}
}