Skip to content

Commit 39cc3b9

Browse files
authored
feat(args): --new arg to bypass persistent conversations (#1813)
Also deletes the past conversation history.
1 parent 9978859 commit 39cc3b9

File tree

3 files changed

+53
-17
lines changed

3 files changed

+53
-17
lines changed

crates/chat-cli/src/cli/chat/cli.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ pub struct Chat {
1717
/// prompt requests permissions to use a tool, unless --trust-all-tools is also used.
1818
#[arg(long)]
1919
pub no_interactive: bool,
20+
/// Start a new conversation and overwrites any previous conversation from this directory.
21+
#[arg(long)]
22+
pub new: bool,
2023
/// The first question to ask
2124
pub input: Option<String>,
2225
/// Context profile to use

crates/chat-cli/src/cli/chat/mod.rs

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,7 @@ pub async fn launch_chat(database: &mut Database, telemetry: &TelemetryThread, a
298298
telemetry,
299299
args.input,
300300
args.no_interactive,
301+
args.new,
301302
args.accept_all,
302303
args.profile,
303304
args.trust_all_tools,
@@ -306,12 +307,13 @@ pub async fn launch_chat(database: &mut Database, telemetry: &TelemetryThread, a
306307
.await
307308
}
308309

309-
#[allow(clippy::too_many_arguments)]
310+
#[allow(clippy::too_many_arguments, clippy::fn_params_excessive_bools)]
310311
pub async fn chat(
311312
database: &mut Database,
312313
telemetry: &TelemetryThread,
313314
input: Option<String>,
314315
no_interactive: bool,
316+
new_conversation: bool,
315317
accept_all: bool,
316318
profile: Option<String>,
317319
trust_all_tools: bool,
@@ -440,6 +442,7 @@ pub async fn chat(
440442
input,
441443
InputSource::new(database, prompt_request_sender, prompt_response_receiver)?,
442444
interactive,
445+
new_conversation,
443446
client,
444447
|| terminal::window_size().map(|s| s.columns.into()).ok(),
445448
tool_manager,
@@ -526,6 +529,7 @@ impl ChatContext {
526529
mut input: Option<String>,
527530
input_source: InputSource,
528531
interactive: bool,
532+
new_conversation: bool,
529533
client: StreamingClient,
530534
terminal_width_provider: fn() -> Option<usize>,
531535
tool_manager: ToolManager,
@@ -537,21 +541,38 @@ impl ChatContext {
537541
let output_clone = output.clone();
538542

539543
let mut existing_conversation = false;
540-
let conversation_state = match std::env::current_dir()
541-
.ok()
542-
.and_then(|cwd| database.get_conversation_by_path(cwd).ok())
543-
.flatten()
544-
{
545-
Some(mut prior) => {
544+
let conversation_state = if new_conversation {
545+
let new_state = ConversationState::new(
546+
ctx_clone,
547+
conversation_id,
548+
tool_config,
549+
profile,
550+
Some(output_clone),
551+
tool_manager,
552+
)
553+
.await;
554+
555+
std::env::current_dir()
556+
.ok()
557+
.and_then(|cwd| database.set_conversation_by_path(cwd, &new_state).ok());
558+
new_state
559+
} else {
560+
let prior = std::env::current_dir()
561+
.ok()
562+
.and_then(|cwd| database.get_conversation_by_path(cwd).ok())
563+
.flatten();
564+
565+
// Only restore conversations where there were actual messages.
566+
// Prevents edge case where user clears conversation with --new, then exits without chatting.
567+
if prior.as_ref().is_some_and(|cs| !cs.history().is_empty()) {
568+
let mut cs = prior.unwrap();
569+
546570
existing_conversation = true;
547-
prior
548-
.reload_serialized_state(Arc::clone(&ctx), Some(output.clone()))
549-
.await;
571+
cs.reload_serialized_state(Arc::clone(&ctx), Some(output.clone())).await;
550572
input = Some(input.unwrap_or("In a few words, summarize our conversation so far.".to_owned()));
551-
prior.tool_manager = tool_manager;
552-
prior
553-
},
554-
None => {
573+
cs.tool_manager = tool_manager;
574+
cs
575+
} else {
555576
ConversationState::new(
556577
ctx_clone,
557578
conversation_id,
@@ -561,7 +582,7 @@ impl ChatContext {
561582
tool_manager,
562583
)
563584
.await
564-
},
585+
}
565586
};
566587

567588
Ok(Self {
@@ -3710,6 +3731,7 @@ mod tests {
37103731
"exit".to_string(),
37113732
]),
37123733
true,
3734+
false,
37133735
test_client,
37143736
|| Some(80),
37153737
tool_manager,
@@ -3855,6 +3877,7 @@ mod tests {
38553877
"exit".to_string(),
38563878
]),
38573879
true,
3880+
false,
38583881
test_client,
38593882
|| Some(80),
38603883
tool_manager,
@@ -3953,6 +3976,7 @@ mod tests {
39533976
"exit".to_string(),
39543977
]),
39553978
true,
3979+
false,
39563980
test_client,
39573981
|| Some(80),
39583982
tool_manager,
@@ -4030,6 +4054,7 @@ mod tests {
40304054
"exit".to_string(),
40314055
]),
40324056
true,
4057+
false,
40334058
test_client,
40344059
|| Some(80),
40354060
tool_manager,

crates/chat-cli/src/cli/mod.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,7 @@ mod test {
369369
subcommand: Some(CliRootCommands::Chat(Chat {
370370
accept_all: false,
371371
no_interactive: false,
372+
new: false,
372373
input: None,
373374
profile: None,
374375
trust_all_tools: false,
@@ -407,6 +408,7 @@ mod test {
407408
CliRootCommands::Chat(Chat {
408409
accept_all: false,
409410
no_interactive: false,
411+
new: false,
410412
input: None,
411413
profile: Some("my-profile".to_string()),
412414
trust_all_tools: false,
@@ -422,6 +424,7 @@ mod test {
422424
CliRootCommands::Chat(Chat {
423425
accept_all: false,
424426
no_interactive: false,
427+
new: false,
425428
input: Some("Hello".to_string()),
426429
profile: Some("my-profile".to_string()),
427430
trust_all_tools: false,
@@ -437,6 +440,7 @@ mod test {
437440
CliRootCommands::Chat(Chat {
438441
accept_all: true,
439442
no_interactive: false,
443+
new: false,
440444
input: None,
441445
profile: Some("my-profile".to_string()),
442446
trust_all_tools: false,
@@ -446,12 +450,13 @@ mod test {
446450
}
447451

448452
#[test]
449-
fn test_chat_with_no_interactive() {
453+
fn test_chat_with_no_interactive_new() {
450454
assert_parse!(
451-
["chat", "--no-interactive"],
455+
["chat", "--no-interactive", "--new"],
452456
CliRootCommands::Chat(Chat {
453457
accept_all: false,
454458
no_interactive: true,
459+
new: true,
455460
input: None,
456461
profile: None,
457462
trust_all_tools: false,
@@ -467,6 +472,7 @@ mod test {
467472
CliRootCommands::Chat(Chat {
468473
accept_all: false,
469474
no_interactive: false,
475+
new: false,
470476
input: None,
471477
profile: None,
472478
trust_all_tools: true,
@@ -482,6 +488,7 @@ mod test {
482488
CliRootCommands::Chat(Chat {
483489
accept_all: false,
484490
no_interactive: false,
491+
new: false,
485492
input: None,
486493
profile: None,
487494
trust_all_tools: false,
@@ -497,6 +504,7 @@ mod test {
497504
CliRootCommands::Chat(Chat {
498505
accept_all: false,
499506
no_interactive: false,
507+
new: false,
500508
input: None,
501509
profile: None,
502510
trust_all_tools: false,

0 commit comments

Comments
 (0)