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

fix: WhatsApp auto-reconnect on server start + accurate status

Louis Beaumont6f623b30a266
diff --git a/crates/screenpipe-connect/src/whatsapp/mod.rs b/crates/screenpipe-connect/src/whatsapp/mod.rs
index c26402d95..6708c36ed 100644
--- a/crates/screenpipe-connect/src/whatsapp/mod.rs
+++ b/crates/screenpipe-connect/src/whatsapp/mod.rs
@@ -216,6 +216,23 @@ impl WhatsAppGateway {
}
}
+/// Find bun executable on PATH.
+pub fn which_bun() -> Option<String> {
+ std::process::Command::new("which")
+ .arg("bun")
+ .output()
+ .ok()
+ .and_then(|o| {
+ if o.status.success() {
+ String::from_utf8(o.stdout)
+ .ok()
+ .map(|s| s.trim().to_string())
+ } else {
+ None
+ }
+ })
+}
+
/// Find node executable on PATH.
fn which_node() -> Option<String> {
std::process::Command::new("which")
diff --git a/crates/screenpipe-engine/src/cli/connection.rs b/crates/screenpipe-engine/src/cli/connection.rs
index 17c6cf9be..862028845 100644
--- a/crates/screenpipe-engine/src/cli/connection.rs
+++ b/crates/screenpipe-engine/src/cli/connection.rs
@@ -18,13 +18,22 @@ pub async fn handle_connection_command(command: &ConnectionCommand) -> anyhow::R
ConnectionCommand::List { json: use_json } => {
let list = cm.list();
- // Add WhatsApp — check session on disk
- let wa_connected = screenpipe_dir
+ // Add WhatsApp — check session on disk AND whether gateway is actually reachable
+ let has_session = screenpipe_dir
.join("whatsapp-session")
.join("creds.json")
.exists();
+ let wa_connected = has_session
+ && reqwest::Client::new()
+ .get("http://localhost:3035/status")
+ .timeout(std::time::Duration::from_secs(2))
+ .send()
+ .await
+ .is_ok();
let wa_desc = if wa_connected {
"WhatsApp messaging. First GET http://localhost:3035/contacts to resolve names to phone numbers, then POST http://localhost:3035/send with {\"to\":\"+PHONE\",\"text\":\"MSG\"}".to_string()
+ } else if has_session {
+ "WhatsApp has a saved session but the gateway is not running. It should auto-reconnect on server restart.".to_string()
} else {
"Not paired. User must pair via Settings > Connections in the desktop app first."
.to_string()
@@ -63,16 +72,30 @@ pub async fn handle_connection_command(command: &ConnectionCommand) -> anyhow::R
ConnectionCommand::Get { id, json: use_json } => {
if id == "whatsapp" {
- let connected = screenpipe_dir
+ let has_session = screenpipe_dir
.join("whatsapp-session")
.join("creds.json")
.exists();
- let info = if connected {
+ let gateway_reachable = has_session
+ && reqwest::Client::new()
+ .get("http://localhost:3035/status")
+ .timeout(std::time::Duration::from_secs(2))
+ .send()
+ .await
+ .is_ok();
+ let info = if gateway_reachable {
json!({
"id": "whatsapp",
"connected": true,
"description": "WhatsApp messaging. First GET http://localhost:3035/contacts to resolve names to phone numbers, then POST http://localhost:3035/send with {\"to\":\"+PHONE\",\"text\":\"MSG\"}"
})
+ } else if has_session {
+ json!({
+ "id": "whatsapp",
+ "connected": false,
+ "has_session": true,
+ "description": "WhatsApp has a saved session but the gateway is not running. It should auto-reconnect on server restart."
+ })
} else {
json!({
"id": "whatsapp",
@@ -83,7 +106,7 @@ pub async fn handle_connection_command(command: &ConnectionCommand) -> anyhow::R
if *use_json {
println!("{}", serde_json::to_string_pretty(&info)?);
} else {
- let status = if connected {
+ let status = if gateway_reachable {
"connected"
} else {
"not connected"
diff --git a/crates/screenpipe-engine/src/connections_api.rs b/crates/screenpipe-engine/src/connections_api.rs
index cb7711b9f..f84c1cd3d 100644
--- a/crates/screenpipe-engine/src/connections_api.rs
+++ b/crates/screenpipe-engine/src/connections_api.rs
@@ -72,7 +72,8 @@ async fn list_connections(State(state): State<ConnectionsState>) -> Json<Value>
"category": "notification",
"description": wa_desc,
"fields": [],
- "connected": wa_connected || has_session,
+ "connected": wa_connected,
+ "has_session": has_session,
}));
}
diff --git a/crates/screenpipe-engine/src/server.rs b/crates/screenpipe-engine/src/server.rs
index e5eadea80..6c67458f8 100644
--- a/crates/screenpipe-engine/src/server.rs
+++ b/crates/screenpipe-engine/src/server.rs
@@ -611,6 +611,20 @@ impl SCServer {
let wa: crate::connections_api::SharedWhatsAppGateway = Arc::new(Mutex::new(
screenpipe_connect::whatsapp::WhatsAppGateway::new(self.screenpipe_dir.clone()),
));
+
+ // Auto-reconnect WhatsApp if a previous session exists on disk
+ {
+ let wa_lock = wa.lock().await;
+ if wa_lock.has_session() {
+ tracing::info!("whatsapp: found existing session, auto-reconnecting...");
+ let bun_path =
+ screenpipe_connect::whatsapp::which_bun().unwrap_or_else(|| "bun".to_string());
+ if let Err(e) = wa_lock.start_pairing(&bun_path).await {
+ tracing::warn!("whatsapp: auto-reconnect failed: {:?}", e);
+ }
+ }
+ }
+
let router = router.nest("/connections", crate::connections_api::router(cm, wa));
// Power management routes (if power manager is available)