zeptoclaw 0.9.0

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
//! Channel factory/registration helpers.

use std::path::PathBuf;
use std::sync::Arc;

use tracing::{info, warn};

use crate::bus::MessageBus;
use crate::config::{Config, MemoryBackend};
use crate::providers::{configured_provider_models, configured_provider_names};

use super::acp_http::AcpHttpChannel;
use super::email_channel::EmailChannel;
use super::lark::LarkChannel;
use super::plugin::{default_channel_plugins_dir, discover_channel_plugins, ChannelPluginAdapter};
use super::webhook::{WebhookChannel, WebhookChannelConfig};
use super::WhatsAppCloudChannel;
use super::{BaseChannelConfig, ChannelManager, DiscordChannel, SlackChannel, TelegramChannel};

/// Register all configured channels that currently have implementations.
///
/// Returns the number of registered channels.
pub async fn register_configured_channels(
    manager: &ChannelManager,
    bus: Arc<MessageBus>,
    config: &Config,
) -> usize {
    // Telegram
    if let Some(ref telegram_config) = config.channels.telegram {
        if telegram_config.enabled {
            if telegram_config.token.is_empty() {
                warn!("Telegram channel enabled but token is empty");
            } else {
                manager
                    .register(Box::new(TelegramChannel::new(
                        telegram_config.clone(),
                        bus.clone(),
                        config.agents.defaults.model.clone(),
                        configured_provider_names(config)
                            .into_iter()
                            .map(|name| name.to_string())
                            .collect(),
                        configured_provider_models(config),
                        !matches!(config.memory.backend, MemoryBackend::Disabled),
                    )))
                    .await;
                info!("Registered Telegram channel");
            }
        }
    }

    // Slack
    if let Some(ref slack_config) = config.channels.slack {
        if slack_config.enabled {
            if slack_config.bot_token.is_empty() {
                warn!("Slack channel enabled but bot token is empty");
            } else {
                manager
                    .register(Box::new(SlackChannel::new(
                        slack_config.clone(),
                        bus.clone(),
                    )))
                    .await;
                info!("Registered Slack channel");
            }
        }
    }

    // Discord
    if let Some(ref discord_config) = config.channels.discord {
        if discord_config.enabled {
            if discord_config.token.is_empty() {
                warn!("Discord channel enabled but token is empty");
            } else {
                manager
                    .register(Box::new(DiscordChannel::new(
                        discord_config.clone(),
                        bus.clone(),
                    )))
                    .await;
                info!("Registered Discord channel");
            }
        }
    }
    // Webhook
    if let Some(ref webhook_config) = config.channels.webhook {
        if webhook_config.enabled {
            let runtime_config = WebhookChannelConfig {
                bind_address: webhook_config.bind_address.clone(),
                port: webhook_config.port,
                path: webhook_config.path.clone(),
                auth_token: webhook_config.auth_token.clone(),
                signature_secret: webhook_config.signature_secret.clone(),
                signature_header: webhook_config.signature_header.clone(),
                sender_id: webhook_config.sender_id.clone(),
                chat_id: webhook_config.chat_id.clone(),
                trust_payload_identity: webhook_config.trust_payload_identity,
            };
            let base_config = BaseChannelConfig {
                name: "webhook".to_string(),
                allowlist: webhook_config.allow_from.clone(),
                deny_by_default: webhook_config.deny_by_default,
            };
            manager
                .register(Box::new(WebhookChannel::new(
                    runtime_config,
                    base_config,
                    bus.clone(),
                )))
                .await;
            info!(
                "Registered Webhook channel on {}:{}",
                webhook_config.bind_address, webhook_config.port
            );
        }
    }

    // WhatsApp Web (native via wa-rs) — requires whatsapp-web feature
    #[cfg(feature = "whatsapp-web")]
    if let Some(ref wa_web_config) = config.channels.whatsapp_web {
        if wa_web_config.enabled {
            manager
                .register(Box::new(super::WhatsAppWebChannel::new(
                    wa_web_config.clone(),
                    bus.clone(),
                )))
                .await;
            info!("Registered WhatsApp Web channel (native)");
        }
    }

    #[cfg(not(feature = "whatsapp-web"))]
    if let Some(ref wa_web_config) = config.channels.whatsapp_web {
        if wa_web_config.enabled {
            warn!(
                "WhatsApp Web channel is enabled in config but this build was compiled without the whatsapp-web feature"
            );
        }
    }

    // WhatsApp Cloud API (official)
    if let Some(ref wac_config) = config.channels.whatsapp_cloud {
        if wac_config.enabled {
            if wac_config.phone_number_id.is_empty() || wac_config.access_token.is_empty() {
                warn!(
                    "WhatsApp Cloud channel enabled but phone_number_id or access_token is empty"
                );
            } else {
                let transcriber = crate::transcription::TranscriberService::from_config(config);
                manager
                    .register(Box::new(WhatsAppCloudChannel::new(
                        wac_config.clone(),
                        bus.clone(),
                        transcriber,
                    )))
                    .await;
                info!(
                    "Registered WhatsApp Cloud API channel on {}:{}",
                    wac_config.bind_address, wac_config.port
                );
            }
        }
    }
    // Lark / Feishu (WS long-connection)
    if let Some(ref lark_cfg) = config.channels.lark {
        if lark_cfg.enabled {
            if lark_cfg.app_id.is_empty() || lark_cfg.app_secret.is_empty() {
                warn!("Lark channel enabled but app_id or app_secret is empty");
            } else {
                manager
                    .register(Box::new(LarkChannel::new(lark_cfg.clone(), bus.clone())))
                    .await;
                let region = if lark_cfg.feishu { "Feishu" } else { "Lark" };
                info!("Registered {} channel (WS long-connection)", region);
            }
        }
    }
    if config
        .channels
        .feishu
        .as_ref()
        .map(|c| c.enabled)
        .unwrap_or(false)
    {
        warn!("Feishu channel is enabled but not implemented");
    }
    if config
        .channels
        .maixcam
        .as_ref()
        .map(|c| c.enabled)
        .unwrap_or(false)
    {
        warn!("MaixCam channel is enabled but not implemented");
    }
    if config
        .channels
        .qq
        .as_ref()
        .map(|c| c.enabled)
        .unwrap_or(false)
    {
        warn!("QQ channel is enabled but not implemented");
    }
    if config
        .channels
        .dingtalk
        .as_ref()
        .map(|c| c.enabled)
        .unwrap_or(false)
    {
        warn!("DingTalk channel is enabled but not implemented");
    }

    // Email (IMAP IDLE + SMTP) — requires channel-email feature
    if let Some(ref email_cfg) = config.channels.email {
        if email_cfg.enabled && !email_cfg.username.is_empty() {
            manager
                .register(Box::new(EmailChannel::new(email_cfg.clone(), bus.clone())))
                .await;
            info!(
                "Registered Email channel (IMAP IDLE on {})",
                email_cfg.imap_host
            );
        } else if !email_cfg.enabled {
            // Channel is present in config but not enabled — skip silently.
        } else {
            warn!("Email channel configured but username is empty");
        }
    }

    // Serial (UART) — requires hardware feature
    #[cfg(feature = "hardware")]
    if let Some(ref serial_config) = config.channels.serial {
        if serial_config.enabled {
            if serial_config.port.is_empty() {
                warn!("Serial channel enabled but port is empty");
            } else {
                manager
                    .register(Box::new(super::serial::SerialChannel::new(
                        serial_config.clone(),
                        bus.clone(),
                    )))
                    .await;
                info!("Registered Serial channel on {}", serial_config.port);
            }
        }
    }

    // MQTT — requires mqtt feature
    #[cfg(feature = "mqtt")]
    if let Some(ref mqtt_config) = config.channels.mqtt {
        if mqtt_config.enabled {
            if mqtt_config.broker_url.is_empty() {
                warn!("MQTT channel enabled but broker_url is empty");
            } else {
                manager
                    .register(Box::new(super::mqtt::MqttChannel::new(
                        mqtt_config.clone(),
                        bus.clone(),
                    )))
                    .await;
                // Redact credentials from broker URL before logging.
                let broker_display = mqtt_config
                    .broker_url
                    .rsplit_once('@')
                    .map(|(_, host)| host)
                    .unwrap_or(&mqtt_config.broker_url);
                info!("Registered MQTT channel (broker: {})", broker_display);
            }
        }
    }

    // ACP (Agent Client Protocol) — HTTP transport only in gateway mode.
    // The ACP stdio transport is exclusively for the `zeptoclaw acp` subcommand
    // (where the process is spawned as a subprocess by an ACP client). Registering
    // it here would consume the gateway process's own stdin, which is never a valid
    // ACP client connection. Use `channels.acp.http` to expose ACP in gateway mode.
    if let Some(ref acp_config) = config.channels.acp {
        if acp_config.enabled {
            warn!(
                "channels.acp.enabled has no effect in gateway mode; \
                 ACP stdio is only used by `zeptoclaw acp`. \
                 To expose ACP in gateway mode, set channels.acp.http.enabled = true."
            );
        }
        // HTTP transport — registered as a separate channel ("acp_http") so that
        // sessions are independent and bus routing is unambiguous.
        if let Some(ref http_cfg) = acp_config.http {
            if http_cfg.enabled {
                let http_base = BaseChannelConfig {
                    name: "acp_http".to_string(),
                    allowlist: acp_config.allow_from.clone(),
                    deny_by_default: acp_config.deny_by_default,
                };
                manager
                    .register(Box::new(AcpHttpChannel::new(
                        acp_config.clone(),
                        http_cfg.clone(),
                        http_base,
                        bus.clone(),
                    )))
                    .await;
                info!(
                    "Registered ACP channel (HTTP on {}:{})",
                    http_cfg.bind, http_cfg.port
                );
            }
        }
    }

    // Channel plugins
    let plugin_dir: Option<PathBuf> = config
        .channels
        .channel_plugins_dir
        .as_ref()
        .map(PathBuf::from)
        .or_else(default_channel_plugins_dir);

    if let Some(ref dir) = plugin_dir {
        let discovered = discover_channel_plugins(dir);
        for (manifest, plugin_path) in discovered {
            let name = manifest.name.clone();
            let base_config = BaseChannelConfig::new(&name);
            let adapter = ChannelPluginAdapter::new(manifest, plugin_path, base_config);
            manager.register(Box::new(adapter)).await;
            info!("Registered channel plugin: {}", name);
        }
    }

    manager.channel_count().await
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::bus::MessageBus;
    use crate::config::{
        AcpChannelConfig, Config, SlackConfig, TelegramConfig, WhatsAppCloudConfig,
    };

    #[tokio::test]
    async fn test_register_configured_channels_registers_telegram() {
        let bus = Arc::new(MessageBus::new());
        let mut config = Config::default();
        config.channels.telegram = Some(TelegramConfig {
            enabled: true,
            token: "test-token".to_string(),
            allow_from: Vec::new(),
            ..Default::default()
        });

        let manager = ChannelManager::new(bus.clone(), config.clone());
        let count = register_configured_channels(&manager, bus, &config).await;

        assert_eq!(count, 1);
        assert!(manager.has_channel("telegram").await);
    }

    #[tokio::test]
    async fn test_register_configured_channels_registers_slack() {
        let bus = Arc::new(MessageBus::new());
        let mut config = Config::default();
        config.channels.slack = Some(SlackConfig {
            enabled: true,
            bot_token: "xoxb-test-token".to_string(),
            app_token: String::new(),
            allow_from: Vec::new(),
            ..Default::default()
        });

        let manager = ChannelManager::new(bus.clone(), config.clone());
        let count = register_configured_channels(&manager, bus, &config).await;

        assert_eq!(count, 1);
        assert!(manager.has_channel("slack").await);
    }

    #[tokio::test]
    async fn test_register_configured_channels_registers_whatsapp_cloud() {
        let bus = Arc::new(MessageBus::new());
        let mut config = Config::default();
        config.channels.whatsapp_cloud = Some(WhatsAppCloudConfig {
            enabled: true,
            phone_number_id: "123456".to_string(),
            access_token: "test-token".to_string(),
            webhook_verify_token: "verify".to_string(),
            port: 0,
            ..Default::default()
        });

        let manager = ChannelManager::new(bus.clone(), config.clone());
        let count = register_configured_channels(&manager, bus, &config).await;

        assert_eq!(count, 1);
        assert!(manager.has_channel("whatsapp_cloud").await);
    }

    #[tokio::test]
    async fn test_register_configured_channels_acp_enabled_alone_registers_nothing() {
        // channels.acp.enabled is a no-op in gateway mode (stdio is only for
        // `zeptoclaw acp`). Setting it without an HTTP config must not register
        // any channel.
        let bus = Arc::new(MessageBus::new());
        let mut config = Config::default();
        config.channels.acp = Some(AcpChannelConfig {
            enabled: true,
            allow_from: Vec::new(),
            deny_by_default: false,
            http: None,
            session_ttl_secs: None,
        });

        let manager = ChannelManager::new(bus.clone(), config.clone());
        let count = register_configured_channels(&manager, bus, &config).await;

        assert_eq!(count, 0);
        assert!(!manager.has_channel("acp").await);
    }

    #[tokio::test]
    async fn test_register_configured_channels_registers_acp_http() {
        use crate::config::AcpHttpConfig;
        let bus = Arc::new(MessageBus::new());
        let mut config = Config::default();
        // Use port 0 so the OS assigns an ephemeral port; the channel is
        // registered but start() is not called in this test.
        // Note: channels.acp.enabled is not required for HTTP registration.
        config.channels.acp = Some(AcpChannelConfig {
            http: Some(AcpHttpConfig {
                enabled: true,
                port: 0,
                ..AcpHttpConfig::default()
            }),
            ..AcpChannelConfig::default()
        });

        let manager = ChannelManager::new(bus.clone(), config.clone());
        let count = register_configured_channels(&manager, bus, &config).await;

        assert_eq!(
            count, 1,
            "only acp_http must be registered; stdio is never registered by gateway"
        );
        assert!(!manager.has_channel("acp").await);
        assert!(manager.has_channel("acp_http").await);
    }
}