Skip to main content

brainos_ganglia/
lib.rs

1//! # Brain Ganglia (Basal Ganglia)
2//!
3//! Proactivity engine — habit detection and open-loop tracking.
4//!
5//! **Habit engine** detects behavioral patterns by bucketing episodic memory
6//! into (keyword, day-of-week, hour) histograms.  When a pattern crosses the
7//! occurrence threshold at the current time slot, and rate-limit / quiet-hour
8//! gates pass, a proactive message is emitted.
9//!
10//! **Open-loop detector** finds unresolved commitments ("I need to …",
11//! "remind me to …") in episodic memory and surfaces reminders.
12
13pub mod habit;
14pub mod openloop;
15
16use chrono::{DateTime, Utc};
17use serde::{Deserialize, Serialize};
18use thiserror::Error;
19
20// ─── Errors ──────────────────────────────────────────────────────────────────
21
22#[derive(Debug, Error)]
23pub enum GangliaError {
24    #[error("Storage error: {0}")]
25    Storage(#[from] storage::sqlite::SqliteError),
26}
27
28// ─── Shared types ────────────────────────────────────────────────────────────
29
30/// A recurring topic pattern detected in episodic memory.
31#[derive(Debug, Clone, Serialize, Deserialize)]
32pub struct TopicPattern {
33    /// The recurring keyword.
34    pub topic: String,
35    /// Day-of-week (0 = Monday, 6 = Sunday).
36    pub day_of_week: u8,
37    /// Hour-of-day bucket (0–23).
38    pub hour: u8,
39    /// Number of episodes that confirm this pattern.
40    pub occurrences: usize,
41    /// Originating agent for this pattern (if all occurrences are from one agent).
42    pub agent: Option<String>,
43}
44
45/// A proactive message generated by the ganglia subsystem.
46#[derive(Debug, Clone)]
47pub struct ProactiveMessage {
48    /// Message to surface to the user.
49    pub content: String,
50    /// The topic that triggered the message.
51    pub triggered_by: String,
52    /// Generation timestamp.
53    pub created_at: DateTime<Utc>,
54    /// Originating agent (if the pattern is agent-attributed).
55    pub agent: Option<String>,
56}
57
58// ─── Re-exports ──────────────────────────────────────────────────────────────
59
60pub use habit::{HabitConfig, HabitEngine};
61pub use openloop::{OpenLoop, OpenLoopConfig, OpenLoopDetector};