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
fix: window and app fitlering
Louis Beaumont95b6342074cc
diff --git a/screenpipe-server/Cargo.toml b/screenpipe-server/Cargo.tomlindex 0562467f9..bc1465840 100644--- a/screenpipe-server/Cargo.toml+++ b/screenpipe-server/Cargo.toml@@ -107,6 +107,9 @@ rand = "0.8"axum-test = "15.3.0"+# Url encoding+url = "2.4.0"+[[bench]]name = "db_benchmarks"harness = falsediff --git a/screenpipe-server/src/db.rs b/screenpipe-server/src/db.rsindex 6eb39251d..88cb9512d 100644--- a/screenpipe-server/src/db.rs+++ b/screenpipe-server/src/db.rs@@ -32,6 +32,7 @@ impl StdError for DatabaseError {}// Intermediate struct for fetching FTS data#[derive(FromRow)]+#[allow(dead_code)]struct FTSSearchResultRaw {text_id: i64,matched_text: String,@@ -65,7 +66,7 @@ pub enum SearchResult {}// Intermediate struct for fetching data-#[derive(FromRow)]+#[derive(FromRow, Debug)]struct OCRResultRaw {frame_id: i64,ocr_text: String,@@ -563,6 +564,7 @@ impl DatabaseManager {Ok(results)}+ #[allow(dead_code)]async fn search_fts(&self,query: &str,@@ -627,7 +629,7 @@ impl DatabaseManager {start_time: Option<DateTime<Utc>>,end_time: Option<DateTime<Utc>>,app_name: Option<&str>,- window_name: Option<&str>, // Add window_name parameter+ window_name: Option<&str>,) -> Result<Vec<OCRResult>, sqlx::Error> {let mut sql = r#"SELECT@@ -659,30 +661,41 @@ impl DatabaseManager {"#.to_string();- if app_name.is_some() {- sql.push_str(" AND ocr_text.app_name = ?6 COLLATE NOCASE");+ let mut param_count = 5; // We already have 5 parameters++ if let Some(_) = app_name {+ param_count += 1;+ sql.push_str(&format!(+ " AND ocr_text.app_name = ?{} COLLATE NOCASE",+ param_count+ ));}- if window_name.is_some() {- sql.push_str(" AND ocr_text.window_name = ?7 COLLATE NOCASE");+ if let Some(_) = window_name {+ param_count += 1;+ sql.push_str(&format!(+ " AND ocr_text.window_name = ?{} COLLATE NOCASE",+ param_count+ ));}sql.push_str(r#"+ GROUP BY+ ocr_text.frame_idORDER BYframes.timestamp DESCLIMIT ?4 OFFSET ?5"#,);- println!("{}", sql);let mut query = sqlx::query_as::<_, OCRResultRaw>(&sql).bind(query).bind(start_time).bind(end_time).bind(limit).bind(offset);- println!("{:?}", start_time);+if let Some(app_name) = app_name {query = query.bind(app_name);}@@ -693,7 +706,6 @@ impl DatabaseManager {let ocr_results_raw = query.fetch_all(&self.pool).await?;- // Convert OCRResultRaw to OCRResultlet ocr_results = ocr_results_raw.into_iter().map(|raw| OCRResult {@@ -821,7 +833,7 @@ impl DatabaseManager {// If no app_name is specified, proceed with normal countingif content_type == ContentType::All || content_type == ContentType::OCR {let ocr_count = self- .count_ocr_results(query, start_time, end_time, None, window_name) // Add window_name parameter+ .count_ocr_results(query, start_time, end_time, None, None) // Add window_name parameter.await?;total_count += ocr_count;}diff --git a/screenpipe-server/src/migrations/20240821074222_create_tags.sql b/screenpipe-server/src/migrations/20240821074222_create_tags.sqlindex d1fea6eae..12eb3583e 100644--- a/screenpipe-server/src/migrations/20240821074222_create_tags.sql+++ b/screenpipe-server/src/migrations/20240821074222_create_tags.sql@@ -1,7 +1,8 @@-- Create tags tableCREATE TABLE tags (id INTEGER PRIMARY KEY AUTOINCREMENT,- name TEXT NOT NULL UNIQUE+ name TEXT NOT NULL UNIQUE,+ created_at DATETIME DEFAULT CURRENT_TIMESTAMP);-- Create junction table for vision (frames) and tagsdiff --git a/screenpipe-server/src/server.rs b/screenpipe-server/src/server.rsindex dd061c4d3..e052ab331 100644--- a/screenpipe-server/src/server.rs+++ b/screenpipe-server/src/server.rs@@ -86,7 +86,7 @@ pub struct PaginationInfo {pub total: i64,}-#[derive(Serialize, Deserialize)]+#[derive(Serialize, Deserialize, Debug)]#[serde(tag = "type", content = "content")]pub enum ContentItem {OCR(OCRContent),@@ -94,7 +94,7 @@ pub enum ContentItem {FTS(FTSContent),}-#[derive(Serialize, Deserialize)]+#[derive(Serialize, Deserialize, Debug)]pub struct OCRContent {pub frame_id: i64,pub text: String,@@ -106,7 +106,7 @@ pub struct OCRContent {pub tags: Vec<String>,}-#[derive(Serialize, Deserialize)]+#[derive(Serialize, Deserialize, Debug)]pub struct AudioContent {pub chunk_id: i64,pub transcription: String,@@ -116,7 +116,7 @@ pub struct AudioContent {pub tags: Vec<String>,}-#[derive(Serialize, Deserialize)]+#[derive(Serialize, Deserialize, Debug)]pub struct FTSContent {pub text_id: i64,pub matched_text: String,diff --git a/screenpipe-server/tests/search_test.rs b/screenpipe-server/tests/search_test.rsindex 9f52625e7..9dd8ae7c8 100644--- a/screenpipe-server/tests/search_test.rs+++ b/screenpipe-server/tests/search_test.rs@@ -38,6 +38,7 @@ async fn insert_test_data(db: &Arc<DatabaseManager>) {// Insert OCR datadb.insert_video_chunk("test_video.mp4").await.unwrap();let frame_id = db.insert_frame().await.unwrap();+ let window_name = "TestWindow";db.insert_ocr_text(frame_id,"Test OCR text",@@ -49,13 +50,25 @@ async fn insert_test_data(db: &Arc<DatabaseManager>) {})).unwrap(),"TestApp",- "TestWindow",+ window_name,Arc::new(screenpipe_vision::OcrEngine::Tesseract),true,).await.unwrap();+ println!("Inserted test data with window_name: {}", window_name);++ // Verify the inserted data+ let result =+ sqlx::query_as::<_, (String,)>("SELECT window_name FROM ocr_text WHERE frame_id = ?")+ .bind(frame_id)+ .fetch_one(&db.pool)+ .await+ .unwrap();++ println!("Verified inserted data: window_name = {:?}", result);+// Insert Audio datalet audio_chunk_id = db.insert_audio_chunk("test_audio.wav").await.unwrap();db.insert_audio_transcription(audio_chunk_id, "Test audio transcription", 0, "TestEngine")@@ -194,17 +207,24 @@ async fn test_time_range_filter() {let one_hour_ago = now - Duration::hours(1);let two_hours_ago = now - Duration::hours(2);+ use url::form_urlencoded;++ let encoded_start_time =+ form_urlencoded::byte_serialize(one_hour_ago.to_rfc3339().as_bytes()).collect::<String>();+ let encoded_end_time =+ form_urlencoded::byte_serialize(now.to_rfc3339().as_bytes()).collect::<String>();++ let uri = format!(+ "/search?q=test&limit=10&offset=0&start_time={}&end_time={}",+ encoded_start_time, encoded_end_time+ );// Test search within the last hourlet response = app.clone().oneshot(Request::builder().method("GET")- .uri(&format!(- "/search?q=test&limit=10&offset=0&start_time={}&end_time={}",- one_hour_ago.to_rfc3339(),- now.to_rfc3339()- ))+ .uri(&uri).body(Body::empty()).unwrap(),)@@ -218,17 +238,22 @@ async fn test_time_range_filter() {assert_eq!(search_results.data.len(), 2);+ let encoded_start_time =+ form_urlencoded::byte_serialize(two_hours_ago.to_rfc3339().as_bytes()).collect::<String>();+ let encoded_end_time =+ form_urlencoded::byte_serialize(one_hour_ago.to_rfc3339().as_bytes()).collect::<String>();++ let uri = format!(+ "/search?q=test&limit=10&offset=0&start_time={}&end_time={}",+ encoded_start_time, encoded_end_time+ );// Test search between two hours ago and one hour ago (should return no results)let response = app.clone().oneshot(Request::builder().method("GET")- .uri(&format!(- "/search?q=test&limit=10&offset=0&start_time={}&end_time={}",- two_hours_ago.to_rfc3339(),- one_hour_ago.to_rfc3339()- ))+ .uri(&uri).body(Body::empty()).unwrap(),)@@ -274,7 +299,7 @@ async fn test_app_name_filter() {.oneshot(Request::builder().method("GET")- .uri("/search?q=test&limit=10&offset=0&app_name=WrongApp&start_time=2024-01-01T00:00:00Z&end_time=2024-01-02T00:00:00Z")+ .uri("/search?q=test&limit=10&offset=0&app_name=WrongApp&start_time=2024-01-01T00:00:00Z&end_time=2024-01-01T00:00:00Z").body(Body::empty()).unwrap(),)