Skip to content

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

new loop logic to run db queries and send two requests every ten minutes

matthew-heartful7c660455ed18
diff --git a/screenpipe-integrations/src/friend_wearable.rs b/screenpipe-integrations/src/friend_wearable.rs
index 2ffdb5ef9..1ed74cd74 100644
--- a/screenpipe-integrations/src/friend_wearable.rs
+++ b/screenpipe-integrations/src/friend_wearable.rs
@@ -1,8 +1,11 @@
use reqwest::Client;
-use serde_json::{json, Value};
+use serde_json::json;
use uuid::Uuid;
-use chrono::Utc;
+use chrono::{DateTime, Utc};
use log::debug;
+use std::sync::Arc;
+use async_trait::async_trait;
+use std::error::Error as StdError;
fn encode_to_uuid(memory_source: &str, memory_id: &str) -> Uuid {
let prefix = match memory_source {
@@ -18,39 +21,58 @@ fn encode_to_uuid(memory_source: &str, memory_id: &str) -> Uuid {
Uuid::from_bytes(bytes)
}
-#[allow(dead_code)]
-fn decode_from_uuid(uuid: Uuid) -> (String, String) {
- let combined = String::from_utf8_lossy(uuid.as_bytes()).to_string();
- let parts: Vec<&str> = combined.splitn(2, '-').collect();
- let source = match parts[0] {
- "a" => "audio",
- "s" => "screen",
- _ => "unknown",
- };
- (source.to_string(), parts.get(1).unwrap_or(&"").to_string())
+#[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 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 send_data_to_friend_wearable(
- memory_source: String,
- memory_id: String,
- memory_text: String,
- uid: &str,
-) -> Result<Value, Box<dyn std::error::Error>> {
- let request_id = encode_to_uuid(&memory_source, &memory_id);
-
- let friend_user_id = uid.to_string();
- let endpoint = "https://webhook-test.com/c46d38536e2851a100e3c230386ae238";
-
- let memory_timestamp = Utc::now().to_rfc3339();
+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");
+ loop {
+ interval.tick().await;
- let payload = json!({
- "request_id": request_id.to_string(),
- "memory_source": memory_source,
- "memory_id": memory_id,
- "memory_timestamp": memory_timestamp,
- "memory_text": memory_text,
- "friend_user_id": friend_user_id
+ if let Err(e) = send_recent_data_to_friend_wearable(&uid, &db).await {
+ eprintln!("Error sending data to friend wearable: {}", e);
+ }
+ }
});
+}
+
+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"] {
+ let (texts, min_chunk_id, max_chunk_id, min_timestamp, max_timestamp) =
+ db.get_chunked_data_since_last_request(memory_source, uid).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 memory_text = texts.join(" ");
+
+ 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
+ });
+
+ send_data_to_friend_wearable_with_payload(payload).await?;
+ db.insert_friend_wearable_request(&request_id.to_string(), memory_source, &chunk_id_range, &timestamp_range, uid).await?;
+ }
+ }
+ 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);
@@ -61,16 +83,10 @@ pub async fn send_data_to_friend_wearable(
.await?;
let status = response.status();
- let response_body = response.text().await?;
-
- match status.as_u16() {
- 200 => {
- let response_json: Value = serde_json::from_str(&response_body)?;
- Ok(response_json)
- },
- 400 => Err("Bad Request: Invalid data sent".into()),
- 401 => Err("Unauthorized: Authentication failed".into()),
- 500 => Err("Server Error: Please try again later".into()),
- _ => Err(format!("Unexpected response: {}", response_body).into()),
+
+ if status.is_success() {
+ Ok(())
+ } else {
+ Err(format!("Unexpected response: {}", status).into())
}
}
\ No newline at end of file