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
//! 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 gateway;
pub mod heartbeat;
pub mod history;
pub mod onboard;
pub mod skills;
pub mod status;
pub mod template;
use anyhow::Result;
use clap::{CommandFactory, Parser, Subcommand, ValueEnum};
use tracing_subscriber::EnvFilter;
#[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,
/// 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,
},
/// 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>,
},
/// 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 agent templates
Template {
#[command(subcommand)]
action: TemplateAction,
},
/// Manage skills
Skills {
#[command(subcommand)]
action: SkillsAction,
},
/// 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,
},
}
#[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,
},
}
#[derive(Subcommand)]
pub enum AuthAction {
/// Log in to AI provider
Login,
/// Log out from AI provider
Logout,
/// Show authentication status
Status,
}
#[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(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 (JSON format when RUST_LOG_FORMAT=json)
let env_filter = EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("warn"));
let use_json = std::env::var("RUST_LOG_FORMAT")
.map(|v| v.eq_ignore_ascii_case("json"))
.unwrap_or(false);
if use_json {
tracing_subscriber::fmt()
.json()
.with_env_filter(env_filter)
.with_target(true)
.with_thread_ids(false)
.init();
} else {
tracing_subscriber::fmt().with_env_filter(env_filter).init();
}
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) => {
onboard::cmd_onboard().await?;
}
Some(Commands::Agent {
message,
template,
stream,
}) => {
agent::cmd_agent(message, template, stream).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 }) => {
gateway::cmd_gateway(containerized).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::Template { action }) => {
template::cmd_template(action).await?;
}
Some(Commands::Skills { action }) => {
skills::cmd_skills(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?;
}
}
Ok(())
}
/// Display version information
fn cmd_version() {
println!("zeptoclaw {}", env!("CARGO_PKG_VERSION"));
println!();
println!("Ultra-lightweight personal AI assistant framework");
println!("https://github.com/qhkm/zeptoclaw");
}