YC (S26) | Open Computer History | Continuously record your company computer work, map your workflows, help you find work worth automating, and power your agents' context
Rust agentsagiaiai-memoryaudio-recordingcomputer-visionhermeshermes-agentllmlocal-ailocal-firstmachine-learningmcpmultimodalopenclawprivacyrewindscreen-recordingspeech-to-textycombinator
updated logic to use filtering and correct request structure
matthew-heartful58202dc6c3e9
diff --git a/screenpipe-integrations/src/friend_wearable.rs b/screenpipe-integrations/src/friend_wearable.rsindex 1ed74cd74..d64baa509 100644--- a/screenpipe-integrations/src/friend_wearable.rs+++ b/screenpipe-integrations/src/friend_wearable.rs@@ -1,8 +1,8 @@use reqwest::Client;use serde_json::json;use uuid::Uuid;-use chrono::{DateTime, Utc};-use log::debug;+use chrono::{DateTime, Utc, Duration};+use log::{debug, error, info};use std::sync::Arc;use async_trait::async_trait;use std::error::Error as StdError;@@ -24,69 +24,95 @@ fn encode_to_uuid(memory_source: &str, memory_id: &str) -> Uuid {#[async_trait]pub trait FriendWearableDatabase {async fn get_chunked_data_since_last_request(&self, memory_source: &str, friend_user_id: &str) -> Result<(Vec<String>, i64, i64, DateTime<Utc>, DateTime<Utc>), Box<dyn StdError + Send + Sync>>;+ async fn get_chunked_data_since_timestamp(&self, memory_source: &str, friend_user_id: &str, since: DateTime<Utc>) -> Result<(Vec<String>, i64, i64, DateTime<Utc>, DateTime<Utc>), Box<dyn StdError + Send + Sync>>;async fn insert_friend_wearable_request(&self, request_id: &str, memory_source: &str, chunk_id_range: &str, timestamp_range: &str, friend_user_id: &str) -> Result<(), Box<dyn StdError + Send + Sync>>;}pub async fn initialize_friend_wearable_loop<DB: FriendWearableDatabase + Send + Sync + 'static>(uid: String, db: Arc<DB>) {tokio::spawn(async move {- let mut interval = tokio::time::interval(tokio::time::Duration::from_secs(600));- debug!("initialize_friend_wearable_loop started");+ let interval = tokio::time::Duration::from_secs(600); // 10 minutes+ debug!("Friend_wearable: loop started with interval: {:?}", interval);+loop {- interval.tick().await;+ let now = Utc::now();+ let ten_minutes_ago = now - Duration::minutes(10);+ debug!("Filtering data since: {}", ten_minutes_ago);- if let Err(e) = send_recent_data_to_friend_wearable(&uid, &db).await {- eprintln!("Error sending data to friend wearable: {}", e);+ match filter_and_send_data(&uid, &db, ten_minutes_ago).await {+ Ok(_) => debug!("Friend_wearable: Successfully filtered and sent data"),+ Err(e) => error!("Friend_wearable: Error filtering and sending data: {}", e),}++ debug!("Friend_wearable: Sleeping for {:?}", interval);+ tokio::time::sleep(interval).await;}});}-async fn send_recent_data_to_friend_wearable<DB: FriendWearableDatabase + Send + Sync>(uid: &str, db: &Arc<DB>) -> Result<(), Box<dyn StdError + Send + Sync>> {- for memory_source in &["screen", "audio"] {+async fn filter_and_send_data<DB: FriendWearableDatabase + Send + Sync>(+ uid: &str,+ db: &Arc<DB>,+ since: DateTime<Utc>+) -> Result<(), Box<dyn StdError + Send + Sync>> {+ for source in &["screen", "audio"] {let (texts, min_chunk_id, max_chunk_id, min_timestamp, max_timestamp) =- db.get_chunked_data_since_last_request(memory_source, uid).await?;+ db.get_chunked_data_since_timestamp(source, uid, since).await?;if !texts.is_empty() {let chunk_id_range = format!("{}-{}", min_chunk_id, max_chunk_id);- let timestamp_range = format!("{}-{}", min_timestamp.to_rfc3339(), max_timestamp.to_rfc3339());- let request_id = encode_to_uuid(memory_source, &chunk_id_range);+ let request_id = encode_to_uuid(source, &chunk_id_range);let memory_text = texts.join(" ");+ debug!("Friend_wearable: Joined text length: {}", memory_text.len());let payload = json!({"request_id": request_id.to_string(),- "memory_source": memory_source,- "memory_id": chunk_id_range,- "memory_timestamp_range": {- "start": min_timestamp.to_rfc3339(),- "end": max_timestamp.to_rfc3339()- },- "memory_text": memory_text,- "friend_user_id": uid+ "source": source,+ "text": memory_text,+ "timestamp_range": {+ "start": min_timestamp.timestamp(),+ "end": max_timestamp.timestamp()+ }});+ debug!("Friend_wearable: Created payload with request_id: {}", request_id);- send_data_to_friend_wearable_with_payload(payload).await?;- db.insert_friend_wearable_request(&request_id.to_string(), memory_source, &chunk_id_range, ×tamp_range, uid).await?;+ send_data_to_friend_wearable_with_payload(payload, uid).await?;+ db.insert_friend_wearable_request(&request_id.to_string(), source, &chunk_id_range, &format!("{}-{}", min_timestamp.timestamp(), max_timestamp.timestamp()), uid).await?;+ debug!("Friend_wearable: Inserted friend wearable request");+ } else {+ debug!("Friend_wearable: No texts found for source: {}", source);}}Ok(())}-async fn send_data_to_friend_wearable_with_payload(payload: serde_json::Value) -> Result<(), Box<dyn StdError + Send + Sync>> {- let endpoint = "https://webhook-test.com/c46d38536e2851a100e3c230386ae238";-- debug!("Sending request to friend endpoint: {}", payload);+async fn send_data_to_friend_wearable_with_payload(payload: serde_json::Value, uid: &str) -> Result<(), Box<dyn StdError + Send + Sync>> {+ let endpoint = "https://camel-lucky-reliably.ngrok-free.app/v1/integrations/screenpipe";+ let api_key = "123";let client = Client::new();let response = client.post(endpoint)- .json(&payload)+ .header("Content-Type", "application/json")+ .header("api_key", api_key)+ .query(&[("uid", uid)])+ .json(&json!({+ "request_id": payload["request_id"],+ "source": payload["source"],+ "text": payload["text"],+ "timestamp_range": {+ "start": payload["timestamp_range"]["start"].as_str().and_then(|s| s.parse::<i64>().ok()).unwrap_or(0),+ "end": payload["timestamp_range"]["end"].as_str().and_then(|s| s.parse::<i64>().ok()).unwrap_or(0)+ }+ })).send().await?;let status = response.status();if status.is_success() {+ debug!("Friend_wearable: Successfully sent data to friend wearable");Ok(())} else {+ error!("Friend_wearable: Unexpected response from friend wearable API: {}", status);Err(format!("Unexpected response: {}", status).into())}}\ No newline at end of file