|
| 1 | +package com.devoxx.genie.service; |
| 2 | + |
| 3 | +import com.devoxx.genie.model.ChatInteraction; |
| 4 | +import com.devoxx.genie.ui.component.PlaceholderTextArea; |
| 5 | + |
| 6 | +import javax.swing.*; |
| 7 | +import java.util.ArrayList; |
| 8 | +import java.util.List; |
| 9 | + |
| 10 | +//public class ChatMessageHistoryService { |
| 11 | +// |
| 12 | +// private final List<ChatInteraction> chatHistory = new ArrayList<>(); |
| 13 | +// private int chatIndex = -1; |
| 14 | +// |
| 15 | +// private final JButton nextButton; |
| 16 | +// private final JButton prevButton; |
| 17 | +// private final JButton clearButton; |
| 18 | +// private final JLabel infoLabel; |
| 19 | +// private final PlaceholderTextArea promptInputField; |
| 20 | +// private final JEditorPane promptOutputArea; |
| 21 | +// |
| 22 | +// public ChatMessageHistoryService(JButton prevButton, |
| 23 | +// JButton nextButton, |
| 24 | +// JLabel infoLabel, |
| 25 | +// JButton clearButton, |
| 26 | +// PlaceholderTextArea promptInputArea, |
| 27 | +// JEditorPane promptOutputArea) { |
| 28 | +// this.nextButton = nextButton; |
| 29 | +// this.prevButton = prevButton; |
| 30 | +// this.infoLabel = infoLabel; |
| 31 | +// this.clearButton = clearButton; |
| 32 | +// this.promptOutputArea = promptOutputArea; |
| 33 | +// this.promptInputField = promptInputArea; |
| 34 | +// this.clearButton.addActionListener(e -> clearHistory()); |
| 35 | +// clearButton.setEnabled(false); |
| 36 | +// |
| 37 | +// updateButtons(); |
| 38 | +// } |
| 39 | +// |
| 40 | +// private void updateButtons() { |
| 41 | +// nextButton.setEnabled(chatIndex < chatHistory.size() - 1); |
| 42 | +// prevButton.setEnabled(chatIndex > 0); |
| 43 | +// clearButton.setEnabled(!chatHistory.isEmpty()); |
| 44 | +// if (chatHistory.size() > 1) { |
| 45 | +// infoLabel.setText(String.format("%d/%d", (chatIndex + 1), chatHistory.size())); |
| 46 | +// } else { |
| 47 | +// infoLabel.setText(""); |
| 48 | +// } |
| 49 | +// } |
| 50 | +// |
| 51 | +// /** |
| 52 | +// * Add a chat message to the history. |
| 53 | +// * @param llmProvider the LLM provider |
| 54 | +// * @param modelName the model name |
| 55 | +// * @param question the chat question |
| 56 | +// * @param response the chat response |
| 57 | +// */ |
| 58 | +// public void addMessage(String llmProvider, String modelName, String question, String response) { |
| 59 | +// chatHistory.add(new ChatInteraction(llmProvider, modelName, question, response)); |
| 60 | +// chatIndex++; |
| 61 | +// updateButtons(); |
| 62 | +// } |
| 63 | +// |
| 64 | +// /** |
| 65 | +// * Set the previous message in the chat history. |
| 66 | +// */ |
| 67 | +// public void setPreviousMessage() { |
| 68 | +// if (chatIndex > 0) { |
| 69 | +// chatIndex--; |
| 70 | +// ChatInteraction chatInteraction = chatHistory.get(chatIndex); |
| 71 | +// promptInputField.setText(chatInteraction.getQuestion()); |
| 72 | +// promptOutputArea.setText(chatInteraction.getResponse()); |
| 73 | +// updateButtons(); |
| 74 | +// } |
| 75 | +// } |
| 76 | +// |
| 77 | +// /** |
| 78 | +// * Set the next message in the chat history. |
| 79 | +// */ |
| 80 | +// public void setNextMessage() { |
| 81 | +// if (chatIndex < chatHistory.size() - 1) { |
| 82 | +// chatIndex++; |
| 83 | +// ChatInteraction chatInteraction = chatHistory.get(chatIndex); |
| 84 | +// promptInputField.setText(chatInteraction.getQuestion()); |
| 85 | +// promptOutputArea.setText(chatInteraction.getResponse()); |
| 86 | +// updateButtons(); |
| 87 | +// } |
| 88 | +// } |
| 89 | +// |
| 90 | +// /** |
| 91 | +// * Clear the chat history, reset chat index and disable related buttons. |
| 92 | +// */ |
| 93 | +// public void clearHistory() { |
| 94 | +// chatHistory.clear(); |
| 95 | +// chatIndex = -1; |
| 96 | +// promptInputField.setText(""); |
| 97 | +// updateButtons(); |
| 98 | +// } |
| 99 | +// |
| 100 | +// public int getTotalChats() { |
| 101 | +// return chatHistory.size(); |
| 102 | +// } |
| 103 | +// |
| 104 | +// public int getCurrentIndex() { |
| 105 | +// return chatIndex; |
| 106 | +// } |
| 107 | +//} |
| 108 | + |
| 109 | +public class ChatMessageHistoryService { |
| 110 | + |
| 111 | + private final List<ChatHistoryObserver> observers = new ArrayList<>(); |
| 112 | + private final List<ChatInteraction> chatHistory = new ArrayList<>(); |
| 113 | + private int chatIndex = -1; |
| 114 | + private ChatInteraction currentChatInteraction; |
| 115 | + |
| 116 | + public void addObserver(ChatHistoryObserver observer) { |
| 117 | + observers.add(observer); |
| 118 | + } |
| 119 | + |
| 120 | + private void notifyHistoryUpdated() { |
| 121 | + for (ChatHistoryObserver observer : observers) { |
| 122 | + observer.onHistoryUpdated(chatIndex, chatHistory.size()); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + public void setPreviousMessage() { |
| 127 | + if (chatIndex > 0) { |
| 128 | + chatIndex--; |
| 129 | + currentChatInteraction = chatHistory.get(chatIndex); |
| 130 | + notifyHistoryUpdated(); // Observer pattern implementation |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + public void setNextMessage() { |
| 135 | + if (chatIndex < chatHistory.size() - 1) { |
| 136 | + chatIndex++; |
| 137 | + currentChatInteraction = chatHistory.get(chatIndex); |
| 138 | + notifyHistoryUpdated(); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + public int getChatIndex() { |
| 143 | + return chatIndex; |
| 144 | + } |
| 145 | + |
| 146 | + public int getChatHistorySize() { |
| 147 | + return chatHistory.size(); |
| 148 | + } |
| 149 | + |
| 150 | + public ChatInteraction getCurrentChatInteraction() { |
| 151 | + return chatHistory.get(chatIndex); |
| 152 | + } |
| 153 | + |
| 154 | + public void addMessage(String llmProvider, |
| 155 | + String modelName, |
| 156 | + String question, |
| 157 | + String response) { |
| 158 | + chatHistory.add(new ChatInteraction(llmProvider, modelName, question, response)); |
| 159 | + chatIndex++; |
| 160 | + notifyHistoryUpdated(); |
| 161 | + } |
| 162 | + |
| 163 | + public void clearHistory() { |
| 164 | + chatHistory.clear(); |
| 165 | + chatIndex = -1; |
| 166 | + } |
| 167 | +} |
0 commit comments