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
feat(timeline): skip directly to valid frame instead of one-by-one
Louis Beaumonta7934132bb5d
diff --git a/screenpipe-app-tauri/components/rewind/timeline.tsx b/screenpipe-app-tauri/components/rewind/timeline.tsxindex 59fb7a22a..cafd63b85 100644--- a/screenpipe-app-tauri/components/rewind/timeline.tsx+++ b/screenpipe-app-tauri/components/rewind/timeline.tsx@@ -789,21 +789,51 @@ export default function Timeline() {}}canNavigatePrev={currentIndex > 0}canNavigateNext={currentIndex < frames.length - 1}- onFrameUnavailable={() => {- // Auto-skip to next available frame- if (currentIndex < frames.length - 1) {- const newIndex = currentIndex + 1;- setCurrentIndex(newIndex);- if (frames[newIndex]) {- setCurrentFrame(frames[newIndex]);+ onFrameUnavailable={async () => {+ // Get the current frame's frame_id+ const failedFrameId = frames[currentIndex]?.devices?.[0]?.frame_id;+ if (!failedFrameId) {+ // Fallback to old behavior if no frame_id+ if (currentIndex < frames.length - 1) {+ setCurrentIndex(currentIndex + 1);+ setCurrentFrame(frames[currentIndex + 1]);}- } else if (currentIndex > 0) {- // If at end, try previous- const newIndex = currentIndex - 1;- setCurrentIndex(newIndex);- if (frames[newIndex]) {- setCurrentFrame(frames[newIndex]);+ return;+ }++ try {+ // Query the server for the next valid frame+ const response = await fetch(+ `http://localhost:3030/frames/next-valid?frame_id=${failedFrameId}&direction=forward&limit=50`+ );++ if (response.ok) {+ const data = await response.json();+ const validFrameId = String(data.frame_id);++ // Find this frame in our local frames array+ const validIndex = frames.findIndex(+ (f) => f.devices?.[0]?.frame_id === validFrameId+ );++ if (validIndex !== -1) {+ // Jump directly to the valid frame+ setCurrentIndex(validIndex);+ setCurrentFrame(frames[validIndex]);+ return;+ }}+ } catch (error) {+ console.error("Failed to find next valid frame:", error);+ }++ // Fallback: try the old sequential approach+ if (currentIndex < frames.length - 1) {+ setCurrentIndex(currentIndex + 1);+ setCurrentFrame(frames[currentIndex + 1]);+ } else if (currentIndex > 0) {+ setCurrentIndex(currentIndex - 1);+ setCurrentFrame(frames[currentIndex - 1]);}}}/>diff --git a/screenpipe-db/src/db.rs b/screenpipe-db/src/db.rsindex 7b72377a1..886848006 100644--- a/screenpipe-db/src/db.rs+++ b/screenpipe-db/src/db.rs@@ -1092,6 +1092,50 @@ impl DatabaseManager {.await}+ /// Get frames after a given frame_id for validation checking+ /// Returns frame_id, file_path, offset_index, and timestamp+ /// Direction: true = forward (newer frames), false = backward (older frames)+ pub async fn get_frames_near(+ &self,+ frame_id: i64,+ forward: bool,+ limit: i32,+ ) -> Result<Vec<(i64, String, i64, DateTime<Utc>)>, sqlx::Error> {+ let query = if forward {+ r#"+ SELECT+ frames.id,+ video_chunks.file_path,+ frames.offset_index,+ frames.timestamp+ FROM frames+ JOIN video_chunks ON frames.video_chunk_id = video_chunks.id+ WHERE frames.id > ?1+ ORDER BY frames.id ASC+ LIMIT ?2+ "#+ } else {+ r#"+ SELECT+ frames.id,+ video_chunks.file_path,+ frames.offset_index,+ frames.timestamp+ FROM frames+ JOIN video_chunks ON frames.video_chunk_id = video_chunks.id+ WHERE frames.id < ?1+ ORDER BY frames.id DESC+ LIMIT ?2+ "#+ };++ sqlx::query_as::<_, (i64, String, i64, DateTime<Utc>)>(query)+ .bind(frame_id)+ .bind(limit)+ .fetch_all(&self.pool)+ .await+ }+/// Get the OCR text_json for a frame, which contains bounding box information/// needed for PII redactionpub async fn get_frame_ocr_text_json(diff --git a/screenpipe-server/src/server.rs b/screenpipe-server/src/server.rsindex e31f78eb4..650cbd481 100644--- a/screenpipe-server/src/server.rs+++ b/screenpipe-server/src/server.rs@@ -1308,6 +1308,7 @@ impl SCServer {.post("/pipes/purge", purge_pipe_handler).get("/frames/:frame_id", get_frame_data).get("/frames/:frame_id/ocr", get_frame_ocr_data)+ .get("/frames/next-valid", get_next_valid_frame).get("/health", health_check).post("/raw_sql", execute_raw_sql).post("/add", add_to_database)@@ -3113,6 +3114,87 @@ pub async fn get_frame_data(}}+/// Query parameters for finding the next valid frame+#[derive(Debug, Deserialize, OaSchema)]+pub struct NextValidFrameQuery {+ /// Current frame_id that failed to load+ pub frame_id: i64,+ /// Direction: "forward" (default) or "backward"+ #[serde(default = "default_direction")]+ pub direction: String,+ /// Maximum number of frames to check (default: 50)+ #[serde(default = "default_frame_check_limit")]+ pub limit: i32,+}++fn default_direction() -> String {+ "forward".to_string()+}++fn default_frame_check_limit() -> i32 {+ 50+}++/// Response for next valid frame endpoint+#[derive(OaSchema, Serialize)]+pub struct NextValidFrameResponse {+ /// The frame_id of the next valid frame+ pub frame_id: i64,+ /// Timestamp of the valid frame+ pub timestamp: DateTime<Utc>,+ /// Number of invalid frames that were skipped+ pub skipped_count: i32,+}++/// Find the next frame that has a valid video file on disk.+/// This allows the frontend to skip directly to a valid frame instead of+/// trying each frame one-by-one when frames fail to load.+#[oasgen]+pub async fn get_next_valid_frame(+ State(state): State<Arc<AppState>>,+ Query(query): Query<NextValidFrameQuery>,+) -> Result<JsonResponse<NextValidFrameResponse>, (StatusCode, JsonResponse<Value>)> {+ let forward = query.direction.to_lowercase() != "backward";++ // Get candidate frames from database+ let candidates = match state.db.get_frames_near(query.frame_id, forward, query.limit).await {+ Ok(frames) => frames,+ Err(e) => {+ error!("Failed to get frames near {}: {}", query.frame_id, e);+ return Err((+ StatusCode::INTERNAL_SERVER_ERROR,+ JsonResponse(json!({+ "error": format!("Database error: {}", e),+ "frame_id": query.frame_id+ })),+ ));+ }+ };++ // Check each frame's video file exists on disk+ let mut skipped = 0;+ for (frame_id, file_path, _offset_index, timestamp) in candidates {+ if std::path::Path::new(&file_path).exists() {+ return Ok(JsonResponse(NextValidFrameResponse {+ frame_id,+ timestamp,+ skipped_count: skipped,+ }));+ }+ skipped += 1;+ }++ // No valid frames found+ Err((+ StatusCode::NOT_FOUND,+ JsonResponse(json!({+ "error": "No valid frames found",+ "frame_id": query.frame_id,+ "checked_count": skipped+ })),+ ))+}+/// Response type for frame OCR data endpoint#[derive(OaSchema, Serialize)]pub struct FrameOcrResponse {