Skip to main content

codetether_agent/tui/app/
session_loader.rs

1use crate::session::Session;
2use crate::tui::app::resume_window::session_resume_window;
3use anyhow::Result;
4
5pub struct LoadedSession {
6    pub session: Session,
7    pub dropped: usize,
8    pub file_bytes: u64,
9}
10
11pub async fn load_session_for_tui(id: &str) -> Result<LoadedSession> {
12    match Session::load_tail(id, session_resume_window()).await {
13        Ok(load) => Ok(LoadedSession {
14            session: load.session,
15            dropped: load.dropped,
16            file_bytes: load.file_bytes,
17        }),
18        Err(native_error) => load_codex(id, native_error).await,
19    }
20}
21
22async fn load_codex(id: &str, native_error: anyhow::Error) -> Result<LoadedSession> {
23    match crate::session::load_or_import_session(id).await {
24        Ok(session) => Ok(LoadedSession {
25            session,
26            dropped: 0,
27            file_bytes: 0,
28        }),
29        Err(codex_error) => {
30            Err(native_error.context(format!("Codex import also failed: {codex_error}")))
31        }
32    }
33}