chasm-cli 2.0.0

Universal chat session manager - harvest, merge, and analyze AI chat history from VS Code, Cursor, and other editors
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
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
// Copyright (c) 2024-2026 Nervosys LLC
// SPDX-License-Identifier: AGPL-3.0-only
//! Proactive Agent System
//!
//! Agents that monitor, detect problems, and solve them with user permission.
//!
//! ## Household Agent
//! - Smart home monitoring
//! - Bill and expense tracking
//! - Maintenance scheduling
//! - Grocery and supply management
//!
//! ## Business Agent
//! - Calendar optimization
//! - Email triage and follow-ups
//! - Meeting preparation
//! - Project health monitoring

#![allow(dead_code)]

use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

// =============================================================================
// Permission System
// =============================================================================

/// Permission level for proactive actions
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[derive(Default)]
pub enum PermissionLevel {
    /// Agent can only send notifications, no actions taken
    NotifyOnly,
    /// Auto-approve low-risk actions (notifications, reading data)
    #[default]
    LowRisk,
    /// Auto-approve medium-risk (scheduling, drafts)
    MediumRisk,
    /// High autonomy - approve most except financial/external comms
    HighAutonomy,
}

impl PermissionLevel {
    /// Actions that can be auto-approved at this level
    pub fn auto_approve_actions(&self) -> Vec<&'static str> {
        match self {
            PermissionLevel::NotifyOnly => vec![],
            PermissionLevel::LowRisk => vec![
                "send_notification",
                "calendar_read",
                "email_read",
                "web_search",
                "package_track",
                "weather_check",
                "energy_read",
            ],
            PermissionLevel::MediumRisk => vec![
                "send_notification",
                "calendar_read",
                "calendar_create",
                "email_read",
                "email_draft",
                "document_create",
                "web_search",
                "package_track",
                "weather_check",
                "energy_read",
                "reminder_create",
                "task_create",
            ],
            PermissionLevel::HighAutonomy => vec!["*"], // All except blocklist
        }
    }

    /// Actions that always require approval regardless of level
    pub fn always_require_approval() -> Vec<&'static str> {
        vec![
            "bill_pay",
            "email_send",
            "slack_send",
            "teams_send",
            "discord_send",
            "sms_send",
            "expense_submit",
            "purchase",
            "transfer_money",
            "delete_file",
            "share_external",
            "change_password",
        ]
    }

    /// Check if an action can be auto-approved
    pub fn can_auto_approve(&self, action: &str) -> bool {
        // Check blocklist first
        if Self::always_require_approval().contains(&action) {
            return false;
        }

        let allowed = self.auto_approve_actions();
        allowed.contains(&"*") || allowed.contains(&action)
    }
}

// =============================================================================
// Problem Detection
// =============================================================================

/// Severity level of a detected problem
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum ProblemSeverity {
    /// Informational, no action required
    Info,
    /// Warning, action recommended
    Warning,
    /// Urgent, action needed soon
    Urgent,
    /// Critical, immediate action required
    Critical,
}

/// Status of a detected problem
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ProblemStatus {
    /// Newly detected
    #[default]
    New,
    /// User acknowledged
    Acknowledged,
    /// Being addressed
    InProgress,
    /// Problem resolved
    Resolved,
    /// User dismissed
    Dismissed,
}

/// Category of problem (household or business)
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ProblemCategory {
    // Household
    BillDue {
        amount: f64,
        due_date: DateTime<Utc>,
        vendor: String,
    },
    MaintenanceNeeded {
        item: String,
        urgency: String,
    },
    SupplyLow {
        item: String,
        current_quantity: u32,
        reorder_threshold: u32,
    },
    EnergyAnomaly {
        device: String,
        expected_kwh: f64,
        actual_kwh: f64,
    },
    DeviceOffline {
        device_id: String,
        device_name: String,
        last_seen: DateTime<Utc>,
    },
    SecurityAlert {
        alert_type: String,
        location: String,
    },
    PackageDelayed {
        tracking_id: String,
        carrier: String,
        expected_date: String,
    },
    AppointmentReminder {
        title: String,
        time: DateTime<Utc>,
        location: Option<String>,
    },
    WeatherAlert {
        alert_type: String,
        severity: String,
    },
    SubscriptionRenewal {
        service: String,
        amount: f64,
        renewal_date: DateTime<Utc>,
    },

    // Business
    CalendarConflict {
        event1: String,
        event2: String,
        overlap_minutes: u32,
    },
    DeadlineApproaching {
        project: String,
        deadline: DateTime<Utc>,
        days_remaining: u32,
    },
    EmailUrgent {
        from: String,
        subject: String,
        received_at: DateTime<Utc>,
    },
    MeetingPrepNeeded {
        meeting: String,
        time: DateTime<Utc>,
        prep_items: Vec<String>,
    },
    FollowUpDue {
        context: String,
        person: String,
        due_date: DateTime<Utc>,
    },
    ExpensePending {
        amount: f64,
        category: String,
        days_pending: u32,
    },
    ProjectAtRisk {
        project: String,
        risk_factors: Vec<String>,
    },
    CompetitorNews {
        competitor: String,
        headline: String,
    },
    TeamBlocker {
        team_member: String,
        blocker: String,
    },
    ReportDue {
        report_name: String,
        due_date: DateTime<Utc>,
    },

    /// Custom problem type
    Custom {
        category: String,
        details: HashMap<String, serde_json::Value>,
    },
}

/// A detected problem requiring attention
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DetectedProblem {
    /// Unique problem ID
    pub id: String,
    /// Agent that detected the problem
    pub agent_id: String,
    /// Problem category with details
    pub category: ProblemCategory,
    /// Human-readable title
    pub title: String,
    /// Detailed description
    pub description: String,
    /// Problem severity
    pub severity: ProblemSeverity,
    /// When the problem was detected
    pub detected_at: DateTime<Utc>,
    /// Source of detection (integration, scan, etc.)
    pub source: String,
    /// Suggested actions to resolve
    pub suggested_actions: Vec<ProactiveAction>,
    /// Current status
    pub status: ProblemStatus,
    /// When the problem was resolved
    pub resolved_at: Option<DateTime<Utc>>,
    /// Additional metadata
    pub metadata: HashMap<String, serde_json::Value>,
}

// =============================================================================
// Proactive Actions
// =============================================================================

/// Risk level of an action
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum ActionRisk {
    Low,
    Medium,
    High,
}

/// Status of a proactive action
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ActionStatus {
    /// Waiting for user approval
    #[default]
    Pending,
    /// User approved the action
    Approved,
    /// User rejected the action
    Rejected,
    /// Action was executed
    Executed,
    /// Action was cancelled
    Cancelled,
    /// Action failed during execution
    Failed,
}

/// A proactive action proposed by an agent
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProactiveAction {
    /// Unique action ID
    pub id: String,
    /// Agent proposing the action
    pub agent_id: String,
    /// Related problem ID (if any)
    pub problem_id: Option<String>,
    /// Type of action (e.g., "send_email", "schedule_maintenance")
    pub action_type: String,
    /// Human-readable description
    pub description: String,
    /// Why the agent recommends this action
    pub reasoning: String,
    /// Estimated impact/benefit
    pub estimated_impact: Option<String>,
    /// Risk level
    pub risk_level: ActionRisk,
    /// Current status
    pub status: ActionStatus,
    /// Whether it was auto-approved based on permission level
    pub auto_approved: bool,
    /// When the action was approved
    pub approved_at: Option<DateTime<Utc>>,
    /// Who approved (user ID or "auto")
    pub approved_by: Option<String>,
    /// When the action was executed
    pub executed_at: Option<DateTime<Utc>>,
    /// Result of execution
    pub result: Option<String>,
    /// Error if execution failed
    pub error: Option<String>,
    /// Action parameters
    pub parameters: HashMap<String, serde_json::Value>,
    /// When the action was created
    pub created_at: DateTime<Utc>,
}

// =============================================================================
// Proactive Agent Configuration
// =============================================================================

/// Configuration for a proactive agent
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProactiveAgentConfig {
    /// Agent ID
    pub agent_id: String,
    /// Whether the agent is enabled
    pub enabled: bool,
    /// Permission level for auto-approving actions
    pub permission_level: PermissionLevel,
    /// Integrations to monitor
    pub monitored_integrations: Vec<String>,
    /// Problem categories to watch for
    pub watch_categories: Vec<String>,
    /// How often to scan (in seconds)
    pub scan_interval_secs: u64,
    /// Quiet hours - don't notify during these times
    pub quiet_hours: Option<QuietHours>,
    /// Maximum pending actions before requiring review
    pub max_pending_actions: u32,
    /// Custom rules for auto-approval
    pub custom_rules: Vec<ApprovalRule>,
}

impl Default for ProactiveAgentConfig {
    fn default() -> Self {
        Self {
            agent_id: String::new(),
            enabled: true,
            permission_level: PermissionLevel::LowRisk,
            monitored_integrations: Vec::new(),
            watch_categories: Vec::new(),
            scan_interval_secs: 300, // 5 minutes
            quiet_hours: None,
            max_pending_actions: 10,
            custom_rules: Vec::new(),
        }
    }
}

/// Quiet hours configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct QuietHours {
    /// Start time (e.g., "22:00")
    pub start: String,
    /// End time (e.g., "07:00")
    pub end: String,
    /// Days to apply (0=Sunday, 6=Saturday)
    pub days: Vec<u8>,
    /// Whether to still allow critical alerts
    pub allow_critical: bool,
}

/// Custom approval rule
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ApprovalRule {
    /// Rule name
    pub name: String,
    /// Action type to match
    pub action_type: String,
    /// Conditions that must be met
    pub conditions: Vec<RuleCondition>,
    /// Whether to auto-approve if conditions match
    pub auto_approve: bool,
}

/// Condition for an approval rule
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RuleCondition {
    /// Field to check
    pub field: String,
    /// Operator
    pub operator: ConditionOperator,
    /// Value to compare
    pub value: serde_json::Value,
}

/// Condition operators
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ConditionOperator {
    Equals,
    NotEquals,
    GreaterThan,
    LessThan,
    Contains,
    StartsWith,
    EndsWith,
    Matches,
}

// =============================================================================
// Household Agent Specifics
// =============================================================================

/// Household agent preset configuration
pub fn household_agent_config() -> ProactiveAgentConfig {
    ProactiveAgentConfig {
        agent_id: "household".to_string(),
        enabled: true,
        permission_level: PermissionLevel::LowRisk,
        monitored_integrations: vec![
            "home_assistant".to_string(),
            "google_calendar".to_string(),
            "gmail".to_string(),
            "todoist".to_string(),
            "plaid".to_string(),
            "amazon".to_string(),
            "instacart".to_string(),
            "hue".to_string(),
            "nest".to_string(),
        ],
        watch_categories: vec![
            "bill_due".to_string(),
            "maintenance_needed".to_string(),
            "supply_low".to_string(),
            "energy_anomaly".to_string(),
            "device_offline".to_string(),
            "security_alert".to_string(),
            "package_delayed".to_string(),
            "appointment_reminder".to_string(),
            "weather_alert".to_string(),
            "subscription_renewal".to_string(),
        ],
        scan_interval_secs: 300,
        quiet_hours: Some(QuietHours {
            start: "22:00".to_string(),
            end: "07:00".to_string(),
            days: vec![0, 1, 2, 3, 4, 5, 6],
            allow_critical: true,
        }),
        max_pending_actions: 10,
        custom_rules: vec![ApprovalRule {
            name: "auto_remind_low_supplies".to_string(),
            action_type: "send_notification".to_string(),
            conditions: vec![RuleCondition {
                field: "category".to_string(),
                operator: ConditionOperator::Equals,
                value: serde_json::json!("supply_low"),
            }],
            auto_approve: true,
        }],
    }
}

// =============================================================================
// Business Agent Specifics
// =============================================================================

/// Business agent preset configuration
pub fn business_agent_config() -> ProactiveAgentConfig {
    ProactiveAgentConfig {
        agent_id: "business".to_string(),
        enabled: true,
        permission_level: PermissionLevel::MediumRisk,
        monitored_integrations: vec![
            "google_calendar".to_string(),
            "outlook".to_string(),
            "gmail".to_string(),
            "slack".to_string(),
            "teams".to_string(),
            "notion".to_string(),
            "linear".to_string(),
            "github".to_string(),
        ],
        watch_categories: vec![
            "calendar_conflict".to_string(),
            "deadline_approaching".to_string(),
            "email_urgent".to_string(),
            "meeting_prep_needed".to_string(),
            "follow_up_due".to_string(),
            "expense_pending".to_string(),
            "project_at_risk".to_string(),
            "competitor_news".to_string(),
            "team_blocker".to_string(),
            "report_due".to_string(),
        ],
        scan_interval_secs: 300,
        quiet_hours: Some(QuietHours {
            start: "20:00".to_string(),
            end: "08:00".to_string(),
            days: vec![0, 6], // Weekends only
            allow_critical: true,
        }),
        max_pending_actions: 15,
        custom_rules: vec![
            ApprovalRule {
                name: "auto_prep_meeting_docs".to_string(),
                action_type: "document_create".to_string(),
                conditions: vec![RuleCondition {
                    field: "category".to_string(),
                    operator: ConditionOperator::Equals,
                    value: serde_json::json!("meeting_prep_needed"),
                }],
                auto_approve: true,
            },
            ApprovalRule {
                name: "auto_flag_deadline".to_string(),
                action_type: "send_notification".to_string(),
                conditions: vec![RuleCondition {
                    field: "days_remaining".to_string(),
                    operator: ConditionOperator::LessThan,
                    value: serde_json::json!(3),
                }],
                auto_approve: true,
            },
        ],
    }
}

// =============================================================================
// Agent Traits
// =============================================================================

/// Trait for proactive monitoring agents
#[async_trait::async_trait]
pub trait ProactiveMonitor: Send + Sync {
    /// Scan integrations for problems
    async fn scan(&self) -> Result<Vec<DetectedProblem>, Box<dyn std::error::Error + Send + Sync>>;

    /// Propose actions for a detected problem
    async fn propose_actions(
        &self,
        problem: &DetectedProblem,
    ) -> Result<Vec<ProactiveAction>, Box<dyn std::error::Error + Send + Sync>>;

    /// Execute an approved action
    async fn execute_action(
        &self,
        action: &ProactiveAction,
    ) -> Result<String, Box<dyn std::error::Error + Send + Sync>>;

    /// Check if an action can be auto-approved
    fn can_auto_approve(&self, action: &ProactiveAction) -> bool;
}

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

    #[test]
    fn test_permission_levels() {
        assert!(!PermissionLevel::NotifyOnly.can_auto_approve("send_notification"));
        assert!(PermissionLevel::LowRisk.can_auto_approve("send_notification"));
        assert!(PermissionLevel::LowRisk.can_auto_approve("calendar_read"));
        assert!(!PermissionLevel::LowRisk.can_auto_approve("bill_pay"));
        assert!(PermissionLevel::MediumRisk.can_auto_approve("calendar_create"));
        assert!(!PermissionLevel::HighAutonomy.can_auto_approve("email_send")); // Always requires approval
    }

    #[test]
    fn test_household_config() {
        let config = household_agent_config();
        assert_eq!(config.agent_id, "household");
        assert!(config
            .monitored_integrations
            .contains(&"home_assistant".to_string()));
    }

    #[test]
    fn test_business_config() {
        let config = business_agent_config();
        assert_eq!(config.agent_id, "business");
        assert!(config
            .watch_categories
            .contains(&"calendar_conflict".to_string()));
    }
}