@@ -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 ) ]
310311pub 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,
0 commit comments