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
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
//! CLI module — command parsing and dispatch
//!
//! All CLI logic lives here. `main.rs` calls `cli::run()`.
pub mod agent;
pub mod batch;
pub mod channel;
pub mod common;
pub mod config;
pub mod daemon;
pub mod doctor;
pub mod gateway;
pub mod heartbeat;
pub mod history;
pub mod memory;
pub mod migrate;
pub mod onboard;
pub mod pair;
pub mod secrets;
pub mod skills;
pub mod status;
pub mod template;
pub mod tools;
pub mod update;
pub mod watch;
use anyhow::Result;
use clap::{CommandFactory, Parser, Subcommand, ValueEnum};
#[derive(Parser)]
#[command(name = "zeptoclaw")]
#[command(version)]
#[command(about = "Ultra-lightweight personal AI assistant", long_about = None)]
struct Cli {
#[command(subcommand)]
command: Option<Commands>,
}
#[derive(Subcommand)]
enum Commands {
/// Initialize configuration and workspace
Onboard {
/// Run full 10-step wizard (express mode by default)
#[arg(long)]
full: bool,
},
/// Start interactive agent mode
Agent {
/// Direct message to process (non-interactive mode)
#[arg(short, long)]
message: Option<String>,
/// Apply an agent template (built-in or ~/.zeptoclaw/templates/*.json)
#[arg(long)]
template: Option<String>,
/// Stream the response token-by-token
#[arg(long)]
stream: bool,
/// Show what tools would be called without executing them
#[arg(long)]
dry_run: bool,
/// Agent mode: observer (read-only), assistant (read/write + approval), autonomous (full access)
#[arg(long)]
mode: Option<String>,
},
/// Process prompts from a file
Batch {
/// Input file (.txt, .json, or .jsonl)
#[arg(long)]
input: std::path::PathBuf,
/// Optional output file (prints to stdout if omitted)
#[arg(long)]
output: Option<std::path::PathBuf>,
/// Output format for results
#[arg(long, value_enum, default_value_t = BatchFormat::Text)]
format: BatchFormat,
/// Stop processing after the first failed prompt
#[arg(long)]
stop_on_error: bool,
/// Stream LLM output internally while collecting final result text
#[arg(long)]
stream: bool,
/// Apply an agent template to all prompts
#[arg(long)]
template: Option<String>,
},
/// Start multi-channel gateway
Gateway {
/// Run in container isolation [optional: docker, apple]
#[arg(long, num_args = 0..=1, default_missing_value = "auto", value_name = "BACKEND")]
containerized: Option<String>,
/// Start a tunnel to expose gateway publicly [cloudflare, ngrok, tailscale, auto]
#[arg(long, value_name = "PROVIDER")]
tunnel: Option<String>,
},
/// Run agent in stdin/stdout mode (for containerized execution)
AgentStdin,
/// Trigger or inspect heartbeat tasks
Heartbeat {
/// Show heartbeat file contents
#[arg(short, long, conflicts_with = "edit")]
show: bool,
/// Edit heartbeat file in $EDITOR
#[arg(short, long, conflicts_with = "show")]
edit: bool,
},
/// Manage conversation history
History {
#[command(subcommand)]
action: HistoryAction,
},
/// Manage long-term memory
Memory {
#[command(subcommand)]
action: MemoryAction,
},
/// Manage agent templates
Template {
#[command(subcommand)]
action: TemplateAction,
},
/// Manage skills
Skills {
#[command(subcommand)]
action: SkillsAction,
},
/// Manage and discover tools
Tools {
#[command(subcommand)]
action: ToolsAction,
},
/// Manage authentication
Auth {
#[command(subcommand)]
action: AuthAction,
},
/// Show version information
Version,
/// Show system status
Status,
/// Manage communication channels
Channel {
#[command(subcommand)]
action: ChannelAction,
},
/// Validate configuration file
Config {
#[command(subcommand)]
action: ConfigAction,
},
/// Manage secret encryption
Secrets {
#[command(subcommand)]
action: SecretsAction,
},
/// Watch a URL for changes and notify
Watch {
/// URL to monitor
url: String,
/// Check interval (e.g., "1h", "30m", "15m")
#[arg(long, default_value = "1h")]
interval: String,
/// Channel to notify on changes (telegram, slack, discord). Omit for stdout only.
#[arg(long)]
notify: Option<String>,
},
/// Manage device pairing (bearer token auth)
Pair {
#[command(subcommand)]
action: PairAction,
},
/// Run system diagnostics
Doctor {
/// Include online provider connectivity checks
#[arg(long)]
online: bool,
},
/// Start supervised daemon (auto-restarts gateway on failure)
Daemon,
/// Migrate config and skills from an OpenClaw installation
Migrate {
/// Path to OpenClaw directory (auto-detected if omitted)
#[arg(long)]
from: Option<String>,
/// Accept all defaults without prompting
#[arg(long, short)]
yes: bool,
/// Preview what would be migrated without making changes
#[arg(long)]
dry_run: bool,
},
/// Check for updates or update to latest version
Update {
/// Only check, don't download
#[arg(long)]
check: bool,
/// Install specific version (e.g., "v0.5.2")
#[arg(long)]
version: Option<String>,
/// Force re-download even if already on latest
#[arg(long)]
force: bool,
},
/// Hardware device management (USB discovery, peripherals)
Hardware {
#[command(subcommand)]
action: HardwareAction,
},
}
#[derive(Subcommand)]
pub enum MemoryAction {
/// List all stored memories
List {
/// Filter by category
#[arg(long)]
category: Option<String>,
},
/// Search memories by query
Search {
/// Search query (matches key, value, category, tags)
query: String,
},
/// Set a memory value
Set {
/// Memory key (e.g. "user:name", "preference:language")
key: String,
/// Memory value
value: String,
/// Category for grouping
#[arg(long, default_value = "general")]
category: String,
/// Comma-separated tags
#[arg(long)]
tags: Option<String>,
},
/// Delete a memory by key
Delete {
/// Memory key to delete
key: String,
},
/// Show memory statistics
Stats,
/// Remove expired memories below decay threshold
Cleanup {
/// Decay score threshold (0.0-1.0). Entries below this are removed.
#[arg(long, default_value_t = 0.1)]
threshold: f32,
},
/// Export longterm memory to a JSON snapshot file
Export {
/// Output file path (default: ~/.zeptoclaw/memory/snapshot.json)
#[arg(long)]
output: Option<std::path::PathBuf>,
},
/// Import longterm memory from a JSON snapshot file
Import {
/// Path to snapshot file
path: std::path::PathBuf,
/// Overwrite existing keys (default: skip existing)
#[arg(long)]
overwrite: bool,
},
}
#[derive(Subcommand)]
pub enum SkillsAction {
/// List skills (ready-only by default)
List {
/// Include unavailable skills
#[arg(short, long)]
all: bool,
},
/// Show full skill content
Show {
/// Skill name
name: String,
},
/// Create a new workspace skill template
Create {
/// Skill name
name: String,
},
/// Search for skills on ClawHub and GitHub
Search {
/// Search query
query: String,
/// Source filter: clawhub, github, or all (default)
#[arg(long, default_value = "all")]
source: String,
},
/// Install a skill from GitHub
Install {
/// ClawHub slug (reserved for future use)
slug: Option<String>,
/// Install from GitHub repository (owner/repo format)
#[arg(long)]
github: Option<String>,
},
}
#[derive(Subcommand)]
pub enum ToolsAction {
/// List all available tools with status
List,
/// Show details for a specific tool
Info {
/// Tool name
name: String,
},
}
#[derive(Subcommand)]
pub enum AuthAction {
/// Log in to AI provider via OAuth browser sign-in
Login {
/// Provider to authenticate with (e.g., "anthropic")
provider: Option<String>,
},
/// Log out from AI provider (delete stored OAuth tokens)
Logout {
/// Provider to log out from (e.g., "anthropic")
provider: Option<String>,
},
/// Show authentication status for all providers
Status,
/// Force refresh OAuth tokens
Refresh {
/// Provider to refresh tokens for
provider: String,
},
/// Set up a Claude Code subscription token for API access
SetupToken,
}
#[derive(Subcommand)]
pub enum ConfigAction {
/// Check configuration for errors and warnings
Check,
}
#[derive(Subcommand)]
pub enum ChannelAction {
/// List all channels and their status
List,
/// Interactive setup for a channel
Setup {
/// Channel name (telegram, discord, slack, whatsapp, webhook)
channel_name: String,
},
/// Test channel connectivity
Test {
/// Channel name (telegram, discord, slack, whatsapp, webhook)
channel_name: String,
},
}
#[derive(Subcommand)]
pub enum HistoryAction {
/// List recent CLI conversations
List {
/// Maximum number of conversations to show
#[arg(long, default_value_t = 20)]
limit: usize,
},
/// Show a conversation by session key or title query
Show {
/// Session key (exact) or title substring (case-insensitive)
query: String,
},
/// Remove old CLI conversations
Cleanup {
/// Keep this many most-recent conversations
#[arg(long, default_value_t = 50)]
keep: usize,
},
}
#[derive(Subcommand)]
pub enum TemplateAction {
/// List available templates (built-in + user-defined)
List,
/// Show full template details
Show {
/// Template name
name: String,
},
}
#[derive(Subcommand)]
pub enum SecretsAction {
/// Encrypt all plaintext secrets in config
Encrypt,
/// Decrypt all secrets for editing
Decrypt,
/// Re-encrypt with a new key
Rotate,
}
#[derive(Subcommand)]
pub enum PairAction {
/// Generate a new 6-digit pairing code
New,
/// List all paired devices
List,
/// Revoke a paired device
Revoke {
/// Device name to revoke
device: String,
},
}
#[derive(Subcommand, Debug, Clone)]
pub enum HardwareAction {
/// List discovered USB devices
List,
/// Show info about a specific device
Info {
/// Device name or VID:PID (e.g., "nucleo-f401re" or "0483:374b")
device: String,
},
}
#[derive(Copy, Clone, Debug, Eq, PartialEq, ValueEnum)]
pub enum BatchFormat {
Text,
Jsonl,
}
/// Entry point for the CLI — called from main().
pub async fn run() -> Result<()> {
// Initialize logging from config (format, level, optional file output).
// Load config early so we can respect the logging settings; fall back to
// defaults if the config file is missing or unreadable.
let logging_cfg = zeptoclaw::config::Config::load()
.map(|c| c.logging)
.unwrap_or_default();
zeptoclaw::utils::logging::init_logging(&logging_cfg);
let cli = Cli::parse();
match cli.command {
None => {
let mut cmd = Cli::command();
cmd.print_help()?;
println!();
}
Some(Commands::Version) => {
cmd_version();
}
Some(Commands::Onboard { full }) => {
onboard::cmd_onboard(full).await?;
}
Some(Commands::Agent {
message,
template,
stream,
dry_run,
mode,
}) => {
agent::cmd_agent(message, template, stream, dry_run, mode).await?;
}
Some(Commands::Batch {
input,
output,
format,
stop_on_error,
stream,
template,
}) => {
batch::cmd_batch(input, output, format, stop_on_error, stream, template).await?;
}
Some(Commands::Gateway {
containerized,
tunnel,
}) => {
gateway::cmd_gateway(containerized, tunnel).await?;
}
Some(Commands::AgentStdin) => {
agent::cmd_agent_stdin().await?;
}
Some(Commands::Heartbeat { show, edit }) => {
heartbeat::cmd_heartbeat(show, edit).await?;
}
Some(Commands::History { action }) => {
history::cmd_history(action).await?;
}
Some(Commands::Memory { action }) => {
memory::cmd_memory(action).await?;
}
Some(Commands::Template { action }) => {
template::cmd_template(action).await?;
}
Some(Commands::Skills { action }) => {
skills::cmd_skills(action).await?;
}
Some(Commands::Tools { action }) => {
tools::cmd_tools(action).await?;
}
Some(Commands::Auth { action }) => {
status::cmd_auth(action).await?;
}
Some(Commands::Status) => {
status::cmd_status().await?;
}
Some(Commands::Channel { action }) => {
channel::cmd_channel(action).await?;
}
Some(Commands::Config { action }) => {
config::cmd_config(action).await?;
}
Some(Commands::Secrets { action }) => {
secrets::cmd_secrets(action).await?;
}
Some(Commands::Watch {
url,
interval,
notify,
}) => {
watch::cmd_watch(url, interval, notify).await?;
}
Some(Commands::Pair { action }) => {
pair::cmd_pair(action).await?;
}
Some(Commands::Doctor { online }) => {
doctor::cmd_doctor(online).await?;
}
Some(Commands::Daemon) => {
daemon::cmd_daemon().await?;
}
Some(Commands::Migrate { from, yes, dry_run }) => {
migrate::cmd_migrate(from, yes, dry_run).await?;
}
Some(Commands::Update {
check,
version,
force,
}) => {
update::cmd_update(check, version, force).await?;
}
Some(Commands::Hardware { action }) => {
cmd_hardware(action);
}
}
Ok(())
}
/// Display version information
fn cmd_version() {
println!("zeptoclaw {}", env!("CARGO_PKG_VERSION"));
println!();
println!("Ultra-lightweight personal AI assistant");
println!("https://github.com/qhkm/zeptoclaw");
}
/// Handle hardware subcommands (list, info).
fn cmd_hardware(action: HardwareAction) {
use zeptoclaw::hardware::HardwareManager;
let mgr = HardwareManager::new();
match action {
HardwareAction::List => {
let devices = mgr.discover_devices();
if devices.is_empty() {
println!("No hardware devices found.");
println!();
#[cfg(not(feature = "hardware"))]
{
println!("Hardware discovery requires the 'hardware' feature.");
println!("Build with: cargo build --features hardware");
}
#[cfg(feature = "hardware")]
{
println!(
"Connect a board (e.g., Nucleo-F401RE, Arduino) via USB and try again."
);
}
} else {
println!("Discovered devices:");
println!();
for d in &devices {
let arch = d.architecture.as_deref().unwrap_or("--");
let detail = d.detail.as_deref().unwrap_or("--");
println!(
" {:04x}:{:04x} {:<20} {:<20} {}",
d.vid, d.pid, d.name, arch, detail
);
}
println!();
println!("{} device(s) found.", devices.len());
}
}
HardwareAction::Info { device } => match mgr.device_info(&device) {
Some(info) => {
println!("Device: {}", info.name);
println!(" VID:PID {:04x}:{:04x}", info.vid, info.pid);
if let Some(arch) = &info.architecture {
println!(" Architecture {}", arch);
}
if let Some(detail) = &info.detail {
println!(" Description {}", detail);
}
if let Some(path) = &info.device_path {
println!(" Serial path {}", path);
}
}
None => {
println!("Device '{}' not found.", device);
println!();
println!("Try: zeptoclaw hardware list");
println!("Or use a VID:PID format (e.g., 0483:374b)");
}
},
}
}