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: db and server core improvements
Louis Beaumont19bb6195f1b1
diff --git a/Cargo.lock b/Cargo.lockindex 3869b5204..94598d2ef 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -6628,7 +6628,7 @@ dependencies = [[[package]]name = "screenpipe-audio"-version = "0.3.51"+version = "0.3.52"dependencies = ["anyhow","bytemuck",@@ -6681,7 +6681,7 @@ dependencies = [[[package]]name = "screenpipe-core"-version = "0.3.51"+version = "0.3.52"dependencies = ["accessibility","accessibility-sys",@@ -6732,7 +6732,7 @@ dependencies = [[[package]]name = "screenpipe-db"-version = "0.3.51"+version = "0.3.52"dependencies = ["anyhow","chrono",@@ -6756,7 +6756,7 @@ dependencies = [[[package]]name = "screenpipe-events"-version = "0.3.51"+version = "0.3.52"dependencies = ["anyhow","chrono",@@ -6774,7 +6774,7 @@ dependencies = [[[package]]name = "screenpipe-integrations"-version = "0.3.51"+version = "0.3.52"dependencies = ["anyhow","image",@@ -6789,7 +6789,7 @@ dependencies = [[[package]]name = "screenpipe-server"-version = "0.3.51"+version = "0.3.52"dependencies = ["anyhow","async-trait",@@ -6858,7 +6858,7 @@ dependencies = [[[package]]name = "screenpipe-vision"-version = "0.3.51"+version = "0.3.52"dependencies = ["accessibility-sys","anyhow",diff --git a/Cargo.toml b/Cargo.tomlindex 82752ce5f..e521529c8 100644--- a/Cargo.toml+++ b/Cargo.toml@@ -6,7 +6,7 @@ exclude = ["apps/screenpipe-app-tauri/src-tauri"]resolver = "2"[workspace.package]-version = "0.3.52"+version = "0.3.53"authors = ["louis030195 <[email protected]>"]description = ""repository = "https://github.com/screenpipe/screenpipe"diff --git a/apps/screenpipe-app-tauri/src-tauri/Cargo.toml b/apps/screenpipe-app-tauri/src-tauri/Cargo.tomlindex 69ca2c999..f611e5b58 100644--- a/apps/screenpipe-app-tauri/src-tauri/Cargo.toml+++ b/apps/screenpipe-app-tauri/src-tauri/Cargo.toml@@ -1,6 +1,6 @@[package]name = "screenpipe-app"-version = "2.0.258"+version = "2.0.259"description = "24/7 memory for your desktop. 100% local."authors = ["you"]license = ""diff --git a/crates/screenpipe-db/src/db.rs b/crates/screenpipe-db/src/db.rsindex 76c83b192..dc20aa8a6 100644--- a/crates/screenpipe-db/src/db.rs+++ b/crates/screenpipe-db/src/db.rs@@ -23,10 +23,10 @@ use futures::future::try_join_all;use crate::{text_similarity::is_similar_transcription, AudioChunksResponse, AudioDevice, AudioEntry,- AudioResult, AudioResultRaw, ContentType, DeviceType, FrameData, FrameRow, InsertUiEvent,- OCREntry, OCRResult, OCRResultRaw, OcrEngine, OcrTextBlock, Order, SearchMatch, SearchResult,- Speaker, TagContentType, TextBounds, TextPosition, TimeSeriesChunk, UiContent, UiEventRecord,- UiEventRow, VideoMetadata,+ AudioResult, AudioResultRaw, ContentType, DeviceType, FrameData, FrameRow, FrameWindowData,+ InsertUiEvent, OCREntry, OCRResult, OCRResultRaw, OcrEngine, OcrTextBlock, Order, SearchMatch,+ SearchResult, Speaker, TagContentType, TextBounds, TextPosition, TimeSeriesChunk, UiContent,+ UiEventRecord, UiEventRow, VideoMetadata,};/// Time window (in seconds) to check for similar transcriptions across devices.@@ -104,7 +104,7 @@ impl DatabaseManager {/// when a deferred reader tries to upgrade to writer after another commit.////// Returns the connection ready for writes. Caller must COMMIT or ROLLBACK.- async fn begin_immediate_with_retry(+ pub async fn begin_immediate_with_retry(&self,) -> Result<PoolConnection<Sqlite>, sqlx::Error> {let max_retries = 5;@@ -138,14 +138,14 @@ impl DatabaseManager {}pub async fn insert_audio_chunk(&self, file_path: &str) -> Result<i64, sqlx::Error> {- let mut tx = self.pool.begin().await?;+ let mut conn = self.begin_immediate_with_retry().await?;let id = sqlx::query("INSERT INTO audio_chunks (file_path, timestamp) VALUES (?1, ?2)").bind(file_path).bind(Utc::now())- .execute(&mut *tx)+ .execute(&mut *conn).await?.last_insert_rowid();- tx.commit().await?;+ sqlx::query("COMMIT").execute(&mut *conn).await?;Ok(id)}@@ -212,7 +212,7 @@ impl DatabaseManager {}let text_length = transcription.len() as i64;- let mut tx = self.pool.begin().await?;+ let mut conn = self.begin_immediate_with_retry().await?;// Insert the transcription, ignoring duplicates (same audio_chunk_id + transcription)// This prevents duplicates from VAD segment overlap issues within the same device@@ -230,11 +230,11 @@ impl DatabaseManager {.bind(start_time).bind(end_time).bind(text_length)- .execute(&mut *tx)+ .execute(&mut *conn).await?;// Commit the transaction- tx.commit().await?;+ sqlx::query("COMMIT").execute(&mut *conn).await?;// Returns 0 if the insert was ignored (duplicate), otherwise returns the new id// Note: last_insert_rowid() returns the previous successful insert's id when ignored,@@ -280,7 +280,7 @@ impl DatabaseManager {transcription: &str,) -> Result<i64, sqlx::Error> {let text_length = transcription.len() as i64;- let mut tx = self.pool.begin().await?;+ let mut conn = self.begin_immediate_with_retry().await?;// Insert the full transcriptionlet affected = sqlx::query(@@ -289,20 +289,20 @@ impl DatabaseManager {.bind(transcription).bind(text_length).bind(audio_chunk_id)- .execute(&mut *tx)+ .execute(&mut *conn).await?.rows_affected();// Commit the transaction for the full transcription- tx.commit().await?;+ sqlx::query("COMMIT").execute(&mut *conn).await?;Ok(affected as i64)}pub async fn insert_speaker(&self, embedding: &[f32]) -> Result<Speaker, SqlxError> {- let mut tx = self.pool.begin().await?;+ let mut conn = self.begin_immediate_with_retry().await?;let id = sqlx::query("INSERT INTO speakers (name) VALUES (NULL)")- .execute(&mut *tx)+ .execute(&mut *conn).await?.last_insert_rowid();@@ -312,9 +312,9 @@ impl DatabaseManager {).bind(bytes).bind(id)- .execute(&mut *tx)+ .execute(&mut *conn).await?;- tx.commit().await?;+ sqlx::query("COMMIT").execute(&mut *conn).await?;Ok(Speaker {id,@@ -328,13 +328,13 @@ impl DatabaseManager {speaker_id: i64,metadata: &str,) -> Result<i64, SqlxError> {- let mut tx = self.pool.begin().await?;+ let mut conn = self.begin_immediate_with_retry().await?;sqlx::query("UPDATE speakers SET metadata = ?1 WHERE id = ?2").bind(metadata).bind(speaker_id)- .execute(&mut *tx)+ .execute(&mut *conn).await?;- tx.commit().await?;+ sqlx::query("COMMIT").execute(&mut *conn).await?;Ok(speaker_id)}@@ -374,13 +374,13 @@ impl DatabaseManager {}pub async fn update_speaker_name(&self, speaker_id: i64, name: &str) -> Result<i64, SqlxError> {- let mut tx = self.pool.begin().await?;+ let mut conn = self.begin_immediate_with_retry().await?;sqlx::query("UPDATE speakers SET name = ?1 WHERE id = ?2").bind(name).bind(speaker_id)- .execute(&mut *tx)+ .execute(&mut *conn).await?;- tx.commit().await?;+ sqlx::query("COMMIT").execute(&mut *conn).await?;Ok(speaker_id)}@@ -389,14 +389,14 @@ impl DatabaseManager {file_path: &str,device_name: &str,) -> Result<i64, sqlx::Error> {- let mut tx = self.pool.begin().await?;+ let mut conn = self.begin_immediate_with_retry().await?;let id = sqlx::query("INSERT INTO video_chunks (file_path, device_name) VALUES (?1, ?2)").bind(file_path).bind(device_name)- .execute(&mut *tx)+ .execute(&mut *conn).await?.last_insert_rowid();- tx.commit().await?;+ sqlx::query("COMMIT").execute(&mut *conn).await?;Ok(id)}@@ -416,7 +416,7 @@ impl DatabaseManager {focused: bool,offset_index: Option<i64>,) -> Result<i64, sqlx::Error> {- let mut tx = self.pool.begin().await?;+ let mut conn = self.begin_immediate_with_retry().await?;debug!("insert_frame Transaction started");// Get the most recent video_chunk_id and file_path@@ -424,7 +424,7 @@ impl DatabaseManager {"SELECT id, file_path FROM video_chunks WHERE device_name = ?1 ORDER BY id DESC LIMIT 1",).bind(device_name)- .fetch_optional(&mut *tx)+ .fetch_optional(&mut *conn).await?;debug!("Fetched most recent video_chunk: {:?}", video_chunk);@@ -433,7 +433,7 @@ impl DatabaseManager {Some((id, path)) => (id, path),None => {debug!("No video chunk found, rolling back transaction");- tx.rollback().await?;+ let _ = sqlx::query("ROLLBACK").execute(&mut *conn).await;return Ok(0);}};@@ -448,7 +448,7 @@ impl DatabaseManager {"SELECT COALESCE(MAX(offset_index), -1) + 1 FROM frames WHERE video_chunk_id = ?1",).bind(video_chunk_id)- .fetch_one(&mut *tx)+ .fetch_one(&mut *conn).await?}};@@ -469,13 +469,13 @@ impl DatabaseManager {.bind(window_name).bind(focused).bind(device_name)- .execute(&mut *tx)+ .execute(&mut *conn).await?.last_insert_rowid();debug!("insert_frame Inserted new frame with id: {}", id);// Commit the transaction- tx.commit().await?;+ sqlx::query("COMMIT").execute(&mut *conn).await?;Ok(id)}@@ -515,21 +515,100 @@ impl DatabaseManager {ocr_engine: Arc<OcrEngine>,) -> Result<(), sqlx::Error> {let text_length = text.len() as i64;- let mut tx = self.pool.begin().await?;+ let mut conn = self.begin_immediate_with_retry().await?;sqlx::query("INSERT INTO ocr_text (frame_id, text, text_json, ocr_engine, text_length) VALUES (?1, ?2, ?3, ?4, ?5)").bind(frame_id).bind(text).bind(text_json).bind(format!("{:?}", *ocr_engine)).bind(text_length)- .execute(&mut *tx)+ .execute(&mut *conn).await?;- tx.commit().await?;+ sqlx::query("COMMIT").execute(&mut *conn).await?;debug!("OCR text inserted into db successfully");Ok(())}+ /// Batch insert frames and their OCR text in a single transaction.+ /// This dramatically reduces write lock contention in the hot path by acquiring+ /// the lock once per capture cycle instead of 2× per window result.+ ///+ /// Returns a Vec of (frame_id, window_index) pairs for each successfully inserted window.+ pub async fn insert_frames_with_ocr_batch(+ &self,+ device_name: &str,+ timestamp: Option<DateTime<Utc>>,+ offset_index: i64,+ windows: &[FrameWindowData],+ ocr_engine: Arc<OcrEngine>,+ ) -> Result<Vec<(i64, usize)>, sqlx::Error> {+ let mut conn = self.begin_immediate_with_retry().await?;++ // Get the most recent video_chunk_id and file_path+ let video_chunk: Option<(i64, String)> = sqlx::query_as(+ "SELECT id, file_path FROM video_chunks WHERE device_name = ?1 ORDER BY id DESC LIMIT 1",+ )+ .bind(device_name)+ .fetch_optional(&mut *conn)+ .await?;++ let (video_chunk_id, file_path) = match video_chunk {+ Some((id, path)) => (id, path),+ None => {+ let _ = sqlx::query("ROLLBACK").execute(&mut *conn).await;+ return Ok(vec![]);+ }+ };++ let timestamp = timestamp.unwrap_or_else(Utc::now);+ let ocr_engine_str = format!("{:?}", *ocr_engine);+ let mut results = Vec::with_capacity(windows.len());++ for (idx, window) in windows.iter().enumerate() {+ // Insert frame+ let frame_id = sqlx::query(+ "INSERT INTO frames (video_chunk_id, offset_index, timestamp, name, browser_url, app_name, window_name, focused, device_name) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9)",+ )+ .bind(video_chunk_id)+ .bind(offset_index)+ .bind(timestamp)+ .bind(&file_path)+ .bind(window.browser_url.as_deref())+ .bind(window.app_name.as_deref())+ .bind(window.window_name.as_deref())+ .bind(window.focused)+ .bind(device_name)+ .execute(&mut *conn)+ .await?+ .last_insert_rowid();++ // Insert OCR text+ let text_length = window.text.len() as i64;+ sqlx::query(+ "INSERT INTO ocr_text (frame_id, text, text_json, ocr_engine, text_length) VALUES (?1, ?2, ?3, ?4, ?5)",+ )+ .bind(frame_id)+ .bind(&window.text)+ .bind(&window.text_json)+ .bind(&ocr_engine_str)+ .bind(text_length)+ .execute(&mut *conn)+ .await?;++ results.push((frame_id, idx));+ }++ sqlx::query("COMMIT").execute(&mut *conn).await?;+ debug!(+ "Batch inserted {} frames with OCR for device {}",+ results.len(),+ device_name+ );++ Ok(results)+ }+#[allow(clippy::too_many_arguments)]pub async fn search(&self,@@ -1716,7 +1795,7 @@ impl DatabaseManager {}async fn add_tags_to_vision(&self, frame_id: i64, tags: Vec<String>) -> Result<(), SqlxError> {- let mut tx = self.pool.begin().await?;+ let mut conn = self.begin_immediate_with_retry().await?;for tag in tags {// Insert tag if it doesn't exist@@ -1724,7 +1803,7 @@ impl DatabaseManager {"INSERT INTO tags (name) VALUES (?) ON CONFLICT(name) DO UPDATE SET name=name RETURNING id",).bind(&tag)- .fetch_one(&mut *tx)+ .fetch_one(&mut *conn).await?;// Insert into vision_tags@@ -1733,11 +1812,11 @@ impl DatabaseManager {).bind(frame_id).bind(tag_id)- .execute(&mut *tx)+ .execute(&mut *conn).await?;}- tx.commit().await?;+ sqlx::query("COMMIT").execute(&mut *conn).await?;Ok(())}@@ -1746,7 +1825,7 @@ impl DatabaseManager {audio_chunk_id: i64,tags: Vec<String>,) -> Result<(), SqlxError> {- let mut tx = self.pool.begin().await?;+ let mut conn = self.begin_immediate_with_retry().await?;for tag in tags {// Insert tag if it doesn't exist@@ -1754,7 +1833,7 @@ impl DatabaseManager {"INSERT INTO tags (name) VALUES (?) ON CONFLICT(name) DO UPDATE SET name=name RETURNING id",).bind(&tag)- .fetch_one(&mut *tx)+ .fetch_one(&mut *conn).await?;// Insert into audio_tags@@ -1763,11 +1842,11 @@ impl DatabaseManager {).bind(audio_chunk_id).bind(tag_id)- .execute(&mut *tx)+ .execute(&mut *conn).await?;}- tx.commit().await?;+ sqlx::query("COMMIT").execute(&mut *conn).await?;Ok(())}@@ -1825,7 +1904,7 @@ impl DatabaseManager {}async fn remove_vision_tags(&self, vision_id: i64, tags: Vec<String>) -> Result<(), SqlxError> {- let mut tx = self.pool.begin().await?;+ let mut conn = self.begin_immediate_with_retry().await?;for tag in tags {sqlx::query(@@ -1836,11 +1915,11 @@ impl DatabaseManager {).bind(vision_id).bind(&tag)- .execute(&mut *tx)+ .execute(&mut *conn).await?;}- tx.commit().await?;+ sqlx::query("COMMIT").execute(&mut *conn).await?;Ok(())}@@ -1849,7 +1928,7 @@ impl DatabaseManager {audio_chunk_id: i64,tags: Vec<String>,) -> Result<(), SqlxError> {- let mut tx = self.pool.begin().await?;+ let mut conn = self.begin_immediate_with_retry().await?;for tag in tags {sqlx::query(@@ -1860,11 +1939,11 @@ impl DatabaseManager {).bind(audio_chunk_id).bind(&tag)- .execute(&mut *tx)+ .execute(&mut *conn).await?;}- tx.commit().await?;+ sqlx::query("COMMIT").execute(&mut *conn).await?;Ok(())}pub async fn execute_raw_sql(&self, query: &str) -> Result<serde_json::Value, sqlx::Error> {@@ -2359,29 +2438,29 @@ impl DatabaseManager {speaker_to_keep_id: i64,speaker_to_merge_id: i64,) -> Result<Speaker, sqlx::Error> {- let mut tx = self.pool.begin().await?;+ let mut conn = self.begin_immediate_with_retry().await?;// for each audio transcription of the speaker to merge, update the speaker_id to the speaker to keepsqlx::query("UPDATE audio_transcriptions SET speaker_id = ? WHERE speaker_id = ?").bind(speaker_to_keep_id).bind(speaker_to_merge_id)- .execute(&mut *tx)+ .execute(&mut *conn).await?;// update speaker_embeddingssqlx::query("UPDATE speaker_embeddings SET speaker_id = ? WHERE speaker_id = ?").bind(speaker_to_keep_id).bind(speaker_to_merge_id)- .execute(&mut *tx)+ .execute(&mut *conn).await?;// delete the speaker to mergesqlx::query("DELETE FROM speakers WHERE id = ?").bind(speaker_to_merge_id)- .execute(&mut *tx)+ .execute(&mut *conn).await?;- tx.commit().await?;+ sqlx::query("COMMIT").execute(&mut *conn).await?;self.get_speaker_by_id(speaker_to_keep_id).await}@@ -2396,7 +2475,7 @@ impl DatabaseManager {}pub async fn delete_speaker(&self, id: i64) -> Result<(), sqlx::Error> {- let mut tx = self.pool.begin().await?;+ let mut conn = self.begin_immediate_with_retry().await?;// Array of (query, operation description) tupleslet operations = [@@ -2420,15 +2499,15 @@ impl DatabaseManager {// Execute each deletion operationfor (query, operation) in operations {- if let Err(e) = sqlx::query(query).bind(id).execute(&mut *tx).await {+ if let Err(e) = sqlx::query(query).bind(id).execute(&mut *conn).await {error!("Failed to delete {} for speaker {}: {}", operation, id, e);- tx.rollback().await?;+ let _ = sqlx::query("ROLLBACK").execute(&mut *conn).await;return Err(e);}debug!("Successfully deleted {} for speaker {}", operation, id);}- tx.commit().await.map_err(|e| {+ sqlx::query("COMMIT").execute(&mut *conn).await.map_err(|e| {error!("Failed to commit speaker deletion transaction: {}", e);e})?;@@ -2520,7 +2599,7 @@ impl DatabaseManager {frames: Vec<DynamicImage>,metadata: VideoMetadata,) -> Result<Vec<i64>, sqlx::Error> {- let mut tx = self.pool.begin().await?;+ let mut conn = self.begin_immediate_with_retry().await?;debug!("creating video chunk {}, metadata: {:?}",&file_path, &metadata@@ -2535,7 +2614,7 @@ impl DatabaseManager {sqlx::query("INSERT INTO video_chunks (device_name, file_path) VALUES (?1, ?2)").bind(device_name).bind(file_path)- .execute(&mut *tx)+ .execute(&mut *conn).await?.last_insert_rowid();@@ -2555,14 +2634,14 @@ impl DatabaseManager {.bind(i as i64).bind(frame_timestamp).bind(metadata.name.as_deref().unwrap_or(file_path)) // Use reference instead of clone- .execute(&mut *tx)+ .execute(&mut *conn).await?.last_insert_rowid();frame_ids.push(frame_id);}- tx.commit().await?;+ sqlx::query("COMMIT").execute(&mut *conn).await?;debug!("created {} frames for video chunk {}",frames.len(),@@ -3326,7 +3405,7 @@ LIMIT ? OFFSET ?return Ok(0);}- let mut tx = self.pool.begin().await?;+ let mut conn = self.begin_immediate_with_retry().await?;let mut count = 0;for event in events {@@ -3378,13 +3457,13 @@ LIMIT ? OFFSET ?.bind(&event.element_automation_id).bind(&event.element_bounds).bind(event.frame_id)- .execute(&mut *tx)+ .execute(&mut *conn).await?;count += 1;}- tx.commit().await?;+ sqlx::query("COMMIT").execute(&mut *conn).await?;debug!("Inserted {} UI events in batch", count);Ok(count)}diff --git a/crates/screenpipe-db/src/migration_worker.rs b/crates/screenpipe-db/src/migration_worker.rsindex 35b8daf82..703e32d6a 100644--- a/crates/screenpipe-db/src/migration_worker.rs+++ b/crates/screenpipe-db/src/migration_worker.rs@@ -505,7 +505,29 @@ async fn migrate_ocr_data_to_frames(/// Process a batch of recordsasync fn process_batch(pool: &SqlitePool, last_id: i64, batch_size: i64) -> Result<(i64, i64)> {- let mut tx = pool.begin().await?;+ // Use BEGIN IMMEDIATE to avoid WAL deadlocks with DEFERRED transactions+ let max_retries = 5;+ let mut conn = pool.acquire().await?;+ for attempt in 1..=max_retries {+ match sqlx::query("BEGIN IMMEDIATE").execute(&mut *conn).await {+ Ok(_) => break,+ Err(e) if attempt < max_retries => {+ let msg = e.to_string().to_lowercase();+ if msg.contains("database is locked") || msg.contains("busy") {+ tracing::warn!(+ "Migration batch BEGIN IMMEDIATE busy (attempt {}/{}), retrying...",+ attempt, max_retries+ );+ drop(conn);+ tokio::time::sleep(std::time::Duration::from_millis(100 * attempt as u64)).await;+ conn = pool.acquire().await?;+ } else {+ return Err(e.into());+ }+ }+ Err(e) => return Err(e.into()),+ }+ }// Query to get a batch of records with unique frame_ids that need migrationlet records = sqlx::query(@@ -529,7 +551,7 @@ async fn process_batch(pool: &SqlitePool, last_id: i64, batch_size: i64) -> Resu).bind(last_id).bind(batch_size)- .fetch_all(&mut *tx)+ .fetch_all(&mut *conn).await?;let mut count = 0;@@ -556,7 +578,7 @@ async fn process_batch(pool: &SqlitePool, last_id: i64, batch_size: i64) -> Resu.bind(window_name).bind(focused).bind(frame_id)- .execute(&mut *tx)+ .execute(&mut *conn).await?;count += 1;@@ -564,7 +586,7 @@ async fn process_batch(pool: &SqlitePool, last_id: i64, batch_size: i64) -> Resu}// Commit the transaction- tx.commit().await?;+ sqlx::query("COMMIT").execute(&mut *conn).await?;debug!("Processed batch: {} records, max_id={}", count, max_id);diff --git a/crates/screenpipe-db/src/types.rs b/crates/screenpipe-db/src/types.rsindex e48d4e527..b732dce65 100644--- a/crates/screenpipe-db/src/types.rs+++ b/crates/screenpipe-db/src/types.rs@@ -5,6 +5,18 @@ use sqlx::FromRow;use std::error::Error as StdError;use std::fmt::{self, Display};+/// Data for a single window result to be batch-inserted with its frame.+/// Used by `insert_frames_with_ocr_batch` to reduce write lock contention.+#[derive(Debug, Clone)]+pub struct FrameWindowData {+ pub app_name: Option<String>,+ pub window_name: Option<String>,+ pub browser_url: Option<String>,+ pub focused: bool,+ pub text: String,+ pub text_json: String,+}+#[derive(OaSchema, Debug)]pub struct DatabaseError(pub String);diff --git a/crates/screenpipe-server/src/core.rs b/crates/screenpipe-server/src/core.rsindex c122ed141..9c8ac6ce9 100644--- a/crates/screenpipe-server/src/core.rs+++ b/crates/screenpipe-server/src/core.rs@@ -3,7 +3,7 @@ use anyhow::Result;use futures::future::join_all;use screenpipe_core::pii_removal::{remove_pii, remove_pii_from_text_json};use screenpipe_core::Language;-use screenpipe_db::{DatabaseManager, Speaker};+use screenpipe_db::{DatabaseManager, FrameWindowData, Speaker};use screenpipe_events::{poll_meetings_events, send_event};use screenpipe_vision::core::WindowOcr;use screenpipe_vision::OcrEngine;@@ -273,49 +273,66 @@ pub async fn record_video(}};+ // Prepare batch data: apply PII removal and collect window data+ let mut batch_windows = Vec::with_capacity(frame.window_ocr_results.len());+ let mut window_metadata = Vec::with_capacity(frame.window_ocr_results.len());+for window_result in &frame.window_ocr_results {- let insert_frame_start = std::time::Instant::now();- let result = db- .insert_frame(- &device_name,- Some(frame.captured_at),- window_result.browser_url.as_deref(),- Some(window_result.app_name.as_str()),- Some(window_result.window_name.as_str()),- window_result.focused,- Some(video_frame_offset), // All windows share the same video frame offset- )- .await;+ let (text, sanitized_text_json) = if use_pii_removal {+ let sanitized_text = remove_pii(&window_result.text);+ let sanitized_json = remove_pii_from_text_json(&window_result.text_json);+ (sanitized_text, sanitized_json)+ } else {+ (window_result.text.clone(), window_result.text_json.clone())+ };+ let text_json = serde_json::to_string(&sanitized_text_json).unwrap_or_default();- let insert_duration = insert_frame_start.elapsed();- if insert_duration.as_millis() > 100 {- warn!(- "Slow DB insert_frame operation: {}ms",- insert_duration.as_millis()- );- }+ batch_windows.push(FrameWindowData {+ app_name: Some(window_result.app_name.clone()),+ window_name: Some(window_result.window_name.clone()),+ browser_url: window_result.browser_url.clone(),+ focused: window_result.focused,+ text: text.clone(),+ text_json: text_json.clone(),+ });- match result {- Ok(frame_id) => {- debug!(- "Successfully inserted frame {} in {}ms",- frame_id,- insert_duration.as_millis()- );+ // Store metadata for realtime events (sent after DB insert)+ window_metadata.push((text, sanitized_text_json, text_json, window_result));+ }- // Apply PII removal if enabled- let (text, sanitized_text_json) = if use_pii_removal {- let sanitized_text = remove_pii(&window_result.text);- let sanitized_json =- remove_pii_from_text_json(&window_result.text_json);- (sanitized_text, sanitized_json)- } else {- (window_result.text.clone(), window_result.text_json.clone())- };- let text_json =- serde_json::to_string(&sanitized_text_json).unwrap_or_default();+ // Batch insert all frames + OCR in a single transaction+ let batch_start = std::time::Instant::now();+ match db+ .insert_frames_with_ocr_batch(+ &device_name,+ Some(frame.captured_at),+ video_frame_offset,+ &batch_windows,+ Arc::new((*ocr_engine).clone().into()),+ )+ .await+ {+ Ok(results) => {+ let batch_duration = batch_start.elapsed();+ if batch_duration.as_millis() > 200 {+ warn!(+ "Slow DB batch insert: {}ms for {} windows",+ batch_duration.as_millis(),+ results.len()+ );+ }+ debug!(+ "Batch inserted {} frames in {}ms",+ results.len(),+ batch_duration.as_millis()+ );+ consecutive_db_errors = 0;- if realtime_vision {+ // Send realtime events after successful DB insert+ if realtime_vision {+ for (frame_id, idx) in &results {+ let (ref text, ref sanitized_text_json, _, window_result) =+ window_metadata[*idx];let send_event_start = std::time::Instant::now();match send_event("ocr_result",@@ -335,54 +352,22 @@ pub async fn record_video(let event_duration = send_event_start.elapsed();if event_duration.as_millis() > 100 {warn!(- "Slow event sending: {}ms",- event_duration.as_millis()+ "Slow event sending: {}ms for frame {}",+ event_duration.as_millis(),+ frame_id);}}Err(e) => error!("Failed to send OCR event: {}", e),}}-- let insert_ocr_start = std::time::Instant::now();- if let Err(e) = db- .insert_ocr_text(- frame_id,- &text,- &text_json,- Arc::new((*ocr_engine).clone().into()),- )- .await- {- error!(- "Failed to insert OCR text: {}, skipping window {} of frame {}",- e, window_result.window_name, frame_id- );- consecutive_db_errors += 1;- continue;- } else {- let ocr_insert_duration = insert_ocr_start.elapsed();- if ocr_insert_duration.as_millis() > 100 {- warn!(- "Slow DB insert_ocr_text operation: {}ms",- ocr_insert_duration.as_millis()- );- }- consecutive_db_errors = 0; // Reset on success- debug!(- "OCR text inserted for frame {} in {}ms",- frame_id,- ocr_insert_duration.as_millis()- );- }- }- Err(e) => {- warn!("Failed to insert frame: {}", e);- consecutive_db_errors += 1;- tokio::time::sleep(Duration::from_millis(100)).await;- continue;}}+ Err(e) => {+ warn!("Failed to batch insert frames: {}", e);+ consecutive_db_errors += 1;+ tokio::time::sleep(Duration::from_millis(100)).await;+ }}} else {// Log when frame queue is empty