This commit is contained in:
Ahmed Ibrahim
2026-01-05 19:38:23 -08:00
parent 284dc5c724
commit 91bc7fc5fd
9 changed files with 1075 additions and 8 deletions
+30 -3
View File
@@ -447,12 +447,24 @@ impl ChatWidget {
let initial_messages = event.initial_messages.clone();
let model_for_header = event.model.clone();
self.set_model(&model_for_header);
self.add_to_history(history_cell::new_session_info(
let session_info = history_cell::new_session_info(
&self.config,
&model_for_header,
event,
self.show_welcome_banner,
));
);
if self.active_cell.as_ref().is_some_and(|cell| {
cell.as_any()
.is::<history_cell::StartupSessionHeaderHistoryCell>()
}) {
// Replace the startup placeholder header ("model: loading") with the configured
// session header now that we know the selected model.
self.active_cell = Some(Box::new(session_info));
self.flush_active_cell();
} else {
self.add_to_history(session_info);
}
if let Some(messages) = initial_messages {
self.replay_initial_messages(messages);
}
@@ -929,6 +941,9 @@ impl ChatWidget {
.as_any()
.downcast_ref::<history_cell::UnifiedExecWaitCell>()
.is_none()
&& !active
.as_any()
.is::<history_cell::StartupSessionHeaderHistoryCell>()
);
if has_non_wait_active {
// Do not preempt non-wait active cells with a wait entry.
@@ -1447,6 +1462,7 @@ impl ChatWidget {
let mut rng = rand::rng();
let placeholder = EXAMPLE_PROMPTS[rng.random_range(0..EXAMPLE_PROMPTS.len())].to_string();
let codex_op_tx = spawn_agent(config.clone(), app_event_tx.clone(), conversation_manager);
let startup_dir = config.cwd.clone();
let mut widget = Self {
app_event_tx: app_event_tx.clone(),
@@ -1462,7 +1478,9 @@ impl ChatWidget {
animations_enabled: config.animations,
skills: None,
}),
active_cell: None,
active_cell: Some(Box::new(
history_cell::StartupSessionHeaderHistoryCell::new(startup_dir),
)),
config,
model: model.clone(),
auth_manager,
@@ -1886,6 +1904,15 @@ impl ChatWidget {
fn flush_active_cell(&mut self) {
self.flush_wait_cell();
if let Some(active) = self.active_cell.take() {
if active
.as_any()
.is::<history_cell::StartupSessionHeaderHistoryCell>()
{
// Startup header is a transient placeholder and should not be flushed into
// history. It will be replaced once SessionConfigured arrives.
self.active_cell = Some(active);
return;
}
self.needs_final_message_separator = true;
self.app_event_tx.send(AppEvent::InsertHistoryCell(active));
}
@@ -0,0 +1,20 @@
---
source: tui/src/chatwidget/tests.rs
assertion_line: 2618
expression: terminal.backend()
---
" "
"╭───────────────────────────────────────╮ "
"│ >_ OpenAI Codex (v0.0.0) │ "
"│ │ "
"│ model: loading /model to change │ "
"│ directory: ~/code/codex/codex-rs/tui │ "
"╰───────────────────────────────────────╯ "
" "
" "
"› Ask Codex to do anything "
" "
" 100% context left · ? for shortcuts "
" "
" "
" "
+67
View File
@@ -2600,6 +2600,73 @@ async fn ui_snapshots_small_heights_idle() {
}
}
#[tokio::test]
async fn startup_header_renders_in_active_cell_before_session_configured_snapshot() {
use ratatui::Terminal;
use ratatui::backend::TestBackend;
let (mut chat, _rx, _op_rx) = make_chatwidget_manual(None).await;
chat.active_cell = Some(Box::new(
crate::history_cell::StartupSessionHeaderHistoryCell::new(chat.config.cwd.clone()),
));
let mut terminal = Terminal::new(TestBackend::new(60, 15)).expect("create terminal");
terminal
.draw(|f| chat.render(f.area(), f.buffer_mut()))
.expect("draw startup header");
assert_snapshot!("startup_header_active_cell", terminal.backend());
}
#[tokio::test]
async fn startup_header_is_replaced_on_session_configured() {
let (mut chat, mut rx, mut op_rx) = make_chatwidget_manual(None).await;
chat.active_cell = Some(Box::new(
crate::history_cell::StartupSessionHeaderHistoryCell::new(chat.config.cwd.clone()),
));
chat.handle_codex_event(Event {
id: "session-1".into(),
msg: EventMsg::SessionConfigured(codex_core::protocol::SessionConfiguredEvent {
session_id: ConversationId::new(),
model: "gpt-5.2-codex".to_string(),
model_provider_id: "test-provider".to_string(),
approval_policy: codex_core::protocol::AskForApproval::OnRequest,
sandbox_policy: codex_core::protocol::SandboxPolicy::WorkspaceWrite {
writable_roots: Vec::new(),
network_access: false,
exclude_tmpdir_env_var: false,
exclude_slash_tmp: false,
},
cwd: chat.config.cwd.clone(),
reasoning_effort: None,
history_log_id: 0,
history_entry_count: 0,
initial_messages: None,
rollout_path: std::env::temp_dir(),
}),
});
assert!(
chat.active_cell.is_none(),
"startup header should be replaced and flushed into history"
);
let history = drain_insert_history(&mut rx);
let rendered = history
.iter()
.map(|lines| lines_to_single_string(lines))
.collect::<String>();
assert!(
rendered.contains("model:") && rendered.contains("gpt-5.2-codex"),
"expected configured model in history: {rendered}"
);
// Drain rx to avoid unused warnings.
let _ = drain_insert_history(&mut rx);
let _ = op_rx.try_recv();
}
// Snapshot test: ChatWidget at very small heights (task running)
// Validates how status + composer are presented within tight space.
#[tokio::test]
+43 -1
View File
@@ -874,6 +874,7 @@ pub(crate) fn new_session_info(
// Header box rendered as history (so it appears at the very top)
let header = SessionHeaderHistoryCell::new(
model.clone(),
Style::default(),
reasoning_effort,
config.cwd.clone(),
CODEX_CLI_VERSION,
@@ -942,6 +943,7 @@ pub(crate) fn new_user_prompt(message: String) -> UserHistoryCell {
struct SessionHeaderHistoryCell {
version: &'static str,
model: String,
model_style: Style,
reasoning_effort: Option<ReasoningEffortConfig>,
directory: PathBuf,
}
@@ -949,6 +951,7 @@ struct SessionHeaderHistoryCell {
impl SessionHeaderHistoryCell {
fn new(
model: String,
model_style: Style,
reasoning_effort: Option<ReasoningEffortConfig>,
directory: PathBuf,
version: &'static str,
@@ -956,6 +959,7 @@ impl SessionHeaderHistoryCell {
Self {
version,
model,
model_style,
reasoning_effort,
directory,
}
@@ -1028,7 +1032,7 @@ impl HistoryCell for SessionHeaderHistoryCell {
let reasoning_label = self.reasoning_label();
let mut model_spans: Vec<Span<'static>> = vec![
Span::from(format!("{model_label} ")).dim(),
Span::from(self.model.clone()),
Span::styled(self.model.clone(), self.model_style),
];
if let Some(reasoning) = reasoning_label {
model_spans.push(Span::from(" "));
@@ -1056,6 +1060,43 @@ impl HistoryCell for SessionHeaderHistoryCell {
}
}
#[derive(Debug)]
pub(crate) struct StartupSessionHeaderHistoryCell(SessionHeaderHistoryCell);
impl StartupSessionHeaderHistoryCell {
pub(crate) fn new(directory: PathBuf) -> Self {
// Render a session header before we know the configured session details.
// This is a transient placeholder until the SessionConfigured event arrives.
let inner = SessionHeaderHistoryCell::new(
"loading".to_string(),
Style::default().dim().italic(),
None,
directory,
CODEX_CLI_VERSION,
);
Self(inner)
}
}
impl HistoryCell for StartupSessionHeaderHistoryCell {
fn display_lines(&self, width: u16) -> Vec<Line<'static>> {
self.0.display_lines(width)
}
fn desired_height(&self, width: u16) -> u16 {
self.0.desired_height(width)
}
fn transcript_lines(&self, _width: u16) -> Vec<Line<'static>> {
// Do not include this placeholder in transcript/log output.
Vec::new()
}
fn desired_transcript_height(&self, _width: u16) -> u16 {
0
}
}
#[derive(Debug)]
pub(crate) struct CompositeHistoryCell {
parts: Vec<Box<dyn HistoryCell>>,
@@ -2200,6 +2241,7 @@ mod tests {
fn session_header_includes_reasoning_level_when_present() {
let cell = SessionHeaderHistoryCell::new(
"gpt-4o".to_string(),
Style::default(),
Some(ReasoningEffortConfig::High),
std::env::temp_dir(),
"test",