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

release-app revert window fts

Louis Beaumonta13bf064ed59
diff --git a/screenpipe-app-tauri/src-tauri/Cargo.toml b/screenpipe-app-tauri/src-tauri/Cargo.toml
index c9bb2272f..204356b72 100644
--- a/screenpipe-app-tauri/src-tauri/Cargo.toml
+++ b/screenpipe-app-tauri/src-tauri/Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "screenpipe-app"
-version = "0.37.6"
+version = "0.37.7"
description = ""
authors = ["you"]
license = ""
diff --git a/screenpipe-server/src/db.rs b/screenpipe-server/src/db.rs
index 7df43c728..3ece39499 100644
--- a/screenpipe-server/src/db.rs
+++ b/screenpipe-server/src/db.rs
@@ -729,28 +729,13 @@ impl DatabaseManager {
max_length: Option<usize>,
frame_name: Option<&str>,
) -> Result<Vec<OCRResult>, sqlx::Error> {
- // combine the individual search aspects into a single fts query string
- let mut fts_parts = Vec::new();
- if !query.is_empty() {
- fts_parts.push(query.to_owned());
- }
- if let Some(app) = app_name {
- if !app.is_empty() {
- // fts field lookup for app_name using SQLite fts syntax
- fts_parts.push(format!("app_name:\"{}\"", app));
- }
- }
- if let Some(window) = window_name {
- if !window.is_empty() {
- // fts field lookup for window_name
- fts_parts.push(format!("window_name:\"{}\"", window));
- }
- }
- let combined_query = fts_parts.join(" ");
+ let base_sql = if query.is_empty() {
+ "ocr_text"
+ } else {
+ "ocr_text_fts JOIN ocr_text ON ocr_text_fts.frame_id = ocr_text.frame_id"
+ };
- // always use the fts table so that the field-specific match works
- let base_sql = "ocr_text_fts JOIN ocr_text ON ocr_text_fts.frame_id = ocr_text.frame_id";
- let where_clause = if combined_query.is_empty() {
+ let where_clause = if query.is_empty() {
"WHERE 1=1"
} else {
"WHERE ocr_text_fts MATCH ?1"
@@ -779,24 +764,24 @@ impl DatabaseManager {
{}
AND (?2 IS NULL OR frames.timestamp >= ?2)
AND (?3 IS NULL OR frames.timestamp <= ?3)
- AND (?4 IS NULL OR COALESCE(ocr_text.text_length, LENGTH(ocr_text.text)) >= ?4)
- AND (?5 IS NULL OR COALESCE(ocr_text.text_length, LENGTH(ocr_text.text)) <= ?5)
- AND (?6 IS NULL OR frames.name LIKE '%' || ?6 || '%' COLLATE NOCASE)
+ AND (?4 IS NULL OR ocr_text.app_name LIKE '%' || ?4 || '%')
+ AND (?5 IS NULL OR ocr_text.window_name LIKE '%' || ?5 || '%')
+ AND (?6 IS NULL OR COALESCE(ocr_text.text_length,LENGTH(ocr_text.text)) >= ?6)
+ AND (?7 IS NULL OR COALESCE(ocr_text.text_length,LENGTH(ocr_text.text)) <= ?7)
+ AND (?8 IS NULL OR frames.name LIKE '%' || ?8 || '%' COLLATE NOCASE)
GROUP BY ocr_text.frame_id
ORDER BY frames.timestamp DESC
- LIMIT ?7 OFFSET ?8
+ LIMIT ?9 OFFSET ?10
"#,
base_sql, where_clause
);
let raw_results: Vec<OCRResultRaw> = sqlx::query_as(&sql)
- .bind(if combined_query.is_empty() {
- "".to_owned()
- } else {
- combined_query
- })
+ .bind(query)
.bind(start_time)
.bind(end_time)
+ .bind(app_name)
+ .bind(window_name)
.bind(min_length.map(|l| l as i64))
.bind(max_length.map(|l| l as i64))
.bind(frame_name)
@@ -982,181 +967,166 @@ impl DatabaseManager {
"[]".to_string()
};
- // build combined fts queries
- let (ocr_fts_query, ui_fts_query) = {
- let mut ocr_parts = vec![];
- let mut ui_parts = vec![];
-
- if !query.is_empty() {
- ocr_parts.push(query.to_owned());
- ui_parts.push(query.to_owned());
- }
-
- if let Some(app) = app_name {
- ocr_parts.push(format!("app_name:\"{}\"", app));
- ui_parts.push(format!("app:\"{}\"", app));
- }
-
- if let Some(window) = window_name {
- ocr_parts.push(format!("window_name:\"{}\"", window));
- ui_parts.push(format!("window:\"{}\"", window));
- }
-
- (ocr_parts.join(" "), ui_parts.join(" "))
- };
-
let sql = match content_type {
- ContentType::OCR => format!(
- r#"SELECT COUNT(DISTINCT frames.id)
- FROM {table}
- JOIN frames ON ocr_text.frame_id = frames.id
- WHERE {match_condition}
- AND (?2 IS NULL OR frames.timestamp >= ?2)
- AND (?3 IS NULL OR frames.timestamp <= ?3)
- AND (?4 IS NULL OR COALESCE(ocr_text.text_length, LENGTH(ocr_text.text)) >= ?4)
- AND (?5 IS NULL OR COALESCE(ocr_text.text_length, LENGTH(ocr_text.text)) <= ?5)
- AND (?6 IS NULL OR frames.name LIKE '%' || ?6 || '%')"#,
- table = if ocr_fts_query.is_empty() {
- "ocr_text"
- } else {
- "ocr_text_fts JOIN ocr_text ON ocr_text_fts.frame_id = ocr_text.frame_id"
- },
- match_condition = if ocr_fts_query.is_empty() {
- "1=1"
- } else {
- "ocr_text_fts MATCH ?1"
- }
- ),
- ContentType::UI => format!(
- r#"SELECT COUNT(DISTINCT ui_monitoring.id)
- FROM {table}
- WHERE {match_condition}
- AND (?2 IS NULL OR ui_monitoring.timestamp >= ?2)
- AND (?3 IS NULL OR ui_monitoring.timestamp <= ?3)
- AND (?4 IS NULL OR COALESCE(ui_monitoring.text_length, LENGTH(ui_monitoring.text_output)) >= ?4)
- AND (?5 IS NULL OR COALESCE(ui_monitoring.text_length, LENGTH(ui_monitoring.text_output)) <= ?5)"#,
- table = if ui_fts_query.is_empty() {
- "ui_monitoring"
- } else {
- "ui_monitoring_fts JOIN ui_monitoring ON ui_monitoring_fts.ui_id = ui_monitoring.id"
- },
- match_condition = if ui_fts_query.is_empty() {
- "1=1"
- } else {
- "ui_monitoring_fts MATCH ?1"
- }
- ),
- ContentType::Audio => format!(
- r#"SELECT COUNT(DISTINCT audio_transcriptions.id)
- FROM {table}
- WHERE {match_condition}
- AND (?2 IS NULL OR audio_transcriptions.timestamp >= ?2)
- AND (?3 IS NULL OR audio_transcriptions.timestamp <= ?3)
- AND (?4 IS NULL OR COALESCE(audio_transcriptions.text_length, LENGTH(audio_transcriptions.transcription)) >= ?4)
- AND (?5 IS NULL OR COALESCE(audio_transcriptions.text_length, LENGTH(audio_transcriptions.transcription)) <= ?5)
- AND (json_array_length(?6) = 0 OR audio_transcriptions.speaker_id IN (SELECT value FROM json_each(?6)))
- "#,
- table = if query.is_empty() {
- "audio_transcriptions"
- } else {
- "audio_transcriptions_fts JOIN audio_transcriptions ON audio_transcriptions_fts.audio_chunk_id = audio_transcriptions.audio_chunk_id"
- },
- match_condition = if query.is_empty() {
- "1=1"
- } else {
- "audio_transcriptions_fts MATCH ?1"
- }
- ),
- ContentType::All => format!(
- r#"SELECT COUNT(*) FROM (
- SELECT DISTINCT frames.id FROM {ocr_table}
+ ContentType::OCR => {
+ format!(
+ r#"
+ SELECT COUNT(DISTINCT frames.id)
+ FROM {table}
JOIN frames ON ocr_text.frame_id = frames.id
- WHERE {ocr_match}
+ WHERE {match_condition}
AND (?2 IS NULL OR frames.timestamp >= ?2)
AND (?3 IS NULL OR frames.timestamp <= ?3)
- AND (?4 IS NULL OR COALESCE(ocr_text.text_length, LENGTH(ocr_text.text)) >= ?4)
- AND (?5 IS NULL OR COALESCE(ocr_text.text_length, LENGTH(ocr_text.text)) <= ?5)
- AND (?6 IS NULL OR frames.name LIKE '%' || ?6 || '%')
- UNION ALL
- SELECT DISTINCT id FROM {audio_table}
- WHERE {audio_match}
- AND (?2 IS NULL OR timestamp >= ?2)
- AND (?3 IS NULL OR timestamp <= ?3)
+ AND (?4 IS NULL OR ocr_text.app_name LIKE '%' || ?4 || '%')
+ AND (?5 IS NULL OR ocr_text.window_name LIKE '%' || ?5 || '%')
+ AND (?6 IS NULL OR COALESCE(ocr_text.text_length, LENGTH(ocr_text.text)) >= ?6)
+ AND (?7 IS NULL OR COALESCE(ocr_text.text_length, LENGTH(ocr_text.text)) <= ?7)
+ AND (?8 IS NULL OR frames.name LIKE '%' || ?8 || '%' COLLATE NOCASE)
+ "#,
+ table = if query.is_empty() {
+ "ocr_text"
+ } else {
+ "ocr_text_fts JOIN ocr_text ON ocr_text_fts.frame_id = ocr_text.frame_id"
+ },
+ match_condition = if query.is_empty() {
+ "1=1"
+ } else {
+ "ocr_text_fts MATCH ?1"
+ }
+ )
+ }
+ ContentType::Audio => {
+ format!(
+ r#"
+ SELECT COUNT(DISTINCT audio_transcriptions.audio_chunk_id || '_' || COALESCE(audio_transcriptions.start_time, '') || '_' || COALESCE(audio_transcriptions.end_time, ''))
+ FROM {table}
+ WHERE {match_condition}
+ AND (?2 IS NULL OR audio_transcriptions.timestamp >= ?2)
+ AND (?3 IS NULL OR audio_transcriptions.timestamp <= ?3)
AND (?4 IS NULL OR COALESCE(audio_transcriptions.text_length, LENGTH(audio_transcriptions.transcription)) >= ?4)
AND (?5 IS NULL OR COALESCE(audio_transcriptions.text_length, LENGTH(audio_transcriptions.transcription)) <= ?5)
- AND (json_array_length(?6) = 0 OR speaker_id IN (SELECT value FROM json_each(?6)))
- UNION ALL
- SELECT DISTINCT id FROM {ui_table}
- WHERE {ui_match}
- AND (?2 IS NULL OR timestamp >= ?2)
- AND (?3 IS NULL OR timestamp <= ?3)
- AND (?4 IS NULL OR COALESCE(ui_monitoring.text_length, LENGTH(ui_monitoring.text_output)) >= ?4)
- AND (?5 IS NULL OR COALESCE(ui_monitoring.text_length, LENGTH(ui_monitoring.text_output)) <= ?5)
- )"#,
- ocr_table = if ocr_fts_query.is_empty() {
- "ocr_text"
- } else {
- "ocr_text_fts JOIN ocr_text ON ocr_text_fts.frame_id = ocr_text.frame_id"
- },
- ocr_match = if ocr_fts_query.is_empty() {
- "1=1"
- } else {
- "ocr_text_fts MATCH ?1"
- },
- audio_table = if query.is_empty() {
- "audio_transcriptions"
- } else {
- "audio_transcriptions_fts JOIN audio_transcriptions ON audio_transcriptions_fts.audio_chunk_id = audio_transcriptions.audio_chunk_id"
- },
- audio_match = if query.is_empty() {
- "1=1"
- } else {
- "audio_transcriptions_fts MATCH ?1"
- },
- ui_table = if ui_fts_query.is_empty() {
- "ui_monitoring"
- } else {
- "ui_monitoring_fts JOIN ui_monitoring ON ui_monitoring_fts.ui_id = ui_monitoring.id"
- },
- ui_match = if ui_fts_query.is_empty() {
- "1=1"
- } else {
- "ui_monitoring_fts MATCH ?1"
- }
- ),
- _ => return Ok(0),
- };
-
- let count: i64 = match content_type {
- ContentType::OCR => {
- sqlx::query_scalar(&sql)
- .bind(ocr_fts_query)
- .bind(start_time)
- .bind(end_time)
- .bind(min_length.map(|l| l as i64))
- .bind(max_length.map(|l| l as i64))
- .bind(frame_name)
- .fetch_one(&self.pool)
- .await?
+ AND (json_array_length(?6) = 0 OR audio_transcriptions.speaker_id IN (SELECT value FROM json_each(?6)))
+ "#,
+ table = if query.is_empty() {
+ "audio_transcriptions"
+ } else {
+ "audio_transcriptions_fts JOIN audio_transcriptions ON audio_transcriptions_fts.audio_chunk_id = audio_transcriptions.audio_chunk_id"
+ },
+ match_condition = if query.is_empty() {
+ "1=1"
+ } else {
+ "audio_transcriptions_fts MATCH ?1"
+ }
+ )
}
ContentType::UI => {
- sqlx::query_scalar(&sql)
- .bind(ui_fts_query)
- .bind(start_time)
- .bind(end_time)
- .bind(min_length.map(|l| l as i64))
- .bind(max_length.map(|l| l as i64))
- .fetch_one(&self.pool)
- .await?
+ format!(
+ r#"
+ SELECT COUNT(DISTINCT ui_monitoring.id)
+ FROM {table}
+ WHERE {match_condition}
+ AND (?2 IS NULL OR ui_monitoring.timestamp >= ?2)
+ AND (?3 IS NULL OR ui_monitoring.timestamp <= ?3)
+ AND (?4 IS NULL OR ui_monitoring.app LIKE '%' || ?4 || '%')
+ AND (?5 IS NULL OR ui_monitoring.window LIKE '%' || ?5 || '%')
+ AND (?6 IS NULL OR COALESCE(ui_monitoring.text_length, LENGTH(ui_monitoring.text_output)) >= ?6)
+ AND (?7 IS NULL OR COALESCE(ui_monitoring.text_length, LENGTH(ui_monitoring.text_output)) <= ?7)
+ "#,
+ table = if query.is_empty() {
+ "ui_monitoring"
+ } else {
+ "ui_monitoring_fts JOIN ui_monitoring ON ui_monitoring_fts.ui_id = ui_monitoring.id"
+ },
+ match_condition = if query.is_empty() {
+ "1=1"
+ } else {
+ "ui_monitoring_fts MATCH ?1"
+ }
+ )
}
ContentType::All => {
+ format!(
+ r#"
+ SELECT COUNT(*) FROM (
+ -- OCR part
+ SELECT DISTINCT frames.id
+ FROM {ocr_table}
+ JOIN frames ON ocr_text.frame_id = frames.id
+ WHERE {ocr_match}
+ AND (?2 IS NULL OR frames.timestamp >= ?2)
+ AND (?3 IS NULL OR frames.timestamp <= ?3)
+ AND (?4 IS NULL OR ocr_text.app_name LIKE '%' || ?4 || '%')
+ AND (?5 IS NULL OR ocr_text.window_name LIKE '%' || ?5 || '%')
+ AND (?6 IS NULL OR COALESCE(ocr_text.text_length, LENGTH(ocr_text.text)) >= ?6)
+ AND (?7 IS NULL OR COALESCE(ocr_text.text_length, LENGTH(ocr_text.text)) <= ?7)
+ AND (?8 IS NULL OR frames.name LIKE '%' || ?8 || '%' COLLATE NOCASE)
+ UNION ALL
+ -- Audio part
+ SELECT DISTINCT audio_transcriptions.id
+ FROM {audio_table}
+ WHERE {audio_match}
+ AND (?2 IS NULL OR audio_transcriptions.timestamp >= ?2)
+ AND (?3 IS NULL OR audio_transcriptions.timestamp <= ?3)
+ AND (?6 IS NULL OR COALESCE(audio_transcriptions.text_length, LENGTH(audio_transcriptions.transcription)) >= ?6)
+ AND (?7 IS NULL OR COALESCE(audio_transcriptions.text_length, LENGTH(audio_transcriptions.transcription)) <= ?7)
+ AND audio_transcriptions.transcription != ''
+ AND (json_array_length(?9) = 0 OR audio_transcriptions.speaker_id IN (SELECT value FROM json_each(?9)))
+ UNION ALL
+ -- UI part
+ SELECT DISTINCT ui_monitoring.id
+ FROM {ui_table}
+ WHERE {ui_match}
+ AND (?2 IS NULL OR ui_monitoring.timestamp >= ?2)
+ AND (?3 IS NULL OR ui_monitoring.timestamp <= ?3)
+ AND (?4 IS NULL OR ui_monitoring.app LIKE '%' || ?4 || '%')
+ AND (?5 IS NULL OR ui_monitoring.window LIKE '%' || ?5 || '%')
+ AND (?6 IS NULL OR COALESCE(ui_monitoring.text_length, LENGTH(ui_monitoring.text_output)) >= ?6)
+ AND (?7 IS NULL OR COALESCE(ui_monitoring.text_length, LENGTH(ui_monitoring.text_output)) <= ?7)
+ AND ui_monitoring.text_output != ''
+ )"#,
+ ocr_table = if query.is_empty() {
+ "ocr_text"
+ } else {
+ "ocr_text_fts JOIN ocr_text ON ocr_text_fts.frame_id = ocr_text.frame_id"
+ },
+ ocr_match = if query.is_empty() {
+ "1=1"
+ } else {
+ "ocr_text_fts MATCH ?1"
+ },
+ audio_table = if query.is_empty() {
+ "audio_transcriptions"
+ } else {
+ "audio_transcriptions_fts JOIN audio_transcriptions ON audio_transcriptions_fts.audio_chunk_id = audio_transcriptions.audio_chunk_id"
+ },
+ audio_match = if query.is_empty() {
+ "1=1"
+ } else {
+ "audio_transcriptions_fts MATCH ?1"
+ },
+ ui_table = if query.is_empty() {
+ "ui_monitoring"
+ } else {
+ "ui_monitoring_fts JOIN ui_monitoring ON ui_monitoring_fts.ui_id = ui_monitoring.id"
+ },
+ ui_match = if query.is_empty() {
+ "1=1"
+ } else {
+ "ui_monitoring_fts MATCH ?1"
+ }
+ )
+ }
+ _ => return Ok(0),
+ };
+
+ let count: i64 = match content_type {
+ ContentType::Audio => {
sqlx::query_scalar(&sql)
- .bind(ocr_fts_query)
+ .bind(query)
.bind(start_time)
.bind(end_time)
.bind(min_length.map(|l| l as i64))
.bind(max_length.map(|l| l as i64))
- .bind(frame_name)
.bind(json_array)
.fetch_one(&self.pool)
.await?
@@ -1166,6 +1136,9 @@ impl DatabaseManager {
.bind(query)
.bind(start_time)
.bind(end_time)
+ .bind(app_name)
+ .bind(window_name)
+ .bind(frame_name)
.bind(min_length.map(|l| l as i64))
.bind(max_length.map(|l| l as i64))
.bind(json_array)
@@ -1558,26 +1531,13 @@ impl DatabaseManager {
limit: u32,
offset: u32,
) -> Result<Vec<UiContent>, sqlx::Error> {
- // combine search aspects into single fts query
- let mut fts_parts = Vec::new();
- if !query.is_empty() {
- fts_parts.push(query.to_owned());
- }
- if let Some(app) = app_name {
- fts_parts.push(format!("app:\"{}\"", app));
- }
- if let Some(window) = window_name {
- fts_parts.push(format!("window:\"{}\"", window));
- }
- let combined_query = fts_parts.join(" ");
-
- let base_sql = if combined_query.is_empty() {
+ let base_sql = if query.is_empty() {
"ui_monitoring"
} else {
"ui_monitoring_fts JOIN ui_monitoring ON ui_monitoring_fts.ui_id = ui_monitoring.id"
};
- let where_clause = if combined_query.is_empty() {
+ let where_clause = if query.is_empty() {
"WHERE 1=1"
} else {
"WHERE ui_monitoring_fts MATCH ?1"
@@ -1605,20 +1565,20 @@ impl DatabaseManager {
{}
AND (?2 IS NULL OR ui_monitoring.timestamp >= ?2)
AND (?3 IS NULL OR ui_monitoring.timestamp <= ?3)
+ AND (?4 IS NULL OR ui_monitoring.app LIKE '%' || ?4 || '%')
+ AND (?5 IS NULL OR ui_monitoring.window LIKE '%' || ?5 || '%')
ORDER BY ui_monitoring.timestamp DESC
- LIMIT ?4 OFFSET ?5
+ LIMIT ?6 OFFSET ?7
"#,
base_sql, where_clause
);
sqlx::query_as(&sql)
- .bind(if combined_query.is_empty() {
- "".to_owned()
- } else {
- combined_query
- })
+ .bind(query)
.bind(start_time)
.bind(end_time)
+ .bind(app_name)
+ .bind(window_name)
.bind(limit)
.bind(offset)
.fetch_all(&self.pool)