Skip to content

armgit / jan

Public Imported from GitHub
0

Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.

fix: fix whatsapp connection flow

Vanalite8705deba8db5
diff --git a/src-tauri/src/core/openclaw/commands.rs b/src-tauri/src/core/openclaw/commands.rs
index 53db39589..28d175147 100644
--- a/src-tauri/src/core/openclaw/commands.rs
+++ b/src-tauri/src/core/openclaw/commands.rs
@@ -3209,11 +3209,7 @@ pub async fn whatsapp_check_auth() -> Result<WhatsAppAuthStatus, String> {
// it into connecting. The flag is reset in whatsapp_start_auth.
if !WA_RESTART_ATTEMPTED.swap(true, Ordering::SeqCst) {
log::info!("Restarting gateway to establish WhatsApp connection");
- let _ = openclaw_command(&["gateway", "restart"]).await
- .stdout(Stdio::piped())
- .stderr(Stdio::piped())
- .output()
- .await;
+ let _ = restart_gateway_cli().await;
}
// Return in_progress so the frontend keeps polling
@@ -3259,17 +3255,46 @@ pub async fn whatsapp_check_auth() -> Result<WhatsAppAuthStatus, String> {
}
}
- // Still waiting for authentication, get QR code from local config
- let config_path = get_whatsapp_config_path()?;
- let qr_code = if config_path.exists() {
- let config_json = std::fs::read_to_string(&config_path).ok();
- config_json.and_then(|json| {
- serde_json::from_str::<serde_json::Value>(&json).ok()
- }).and_then(|settings| {
- settings.get("qr_code").and_then(|v| v.as_str()).map(String::from)
- })
- } else {
- None
+ // Still waiting for authentication — refresh the QR code from Gateway.
+ // WhatsApp QR codes expire every ~20s, so we must fetch a fresh one
+ // rather than serving the stale one from local config.
+ let qr_params = serde_json::json!({
+ "accountId": "default",
+ "timeoutMs": 5000
+ });
+
+ let qr_code = match gateway_request("web.login.start", qr_params).await {
+ Ok(qr_payload) => {
+ let qr = qr_payload.get("qrDataUrl")
+ .and_then(|v| v.as_str())
+ .map(String::from);
+
+ // Update local config with fresh QR
+ if let Some(ref qr_data) = qr {
+ if let Ok(config_path) = get_whatsapp_config_path() {
+ if config_path.exists() {
+ if let Ok(content) = std::fs::read_to_string(&config_path) {
+ if let Ok(mut cfg) = serde_json::from_str::<serde_json::Value>(&content) {
+ cfg["qr_code"] = serde_json::json!(qr_data);
+ let _ = std::fs::write(&config_path, serde_json::to_string_pretty(&cfg).unwrap_or_default());
+ }
+ }
+ }
+ }
+ }
+ qr
+ }
+ Err(_) => {
+ // Fall back to local config if Gateway call fails
+ let config_path = get_whatsapp_config_path()?;
+ if config_path.exists() {
+ std::fs::read_to_string(&config_path).ok()
+ .and_then(|json| serde_json::from_str::<serde_json::Value>(&json).ok())
+ .and_then(|settings| settings.get("qr_code").and_then(|v| v.as_str()).map(String::from))
+ } else {
+ None
+ }
+ }
};
Ok(WhatsAppAuthStatus {
diff --git a/web-app/src/containers/dialogs/WhatsAppWizardDialog.tsx b/web-app/src/containers/dialogs/WhatsAppWizardDialog.tsx
index 29f08de2e..311a027bc 100644
--- a/web-app/src/containers/dialogs/WhatsAppWizardDialog.tsx
+++ b/web-app/src/containers/dialogs/WhatsAppWizardDialog.tsx
@@ -164,7 +164,7 @@ export function WhatsAppWizardDialog({
} catch {
// Polling error — will retry on next interval
}
- }, 2000)
+ }, 5000)
setPollingInterval(interval)
setIsPolling(true)