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 pub temperature: Option<f32>,
43
44 pub max_tokens: Option<usize>,
49}
50
51impl RunnableConfig {
52 pub fn new() -> Self {
54 Self::default()
55 }
56
57 pub fn with_tag(mut self, tag: impl Into<String>) -> Self {
59 self.tags.push(tag.into());
60 self
61 }
62
63 pub fn with_metadata(mut self, key: impl Into<String>, value: Value) -> Self {
65 self.metadata.insert(key.into(), value);
66 self
67 }
68
69 pub fn with_max_concurrency(mut self, max: usize) -> Self {
71 self.max_concurrency = Some(max);
72 self
73 }
74
75 pub fn with_run_id(mut self, id: Uuid) -> Self {
77 self.run_id = Some(id);
78 self
79 }
80
81 pub fn with_run_name(mut self, name: impl Into<String>) -> Self {
83 self.run_name = Some(name.into());
84 self
85 }
86
87 pub fn with_callbacks(mut self, callbacks: Arc<CallbackManager>) -> Self {
89 self.callbacks = Some(callbacks);
90 self
91 }
92
93 pub fn with_cancellation_token(mut self, token: CancellationToken) -> Self {
95 self.cancellation_token = Some(token);
96 self
97 }
98
99 pub fn with_temperature(mut self, temperature: f32) -> Self {
101 self.temperature = Some(temperature);
102 self
103 }
104
105 pub fn with_max_tokens(mut self, max_tokens: usize) -> Self {
107 self.max_tokens = Some(max_tokens);
108 self
109 }
110
111 pub fn is_cancelled(&self) -> bool {
113 self.cancellation_token
114 .as_ref()
115 .is_some_and(|t| t.is_cancelled())
116 }
117
118 pub fn merge(mut self, other: RunnableConfig) -> Self {
120 for tag in other.tags {
122 if !self.tags.iter().any(|existing| existing == &tag) {
123 self.tags.push(tag);
124 }
125 }
126
127 self.metadata.extend(other.metadata);
129
130 if other.max_concurrency.is_some() {
132 self.max_concurrency = other.max_concurrency;
133 }
134 if other.run_id.is_some() {
135 self.run_id = other.run_id;
136 }
137 if other.run_name.is_some() {
138 self.run_name = other.run_name;
139 }
140 if other.cancellation_token.is_some() {
141 self.cancellation_token = other.cancellation_token;
142 }
143 if other.temperature.is_some() {
144 self.temperature = other.temperature;
145 }
146 if other.max_tokens.is_some() {
147 self.max_tokens = other.max_tokens;
148 }
149
150 if let (Some(self_cb), Some(other_cb)) = (&self.callbacks, &other.callbacks) {
154 self.callbacks = Some(Arc::new(self_cb.merge_with(other_cb)));
155 } else if other.callbacks.is_some() {
156 self.callbacks = other.callbacks;
157 }
158
159 self
160 }
161}