lc_core/runnables/
config.rs1use 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#[derive(Debug, Clone, Default)]
15pub struct RunnableConfig {
16 pub tags: Vec<String>,
18
19 pub metadata: HashMap<String, Value>,
21
22 pub max_concurrency: Option<usize>,
24
25 pub run_id: Option<Uuid>,
27
28 pub run_name: Option<String>,
30
31 pub callbacks: Option<Arc<CallbackManager>>,
33
34 pub cancellation_token: Option<CancellationToken>,
36}
37
38impl RunnableConfig {
39 pub fn new() -> Self {
41 Self::default()
42 }
43
44 pub fn with_tag(mut self, tag: impl Into<String>) -> Self {
46 self.tags.push(tag.into());
47 self
48 }
49
50 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 pub fn with_max_concurrency(mut self, max: usize) -> Self {
58 self.max_concurrency = Some(max);
59 self
60 }
61
62 pub fn with_run_id(mut self, id: Uuid) -> Self {
64 self.run_id = Some(id);
65 self
66 }
67
68 pub fn with_run_name(mut self, name: impl Into<String>) -> Self {
70 self.run_name = Some(name.into());
71 self
72 }
73
74 pub fn with_callbacks(mut self, callbacks: Arc<CallbackManager>) -> Self {
76 self.callbacks = Some(callbacks);
77 self
78 }
79
80 pub fn with_cancellation_token(mut self, token: CancellationToken) -> Self {
82 self.cancellation_token = Some(token);
83 self
84 }
85
86 pub fn is_cancelled(&self) -> bool {
88 self.cancellation_token
89 .as_ref()
90 .is_some_and(|t| t.is_cancelled())
91 }
92
93 pub fn merge(mut self, other: RunnableConfig) -> Self {
95 self.tags.extend(other.tags);
97 self.tags.sort();
98 self.tags.dedup();
99
100 self.metadata.extend(other.metadata);
102
103 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}