aidaemon 0.11.13

A personal AI agent that runs as a background daemon, accessible via Telegram, Slack, or Discord, with tool use, MCP integration, and persistent memory
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
//! Health probe types and executor implementations.

use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::time::{Duration, Instant};
use tokio::io::AsyncWriteExt;
use tokio::net::TcpStream;
use tokio::process::Command;
use tracing::{debug, warn};

/// Type of health probe.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ProbeType {
    /// HTTP/HTTPS endpoint check
    Http,
    /// Shell command execution
    Command,
    /// File existence/age check
    File,
    /// TCP port connectivity
    Port,
    /// Custom probe (reserved for future extensions)
    Custom,
}

impl ProbeType {
    pub fn as_str(&self) -> &'static str {
        match self {
            ProbeType::Http => "http",
            ProbeType::Command => "command",
            ProbeType::File => "file",
            ProbeType::Port => "port",
            ProbeType::Custom => "custom",
        }
    }

    pub fn from_str(s: &str) -> Self {
        match s.to_lowercase().as_str() {
            "http" | "https" => ProbeType::Http,
            "command" | "cmd" => ProbeType::Command,
            "file" => ProbeType::File,
            "port" | "tcp" => ProbeType::Port,
            _ => ProbeType::Custom,
        }
    }
}

/// Status of a probe check result.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ProbeStatus {
    Healthy,
    Unhealthy,
    Timeout,
    Error,
}

impl ProbeStatus {
    pub fn as_str(&self) -> &'static str {
        match self {
            ProbeStatus::Healthy => "healthy",
            ProbeStatus::Unhealthy => "unhealthy",
            ProbeStatus::Timeout => "timeout",
            ProbeStatus::Error => "error",
        }
    }

    pub fn from_str(s: &str) -> Self {
        match s.to_lowercase().as_str() {
            "healthy" | "ok" | "up" => ProbeStatus::Healthy,
            "unhealthy" | "down" | "fail" => ProbeStatus::Unhealthy,
            "timeout" => ProbeStatus::Timeout,
            _ => ProbeStatus::Error,
        }
    }

    pub fn is_healthy(&self) -> bool {
        matches!(self, ProbeStatus::Healthy)
    }
}

/// Configuration options for probe execution.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ProbeConfig {
    /// Timeout in seconds (default: 10)
    #[serde(default = "default_timeout_secs")]
    pub timeout_secs: u64,

    /// Expected HTTP status code (default: 200)
    #[serde(default = "default_expected_status")]
    pub expected_status: Option<u16>,

    /// Expected response body substring
    pub expected_body: Option<String>,

    /// HTTP method (default: GET)
    #[serde(default = "default_http_method")]
    pub method: String,

    /// HTTP headers to include
    #[serde(default)]
    pub headers: HashMap<String, String>,

    /// For file probe: max age in seconds (file is unhealthy if older)
    pub max_age_secs: Option<u64>,

    /// For command probe: expected exit code (default: 0)
    #[serde(default)]
    pub expected_exit_code: Option<i32>,
}

fn default_timeout_secs() -> u64 {
    10
}

fn default_expected_status() -> Option<u16> {
    Some(200)
}

fn default_http_method() -> String {
    "GET".to_string()
}

/// Health probe definition.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HealthProbe {
    pub id: String,
    pub name: String,
    pub description: Option<String>,
    pub probe_type: ProbeType,
    pub target: String,
    pub schedule: String,
    pub source: String,
    pub config: ProbeConfig,
    pub consecutive_failures_alert: u32,
    pub latency_threshold_ms: Option<u32>,
    pub alert_session_ids: Vec<String>,
    pub is_paused: bool,
    pub last_run_at: Option<DateTime<Utc>>,
    pub next_run_at: DateTime<Utc>,
    pub created_at: DateTime<Utc>,
    pub updated_at: DateTime<Utc>,
}

impl HealthProbe {
    /// Create a new probe with defaults.
    pub fn new(
        name: String,
        probe_type: ProbeType,
        target: String,
        schedule: String,
        source: String,
    ) -> Self {
        let now = Utc::now();
        Self {
            id: uuid::Uuid::new_v4().to_string(),
            name,
            description: None,
            probe_type,
            target,
            schedule,
            source,
            config: ProbeConfig::default(),
            consecutive_failures_alert: 3,
            latency_threshold_ms: None,
            alert_session_ids: Vec::new(),
            is_paused: false,
            last_run_at: None,
            next_run_at: now,
            created_at: now,
            updated_at: now,
        }
    }
}

/// Result of a probe check.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProbeResult {
    pub id: i64,
    pub probe_id: String,
    pub status: ProbeStatus,
    pub latency_ms: Option<u32>,
    pub error_message: Option<String>,
    pub response_body: Option<String>,
    pub checked_at: DateTime<Utc>,
}

impl ProbeResult {
    pub fn new(probe_id: String, status: ProbeStatus) -> Self {
        Self {
            id: 0,
            probe_id,
            status,
            latency_ms: None,
            error_message: None,
            response_body: None,
            checked_at: Utc::now(),
        }
    }

    pub fn with_latency(mut self, latency_ms: u32) -> Self {
        self.latency_ms = Some(latency_ms);
        self
    }

    pub fn with_error(mut self, error: impl Into<String>) -> Self {
        self.error_message = Some(error.into());
        self
    }

    pub fn with_body(mut self, body: impl Into<String>) -> Self {
        let body = body.into();
        // Truncate to ~1KB
        self.response_body = Some(crate::utils::truncate_str(&body, 1024));
        self
    }
}

/// Probe executor that runs checks based on probe type.
pub struct ProbeExecutor;

impl ProbeExecutor {
    /// Execute a probe and return the result.
    pub async fn execute(probe: &HealthProbe) -> ProbeResult {
        let start = Instant::now();
        let timeout = Duration::from_secs(probe.config.timeout_secs);

        let result = match tokio::time::timeout(timeout, Self::execute_inner(probe)).await {
            Ok(result) => result,
            Err(_) => {
                let latency = start.elapsed().as_millis() as u32;
                ProbeResult::new(probe.id.clone(), ProbeStatus::Timeout)
                    .with_latency(latency)
                    .with_error(format!(
                        "Probe timed out after {}s",
                        probe.config.timeout_secs
                    ))
            }
        };

        let latency = start.elapsed().as_millis() as u32;
        if result.latency_ms.is_none() {
            ProbeResult {
                latency_ms: Some(latency),
                ..result
            }
        } else {
            result
        }
    }

    async fn execute_inner(probe: &HealthProbe) -> ProbeResult {
        match probe.probe_type {
            ProbeType::Http => Self::execute_http(probe).await,
            ProbeType::Command => Self::execute_command(probe).await,
            ProbeType::File => Self::execute_file(probe).await,
            ProbeType::Port => Self::execute_port(probe).await,
            ProbeType::Custom => ProbeResult::new(probe.id.clone(), ProbeStatus::Error)
                .with_error("Custom probes not implemented"),
        }
    }

    /// Execute an HTTP probe.
    async fn execute_http(probe: &HealthProbe) -> ProbeResult {
        let client = reqwest::Client::builder()
            .timeout(Duration::from_secs(probe.config.timeout_secs))
            .build();

        let client = match client {
            Ok(c) => c,
            Err(e) => {
                return ProbeResult::new(probe.id.clone(), ProbeStatus::Error)
                    .with_error(format!("Failed to create HTTP client: {}", e));
            }
        };

        let method = probe.config.method.to_uppercase();
        let mut request = match method.as_str() {
            "GET" => client.get(&probe.target),
            "POST" => client.post(&probe.target),
            "HEAD" => client.head(&probe.target),
            "PUT" => client.put(&probe.target),
            "DELETE" => client.delete(&probe.target),
            _ => client.get(&probe.target),
        };

        // Add custom headers
        for (key, value) in &probe.config.headers {
            request = request.header(key, value);
        }

        let start = Instant::now();
        let response = match request.send().await {
            Ok(r) => r,
            Err(e) => {
                let latency = start.elapsed().as_millis() as u32;
                let status = if e.is_timeout() {
                    ProbeStatus::Timeout
                } else if e.is_connect() {
                    ProbeStatus::Unhealthy
                } else {
                    ProbeStatus::Error
                };
                return ProbeResult::new(probe.id.clone(), status)
                    .with_latency(latency)
                    .with_error(format!("HTTP request failed: {}", e));
            }
        };

        let latency = start.elapsed().as_millis() as u32;
        let status_code = response.status().as_u16();

        // Check expected status
        let expected = probe.config.expected_status.unwrap_or(200);
        let status_ok = status_code == expected;

        // Get response body for validation
        let body = response.text().await.unwrap_or_default();

        // Check expected body
        let body_ok = match &probe.config.expected_body {
            Some(expected_body) => body.contains(expected_body),
            None => true,
        };

        let probe_status = if status_ok && body_ok {
            ProbeStatus::Healthy
        } else {
            ProbeStatus::Unhealthy
        };

        let mut result = ProbeResult::new(probe.id.clone(), probe_status)
            .with_latency(latency)
            .with_body(&body);

        if !status_ok {
            result =
                result.with_error(format!("Expected status {}, got {}", expected, status_code));
        } else if !body_ok {
            result = result.with_error(format!(
                "Response body does not contain expected text: {:?}",
                probe.config.expected_body
            ));
        }

        result
    }

    /// Execute a command probe.
    async fn execute_command(probe: &HealthProbe) -> ProbeResult {
        let start = Instant::now();

        // Parse command (simple shell execution)
        let output = Command::new("sh")
            .arg("-c")
            .arg(&probe.target)
            .output()
            .await;

        let latency = start.elapsed().as_millis() as u32;

        match output {
            Ok(output) => {
                let exit_code = output.status.code().unwrap_or(-1);
                let expected_code = probe.config.expected_exit_code.unwrap_or(0);
                let stdout = String::from_utf8_lossy(&output.stdout);
                let stderr = String::from_utf8_lossy(&output.stderr);

                let is_healthy = exit_code == expected_code;
                let probe_status = if is_healthy {
                    ProbeStatus::Healthy
                } else {
                    ProbeStatus::Unhealthy
                };

                let combined_output = if stderr.is_empty() {
                    stdout.to_string()
                } else {
                    format!("{}\n{}", stdout, stderr)
                };

                let mut result = ProbeResult::new(probe.id.clone(), probe_status)
                    .with_latency(latency)
                    .with_body(combined_output);

                if !is_healthy {
                    result = result.with_error(format!(
                        "Command exited with code {} (expected {})",
                        exit_code, expected_code
                    ));
                }

                result
            }
            Err(e) => ProbeResult::new(probe.id.clone(), ProbeStatus::Error)
                .with_latency(latency)
                .with_error(format!("Failed to execute command: {}", e)),
        }
    }

    /// Execute a file probe.
    async fn execute_file(probe: &HealthProbe) -> ProbeResult {
        let start = Instant::now();
        let path = std::path::Path::new(&probe.target);

        // Check if file exists
        if !path.exists() {
            let latency = start.elapsed().as_millis() as u32;
            return ProbeResult::new(probe.id.clone(), ProbeStatus::Unhealthy)
                .with_latency(latency)
                .with_error(format!("File does not exist: {}", probe.target));
        }

        // Check file age if max_age_secs is specified
        if let Some(max_age) = probe.config.max_age_secs {
            match std::fs::metadata(path) {
                Ok(meta) => match meta.modified() {
                    Ok(modified) => {
                        let age = modified.elapsed().unwrap_or_default();
                        let latency = start.elapsed().as_millis() as u32;

                        if age.as_secs() > max_age {
                            return ProbeResult::new(probe.id.clone(), ProbeStatus::Unhealthy)
                                .with_latency(latency)
                                .with_error(format!(
                                    "File is too old: {} seconds (max: {})",
                                    age.as_secs(),
                                    max_age
                                ));
                        }
                    }
                    Err(e) => {
                        let latency = start.elapsed().as_millis() as u32;
                        return ProbeResult::new(probe.id.clone(), ProbeStatus::Error)
                            .with_latency(latency)
                            .with_error(format!("Cannot read file modification time: {}", e));
                    }
                },
                Err(e) => {
                    let latency = start.elapsed().as_millis() as u32;
                    return ProbeResult::new(probe.id.clone(), ProbeStatus::Error)
                        .with_latency(latency)
                        .with_error(format!("Cannot read file metadata: {}", e));
                }
            }
        }

        let latency = start.elapsed().as_millis() as u32;
        ProbeResult::new(probe.id.clone(), ProbeStatus::Healthy).with_latency(latency)
    }

    /// Execute a port probe (TCP connectivity).
    async fn execute_port(probe: &HealthProbe) -> ProbeResult {
        let start = Instant::now();

        // Parse target as host:port
        let target = &probe.target;

        // Try to connect
        match TcpStream::connect(target).await {
            Ok(mut stream) => {
                // Connection successful - try to send/receive to ensure it's truly alive
                let latency = start.elapsed().as_millis() as u32;

                // Gracefully shutdown the connection
                let _ = stream.shutdown().await;

                debug!(target = %target, latency_ms = latency, "Port probe healthy");
                ProbeResult::new(probe.id.clone(), ProbeStatus::Healthy).with_latency(latency)
            }
            Err(e) => {
                let latency = start.elapsed().as_millis() as u32;
                warn!(target = %target, error = %e, "Port probe failed");
                ProbeResult::new(probe.id.clone(), ProbeStatus::Unhealthy)
                    .with_latency(latency)
                    .with_error(format!("Connection failed: {}", e))
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_probe_type_conversion() {
        assert_eq!(ProbeType::Http.as_str(), "http");
        assert_eq!(ProbeType::from_str("http"), ProbeType::Http);
        assert_eq!(ProbeType::from_str("HTTPS"), ProbeType::Http);
        assert_eq!(ProbeType::from_str("command"), ProbeType::Command);
        assert_eq!(ProbeType::from_str("tcp"), ProbeType::Port);
        assert_eq!(ProbeType::from_str("unknown"), ProbeType::Custom);
    }

    #[test]
    fn test_probe_status_conversion() {
        assert_eq!(ProbeStatus::Healthy.as_str(), "healthy");
        assert_eq!(ProbeStatus::from_str("healthy"), ProbeStatus::Healthy);
        assert_eq!(ProbeStatus::from_str("ok"), ProbeStatus::Healthy);
        assert_eq!(ProbeStatus::from_str("down"), ProbeStatus::Unhealthy);
        assert!(ProbeStatus::Healthy.is_healthy());
        assert!(!ProbeStatus::Unhealthy.is_healthy());
    }

    #[test]
    fn test_probe_result_body_truncation() {
        let long_body = "x".repeat(2000);
        let result =
            ProbeResult::new("test".to_string(), ProbeStatus::Healthy).with_body(long_body);

        assert!(result.response_body.as_ref().unwrap().len() <= 1024);
        assert!(result.response_body.as_ref().unwrap().ends_with("..."));
    }
}