1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/// Advanced macro utilities for code generation and pattern reduction
/// Macro for generating message types with automatic builder pattern
#[macro_export]
macro_rules! define_messages {
(
$(
$(#[$meta:meta])*
$variant:ident {
$(
$(#[$field_meta:meta])*
$field:ident: $type:ty
),* $(,)?
}
)*
) => {
use serde::{Serialize, Deserialize};
use uuid::Uuid;
use std::collections::HashMap;
/// Unified message enum containing all message types
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum UnifiedMessage {
$(
$(#[$meta])*
$variant($variant),
)*
}
$(
$(#[$meta])*
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct $variant {
/// Unique message ID
#[serde(default = "generate_id")]
pub id: String,
$(
$(#[$field_meta])*
pub $field: $type,
)*
/// Message timestamp
#[serde(default = "chrono::Utc::now")]
pub timestamp: chrono::DateTime<chrono::Utc>,
}
impl $variant {
/// Create new message with builder pattern
pub fn builder() -> paste::paste! { [<$variant Builder>] } {
paste::paste! { [<$variant Builder>]::default() }
}
/// Quick constructor for simple cases
pub fn new($($field: $type),*) -> Self {
Self {
id: generate_id(),
$($field,)*
timestamp: chrono::Utc::now(),
}
}
/// Convert to UnifiedMessage
pub fn into_unified(self) -> UnifiedMessage {
UnifiedMessage::$variant(self)
}
}
paste::paste! {
#[derive(Default)]
pub struct [<$variant Builder>] {
id: Option<String>,
$($field: Option<$type>,)*
timestamp: Option<chrono::DateTime<chrono::Utc>>,
}
impl [<$variant Builder>] {
pub fn id(mut self, id: impl Into<String>) -> Self {
self.id = Some(id.into());
self
}
$(
pub fn $field(mut self, $field: $type) -> Self {
self.$field = Some($field);
self
}
)*
pub fn timestamp(mut self, timestamp: chrono::DateTime<chrono::Utc>) -> Self {
self.timestamp = Some(timestamp);
self
}
pub fn build(self) -> Result<$variant, &'static str> {
Ok($variant {
id: self.id.unwrap_or_else(generate_id),
$(
$field: self.$field.ok_or(concat!("Missing field: ", stringify!($field)))?,
)*
timestamp: self.timestamp.unwrap_or_else(chrono::Utc::now),
})
}
}
}
)*
fn generate_id() -> String {
Uuid::new_v4().to_string()
}
};
}
/// Macro for defining async state machines with automatic transition handling
#[macro_export]
macro_rules! async_state_machine {
(
machine: $machine:ident,
context: $context:ty,
error: $error:ty,
states: {
$(
$state:ident $(($($state_data:ty),*))? {
$(
on $event:ident $(($($event_data:ident: $event_type:ty),*))? => $next_state:ident
$transition_body:block
)*
}
),* $(,)?
}
) => {
use async_trait::async_trait;
use std::collections::HashMap;
use std::sync::Arc;
use tokio::sync::RwLock;
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum State {
$(
$state $(($($state_data),*))?
),*
}
#[derive(Debug, Clone)]
pub enum Event {
$(
$(
$event $(($($event_type),*))?
),*
),*
}
pub struct $machine {
state: Arc<RwLock<State>>,
context: Arc<RwLock<$context>>,
transition_hooks: Arc<RwLock<TransitionHooks>>,
}
struct TransitionHooks {
on_enter: HashMap<State, Box<dyn Fn(&$context) + Send + Sync>>,
on_exit: HashMap<State, Box<dyn Fn(&$context) + Send + Sync>>,
}
impl $machine {
pub fn new(initial_context: $context) -> Self {
Self {
state: Arc::new(RwLock::new(State::default())),
context: Arc::new(RwLock::new(initial_context)),
transition_hooks: Arc::new(RwLock::new(TransitionHooks {
on_enter: HashMap::new(),
on_exit: HashMap::new(),
})),
}
}
pub async fn handle_event(&self, event: Event) -> Result<(), $error> {
let current_state = self.state.read().await.clone();
match (¤t_state, &event) {
$(
$(
(State::$state $(($($state_data),*))?, Event::$event $(($($event_data),*))?) => {
self.transition_to(State::$next_state, || async {
$transition_body
}).await?;
}
)*
),*
_ => return Err(anyhow::anyhow!("Invalid transition from {:?} on {:?}", current_state, event).into()),
}
Ok(())
}
async fn transition_to<F, Fut>(&self, new_state: State, transition_fn: F) -> Result<(), $error>
where
F: FnOnce() -> Fut,
Fut: std::future::Future<Output = Result<(), $error>>,
{
let old_state = {
let mut state = self.state.write().await;
let old = state.clone();
*state = new_state.clone();
old
};
// Execute exit hook
if let Some(on_exit) = self.transition_hooks.read().await.on_exit.get(&old_state) {
on_exit(&*self.context.read().await);
}
// Execute transition
transition_fn().await?;
// Execute enter hook
if let Some(on_enter) = self.transition_hooks.read().await.on_enter.get(&new_state) {
on_enter(&*self.context.read().await);
}
info!("Transitioned from {:?} to {:?}", old_state, new_state);
Ok(())
}
pub async fn current_state(&self) -> State {
self.state.read().await.clone()
}
pub async fn with_context<F, R>(&self, f: F) -> R
where
F: FnOnce(&$context) -> R,
{
f(&*self.context.read().await)
}
pub async fn with_context_mut<F, R>(&self, f: F) -> R
where
F: FnOnce(&mut $context) -> R,
{
f(&mut *self.context.write().await)
}
}
impl Default for State {
fn default() -> Self {
// First state is the default
$(
return State::$state $((<$($state_data>::default()),*))?;
)*
}
}
};
}
/// Macro for generating pattern DSL
#[macro_export]
macro_rules! pattern_dsl {
(
$(
pattern $name:ident {
triggers: [$($trigger:expr),* $(,)?],
confidence: $confidence:expr,
tasks: [
$(
{
description: $desc:expr,
task_type: $task_type:ident,
priority: $priority:ident,
duration: $duration:expr,
agent: $agent:expr $(,)?
}
),* $(,)?
] $(,)?
}
)*
) => {{
use std::collections::HashMap;
let mut patterns = HashMap::new();
$(
patterns.insert(
stringify!($name).to_string(),
$crate::orchestrator::TaskPattern {
pattern_id: stringify!($name).to_string(),
trigger_conditions: vec![$($trigger.to_string()),*],
generated_tasks: vec![
$(
$crate::orchestrator::TaskTemplate {
description_template: $desc.to_string(),
task_type: $crate::task::TaskType::$task_type,
priority: $crate::task::Priority::$priority,
estimated_duration: $duration,
required_agent_type: $agent.to_string(),
variables: HashMap::new(),
}
),*
],
confidence: $confidence,
usage_count: 0,
}
);
)*
patterns
}};
}
/// Macro for async operation with automatic retry, timeout, and error handling
#[macro_export]
macro_rules! async_operation {
(
name: $name:expr,
timeout: $timeout:expr,
retries: $retries:expr,
$body:block
) => {{
use tokio::time::{Duration, timeout};
use tracing::{debug, error, instrument};
#[instrument(name = $name, skip_all)]
async fn operation() -> Result<_, Box<dyn std::error::Error>> {
$body
}
let mut attempts = 0;
let max_retries = $retries;
let timeout_duration = Duration::from_secs($timeout);
loop {
attempts += 1;
debug!("Attempt {} of {}", attempts, max_retries + 1);
match timeout(timeout_duration, operation()).await {
Ok(Ok(result)) => {
metrics::counter!(concat!($name, ".success"), 1);
break Ok(result);
}
Ok(Err(e)) if attempts <= max_retries => {
error!("Operation failed (attempt {}): {:?}", attempts, e);
metrics::counter!(concat!($name, ".retry"), 1);
// Exponential backoff
let backoff = Duration::from_millis(100 * 2u64.pow(attempts - 1));
tokio::time::sleep(backoff).await;
continue;
}
Ok(Err(e)) => {
metrics::counter!(concat!($name, ".failure"), 1);
break Err(e);
}
Err(_) => {
metrics::counter!(concat!($name, ".timeout"), 1);
break Err("Operation timed out".into());
}
}
}
}};
}
/// Macro for generating error types with context
#[macro_export]
macro_rules! define_errors {
(
$(
$(#[$meta:meta])*
$name:ident {
$(
$(#[$variant_meta:meta])*
$variant:ident $(($($field:ty),*))? => $message:expr
),* $(,)?
}
)*
) => {
$(
$(#[$meta])*
#[derive(Debug, thiserror::Error)]
pub enum $name {
$(
$(#[$variant_meta])*
#[error($message)]
$variant $(($($field),*))?,
)*
#[error("Internal error: {0}")]
Internal(String),
#[error(transparent)]
Other(#[from] anyhow::Error),
}
impl $name {
pub fn internal(msg: impl Into<String>) -> Self {
Self::Internal(msg.into())
}
}
)*
};
}