1
1
use clap:: Args ;
2
2
3
+ use crate :: cli:: chat:: consts:: MAX_USER_MESSAGE_SIZE ;
4
+ use crate :: cli:: chat:: message:: UserMessageContent ;
3
5
use crate :: cli:: chat:: {
4
6
ChatError ,
5
7
ChatSession ,
@@ -24,17 +26,60 @@ How it works
24
26
• Creates an AI-generated summary of your conversation
25
27
• Retains key information, code, and tool executions in the summary
26
28
• Clears the conversation history to free up space
27
- • The assistant will reference the summary context in future responses"
29
+ • The assistant will reference the summary context in future responses
30
+
31
+ Compaction will be automatically performed whenever the context window overflows.
32
+ To disable this behavior, run: `q settings chat.disableAutoCompaction true`"
28
33
) ]
29
34
pub struct CompactArgs {
30
35
/// The prompt to use when generating the summary
31
36
prompt : Option < String > ,
32
37
#[ arg( long) ]
33
38
show_summary : bool ,
39
+ /// The number of user and assistant message pairs to exclude from the summarization.
40
+ #[ arg( long) ]
41
+ messages_to_exclude : Option < usize > ,
42
+ /// Whether or not large messages should be truncated.
43
+ #[ arg( long) ]
44
+ truncate_large_messages : Option < bool > ,
45
+ /// Maximum allowed size of messages in the conversation history. Requires
46
+ /// truncate_large_messages to be set.
47
+ #[ arg( long, requires = "truncate_large_messages" ) ]
48
+ max_message_length : Option < usize > ,
34
49
}
35
50
36
51
impl CompactArgs {
37
52
pub async fn execute ( self , os : & Os , session : & mut ChatSession ) -> Result < ChatState , ChatError > {
38
- session. compact_history ( os, self . prompt , self . show_summary , true ) . await
53
+ let default = CompactStrategy :: default ( ) ;
54
+ session
55
+ . compact_history ( os, self . prompt , self . show_summary , CompactStrategy {
56
+ messages_to_exclude : self . messages_to_exclude . unwrap_or ( default. messages_to_exclude ) ,
57
+ truncate_large_messages : self . truncate_large_messages . unwrap_or ( default. truncate_large_messages ) ,
58
+ max_message_length : self . max_message_length . map_or ( default. max_message_length , |v| {
59
+ v. clamp ( UserMessageContent :: TRUNCATED_SUFFIX . len ( ) , MAX_USER_MESSAGE_SIZE )
60
+ } ) ,
61
+ } )
62
+ . await
63
+ }
64
+ }
65
+
66
+ /// Parameters for performing the history compaction request.
67
+ #[ derive( Debug , Copy , Clone ) ]
68
+ pub struct CompactStrategy {
69
+ /// Number of user/assistant pairs to exclude from the history as part of compaction.
70
+ pub messages_to_exclude : usize ,
71
+ /// Whether or not to truncate large messages in the history.
72
+ pub truncate_large_messages : bool ,
73
+ /// Maximum allowed size of messages in the conversation history.
74
+ pub max_message_length : usize ,
75
+ }
76
+
77
+ impl Default for CompactStrategy {
78
+ fn default ( ) -> Self {
79
+ Self {
80
+ messages_to_exclude : Default :: default ( ) ,
81
+ truncate_large_messages : Default :: default ( ) ,
82
+ max_message_length : MAX_USER_MESSAGE_SIZE ,
83
+ }
39
84
}
40
85
}
0 commit comments