codex-rs: Rename /clear to /new, make it start an entirely new chat (#1264)

I noticed that `/clear` wasn't fully clearing chat history; it would
clear the chat history widgets _in the UI_, but the LLM still had access
to information from previous messages.

This PR renames `/clear` to `/new` for clarity as per Michael's
suggestion, resetting `app_state` to a fresh `ChatWidget`.
This commit is contained in:
Reilly Wood
2025-06-06 16:29:37 -07:00
committed by GitHub
parent 029f39b9da
commit 345a38502d
4 changed files with 24 additions and 21 deletions

View File

@@ -40,6 +40,9 @@ pub(crate) struct App<'a> {
app_event_rx: Receiver<AppEvent>, app_event_rx: Receiver<AppEvent>,
app_state: AppState<'a>, app_state: AppState<'a>,
/// Config is stored here so we can recreate ChatWidgets as needed.
config: Config,
/// Stored parameters needed to instantiate the ChatWidget later, e.g., /// Stored parameters needed to instantiate the ChatWidget later, e.g.,
/// after dismissing the Git-repo warning. /// after dismissing the Git-repo warning.
chat_args: Option<ChatWidgetArgs>, chat_args: Option<ChatWidgetArgs>,
@@ -122,7 +125,7 @@ impl<'a> App<'a> {
screen: LoginScreen::new(app_event_tx.clone(), config.codex_home.clone()), screen: LoginScreen::new(app_event_tx.clone(), config.codex_home.clone()),
}, },
Some(ChatWidgetArgs { Some(ChatWidgetArgs {
config, config: config.clone(),
initial_prompt, initial_prompt,
initial_images, initial_images,
}), }),
@@ -133,14 +136,18 @@ impl<'a> App<'a> {
screen: GitWarningScreen::new(), screen: GitWarningScreen::new(),
}, },
Some(ChatWidgetArgs { Some(ChatWidgetArgs {
config, config: config.clone(),
initial_prompt, initial_prompt,
initial_images, initial_images,
}), }),
) )
} else { } else {
let chat_widget = let chat_widget = ChatWidget::new(
ChatWidget::new(config, app_event_tx.clone(), initial_prompt, initial_images); config.clone(),
app_event_tx.clone(),
initial_prompt,
initial_images,
);
( (
AppState::Chat { AppState::Chat {
widget: Box::new(chat_widget), widget: Box::new(chat_widget),
@@ -153,6 +160,7 @@ impl<'a> App<'a> {
app_event_tx, app_event_tx,
app_event_rx, app_event_rx,
app_state, app_state,
config,
chat_args, chat_args,
} }
} }
@@ -224,10 +232,16 @@ impl<'a> App<'a> {
AppState::Login { .. } | AppState::GitWarning { .. } => {} AppState::Login { .. } | AppState::GitWarning { .. } => {}
}, },
AppEvent::DispatchCommand(command) => match command { AppEvent::DispatchCommand(command) => match command {
SlashCommand::Clear => match &mut self.app_state { SlashCommand::New => {
AppState::Chat { widget } => widget.clear_conversation_history(), let new_widget = Box::new(ChatWidget::new(
AppState::Login { .. } | AppState::GitWarning { .. } => {} self.config.clone(),
}, self.app_event_tx.clone(),
None,
Vec::new(),
));
self.app_state = AppState::Chat { widget: new_widget };
self.app_event_tx.send(AppEvent::Redraw);
}
SlashCommand::ToggleMouseMode => { SlashCommand::ToggleMouseMode => {
if let Err(e) = mouse_capture.toggle() { if let Err(e) = mouse_capture.toggle() {
tracing::error!("Failed to toggle mouse mode: {e}"); tracing::error!("Failed to toggle mouse mode: {e}");

View File

@@ -207,11 +207,6 @@ impl ChatWidget<'_> {
self.conversation_history.scroll_to_bottom(); self.conversation_history.scroll_to_bottom();
} }
pub(crate) fn clear_conversation_history(&mut self) {
self.conversation_history.clear();
self.request_redraw();
}
pub(crate) fn handle_codex_event(&mut self, event: Event) { pub(crate) fn handle_codex_event(&mut self, event: Event) {
let Event { id, msg } = event; let Event { id, msg } = event;
match msg { match msg {

View File

@@ -245,12 +245,6 @@ impl ConversationHistoryWidget {
}); });
} }
/// Remove all history entries and reset scrolling.
pub fn clear(&mut self) {
self.entries.clear();
self.scroll_position = usize::MAX;
}
pub fn record_completed_exec_command( pub fn record_completed_exec_command(
&mut self, &mut self,
call_id: String, call_id: String,

View File

@@ -12,7 +12,7 @@ use strum_macros::IntoStaticStr;
)] )]
#[strum(serialize_all = "kebab-case")] #[strum(serialize_all = "kebab-case")]
pub enum SlashCommand { pub enum SlashCommand {
Clear, New,
ToggleMouseMode, ToggleMouseMode,
Quit, Quit,
} }
@@ -21,7 +21,7 @@ impl SlashCommand {
/// User-visible description shown in the popup. /// User-visible description shown in the popup.
pub fn description(self) -> &'static str { pub fn description(self) -> &'static str {
match self { match self {
SlashCommand::Clear => "Clear the chat history.", SlashCommand::New => "Start a new chat.",
SlashCommand::ToggleMouseMode => { SlashCommand::ToggleMouseMode => {
"Toggle mouse mode (enable for scrolling, disable for text selection)" "Toggle mouse mode (enable for scrolling, disable for text selection)"
} }