Back · shared
import os
import time
import datetime

class AmbientEngine:
    """
    Ambient AI Engine: Minimizing interaction, maximizing wellbeing.
    Follows BRAIN_PROMPT.md principles.
    """
    def __init__(self):
        self.last_check = time.time()
        self.boot_time = self._get_boot_time()
        self.last_net_stats = self._get_net_stats()

    def _get_boot_time(self):
        # Linux specific way to get boot time
        try:
            with open('/proc/stat', 'r') as f:
                for line in f:
                    if line.startswith('btime'):
                        return int(line.split()[1])
        except Exception:
            return time.time()
        return time.time()

    def _get_net_stats(self):
        """Get total network bytes received."""
        try:
            with open('/proc/net/dev', 'r') as f:
                lines = f.readlines()
                total_rx = 0
                for line in lines[2:]:  # Skip headers
                    parts = line.split()
                    if len(parts) > 1:
                        total_rx += int(parts[1])
                return total_rx
        except Exception:
            return 0

    def get_cognitive_load(self):
        """Simulate cognitive load detection via network spikes."""
        current_net = self._get_net_stats()
        diff = current_net - self.last_net_stats
        self.last_net_stats = current_net

        # Define high load as significant network activity
        if diff > 1024 * 1024:  # > 1MB since last check
            return "High"
        return "Normal"

    def trigger_haptic_feedback(self, intensity="mild"):
        """Ambient Principle: Invisibility over Interface."""
        # Simulated haptic feedback (e.g., vibrating a watch or phone)
        # In a real wearable, this would call a driver.
        return f"[HAPTIC FEEDBACK: {intensity.upper()}] (Silent nudge to user)"

    def get_uptime_minutes(self):
        uptime_seconds = time.time() - self.boot_time
        return int(uptime_seconds // 60)

    def analyze_wellbeing(self):
        uptime = self.get_uptime_minutes()
        load = self.get_cognitive_load()

        # Ambient Principle: Human Wellbeing First
        if uptime > 120 or load == "High":
            haptic = self.trigger_haptic_feedback("pulse")
            return f"Recommendation: High activity/time detected. {haptic} Suggesting a break."
        return "System state: Optimal for focus."

    def pulse(self):
        """The 'heartbeat' of the Ambient AI."""
        now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        status = self.analyze_wellbeing()
        return f"[{now}] Ambient AI Heartbeat: {status}"

if __name__ == "__main__":
    engine = AmbientEngine()
    print("Ambient AI Engine initialized.")
    print(engine.pulse())