Skip to content

Commit 345a385

Browse files
authored
codex-rs: Rename /clear to /new, make it start an entirely new chat (openai#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`.
1 parent 029f39b commit 345a385

File tree

4 files changed

+24
-21
lines changed

4 files changed

+24
-21
lines changed

codex-rs/tui/src/app.rs

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ pub(crate) struct App<'a> {
4040
app_event_rx: Receiver<AppEvent>,
4141
app_state: AppState<'a>,
4242

43+
/// Config is stored here so we can recreate ChatWidgets as needed.
44+
config: Config,
45+
4346
/// Stored parameters needed to instantiate the ChatWidget later, e.g.,
4447
/// after dismissing the Git-repo warning.
4548
chat_args: Option<ChatWidgetArgs>,
@@ -122,7 +125,7 @@ impl<'a> App<'a> {
122125
screen: LoginScreen::new(app_event_tx.clone(), config.codex_home.clone()),
123126
},
124127
Some(ChatWidgetArgs {
125-
config,
128+
config: config.clone(),
126129
initial_prompt,
127130
initial_images,
128131
}),
@@ -133,14 +136,18 @@ impl<'a> App<'a> {
133136
screen: GitWarningScreen::new(),
134137
},
135138
Some(ChatWidgetArgs {
136-
config,
139+
config: config.clone(),
137140
initial_prompt,
138141
initial_images,
139142
}),
140143
)
141144
} else {
142-
let chat_widget =
143-
ChatWidget::new(config, app_event_tx.clone(), initial_prompt, initial_images);
145+
let chat_widget = ChatWidget::new(
146+
config.clone(),
147+
app_event_tx.clone(),
148+
initial_prompt,
149+
initial_images,
150+
);
144151
(
145152
AppState::Chat {
146153
widget: Box::new(chat_widget),
@@ -153,6 +160,7 @@ impl<'a> App<'a> {
153160
app_event_tx,
154161
app_event_rx,
155162
app_state,
163+
config,
156164
chat_args,
157165
}
158166
}
@@ -224,10 +232,16 @@ impl<'a> App<'a> {
224232
AppState::Login { .. } | AppState::GitWarning { .. } => {}
225233
},
226234
AppEvent::DispatchCommand(command) => match command {
227-
SlashCommand::Clear => match &mut self.app_state {
228-
AppState::Chat { widget } => widget.clear_conversation_history(),
229-
AppState::Login { .. } | AppState::GitWarning { .. } => {}
230-
},
235+
SlashCommand::New => {
236+
let new_widget = Box::new(ChatWidget::new(
237+
self.config.clone(),
238+
self.app_event_tx.clone(),
239+
None,
240+
Vec::new(),
241+
));
242+
self.app_state = AppState::Chat { widget: new_widget };
243+
self.app_event_tx.send(AppEvent::Redraw);
244+
}
231245
SlashCommand::ToggleMouseMode => {
232246
if let Err(e) = mouse_capture.toggle() {
233247
tracing::error!("Failed to toggle mouse mode: {e}");

codex-rs/tui/src/chatwidget.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -207,11 +207,6 @@ impl ChatWidget<'_> {
207207
self.conversation_history.scroll_to_bottom();
208208
}
209209

210-
pub(crate) fn clear_conversation_history(&mut self) {
211-
self.conversation_history.clear();
212-
self.request_redraw();
213-
}
214-
215210
pub(crate) fn handle_codex_event(&mut self, event: Event) {
216211
let Event { id, msg } = event;
217212
match msg {

codex-rs/tui/src/conversation_history_widget.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -245,12 +245,6 @@ impl ConversationHistoryWidget {
245245
});
246246
}
247247

248-
/// Remove all history entries and reset scrolling.
249-
pub fn clear(&mut self) {
250-
self.entries.clear();
251-
self.scroll_position = usize::MAX;
252-
}
253-
254248
pub fn record_completed_exec_command(
255249
&mut self,
256250
call_id: String,

codex-rs/tui/src/slash_command.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use strum_macros::IntoStaticStr;
1212
)]
1313
#[strum(serialize_all = "kebab-case")]
1414
pub enum SlashCommand {
15-
Clear,
15+
New,
1616
ToggleMouseMode,
1717
Quit,
1818
}
@@ -21,7 +21,7 @@ impl SlashCommand {
2121
/// User-visible description shown in the popup.
2222
pub fn description(self) -> &'static str {
2323
match self {
24-
SlashCommand::Clear => "Clear the chat history.",
24+
SlashCommand::New => "Start a new chat.",
2525
SlashCommand::ToggleMouseMode => {
2626
"Toggle mouse mode (enable for scrolling, disable for text selection)"
2727
}

0 commit comments

Comments
 (0)