zeptoclaw 0.7.4

Ultra-lightweight personal AI assistant
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
//! WhatsApp Web native channel via wa-rs.
//!
//! Pairs via QR code like WhatsApp Desktop and persists session state to a
//! local SQLite database.

use std::path::PathBuf;
use std::str::FromStr;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;

use async_trait::async_trait;
use tokio::sync::Mutex;
use tokio::task::JoinHandle;
use tracing::{error, info, warn};
use wa_rs::bot::Bot;
use wa_rs::proto_helpers::MessageExt;
use wa_rs::store::SqliteStore;
use wa_rs::types::events::Event;
use wa_rs::wa_rs_proto::whatsapp as wa;
use wa_rs::{Client, Jid};
use wa_rs_tokio_transport::TokioWebSocketTransportFactory;
use wa_rs_ureq_http::UreqHttpClient;

use qrcode::QrCode;

use crate::bus::{InboundMessage, MessageBus, OutboundMessage};
use crate::channels::types::{BaseChannelConfig, Channel};
use crate::config::WhatsAppWebConfig;
use crate::error::{Result, ZeptoError};

/// Render a QR code string as unicode block characters in the terminal.
///
/// Uses 2x1 half-block characters for compact display:
/// - `█` (U+2588) = both pixels dark
/// - `▀` (U+2580) = top dark, bottom light
/// - `▄` (U+2584) = top light, bottom dark
/// - ` ` (space)  = both pixels light
fn render_qr_terminal(data: &str) -> Option<String> {
    let code = QrCode::new(data.as_bytes()).ok()?;
    let width = code.width();
    let colors: Vec<bool> = code
        .into_colors()
        .into_iter()
        .map(|c| c == qrcode::Color::Dark)
        .collect();

    // Add 1-module quiet zone on each side
    let padded_width = width + 2;
    let padded_height = width + 2;

    let pixel = |row: usize, col: usize| -> bool {
        if row == 0 || row > width || col == 0 || col > width {
            false // quiet zone
        } else {
            colors[(row - 1) * width + (col - 1)]
        }
    };

    let mut output = String::new();
    let mut y = 0;
    while y < padded_height {
        for x in 0..padded_width {
            let top = pixel(y, x);
            let bottom = if y + 1 < padded_height {
                pixel(y + 1, x)
            } else {
                false
            };
            output.push(match (top, bottom) {
                (true, true) => '\u{2588}',
                (true, false) => '\u{2580}',
                (false, true) => '\u{2584}',
                (false, false) => ' ',
            });
        }
        output.push('\n');
        y += 2;
    }
    Some(output)
}

fn normalize_phone(phone: &str) -> String {
    phone
        .trim()
        .split('@')
        .next()
        .unwrap_or_default()
        .chars()
        .filter(|c| c.is_ascii_digit())
        .collect()
}

fn expand_auth_dir(path: &str) -> String {
    if let Some(rest) = path.strip_prefix("~/") {
        if let Some(home) = dirs::home_dir() {
            return home.join(rest).to_string_lossy().to_string();
        }
    }
    path.to_string()
}

fn sqlite_store_path(path: &str) -> PathBuf {
    let expanded = PathBuf::from(expand_auth_dir(path));
    if expanded.extension().is_some() {
        expanded
    } else {
        expanded.join("session.sqlite3")
    }
}

/// Resolve a sender to a phone number for allowlist matching.
///
/// WhatsApp Web may deliver messages with a LID-based sender JID
/// (e.g. `78971563720736@lid`) instead of a phone number JID
/// (`60123456789@s.whatsapp.net`). The `sender_alt` field on
/// `MessageSource` contains the phone number JID when the primary
/// sender is a LID. This function checks `sender_alt` first,
/// falling back to the primary sender JID.
fn resolve_sender_phone(sender: &Jid, sender_alt: &Option<Jid>) -> String {
    // Prefer sender_alt (contains phone JID when sender is LID)
    if let Some(ref alt) = sender_alt {
        if alt.is_pn() {
            return normalize_phone(&alt.to_string());
        }
    }
    // If sender itself is a phone JID, use it directly
    if sender.is_pn() {
        return normalize_phone(&sender.to_string());
    }
    // Fallback: use sender as-is (LID user part)
    normalize_phone(&sender.to_string())
}

fn parse_chat_jid(chat_id: &str) -> Result<Jid> {
    let jid = if chat_id.contains('@') {
        chat_id.trim().to_string()
    } else {
        format!("{}@s.whatsapp.net", normalize_phone(chat_id))
    };

    Jid::from_str(&jid)
        .map_err(|e| ZeptoError::Channel(format!("WhatsApp Web: invalid recipient '{jid}': {e}")))
}

fn build_outbound_message(msg: &OutboundMessage) -> wa::Message {
    if let Some(reply_to) = msg.reply_to.as_deref() {
        wa::Message {
            extended_text_message: Some(Box::new(wa::message::ExtendedTextMessage {
                text: Some(msg.content.clone()),
                context_info: Some(Box::new(wa::ContextInfo {
                    stanza_id: Some(reply_to.to_string()),
                    ..Default::default()
                })),
                ..Default::default()
            })),
            ..Default::default()
        }
    } else {
        wa::Message {
            conversation: Some(msg.content.clone()),
            ..Default::default()
        }
    }
}

struct RuntimeState {
    client: Arc<Client>,
    task: JoinHandle<()>,
}

/// WhatsApp Web channel using the native wa-rs protocol.
pub struct WhatsAppWebChannel {
    config: WhatsAppWebConfig,
    base_config: BaseChannelConfig,
    bus: Arc<MessageBus>,
    running: Arc<AtomicBool>,
    runtime: Arc<Mutex<Option<RuntimeState>>>,
}

impl WhatsAppWebChannel {
    pub fn new(config: WhatsAppWebConfig, bus: Arc<MessageBus>) -> Self {
        let normalized_allowlist: Vec<String> = config
            .allow_from
            .iter()
            .map(|p| normalize_phone(p))
            .collect();

        let base_config = BaseChannelConfig {
            name: "whatsapp_web".to_string(),
            allowlist: normalized_allowlist,
            deny_by_default: config.deny_by_default,
        };

        Self {
            config,
            base_config,
            bus,
            running: Arc::new(AtomicBool::new(false)),
            runtime: Arc::new(Mutex::new(None)),
        }
    }
}

#[async_trait]
impl Channel for WhatsAppWebChannel {
    fn name(&self) -> &str {
        &self.base_config.name
    }

    async fn start(&mut self) -> Result<()> {
        {
            let runtime = self.runtime.lock().await;
            if runtime.is_some() && self.running.load(Ordering::SeqCst) {
                info!("WhatsApp Web channel already running");
                return Ok(());
            }
        }

        let store_path = sqlite_store_path(&self.config.auth_dir);
        if let Some(parent) = store_path.parent() {
            tokio::fs::create_dir_all(parent).await.map_err(|e| {
                ZeptoError::Channel(format!(
                    "WhatsApp Web: failed to create auth directory {}: {}",
                    parent.display(),
                    e
                ))
            })?;
        }

        let store = Arc::new(
            SqliteStore::new(store_path.to_str().ok_or_else(|| {
                ZeptoError::Channel(format!(
                    "WhatsApp Web: invalid auth path {}",
                    store_path.display()
                ))
            })?)
            .await
            .map_err(|e| {
                ZeptoError::Channel(format!(
                    "WhatsApp Web: failed to initialize SQLite store {}: {}",
                    store_path.display(),
                    e
                ))
            })?,
        );

        let bus = self.bus.clone();
        let base_config = self.base_config.clone();
        let running = self.running.clone();

        let mut bot = Bot::builder()
            .with_backend(store)
            .with_transport_factory(TokioWebSocketTransportFactory::new())
            .with_http_client(UreqHttpClient::new())
            .on_event(move |event, client| {
                let bus = bus.clone();
                let base_config = base_config.clone();
                let running = running.clone();
                async move {
                    match event {
                        Event::Connected(_) => {
                            running.store(true, Ordering::SeqCst);
                            info!("WhatsApp Web connected");
                        }
                        Event::PairingQrCode { code, timeout } => {
                            eprintln!();
                            eprintln!("╔══════════════════════════════════════════╗");
                            eprintln!("║   Scan this QR code with WhatsApp       ║");
                            eprintln!("║   Phone → Settings → Linked Devices     ║");
                            eprintln!(
                                "║   Valid for {}s                        ║",
                                timeout.as_secs()
                            );
                            eprintln!("╚══════════════════════════════════════════╝");
                            eprintln!();
                            match render_qr_terminal(&code) {
                                Some(qr) => eprint!("{}", qr),
                                None => {
                                    eprintln!("Failed to render QR code. Please try again.");
                                }
                            }
                            eprintln!();
                        }
                        Event::PairingCode { code, timeout } => {
                            eprintln!(
                                "WhatsApp Web pair code (valid for {}s): {}",
                                timeout.as_secs(),
                                code
                            );
                        }
                        Event::LoggedOut(reason) => {
                            running.store(false, Ordering::SeqCst);
                            warn!("WhatsApp Web logged out: {:?}", reason.reason);
                        }
                        Event::Disconnected(_) => {
                            // Don't set running = false here — wa-rs will
                            // reconnect automatically. Only mark dead on
                            // LoggedOut or when the bot task actually exits.
                            warn!("WhatsApp Web disconnected (will reconnect)");
                        }
                        Event::Message(message, info) => {
                            if info.source.is_from_me {
                                return;
                            }

                            let sender_jid = info.source.sender.to_string();
                            let sender_id =
                                resolve_sender_phone(&info.source.sender, &info.source.sender_alt);
                            if !base_config.is_allowed(&sender_id) {
                                info!(
                                    "WhatsApp Web: sender {} (jid: {}) not in allowlist, ignoring",
                                    sender_id, sender_jid
                                );
                                return;
                            }

                            let content = message
                                .text_content()
                                .or_else(|| message.get_caption())
                                .map(str::trim)
                                .unwrap_or_default()
                                .to_string();

                            if content.is_empty() {
                                return;
                            }

                            let chat_id = info.source.chat.to_string();
                            let mut inbound =
                                InboundMessage::new("whatsapp_web", &sender_id, &chat_id, &content)
                                    .with_metadata("whatsapp_message_id", &info.id)
                                    .with_metadata("sender_jid", &sender_jid)
                                    .with_metadata("chat_jid", &chat_id);

                            if !info.push_name.is_empty() {
                                inbound = inbound.with_metadata("sender_name", &info.push_name);
                            }
                            if info.source.is_group {
                                inbound = inbound.with_metadata("is_group", "true");
                            }

                            if let Err(e) = bus.publish_inbound(inbound).await {
                                error!("WhatsApp Web: failed to publish inbound message: {}", e);
                            }
                        }
                        Event::PairError(err) => {
                            warn!("WhatsApp Web pairing failed: {}", err.error);
                        }
                        _ => {
                            let _ = client;
                        }
                    }
                }
            })
            .build()
            .await
            .map_err(|e| ZeptoError::Channel(format!("WhatsApp Web: bot build failed: {e}")))?;

        let client = bot.client();
        let run_handle = bot
            .run()
            .await
            .map_err(|e| ZeptoError::Channel(format!("WhatsApp Web: bot run failed: {e}")))?;

        self.running.store(true, Ordering::SeqCst);
        let running = self.running.clone();
        let task = tokio::spawn(async move {
            if let Err(e) = run_handle.await {
                error!("WhatsApp Web task failed: {}", e);
            }
            running.store(false, Ordering::SeqCst);
        });

        let mut runtime = self.runtime.lock().await;
        *runtime = Some(RuntimeState { client, task });

        info!(
            auth_db = %store_path.display(),
            "WhatsApp Web channel started"
        );
        Ok(())
    }

    async fn stop(&mut self) -> Result<()> {
        let state = self.runtime.lock().await.take();
        let Some(state) = state else {
            self.running.store(false, Ordering::SeqCst);
            return Ok(());
        };

        state.client.disconnect().await;

        match tokio::time::timeout(std::time::Duration::from_secs(10), state.task).await {
            Ok(Ok(())) => {}
            Ok(Err(e)) => error!("WhatsApp Web task join failed: {}", e),
            Err(_) => warn!("WhatsApp Web task did not stop within 10 seconds"),
        }

        self.running.store(false, Ordering::SeqCst);
        info!("WhatsApp Web channel stopped");
        Ok(())
    }

    async fn send(&self, msg: OutboundMessage) -> Result<()> {
        if msg.content.trim().is_empty() {
            return Err(ZeptoError::Channel(
                "WhatsApp Web: outbound content cannot be empty".to_string(),
            ));
        }

        let client = {
            let runtime = self.runtime.lock().await;
            runtime
                .as_ref()
                .map(|state| state.client.clone())
                .ok_or_else(|| {
                    ZeptoError::Channel("WhatsApp Web: channel not started".to_string())
                })?
        };

        let jid = parse_chat_jid(&msg.chat_id)?;
        let wa_message = build_outbound_message(&msg);
        client
            .send_message(jid, wa_message)
            .await
            .map_err(|e| ZeptoError::Channel(format!("WhatsApp Web: send failed: {e}")))?;

        Ok(())
    }

    fn is_running(&self) -> bool {
        self.running.load(Ordering::SeqCst)
    }

    fn is_allowed(&self, user_id: &str) -> bool {
        self.base_config.is_allowed(&normalize_phone(user_id))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::bus::MessageBus;

    fn make_channel(config: WhatsAppWebConfig) -> WhatsAppWebChannel {
        WhatsAppWebChannel::new(config, Arc::new(MessageBus::new()))
    }

    #[test]
    fn test_default_config() {
        let cfg = WhatsAppWebConfig::default();
        assert!(!cfg.enabled);
        assert_eq!(cfg.auth_dir, "~/.zeptoclaw/state/whatsapp_web");
        assert!(cfg.allow_from.is_empty());
        assert!(!cfg.deny_by_default);
    }

    #[test]
    fn test_normalize_phone_with_plus() {
        assert_eq!(normalize_phone("+60123456789"), "60123456789");
    }

    #[test]
    fn test_normalize_phone_jid() {
        assert_eq!(normalize_phone("60123456789@s.whatsapp.net"), "60123456789");
    }

    #[test]
    fn test_expand_auth_dir_tilde() {
        let expanded = expand_auth_dir("~/.zeptoclaw/state/whatsapp_web");
        if dirs::home_dir().is_some() {
            assert!(!expanded.starts_with('~'));
        }
    }

    #[test]
    fn test_sqlite_store_path_from_directory() {
        let path = sqlite_store_path("/tmp/wa-state");
        assert_eq!(path, std::path::Path::new("/tmp/wa-state/session.sqlite3"));
    }

    #[test]
    fn test_sqlite_store_path_from_file() {
        let path = sqlite_store_path("/tmp/wa-state.sqlite");
        assert_eq!(path, std::path::Path::new("/tmp/wa-state.sqlite"));
    }

    #[test]
    fn test_is_allowed_normalized_match() {
        let ch = make_channel(WhatsAppWebConfig {
            allow_from: vec!["+60123456789".to_string()],
            ..Default::default()
        });
        assert!(ch.is_allowed("60123456789@s.whatsapp.net"));
    }

    #[test]
    fn test_is_allowed_deny_by_default() {
        let ch = make_channel(WhatsAppWebConfig {
            allow_from: vec![],
            deny_by_default: true,
            ..Default::default()
        });
        assert!(!ch.is_allowed("60999999999"));
    }

    #[test]
    fn test_parse_chat_jid_from_phone() {
        let jid = parse_chat_jid("+60123456789").unwrap();
        assert_eq!(jid.to_string(), "60123456789@s.whatsapp.net");
    }

    #[test]
    fn test_parse_chat_jid_from_jid() {
        let jid = parse_chat_jid("60123456789@s.whatsapp.net").unwrap();
        assert_eq!(jid.to_string(), "60123456789@s.whatsapp.net");
    }

    #[test]
    fn test_build_outbound_message_plain() {
        let message = build_outbound_message(&OutboundMessage::new(
            "whatsapp_web",
            "60123456789",
            "Hello",
        ));
        assert_eq!(message.conversation.as_deref(), Some("Hello"));
    }

    #[test]
    fn test_build_outbound_message_reply() {
        let message = build_outbound_message(
            &OutboundMessage::new("whatsapp_web", "60123456789", "Hello").with_reply("abc123"),
        );
        let reply = message.extended_text_message.expect("reply message");
        assert_eq!(reply.text.as_deref(), Some("Hello"));
        assert_eq!(
            reply.context_info.and_then(|ctx| ctx.stanza_id).as_deref(),
            Some("abc123")
        );
    }

    #[test]
    fn test_channel_name() {
        let ch = make_channel(WhatsAppWebConfig::default());
        assert_eq!(ch.name(), "whatsapp_web");
    }

    #[test]
    fn test_channel_not_running_initially() {
        let ch = make_channel(WhatsAppWebConfig::default());
        assert!(!ch.is_running());
    }

    #[tokio::test]
    async fn test_send_errors_when_not_started() {
        let ch = make_channel(WhatsAppWebConfig::default());
        let msg = OutboundMessage::new("whatsapp_web", "60123456789", "Hello");
        let result = ch.send(msg).await;
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("not started"));
    }

    #[test]
    fn test_resolve_sender_phone_from_pn_jid() {
        let sender = Jid::from_str("60123456789@s.whatsapp.net").unwrap();
        let result = resolve_sender_phone(&sender, &None);
        assert_eq!(result, "60123456789");
    }

    #[test]
    fn test_resolve_sender_phone_from_lid_with_alt() {
        let sender = Jid::from_str("78971563720736@lid").unwrap();
        let alt = Some(Jid::from_str("60123456789@s.whatsapp.net").unwrap());
        let result = resolve_sender_phone(&sender, &alt);
        assert_eq!(result, "60123456789");
    }

    #[test]
    fn test_resolve_sender_phone_from_lid_without_alt() {
        let sender = Jid::from_str("78971563720736@lid").unwrap();
        let result = resolve_sender_phone(&sender, &None);
        assert_eq!(result, "78971563720736");
    }

    #[test]
    fn test_resolve_sender_phone_lid_alt_is_also_lid() {
        let sender = Jid::from_str("111111@lid").unwrap();
        let alt = Some(Jid::from_str("222222@lid").unwrap());
        let result = resolve_sender_phone(&sender, &alt);
        // Neither is a PN, falls back to sender user part
        assert_eq!(result, "111111");
    }
}