synheart-sensor-agent 0.2.2

Privacy-first PC background sensor for behavioral research
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
//! Gateway client for syncing HSI snapshots to synheart-core-gateway.
//!
//! Provides integration with the local synheart-core-gateway for real-time
//! HSI processing. Use [`GatewayConfig`] to configure the connection and
//! [`GatewayClient`] (async) or [`BlockingGatewayClient`] (sync) to send
//! [`HsiSnapshot`](crate::core::HsiSnapshot)s.

use crate::core::HsiSnapshot;
use serde::{Deserialize, Serialize};
use std::path::PathBuf;

/// Gateway configuration.
#[derive(Debug, Clone)]
pub struct GatewayConfig {
    /// Gateway host (default: 127.0.0.1)
    pub host: String,
    /// Gateway port
    pub port: u16,
    /// Bearer authentication token
    pub token: String,
}

impl GatewayConfig {
    /// Create a new gateway configuration.
    pub fn new(host: impl Into<String>, port: u16, token: impl Into<String>) -> Self {
        Self {
            host: host.into(),
            port,
            token: token.into(),
        }
    }

    /// Load configuration from SyniLife runtime directory.
    ///
    /// Reads port from `~/Library/Application Support/SyniLife/runtime/gateway.port`
    /// and token from `~/Library/Application Support/SyniLife/runtime/gateway.token`
    pub fn from_runtime_dir() -> Result<Self, GatewayError> {
        let state_dir = Self::default_state_dir()?;
        let runtime_dir = state_dir.join("runtime");

        let port_path = runtime_dir.join("gateway.port");
        let token_path = runtime_dir.join("gateway.token");

        let port_str = std::fs::read_to_string(&port_path).map_err(|e| {
            GatewayError::Config(format!(
                "Failed to read gateway port from {port_path:?}: {e}"
            ))
        })?;

        let port: u16 = port_str.trim().parse().map_err(|e| {
            GatewayError::Config(format!("Invalid port number '{}': {}", port_str.trim(), e))
        })?;

        let token = std::fs::read_to_string(&token_path)
            .map_err(|e| {
                GatewayError::Config(format!(
                    "Failed to read gateway token from {token_path:?}: {e}"
                ))
            })?
            .trim()
            .to_string();

        Ok(Self {
            host: "127.0.0.1".to_string(),
            port,
            token,
        })
    }

    /// Get the default SyniLife state directory.
    fn default_state_dir() -> Result<PathBuf, GatewayError> {
        #[cfg(target_os = "macos")]
        {
            if let Some(home) = dirs::home_dir() {
                return Ok(home.join("Library/Application Support/SyniLife"));
            }
        }

        #[cfg(target_os = "linux")]
        {
            if let Some(data_dir) = dirs::data_dir() {
                return Ok(data_dir.join("SyniLife"));
            }
        }

        #[cfg(target_os = "windows")]
        {
            if let Some(data_dir) = dirs::data_dir() {
                return Ok(data_dir.join("SyniLife"));
            }
        }

        Err(GatewayError::Config(
            "Could not determine SyniLife state directory".to_string(),
        ))
    }

    /// Get the full gateway URL.
    pub fn url(&self) -> String {
        format!("http://{}:{}", self.host, self.port)
    }

    /// Get the behavioral ingest endpoint URL.
    pub fn ingest_url(&self) -> String {
        format!("{}/v1/ingest/behavioral", self.url())
    }

    /// Get the health check endpoint URL.
    pub fn health_url(&self) -> String {
        format!("{}/health", self.url())
    }
}

/// Gateway client error types.
#[derive(Debug)]
pub enum GatewayError {
    /// Configuration error
    Config(String),
    /// Network/HTTP error
    Network(String),
    /// Server returned an error response.
    Server {
        /// HTTP status code.
        status: u16,
        /// Error message from the server.
        message: String,
    },
    /// JSON serialization error
    Serialization(String),
}

impl std::fmt::Display for GatewayError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            GatewayError::Config(msg) => write!(f, "Gateway config error: {msg}"),
            GatewayError::Network(msg) => write!(f, "Gateway network error: {msg}"),
            GatewayError::Server { status, message } => {
                write!(f, "Gateway server error ({status}): {message}")
            }
            GatewayError::Serialization(msg) => write!(f, "Gateway serialization error: {msg}"),
        }
    }
}

impl std::error::Error for GatewayError {}

/// Session payload for the behavioral ingest endpoint.
#[derive(Debug, Clone, Serialize)]
pub struct BehavioralSession {
    /// Session containing HSI snapshots
    pub session: SessionPayload,
}

/// Session payload structure matching core-gateway expectations.
#[derive(Debug, Clone, Serialize)]
pub struct SessionPayload {
    /// Session identifier
    pub session_id: String,
    /// Device identifier
    pub device_id: String,
    /// Timezone
    pub timezone: String,
    /// Session start time (RFC3339)
    pub start_time: String,
    /// Session end time (RFC3339)
    pub end_time: String,
    /// HSI snapshots as events
    pub snapshots: Vec<HsiSnapshot>,
    /// Metadata
    pub meta: SessionMeta,
}

/// Session metadata.
#[derive(Debug, Clone, Serialize)]
pub struct SessionMeta {
    /// Source identifier
    pub source: String,
    /// Version
    pub version: String,
    /// Snapshot count
    pub snapshot_count: usize,
}

/// Gateway response from the behavioral ingest endpoint.
#[derive(Debug, Clone, Deserialize)]
pub struct GatewayResponse {
    /// Timestamp of processing
    pub timestamp: String,
    /// Flux payload (if processed)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub flux_payload: Option<serde_json::Value>,
    /// HSI state summary
    #[serde(skip_serializing_if = "Option::is_none")]
    pub state: Option<HsiState>,
}

/// HSI state summary from gateway.
#[derive(Debug, Clone, Deserialize)]
pub struct HsiState {
    /// Focus level
    pub focus: Option<String>,
    /// Load level
    pub load: Option<String>,
    /// Recovery level
    pub recovery: Option<String>,
}

impl std::fmt::Display for HsiState {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let focus = self.focus.as_deref().unwrap_or("unknown");
        let load = self.load.as_deref().unwrap_or("unknown");
        let recovery = self.recovery.as_deref().unwrap_or("unknown");
        write!(f, "focus: {focus}, load: {load}, recovery: {recovery}")
    }
}

/// Gateway client for syncing with synheart-core-gateway.
#[cfg(feature = "gateway")]
pub struct GatewayClient {
    config: GatewayConfig,
    client: reqwest::Client,
    device_id: String,
}

#[cfg(feature = "gateway")]
impl GatewayClient {
    /// Create a new gateway client.
    pub fn new(config: GatewayConfig) -> Self {
        let client = reqwest::Client::builder()
            .timeout(std::time::Duration::from_secs(10))
            .build()
            .expect("Failed to create HTTP client");

        // Generate device ID from hostname + instance
        let hostname = hostname::get()
            .map(|h| h.to_string_lossy().to_string())
            .unwrap_or_else(|_| "unknown".to_string());
        let device_id = format!(
            "sensor-{}-{}",
            hostname,
            &uuid::Uuid::new_v4().to_string()[..8]
        );

        Self {
            config,
            client,
            device_id,
        }
    }

    /// Create a new gateway client from runtime directory configuration.
    pub fn from_runtime() -> Result<Self, GatewayError> {
        let config = GatewayConfig::from_runtime_dir()?;
        Ok(Self::new(config))
    }

    /// Test connection to the gateway.
    pub async fn test_connection(&self) -> Result<bool, GatewayError> {
        let response = self
            .client
            .get(self.config.health_url())
            .send()
            .await
            .map_err(|e| GatewayError::Network(e.to_string()))?;

        Ok(response.status().is_success())
    }

    /// Sync HSI snapshots to the gateway.
    pub async fn sync_snapshots(
        &self,
        snapshots: &[HsiSnapshot],
        session_id: &str,
    ) -> Result<GatewayResponse, GatewayError> {
        if snapshots.is_empty() {
            return Err(GatewayError::Config("No snapshots to sync".to_string()));
        }

        // Build session payload
        let start_time = snapshots
            .first()
            .map(|s| s.observed_at_utc.clone())
            .unwrap_or_default();
        let end_time = snapshots
            .last()
            .map(|s| s.computed_at_utc.clone())
            .unwrap_or_default();

        let timezone = chrono_tz::Tz::UTC.to_string();

        let session = BehavioralSession {
            session: SessionPayload {
                session_id: session_id.to_string(),
                device_id: self.device_id.clone(),
                timezone,
                start_time,
                end_time,
                snapshots: snapshots.to_vec(),
                meta: SessionMeta {
                    source: "synheart-sensor-agent".to_string(),
                    version: env!("CARGO_PKG_VERSION").to_string(),
                    snapshot_count: snapshots.len(),
                },
            },
        };

        let response = self
            .client
            .post(self.config.ingest_url())
            .header("Authorization", format!("Bearer {}", self.config.token))
            .header("Content-Type", "application/json")
            .json(&session)
            .send()
            .await
            .map_err(|e| GatewayError::Network(e.to_string()))?;

        let status = response.status();
        if !status.is_success() {
            let message = response
                .text()
                .await
                .unwrap_or_else(|_| "Unknown error".to_string());
            return Err(GatewayError::Server {
                status: status.as_u16(),
                message,
            });
        }

        let gateway_response: GatewayResponse = response
            .json()
            .await
            .map_err(|e| GatewayError::Serialization(e.to_string()))?;

        Ok(gateway_response)
    }

    /// Get the device ID.
    pub fn device_id(&self) -> &str {
        &self.device_id
    }
}

/// Blocking gateway client for use in synchronous contexts.
#[cfg(feature = "gateway")]
pub struct BlockingGatewayClient {
    inner: GatewayClient,
    runtime: tokio::runtime::Runtime,
}

#[cfg(feature = "gateway")]
impl BlockingGatewayClient {
    /// Create a new blocking gateway client.
    pub fn new(config: GatewayConfig) -> Result<Self, GatewayError> {
        let runtime = tokio::runtime::Builder::new_current_thread()
            .enable_all()
            .build()
            .map_err(|e| GatewayError::Config(format!("Failed to create runtime: {e}")))?;

        Ok(Self {
            inner: GatewayClient::new(config),
            runtime,
        })
    }

    /// Create a new blocking gateway client from runtime directory configuration.
    pub fn from_runtime() -> Result<Self, GatewayError> {
        let config = GatewayConfig::from_runtime_dir()?;
        Self::new(config)
    }

    /// Test connection to the gateway.
    pub fn test_connection(&self) -> Result<bool, GatewayError> {
        self.runtime.block_on(self.inner.test_connection())
    }

    /// Sync HSI snapshots to the gateway.
    pub fn sync_snapshots(
        &self,
        snapshots: &[HsiSnapshot],
        session_id: &str,
    ) -> Result<GatewayResponse, GatewayError> {
        self.runtime
            .block_on(self.inner.sync_snapshots(snapshots, session_id))
    }

    /// Get the device ID.
    pub fn device_id(&self) -> &str {
        self.inner.device_id()
    }
}

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

    #[test]
    fn test_gateway_config_url() {
        let config = GatewayConfig::new("127.0.0.1", 8080, "test-token");
        assert_eq!(config.url(), "http://127.0.0.1:8080");
        assert_eq!(
            config.ingest_url(),
            "http://127.0.0.1:8080/v1/ingest/behavioral"
        );
        assert_eq!(config.health_url(), "http://127.0.0.1:8080/health");
    }

    #[test]
    fn test_hsi_state_display() {
        let state = HsiState {
            focus: Some("high".to_string()),
            load: Some("moderate".to_string()),
            recovery: None,
        };
        let display = format!("{state}");
        assert!(display.contains("high"));
        assert!(display.contains("moderate"));
    }
}