asterai 1.0.0-alpha.16

CLI for asterai - the portable AI compute platform
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
use app::{App, AuthState, Screen};
use crossterm::event::{self, Event, KeyCode, KeyModifiers};
use crossterm::execute;
use crossterm::terminal::{
    EnterAlternateScreen, LeaveAlternateScreen, disable_raw_mode, enable_raw_mode,
};
use ratatui::prelude::*;
use std::io::{BufWriter, Write};
use std::time::Duration;

pub mod app;
pub mod ops;
pub mod views;

pub type Tty = BufWriter<std::fs::File>;

struct SavedStdio {
    out: i32,
    err: i32,
}

pub async fn run() -> eyre::Result<()> {
    #[cfg(windows)]
    {
        unsafe extern "system" {
            fn SetConsoleOutputCP(code_page: u32) -> i32;
        }
        unsafe {
            SetConsoleOutputCP(65001);
        }
    }
    let (saved, mut tty) = redirect_stdio();
    enable_raw_mode()?;
    execute!(tty, EnterAlternateScreen)?;
    let backend = CrosstermBackend::new(tty);
    let mut terminal = Terminal::new(backend)?;
    let mut app = App::default();
    let result = run_app(&mut terminal, &mut app).await;
    disable_raw_mode()?;
    execute!(terminal.backend_mut(), LeaveAlternateScreen)?;
    terminal.show_cursor()?;
    let _ = Write::flush(terminal.backend_mut());
    restore_stdio(saved);
    result?;
    std::process::exit(0);
}

async fn run_app(
    terminal: &mut Terminal<CrosstermBackend<Tty>>,
    app: &mut App,
) -> eyre::Result<()> {
    // Fire async CLI version check (non-blocking).
    let (vtx, vrx) = tokio::sync::oneshot::channel();
    app.pending_version_check = Some(vrx);
    tokio::spawn(async move {
        let ver = ops::fetch_latest_cli_version().await;
        let _ = vtx.send(ver);
    });
    let username = ops::check_auth().await;
    match username {
        Some(_username) => {
            views::picker::discover_agents(app);
        }
        None => {
            app.screen = Screen::Auth(AuthState::NeedLogin {
                input: String::new(),
                error: None,
            });
        }
    }
    loop {
        terminal.draw(|f| views::render(f, app))?;
        if app.should_quit {
            // Suppress panics from background wasmtime tasks during
            // tokio shutdown — they're harmless but look scary.
            std::panic::set_hook(Box::new(|_| {}));
            return Ok(());
        }
        // Clear expired toasts.
        if let Screen::Chat(state) = &mut app.screen
            && let Some(until) = state.toast_until
            && std::time::Instant::now() >= until
        {
            state.toast = None;
            state.toast_until = None;
        }
        let has_toast = matches!(&app.screen, Screen::Chat(s) if s.toast.is_some());
        let has_pending = has_toast
            || app.pending_response.is_some()
            || app.pending_banner.is_some()
            || app.pending_components.is_some()
            || app.pending_version_check.is_some()
            || app.pending_sync.is_some()
            || app.pending_env_check.is_some()
            || app.pending_runtime.is_some();
        // Always poll with timeout so the cursor blink can advance.
        let is_chat = matches!(&app.screen, Screen::Chat(_));
        let needs_poll = has_pending || is_chat;
        let ev = match needs_poll {
            true => match event::poll(Duration::from_millis(100))? {
                true => Some(event::read()?),
                false => None,
            },
            false => Some(event::read()?),
        };
        if app.pending_response.is_some() {
            check_pending_response(app);
        }
        // Tick spinners on timeout (no user event).
        if ev.is_none() {
            match &mut app.screen {
                Screen::Chat(state) => {
                    state.spinner_tick = state.spinner_tick.wrapping_add(1);
                }
                Screen::Picker(state) => {
                    state.spinner_tick = state.spinner_tick.wrapping_add(1);
                }
                _ => {}
            }
        }
        if app.pending_banner.is_some() {
            check_pending_banner(app);
        }
        if app.pending_components.is_some() {
            check_pending_components(app);
        }
        if let Some(rx) = &mut app.pending_version_check
            && let Ok(ver) = rx.try_recv()
        {
            app.latest_cli_version = ver;
            app.pending_version_check = None;
        }
        if let Some(rx) = &mut app.pending_sync
            && let Ok(remote_entries) = rx.try_recv()
        {
            app.pending_sync = None;
            if let Screen::Picker(state) = &mut app.screen {
                merge_sync_status(state, remote_entries);
            }
        }
        if let Some(rx) = &mut app.pending_env_check {
            match rx.try_recv() {
                Ok(status) => {
                    app.pending_env_check = None;
                    if let Screen::Chat(state) = &mut app.screen {
                        state.tool_env_status = status;
                    }
                }
                Err(tokio::sync::oneshot::error::TryRecvError::Closed) => {
                    app.pending_env_check = None;
                }
                Err(tokio::sync::oneshot::error::TryRecvError::Empty) => {}
            }
        }
        if let Some(rx) = &mut app.pending_runtime {
            match rx.try_recv() {
                Ok(Ok(rt)) => {
                    app.pending_runtime = None;
                    app.runtime = Some(std::sync::Arc::new(tokio::sync::Mutex::new(rt)));
                    app.screen = Screen::Chat(Box::default());
                    views::chat::start_banner_fetch(app);
                    views::chat::start_env_check(app);
                }
                Ok(Err(e)) => {
                    app.pending_runtime = None;
                    app.agent = None;
                    if let Screen::Picker(state) = &mut app.screen {
                        state.error = Some(format!("Failed to load: {e}"));
                    }
                }
                Err(tokio::sync::oneshot::error::TryRecvError::Closed) => {
                    app.pending_runtime = None;
                    app.agent = None;
                    if let Screen::Picker(state) = &mut app.screen {
                        state.error = Some("Runtime build cancelled.".to_string());
                    }
                }
                Err(tokio::sync::oneshot::error::TryRecvError::Empty) => {}
            }
        }
        let Some(ev) = ev else {
            continue;
        };
        // Ctrl+C quits from any screen. On Windows, skip chat screen
        // because Windows Terminal reserves Ctrl+C for copy.
        if let Event::Key(key) = &ev
            && key.modifiers.contains(KeyModifiers::CONTROL)
            && key.code == KeyCode::Char('c')
        {
            #[cfg(windows)]
            if matches!(app.screen, Screen::Chat(_)) {
                // Fall through to handle_event for Windows copy.
            } else {
                app.should_quit = true;
                continue;
            }
            #[cfg(not(windows))]
            {
                app.should_quit = true;
                continue;
            }
        }
        views::handle_event(app, ev, terminal).await?;
        // If we returned to the picker (e.g. Esc from chat), re-discover agents.
        if let Screen::Picker(state) = &app.screen
            && state.loading
        {
            terminal.draw(|f| views::render(f, app))?;
            views::picker::discover_agents(app);
        }
    }
}

fn check_pending_response(app: &mut App) {
    let Some(rx) = &mut app.pending_response else {
        return;
    };
    match rx.try_recv() {
        Ok(result) => {
            app.pending_response = None;
            if let Screen::Chat(state) = &mut app.screen {
                state.waiting = false;
                match result {
                    Ok(Some(text)) => {
                        state.messages.push(app::ChatMessage {
                            role: app::MessageRole::Assistant,
                            content: text,
                            styled_lines: None,
                        });
                    }
                    Ok(None) => {
                        state.messages.push(app::ChatMessage {
                            role: app::MessageRole::System,
                            content: "(No response received)".to_string(),
                            styled_lines: None,
                        });
                    }
                    Err(e) => {
                        state.messages.push(app::ChatMessage {
                            role: app::MessageRole::System,
                            content: format!("Error: {e:#}"),
                            styled_lines: None,
                        });
                    }
                }
            }
        }
        Err(tokio::sync::oneshot::error::TryRecvError::Empty) => {}
        Err(tokio::sync::oneshot::error::TryRecvError::Closed) => {
            app.pending_response = None;
            if let Screen::Chat(state) = &mut app.screen {
                state.waiting = false;
                state.messages.push(app::ChatMessage {
                    role: app::MessageRole::System,
                    content: "Request was cancelled.".to_string(),
                    styled_lines: None,
                });
            }
        }
    }
}

fn check_pending_components(app: &mut App) {
    let Some(rx) = &mut app.pending_components else {
        return;
    };
    match rx.try_recv() {
        Ok(Ok(items)) => {
            app.pending_components = None;
            if let Screen::Chat(state) = &mut app.screen {
                state.dynamic_loading = false;
                state.dynamic_items = items;
                state.dynamic_matches = (0..state.dynamic_items.len()).collect();
                state.dynamic_selected = 0;
            }
        }
        Ok(Err(e)) => {
            app.pending_components = None;
            if let Screen::Chat(state) = &mut app.screen {
                state.dynamic_loading = false;
                state.dynamic_command = None;
                state.messages.push(app::ChatMessage {
                    role: app::MessageRole::System,
                    content: format!("Failed to load components: {e:#}"),
                    styled_lines: None,
                });
            }
        }
        Err(tokio::sync::oneshot::error::TryRecvError::Empty) => {}
        Err(tokio::sync::oneshot::error::TryRecvError::Closed) => {
            app.pending_components = None;
            if let Screen::Chat(state) = &mut app.screen {
                state.dynamic_loading = false;
                state.dynamic_command = None;
            }
        }
    }
}

fn check_pending_banner(app: &mut App) {
    let Some(rx) = &mut app.pending_banner else {
        return;
    };
    match rx.try_recv() {
        Ok(Some(text)) => {
            app.pending_banner = None;
            if let Screen::Chat(state) = &mut app.screen {
                // Clean up LLM response: trim, take first line only.
                let clean = text.trim().lines().next().unwrap_or("").trim().to_string();
                if !clean.is_empty() {
                    state.banner_text = clean;
                }
                state.banner_loading = false;
            }
        }
        Ok(None) => {
            // No data returned — keep the quote.
            app.pending_banner = None;
            if let Screen::Chat(state) = &mut app.screen {
                state.banner_loading = false;
            }
        }
        Err(tokio::sync::oneshot::error::TryRecvError::Empty) => {}
        Err(tokio::sync::oneshot::error::TryRecvError::Closed) => {
            app.pending_banner = None;
            if let Screen::Chat(state) = &mut app.screen {
                state.banner_loading = false;
            }
        }
    }
}

/// Merge remote sync status into the picker. Updates sync tags and adds remote-only envs.
fn merge_sync_status(
    state: &mut app::PickerState,
    remote_entries: Vec<crate::command::env::list::EnvListEntry>,
) {
    use crate::artifact::ArtifactSyncTag;
    // Build a map of remote entries for quick lookup.
    let remote_map: std::collections::HashMap<String, &crate::command::env::list::EnvListEntry> =
        remote_entries.iter().map(|e| (e.name.clone(), e)).collect();
    // Update sync tags on existing local agents.
    for agent in &mut state.agents {
        if let Some(remote) = remote_map.get(&agent.name) {
            agent.sync_tag = remote.sync_tag;
            agent.remote_version = remote.remote_version.clone();
        }
    }
    // Add remote-only environments (not already in the local list).
    let local_names: std::collections::HashSet<String> =
        state.agents.iter().map(|a| a.name.clone()).collect();
    for entry in &remote_entries {
        if entry.sync_tag == ArtifactSyncTag::Remote && !local_names.contains(&entry.name) {
            state.agents.push(app::AgentEntry {
                name: entry.name.clone(),
                namespace: entry.namespace.clone(),
                component_count: 0,
                bot_name: entry.name.clone(),
                model: None,
                sync_tag: ArtifactSyncTag::Remote,
                local_version: None,
                remote_version: entry.remote_version.clone(),
            });
        }
    }
}

#[cfg(unix)]
fn redirect_stdio() -> (SavedStdio, Tty) {
    use std::os::fd::{AsRawFd, FromRawFd};
    let stdout_fd = std::io::stdout().as_raw_fd();
    let stderr_fd = std::io::stderr().as_raw_fd();
    let saved_out = unsafe { libc::dup(stdout_fd) };
    let saved_err = unsafe { libc::dup(stderr_fd) };
    if let Ok(devnull) = std::fs::File::open("/dev/null") {
        let null_fd = devnull.as_raw_fd();
        unsafe {
            libc::dup2(null_fd, stdout_fd);
            libc::dup2(null_fd, stderr_fd);
        }
    }
    let tty_fd = unsafe { libc::dup(saved_out) };
    let tty_file = unsafe { std::fs::File::from_raw_fd(tty_fd) };
    let tty: Tty = BufWriter::new(tty_file);
    (
        SavedStdio {
            out: saved_out,
            err: saved_err,
        },
        tty,
    )
}

#[cfg(unix)]
fn restore_stdio(saved: SavedStdio) {
    use std::os::fd::AsRawFd;
    let stdout_fd = std::io::stdout().as_raw_fd();
    let stderr_fd = std::io::stderr().as_raw_fd();
    unsafe {
        libc::dup2(saved.out, stdout_fd);
        libc::dup2(saved.err, stderr_fd);
        libc::close(saved.out);
        libc::close(saved.err);
    }
}

#[cfg(windows)]
unsafe extern "C" {
    fn _dup(fd: i32) -> i32;
    fn _dup2(fd1: i32, fd2: i32) -> i32;
    fn _close(fd: i32) -> i32;
    fn _get_osfhandle(fd: i32) -> isize;
    fn _open_osfhandle(handle: isize, flags: i32) -> i32;
}

#[cfg(windows)]
unsafe extern "system" {
    fn GetCurrentProcess() -> isize;
    fn DuplicateHandle(
        source_process: isize,
        source_handle: isize,
        target_process: isize,
        target_handle: *mut isize,
        desired_access: u32,
        inherit_handle: i32,
        options: u32,
    ) -> i32;
    fn SetStdHandle(std_handle: u32, handle: isize) -> i32;
}

#[cfg(windows)]
const STD_OUTPUT_HANDLE: u32 = 0xFFFF_FFF5; // -11
#[cfg(windows)]
const STD_ERROR_HANDLE: u32 = 0xFFFF_FFF4; // -12

#[cfg(windows)]
const DUPLICATE_SAME_ACCESS: u32 = 2;

#[cfg(windows)]
fn redirect_stdio() -> (SavedStdio, Tty) {
    use std::os::windows::io::{FromRawHandle, IntoRawHandle};
    unsafe {
        let saved_out = _dup(1);
        let saved_err = _dup(2);
        // Redirect stdout and stderr to NUL.
        // Must set both CRT fds (_dup2) AND Win32 handles (SetStdHandle)
        // because Rust's println! uses GetStdHandle, not the CRT fd.
        if let Ok(devnull) = std::fs::OpenOptions::new().write(true).open("NUL") {
            let null_handle = devnull.into_raw_handle();
            let null_fd = _open_osfhandle(null_handle as isize, 0);
            if null_fd != -1 {
                _dup2(null_fd, 1);
                _dup2(null_fd, 2);
                _close(null_fd);
                // Set Win32 handles using the dup'd fds (not null_fd which is now closed).
                SetStdHandle(STD_OUTPUT_HANDLE, _get_osfhandle(1));
                SetStdHandle(STD_ERROR_HANDLE, _get_osfhandle(2));
            }
        }
        // Duplicate the saved stdout handle for ratatui.
        let handle = _get_osfhandle(saved_out);
        let process = GetCurrentProcess();
        let mut tty_handle: isize = 0;
        DuplicateHandle(
            process,
            handle,
            process,
            &mut tty_handle,
            0,
            0,
            DUPLICATE_SAME_ACCESS,
        );
        let tty_file = std::fs::File::from_raw_handle(tty_handle as *mut std::ffi::c_void);
        let tty: Tty = BufWriter::new(tty_file);
        (
            SavedStdio {
                out: saved_out,
                err: saved_err,
            },
            tty,
        )
    }
}

#[cfg(windows)]
fn restore_stdio(saved: SavedStdio) {
    unsafe {
        _dup2(saved.out, 1);
        _dup2(saved.err, 2);
        SetStdHandle(STD_OUTPUT_HANDLE, _get_osfhandle(1));
        SetStdHandle(STD_ERROR_HANDLE, _get_osfhandle(2));
        _close(saved.out);
        _close(saved.err);
    }
}