Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -20,7 +18,7 @@
*/
public class ChatHistory implements Iterable<ChatMessageContent<?>> {

private final Collection<ChatMessageContent<?>> chatMessageContents;
private final List<ChatMessageContent<?>> chatMessageContents;

/**
* The default constructor
Expand All @@ -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));
Expand All @@ -48,7 +46,8 @@ public ChatHistory(@Nullable String instructions) {
* @param chatMessageContents The chat message contents to add to the chat history
*/
public ChatHistory(List<? extends ChatMessageContent<?>> chatMessageContents) {
this.chatMessageContents = new ConcurrentLinkedQueue<>(chatMessageContents);
this.chatMessageContents = Collections
.synchronizedList(new ArrayList<>(chatMessageContents));
}

/**
Expand All @@ -70,7 +69,7 @@ public Optional<ChatMessageContent<?>> getLastMessage() {
return Optional.empty();
}
return Optional
.of(((ConcurrentLinkedQueue<ChatMessageContent<?>>) chatMessageContents).peek());
.of(chatMessageContents.get(chatMessageContents.size() - 1));
}

/**
Expand Down