elisym-client 0.4.8

CLI agent runner for the elisym protocol
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
pub mod event;
pub mod ui;

use std::collections::VecDeque;
use std::time::Instant;

use ratatui::widgets::TableState;
use tokio::sync::mpsc;

/// Maximum global log lines retained.
const MAX_GLOBAL_LOGS: usize = 500;

#[derive(Debug)]
pub enum AppEvent {
    JobReceived {
        job_id: String,
        customer_id: String,
        input: String,
    },
    PaymentRequested {
        job_id: String,
        price: u64,
        fee: u64,
    },
    PaymentReceived {
        job_id: String,
        net_amount: u64,
    },
    PaymentTimeout {
        job_id: String,
    },
    SkillStarted {
        job_id: String,
        skill_name: String,
    },
    LlmRound {
        job_id: String,
        round: usize,
        max_rounds: usize,
    },
    ToolStarted {
        job_id: String,
        tool_name: String,
    },
    ToolCompleted {
        job_id: String,
        tool_name: String,
        output_len: usize,
    },
    ToolFailed {
        job_id: String,
        tool_name: String,
        error: String,
    },
    JobCompleted {
        job_id: String,
        result_len: usize,
    },
    JobFailed {
        job_id: String,
        error: String,
    },
    WalletBalance(u64),
    Ping {
        from: String,
    },
}

#[derive(Debug, Clone)]
pub enum JobStatus {
    PaymentPending,
    Processing,
    Completed,
    Failed(String),
}

impl std::fmt::Display for JobStatus {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            JobStatus::PaymentPending => write!(f, "$ Payment"),
            JobStatus::Processing => write!(f, "⚙ Running"),
            JobStatus::Completed => write!(f, "✓ Done"),
            JobStatus::Failed(_) => write!(f, "✗ Failed"),
        }
    }
}

#[derive(Debug, Clone)]
pub struct LogLine {
    pub time: String,
    pub icon: &'static str,
    pub message: String,
}

pub struct JobEntry {
    pub job_id: String,
    pub customer_id: String,
    pub input: String,
    pub status: JobStatus,
    pub skill_name: Option<String>,
    pub price: Option<u64>,
    pub fee: Option<u64>,
    pub net_amount: Option<u64>,
    pub started_at: Instant,
    pub completed_at: Option<Instant>,
    pub logs: Vec<LogLine>,
}

pub enum Screen {
    Main,
    JobDetail(usize),
    Recovery,
}

pub enum Focus {
    Table,
    Log,
}

pub struct App {
    pub screen: Screen,
    pub focus: Focus,
    pub jobs: Vec<JobEntry>,
    pub global_logs: VecDeque<LogLine>,
    pub table_state: TableState,
    pub log_scroll: u16,
    pub detail_scroll: u16,
    // Header info
    pub agent_name: String,
    pub skill_name: String,
    pub price: u64,
    pub wallet_balance: u64,
    pub network: String,
    // Sound
    pub sound_enabled: bool,
    pub sound_volume: f32,
    // Recovery
    pub ledger: Option<std::sync::Arc<tokio::sync::Mutex<crate::ledger::JobLedger>>>,
    pub recovery_entries: Vec<crate::ledger::LedgerEntry>,
    pub recovery_table_state: TableState,
    pub recovery_detail_scroll: u16,
    /// Channel to trigger immediate recovery sweep in the runtime.
    pub retry_tx: Option<mpsc::UnboundedSender<String>>,
}

impl App {
    #[allow(clippy::too_many_arguments)]
    pub fn new(
        agent_name: String,
        skill_name: String,
        price: u64,
        wallet_balance: u64,
        network: String,
        sound_enabled: bool,
        sound_volume: f32,
    ) -> Self {
        Self {
            screen: Screen::Main,
            focus: Focus::Table,
            jobs: Vec::new(),
            global_logs: VecDeque::new(),
            table_state: TableState::default(),
            log_scroll: 0,
            detail_scroll: 0,
            agent_name,
            skill_name,
            price,
            wallet_balance,
            network,
            sound_enabled,
            sound_volume,
            ledger: None,
            recovery_entries: Vec::new(),
            recovery_table_state: TableState::default(),
            recovery_detail_scroll: 0,
            retry_tx: None,
        }
    }

    pub fn set_ledger(&mut self, ledger: std::sync::Arc<tokio::sync::Mutex<crate::ledger::JobLedger>>) {
        self.ledger = Some(ledger);
    }

    pub fn set_retry_tx(&mut self, tx: mpsc::UnboundedSender<String>) {
        self.retry_tx = Some(tx);
    }

    /// Retry the currently selected recovery entry.
    /// Resets Failed → Paid/Executed in ledger and triggers immediate recovery sweep.
    pub fn retry_selected(&mut self) -> bool {
        let idx = match self.recovery_table_state.selected() {
            Some(i) => i,
            None => return false,
        };
        let entry = match self.recovery_entries.get(idx) {
            Some(e) => e,
            None => return false,
        };

        let job_id = entry.job_id.clone();
        let status = entry.status.clone();

        if matches!(status, crate::ledger::LedgerStatus::Delivered) {
            return false;
        }

        let ledger = match self.ledger {
            Some(ref l) => l.clone(),
            None => return false,
        };

        let mut lg = match ledger.try_lock() {
            Ok(lg) => lg,
            Err(_) => return false,
        };

        let reset = match status {
            crate::ledger::LedgerStatus::Failed => {
                lg.reset_for_retry(&job_id).unwrap_or(false)
            }
            crate::ledger::LedgerStatus::Paid | crate::ledger::LedgerStatus::Executed => {
                // Already pending — just reset retry count to give it fresh attempts
                let _ = lg.increment_retry(&job_id);
                true
            }
            _ => false,
        };

        if reset {
            // Refresh the view while we still hold the lock
            self.recovery_entries = lg.all_entries();
            drop(lg);

            // Notify runtime to run recovery sweep now
            if let Some(ref tx) = self.retry_tx {
                let _ = tx.send(job_id.clone());
            }
            self.add_global_log("", format!("Manual retry queued: {}...", &job_id[..12.min(job_id.len())]));
            return true;
        }

        false
    }

    /// Refresh recovery entries from ledger (called when opening recovery screen).
    pub fn refresh_recovery(&mut self) {
        if let Some(ref ledger) = self.ledger {
            if let Ok(lg) = ledger.try_lock() {
                self.recovery_entries = lg.all_entries();
            }
        }
    }

    pub fn toggle_sound(&mut self) {
        self.sound_enabled = !self.sound_enabled;
        let status = if self.sound_enabled { "ON" } else { "OFF" };
        self.add_global_log("", format!("Sound {}", status));
        // Persist to global config
        let mut gc = crate::cli::global_config::load_global_config();
        gc.tui.sound_enabled = self.sound_enabled;
        let _ = crate::cli::global_config::save_global_config(&gc);
    }

    fn now_str() -> String {
        let now = chrono::Local::now();
        now.format("%H:%M:%S").to_string()
    }

    fn add_global_log(&mut self, icon: &'static str, message: String) {
        let line = LogLine {
            time: Self::now_str(),
            icon,
            message,
        };
        self.global_logs.push_back(line);
        if self.global_logs.len() > MAX_GLOBAL_LOGS {
            self.global_logs.pop_front();
        }
    }

    fn find_job_mut(&mut self, job_id: &str) -> Option<&mut JobEntry> {
        self.jobs.iter_mut().find(|j| j.job_id == job_id)
    }

    fn add_job_log(job: &mut JobEntry, icon: &'static str, message: String) {
        job.logs.push(LogLine {
            time: Self::now_str(),
            icon,
            message,
        });
    }

    pub fn update(&mut self, event: AppEvent) {
        match event {
            AppEvent::JobReceived {
                job_id,
                customer_id,
                input,
            } => {
                let short_id = &job_id[..12.min(job_id.len())];
                let short_customer = &customer_id[..12.min(customer_id.len())];
                self.add_global_log("", format!("New job {}... from {}...", short_id, short_customer));

                let mut entry = JobEntry {
                    job_id: job_id.clone(),
                    customer_id,
                    input,
                    status: JobStatus::PaymentPending,
                    skill_name: None,
                    price: None,
                    fee: None,
                    net_amount: None,
                    started_at: Instant::now(),
                    completed_at: None,
                    logs: Vec::new(),
                };
                Self::add_job_log(&mut entry, "", "Job received".into());
                self.jobs.push(entry);

                // Auto-select first job if none selected
                if self.table_state.selected().is_none() {
                    self.table_state.select(Some(self.jobs.len() - 1));
                }
            }
            AppEvent::PaymentRequested { job_id, price, fee } => {
                let short_id = &job_id[..12.min(job_id.len())];
                let price_sol = crate::util::format_sol_compact(price);
                self.add_global_log("$", format!("Requesting payment: {} SOL [{}...]", price_sol, short_id));

                if let Some(job) = self.find_job_mut(&job_id) {
                    job.price = Some(price);
                    job.fee = Some(fee);
                    Self::add_job_log(job, "$", format!("Requesting payment: {} SOL", price_sol));
                }
            }
            AppEvent::PaymentReceived { job_id, net_amount } => {
                let short_id = &job_id[..12.min(job_id.len())];
                let net_sol = crate::util::format_sol_compact(net_amount);
                self.add_global_log("$", format!("Payment received ({} SOL net) [{}...]", net_sol, short_id));

                if let Some(job) = self.find_job_mut(&job_id) {
                    job.net_amount = Some(net_amount);
                    job.status = JobStatus::Processing;
                    Self::add_job_log(job, "", format!("Payment received ({} SOL net)", net_sol));
                }
            }
            AppEvent::PaymentTimeout { job_id } => {
                let short_id = &job_id[..12.min(job_id.len())];
                self.add_global_log("", format!("Payment timeout [{}...]", short_id));

                if let Some(job) = self.find_job_mut(&job_id) {
                    job.status = JobStatus::Failed("payment timeout".into());
                    job.completed_at = Some(Instant::now());
                    Self::add_job_log(job, "", "Payment timeout".into());
                }
            }
            AppEvent::SkillStarted { job_id, skill_name } => {
                let short_id = &job_id[..12.min(job_id.len())];
                self.add_global_log("", format!("Running skill {} [{}...]", skill_name, short_id));

                if let Some(job) = self.find_job_mut(&job_id) {
                    job.skill_name = Some(skill_name.clone());
                    job.status = JobStatus::Processing;
                    Self::add_job_log(job, "", format!("Running skill {}", skill_name));
                }
            }
            AppEvent::LlmRound {
                job_id,
                round,
                max_rounds,
            } => {
                if let Some(job) = self.find_job_mut(&job_id) {
                    Self::add_job_log(job, "", format!("LLM round {}/{}", round, max_rounds));
                }
            }
            AppEvent::ToolStarted { job_id, tool_name } => {
                let short_id = &job_id[..12.min(job_id.len())];
                self.add_global_log("", format!("Running tool {} [{}...]", tool_name, short_id));

                if let Some(job) = self.find_job_mut(&job_id) {
                    Self::add_job_log(job, "", format!("Running tool {}", tool_name));
                }
            }
            AppEvent::ToolCompleted {
                job_id,
                tool_name,
                output_len,
            } => {
                if let Some(job) = self.find_job_mut(&job_id) {
                    Self::add_job_log(
                        job,
                        "",
                        format!("Tool {} done ({} chars)", tool_name, output_len),
                    );
                }
            }
            AppEvent::ToolFailed {
                job_id,
                tool_name,
                error,
            } => {
                if let Some(job) = self.find_job_mut(&job_id) {
                    Self::add_job_log(
                        job,
                        "",
                        format!("Tool {} failed: {}", tool_name, error),
                    );
                }
            }
            AppEvent::JobCompleted { job_id, result_len } => {
                let short_id = &job_id[..12.min(job_id.len())];
                self.add_global_log(
                    "",
                    format!("Job {}... done ({} chars)", short_id, result_len),
                );
                if self.sound_enabled {
                    play_sound("Blow", self.sound_volume);
                }

                if let Some(job) = self.find_job_mut(&job_id) {
                    job.status = JobStatus::Completed;
                    job.completed_at = Some(Instant::now());
                    Self::add_job_log(
                        job,
                        "",
                        format!("Result delivered ({} chars)", result_len),
                    );
                }
            }
            AppEvent::JobFailed { job_id, error } => {
                let short_id = &job_id[..12.min(job_id.len())];
                self.add_global_log("", format!("Job {}... failed: {}", short_id, error));

                if let Some(job) = self.find_job_mut(&job_id) {
                    job.status = JobStatus::Failed(error.clone());
                    job.completed_at = Some(Instant::now());
                    Self::add_job_log(job, "", format!("Failed: {}", error));
                }
            }
            AppEvent::WalletBalance(balance) => {
                self.wallet_balance = balance;
            }
            AppEvent::Ping { from } => {
                let short = &from[..12.min(from.len())];
                self.add_global_log("", format!("Ping from {}... — pong sent", short));
            }
        }
    }

    pub fn select_next(&mut self) {
        if self.jobs.is_empty() {
            return;
        }
        let i = self
            .table_state
            .selected()
            .map(|i| if i + 1 >= self.jobs.len() { 0 } else { i + 1 })
            .unwrap_or(0);
        self.table_state.select(Some(i));
    }

    pub fn select_prev(&mut self) {
        if self.jobs.is_empty() {
            return;
        }
        let i = self
            .table_state
            .selected()
            .map(|i| if i == 0 { self.jobs.len() - 1 } else { i - 1 })
            .unwrap_or(0);
        self.table_state.select(Some(i));
    }
}

pub fn create_event_channel() -> (mpsc::UnboundedSender<AppEvent>, mpsc::UnboundedReceiver<AppEvent>) {
    mpsc::unbounded_channel()
}

/// Play a macOS system sound by name (e.g. "Blow", "Glass", "Ping").
/// Non-blocking, fire-and-forget. Does nothing on non-macOS.
fn play_sound(name: &str, volume: f32) {
    #[cfg(target_os = "macos")]
    {
        let path = format!("/System/Library/Sounds/{}.aiff", name);
        let vol = format!("{:.2}", volume.clamp(0.0, 1.0));
        let _ = std::process::Command::new("afplay")
            .args([&path, "-v", &vol])
            .stdin(std::process::Stdio::null())
            .stdout(std::process::Stdio::null())
            .stderr(std::process::Stdio::null())
            .spawn();
    }
    #[cfg(not(target_os = "macos"))]
    {
        let _ = (name, volume);
    }
}