rsclaw 2026.5.1

AI Agent Engine Compatible with OpenClaw
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
use anyhow::Result;
use crossterm::{
    event::{self, Event, KeyCode, KeyEventKind},
    execute,
    terminal::{EnterAlternateScreen, LeaveAlternateScreen, disable_raw_mode, enable_raw_mode},
};
use ratatui::{
    Terminal,
    backend::CrosstermBackend,
    layout::{Constraint, Direction, Layout},
    style::{Color, Modifier, Style},
    text::{Line, Span},
    widgets::{Block, Borders, List, ListItem, Paragraph},
};

use super::gateway::gateway_pid_file;
use super::style::{banner, dim, green, kv, red};
use crate::{
    cli::{HealthArgs, LogsArgs, StatusArgs, TuiArgs},
    config,
};

// ---------------------------------------------------------------------------
// cmd_logs / cmd_status / cmd_health
// ---------------------------------------------------------------------------

pub async fn cmd_logs(args: LogsArgs) -> Result<()> {
    let log_file = config::loader::log_file();
    if !log_file.exists() {
        println!("no gateway.log found at {}", log_file.display());
        return Ok(());
    }
    if args.follow {
        // Stream new lines from the log file.
        use tokio::io::{AsyncBufReadExt as _, BufReader};
        let file = tokio::fs::File::open(&log_file).await?;
        let mut reader = BufReader::new(file).lines();
        // Seek to end by consuming existing lines first.
        while reader.next_line().await?.is_some() {}
        loop {
            match reader.next_line().await? {
                Some(line) => println!("{line}"),
                None => tokio::time::sleep(std::time::Duration::from_millis(200)).await,
            }
        }
    } else {
        // Print last N lines (default 50, overridden by --limit).
        let content = std::fs::read_to_string(&log_file)?;
        let lines: Vec<&str> = content.lines().collect();
        let limit = args.limit.unwrap_or(50);
        let start = lines.len().saturating_sub(limit);
        for line in &lines[start..] {
            if args.json {
                println!("{}", serde_json::json!({"line": line}));
            } else {
                println!("{line}");
            }
        }
        Ok(())
    }
}

pub async fn cmd_status(args: StatusArgs) -> Result<()> {
    let version = option_env!("RSCLAW_BUILD_VERSION").unwrap_or("dev");
    if args.json {
        let mut info = serde_json::json!({
            "version": version,
        });
        match config::load() {
            Ok(cfg) => {
                info["config"] = serde_json::json!("ok");
                info["agents"] = serde_json::json!(cfg.agents.list.len());
            }
            Err(e) => {
                info["config"] = serde_json::json!(format!("error: {e:#}"));
            }
        }
        println!("{}", serde_json::to_string_pretty(&info)?);
    } else {
        banner(&format!("rsclaw v{version}"));

        // Config path
        let config_path = config::loader::detect_config_path()
            .map(|p| p.display().to_string())
            .unwrap_or_else(|| dim("not found").to_string());
        kv("Config:", &config_path);

        // Gateway status
        let gw_status = gateway_status_str();
        let running = gw_status.starts_with("running");
        let status_display = if running { green(&gw_status) } else { red(&gw_status) };
        kv("Gateway:", &status_display);

        // Suppress tracing output during config load for status display
        match config::load_quiet() {
            Ok(cfg) => {
                // Providers
                let provider_count = cfg
                    .model
                    .models
                    .as_ref()
                    .map(|m| m.providers.len())
                    .unwrap_or(0);
                if provider_count > 0 {
                    let mut names: Vec<&str> = cfg
                        .model
                        .models
                        .as_ref()
                        .map(|m| m.providers.keys().map(|k| k.as_str()).collect())
                        .unwrap_or_default();
                    names.sort();
                    kv(
                        "Providers:",
                        &format!("{} ({})", names.join(", "), provider_count),
                    );
                } else {
                    kv("Providers:", &dim("none configured"));
                }

                // Channels -- enumerate active ones from the per-platform Option fields
                let ch = &cfg.channel.channels;
                let mut ch_names: Vec<&str> = Vec::new();
                if ch.telegram.is_some() { ch_names.push("telegram"); }
                if ch.discord.is_some() { ch_names.push("discord"); }
                if ch.slack.is_some() { ch_names.push("slack"); }
                if ch.whatsapp.is_some() { ch_names.push("whatsapp"); }
                if ch.signal.is_some() { ch_names.push("signal"); }
                if ch.imessage.is_some() { ch_names.push("imessage"); }
                if ch.mattermost.is_some() { ch_names.push("mattermost"); }
                if ch.msteams.is_some() { ch_names.push("msteams"); }
                if ch.googlechat.is_some() { ch_names.push("googlechat"); }
                if ch.feishu.is_some() { ch_names.push("feishu"); }
                if ch.dingtalk.is_some() { ch_names.push("dingtalk"); }
                if ch.wecom.is_some() { ch_names.push("wecom"); }
                if ch.wechat.is_some() { ch_names.push("wechat"); }
                if ch.qq.is_some() { ch_names.push("qq"); }
                if ch.line.is_some() { ch_names.push("line"); }
                if ch.zalo.is_some() { ch_names.push("zalo"); }
                if ch.matrix.is_some() { ch_names.push("matrix"); }
                let ch_count = ch_names.len();
                if ch_count > 0 {
                    kv(
                        "Channels:",
                        &format!("{} ({})", ch_names.join(", "), ch_count),
                    );
                } else {
                    kv("Channels:", &dim("none configured"));
                }

                // Agents
                let agent_count = cfg.agents.list.len();
                if agent_count > 0 {
                    let first = &cfg.agents.list[0];
                    let model = first
                        .model
                        .as_ref()
                        .and_then(|m| m.primary.as_deref())
                        .or_else(|| {
                            cfg.agents
                                .defaults
                                .model
                                .as_ref()
                                .and_then(|m| m.primary.as_deref())
                        })
                        .unwrap_or("--");
                    if agent_count == 1 {
                        kv(
                            "Agent:",
                            &format!("{} (model: {})", first.id, model),
                        );
                    } else {
                        kv(
                            "Agents:",
                            &format!(
                                "{} + {} more (model: {})",
                                first.id,
                                agent_count - 1,
                                model
                            ),
                        );
                    }
                } else {
                    // agents.list empty 鈥?default "main" agent is auto-synthesized
                    let model = cfg.agents
                        .defaults
                        .model
                        .as_ref()
                        .and_then(|m| m.primary.as_deref())
                        .unwrap_or("--");
                    kv("Agent:", &format!("main (default, model: {})", model));
                }

                // Port info when gateway is running
                if running {
                    kv("Port:", &format!("{}", cfg.gateway.port));
                }
            }
            Err(e) => {
                kv("Config:", &red(&format!("error \u{2014} {e:#}")));
            }
        }

        // Tools
        let (avail, total) = super::tools::tools_count();
        let summary = super::tools::tools_summary_line();
        kv("Tools:", &format!("{}/{}{}", avail, total, summary));

        println!();
    }
    Ok(())
}

pub async fn cmd_health(args: HealthArgs) -> Result<()> {
    if args.json {
        println!("{}", serde_json::json!({"status": "ok"}));
    } else {
        println!("OK");
    }
    Ok(())
}

// ---------------------------------------------------------------------------
// TUI
// ---------------------------------------------------------------------------

struct TuiState {
    /// "running (pid XXXX)" or "stopped"
    gateway_status: String,
    gateway_port: u16,
    /// (id, model, sessions) -- session count is "--" (requires live gateway
    /// query)
    agents: Vec<(String, String, String)>,
    /// Last 10 log lines
    logs: Vec<String>,
}

impl TuiState {
    fn load() -> Self {
        let gateway_status = gateway_status_str();

        let (gateway_port, agents) = match config::load() {
            Ok(cfg) => {
                let port = cfg.gateway.port;
                let list = cfg
                    .agents
                    .list
                    .iter()
                    .map(|a| {
                        let model = a
                            .model
                            .as_ref()
                            .and_then(|m| m.primary.as_deref())
                            .unwrap_or("--")
                            .to_string();
                        (a.id.clone(), model, "--".to_string())
                    })
                    .collect();
                (port, list)
            }
            Err(_) => (18888, vec![]),
        };

        let logs = read_last_log_lines(10);

        TuiState {
            gateway_status,
            gateway_port,
            agents,
            logs,
        }
    }
}

fn gateway_status_str() -> String {
    let pid_path = gateway_pid_file();
    let pid_str = match std::fs::read_to_string(&pid_path) {
        Ok(s) => s.trim().to_string(),
        Err(_) => return "stopped".to_string(),
    };
    let pid: u32 = match pid_str.parse() {
        Ok(p) => p,
        Err(_) => return "stopped (bad PID file)".to_string(),
    };
    let alive = crate::sys::process_alive(pid);

    if alive {
        format!("running (pid {pid})")
    } else {
        let _ = std::fs::remove_file(&pid_path);
        format!("stopped (stale pid {pid})")
    }
}

fn read_last_log_lines(n: usize) -> Vec<String> {
    let log_path = config::loader::log_file();
    match std::fs::read_to_string(&log_path) {
        Ok(content) => {
            let lines: Vec<&str> = content.lines().collect();
            let start = lines.len().saturating_sub(n);
            lines[start..].iter().map(|s| s.to_string()).collect()
        }
        Err(_) => vec!["(no gateway.log found)".to_string()],
    }
}

pub async fn cmd_tui(_args: TuiArgs) -> Result<()> {
    enable_raw_mode()?;
    let mut stdout = std::io::stdout();
    execute!(stdout, EnterAlternateScreen)?;
    let backend = CrosstermBackend::new(stdout);
    let mut terminal = Terminal::new(backend)?;

    let result = run_tui_loop(&mut terminal);

    disable_raw_mode()?;
    execute!(terminal.backend_mut(), LeaveAlternateScreen)?;
    terminal.show_cursor()?;

    result
}

fn run_tui_loop(terminal: &mut Terminal<CrosstermBackend<std::io::Stdout>>) -> Result<()> {
    let mut state = TuiState::load();

    loop {
        terminal.draw(|f| draw_ui(f, &state))?;

        if event::poll(std::time::Duration::from_millis(500))?
            && let Event::Key(key) = event::read()?
            && key.kind == KeyEventKind::Press
        {
            match key.code {
                KeyCode::Char('q') | KeyCode::Char('Q') => break,
                KeyCode::Char('r') | KeyCode::Char('R') => {
                    state = TuiState::load();
                }
                _ => {}
            }
        }
    }
    Ok(())
}

fn draw_ui(f: &mut ratatui::Frame, state: &TuiState) {
    let area = f.area();

    // Split: gateway(3) / agents(dynamic) / logs(dynamic) / help(1)
    let chunks = Layout::default()
        .direction(Direction::Vertical)
        .constraints([
            Constraint::Length(3),
            Constraint::Min(3),
            Constraint::Min(4),
            Constraint::Length(1),
        ])
        .split(area);

    // -- Gateway status --
    let running = state.gateway_status.starts_with("running");
    let status_color = if running { Color::Green } else { Color::Red };
    let gw_text = Line::from(vec![
        Span::raw("  Status: "),
        Span::styled(
            &state.gateway_status,
            Style::default()
                .fg(status_color)
                .add_modifier(Modifier::BOLD),
        ),
        Span::raw(format!("   Port: {}", state.gateway_port)),
    ]);
    let gw_para =
        Paragraph::new(gw_text).block(Block::default().title(" Gateway ").borders(Borders::ALL));
    f.render_widget(gw_para, chunks[0]);

    // -- Agent list --
    let header = ListItem::new(Line::from(vec![Span::styled(
        format!("{:<20} {:<36} {}", "ID", "Model", "Sessions"),
        Style::default().add_modifier(Modifier::BOLD),
    )]));
    let mut items: Vec<ListItem> = vec![header];
    if state.agents.is_empty() {
        items.push(ListItem::new("  (no config loaded)"));
    } else {
        for (id, model, sessions) in &state.agents {
            items.push(ListItem::new(format!(
                "{:<20} {:<36} {}",
                id, model, sessions
            )));
        }
    }
    let agent_list =
        List::new(items).block(Block::default().title(" Agents ").borders(Borders::ALL));
    f.render_widget(agent_list, chunks[1]);

    // -- Logs --
    let log_items: Vec<ListItem> = if state.logs.is_empty() {
        vec![ListItem::new("  (no log entries)")]
    } else {
        state
            .logs
            .iter()
            .map(|l| ListItem::new(l.as_str()))
            .collect()
    };
    let log_list = List::new(log_items).block(
        Block::default()
            .title(" Recent Logs ")
            .borders(Borders::ALL),
    );
    f.render_widget(log_list, chunks[2]);

    // -- Help bar --
    let help = Paragraph::new(Line::from(vec![
        Span::styled(" q", Style::default().fg(Color::Yellow)),
        Span::raw(" quit   "),
        Span::styled("r", Style::default().fg(Color::Yellow)),
        Span::raw(" refresh"),
    ]));
    f.render_widget(help, chunks[3]);
}