vex-cli 0.7.13

AI-native version control workflow engine
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
use serde_json::{Value, json};

use crate::daemon_client;

pub fn spawn(
    project_name: &str,
    ws_name: Option<&str>,
    agent_type: Option<&str>,
    host: Option<&str>,
) {
    let mut client = daemon_client::connect_to_hub_or_exit();

    let mut params = json!({"project": project_name});
    if let Some(ws) = ws_name {
        params["ws"] = json!(ws);
    }
    if let Some(t) = agent_type {
        params["type"] = json!(t);
    }
    if let Some(h) = host {
        params["host"] = json!(h);
    }

    match client.request("agent.spawn", params) {
        Ok(result) => {
            let agent_id = result
                .get("agent_id")
                .and_then(|v| v.as_str())
                .unwrap_or("unknown");
            let context = ws_name.unwrap_or(project_name);
            println!("Spawned agent '{agent_id}' in '{context}'");
        }
        Err(e) => {
            eprintln!("Error: {e}");
            std::process::exit(1);
        }
    }
}

pub fn prompt(agent_id: &str, text: &str, host: Option<&str>) {
    let mut client = daemon_client::connect_to_hub_or_exit();

    let mut params = json!({"agent_id": agent_id, "text": text});
    if let Some(h) = host {
        params["host"] = json!(h);
    }
    match client.request("agent.prompt", params) {
        Ok(_) => println!("Prompt sent to agent '{agent_id}'"),
        Err(e) => {
            eprintln!("Error: {e}");
            std::process::exit(1);
        }
    }
}

pub fn kill(agent_id: &str, host: Option<&str>) {
    let mut client = daemon_client::connect_to_hub_or_exit();

    let mut params = json!({"agent_id": agent_id});
    if let Some(h) = host {
        params["host"] = json!(h);
    }
    match client.request("agent.kill", params) {
        Ok(_) => println!("Killed agent '{agent_id}'"),
        Err(e) => {
            eprintln!("Error: {e}");
            std::process::exit(1);
        }
    }
}

pub fn attach_shell(agent_id: &str, host: Option<&str>) {
    let mut client = daemon_client::connect_to_hub_or_exit();

    let mut params = json!({"agent_id": agent_id});
    if let Some(h) = host {
        params["host"] = json!(h);
    }
    let shell_id = match client.request("agent.shell-id", params) {
        Ok(result) => result
            .get("shell_id")
            .and_then(|v| v.as_str())
            .unwrap_or_default()
            .to_string(),
        Err(e) => {
            eprintln!("Error: {e}");
            std::process::exit(1);
        }
    };

    drop(client);
    crate::shell::attach(&shell_id, host);
}

/// Watch agent conversation live by polling parsed JSONL messages.
/// Show agent conversation from parsed JSONL. With --watch, keep polling.
pub fn conversation(agent_id: &str, watch: bool, verbose: bool, host: Option<&str>) {
    let mut client = daemon_client::connect_to_hub_or_exit();

    let dim = "\x1b[2m";
    let reset = "\x1b[0m";
    let blue = "\x1b[34m";
    let green = "\x1b[32m";
    let yellow = "\x1b[33m";
    let cyan = "\x1b[36m";

    let mut status_params = json!({"agent_id": agent_id});
    if let Some(h) = host {
        status_params["host"] = json!(h);
    }

    // Show session file path
    if let Ok(status) = client.request("agent.status", status_params)
        && let Some(session) = status.get("session_file").and_then(|v| v.as_str())
    {
        eprintln!("{dim}session: {session}{reset}");
    }

    if watch {
        eprintln!("{dim}[watching — press Ctrl+C to stop]{reset}");
    }
    eprintln!();

    let mut seen = 0usize;

    loop {
        let mut msg_params = json!({"agent_id": agent_id});
        if let Some(h) = host {
            msg_params["host"] = json!(h);
        }
        let messages = match client.request("agent.messages", msg_params) {
            Ok(val) => val.as_array().cloned().unwrap_or_default(),
            Err(e) => {
                eprintln!("Error: {e}");
                std::process::exit(1);
            }
        };

        for msg in messages.iter().skip(seen) {
            let role = msg.get("role").and_then(|v| v.as_str()).unwrap_or("");

            match role {
                "user" => {
                    let content = msg.get("content").and_then(|v| v.as_str()).unwrap_or("");
                    println!("{blue}━━━ User ━━━{reset}");
                    println!("{content}");
                    println!();
                }
                "assistant" => {
                    if let Some(blocks) = msg.get("content").and_then(|v| v.as_array()) {
                        let has_text = blocks
                            .iter()
                            .any(|b| b.get("type").and_then(|v| v.as_str()) == Some("text"));

                        if verbose {
                            println!("{green}━━━ Assistant ━━━{reset}");
                            for block in blocks {
                                render_block(block, dim, reset, yellow, cyan);
                            }
                            println!();
                        } else if has_text {
                            println!("{green}━━━ Assistant ━━━{reset}");
                            for block in blocks {
                                if block.get("type").and_then(|v| v.as_str()) == Some("text") {
                                    render_block(block, dim, reset, yellow, cyan);
                                }
                            }
                            println!();
                        }
                        // If no text blocks and not verbose, skip entirely
                    } else if let Some(content) = msg.get("content").and_then(|v| v.as_str()) {
                        println!("{green}━━━ Assistant ━━━{reset}");
                        println!("{content}");
                        println!();
                    }
                }
                "tool_result" if verbose => {
                    if let Some(blocks) = msg.get("content").and_then(|v| v.as_array()) {
                        for block in blocks {
                            render_tool_result(block, dim, reset, cyan);
                        }
                    }
                }
                _ => {}
            }
        }

        seen = messages.len();

        if !watch {
            if seen == 0 {
                println!("{dim}No messages yet.{reset}");
            }
            break;
        }

        std::thread::sleep(std::time::Duration::from_secs(2));
    }
}

fn render_block(block: &Value, dim: &str, reset: &str, yellow: &str, cyan: &str) {
    let btype = block.get("type").and_then(|v| v.as_str()).unwrap_or("");
    match btype {
        "text" => {
            let text = block.get("text").and_then(|v| v.as_str()).unwrap_or("");
            println!("{text}");
        }
        "thinking" => {
            let thinking = block.get("thinking").and_then(|v| v.as_str()).unwrap_or("");
            let preview = if thinking.len() > 200 {
                &thinking[..200]
            } else {
                thinking
            };
            println!("{dim}  [thinking] {preview}...{reset}");
        }
        "tool_use" => {
            let name = block.get("name").and_then(|v| v.as_str()).unwrap_or("?");
            let input = block
                .get("input")
                .map(|v| {
                    let s = v.to_string();
                    if s.len() > 120 {
                        format!("{}...", &s[..120])
                    } else {
                        s
                    }
                })
                .unwrap_or_default();
            println!("{yellow}  [{cyan}{name}{yellow}]{reset} {dim}{input}{reset}");
        }
        "tool_result" => {
            // tool_result blocks inside assistant messages — show inline
            render_tool_result_block(block, dim, reset, cyan);
        }
        _ => {}
    }
}

/// Render a standalone tool_result message (the response to a tool_use).
fn render_tool_result(block: &Value, dim: &str, reset: &str, cyan: &str) {
    render_tool_result_block(block, dim, reset, cyan);
}

fn render_tool_result_block(block: &Value, dim: &str, reset: &str, cyan: &str) {
    let tool_use_id = block
        .get("tool_use_id")
        .and_then(|v| v.as_str())
        .unwrap_or("?");
    // Show a short ID suffix for context
    let id_short = if tool_use_id.len() > 12 {
        &tool_use_id[tool_use_id.len() - 8..]
    } else {
        tool_use_id
    };

    let content = &block["content"];
    let preview = match content {
        Value::String(s) => {
            if s.len() > 300 {
                format!("{}", &s[..300])
            } else {
                s.clone()
            }
        }
        Value::Array(arr) => {
            // content can be an array of {type: "text", text: "..."} blocks
            let texts: Vec<&str> = arr
                .iter()
                .filter_map(|v| v.get("text").and_then(|t| t.as_str()))
                .collect();
            let joined = texts.join("\n");
            if joined.len() > 300 {
                format!("{}", &joined[..300])
            } else {
                joined
            }
        }
        _ => content.to_string(),
    };

    let lines: Vec<&str> = preview.lines().collect();
    if lines.len() <= 4 {
        println!("{dim}{cyan}{id_short}{reset} {dim}{preview}{reset}");
    } else {
        println!(
            "{dim}{cyan}{id_short}{reset} {dim}{} (+{} more lines){reset}",
            lines[..3].join("\n    "),
            lines.len() - 3
        );
    }
}

pub fn list(project_name: Option<&str>, ws_name: Option<&str>, host: Option<&str>) {
    let mut client = daemon_client::connect_to_hub_or_exit();

    let mut params = json!({});
    if let Some(h) = host {
        params["host"] = json!(h);
    }

    match client.request("agent.list", params) {
        Ok(result) => {
            let agents = result.as_array().unwrap_or(&Vec::new()).clone();

            let filtered: Vec<&Value> = agents
                .iter()
                .filter(|a| {
                    if let Some(ws) = ws_name {
                        a.get("workstream_id")
                            .and_then(|v| v.as_str())
                            .is_some_and(|id| id == ws)
                    } else {
                        true
                    }
                })
                .collect();

            if filtered.is_empty() {
                if let Some(ws) = ws_name {
                    let project = project_name.unwrap_or("?");
                    println!("No agents running for '{project}/{ws}'.");
                } else {
                    println!("No agents running.");
                }
                return;
            }

            println!(
                "{:<12} {:<15} {:<10} {:<15} {:<10} SESSION",
                "HOST", "ID", "TYPE", "STATE", "SHELL"
            );
            println!("{}", "".repeat(90));
            for agent in &filtered {
                let session = agent
                    .get("session_file")
                    .and_then(|v| v.as_str())
                    .unwrap_or("-");
                let session_short = session.rsplit('/').next().unwrap_or(session);
                println!(
                    "{:<12} {:<15} {:<10} {:<15} {:<10} {}",
                    agent.get("host").and_then(|v| v.as_str()).unwrap_or("-"),
                    agent.get("id").and_then(|v| v.as_str()).unwrap_or("-"),
                    agent.get("type").and_then(|v| v.as_str()).unwrap_or("-"),
                    agent.get("state").and_then(|v| v.as_str()).unwrap_or("-"),
                    agent
                        .get("shell_id")
                        .and_then(|v| v.as_str())
                        .unwrap_or("-"),
                    session_short,
                );
            }
        }
        Err(e) => {
            eprintln!("Error: {e}");
            std::process::exit(1);
        }
    }
}

pub fn status(agent_id: &str, host: Option<&str>) {
    let mut client = daemon_client::connect_to_hub_or_exit();

    let mut params = json!({"agent_id": agent_id});
    if let Some(h) = host {
        params["host"] = json!(h);
    }
    match client.request("agent.status", params) {
        Ok(result) => {
            println!(
                "Agent: {}",
                result
                    .get("id")
                    .and_then(|v| v.as_str())
                    .unwrap_or(agent_id)
            );
            println!(
                "  Type: {}",
                result.get("type").and_then(|v| v.as_str()).unwrap_or("-")
            );
            println!(
                "  State: {}",
                result.get("state").and_then(|v| v.as_str()).unwrap_or("-")
            );
            println!(
                "  Shell: {}",
                result
                    .get("shell_id")
                    .and_then(|v| v.as_str())
                    .unwrap_or("-")
            );
            println!(
                "  Session: {}",
                result
                    .get("session_file")
                    .and_then(|v| v.as_str())
                    .unwrap_or("-")
            );
            println!(
                "  Messages: {}",
                result
                    .get("message_count")
                    .and_then(|v| v.as_u64())
                    .unwrap_or(0)
            );
            println!(
                "  Spawned: {}",
                result
                    .get("spawned_at")
                    .and_then(|v| v.as_str())
                    .unwrap_or("-")
            );
            println!(
                "  Last activity: {}",
                result
                    .get("last_activity")
                    .and_then(|v| v.as_str())
                    .unwrap_or("-")
            );
        }
        Err(e) => {
            eprintln!("Error: {e}");
            std::process::exit(1);
        }
    }
}