mirror of
https://github.com/openai/codex.git
synced 2026-11-15 21:50:46 +00:00
## What changed - Show a refusal status while preparing alternative suggestions and remove the pending notice above the composer. - Clarify that choosing a rewrite starts a new chat with earlier context, and update overview suggestions to explicitly say users can ask Codex. - Add an option to report safety-check concerns through the consent flow when reporting is enabled. ## Testing Update safety recourse snapshots and extend coverage for reporting consent, returning to the picker, and Daybreak notices when enabled or unavailable for the selected model. GitOrigin-RevId: 99f565ebec7064ec55d388e73acede2dfe639474
480 lines
18 KiB
Rust
480 lines
18 KiB
Rust
//! Shared composer and live-history composition for inline and owned transcript surfaces.
|
|
|
|
use super::provider_error::ProviderErrorNotice;
|
|
use super::transcript::ActiveCellLayoutCache;
|
|
use super::transcript::ActiveCellLayoutCacheKey;
|
|
use super::*;
|
|
use crate::keymap::KeymapContext;
|
|
use crate::render::RectExt;
|
|
use crate::terminal_hyperlinks::HyperlinkParagraph;
|
|
use crate::wrapping::RtOptions;
|
|
use crate::wrapping::word_wrap_lines;
|
|
use ratatui::style::Styled as _;
|
|
use ratatui::text::Span;
|
|
use ratatui::widgets::Block;
|
|
use std::cell::Cell;
|
|
|
|
struct ExternalWriterNotice {
|
|
agents_navigation_key_available: bool,
|
|
transcript_hint: Option<crate::key_hint::ShortcutHint>,
|
|
}
|
|
|
|
impl Renderable for ExternalWriterNotice {
|
|
fn render(&self, area: Rect, buf: &mut Buffer) {
|
|
let content_width = area.width.saturating_sub(/*rhs*/ 4);
|
|
let card_lines = self.card_lines(content_width);
|
|
let card_height = (card_lines.len() as u16).saturating_add(/*rhs*/ 2);
|
|
let card = Rect::new(area.x, area.y, area.width, card_height.min(area.height));
|
|
Widget::render(
|
|
Block::default().style(crate::style::user_message_style()),
|
|
card,
|
|
buf,
|
|
);
|
|
let content = card.inset(Insets::tlbr(
|
|
/*top*/ 1, /*left*/ 2, /*bottom*/ 1, /*right*/ 2,
|
|
));
|
|
Renderable::render(&Paragraph::new(card_lines), content, buf);
|
|
let footer_y = card.bottom();
|
|
if footer_y < area.bottom() {
|
|
let footer = Rect::new(
|
|
area.x.saturating_add(/*rhs*/ 2),
|
|
footer_y,
|
|
area.width.saturating_sub(/*rhs*/ 2),
|
|
area.bottom().saturating_sub(footer_y),
|
|
);
|
|
Renderable::render(
|
|
&Paragraph::new(self.footer_lines(footer.width)),
|
|
footer,
|
|
buf,
|
|
);
|
|
}
|
|
}
|
|
|
|
fn desired_height(&self, width: u16) -> u16 {
|
|
(self.card_lines(width.saturating_sub(/*rhs*/ 4)).len() as u16)
|
|
.saturating_add(/*rhs*/ 2)
|
|
.saturating_add(self.footer_lines(width.saturating_sub(/*rhs*/ 2)).len() as u16)
|
|
}
|
|
}
|
|
|
|
impl ExternalWriterNotice {
|
|
fn card_lines(&self, width: u16) -> Vec<Line<'static>> {
|
|
let title: Line<'static> = vec![
|
|
"🔒".into(),
|
|
" ".into(),
|
|
"This conversation is open in another app".bold(),
|
|
]
|
|
.into();
|
|
let retry: Line<'static> = vec![
|
|
Span::styled("r", crate::style::accent_style()),
|
|
" to retry".into(),
|
|
]
|
|
.into();
|
|
let mut lines = word_wrap_lines(&[title], usize::from(width));
|
|
if lines.len() == 1 && lines[0].width() + retry.width() + 2 <= usize::from(width) {
|
|
let gap = usize::from(width) - lines[0].width() - retry.width();
|
|
lines[0].spans.push(" ".repeat(gap).into());
|
|
lines[0].spans.extend(retry.spans);
|
|
} else {
|
|
lines.push(retry);
|
|
}
|
|
lines.extend(word_wrap_lines(
|
|
&[Line::from(
|
|
"Close it there and press R to continue here.".dim(),
|
|
)],
|
|
RtOptions::new(usize::from(width))
|
|
.initial_indent(" ".into())
|
|
.subsequent_indent(" ".into()),
|
|
));
|
|
lines
|
|
}
|
|
|
|
fn footer_lines(&self, width: u16) -> Vec<Line<'static>> {
|
|
let mut items = vec![
|
|
("r".to_string(), "retry".to_string()),
|
|
("f".to_string(), "fork".to_string()),
|
|
];
|
|
let escape = crate::key_hint::plain(KeyCode::Esc).display_label();
|
|
let quit_keys = [
|
|
crate::key_hint::ctrl(KeyCode::Char('c')).display_label(),
|
|
crate::key_hint::plain(KeyCode::Char('q')).display_label(),
|
|
];
|
|
let key = if self.agents_navigation_key_available {
|
|
let left = crate::key_hint::plain(KeyCode::Left).display_label();
|
|
format!("{left}/{escape}")
|
|
} else {
|
|
escape
|
|
};
|
|
items.push((key, "command center".to_string()));
|
|
items.push((quit_keys.join("/"), "exit".to_string()));
|
|
if let Some(hint) = self.transcript_hint {
|
|
items.push((hint.display_label(), "transcript".to_string()));
|
|
}
|
|
let mut spans = vec![" ".set_style(crate::style::footer_hint_label_style())];
|
|
for (idx, (key, label)) in items.into_iter().enumerate() {
|
|
if idx > 0 {
|
|
spans.push(" ".set_style(crate::style::footer_hint_label_style()));
|
|
}
|
|
spans.extend(crate::key_hint::key_label_spans(&key));
|
|
spans.push(format!(" {label}").set_style(crate::style::footer_hint_label_style()));
|
|
}
|
|
word_wrap_lines(&[Line::from(spans)], usize::from(width))
|
|
}
|
|
}
|
|
|
|
impl ChatWidget {
|
|
pub(crate) fn prepare_composer_images(&mut self, protocol: Option<crate::pets::ImageProtocol>) {
|
|
self.composer_images = protocol
|
|
.filter(|_| {
|
|
!self.bottom_pane.composer_local_images().is_empty()
|
|
&& self.no_modal_or_popup_active()
|
|
})
|
|
.and_then(|protocol| {
|
|
crate::transcript_image::composer::ComposerImages::for_terminal(
|
|
protocol,
|
|
self.composer_images.take(),
|
|
)
|
|
});
|
|
}
|
|
|
|
pub(crate) fn as_renderable(&self) -> RenderableItem<'_> {
|
|
let backdrop = self.as_backdrop_renderable();
|
|
match self.centered_dialog() {
|
|
Some(dialog) => RenderableItem::Owned(Box::new(crate::bottom_pane::DialogOverlay {
|
|
backdrop,
|
|
dialog,
|
|
})),
|
|
None => backdrop,
|
|
}
|
|
}
|
|
|
|
pub(crate) fn centered_dialog(&self) -> Option<crate::bottom_pane::CenteredView<'_>> {
|
|
self.bottom_pane.centered_dialog()
|
|
}
|
|
|
|
fn as_backdrop_renderable(&self) -> RenderableItem<'_> {
|
|
if self
|
|
.bottom_pane
|
|
.selected_index_for_active_view(crate::app::AGENTS_OVERVIEW_VIEW_ID)
|
|
.is_some()
|
|
|| (self.bottom_pane.has_centered_view()
|
|
&& self
|
|
.bottom_pane
|
|
.selected_index_for_present_view(crate::app::AGENTS_OVERVIEW_VIEW_ID)
|
|
.is_some())
|
|
{
|
|
return self
|
|
.bottom_pane_renderable(crate::bottom_pane::ComposerRenderOptions::default());
|
|
}
|
|
|
|
let active_cell_right_reserve = self.ambient_pet_wrap_reserved_cols();
|
|
let active_cell_renderable = match &self.transcript.active_cell {
|
|
Some(cell) => RenderableItem::Owned(Box::new(TranscriptAreaRenderable {
|
|
child: cell.as_ref(),
|
|
// The initial header becomes the first history cell, which has no leading separator.
|
|
top: if cell.as_any().is::<history_cell::SessionHeaderHistoryCell>() {
|
|
0
|
|
} else {
|
|
1
|
|
},
|
|
right: active_cell_right_reserve,
|
|
// Externally backed transcript cells can also change viewport height without an
|
|
// active-cell revision. Spinner cells remain safe because their indicator width
|
|
// is stable and their display lines are still rebuilt on every frame.
|
|
persistent_layout: cell.has_stable_transcript_height().then_some(
|
|
PersistentActiveCellLayout {
|
|
cache: &self.transcript.active_cell_layout,
|
|
cell_identity: cell.as_ref() as *const dyn HistoryCell as *const ()
|
|
as usize,
|
|
revision: self.transcript.active_cell_revision,
|
|
render_mode: self.history_render_mode(),
|
|
},
|
|
),
|
|
})),
|
|
None => RenderableItem::Owned(Box::new(())),
|
|
};
|
|
let mut flex = FlexRenderable::new();
|
|
flex.push(/*flex*/ 1, active_cell_renderable);
|
|
for cell in self
|
|
.realtime_conversation
|
|
.pending_history_cells
|
|
.iter()
|
|
.chain(self.realtime_conversation.live_transcript_cells())
|
|
{
|
|
flex.push(
|
|
/*flex*/ 1,
|
|
RenderableItem::Owned(Box::new(TranscriptAreaRenderable {
|
|
child: cell.as_ref(),
|
|
top: 1,
|
|
right: active_cell_right_reserve,
|
|
persistent_layout: None,
|
|
})),
|
|
);
|
|
}
|
|
|
|
if let Some(cell) = self.pending_rate_limit_reset_hint() {
|
|
flex.push(
|
|
/*flex*/ 1,
|
|
RenderableItem::Owned(Box::new(TranscriptAreaRenderable {
|
|
child: cell,
|
|
top: 1,
|
|
right: active_cell_right_reserve,
|
|
persistent_layout: None,
|
|
})),
|
|
);
|
|
}
|
|
flex.push(
|
|
/*flex*/ 0,
|
|
self.bottom_pane_renderable(crate::bottom_pane::ComposerRenderOptions::default())
|
|
.inset(Insets::tlbr(
|
|
/*top*/ 1, /*left*/ 0, /*bottom*/ 0, /*right*/ 0,
|
|
)),
|
|
);
|
|
RenderableItem::Owned(Box::new(flex))
|
|
}
|
|
|
|
/// Returns the composer, footer, and active modal without the live transcript above it.
|
|
///
|
|
/// Both transcript surfaces use this composition so read-only notices and cursor placement
|
|
/// remain consistent. Owned transcripts reserve their shared hint row above the composer.
|
|
pub(crate) fn bottom_pane_renderable<'a>(
|
|
&'a self,
|
|
mut options: crate::bottom_pane::ComposerRenderOptions<'a>,
|
|
) -> RenderableItem<'a> {
|
|
if self.fork_in_progress {
|
|
RenderableItem::Owned(Box::new(
|
|
Paragraph::new("Forking conversation…".dim()).inset(Insets::tlbr(
|
|
/*top*/ 1, /*left*/ 2, /*bottom*/ 1, /*right*/ 2,
|
|
)),
|
|
))
|
|
} else if let Some(error) = &self.provider_error
|
|
&& !self.bottom_pane.has_active_view()
|
|
{
|
|
let can_continue = self.thread_id.is_some() && self.current_rollout_path.is_some();
|
|
RenderableItem::Owned(Box::new(ProviderErrorNotice {
|
|
title: if can_continue
|
|
&& matches!(
|
|
error,
|
|
codex_app_server_protocol::ModelProviderError::ModelProviderMismatch { .. }
|
|
) {
|
|
"Continue in a new chat"
|
|
} else {
|
|
error.title()
|
|
},
|
|
message: self.provider_error_message(error),
|
|
can_continue,
|
|
configuration_location: self
|
|
.remote_connection
|
|
.as_ref()
|
|
.filter(|remote| !remote.is_local_daemon)
|
|
.map(|remote| format!("Configuration on {}", remote.address)),
|
|
}))
|
|
} else if self.external_writer_view && !self.bottom_pane.has_active_view() {
|
|
RenderableItem::Owned(Box::new(ExternalWriterNotice {
|
|
agents_navigation_key_available: self.agents_navigation_key_available(),
|
|
transcript_hint: self.bottom_pane.transcript_shortcut_hint(),
|
|
}))
|
|
} else {
|
|
let right_reserve = if self
|
|
.bottom_pane
|
|
.selected_index_for_active_view(crate::app::AGENTS_OVERVIEW_VIEW_ID)
|
|
.is_some()
|
|
{
|
|
0
|
|
} else {
|
|
self.ambient_pet_wrap_reserved_cols()
|
|
};
|
|
options.rendered_selection = Some(&self.rendered_selection);
|
|
options.image_previews = self.composer_images.as_ref();
|
|
options.warning_count = self.warning_display_state.count;
|
|
options.textarea_right_reserve = right_reserve;
|
|
options.separate_status_line = options.command_popup_placement
|
|
!= crate::bottom_pane::CommandPopupPlacement::AboveComposer;
|
|
let bottom_pane = self.bottom_pane.backdrop_with_options(options);
|
|
let above_composer = if self.quick_dial_menu && !options.floating_shortcuts {
|
|
RenderableItem::Owned(Box::new(self.quick_dial_hud()))
|
|
} else if let Some(backend) = self.visible_new_backend_control() {
|
|
let mut spans = vec![" ".into(), backend.into()];
|
|
let keymap = self.bottom_pane.keymap();
|
|
if let Some(key) = keymap.primary_hint(KeymapContext::Global, "change_host") {
|
|
spans.extend([" ".into(), key.into(), " to cycle".dim()]);
|
|
}
|
|
RenderableItem::Owned(Box::new(Paragraph::new(Line::from(spans))))
|
|
} else {
|
|
return bottom_pane;
|
|
};
|
|
let mut column = FlexRenderable::new();
|
|
column.push(/*flex*/ 1, above_composer);
|
|
column.push(/*flex*/ 0, bottom_pane);
|
|
RenderableItem::Owned(Box::new(column))
|
|
}
|
|
}
|
|
|
|
/// Returns compact live-history lines using the current rich or raw presentation.
|
|
#[cfg(test)]
|
|
pub(crate) fn active_cell_display_hyperlink_lines(
|
|
&self,
|
|
width: u16,
|
|
) -> Option<Vec<HyperlinkLine>> {
|
|
self.active_cell_hyperlink_lines_with(width, |cell, width| {
|
|
cell.display_hyperlink_lines_for_mode(width, self.history_render_mode())
|
|
})
|
|
}
|
|
|
|
/// Combines the same live cells for compact and detailed transcript rendering.
|
|
pub(super) fn active_cell_hyperlink_lines_with(
|
|
&self,
|
|
width: u16,
|
|
render_cell: impl Fn(&dyn HistoryCell, u16) -> Vec<HyperlinkLine>,
|
|
) -> Option<Vec<HyperlinkLine>> {
|
|
let cells = self
|
|
.transcript
|
|
.active_cell
|
|
.as_deref()
|
|
.into_iter()
|
|
.chain(
|
|
self.realtime_conversation
|
|
.pending_history_cells
|
|
.iter()
|
|
.chain(self.realtime_conversation.live_transcript_cells())
|
|
.map(AsRef::as_ref),
|
|
)
|
|
.chain(
|
|
self.pending_rate_limit_reset_hint()
|
|
.map(|cell| cell as &dyn HistoryCell),
|
|
);
|
|
let mut lines = Vec::new();
|
|
for cell in cells {
|
|
let cell_lines = render_cell(cell, width);
|
|
if !cell_lines.is_empty() && !lines.is_empty() {
|
|
lines.push(HyperlinkLine::from(""));
|
|
}
|
|
lines.extend(cell_lines);
|
|
}
|
|
(!lines.is_empty()).then_some(lines)
|
|
}
|
|
|
|
pub(crate) fn note_rendered_width(&self, width: u16) {
|
|
self.last_rendered_width.set(Some(width));
|
|
}
|
|
}
|
|
|
|
struct TranscriptAreaRenderable<'a> {
|
|
child: &'a dyn HistoryCell,
|
|
top: u16,
|
|
right: u16,
|
|
persistent_layout: Option<PersistentActiveCellLayout<'a>>,
|
|
}
|
|
|
|
struct PersistentActiveCellLayout<'a> {
|
|
cache: &'a Cell<Option<ActiveCellLayoutCache>>,
|
|
cell_identity: usize,
|
|
revision: u64,
|
|
render_mode: HistoryRenderMode,
|
|
}
|
|
|
|
impl Renderable for TranscriptAreaRenderable<'_> {
|
|
fn render(&self, area: Rect, buf: &mut Buffer) {
|
|
let area = self.child_area(area);
|
|
let lines = self.child.display_hyperlink_lines(area.width);
|
|
let paragraph = HyperlinkParagraph::new(&lines, Style::default());
|
|
let y = if area.height == 0 {
|
|
0
|
|
} else {
|
|
let rendered_height = if let Some((cache, mut layout)) = self.layout(area.width) {
|
|
if let Some(height) = layout.rendered_height {
|
|
height
|
|
} else {
|
|
let height = paragraph.line_count(area.width);
|
|
layout.rendered_height = Some(height);
|
|
cache.set(Some(layout));
|
|
height
|
|
}
|
|
} else {
|
|
paragraph.line_count(area.width)
|
|
};
|
|
let overflow = rendered_height.saturating_sub(usize::from(area.height));
|
|
u16::try_from(overflow).unwrap_or(u16::MAX)
|
|
};
|
|
Clear.render(area, buf);
|
|
paragraph.scroll(y).render(area, buf);
|
|
}
|
|
|
|
fn desired_height(&self, width: u16) -> u16 {
|
|
let child_width = width.saturating_sub(self.right).max(1);
|
|
let desired_height = if let Some((cache, mut layout)) = self.layout(child_width) {
|
|
if let Some(height) = layout.desired_height {
|
|
height
|
|
} else {
|
|
let height = HistoryCell::desired_height(self.child, child_width);
|
|
layout.desired_height = Some(height);
|
|
cache.set(Some(layout));
|
|
height
|
|
}
|
|
} else {
|
|
HistoryCell::desired_height(self.child, child_width)
|
|
};
|
|
desired_height + self.top
|
|
}
|
|
}
|
|
|
|
impl TranscriptAreaRenderable<'_> {
|
|
fn layout(
|
|
&self,
|
|
width: u16,
|
|
) -> Option<(&Cell<Option<ActiveCellLayoutCache>>, ActiveCellLayoutCache)> {
|
|
let persistent = self.persistent_layout.as_ref()?;
|
|
let key = ActiveCellLayoutCacheKey {
|
|
cell_identity: persistent.cell_identity,
|
|
revision: persistent.revision,
|
|
width,
|
|
render_mode: persistent.render_mode,
|
|
syntax_theme_revision: crate::render::highlight::syntax_theme_revision(),
|
|
};
|
|
let layout = persistent
|
|
.cache
|
|
.get()
|
|
.filter(|layout| layout.key == key)
|
|
.unwrap_or(ActiveCellLayoutCache {
|
|
key,
|
|
desired_height: None,
|
|
rendered_height: None,
|
|
});
|
|
Some((persistent.cache, layout))
|
|
}
|
|
|
|
fn child_area(&self, area: Rect) -> Rect {
|
|
let y = area.y.saturating_add(self.top);
|
|
let height = area.height.saturating_sub(self.top);
|
|
Rect::new(
|
|
area.x,
|
|
y,
|
|
area.width.saturating_sub(self.right).max(1),
|
|
height,
|
|
)
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
#[path = "rendering_tests.rs"]
|
|
mod tests;
|
|
|
|
impl Renderable for ChatWidget {
|
|
fn render(&self, area: Rect, buf: &mut Buffer) {
|
|
self.as_renderable().render(area, buf);
|
|
self.note_rendered_width(area.width);
|
|
}
|
|
|
|
fn desired_height(&self, width: u16) -> u16 {
|
|
self.as_renderable().desired_height(width)
|
|
}
|
|
|
|
fn cursor_pos(&self, area: Rect) -> Option<(u16, u16)> {
|
|
self.as_renderable().cursor_pos(area)
|
|
}
|
|
|
|
fn cursor_style(&self, area: Rect) -> crossterm::cursor::SetCursorStyle {
|
|
self.as_renderable().cursor_style(area)
|
|
}
|
|
}
|