2025-05-14 10:13:29 -07:00
|
|
|
//! Bottom pane: shows the ChatComposer or a BottomPaneView, if one is active.
|
2025-08-22 18:05:43 +01:00
|
|
|
use std::path::PathBuf;
|
2025-05-14 10:13:29 -07:00
|
|
|
|
2025-06-28 15:04:23 -07:00
|
|
|
use crate::app_event_sender::AppEventSender;
|
2025-08-20 13:47:24 -07:00
|
|
|
use crate::tui::FrameRequester;
|
2025-06-28 15:04:23 -07:00
|
|
|
use crate::user_approval_widget::ApprovalRequest;
|
2025-05-14 10:13:29 -07:00
|
|
|
use bottom_pane_view::BottomPaneView;
|
2025-09-06 08:19:23 -07:00
|
|
|
use codex_core::protocol::TokenUsageInfo;
|
2025-06-28 15:04:23 -07:00
|
|
|
use codex_file_search::FileMatch;
|
2025-05-14 10:13:29 -07:00
|
|
|
use crossterm::event::KeyEvent;
|
|
|
|
|
use ratatui::buffer::Buffer;
|
2025-08-22 16:32:31 -07:00
|
|
|
use ratatui::layout::Constraint;
|
|
|
|
|
use ratatui::layout::Layout;
|
2025-05-14 10:13:29 -07:00
|
|
|
use ratatui::layout::Rect;
|
|
|
|
|
use ratatui::widgets::WidgetRef;
|
2025-08-28 12:54:12 -07:00
|
|
|
use std::time::Duration;
|
2025-05-14 10:13:29 -07:00
|
|
|
|
|
|
|
|
mod approval_modal_view;
|
|
|
|
|
mod bottom_pane_view;
|
|
|
|
|
mod chat_composer;
|
feat: record messages from user in ~/.codex/history.jsonl (#939)
This is a large change to support a "history" feature like you would
expect in a shell like Bash.
History events are recorded in `$CODEX_HOME/history.jsonl`. Because it
is a JSONL file, it is straightforward to append new entries (as opposed
to the TypeScript file that uses `$CODEX_HOME/history.json`, so to be
valid JSON, each new entry entails rewriting the entire file). Because
it is possible for there to be multiple instances of Codex CLI writing
to `history.jsonl` at once, we use advisory file locking when working
with `history.jsonl` in `codex-rs/core/src/message_history.rs`.
Because we believe history is a sufficiently useful feature, we enable
it by default. Though to provide some safety, we set the file
permissions of `history.jsonl` to be `o600` so that other users on the
system cannot read the user's history. We do not yet support a default
list of `SENSITIVE_PATTERNS` as the TypeScript CLI does:
https://github.com/openai/codex/blob/3fdf9df1335ac9501e3fb0e61715359145711e8b/codex-cli/src/utils/storage/command-history.ts#L10-L17
We are going to take a more conservative approach to this list in the
Rust CLI. For example, while `/\b[A-Za-z0-9-_]{20,}\b/` might exclude
sensitive information like API tokens, it would also exclude valuable
information such as references to Git commits.
As noted in the updated documentation, users can opt-out of history by
adding the following to `config.toml`:
```toml
[history]
persistence = "none"
```
Because `history.jsonl` could, in theory, be quite large, we take a[n
arguably overly pedantic] approach in reading history entries into
memory. Specifically, we start by telling the client the current number
of entries in the history file (`history_entry_count`) as well as the
inode (`history_log_id`) of `history.jsonl` (see the new fields on
`SessionConfiguredEvent`).
The client is responsible for keeping new entries in memory to create a
"local history," but if the user hits up enough times to go "past" the
end of local history, then the client should use the new
`GetHistoryEntryRequest` in the protocol to fetch older entries.
Specifically, it should pass the `history_log_id` it was given
originally and work backwards from `history_entry_count`. (It should
really fetch history in batches rather than one-at-a-time, but that is
something we can improve upon in subsequent PRs.)
The motivation behind this crazy scheme is that it is designed to defend
against:
* The `history.jsonl` being truncated during the session such that the
index into the history is no longer consistent with what had been read
up to that point. We do not yet have logic to enforce a `max_bytes` for
`history.jsonl`, but once we do, we will aspire to implement it in a way
that should result in a new inode for the file on most systems.
* New items from concurrent Codex CLI sessions amending to the history.
Because, in absence of truncation, `history.jsonl` is an append-only
log, so long as the client reads backwards from `history_entry_count`,
it should always get a consistent view of history. (That said, it will
not be able to read _new_ commands from concurrent sessions, but perhaps
we will introduce a `/` command to reload latest history or something
down the road.)
Admittedly, my testing of this feature thus far has been fairly light. I
expect we will find bugs and introduce enhancements/fixes going forward.
2025-05-15 16:26:23 -07:00
|
|
|
mod chat_composer_history;
|
feat: add support for commands in the Rust TUI (#935)
Introduces support for slash commands like in the TypeScript CLI. We do
not support the full set of commands yet, but the core abstraction is
there now.
In particular, we have a `SlashCommand` enum and due to thoughtful use
of the [strum](https://crates.io/crates/strum) crate, it requires
minimal boilerplate to add a new command to the list.
The key new piece of UI is `CommandPopup`, though the keyboard events
are still handled by `ChatComposer`. The behavior is roughly as follows:
* if the first character in the composer is `/`, the command popup is
displayed (if you really want to send a message to Codex that starts
with a `/`, simply put a space before the `/`)
* while the popup is displayed, up/down can be used to change the
selection of the popup
* if there is a selection, hitting tab completes the command, but does
not send it
* if there is a selection, hitting enter sends the command
* if the prefix of the composer matches a command, the command will be
visible in the popup so the user can see the description (commands could
take arguments, so additional text may appear after the command name
itself)
https://github.com/user-attachments/assets/39c3e6ee-eeb7-4ef7-a911-466d8184975f
Incidentally, Codex wrote almost all the code for this PR!
2025-05-14 12:55:49 -07:00
|
|
|
mod command_popup;
|
feat: add support for @ to do file search (#1401)
Introduces support for `@` to trigger a fuzzy-filename search in the
composer. Under the hood, this leverages
https://crates.io/crates/nucleo-matcher to do the fuzzy matching and
https://crates.io/crates/ignore to build up the list of file candidates
(so that it respects `.gitignore`).
For simplicity (at least for now), we do not do any caching between
searches like VS Code does for its file search:
https://github.com/microsoft/vscode/blob/1d89ed699b2e924d418c856318a3e12bca67ff3a/src/vs/workbench/services/search/node/rawSearchService.ts#L212-L218
Because we do not do any caching, I saw queries take up to three seconds
on large repositories with hundreds of thousands of files. To that end,
we do not perform searches synchronously on each keystroke, but instead
dispatch an event to do the search on a background thread that
asynchronously reports back to the UI when the results are available.
This is largely handled by the `FileSearchManager` introduced in this
PR, which also has logic for debouncing requests so there is at most one
search in flight at a time.
While we could potentially polish and tune this feature further, it may
already be overengineered for how it will be used, in practice, so we
can improve things going forward if it turns out that this is not "good
enough" in the wild.
Note this feature does not work like `@` in the TypeScript CLI, which
was more like directory-based tab completion. In the Rust CLI, `@`
triggers a full-repo fuzzy-filename search.
Fixes https://github.com/openai/codex/issues/1261.
2025-06-28 13:47:42 -07:00
|
|
|
mod file_search_popup;
|
2025-08-19 10:55:07 -07:00
|
|
|
mod list_selection_view;
|
2025-08-28 12:54:12 -07:00
|
|
|
mod paste_burst;
|
2025-08-06 21:23:09 -07:00
|
|
|
mod popup_consts;
|
|
|
|
|
mod scroll_state;
|
|
|
|
|
mod selection_popup_common;
|
2025-08-03 11:31:35 -07:00
|
|
|
mod textarea;
|
2025-05-14 10:13:29 -07:00
|
|
|
|
2025-07-28 12:00:06 -07:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
|
|
|
pub(crate) enum CancellationEvent {
|
|
|
|
|
Handled,
|
2025-09-07 20:21:53 -07:00
|
|
|
NotHandled,
|
2025-07-28 12:00:06 -07:00
|
|
|
}
|
|
|
|
|
|
2025-05-14 10:13:29 -07:00
|
|
|
pub(crate) use chat_composer::ChatComposer;
|
|
|
|
|
pub(crate) use chat_composer::InputResult;
|
2025-08-28 19:16:39 -07:00
|
|
|
use codex_protocol::custom_prompts::CustomPrompt;
|
2025-05-14 10:13:29 -07:00
|
|
|
|
2025-08-25 14:38:38 -07:00
|
|
|
use crate::status_indicator_widget::StatusIndicatorWidget;
|
2025-05-14 10:13:29 -07:00
|
|
|
use approval_modal_view::ApprovalModalView;
|
2025-08-19 10:55:07 -07:00
|
|
|
pub(crate) use list_selection_view::SelectionAction;
|
|
|
|
|
pub(crate) use list_selection_view::SelectionItem;
|
2025-05-14 10:13:29 -07:00
|
|
|
|
|
|
|
|
/// Pane displayed in the lower half of the chat UI.
|
2025-08-20 13:47:24 -07:00
|
|
|
pub(crate) struct BottomPane {
|
2025-05-14 10:13:29 -07:00
|
|
|
/// Composer is retained even when a BottomPaneView is displayed so the
|
|
|
|
|
/// input state is retained when the view is closed.
|
2025-08-03 11:31:35 -07:00
|
|
|
composer: ChatComposer,
|
2025-05-14 10:13:29 -07:00
|
|
|
|
2025-08-25 14:38:38 -07:00
|
|
|
/// If present, this is displayed instead of the `composer` (e.g. modals).
|
2025-08-20 13:47:24 -07:00
|
|
|
active_view: Option<Box<dyn BottomPaneView>>,
|
2025-05-14 10:13:29 -07:00
|
|
|
|
2025-05-15 14:50:30 -07:00
|
|
|
app_event_tx: AppEventSender,
|
2025-08-20 13:47:24 -07:00
|
|
|
frame_requester: FrameRequester,
|
|
|
|
|
|
2025-05-14 10:13:29 -07:00
|
|
|
has_input_focus: bool,
|
|
|
|
|
is_task_running: bool,
|
2025-06-27 13:37:11 -04:00
|
|
|
ctrl_c_quit_hint: bool,
|
2025-08-23 23:23:15 -07:00
|
|
|
esc_backtrack_hint: bool,
|
2025-08-04 21:23:22 -07:00
|
|
|
|
2025-08-25 14:38:38 -07:00
|
|
|
/// Inline status indicator shown above the composer while a task is running.
|
|
|
|
|
status: Option<StatusIndicatorWidget>,
|
|
|
|
|
/// Queued user messages to show under the status indicator.
|
|
|
|
|
queued_user_messages: Vec<String>,
|
2025-05-14 10:13:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(crate) struct BottomPaneParams {
|
2025-05-15 14:50:30 -07:00
|
|
|
pub(crate) app_event_tx: AppEventSender,
|
2025-08-20 13:47:24 -07:00
|
|
|
pub(crate) frame_requester: FrameRequester,
|
2025-05-14 10:13:29 -07:00
|
|
|
pub(crate) has_input_focus: bool,
|
2025-07-31 17:30:44 -07:00
|
|
|
pub(crate) enhanced_keys_supported: bool,
|
2025-08-15 22:37:10 -04:00
|
|
|
pub(crate) placeholder_text: String,
|
2025-08-28 12:54:12 -07:00
|
|
|
pub(crate) disable_paste_burst: bool,
|
2025-05-14 10:13:29 -07:00
|
|
|
}
|
|
|
|
|
|
2025-08-20 13:47:24 -07:00
|
|
|
impl BottomPane {
|
2025-08-25 22:47:26 -07:00
|
|
|
const BOTTOM_PAD_LINES: u16 = 1;
|
2025-05-14 10:13:29 -07:00
|
|
|
pub fn new(params: BottomPaneParams) -> Self {
|
2025-07-31 17:30:44 -07:00
|
|
|
let enhanced_keys_supported = params.enhanced_keys_supported;
|
2025-05-14 10:13:29 -07:00
|
|
|
Self {
|
2025-07-31 17:30:44 -07:00
|
|
|
composer: ChatComposer::new(
|
|
|
|
|
params.has_input_focus,
|
|
|
|
|
params.app_event_tx.clone(),
|
|
|
|
|
enhanced_keys_supported,
|
2025-08-15 22:37:10 -04:00
|
|
|
params.placeholder_text,
|
2025-08-28 12:54:12 -07:00
|
|
|
params.disable_paste_burst,
|
2025-07-31 17:30:44 -07:00
|
|
|
),
|
2025-05-14 10:13:29 -07:00
|
|
|
active_view: None,
|
|
|
|
|
app_event_tx: params.app_event_tx,
|
2025-08-20 13:47:24 -07:00
|
|
|
frame_requester: params.frame_requester,
|
2025-05-14 10:13:29 -07:00
|
|
|
has_input_focus: params.has_input_focus,
|
|
|
|
|
is_task_running: false,
|
2025-06-27 13:37:11 -04:00
|
|
|
ctrl_c_quit_hint: false,
|
2025-08-25 14:38:38 -07:00
|
|
|
status: None,
|
|
|
|
|
queued_user_messages: Vec::new(),
|
2025-08-23 23:23:15 -07:00
|
|
|
esc_backtrack_hint: false,
|
2025-05-14 10:13:29 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-31 00:43:21 -07:00
|
|
|
pub fn desired_height(&self, width: u16) -> u16 {
|
2025-09-02 10:29:58 -07:00
|
|
|
// Always reserve one blank row above the pane for visual spacing.
|
|
|
|
|
let top_margin = 1;
|
2025-08-25 14:38:38 -07:00
|
|
|
|
|
|
|
|
// Base height depends on whether a modal/overlay is active.
|
2025-09-02 10:29:58 -07:00
|
|
|
let base = match self.active_view.as_ref() {
|
|
|
|
|
Some(view) => view.desired_height(width),
|
|
|
|
|
None => self.composer.desired_height(width).saturating_add(
|
|
|
|
|
self.status
|
|
|
|
|
.as_ref()
|
|
|
|
|
.map_or(0, |status| status.desired_height(width)),
|
|
|
|
|
),
|
2025-08-04 21:23:22 -07:00
|
|
|
};
|
2025-08-25 14:38:38 -07:00
|
|
|
// Account for bottom padding rows. Top spacing is handled in layout().
|
|
|
|
|
base.saturating_add(Self::BOTTOM_PAD_LINES)
|
|
|
|
|
.saturating_add(top_margin)
|
2025-08-22 16:32:31 -07:00
|
|
|
}
|
|
|
|
|
|
2025-08-25 14:38:38 -07:00
|
|
|
fn layout(&self, area: Rect) -> [Rect; 2] {
|
2025-09-02 10:29:58 -07:00
|
|
|
// At small heights, bottom pane takes the entire height.
|
|
|
|
|
let (top_margin, bottom_margin) = if area.height <= BottomPane::BOTTOM_PAD_LINES + 1 {
|
|
|
|
|
(0, 0)
|
2025-08-22 16:32:31 -07:00
|
|
|
} else {
|
2025-09-02 10:29:58 -07:00
|
|
|
(1, BottomPane::BOTTOM_PAD_LINES)
|
2025-08-22 16:32:31 -07:00
|
|
|
};
|
2025-08-04 21:23:22 -07:00
|
|
|
|
2025-09-02 10:29:58 -07:00
|
|
|
let area = Rect {
|
|
|
|
|
x: area.x,
|
|
|
|
|
y: area.y + top_margin,
|
|
|
|
|
width: area.width,
|
|
|
|
|
height: area.height - top_margin - bottom_margin,
|
|
|
|
|
};
|
|
|
|
|
match self.active_view.as_ref() {
|
|
|
|
|
Some(_) => [Rect::ZERO, area],
|
|
|
|
|
None => {
|
|
|
|
|
let status_height = self
|
|
|
|
|
.status
|
|
|
|
|
.as_ref()
|
|
|
|
|
.map_or(0, |status| status.desired_height(area.width));
|
|
|
|
|
Layout::vertical([Constraint::Max(status_height), Constraint::Min(1)]).areas(area)
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-08-03 11:31:35 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn cursor_pos(&self, area: Rect) -> Option<(u16, u16)> {
|
|
|
|
|
// Hide the cursor whenever an overlay view is active (e.g. the
|
|
|
|
|
// status indicator shown while a task is running, or approval modal).
|
|
|
|
|
// In these states the textarea is not interactable, so we should not
|
|
|
|
|
// show its caret.
|
2025-08-25 14:38:38 -07:00
|
|
|
if self.active_view.is_some() {
|
2025-08-03 11:31:35 -07:00
|
|
|
None
|
|
|
|
|
} else {
|
2025-08-25 14:38:38 -07:00
|
|
|
let [_, content] = self.layout(area);
|
2025-08-22 16:32:31 -07:00
|
|
|
self.composer.cursor_pos(content)
|
2025-08-03 11:31:35 -07:00
|
|
|
}
|
2025-07-30 17:06:55 -07:00
|
|
|
}
|
|
|
|
|
|
2025-05-14 10:13:29 -07:00
|
|
|
/// Forward a key event to the active view or the composer.
|
2025-05-15 14:50:30 -07:00
|
|
|
pub fn handle_key_event(&mut self, key_event: KeyEvent) -> InputResult {
|
2025-05-14 10:13:29 -07:00
|
|
|
if let Some(mut view) = self.active_view.take() {
|
2025-05-15 14:50:30 -07:00
|
|
|
view.handle_key_event(self, key_event);
|
2025-05-14 10:13:29 -07:00
|
|
|
if !view.is_complete() {
|
|
|
|
|
self.active_view = Some(view);
|
2025-09-04 12:37:43 -07:00
|
|
|
} else {
|
|
|
|
|
self.on_active_view_complete();
|
2025-05-14 10:13:29 -07:00
|
|
|
}
|
2025-05-15 14:50:30 -07:00
|
|
|
self.request_redraw();
|
|
|
|
|
InputResult::None
|
2025-05-14 10:13:29 -07:00
|
|
|
} else {
|
2025-08-25 14:38:38 -07:00
|
|
|
// If a task is running and a status line is visible, allow Esc to
|
|
|
|
|
// send an interrupt even while the composer has focus.
|
|
|
|
|
if matches!(key_event.code, crossterm::event::KeyCode::Esc)
|
|
|
|
|
&& self.is_task_running
|
|
|
|
|
&& let Some(status) = &self.status
|
|
|
|
|
{
|
|
|
|
|
// Send Op::Interrupt
|
|
|
|
|
status.interrupt();
|
|
|
|
|
self.request_redraw();
|
|
|
|
|
return InputResult::None;
|
|
|
|
|
}
|
2025-05-14 10:13:29 -07:00
|
|
|
let (input_result, needs_redraw) = self.composer.handle_key_event(key_event);
|
|
|
|
|
if needs_redraw {
|
2025-05-15 14:50:30 -07:00
|
|
|
self.request_redraw();
|
2025-05-14 10:13:29 -07:00
|
|
|
}
|
2025-08-28 12:54:12 -07:00
|
|
|
if self.composer.is_in_paste_burst() {
|
|
|
|
|
self.request_redraw_in(ChatComposer::recommended_paste_flush_delay());
|
|
|
|
|
}
|
2025-05-15 14:50:30 -07:00
|
|
|
input_result
|
2025-05-14 10:13:29 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-28 12:00:06 -07:00
|
|
|
/// Handle Ctrl-C in the bottom pane. If a modal view is active it gets a
|
|
|
|
|
/// chance to consume the event (e.g. to dismiss itself).
|
|
|
|
|
pub(crate) fn on_ctrl_c(&mut self) -> CancellationEvent {
|
|
|
|
|
let mut view = match self.active_view.take() {
|
|
|
|
|
Some(view) => view,
|
2025-09-07 20:21:53 -07:00
|
|
|
None => {
|
|
|
|
|
return if self.composer_is_empty() {
|
|
|
|
|
CancellationEvent::NotHandled
|
|
|
|
|
} else {
|
|
|
|
|
self.set_composer_text(String::new());
|
|
|
|
|
self.show_ctrl_c_quit_hint();
|
|
|
|
|
CancellationEvent::Handled
|
|
|
|
|
};
|
|
|
|
|
}
|
2025-07-28 12:00:06 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let event = view.on_ctrl_c(self);
|
|
|
|
|
match event {
|
|
|
|
|
CancellationEvent::Handled => {
|
|
|
|
|
if !view.is_complete() {
|
|
|
|
|
self.active_view = Some(view);
|
2025-09-04 12:37:43 -07:00
|
|
|
} else {
|
|
|
|
|
self.on_active_view_complete();
|
2025-07-28 12:00:06 -07:00
|
|
|
}
|
|
|
|
|
self.show_ctrl_c_quit_hint();
|
|
|
|
|
}
|
2025-09-07 20:21:53 -07:00
|
|
|
CancellationEvent::NotHandled => {
|
2025-07-28 12:00:06 -07:00
|
|
|
self.active_view = Some(view);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
event
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-12 15:32:00 -07:00
|
|
|
pub fn handle_paste(&mut self, pasted: String) {
|
|
|
|
|
if self.active_view.is_none() {
|
|
|
|
|
let needs_redraw = self.composer.handle_paste(pasted);
|
|
|
|
|
if needs_redraw {
|
|
|
|
|
self.request_redraw();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-11 14:15:41 -07:00
|
|
|
pub(crate) fn insert_str(&mut self, text: &str) {
|
|
|
|
|
self.composer.insert_str(text);
|
|
|
|
|
self.request_redraw();
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-25 14:38:38 -07:00
|
|
|
/// Replace the composer text with `text`.
|
|
|
|
|
pub(crate) fn set_composer_text(&mut self, text: String) {
|
|
|
|
|
self.composer.set_text_content(text);
|
|
|
|
|
self.request_redraw();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Get the current composer text (for tests and programmatic checks).
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
pub(crate) fn composer_text(&self) -> String {
|
|
|
|
|
self.composer.current_text()
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-20 16:58:56 -07:00
|
|
|
/// Update the animated header shown to the left of the brackets in the
|
2025-08-25 14:38:38 -07:00
|
|
|
/// status indicator (defaults to "Working"). No-ops if the status
|
|
|
|
|
/// indicator is not active.
|
2025-08-20 16:58:56 -07:00
|
|
|
pub(crate) fn update_status_header(&mut self, header: String) {
|
2025-08-25 14:38:38 -07:00
|
|
|
if let Some(status) = self.status.as_mut() {
|
|
|
|
|
status.update_header(header);
|
2025-08-20 16:58:56 -07:00
|
|
|
self.request_redraw();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-27 13:37:11 -04:00
|
|
|
pub(crate) fn show_ctrl_c_quit_hint(&mut self) {
|
|
|
|
|
self.ctrl_c_quit_hint = true;
|
|
|
|
|
self.composer
|
|
|
|
|
.set_ctrl_c_quit_hint(true, self.has_input_focus);
|
|
|
|
|
self.request_redraw();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(crate) fn clear_ctrl_c_quit_hint(&mut self) {
|
|
|
|
|
if self.ctrl_c_quit_hint {
|
|
|
|
|
self.ctrl_c_quit_hint = false;
|
|
|
|
|
self.composer
|
|
|
|
|
.set_ctrl_c_quit_hint(false, self.has_input_focus);
|
|
|
|
|
self.request_redraw();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-07 20:21:53 -07:00
|
|
|
#[cfg(test)]
|
2025-06-27 13:37:11 -04:00
|
|
|
pub(crate) fn ctrl_c_quit_hint_visible(&self) -> bool {
|
|
|
|
|
self.ctrl_c_quit_hint
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-23 23:23:15 -07:00
|
|
|
pub(crate) fn show_esc_backtrack_hint(&mut self) {
|
|
|
|
|
self.esc_backtrack_hint = true;
|
|
|
|
|
self.composer.set_esc_backtrack_hint(true);
|
|
|
|
|
self.request_redraw();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(crate) fn clear_esc_backtrack_hint(&mut self) {
|
|
|
|
|
if self.esc_backtrack_hint {
|
|
|
|
|
self.esc_backtrack_hint = false;
|
|
|
|
|
self.composer.set_esc_backtrack_hint(false);
|
|
|
|
|
self.request_redraw();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// esc_backtrack_hint_visible removed; hints are controlled internally.
|
|
|
|
|
|
2025-05-15 14:50:30 -07:00
|
|
|
pub fn set_task_running(&mut self, running: bool) {
|
2025-05-14 10:13:29 -07:00
|
|
|
self.is_task_running = running;
|
2025-09-07 20:21:53 -07:00
|
|
|
self.composer.set_task_running(running);
|
2025-05-14 10:13:29 -07:00
|
|
|
|
2025-08-04 21:23:22 -07:00
|
|
|
if running {
|
2025-08-25 14:38:38 -07:00
|
|
|
if self.status.is_none() {
|
|
|
|
|
self.status = Some(StatusIndicatorWidget::new(
|
2025-05-14 10:13:29 -07:00
|
|
|
self.app_event_tx.clone(),
|
2025-08-20 13:47:24 -07:00
|
|
|
self.frame_requester.clone(),
|
2025-08-25 14:38:38 -07:00
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
if let Some(status) = self.status.as_mut() {
|
|
|
|
|
status.set_queued_messages(self.queued_user_messages.clone());
|
2025-05-14 10:13:29 -07:00
|
|
|
}
|
2025-08-04 21:23:22 -07:00
|
|
|
self.request_redraw();
|
|
|
|
|
} else {
|
2025-08-25 14:38:38 -07:00
|
|
|
// Hide the status indicator when a task completes, but keep other modal views.
|
|
|
|
|
self.status = None;
|
2025-05-14 10:13:29 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-19 10:55:07 -07:00
|
|
|
/// Show a generic list selection view with the provided items.
|
|
|
|
|
pub(crate) fn show_selection_view(
|
|
|
|
|
&mut self,
|
|
|
|
|
title: String,
|
|
|
|
|
subtitle: Option<String>,
|
|
|
|
|
footer_hint: Option<String>,
|
|
|
|
|
items: Vec<SelectionItem>,
|
|
|
|
|
) {
|
|
|
|
|
let view = list_selection_view::ListSelectionView::new(
|
|
|
|
|
title,
|
|
|
|
|
subtitle,
|
|
|
|
|
footer_hint,
|
|
|
|
|
items,
|
|
|
|
|
self.app_event_tx.clone(),
|
|
|
|
|
);
|
|
|
|
|
self.active_view = Some(Box::new(view));
|
|
|
|
|
self.request_redraw();
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-25 14:38:38 -07:00
|
|
|
/// Update the queued messages shown under the status header.
|
|
|
|
|
pub(crate) fn set_queued_user_messages(&mut self, queued: Vec<String>) {
|
|
|
|
|
self.queued_user_messages = queued.clone();
|
|
|
|
|
if let Some(status) = self.status.as_mut() {
|
|
|
|
|
status.set_queued_messages(queued);
|
2025-08-15 15:32:41 -07:00
|
|
|
}
|
2025-08-25 14:38:38 -07:00
|
|
|
self.request_redraw();
|
2025-08-15 15:32:41 -07:00
|
|
|
}
|
|
|
|
|
|
2025-08-28 19:16:39 -07:00
|
|
|
/// Update custom prompts available for the slash popup.
|
|
|
|
|
pub(crate) fn set_custom_prompts(&mut self, prompts: Vec<CustomPrompt>) {
|
|
|
|
|
self.composer.set_custom_prompts(prompts);
|
|
|
|
|
self.request_redraw();
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-16 08:59:26 -07:00
|
|
|
pub(crate) fn composer_is_empty(&self) -> bool {
|
|
|
|
|
self.composer.is_empty()
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-27 13:37:11 -04:00
|
|
|
pub(crate) fn is_task_running(&self) -> bool {
|
|
|
|
|
self.is_task_running
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-25 15:38:46 -07:00
|
|
|
/// Return true when the pane is in the regular composer state without any
|
|
|
|
|
/// overlays or popups and not running a task. This is the safe context to
|
|
|
|
|
/// use Esc-Esc for backtracking from the main view.
|
|
|
|
|
pub(crate) fn is_normal_backtrack_mode(&self) -> bool {
|
|
|
|
|
!self.is_task_running && self.active_view.is_none() && !self.composer.popup_active()
|
|
|
|
|
}
|
|
|
|
|
|
feat: show number of tokens remaining in UI (#1388)
When using the OpenAI Responses API, we now record the `usage` field for
a `"response.completed"` event, which includes metrics about the number
of tokens consumed. We also introduce `openai_model_info.rs`, which
includes current data about the most common OpenAI models available via
the API (specifically `context_window` and `max_output_tokens`). If
Codex does not recognize the model, you can set `model_context_window`
and `model_max_output_tokens` explicitly in `config.toml`.
When then introduce a new event type to `protocol.rs`, `TokenCount`,
which includes the `TokenUsage` for the most recent turn.
Finally, we update the TUI to record the running sum of tokens used so
the percentage of available context window remaining can be reported via
the placeholder text for the composer:

We could certainly get much fancier with this (such as reporting the
estimated cost of the conversation), but for now, we are just trying to
achieve feature parity with the TypeScript CLI.
Though arguably this improves upon the TypeScript CLI, as the TypeScript
CLI uses heuristics to estimate the number of tokens used rather than
using the `usage` information directly:
https://github.com/openai/codex/blob/296996d74e345b1b05d8c3451a06ace21c5ada96/codex-cli/src/utils/approximate-tokens-used.ts#L3-L16
Fixes https://github.com/openai/codex/issues/1242
2025-06-25 23:31:11 -07:00
|
|
|
/// Update the *context-window remaining* indicator in the composer. This
|
|
|
|
|
/// is forwarded directly to the underlying `ChatComposer`.
|
2025-09-06 08:19:23 -07:00
|
|
|
pub(crate) fn set_token_usage(&mut self, token_info: Option<TokenUsageInfo>) {
|
|
|
|
|
self.composer.set_token_usage(token_info);
|
feat: show number of tokens remaining in UI (#1388)
When using the OpenAI Responses API, we now record the `usage` field for
a `"response.completed"` event, which includes metrics about the number
of tokens consumed. We also introduce `openai_model_info.rs`, which
includes current data about the most common OpenAI models available via
the API (specifically `context_window` and `max_output_tokens`). If
Codex does not recognize the model, you can set `model_context_window`
and `model_max_output_tokens` explicitly in `config.toml`.
When then introduce a new event type to `protocol.rs`, `TokenCount`,
which includes the `TokenUsage` for the most recent turn.
Finally, we update the TUI to record the running sum of tokens used so
the percentage of available context window remaining can be reported via
the placeholder text for the composer:

We could certainly get much fancier with this (such as reporting the
estimated cost of the conversation), but for now, we are just trying to
achieve feature parity with the TypeScript CLI.
Though arguably this improves upon the TypeScript CLI, as the TypeScript
CLI uses heuristics to estimate the number of tokens used rather than
using the `usage` information directly:
https://github.com/openai/codex/blob/296996d74e345b1b05d8c3451a06ace21c5ada96/codex-cli/src/utils/approximate-tokens-used.ts#L3-L16
Fixes https://github.com/openai/codex/issues/1242
2025-06-25 23:31:11 -07:00
|
|
|
self.request_redraw();
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-14 10:13:29 -07:00
|
|
|
/// Called when the agent requests user approval.
|
2025-05-15 14:50:30 -07:00
|
|
|
pub fn push_approval_request(&mut self, request: ApprovalRequest) {
|
2025-05-14 10:13:29 -07:00
|
|
|
let request = if let Some(view) = self.active_view.as_mut() {
|
|
|
|
|
match view.try_consume_approval_request(request) {
|
|
|
|
|
Some(request) => request,
|
|
|
|
|
None => {
|
2025-05-15 14:50:30 -07:00
|
|
|
self.request_redraw();
|
|
|
|
|
return;
|
2025-05-14 10:13:29 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
request
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Otherwise create a new approval modal overlay.
|
|
|
|
|
let modal = ApprovalModalView::new(request, self.app_event_tx.clone());
|
2025-09-04 12:37:43 -07:00
|
|
|
self.pause_status_timer_for_modal();
|
2025-05-14 10:13:29 -07:00
|
|
|
self.active_view = Some(Box::new(modal));
|
|
|
|
|
self.request_redraw()
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-04 12:37:43 -07:00
|
|
|
fn on_active_view_complete(&mut self) {
|
|
|
|
|
self.resume_status_timer_after_modal();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn pause_status_timer_for_modal(&mut self) {
|
|
|
|
|
if let Some(status) = self.status.as_mut() {
|
|
|
|
|
status.pause_timer();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn resume_status_timer_after_modal(&mut self) {
|
|
|
|
|
if let Some(status) = self.status.as_mut() {
|
|
|
|
|
status.resume_timer();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-14 10:13:29 -07:00
|
|
|
/// Height (terminal rows) required by the current bottom pane.
|
2025-05-15 14:50:30 -07:00
|
|
|
pub(crate) fn request_redraw(&self) {
|
2025-08-20 13:47:24 -07:00
|
|
|
self.frame_requester.schedule_frame();
|
2025-05-14 10:13:29 -07:00
|
|
|
}
|
feat: record messages from user in ~/.codex/history.jsonl (#939)
This is a large change to support a "history" feature like you would
expect in a shell like Bash.
History events are recorded in `$CODEX_HOME/history.jsonl`. Because it
is a JSONL file, it is straightforward to append new entries (as opposed
to the TypeScript file that uses `$CODEX_HOME/history.json`, so to be
valid JSON, each new entry entails rewriting the entire file). Because
it is possible for there to be multiple instances of Codex CLI writing
to `history.jsonl` at once, we use advisory file locking when working
with `history.jsonl` in `codex-rs/core/src/message_history.rs`.
Because we believe history is a sufficiently useful feature, we enable
it by default. Though to provide some safety, we set the file
permissions of `history.jsonl` to be `o600` so that other users on the
system cannot read the user's history. We do not yet support a default
list of `SENSITIVE_PATTERNS` as the TypeScript CLI does:
https://github.com/openai/codex/blob/3fdf9df1335ac9501e3fb0e61715359145711e8b/codex-cli/src/utils/storage/command-history.ts#L10-L17
We are going to take a more conservative approach to this list in the
Rust CLI. For example, while `/\b[A-Za-z0-9-_]{20,}\b/` might exclude
sensitive information like API tokens, it would also exclude valuable
information such as references to Git commits.
As noted in the updated documentation, users can opt-out of history by
adding the following to `config.toml`:
```toml
[history]
persistence = "none"
```
Because `history.jsonl` could, in theory, be quite large, we take a[n
arguably overly pedantic] approach in reading history entries into
memory. Specifically, we start by telling the client the current number
of entries in the history file (`history_entry_count`) as well as the
inode (`history_log_id`) of `history.jsonl` (see the new fields on
`SessionConfiguredEvent`).
The client is responsible for keeping new entries in memory to create a
"local history," but if the user hits up enough times to go "past" the
end of local history, then the client should use the new
`GetHistoryEntryRequest` in the protocol to fetch older entries.
Specifically, it should pass the `history_log_id` it was given
originally and work backwards from `history_entry_count`. (It should
really fetch history in batches rather than one-at-a-time, but that is
something we can improve upon in subsequent PRs.)
The motivation behind this crazy scheme is that it is designed to defend
against:
* The `history.jsonl` being truncated during the session such that the
index into the history is no longer consistent with what had been read
up to that point. We do not yet have logic to enforce a `max_bytes` for
`history.jsonl`, but once we do, we will aspire to implement it in a way
that should result in a new inode for the file on most systems.
* New items from concurrent Codex CLI sessions amending to the history.
Because, in absence of truncation, `history.jsonl` is an append-only
log, so long as the client reads backwards from `history_entry_count`,
it should always get a consistent view of history. (That said, it will
not be able to read _new_ commands from concurrent sessions, but perhaps
we will introduce a `/` command to reload latest history or something
down the road.)
Admittedly, my testing of this feature thus far has been fairly light. I
expect we will find bugs and introduce enhancements/fixes going forward.
2025-05-15 16:26:23 -07:00
|
|
|
|
2025-08-28 12:54:12 -07:00
|
|
|
pub(crate) fn request_redraw_in(&self, dur: Duration) {
|
|
|
|
|
self.frame_requester.schedule_frame_in(dur);
|
|
|
|
|
}
|
|
|
|
|
|
feat: record messages from user in ~/.codex/history.jsonl (#939)
This is a large change to support a "history" feature like you would
expect in a shell like Bash.
History events are recorded in `$CODEX_HOME/history.jsonl`. Because it
is a JSONL file, it is straightforward to append new entries (as opposed
to the TypeScript file that uses `$CODEX_HOME/history.json`, so to be
valid JSON, each new entry entails rewriting the entire file). Because
it is possible for there to be multiple instances of Codex CLI writing
to `history.jsonl` at once, we use advisory file locking when working
with `history.jsonl` in `codex-rs/core/src/message_history.rs`.
Because we believe history is a sufficiently useful feature, we enable
it by default. Though to provide some safety, we set the file
permissions of `history.jsonl` to be `o600` so that other users on the
system cannot read the user's history. We do not yet support a default
list of `SENSITIVE_PATTERNS` as the TypeScript CLI does:
https://github.com/openai/codex/blob/3fdf9df1335ac9501e3fb0e61715359145711e8b/codex-cli/src/utils/storage/command-history.ts#L10-L17
We are going to take a more conservative approach to this list in the
Rust CLI. For example, while `/\b[A-Za-z0-9-_]{20,}\b/` might exclude
sensitive information like API tokens, it would also exclude valuable
information such as references to Git commits.
As noted in the updated documentation, users can opt-out of history by
adding the following to `config.toml`:
```toml
[history]
persistence = "none"
```
Because `history.jsonl` could, in theory, be quite large, we take a[n
arguably overly pedantic] approach in reading history entries into
memory. Specifically, we start by telling the client the current number
of entries in the history file (`history_entry_count`) as well as the
inode (`history_log_id`) of `history.jsonl` (see the new fields on
`SessionConfiguredEvent`).
The client is responsible for keeping new entries in memory to create a
"local history," but if the user hits up enough times to go "past" the
end of local history, then the client should use the new
`GetHistoryEntryRequest` in the protocol to fetch older entries.
Specifically, it should pass the `history_log_id` it was given
originally and work backwards from `history_entry_count`. (It should
really fetch history in batches rather than one-at-a-time, but that is
something we can improve upon in subsequent PRs.)
The motivation behind this crazy scheme is that it is designed to defend
against:
* The `history.jsonl` being truncated during the session such that the
index into the history is no longer consistent with what had been read
up to that point. We do not yet have logic to enforce a `max_bytes` for
`history.jsonl`, but once we do, we will aspire to implement it in a way
that should result in a new inode for the file on most systems.
* New items from concurrent Codex CLI sessions amending to the history.
Because, in absence of truncation, `history.jsonl` is an append-only
log, so long as the client reads backwards from `history_entry_count`,
it should always get a consistent view of history. (That said, it will
not be able to read _new_ commands from concurrent sessions, but perhaps
we will introduce a `/` command to reload latest history or something
down the road.)
Admittedly, my testing of this feature thus far has been fairly light. I
expect we will find bugs and introduce enhancements/fixes going forward.
2025-05-15 16:26:23 -07:00
|
|
|
// --- History helpers ---
|
|
|
|
|
|
|
|
|
|
pub(crate) fn set_history_metadata(&mut self, log_id: u64, entry_count: usize) {
|
|
|
|
|
self.composer.set_history_metadata(log_id, entry_count);
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-28 12:54:12 -07:00
|
|
|
pub(crate) fn flush_paste_burst_if_due(&mut self) -> bool {
|
|
|
|
|
self.composer.flush_paste_burst_if_due()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(crate) fn is_in_paste_burst(&self) -> bool {
|
|
|
|
|
self.composer.is_in_paste_burst()
|
|
|
|
|
}
|
|
|
|
|
|
feat: record messages from user in ~/.codex/history.jsonl (#939)
This is a large change to support a "history" feature like you would
expect in a shell like Bash.
History events are recorded in `$CODEX_HOME/history.jsonl`. Because it
is a JSONL file, it is straightforward to append new entries (as opposed
to the TypeScript file that uses `$CODEX_HOME/history.json`, so to be
valid JSON, each new entry entails rewriting the entire file). Because
it is possible for there to be multiple instances of Codex CLI writing
to `history.jsonl` at once, we use advisory file locking when working
with `history.jsonl` in `codex-rs/core/src/message_history.rs`.
Because we believe history is a sufficiently useful feature, we enable
it by default. Though to provide some safety, we set the file
permissions of `history.jsonl` to be `o600` so that other users on the
system cannot read the user's history. We do not yet support a default
list of `SENSITIVE_PATTERNS` as the TypeScript CLI does:
https://github.com/openai/codex/blob/3fdf9df1335ac9501e3fb0e61715359145711e8b/codex-cli/src/utils/storage/command-history.ts#L10-L17
We are going to take a more conservative approach to this list in the
Rust CLI. For example, while `/\b[A-Za-z0-9-_]{20,}\b/` might exclude
sensitive information like API tokens, it would also exclude valuable
information such as references to Git commits.
As noted in the updated documentation, users can opt-out of history by
adding the following to `config.toml`:
```toml
[history]
persistence = "none"
```
Because `history.jsonl` could, in theory, be quite large, we take a[n
arguably overly pedantic] approach in reading history entries into
memory. Specifically, we start by telling the client the current number
of entries in the history file (`history_entry_count`) as well as the
inode (`history_log_id`) of `history.jsonl` (see the new fields on
`SessionConfiguredEvent`).
The client is responsible for keeping new entries in memory to create a
"local history," but if the user hits up enough times to go "past" the
end of local history, then the client should use the new
`GetHistoryEntryRequest` in the protocol to fetch older entries.
Specifically, it should pass the `history_log_id` it was given
originally and work backwards from `history_entry_count`. (It should
really fetch history in batches rather than one-at-a-time, but that is
something we can improve upon in subsequent PRs.)
The motivation behind this crazy scheme is that it is designed to defend
against:
* The `history.jsonl` being truncated during the session such that the
index into the history is no longer consistent with what had been read
up to that point. We do not yet have logic to enforce a `max_bytes` for
`history.jsonl`, but once we do, we will aspire to implement it in a way
that should result in a new inode for the file on most systems.
* New items from concurrent Codex CLI sessions amending to the history.
Because, in absence of truncation, `history.jsonl` is an append-only
log, so long as the client reads backwards from `history_entry_count`,
it should always get a consistent view of history. (That said, it will
not be able to read _new_ commands from concurrent sessions, but perhaps
we will introduce a `/` command to reload latest history or something
down the road.)
Admittedly, my testing of this feature thus far has been fairly light. I
expect we will find bugs and introduce enhancements/fixes going forward.
2025-05-15 16:26:23 -07:00
|
|
|
pub(crate) fn on_history_entry_response(
|
|
|
|
|
&mut self,
|
|
|
|
|
log_id: u64,
|
|
|
|
|
offset: usize,
|
|
|
|
|
entry: Option<String>,
|
|
|
|
|
) {
|
|
|
|
|
let updated = self
|
|
|
|
|
.composer
|
|
|
|
|
.on_history_entry_response(log_id, offset, entry);
|
|
|
|
|
|
|
|
|
|
if updated {
|
|
|
|
|
self.request_redraw();
|
|
|
|
|
}
|
|
|
|
|
}
|
feat: add support for @ to do file search (#1401)
Introduces support for `@` to trigger a fuzzy-filename search in the
composer. Under the hood, this leverages
https://crates.io/crates/nucleo-matcher to do the fuzzy matching and
https://crates.io/crates/ignore to build up the list of file candidates
(so that it respects `.gitignore`).
For simplicity (at least for now), we do not do any caching between
searches like VS Code does for its file search:
https://github.com/microsoft/vscode/blob/1d89ed699b2e924d418c856318a3e12bca67ff3a/src/vs/workbench/services/search/node/rawSearchService.ts#L212-L218
Because we do not do any caching, I saw queries take up to three seconds
on large repositories with hundreds of thousands of files. To that end,
we do not perform searches synchronously on each keystroke, but instead
dispatch an event to do the search on a background thread that
asynchronously reports back to the UI when the results are available.
This is largely handled by the `FileSearchManager` introduced in this
PR, which also has logic for debouncing requests so there is at most one
search in flight at a time.
While we could potentially polish and tune this feature further, it may
already be overengineered for how it will be used, in practice, so we
can improve things going forward if it turns out that this is not "good
enough" in the wild.
Note this feature does not work like `@` in the TypeScript CLI, which
was more like directory-based tab completion. In the Rust CLI, `@`
triggers a full-repo fuzzy-filename search.
Fixes https://github.com/openai/codex/issues/1261.
2025-06-28 13:47:42 -07:00
|
|
|
|
2025-06-28 15:04:23 -07:00
|
|
|
pub(crate) fn on_file_search_result(&mut self, query: String, matches: Vec<FileMatch>) {
|
feat: add support for @ to do file search (#1401)
Introduces support for `@` to trigger a fuzzy-filename search in the
composer. Under the hood, this leverages
https://crates.io/crates/nucleo-matcher to do the fuzzy matching and
https://crates.io/crates/ignore to build up the list of file candidates
(so that it respects `.gitignore`).
For simplicity (at least for now), we do not do any caching between
searches like VS Code does for its file search:
https://github.com/microsoft/vscode/blob/1d89ed699b2e924d418c856318a3e12bca67ff3a/src/vs/workbench/services/search/node/rawSearchService.ts#L212-L218
Because we do not do any caching, I saw queries take up to three seconds
on large repositories with hundreds of thousands of files. To that end,
we do not perform searches synchronously on each keystroke, but instead
dispatch an event to do the search on a background thread that
asynchronously reports back to the UI when the results are available.
This is largely handled by the `FileSearchManager` introduced in this
PR, which also has logic for debouncing requests so there is at most one
search in flight at a time.
While we could potentially polish and tune this feature further, it may
already be overengineered for how it will be used, in practice, so we
can improve things going forward if it turns out that this is not "good
enough" in the wild.
Note this feature does not work like `@` in the TypeScript CLI, which
was more like directory-based tab completion. In the Rust CLI, `@`
triggers a full-repo fuzzy-filename search.
Fixes https://github.com/openai/codex/issues/1261.
2025-06-28 13:47:42 -07:00
|
|
|
self.composer.on_file_search_result(query, matches);
|
|
|
|
|
self.request_redraw();
|
|
|
|
|
}
|
2025-08-22 18:05:43 +01:00
|
|
|
|
|
|
|
|
pub(crate) fn attach_image(
|
|
|
|
|
&mut self,
|
|
|
|
|
path: PathBuf,
|
|
|
|
|
width: u32,
|
|
|
|
|
height: u32,
|
|
|
|
|
format_label: &str,
|
|
|
|
|
) {
|
|
|
|
|
if self.active_view.is_none() {
|
|
|
|
|
self.composer
|
|
|
|
|
.attach_image(path, width, height, format_label);
|
|
|
|
|
self.request_redraw();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(crate) fn take_recent_submission_images(&mut self) -> Vec<PathBuf> {
|
|
|
|
|
self.composer.take_recent_submission_images()
|
|
|
|
|
}
|
2025-05-14 10:13:29 -07:00
|
|
|
}
|
|
|
|
|
|
2025-08-20 13:47:24 -07:00
|
|
|
impl WidgetRef for &BottomPane {
|
2025-05-14 10:13:29 -07:00
|
|
|
fn render_ref(&self, area: Rect, buf: &mut Buffer) {
|
2025-08-25 14:38:38 -07:00
|
|
|
let [status_area, content] = self.layout(area);
|
2025-08-22 16:32:31 -07:00
|
|
|
|
2025-08-25 14:38:38 -07:00
|
|
|
// When a modal view is active, it owns the whole content area.
|
2025-08-04 21:23:22 -07:00
|
|
|
if let Some(view) = &self.active_view {
|
2025-08-22 16:32:31 -07:00
|
|
|
view.render(content, buf);
|
2025-08-14 14:10:21 -04:00
|
|
|
} else {
|
2025-08-25 14:38:38 -07:00
|
|
|
// No active modal:
|
|
|
|
|
// If a status indicator is active, render it above the composer.
|
|
|
|
|
if let Some(status) = &self.status {
|
|
|
|
|
status.render_ref(status_area, buf);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Render the composer in the remaining area.
|
|
|
|
|
self.composer.render_ref(content, buf);
|
2025-05-14 10:13:29 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-07-28 12:00:06 -07:00
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
|
|
|
|
use super::*;
|
|
|
|
|
use crate::app_event::AppEvent;
|
2025-08-04 21:23:22 -07:00
|
|
|
use ratatui::buffer::Buffer;
|
|
|
|
|
use ratatui::layout::Rect;
|
2025-08-20 10:11:09 -07:00
|
|
|
use tokio::sync::mpsc::unbounded_channel;
|
2025-07-28 12:00:06 -07:00
|
|
|
|
|
|
|
|
fn exec_request() -> ApprovalRequest {
|
|
|
|
|
ApprovalRequest::Exec {
|
|
|
|
|
id: "1".to_string(),
|
|
|
|
|
command: vec!["echo".into(), "ok".into()],
|
|
|
|
|
reason: None,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn ctrl_c_on_modal_consumes_and_shows_quit_hint() {
|
2025-08-20 10:11:09 -07:00
|
|
|
let (tx_raw, _rx) = unbounded_channel::<AppEvent>();
|
2025-07-28 12:00:06 -07:00
|
|
|
let tx = AppEventSender::new(tx_raw);
|
|
|
|
|
let mut pane = BottomPane::new(BottomPaneParams {
|
|
|
|
|
app_event_tx: tx,
|
2025-09-07 20:21:53 -07:00
|
|
|
frame_requester: FrameRequester::test_dummy(),
|
2025-07-28 12:00:06 -07:00
|
|
|
has_input_focus: true,
|
2025-07-31 17:30:44 -07:00
|
|
|
enhanced_keys_supported: false,
|
2025-08-15 22:37:10 -04:00
|
|
|
placeholder_text: "Ask Codex to do anything".to_string(),
|
2025-08-28 12:54:12 -07:00
|
|
|
disable_paste_burst: false,
|
2025-07-28 12:00:06 -07:00
|
|
|
});
|
|
|
|
|
pane.push_approval_request(exec_request());
|
|
|
|
|
assert_eq!(CancellationEvent::Handled, pane.on_ctrl_c());
|
|
|
|
|
assert!(pane.ctrl_c_quit_hint_visible());
|
2025-09-07 20:21:53 -07:00
|
|
|
assert_eq!(CancellationEvent::NotHandled, pane.on_ctrl_c());
|
2025-07-28 12:00:06 -07:00
|
|
|
}
|
2025-08-04 21:23:22 -07:00
|
|
|
|
2025-08-12 17:37:28 -07:00
|
|
|
// live ring removed; related tests deleted.
|
2025-08-04 21:23:22 -07:00
|
|
|
|
2025-08-05 01:56:13 -07:00
|
|
|
#[test]
|
|
|
|
|
fn overlay_not_shown_above_approval_modal() {
|
2025-08-20 10:11:09 -07:00
|
|
|
let (tx_raw, _rx) = unbounded_channel::<AppEvent>();
|
2025-08-05 01:56:13 -07:00
|
|
|
let tx = AppEventSender::new(tx_raw);
|
|
|
|
|
let mut pane = BottomPane::new(BottomPaneParams {
|
|
|
|
|
app_event_tx: tx,
|
2025-09-07 20:21:53 -07:00
|
|
|
frame_requester: FrameRequester::test_dummy(),
|
2025-08-05 01:56:13 -07:00
|
|
|
has_input_focus: true,
|
|
|
|
|
enhanced_keys_supported: false,
|
2025-08-15 22:37:10 -04:00
|
|
|
placeholder_text: "Ask Codex to do anything".to_string(),
|
2025-08-28 12:54:12 -07:00
|
|
|
disable_paste_burst: false,
|
2025-08-05 01:56:13 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Create an approval modal (active view).
|
|
|
|
|
pane.push_approval_request(exec_request());
|
|
|
|
|
|
2025-08-14 14:10:21 -04:00
|
|
|
// Render and verify the top row does not include an overlay.
|
2025-08-05 01:56:13 -07:00
|
|
|
let area = Rect::new(0, 0, 60, 6);
|
|
|
|
|
let mut buf = Buffer::empty(area);
|
|
|
|
|
(&pane).render_ref(area, &mut buf);
|
|
|
|
|
|
|
|
|
|
let mut r0 = String::new();
|
|
|
|
|
for x in 0..area.width {
|
|
|
|
|
r0.push(buf[(x, 0)].symbol().chars().next().unwrap_or(' '));
|
|
|
|
|
}
|
|
|
|
|
assert!(
|
|
|
|
|
!r0.contains("Working"),
|
2025-08-14 14:10:21 -04:00
|
|
|
"overlay should not render above modal"
|
2025-08-05 01:56:13 -07:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2025-08-25 14:38:38 -07:00
|
|
|
fn composer_shown_after_denied_while_task_running() {
|
2025-08-20 10:11:09 -07:00
|
|
|
let (tx_raw, rx) = unbounded_channel::<AppEvent>();
|
2025-08-05 01:56:13 -07:00
|
|
|
let tx = AppEventSender::new(tx_raw);
|
|
|
|
|
let mut pane = BottomPane::new(BottomPaneParams {
|
|
|
|
|
app_event_tx: tx.clone(),
|
2025-09-07 20:21:53 -07:00
|
|
|
frame_requester: FrameRequester::test_dummy(),
|
2025-08-05 01:56:13 -07:00
|
|
|
has_input_focus: true,
|
|
|
|
|
enhanced_keys_supported: false,
|
2025-08-15 22:37:10 -04:00
|
|
|
placeholder_text: "Ask Codex to do anything".to_string(),
|
2025-08-28 12:54:12 -07:00
|
|
|
disable_paste_burst: false,
|
2025-08-05 01:56:13 -07:00
|
|
|
});
|
|
|
|
|
|
2025-08-25 14:38:38 -07:00
|
|
|
// Start a running task so the status indicator is active above the composer.
|
2025-08-05 01:56:13 -07:00
|
|
|
pane.set_task_running(true);
|
|
|
|
|
|
|
|
|
|
// Push an approval modal (e.g., command approval) which should hide the status view.
|
|
|
|
|
pane.push_approval_request(exec_request());
|
|
|
|
|
|
2025-08-26 10:08:06 -07:00
|
|
|
// Simulate pressing 'n' (No) on the modal.
|
2025-08-05 01:56:13 -07:00
|
|
|
use crossterm::event::KeyCode;
|
|
|
|
|
use crossterm::event::KeyEvent;
|
|
|
|
|
use crossterm::event::KeyModifiers;
|
|
|
|
|
pane.handle_key_event(KeyEvent::new(KeyCode::Char('n'), KeyModifiers::NONE));
|
|
|
|
|
|
2025-08-25 14:38:38 -07:00
|
|
|
// After denial, since the task is still running, the status indicator should be
|
|
|
|
|
// visible above the composer. The modal should be gone.
|
2025-08-05 01:56:13 -07:00
|
|
|
assert!(
|
2025-08-25 14:38:38 -07:00
|
|
|
pane.active_view.is_none(),
|
|
|
|
|
"no active modal view after denial"
|
2025-08-05 01:56:13 -07:00
|
|
|
);
|
|
|
|
|
|
2025-08-25 14:38:38 -07:00
|
|
|
// Render and ensure the top row includes the Working header and a composer line below.
|
|
|
|
|
// Give the animation thread a moment to tick.
|
2025-09-07 20:21:53 -07:00
|
|
|
std::thread::sleep(Duration::from_millis(120));
|
2025-08-25 14:38:38 -07:00
|
|
|
let area = Rect::new(0, 0, 40, 6);
|
2025-08-05 01:56:13 -07:00
|
|
|
let mut buf = Buffer::empty(area);
|
|
|
|
|
(&pane).render_ref(area, &mut buf);
|
2025-08-22 16:32:31 -07:00
|
|
|
let mut row1 = String::new();
|
2025-08-05 01:56:13 -07:00
|
|
|
for x in 0..area.width {
|
2025-08-22 16:32:31 -07:00
|
|
|
row1.push(buf[(x, 1)].symbol().chars().next().unwrap_or(' '));
|
2025-08-05 01:56:13 -07:00
|
|
|
}
|
|
|
|
|
assert!(
|
2025-08-22 16:32:31 -07:00
|
|
|
row1.contains("Working"),
|
|
|
|
|
"expected Working header after denial on row 1: {row1:?}"
|
2025-08-05 01:56:13 -07:00
|
|
|
);
|
|
|
|
|
|
2025-08-25 14:38:38 -07:00
|
|
|
// Composer placeholder should be visible somewhere below.
|
|
|
|
|
let mut found_composer = false;
|
|
|
|
|
for y in 1..area.height.saturating_sub(2) {
|
|
|
|
|
let mut row = String::new();
|
|
|
|
|
for x in 0..area.width {
|
|
|
|
|
row.push(buf[(x, y)].symbol().chars().next().unwrap_or(' '));
|
|
|
|
|
}
|
|
|
|
|
if row.contains("Ask Codex") {
|
|
|
|
|
found_composer = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
assert!(
|
|
|
|
|
found_composer,
|
|
|
|
|
"expected composer visible under status line"
|
|
|
|
|
);
|
|
|
|
|
|
2025-08-05 01:56:13 -07:00
|
|
|
// Drain the channel to avoid unused warnings.
|
|
|
|
|
drop(rx);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn status_indicator_visible_during_command_execution() {
|
2025-08-20 10:11:09 -07:00
|
|
|
let (tx_raw, _rx) = unbounded_channel::<AppEvent>();
|
2025-08-05 01:56:13 -07:00
|
|
|
let tx = AppEventSender::new(tx_raw);
|
|
|
|
|
let mut pane = BottomPane::new(BottomPaneParams {
|
|
|
|
|
app_event_tx: tx,
|
2025-09-07 20:21:53 -07:00
|
|
|
frame_requester: FrameRequester::test_dummy(),
|
2025-08-05 01:56:13 -07:00
|
|
|
has_input_focus: true,
|
|
|
|
|
enhanced_keys_supported: false,
|
2025-08-15 22:37:10 -04:00
|
|
|
placeholder_text: "Ask Codex to do anything".to_string(),
|
2025-08-28 12:54:12 -07:00
|
|
|
disable_paste_burst: false,
|
2025-08-05 01:56:13 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Begin a task: show initial status.
|
|
|
|
|
pane.set_task_running(true);
|
|
|
|
|
|
2025-08-25 14:38:38 -07:00
|
|
|
// Use a height that allows the status line to be visible above the composer.
|
|
|
|
|
let area = Rect::new(0, 0, 40, 6);
|
2025-08-05 01:56:13 -07:00
|
|
|
let mut buf = Buffer::empty(area);
|
|
|
|
|
(&pane).render_ref(area, &mut buf);
|
|
|
|
|
|
|
|
|
|
let mut row0 = String::new();
|
|
|
|
|
for x in 0..area.width {
|
2025-08-22 16:32:31 -07:00
|
|
|
row0.push(buf[(x, 1)].symbol().chars().next().unwrap_or(' '));
|
2025-08-05 01:56:13 -07:00
|
|
|
}
|
|
|
|
|
assert!(
|
|
|
|
|
row0.contains("Working"),
|
|
|
|
|
"expected Working header: {row0:?}"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-04 21:23:22 -07:00
|
|
|
#[test]
|
2025-08-25 14:38:38 -07:00
|
|
|
fn bottom_padding_present_with_status_above_composer() {
|
2025-08-20 10:11:09 -07:00
|
|
|
let (tx_raw, _rx) = unbounded_channel::<AppEvent>();
|
2025-08-04 21:23:22 -07:00
|
|
|
let tx = AppEventSender::new(tx_raw);
|
|
|
|
|
let mut pane = BottomPane::new(BottomPaneParams {
|
|
|
|
|
app_event_tx: tx,
|
2025-09-07 20:21:53 -07:00
|
|
|
frame_requester: FrameRequester::test_dummy(),
|
2025-08-04 21:23:22 -07:00
|
|
|
has_input_focus: true,
|
|
|
|
|
enhanced_keys_supported: false,
|
2025-08-15 22:37:10 -04:00
|
|
|
placeholder_text: "Ask Codex to do anything".to_string(),
|
2025-08-28 12:54:12 -07:00
|
|
|
disable_paste_burst: false,
|
2025-08-04 21:23:22 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Activate spinner (status view replaces composer) with no live ring.
|
|
|
|
|
pane.set_task_running(true);
|
|
|
|
|
|
|
|
|
|
// Use height == desired_height; expect 1 status row at top and 2 bottom padding rows.
|
|
|
|
|
let height = pane.desired_height(30);
|
|
|
|
|
assert!(
|
|
|
|
|
height >= 3,
|
|
|
|
|
"expected at least 3 rows with bottom padding; got {height}"
|
|
|
|
|
);
|
|
|
|
|
let area = Rect::new(0, 0, 30, height);
|
|
|
|
|
let mut buf = Buffer::empty(area);
|
|
|
|
|
(&pane).render_ref(area, &mut buf);
|
|
|
|
|
|
2025-08-22 16:32:31 -07:00
|
|
|
// Row 1 contains the status header (row 0 is the spacer)
|
2025-08-04 21:23:22 -07:00
|
|
|
let mut top = String::new();
|
|
|
|
|
for x in 0..area.width {
|
2025-08-22 16:32:31 -07:00
|
|
|
top.push(buf[(x, 1)].symbol().chars().next().unwrap_or(' '));
|
2025-08-04 21:23:22 -07:00
|
|
|
}
|
2025-08-25 14:38:38 -07:00
|
|
|
assert!(
|
|
|
|
|
top.trim_start().starts_with("Working"),
|
|
|
|
|
"expected top row to start with 'Working': {top:?}"
|
|
|
|
|
);
|
2025-08-04 21:23:22 -07:00
|
|
|
assert!(
|
|
|
|
|
top.contains("Working"),
|
|
|
|
|
"expected Working header on top row: {top:?}"
|
|
|
|
|
);
|
|
|
|
|
|
2025-08-25 22:47:26 -07:00
|
|
|
// Last row should be blank padding; the row above should generally contain composer content.
|
2025-08-04 21:23:22 -07:00
|
|
|
let mut r_last = String::new();
|
|
|
|
|
for x in 0..area.width {
|
|
|
|
|
r_last.push(buf[(x, height - 1)].symbol().chars().next().unwrap_or(' '));
|
|
|
|
|
}
|
|
|
|
|
assert!(
|
|
|
|
|
r_last.trim().is_empty(),
|
|
|
|
|
"expected last row blank: {r_last:?}"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn bottom_padding_shrinks_when_tiny() {
|
2025-08-20 10:11:09 -07:00
|
|
|
let (tx_raw, _rx) = unbounded_channel::<AppEvent>();
|
2025-08-04 21:23:22 -07:00
|
|
|
let tx = AppEventSender::new(tx_raw);
|
|
|
|
|
let mut pane = BottomPane::new(BottomPaneParams {
|
|
|
|
|
app_event_tx: tx,
|
2025-09-07 20:21:53 -07:00
|
|
|
frame_requester: FrameRequester::test_dummy(),
|
2025-08-04 21:23:22 -07:00
|
|
|
has_input_focus: true,
|
|
|
|
|
enhanced_keys_supported: false,
|
2025-08-15 22:37:10 -04:00
|
|
|
placeholder_text: "Ask Codex to do anything".to_string(),
|
2025-08-28 12:54:12 -07:00
|
|
|
disable_paste_burst: false,
|
2025-08-04 21:23:22 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
pane.set_task_running(true);
|
|
|
|
|
|
2025-09-02 10:29:58 -07:00
|
|
|
// Height=2 → status on one row, composer on the other.
|
2025-08-04 21:23:22 -07:00
|
|
|
let area2 = Rect::new(0, 0, 20, 2);
|
|
|
|
|
let mut buf2 = Buffer::empty(area2);
|
|
|
|
|
(&pane).render_ref(area2, &mut buf2);
|
|
|
|
|
let mut row0 = String::new();
|
|
|
|
|
let mut row1 = String::new();
|
|
|
|
|
for x in 0..area2.width {
|
|
|
|
|
row0.push(buf2[(x, 0)].symbol().chars().next().unwrap_or(' '));
|
|
|
|
|
row1.push(buf2[(x, 1)].symbol().chars().next().unwrap_or(' '));
|
|
|
|
|
}
|
2025-08-25 14:38:38 -07:00
|
|
|
let has_composer = row0.contains("Ask Codex") || row1.contains("Ask Codex");
|
2025-08-04 21:23:22 -07:00
|
|
|
assert!(
|
2025-08-25 14:38:38 -07:00
|
|
|
has_composer,
|
|
|
|
|
"expected composer to be visible on one of the rows: row0={row0:?}, row1={row1:?}"
|
|
|
|
|
);
|
|
|
|
|
assert!(
|
2025-09-02 10:29:58 -07:00
|
|
|
row0.contains("Working") || row1.contains("Working"),
|
|
|
|
|
"expected status header to be visible at height=2: row0={row0:?}, row1={row1:?}"
|
2025-08-04 21:23:22 -07:00
|
|
|
);
|
|
|
|
|
|
2025-08-25 14:38:38 -07:00
|
|
|
// Height=1 → no padding; single row is the composer (status hidden).
|
2025-08-04 21:23:22 -07:00
|
|
|
let area1 = Rect::new(0, 0, 20, 1);
|
|
|
|
|
let mut buf1 = Buffer::empty(area1);
|
|
|
|
|
(&pane).render_ref(area1, &mut buf1);
|
|
|
|
|
let mut only = String::new();
|
|
|
|
|
for x in 0..area1.width {
|
|
|
|
|
only.push(buf1[(x, 0)].symbol().chars().next().unwrap_or(' '));
|
|
|
|
|
}
|
|
|
|
|
assert!(
|
2025-08-25 14:38:38 -07:00
|
|
|
only.contains("Ask Codex"),
|
|
|
|
|
"expected composer with no padding: {only:?}"
|
2025-08-04 21:23:22 -07:00
|
|
|
);
|
|
|
|
|
}
|
2025-07-28 12:00:06 -07:00
|
|
|
}
|