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

feat: #295

Louis Beaumont2bd6c73d47c8
diff --git a/screenpipe-server/src/bin/screenpipe-server.rs b/screenpipe-server/src/bin/screenpipe-server.rs
index cbcefc3cd..e114b7de7 100644
--- a/screenpipe-server/src/bin/screenpipe-server.rs
+++ b/screenpipe-server/src/bin/screenpipe-server.rs
@@ -24,7 +24,7 @@ use screenpipe_server::{
cli::{Cli, CliAudioTranscriptionEngine, CliOcrEngine, Command, PipeCommand},
start_continuous_recording, DatabaseManager, PipeManager, ResourceMonitor, Server,
};
-use screenpipe_vision::monitor::{get_monitor_by_id, list_monitors};
+use screenpipe_vision::monitor::list_monitors;
use serde_json::{json, Value};
use tokio::{
runtime::Runtime,
@@ -263,21 +263,12 @@ async fn main() -> anyhow::Result<()> {
let warning_ocr_engine_clone = cli.ocr_engine.clone();
let warning_audio_transcription_engine_clone = cli.audio_transcription_engine.clone();
- let monitor_id = cli.monitor_id.unwrap_or(all_monitors.first().unwrap().id());
+ let monitor_ids = if cli.monitor_id.is_empty() {
+ all_monitors.iter().map(|m| m.id()).collect::<Vec<_>>()
+ } else {
+ cli.monitor_id.clone()
+ };
- // try to use the monitor selected, if not available throw an error
- get_monitor_by_id(monitor_id).await.unwrap_or_else(|| {
- eprintln!(
- "{}",
- format!(
- "Monitor with id {} not found. Try 'screenpipe --list-monitors'",
- monitor_id
- )
- .red()
- );
- std::process::exit(1);
- });
- debug!("Monitor with id {} found", monitor_id);
let ocr_engine_clone = cli.ocr_engine.clone();
let restart_interval = cli.restart_interval;
@@ -294,6 +285,7 @@ async fn main() -> anyhow::Result<()> {
let vision_control_clone = Arc::clone(&vision_control);
let shutdown_tx_clone = shutdown_tx.clone();
let friend_wearable_uid_clone = friend_wearable_uid.clone(); // Clone here
+ let monitor_ids_clone = cli.monitor_id.clone();
let fps = if cli.fps.is_finite() && cli.fps > 0.0 {
cli.fps
@@ -328,7 +320,7 @@ async fn main() -> anyhow::Result<()> {
Arc::new(cli.audio_transcription_engine.clone().into()),
Arc::new(cli.ocr_engine.clone().into()),
friend_wearable_uid_clone.clone(),
- monitor_id,
+ monitor_ids_clone.clone(),
cli.use_pii_removal,
cli.disable_vision,
&vision_handle,
@@ -415,7 +407,10 @@ async fn main() -> anyhow::Result<()> {
"│ OCR Engine │ {:<34} │",
format!("{:?}", ocr_engine_clone)
);
- println!("│ Monitor ID │ {:<34} │", monitor_id);
+ println!(
+ "│ Monitor IDs │ {:<34} │",
+ format_cell(&format!("{:?}", monitor_ids), VALUE_WIDTH)
+ );
println!(
"│ Data Directory │ {:<34} │",
local_data_dir_clone.display()
diff --git a/screenpipe-server/src/cli.rs b/screenpipe-server/src/cli.rs
index 465b42a28..98b98a7e4 100644
--- a/screenpipe-server/src/cli.rs
+++ b/screenpipe-server/src/cli.rs
@@ -135,9 +135,9 @@ pub struct Cli {
#[arg(long)]
pub list_monitors: bool,
- /// Monitor ID to use, this will be used to select the monitor to record
+ /// Monitor IDs to use, these will be used to select the monitors to record
#[arg(short = 'm', long)]
- pub monitor_id: Option<u32>,
+ pub monitor_id: Vec<u32>,
/// Enable PII removal from OCR text property that is saved to db and returned in search results
#[arg(long, default_value_t = false)]
diff --git a/screenpipe-server/src/core.rs b/screenpipe-server/src/core.rs
index 7ada9793b..a312ed82d 100644
--- a/screenpipe-server/src/core.rs
+++ b/screenpipe-server/src/core.rs
@@ -2,6 +2,7 @@ use crate::{DatabaseManager, VideoCapture};
use anyhow::Result;
use chrono::Utc;
use crossbeam::queue::SegQueue;
+use futures::future::join_all;
use log::{debug, error, info, warn};
use screenpipe_audio::{
create_whisper_channel, record_and_transcribe, AudioDevice, AudioInput,
@@ -31,7 +32,7 @@ pub async fn start_continuous_recording(
audio_transcription_engine: Arc<AudioTranscriptionEngine>,
ocr_engine: Arc<OcrEngine>,
friend_wearable_uid: Option<String>,
- monitor_id: u32,
+ monitor_ids: Vec<u32>,
use_pii_removal: bool,
vision_disabled: bool,
vision_handle: &Handle,
@@ -56,17 +57,8 @@ pub async fn start_continuous_recording(
create_whisper_channel(audio_transcription_engine.clone()).await?
};
let whisper_sender_clone = whisper_sender.clone();
- let db_manager_video = Arc::clone(&db);
let db_manager_audio = Arc::clone(&db);
-
- let is_running_video = Arc::clone(&vision_control);
-
- let output_path_video = Arc::clone(&output_path);
let output_path_audio = Arc::clone(&output_path);
-
- let friend_wearable_uid_video = friend_wearable_uid.clone();
- let ignored_windows_video = ignored_windows.to_vec();
- let include_windows_video = include_windows.to_vec();
// Initialize friend wearable loop
if let Some(uid) = &friend_wearable_uid {
tokio::spawn(initialize_friend_wearable_loop(
@@ -75,28 +67,41 @@ pub async fn start_continuous_recording(
));
}
- let video_task = if !vision_disabled {
- vision_handle.spawn(async move {
- record_video(
- db_manager_video,
- output_path_video,
- fps,
- is_running_video,
- save_text_files,
- ocr_engine,
- friend_wearable_uid_video,
- monitor_id,
- use_pii_removal,
- &ignored_windows_video,
- &include_windows_video,
- )
- .await
- })
+ let video_tasks = if !vision_disabled {
+ monitor_ids
+ .iter()
+ .map(|&monitor_id| {
+ let db_manager_video = Arc::clone(&db);
+ let output_path_video = Arc::clone(&output_path);
+ let is_running_video = Arc::clone(&vision_control);
+ let ocr_engine = Arc::clone(&ocr_engine);
+ let friend_wearable_uid_video = friend_wearable_uid.clone();
+ let ignored_windows_video = ignored_windows.to_vec();
+ let include_windows_video = include_windows.to_vec();
+
+ vision_handle.spawn(async move {
+ record_video(
+ db_manager_video,
+ output_path_video,
+ fps,
+ is_running_video,
+ save_text_files,
+ ocr_engine,
+ friend_wearable_uid_video,
+ monitor_id,
+ use_pii_removal,
+ &ignored_windows_video,
+ &include_windows_video,
+ )
+ .await
+ })
+ })
+ .collect::<Vec<_>>()
} else {
- vision_handle.spawn(async move {
+ vec![vision_handle.spawn(async move {
tokio::time::sleep(Duration::from_secs(60)).await;
Ok(())
- })
+ })]
};
let audio_task = if !audio_disabled {
@@ -120,14 +125,16 @@ pub async fn start_continuous_recording(
})
};
- // Wait for both tasks to complete
- let (video_result, audio_result) = tokio::join!(video_task, audio_task);
+ // Join all video tasks
+ let video_results = join_all(video_tasks);
// Handle any errors from the tasks
- if let Err(e) = video_result {
- error!("Video recording error: {:?}", e);
+ for (i, result) in video_results.await.into_iter().enumerate() {
+ if let Err(e) = result {
+ error!("Video recording error for monitor {}: {:?}", i, e);
+ }
}
- if let Err(e) = audio_result {
+ if let Err(e) = audio_task.await {
error!("Audio recording error: {:?}", e);
}
diff --git a/screenpipe-vision/src/capture_screenshot_by_window.rs b/screenpipe-vision/src/capture_screenshot_by_window.rs
index 0e71027be..7d7358701 100644
--- a/screenpipe-vision/src/capture_screenshot_by_window.rs
+++ b/screenpipe-vision/src/capture_screenshot_by_window.rs
@@ -31,64 +31,62 @@ impl From<XCapError> for CaptureError {
}
pub async fn capture_all_visible_windows(
+ monitor: &Monitor,
ignore_list: &[String],
include_list: &[String],
) -> Result<Vec<(DynamicImage, String, String, bool)>, Box<dyn Error>> {
- let monitors = Monitor::all()?;
-
let mut all_captured_images = Vec::new();
- for monitor in monitors {
- let windows = retry_with_backoff(
- || {
- let windows = Window::all()?;
- if windows.is_empty() {
- Err(CaptureError::NoWindows)
- } else {
- Ok(windows)
- }
- },
- 3,
- Duration::from_millis(500),
- )
- .await?;
-
- // ! THIS is extremely inefficient
- let focused_window = get_focused_window(&monitor, ignore_list, include_list).await;
-
- for (_index, window) in windows.into_iter().enumerate() {
- if is_valid_window(&window, &monitor, ignore_list, include_list) {
- let app_name = window.app_name();
- let window_name = window.title();
- let is_focused = focused_window
- .as_ref()
- .map_or(false, |fw| fw.id() == window.id());
-
- match window.capture_image() {
- Ok(buffer) => {
- let image = DynamicImage::ImageRgba8(
- image::ImageBuffer::from_raw(
- buffer.width() as u32,
- buffer.height() as u32,
- buffer.into_raw(),
- )
- .unwrap(),
- );
-
- all_captured_images.push((
- image,
- app_name.to_string(),
- window_name.to_string(),
- is_focused,
- ));
- }
- Err(e) => error!(
- "Failed to capture image for window {} on monitor {}: {}",
- window_name,
- monitor.name(),
- e
- ),
+ let windows = retry_with_backoff(
+ || {
+ let windows = Window::all()?;
+ if windows.is_empty() {
+ Err(CaptureError::NoWindows)
+ } else {
+ Ok(windows)
+ }
+ },
+ 3,
+ Duration::from_millis(500),
+ )
+ .await?;
+
+ let focused_window = windows
+ .iter()
+ .find(|&w| is_valid_window(w, monitor, ignore_list, include_list));
+
+ for window in &windows {
+ if is_valid_window(window, monitor, ignore_list, include_list) {
+ let app_name = window.app_name();
+ let window_name = window.title();
+ let is_focused = focused_window
+ .as_ref()
+ .map_or(false, |fw| fw.id() == window.id());
+
+ match window.capture_image() {
+ Ok(buffer) => {
+ let image = DynamicImage::ImageRgba8(
+ image::ImageBuffer::from_raw(
+ buffer.width() as u32,
+ buffer.height() as u32,
+ buffer.into_raw(),
+ )
+ .unwrap(),
+ );
+
+ all_captured_images.push((
+ image,
+ app_name.to_string(),
+ window_name.to_string(),
+ is_focused,
+ ));
}
+ Err(e) => error!(
+ "Failed to capture image for window {} on monitor {}: {}",
+ window_name,
+ monitor.name(),
+ e
+ ),
}
}
}
@@ -168,23 +166,3 @@ where
}
unreachable!()
}
-
-async fn get_focused_window(
- monitor: &Monitor,
- ignore_list: &[String],
- include_list: &[String],
-) -> Option<Window> {
- retry_with_backoff(
- || -> Result<Option<Window>, CaptureError> {
- let windows = Window::all()?;
- Ok(windows
- .into_iter()
- .find(|w| is_valid_window(w, &monitor, ignore_list, include_list)))
- },
- 3,
- Duration::from_millis(500),
- )
- .await
- .ok()
- .flatten()
-}
diff --git a/screenpipe-vision/src/utils.rs b/screenpipe-vision/src/utils.rs
index f4c0485c2..75f993e7b 100644
--- a/screenpipe-vision/src/utils.rs
+++ b/screenpipe-vision/src/utils.rs
@@ -72,12 +72,9 @@ pub async fn capture_screenshot(
let image_hash = calculate_hash(&image);
let capture_duration = capture_start.elapsed();
- // info!("Attempting to capture all visible windows");
- let window_images = match capture_all_visible_windows(ignore_list, include_list).await {
- Ok(images) => {
- // info!("Successfully captured {} window images", images.len());
- images
- }
+ let window_images = match capture_all_visible_windows(monitor, ignore_list, include_list).await
+ {
+ Ok(images) => images,
Err(e) => {
warn!(
"Failed to capture window images: {}. Continuing with empty result.",