askit-std-agents 0.7.0

Standard Agents of Agent Stream Kit
Documentation
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
use std::str::FromStr;
use std::sync::{Arc, Mutex};
use std::time::Duration;
use std::vec;

use agent_stream_kit::{
    ASKit, Agent, AgentContext, AgentData, AgentError, AgentOutput, AgentSpec, AgentStatus,
    AgentValue, AsAgent, askit_agent, async_trait,
};
use chrono::{DateTime, Local, Utc};
use cron::Schedule;
use log;
use regex::Regex;
use tokio::task::JoinHandle;

static CATEGORY: &str = "Std/Time";

static PIN_TIME: &str = "time";
static PIN_UNIT: &str = "unit";

static CONFIG_DELAY: &str = "delay";
static CONFIG_MAX_NUM_DATA: &str = "max_num_data";
static CONFIG_INTERVAL: &str = "interval";
static CONFIG_SCHEDULE: &str = "schedule";
static CONFIG_TIME: &str = "time";

const DELAY_MS_DEFAULT: i64 = 1000; // 1 second in milliseconds
const MAX_NUM_DATA_DEFAULT: i64 = 10;
static INTERVAL_DEFAULT: &str = "10s";
static TIME_DEFAULT: &str = "1s";

// Delay Agent
#[askit_agent(
    title = "Delay",
    description = "Delays output by a specified time",
    category = CATEGORY,
    inputs = ["*"],
    outputs = ["*"],
    integer_config(name = CONFIG_DELAY, default = DELAY_MS_DEFAULT, title = "delay (ms)"),
    integer_config(name = CONFIG_MAX_NUM_DATA, default = MAX_NUM_DATA_DEFAULT, title = "max num data")
)]
struct DelayAgent {
    data: AgentData,
    num_waiting_data: Arc<Mutex<i64>>,
}

#[async_trait]
impl AsAgent for DelayAgent {
    fn new(askit: ASKit, id: String, spec: AgentSpec) -> Result<Self, AgentError> {
        Ok(Self {
            data: AgentData::new(askit, id, spec),
            num_waiting_data: Arc::new(Mutex::new(0)),
        })
    }

    async fn process(
        &mut self,
        ctx: AgentContext,
        pin: String,
        value: AgentValue,
    ) -> Result<(), AgentError> {
        let config = self.configs()?;
        let delay_ms = config.get_integer_or(CONFIG_DELAY, DELAY_MS_DEFAULT);
        let max_num_data = config.get_integer_or(CONFIG_MAX_NUM_DATA, MAX_NUM_DATA_DEFAULT);

        // To avoid generating too many timers
        {
            let num_waiting_data = self.num_waiting_data.clone();
            let mut num_waiting_data = num_waiting_data.lock().unwrap();
            if *num_waiting_data >= max_num_data {
                return Ok(());
            }
            *num_waiting_data += 1;
        }

        tokio::time::sleep(Duration::from_millis(delay_ms as u64)).await;

        self.try_output(ctx.clone(), pin, value.clone())?;

        let mut num_waiting_data = self.num_waiting_data.lock().unwrap();
        *num_waiting_data -= 1;

        Ok(())
    }
}

// Interval Timer Agent
#[askit_agent(
    title = "Interval Timer",
    description = "Outputs a unit signal at specified intervals",
    category = CATEGORY,
    outputs = [PIN_UNIT],
    string_config(name = CONFIG_INTERVAL, default = INTERVAL_DEFAULT, description = "(ex. 10s, 5m, 100ms, 1h, 1d)")
)]
struct IntervalTimerAgent {
    data: AgentData,
    timer_handle: Arc<Mutex<Option<JoinHandle<()>>>>,
    interval_ms: u64,
}

impl IntervalTimerAgent {
    fn start_timer(&mut self) -> Result<(), AgentError> {
        let timer_handle = self.timer_handle.clone();
        let interval_ms = self.interval_ms;

        let askit = self.askit().clone();
        let agent_id = self.id().to_string();
        let handle = self.runtime().spawn(async move {
            loop {
                // Sleep for the configured interval
                tokio::time::sleep(tokio::time::Duration::from_millis(interval_ms)).await;

                // Check if we've been stopped
                if let Ok(handle) = timer_handle.lock() {
                    if handle.is_none() {
                        break;
                    }
                }

                // Create a unit output
                if let Err(e) = askit.try_send_agent_out(
                    agent_id.clone(),
                    AgentContext::new(),
                    PIN_UNIT.to_string(),
                    AgentValue::unit(),
                ) {
                    log::error!("Failed to send interval timer output: {}", e);
                }
            }
        });

        // Store the timer handle
        if let Ok(mut timer_handle) = self.timer_handle.lock() {
            *timer_handle = Some(handle);
        }

        Ok(())
    }

    fn stop_timer(&mut self) -> Result<(), AgentError> {
        // Cancel the timer
        if let Ok(mut timer_handle) = self.timer_handle.lock() {
            if let Some(handle) = timer_handle.take() {
                handle.abort();
            }
        }
        Ok(())
    }
}

#[async_trait]
impl AsAgent for IntervalTimerAgent {
    fn new(askit: ASKit, id: String, spec: AgentSpec) -> Result<Self, AgentError> {
        let interval = spec
            .configs
            .as_ref()
            .ok_or(AgentError::NoConfig)?
            .get_string_or(CONFIG_INTERVAL, INTERVAL_DEFAULT);
        let interval_ms = parse_duration_to_ms(&interval)?;

        Ok(Self {
            data: AgentData::new(askit, id, spec),
            timer_handle: Default::default(),
            interval_ms,
        })
    }

    async fn start(&mut self) -> Result<(), AgentError> {
        self.start_timer()
    }

    async fn stop(&mut self) -> Result<(), AgentError> {
        self.stop_timer()
    }

    fn configs_changed(&mut self) -> Result<(), AgentError> {
        // Check if interval has changed
        let interval = self.configs()?.get_string(CONFIG_INTERVAL)?;
        let new_interval = parse_duration_to_ms(&interval)?;
        if new_interval != self.interval_ms {
            self.interval_ms = new_interval;
            if *self.status() == AgentStatus::Start {
                // Restart the timer with the new interval
                self.stop_timer()?;
                self.start_timer()?;
            }
        }
        Ok(())
    }
}

// OnStart
#[askit_agent(
    title = "On Start",
    category = CATEGORY,
    outputs = [PIN_UNIT],
    integer_config(name = CONFIG_DELAY, default = DELAY_MS_DEFAULT, title = "delay (ms)")
)]
struct OnStartAgent {
    data: AgentData,
}

#[async_trait]
impl AsAgent for OnStartAgent {
    fn new(askit: ASKit, id: String, spec: AgentSpec) -> Result<Self, AgentError> {
        Ok(Self {
            data: AgentData::new(askit, id, spec),
        })
    }

    async fn start(&mut self) -> Result<(), AgentError> {
        let config = self.configs()?;
        let delay_ms = config.get_integer_or(CONFIG_DELAY, DELAY_MS_DEFAULT);

        let askit = self.askit().clone();
        let agent_id = self.id().to_string();

        self.runtime().spawn(async move {
            tokio::time::sleep(Duration::from_millis(delay_ms as u64)).await;

            if let Err(e) = askit.try_send_agent_out(
                agent_id,
                AgentContext::new(),
                PIN_UNIT.to_string(),
                AgentValue::unit(),
            ) {
                log::error!("Failed to send delayed output: {}", e);
            }
        });

        Ok(())
    }
}

// Schedule Timer Agent
#[askit_agent(
    title = "Schedule Timer",
    category = CATEGORY,
    outputs = [PIN_TIME],
    string_config(name = CONFIG_SCHEDULE, default = "0 0 * * * *", description = "sec min hour day month week year")
)]
struct ScheduleTimerAgent {
    data: AgentData,
    cron_schedule: Option<Schedule>,
    timer_handle: Arc<Mutex<Option<JoinHandle<()>>>>,
}

impl ScheduleTimerAgent {
    fn start_timer(&mut self) -> Result<(), AgentError> {
        let Some(schedule) = &self.cron_schedule else {
            return Err(AgentError::InvalidConfig("No schedule defined".into()));
        };

        let askit = self.askit().clone();
        let agent_id = self.id().to_string();
        let timer_handle = self.timer_handle.clone();
        let schedule = schedule.clone();

        let handle = self.runtime().spawn(async move {
            loop {
                // Calculate the next time this schedule should run
                let now: DateTime<Utc> = Utc::now();
                let next = match schedule.upcoming(Utc).next() {
                    Some(next_time) => next_time,
                    None => {
                        log::error!("No upcoming schedule times found");
                        break;
                    }
                };

                // Calculate the duration until the next scheduled time
                let duration = match (next - now).to_std() {
                    Ok(duration) => duration,
                    Err(e) => {
                        log::error!("Failed to calculate duration until next schedule: {}", e);
                        // If we can't calculate the duration, sleep for a short time and try again
                        tokio::time::sleep(Duration::from_secs(60)).await;
                        continue;
                    }
                };

                let next_local = next.with_timezone(&Local);
                log::debug!(
                    "Scheduling timer for '{}' to fire at {} (in {:?})",
                    agent_id,
                    next_local.format("%Y-%m-%d %H:%M:%S %z"),
                    duration
                );

                // Sleep until the next scheduled time
                tokio::time::sleep(duration).await;

                // Check if we've been stopped
                if let Ok(handle) = timer_handle.lock() {
                    if handle.is_none() {
                        break;
                    }
                }

                // Get the current local timestamp (in seconds)
                let current_local_time = Local::now().timestamp();

                // Output the timestamp as an integer
                if let Err(e) = askit.try_send_agent_out(
                    agent_id.clone(),
                    AgentContext::new(),
                    PIN_TIME.to_string(),
                    AgentValue::integer(current_local_time),
                ) {
                    log::error!("Failed to send schedule timer output: {}", e);
                }
            }
        });

        // Store the timer handle
        if let Ok(mut timer_handle) = self.timer_handle.lock() {
            *timer_handle = Some(handle);
        }

        Ok(())
    }

    fn stop_timer(&mut self) -> Result<(), AgentError> {
        // Cancel the timer
        if let Ok(mut timer_handle) = self.timer_handle.lock() {
            if let Some(handle) = timer_handle.take() {
                handle.abort();
            }
        }
        Ok(())
    }

    fn parse_schedule(&mut self, schedule_str: &str) -> Result<(), AgentError> {
        if schedule_str.trim().is_empty() {
            self.cron_schedule = None;
            return Ok(());
        }

        let schedule = Schedule::from_str(schedule_str).map_err(|e| {
            AgentError::InvalidConfig(format!("Invalid cron schedule '{}': {}", schedule_str, e))
        })?;
        self.cron_schedule = Some(schedule);
        Ok(())
    }
}

#[async_trait]
impl AsAgent for ScheduleTimerAgent {
    fn new(askit: ASKit, id: String, spec: AgentSpec) -> Result<Self, AgentError> {
        let schedule_str = spec
            .configs
            .as_ref()
            .map(|cfg| cfg.get_string(CONFIG_SCHEDULE))
            .transpose()?;

        let mut agent = Self {
            data: AgentData::new(askit, id, spec),
            cron_schedule: None,
            timer_handle: Default::default(),
        };

        if let Some(schedule_str) = schedule_str {
            if !schedule_str.is_empty() {
                agent.parse_schedule(&schedule_str)?;
            }
        }

        Ok(agent)
    }

    async fn start(&mut self) -> Result<(), AgentError> {
        if self.cron_schedule.is_some() {
            self.start_timer()?;
        }
        Ok(())
    }

    async fn stop(&mut self) -> Result<(), AgentError> {
        self.stop_timer()
    }

    fn configs_changed(&mut self) -> Result<(), AgentError> {
        // Check if schedule has changed
        let schedule_str = self.configs()?.get_string(CONFIG_SCHEDULE)?;
        self.parse_schedule(&schedule_str)?;

        if *self.status() == AgentStatus::Start {
            // Restart the timer with the new schedule
            self.stop_timer()?;
            if self.cron_schedule.is_some() {
                self.start_timer()?;
            }
        }
        Ok(())
    }
}

// Throttle agent
#[askit_agent(
    title = "Throttle Time",
    category = CATEGORY,
    inputs = ["*"],
    outputs = ["*"],
    string_config(name = CONFIG_TIME, default = TIME_DEFAULT, description = "(ex. 10s, 5m, 100ms, 1h, 1d)"),
    integer_config(name = CONFIG_MAX_NUM_DATA, title = "max num data", description = "0: no data, -1: all data")
)]
struct ThrottleTimeAgent {
    data: AgentData,
    timer_handle: Arc<Mutex<Option<JoinHandle<()>>>>,
    time_ms: u64,
    max_num_data: i64,
    waiting_data: Arc<Mutex<Vec<(AgentContext, String, AgentValue)>>>,
}

impl ThrottleTimeAgent {
    fn start_timer(&mut self) -> Result<(), AgentError> {
        let timer_handle = self.timer_handle.clone();
        let time_ms = self.time_ms;

        let waiting_data = self.waiting_data.clone();
        let askit = self.askit().clone();
        let agent_id = self.id().to_string();

        let handle = self.runtime().spawn(async move {
            loop {
                // Sleep for the configured interval
                tokio::time::sleep(tokio::time::Duration::from_millis(time_ms)).await;

                // Check if we've been stopped
                let mut handle = timer_handle.lock().unwrap();
                if handle.is_none() {
                    break;
                }

                // process the waiting data
                let mut wd = waiting_data.lock().unwrap();
                if wd.len() > 0 {
                    // If there are data waiting, output the first one
                    let (ctx, pin, data) = wd.remove(0);
                    askit
                        .try_send_agent_out(agent_id.clone(), ctx, pin, data)
                        .unwrap_or_else(|e| {
                            log::error!("Failed to send delayed output: {}", e);
                        });
                }

                // If there are no data waiting, we stop the timer
                if wd.len() == 0 {
                    handle.take();
                    break;
                }
            }
        });

        // Store the timer handle
        if let Ok(mut timer_handle) = self.timer_handle.lock() {
            *timer_handle = Some(handle);
        }

        Ok(())
    }

    fn stop_timer(&mut self) -> Result<(), AgentError> {
        // Cancel the timer
        if let Ok(mut timer_handle) = self.timer_handle.lock() {
            if let Some(handle) = timer_handle.take() {
                handle.abort();
            }
        }
        Ok(())
    }
}

#[async_trait]
impl AsAgent for ThrottleTimeAgent {
    fn new(askit: ASKit, id: String, spec: AgentSpec) -> Result<Self, AgentError> {
        let time = spec
            .configs
            .as_ref()
            .ok_or(AgentError::NoConfig)?
            .get_string_or(CONFIG_TIME, TIME_DEFAULT);
        let time_ms = parse_duration_to_ms(&time)?;

        let max_num_data = spec
            .configs
            .as_ref()
            .ok_or(AgentError::NoConfig)?
            .get_integer_or(CONFIG_MAX_NUM_DATA, 0);

        Ok(Self {
            data: AgentData::new(askit, id, spec),
            timer_handle: Default::default(),
            time_ms,
            max_num_data,
            waiting_data: Arc::new(Mutex::new(vec![])),
        })
    }

    async fn stop(&mut self) -> Result<(), AgentError> {
        self.stop_timer()
    }

    fn configs_changed(&mut self) -> Result<(), AgentError> {
        // Check if interval has changed
        let time = self.configs()?.get_string(CONFIG_TIME)?;
        let new_time = parse_duration_to_ms(&time)?;
        if new_time != self.time_ms {
            self.time_ms = new_time;
        }

        // Check if max_num_data has changed
        let max_num_data = self.configs()?.get_integer(CONFIG_MAX_NUM_DATA)?;
        if self.max_num_data != max_num_data {
            let mut wd = self.waiting_data.lock().unwrap();
            let wd_len = wd.len();
            if max_num_data >= 0 && wd_len > (max_num_data as usize) {
                // If we have reached the max data to keep, we drop the oldest one
                wd.drain(0..(wd_len - (max_num_data as usize)));
            }
            self.max_num_data = max_num_data;
        }
        Ok(())
    }

    async fn process(
        &mut self,
        ctx: AgentContext,
        pin: String,
        value: AgentValue,
    ) -> Result<(), AgentError> {
        if self.timer_handle.lock().unwrap().is_some() {
            // If the timer is running, we just add the data to the waiting list
            let mut wd = self.waiting_data.lock().unwrap();

            // If max_num_data is 0, we don't need to keep any data
            if self.max_num_data == 0 {
                return Ok(());
            }

            wd.push((ctx, pin, value));
            if self.max_num_data > 0 && wd.len() > self.max_num_data as usize {
                // If we have reached the max data to keep, we drop the oldest one
                wd.remove(0);
            }

            return Ok(());
        }

        // Start the timer
        self.start_timer()?;

        // Output the data
        self.try_output(ctx, pin, value)?;

        Ok(())
    }
}

// Parse time duration strings like "2s", "10m", "200ms"
fn parse_duration_to_ms(duration_str: &str) -> Result<u64, AgentError> {
    const MIN_DURATION: u64 = 10;

    // Regular expression to match number followed by optional unit
    let re = Regex::new(r"^(\d+)(?:([a-zA-Z]+))?$").expect("Failed to compile regex");

    if let Some(captures) = re.captures(duration_str.trim()) {
        let value: u64 = captures.get(1).unwrap().as_str().parse().map_err(|e| {
            AgentError::InvalidConfig(format!(
                "Invalid number in duration '{}': {}",
                duration_str, e
            ))
        })?;

        // Get the unit if present, default to "s" (seconds)
        let unit = captures
            .get(2)
            .map_or("s".to_string(), |m| m.as_str().to_lowercase());

        // Convert to milliseconds based on unit
        let milliseconds = match unit.as_str() {
            "ms" => value,               // already in milliseconds
            "s" => value * 1000,         // seconds to milliseconds
            "m" => value * 60 * 1000,    // minutes to milliseconds
            "h" => value * 3600 * 1000,  // hours to milliseconds
            "d" => value * 86400 * 1000, // days to milliseconds
            _ => {
                return Err(AgentError::InvalidConfig(format!(
                    "Unknown time unit: {}",
                    unit
                )));
            }
        };

        // Ensure we don't return less than the minimum duration
        Ok(std::cmp::max(milliseconds, MIN_DURATION))
    } else {
        // If the string doesn't match the pattern, try to parse it as a plain number
        // and assume it's in seconds
        let value: u64 = duration_str.parse().map_err(|e| {
            AgentError::InvalidConfig(format!("Invalid duration format '{}': {}", duration_str, e))
        })?;
        Ok(std::cmp::max(value * 1000, MIN_DURATION)) // Convert to ms
    }
}