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 shutdown (#1287)
Ezra Ellette00d91297680b
diff --git a/screenpipe-audio/src/deepgram/realtime.rs b/screenpipe-audio/src/deepgram/realtime.rsindex 866ad365d..7915a7ab0 100644--- a/screenpipe-audio/src/deepgram/realtime.rs+++ b/screenpipe-audio/src/deepgram/realtime.rs@@ -68,10 +68,7 @@ pub async fn start_deepgram_stream(}});- info!(- "Starting deepgram stream with api key: {:?} and url: {:?}",- api_key, DEEPGRAM_WEBSOCKET_URL.as_str()- );+ info!("Starting deepgram stream for device: {}", device);let deepgram = match DEEPGRAM_WEBSOCKET_URL.as_str().is_empty() {true => deepgram::Deepgram::new(api_key)?,@@ -101,6 +98,7 @@ pub async fn start_deepgram_stream(loop {tokio::select! {_ = &mut shutdown_rx => {+ info!("Shutting down deepgram stream for device: {}", device);break;}result = results.try_next() => {@@ -130,7 +128,9 @@ fn get_stream(mut stream: Receiver<Vec<f32>>) -> FuturesReceiver<Result<Bytes, Rfor sample in data {bytes.put_i16_le((sample * i16::MAX as f32) as i16);}- tx.send(Ok(bytes.freeze())).await.unwrap();+ if tx.send(Ok(bytes.freeze())).await.is_err() {+ tx.close_channel();+ }}});diff --git a/screenpipe-audio/src/stt.rs b/screenpipe-audio/src/stt.rsindex 75b794a63..d4bbfa451 100644--- a/screenpipe-audio/src/stt.rs+++ b/screenpipe-audio/src/stt.rs@@ -16,6 +16,7 @@ use log::{debug, error};#[cfg(target_os = "macos")]use objc::rc::autoreleasepool;use screenpipe_core::{AudioDevice, DeviceManager, Language};+// use std::time::Duration;use std::{path::Path,sync::Arc,@@ -277,6 +278,7 @@ pub async fn create_whisper_channel(}}},+ // default(Duration::from_millis(100)) => {}}}});diff --git a/screenpipe-core/src/devices.rs b/screenpipe-core/src/devices.rsindex eabb4479c..1a19c5151 100644--- a/screenpipe-core/src/devices.rs+++ b/screenpipe-core/src/devices.rs@@ -1,4 +1,4 @@-use std::{collections::HashMap, fmt};+use std::{collections::HashMap, fmt, str::FromStr};use anyhow::anyhow;use serde::{Deserialize, Serialize};@@ -30,8 +30,10 @@ impl fmt::Display for DeviceType {}}-impl DeviceType {- pub fn from_str(s: &str) -> anyhow::Result<Self> {+impl FromStr for DeviceType {+ type Err = anyhow::Error;++ fn from_str(s: &str) -> Result<Self, Self::Err> {// Check if it's a monitor ID (vision device)if let Ok(monitor_id) = s.parse::<u32>() {return Ok(DeviceType::Vision(monitor_id));@@ -155,8 +157,8 @@ pub struct DeviceStateChange {pub control: DeviceControl,}-impl DeviceManager {- pub fn default() -> Self {+impl Default for DeviceManager {+ fn default() -> Self {let (sender, dummy_receiver) = tokio::sync::broadcast::channel(32);let (state_sender, state_receiver) = tokio::sync::mpsc::channel(32);@@ -170,7 +172,9 @@ impl DeviceManager {state_sender,}}+}+impl DeviceManager {async fn manage_state(mut receiver: tokio::sync::mpsc::Receiver<DeviceStateRequest>) {let mut devices = HashMap::new();let mut watchers = Vec::new();@@ -246,4 +250,13 @@ impl DeviceManager {self.sender.send(control)?;Ok(())}++ pub async fn shutdown(&self) {+ for device in self.get_active_devices().await.values_mut() {+ device.is_running = false;+ if !self.state_sender.is_closed() {+ self.update_device(device.clone()).await.unwrap();+ }+ }+ }}diff --git a/screenpipe-server/src/bin/screenpipe-server.rs b/screenpipe-server/src/bin/screenpipe-server.rsindex b64ef0be1..5336b5369 100644--- a/screenpipe-server/src/bin/screenpipe-server.rs+++ b/screenpipe-server/src/bin/screenpipe-server.rs@@ -675,6 +675,7 @@ async fn main() -> anyhow::Result<()> {let audio_chunk_duration = Duration::from_secs(cli.audio_chunk_duration);let dm_clone = device_manager.clone();let device_manager_clone = device_manager.clone();+ let device_manager_clone_2 = device_manager.clone();let (whisper_sender, whisper_receiver) = if cli.disable_audio {// Create a dummy channel if no audio devices are available, e.g. audio disabled@@ -1347,6 +1348,8 @@ async fn main() -> anyhow::Result<()> {}}+ device_manager_clone_2.shutdown().await;+tokio::task::block_in_place(|| {drop(vision_runtime);drop(audio_runtime);diff --git a/screenpipe-server/src/core.rs b/screenpipe-server/src/core.rsindex 96d84b8af..00785a377 100644--- a/screenpipe-server/src/core.rs+++ b/screenpipe-server/src/core.rs@@ -14,6 +14,7 @@ use screenpipe_events::{poll_meetings_events, send_event};use screenpipe_vision::core::WindowOcr;use screenpipe_vision::{CaptureResult, OcrEngine};use std::collections::HashMap;+use std::str::FromStr;use std::sync::atomic::{AtomicBool, Ordering};use std::sync::Arc;use std::sync::Weak;@@ -161,6 +162,7 @@ pub async fn start_continuous_recording(Ok(())}+#[allow(clippy::too_many_arguments)]async fn record_vision(device_manager: Arc<DeviceManager>,ocr_engine: Arc<OcrEngine>,@@ -407,6 +409,7 @@ async fn process_ocr_frame(}}+#[allow(clippy::too_many_arguments)]async fn record_audio(device_manager: Arc<DeviceManager>,db: Arc<DatabaseManager>,