@@ -54,15 +54,18 @@ public final class MessageChatMemoryAdvisor implements BaseChatMemoryAdvisor {
54
54
55
55
private final Scheduler scheduler ;
56
56
57
+ private final boolean storeAllUserMessages ;
58
+
57
59
private MessageChatMemoryAdvisor (ChatMemory chatMemory , String defaultConversationId , int order ,
58
- Scheduler scheduler ) {
60
+ Scheduler scheduler , boolean storeAllUserMessages ) {
59
61
Assert .notNull (chatMemory , "chatMemory cannot be null" );
60
62
Assert .hasText (defaultConversationId , "defaultConversationId cannot be null or empty" );
61
63
Assert .notNull (scheduler , "scheduler cannot be null" );
62
64
this .chatMemory = chatMemory ;
63
65
this .defaultConversationId = defaultConversationId ;
64
66
this .order = order ;
65
67
this .scheduler = scheduler ;
68
+ this .storeAllUserMessages = storeAllUserMessages ;
66
69
}
67
70
68
71
@ Override
@@ -88,12 +91,19 @@ public ChatClientRequest before(ChatClientRequest chatClientRequest, AdvisorChai
88
91
89
92
// 3. Create a new request with the advised messages.
90
93
ChatClientRequest processedChatClientRequest = chatClientRequest .mutate ()
91
- .prompt (chatClientRequest .prompt ().mutate ().messages (processedMessages ).build ())
92
- .build ();
94
+ .prompt (chatClientRequest .prompt ().mutate ().messages (processedMessages ).build ())
95
+ .build ();
93
96
94
97
// 4. Add the new user message to the conversation memory.
95
- UserMessage userMessage = processedChatClientRequest .prompt ().getUserMessage ();
96
- this .chatMemory .add (conversationId , userMessage );
98
+ if (this .storeAllUserMessages ) {
99
+ // Store all user messages: add the new message to the existing message list
100
+ List allUserMessages = processedChatClientRequest .prompt ().getUserMessages ();
101
+ this .chatMemory .add (conversationId , allUserMessages );
102
+ } else {
103
+ // Store only the latest user message
104
+ UserMessage userMessage = processedChatClientRequest .prompt ().getUserMessage ();
105
+ this .chatMemory .add (conversationId , userMessage );
106
+ }
97
107
98
108
return processedChatClientRequest ;
99
109
}
@@ -103,10 +113,10 @@ public ChatClientResponse after(ChatClientResponse chatClientResponse, AdvisorCh
103
113
List <Message > assistantMessages = new ArrayList <>();
104
114
if (chatClientResponse .chatResponse () != null ) {
105
115
assistantMessages = chatClientResponse .chatResponse ()
106
- .getResults ()
107
- .stream ()
108
- .map (g -> (Message ) g .getOutput ())
109
- .toList ();
116
+ .getResults ()
117
+ .stream ()
118
+ .map (g -> (Message ) g .getOutput ())
119
+ .toList ();
110
120
}
111
121
this .chatMemory .add (this .getConversationId (chatClientResponse .context (), this .defaultConversationId ),
112
122
assistantMessages );
@@ -121,11 +131,11 @@ public Flux<ChatClientResponse> adviseStream(ChatClientRequest chatClientRequest
121
131
122
132
// Process the request with the before method
123
133
return Mono .just (chatClientRequest )
124
- .publishOn (scheduler )
125
- .map (request -> this .before (request , streamAdvisorChain ))
126
- .flatMapMany (streamAdvisorChain ::nextStream )
127
- .transform (flux -> new ChatClientMessageAggregator ().aggregateChatClientResponse (flux ,
128
- response -> this .after (response , streamAdvisorChain )));
134
+ .publishOn (scheduler )
135
+ .map (request -> this .before (request , streamAdvisorChain ))
136
+ .flatMapMany (streamAdvisorChain ::nextStream )
137
+ .transform (flux -> new ChatClientMessageAggregator ().aggregateChatClientResponse (flux ,
138
+ response -> this .after (response , streamAdvisorChain )));
129
139
}
130
140
131
141
public static Builder builder (ChatMemory chatMemory ) {
@@ -142,6 +152,8 @@ public static final class Builder {
142
152
143
153
private ChatMemory chatMemory ;
144
154
155
+ private boolean storeAllUserMessages = false ;
156
+
145
157
private Builder (ChatMemory chatMemory ) {
146
158
this .chatMemory = chatMemory ;
147
159
}
@@ -171,12 +183,22 @@ public Builder scheduler(Scheduler scheduler) {
171
183
return this ;
172
184
}
173
185
186
+ /**
187
+ * Configure whether to store all user messages or only the latest one.
188
+ * @param storeAllUserMessages true to store all user messages, false to store only the latest
189
+ * @return the builder
190
+ */
191
+ public Builder storeAllUserMessages (boolean storeAllUserMessages ) {
192
+ this .storeAllUserMessages = storeAllUserMessages ;
193
+ return this ;
194
+ }
195
+
174
196
/**
175
197
* Build the advisor.
176
198
* @return the advisor
177
199
*/
178
200
public MessageChatMemoryAdvisor build () {
179
- return new MessageChatMemoryAdvisor (this .chatMemory , this .conversationId , this .order , this .scheduler );
201
+ return new MessageChatMemoryAdvisor (this .chatMemory , this .conversationId , this .order , this .scheduler , this . storeAllUserMessages );
180
202
}
181
203
182
204
}
0 commit comments