@@ -542,7 +542,82 @@ impl ChatSession {
542
542
true => {
543
543
let mut cs = previous_conversation. unwrap ( ) ;
544
544
existing_conversation = true ;
545
- input = Some ( input. unwrap_or ( "In a few words, summarize our conversation so far." . to_owned ( ) ) ) ;
545
+ cs. reload_serialized_state ( os) . await ;
546
+
547
+ // Extract last exchange (user question + assistant response)
548
+ let resume_prompt = if let Some ( ( user_msg, assistant_msg) ) = cs. history ( ) . back ( ) {
549
+ let user_question = match & user_msg. content {
550
+ message:: UserMessageContent :: Prompt { prompt } => prompt. clone ( ) ,
551
+ message:: UserMessageContent :: CancelledToolUses { prompt, .. } => {
552
+ prompt. clone ( ) . unwrap_or_else ( || "[Tool use cancelled]" . to_string ( ) )
553
+ } ,
554
+ message:: UserMessageContent :: ToolUseResults { .. } => "[Tool results provided]" . to_string ( ) ,
555
+ } ;
556
+
557
+ let assistant_response = assistant_msg. content ( ) ;
558
+ let tool_summary = assistant_msg
559
+ . tool_uses ( )
560
+ . filter ( |tools| !tools. is_empty ( ) )
561
+ . map ( |tools| {
562
+ tools
563
+ . iter ( )
564
+ . map ( |tool| {
565
+ let purpose = tool
566
+ . args
567
+ . get ( "summary" )
568
+ . or_else ( || tool. args . get ( "description" ) )
569
+ . or_else ( || tool. args . get ( "command" ) )
570
+ . or_else ( || tool. args . get ( "path" ) )
571
+ . and_then ( |v| v. as_str ( ) )
572
+ . unwrap_or ( "perform an action" ) ;
573
+ format ! ( "- Used tool '{}' to: {}" , tool. name, purpose)
574
+ } )
575
+ . collect :: < Vec < _ > > ( )
576
+ . join ( "\n " )
577
+ } ) ;
578
+
579
+ if user_question. trim ( ) . is_empty ( ) || assistant_response. trim ( ) . is_empty ( ) {
580
+ "In a few words, summarize our conversation so far." . to_string ( )
581
+ } else {
582
+ // Smart truncation - keep meaningful length but not overwhelming
583
+ let max_question_len = 800 ;
584
+ let max_response_len = 1200 ;
585
+
586
+ let truncated_question = if user_question. len ( ) > max_question_len {
587
+ format ! ( "{}..." , & user_question[ ..max_question_len] . trim( ) )
588
+ } else {
589
+ user_question
590
+ } ;
591
+
592
+ let truncated_response = if assistant_response. len ( ) > max_response_len {
593
+ format ! ( "{}..." , & assistant_response[ ..max_response_len] . trim( ) )
594
+ } else {
595
+ assistant_response. to_string ( )
596
+ } ;
597
+
598
+ let mut parts = vec ! [
599
+ "Please show our last exchange, then provide a summary:\n " . to_string( ) ,
600
+ "--- Resuming from our last conversation ---" . to_string( ) ,
601
+ format!( "User prompt:\n {}" , truncated_question) ,
602
+ format!( "Amazon Q response:\n {}" , truncated_response) ,
603
+ ] ;
604
+
605
+ if let Some ( tools) = tool_summary {
606
+ parts. push ( format ! ( "Tool actions performed:\n {}" , tools) ) ;
607
+ }
608
+
609
+ parts. extend ( [
610
+ "--- End of previous conversation ---" . to_string ( ) ,
611
+ "Now provide a brief summary of our conversation so far." . to_string ( ) ,
612
+ ] ) ;
613
+
614
+ parts. join ( "\n \n " )
615
+ }
616
+ } else {
617
+ "In a few words, summarize our conversation so far." . to_string ( )
618
+ } ;
619
+
620
+ input = Some ( input. unwrap_or ( resume_prompt) ) ;
546
621
cs. tool_manager = tool_manager;
547
622
if let Some ( profile) = cs. current_profile ( ) {
548
623
if agents. switch ( profile) . is_err ( ) {
0 commit comments