Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 54 additions & 12 deletions codex-rs/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ use codex_utils_absolute_path::AbsolutePathBuf;
use codex_utils_cli::CliConfigOverrides;
use codex_utils_cli::ProfileV2Name;
use codex_utils_cli::SharedCliOptions;
use codex_utils_cli::resume_hint;
use owo_colors::OwoColorize;
use std::io::IsTerminal;
use std::io::Write;
use std::path::PathBuf;
use supports_color::Stream;

Expand Down Expand Up @@ -684,10 +684,11 @@ fn parse_socket_path(raw: &str) -> Result<AbsolutePathBuf, String> {
}

fn format_exit_messages(exit_info: AppExitInfo, color_enabled: bool) -> Vec<String> {
let is_fatal = matches!(&exit_info.exit_reason, ExitReason::Fatal(_));
let AppExitInfo {
token_usage,
thread_id: conversation_id,
thread_name,
resume_hint,
..
} = exit_info;

Expand All @@ -696,33 +697,39 @@ fn format_exit_messages(exit_info: AppExitInfo, color_enabled: bool) -> Vec<Stri
lines.push(token_usage.to_string());
}

if let Some(resume_cmd) = resume_hint(thread_name.as_deref(), conversation_id) {
if let Some(resume_cmd) = resume_hint {
let command = if color_enabled {
resume_cmd.cyan().to_string()
} else {
resume_cmd
};
lines.push(format!("To continue this session, run {command}"));
} else if is_fatal && let Some(conversation_id) = conversation_id {
lines.push(format!("Session ID: {conversation_id}"));
}

lines
}

/// Handle the app exit and print the results. Optionally run the update action.
fn handle_app_exit(exit_info: AppExitInfo) -> anyhow::Result<()> {
match exit_info.exit_reason {
let is_fatal = match &exit_info.exit_reason {
ExitReason::Fatal(message) => {
eprintln!("ERROR: {message}");
std::process::exit(1);
true
}
ExitReason::UserRequested => { /* normal exit */ }
}
ExitReason::UserRequested => false,
};

let update_action = exit_info.update_action;
let color_enabled = supports_color::on(Stream::Stdout).is_some();
for line in format_exit_messages(exit_info, color_enabled) {
Comment thread
etraut-openai marked this conversation as resolved.
println!("{line}");
}
if is_fatal {
std::io::stdout().flush()?;
std::process::exit(1);
}
if let Some(action) = update_action {
run_update_action(action)?;
}
Expand Down Expand Up @@ -2977,12 +2984,13 @@ mod tests {
total_tokens: 2,
..Default::default()
};
let thread_id = conversation_id
.map(ThreadId::from_string)
.map(Result::unwrap);
AppExitInfo {
token_usage,
thread_id: conversation_id
.map(ThreadId::from_string)
.map(Result::unwrap),
thread_name: thread_name.map(str::to_string),
thread_id,
resume_hint: codex_utils_cli::resume_hint(thread_name, thread_id),
update_action: None,
exit_reason: ExitReason::UserRequested,
}
Expand All @@ -2993,14 +3001,48 @@ mod tests {
let exit_info = AppExitInfo {
token_usage: TokenUsage::default(),
thread_id: None,
thread_name: None,
resume_hint: None,
update_action: None,
exit_reason: ExitReason::UserRequested,
};
let lines = format_exit_messages(exit_info, /*color_enabled*/ false);
assert!(lines.is_empty());
}

#[test]
fn format_exit_messages_includes_session_id_for_fatal_exit_without_resume_hint() {
let exit_info = AppExitInfo {
token_usage: TokenUsage::default(),
thread_id: Some(ThreadId::from_string("123e4567-e89b-12d3-a456-426614174000").unwrap()),
resume_hint: None,
update_action: None,
exit_reason: ExitReason::Fatal("boom".to_string()),
};
let lines = format_exit_messages(exit_info, /*color_enabled*/ false);
assert_eq!(
lines,
vec!["Session ID: 123e4567-e89b-12d3-a456-426614174000".to_string()]
);
}

#[test]
fn format_exit_messages_includes_resume_hint_for_fatal_exit() {
let mut exit_info = sample_exit_info(
Some("123e4567-e89b-12d3-a456-426614174000"),
/*thread_name*/ None,
);
exit_info.exit_reason = ExitReason::Fatal("boom".to_string());
let lines = format_exit_messages(exit_info, /*color_enabled*/ false);
assert_eq!(
lines,
vec![
"Token usage: total=2 input=0 output=2".to_string(),
"To continue this session, run codex resume 123e4567-e89b-12d3-a456-426614174000"
.to_string(),
]
);
}

#[test]
fn format_exit_messages_includes_resume_hint_without_color() {
let exit_info = sample_exit_info(
Expand Down
29 changes: 18 additions & 11 deletions codex-rs/tui/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ const COMMIT_ANIMATION_TICK: Duration = tui::TARGET_FRAME_INTERVAL;
pub struct AppExitInfo {
pub token_usage: TokenUsage,
pub thread_id: Option<ThreadId>,
pub thread_name: Option<String>,
pub resume_hint: Option<String>,
pub update_action: Option<UpdateAction>,
pub exit_reason: ExitReason,
}
Expand All @@ -411,7 +411,7 @@ impl AppExitInfo {
Self {
token_usage: TokenUsage::default(),
thread_id: None,
thread_name: None,
resume_hint: None,
update_action: None,
exit_reason: ExitReason::Fatal(message.into()),
}
Expand All @@ -437,10 +437,7 @@ fn session_summary(
rollout_path: Option<&Path>,
) -> Option<SessionSummary> {
let usage_line = (!token_usage.is_zero()).then(|| token_usage.to_string());
let resumable_thread = resumable_thread(thread_id, thread_name, rollout_path);
let resume_hint = resumable_thread.as_ref().and_then(|thread| {
codex_utils_cli::resume_hint(thread.thread_name.as_deref(), Some(thread.thread_id))
});
let resume_hint = resume_hint_for_resumable_thread(thread_id, thread_name, rollout_path);

if usage_line.is_none() && resume_hint.is_none() {
return None;
Expand Down Expand Up @@ -471,6 +468,15 @@ fn resumable_thread(
})
}

fn resume_hint_for_resumable_thread(
thread_id: Option<ThreadId>,
thread_name: Option<String>,
rollout_path: Option<&Path>,
) -> Option<String> {
let thread = resumable_thread(thread_id, thread_name, rollout_path)?;
codex_utils_cli::resume_hint(thread.thread_name.as_deref(), Some(thread.thread_id))
}

fn rollout_path_is_resumable(rollout_path: &Path) -> bool {
std::fs::metadata(rollout_path).is_ok_and(|metadata| metadata.is_file() && metadata.len() > 0)
}
Expand Down Expand Up @@ -813,7 +819,7 @@ impl App {
return Ok(AppExitInfo {
token_usage: TokenUsage::default(),
thread_id: None,
thread_name: None,
resume_hint: None,
update_action: None,
exit_reason: ExitReason::UserRequested,
});
Expand Down Expand Up @@ -1258,15 +1264,16 @@ See the Codex keymap documentation for supported actions and examples."
return Err(err);
}
};
let resumable_thread = resumable_thread(
app.chat_widget.thread_id(),
let thread_id = app.chat_widget.thread_id().or(app.primary_thread_id);
let resume_hint = resume_hint_for_resumable_thread(
thread_id,
app.chat_widget.thread_name(),
app.chat_widget.rollout_path().as_deref(),
);
Ok(AppExitInfo {
token_usage: app.token_usage(),
thread_id: resumable_thread.as_ref().map(|thread| thread.thread_id),
thread_name: resumable_thread.and_then(|thread| thread.thread_name),
thread_id,
resume_hint,
update_action: app.pending_update_action,
exit_reason,
})
Expand Down
2 changes: 1 addition & 1 deletion codex-rs/tui/src/app/startup_prompts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ pub(super) async fn handle_model_migration_prompt_if_needed(
return Some(AppExitInfo {
token_usage: TokenUsage::default(),
thread_id: None,
thread_name: None,
resume_hint: None,
update_action: None,
exit_reason: ExitReason::UserRequested,
});
Expand Down
12 changes: 6 additions & 6 deletions codex-rs/tui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1382,7 +1382,7 @@ async fn run_ratatui_app(
return Ok(AppExitInfo {
token_usage: crate::token_usage::TokenUsage::default(),
thread_id: None,
thread_name: None,
resume_hint: None,
update_action: Some(action),
exit_reason: ExitReason::UserRequested,
});
Expand Down Expand Up @@ -1475,7 +1475,7 @@ async fn run_ratatui_app(
return Ok(AppExitInfo {
token_usage: crate::token_usage::TokenUsage::default(),
thread_id: None,
thread_name: None,
resume_hint: None,
update_action: None,
exit_reason: ExitReason::UserRequested,
});
Expand Down Expand Up @@ -1525,7 +1525,7 @@ async fn run_ratatui_app(
Ok(AppExitInfo {
token_usage: crate::token_usage::TokenUsage::default(),
thread_id: None,
thread_name: None,
resume_hint: None,
update_action: None,
exit_reason: ExitReason::Fatal(format!(
"No saved session found with ID {id_str}. Run `codex {action}` without an ID to choose from existing sessions."
Expand Down Expand Up @@ -1582,7 +1582,7 @@ async fn run_ratatui_app(
return Ok(AppExitInfo {
token_usage: crate::token_usage::TokenUsage::default(),
thread_id: None,
thread_name: None,
resume_hint: None,
update_action: None,
exit_reason: ExitReason::UserRequested,
});
Expand Down Expand Up @@ -1643,7 +1643,7 @@ async fn run_ratatui_app(
return Ok(AppExitInfo {
token_usage: crate::token_usage::TokenUsage::default(),
thread_id: None,
thread_name: None,
resume_hint: None,
update_action: None,
exit_reason: ExitReason::UserRequested,
});
Expand Down Expand Up @@ -1688,7 +1688,7 @@ async fn run_ratatui_app(
return Ok(AppExitInfo {
token_usage: crate::token_usage::TokenUsage::default(),
thread_id: None,
thread_name: None,
resume_hint: None,
update_action: None,
exit_reason: ExitReason::UserRequested,
});
Expand Down
21 changes: 14 additions & 7 deletions codex-rs/tui/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ use codex_tui::Cli;
use codex_tui::ExitReason;
use codex_tui::run_main;
use codex_utils_cli::CliConfigOverrides;
use codex_utils_cli::resume_hint;
use std::io::Write;
use supports_color::Stream;

fn format_exit_messages(exit_info: AppExitInfo, color_enabled: bool) -> Vec<String> {
let is_fatal = matches!(&exit_info.exit_reason, ExitReason::Fatal(_));
let AppExitInfo {
token_usage,
thread_id,
thread_name,
resume_hint,
..
} = exit_info;

Expand All @@ -23,13 +24,15 @@ fn format_exit_messages(exit_info: AppExitInfo, color_enabled: bool) -> Vec<Stri
lines.push(token_usage.to_string());
}

if let Some(resume_cmd) = resume_hint(thread_name.as_deref(), thread_id) {
if let Some(resume_cmd) = resume_hint {
let command = if color_enabled {
format!("\u{1b}[36m{resume_cmd}\u{1b}[39m")
} else {
resume_cmd
};
lines.push(format!("To continue this session, run {command}"));
} else if is_fatal && let Some(thread_id) = thread_id {
lines.push(format!("Session ID: {thread_id}"));
}

lines
Expand Down Expand Up @@ -59,18 +62,22 @@ fn main() -> anyhow::Result<()> {
/*explicit_remote_endpoint*/ None,
)
.await?;
match exit_info.exit_reason {
let is_fatal = match &exit_info.exit_reason {
ExitReason::Fatal(message) => {
eprintln!("ERROR: {message}");
std::process::exit(1);
true
}
ExitReason::UserRequested => {}
}
ExitReason::UserRequested => false,
};

let color_enabled = supports_color::on(Stream::Stdout).is_some();
for line in format_exit_messages(exit_info, color_enabled) {
println!("{line}");
}
if is_fatal {
std::io::stdout().flush()?;
std::process::exit(1);
}
Ok(())
})
}
Loading