Skip to main content

lc_core/runnables/
config.rs

1// src/core/runnables/config.rs
2//! Runnable execution configuration.
3
4use serde_json::Value;
5use std::collections::HashMap;
6use std::sync::Arc;
7use uuid::Uuid;
8
9use lc_callbacks::CallbackManager;
10
11use super::cancellation::CancellationToken;
12
13/// Runnable execution configuration.
14#[derive(Debug, Clone, Default)]
15pub struct RunnableConfig {
16    /// Tags for filtering and tracking.
17    pub tags: Vec<String>,
18
19    /// Metadata - custom data (JSON serializable).
20    pub metadata: HashMap<String, Value>,
21
22    /// Max concurrency for batch operations.
23    pub max_concurrency: Option<usize>,
24
25    /// Run ID for tracking.
26    pub run_id: Option<Uuid>,
27
28    /// Run name for debugging.
29    pub run_name: Option<String>,
30
31    /// Callback manager for tracing and monitoring.
32    pub callbacks: Option<Arc<CallbackManager>>,
33
34    /// Cancellation token for aborting long-running operations.
35    pub cancellation_token: Option<CancellationToken>,
36}
37
38impl RunnableConfig {
39    /// Creates an empty configuration.
40    pub fn new() -> Self {
41        Self::default()
42    }
43
44    /// Adds a tag.
45    pub fn with_tag(mut self, tag: impl Into<String>) -> Self {
46        self.tags.push(tag.into());
47        self
48    }
49
50    /// Adds metadata.
51    pub fn with_metadata(mut self, key: impl Into<String>, value: Value) -> Self {
52        self.metadata.insert(key.into(), value);
53        self
54    }
55
56    /// Sets max concurrency.
57    pub fn with_max_concurrency(mut self, max: usize) -> Self {
58        self.max_concurrency = Some(max);
59        self
60    }
61
62    /// Sets run ID.
63    pub fn with_run_id(mut self, id: Uuid) -> Self {
64        self.run_id = Some(id);
65        self
66    }
67
68    /// Sets run name.
69    pub fn with_run_name(mut self, name: impl Into<String>) -> Self {
70        self.run_name = Some(name.into());
71        self
72    }
73
74    /// Sets callback manager.
75    pub fn with_callbacks(mut self, callbacks: Arc<CallbackManager>) -> Self {
76        self.callbacks = Some(callbacks);
77        self
78    }
79
80    /// Sets cancellation token.
81    pub fn with_cancellation_token(mut self, token: CancellationToken) -> Self {
82        self.cancellation_token = Some(token);
83        self
84    }
85
86    /// Checks if cancellation has been requested.
87    pub fn is_cancelled(&self) -> bool {
88        self.cancellation_token
89            .as_ref()
90            .is_some_and(|t| t.is_cancelled())
91    }
92
93    /// Merges two configurations (later overrides earlier).
94    pub fn merge(mut self, other: RunnableConfig) -> Self {
95        // Merge tags (union)
96        self.tags.extend(other.tags);
97        self.tags.sort();
98        self.tags.dedup();
99
100        // Merge metadata (override)
101        self.metadata.extend(other.metadata);
102
103        // Override other fields
104        if other.max_concurrency.is_some() {
105            self.max_concurrency = other.max_concurrency;
106        }
107        if other.run_id.is_some() {
108            self.run_id = other.run_id;
109        }
110        if other.run_name.is_some() {
111            self.run_name = other.run_name;
112        }
113        if other.callbacks.is_some() {
114            self.callbacks = other.callbacks;
115        }
116        if other.cancellation_token.is_some() {
117            self.cancellation_token = other.cancellation_token;
118        }
119
120        self
121    }
122}