Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions codex-rs/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions codex-rs/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,11 @@ async fn cli_main(codex_linux_sandbox_exe: Option<PathBuf>) -> anyhow::Result<()
root_config_overrides.clone(),
);
let usage = codex_tui::run_main(interactive, codex_linux_sandbox_exe).await?;
if !usage.is_zero() {
println!("{}", codex_core::protocol::FinalOutput::from(usage));
if !usage.token_usage.is_zero() {
println!(
"{}",
codex_core::protocol::FinalOutput::from(usage.token_usage)
);
}
}
Some(Subcommand::Exec(mut exec_cli)) => {
Expand Down
1 change: 1 addition & 0 deletions codex-rs/tui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ unicode-segmentation = "1.12.0"
unicode-width = "0.1"
url = "2"
pathdiff = "0.2"
owo-colors = "4.2.0"

[target.'cfg(unix)'.dependencies]
libc = "0.2"
Expand Down
14 changes: 12 additions & 2 deletions codex-rs/tui/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use codex_core::config::persist_model_selection;
use codex_core::model_family::find_family_for_model;
use codex_core::protocol::TokenUsage;
use codex_core::protocol_config_types::ReasoningEffort as ReasoningEffortConfig;
use codex_protocol::mcp_protocol::ConversationId;
use color_eyre::eyre::Result;
use color_eyre::eyre::WrapErr;
use crossterm::event::KeyCode;
Expand All @@ -33,6 +34,12 @@ use tokio::select;
use tokio::sync::mpsc::unbounded_channel;
// use uuid::Uuid;

#[derive(Debug, Clone)]
pub struct AppExitInfo {
pub token_usage: TokenUsage,
pub conversation_id: Option<ConversationId>,
}

pub(crate) struct App {
pub(crate) server: Arc<ConversationManager>,
pub(crate) app_event_tx: AppEventSender,
Expand Down Expand Up @@ -70,7 +77,7 @@ impl App {
initial_prompt: Option<String>,
initial_images: Vec<PathBuf>,
resume_selection: ResumeSelection,
) -> Result<TokenUsage> {
) -> Result<AppExitInfo> {
use tokio_stream::StreamExt;
let (app_event_tx, mut app_event_rx) = unbounded_channel();
let app_event_tx = AppEventSender::new(app_event_tx);
Expand Down Expand Up @@ -153,7 +160,10 @@ impl App {
}
} {}
tui.terminal.clear()?;
Ok(app.token_usage())
Ok(AppExitInfo {
token_usage: app.token_usage(),
conversation_id: app.chat_widget.conversation_id(),
})
}

pub(crate) async fn handle_tui_event(
Expand Down
10 changes: 7 additions & 3 deletions codex-rs/tui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#![deny(clippy::print_stdout, clippy::print_stderr)]
#![deny(clippy::disallowed_methods)]
use app::App;
pub use app::AppExitInfo;
use codex_core::AuthManager;
use codex_core::BUILT_IN_OSS_MODEL_PROVIDER_ID;
use codex_core::CodexAuth;
Expand Down Expand Up @@ -85,7 +86,7 @@ use codex_core::internal_storage::InternalStorage;
pub async fn run_main(
cli: Cli,
codex_linux_sandbox_exe: Option<PathBuf>,
) -> std::io::Result<codex_core::protocol::TokenUsage> {
) -> std::io::Result<AppExitInfo> {
let (sandbox_mode, approval_policy) = if cli.full_auto {
(
Some(SandboxMode::WorkspaceWrite),
Expand Down Expand Up @@ -257,7 +258,7 @@ async fn run_ratatui_app(
mut internal_storage: InternalStorage,
active_profile: Option<String>,
should_show_trust_screen: bool,
) -> color_eyre::Result<codex_core::protocol::TokenUsage> {
) -> color_eyre::Result<AppExitInfo> {
let mut config = config;
color_eyre::install()?;

Expand Down Expand Up @@ -369,7 +370,10 @@ async fn run_ratatui_app(
resume_picker::ResumeSelection::Exit => {
restore();
session_log::log_session_end();
return Ok(codex_core::protocol::TokenUsage::default());
return Ok(AppExitInfo {
token_usage: codex_core::protocol::TokenUsage::default(),
conversation_id: None,
});
}
other => other,
}
Expand Down
20 changes: 17 additions & 3 deletions codex-rs/tui/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use codex_arg0::arg0_dispatch_or_else;
use codex_common::CliConfigOverrides;
use codex_tui::Cli;
use codex_tui::run_main;
use owo_colors::OwoColorize;
use supports_color::Stream;

#[derive(Parser, Debug)]
struct TopCli {
Expand All @@ -21,9 +23,21 @@ fn main() -> anyhow::Result<()> {
.config_overrides
.raw_overrides
.splice(0..0, top_cli.config_overrides.raw_overrides);
let usage = run_main(inner, codex_linux_sandbox_exe).await?;
if !usage.is_zero() {
println!("{}", codex_core::protocol::FinalOutput::from(usage));
let exit_info = run_main(inner, codex_linux_sandbox_exe).await?;
let token_usage = exit_info.token_usage;
let conversation_id = exit_info.conversation_id;
if !token_usage.is_zero() {
println!("{}", codex_core::protocol::FinalOutput::from(token_usage),);
if let Some(session_id) = conversation_id {
let command = format!("codex resume {session_id}");
let prefix = "To continue this session, run ";
let suffix = ".";
if supports_color::on(Stream::Stdout).is_some() {
println!("{}{}{}", prefix, command.cyan(), suffix);
} else {
println!("{prefix}{command}{suffix}");
}
}
}
Ok(())
})
Expand Down
Loading