diff --git a/semantickernel-api/src/main/java/com/microsoft/semantickernel/services/chatcompletion/ChatHistory.java b/semantickernel-api/src/main/java/com/microsoft/semantickernel/services/chatcompletion/ChatHistory.java index 903669b3..ea910c01 100644 --- a/semantickernel-api/src/main/java/com/microsoft/semantickernel/services/chatcompletion/ChatHistory.java +++ b/semantickernel-api/src/main/java/com/microsoft/semantickernel/services/chatcompletion/ChatHistory.java @@ -5,13 +5,11 @@ import com.microsoft.semantickernel.services.chatcompletion.message.ChatMessageTextContent; import java.nio.charset.Charset; import java.util.ArrayList; -import java.util.Collection; import java.util.Collections; import java.util.Iterator; import java.util.List; import java.util.Optional; import java.util.Spliterator; -import java.util.concurrent.ConcurrentLinkedQueue; import java.util.function.Consumer; import javax.annotation.Nullable; @@ -20,7 +18,7 @@ */ public class ChatHistory implements Iterable> { - private final Collection> chatMessageContents; + private final List> chatMessageContents; /** * The default constructor @@ -35,7 +33,7 @@ public ChatHistory() { * @param instructions The instructions to add to the chat history */ public ChatHistory(@Nullable String instructions) { - this.chatMessageContents = new ConcurrentLinkedQueue<>(); + this.chatMessageContents = Collections.synchronizedList(new ArrayList<>()); if (instructions != null) { this.chatMessageContents.add( ChatMessageTextContent.systemMessage(instructions)); @@ -48,7 +46,8 @@ public ChatHistory(@Nullable String instructions) { * @param chatMessageContents The chat message contents to add to the chat history */ public ChatHistory(List> chatMessageContents) { - this.chatMessageContents = new ConcurrentLinkedQueue<>(chatMessageContents); + this.chatMessageContents = Collections + .synchronizedList(new ArrayList<>(chatMessageContents)); } /** @@ -70,7 +69,7 @@ public Optional> getLastMessage() { return Optional.empty(); } return Optional - .of(((ConcurrentLinkedQueue>) chatMessageContents).peek()); + .of(chatMessageContents.get(chatMessageContents.size() - 1)); } /**