Skip to content

Commit 0b71284

Browse files
authored
Merge pull request #22 from devoxx/issue-21
Issue 21
2 parents 881c591 + 854d194 commit 0b71284

File tree

10 files changed

+427
-72
lines changed

10 files changed

+427
-72
lines changed

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ plugins {
55
}
66

77
group = "com.devoxx.genie"
8-
version = "0.0.8"
8+
version = "0.0.9"
99

1010
repositories {
1111
mavenCentral()
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.devoxx.genie.model;
2+
3+
public class ChatInteraction {
4+
private String llmProvider;
5+
private String modelName;
6+
private String question;
7+
private String response;
8+
9+
public ChatInteraction(String llmProvider,
10+
String modelName,
11+
String question,
12+
String response) {
13+
this.llmProvider = llmProvider;
14+
this.modelName = modelName;
15+
this.question = question;
16+
this.response = response;
17+
}
18+
19+
public String getLlmProvider() {
20+
return llmProvider;
21+
}
22+
23+
public void setLlmProvider(String llmProvider) {
24+
this.llmProvider = llmProvider;
25+
}
26+
27+
public String getModelName() {
28+
return modelName;
29+
}
30+
31+
public void setModelName(String modelName) {
32+
this.modelName = modelName;
33+
}
34+
35+
public String getQuestion() {
36+
return question;
37+
}
38+
39+
public void setQuestion(String question) {
40+
this.question = question;
41+
}
42+
43+
public String getResponse() {
44+
return response;
45+
}
46+
47+
public void setResponse(String response) {
48+
this.response = response;
49+
}
50+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.devoxx.genie.service;
2+
3+
public interface ChatHistoryObserver {
4+
void onHistoryUpdated(int currentIndex, int totalMessages);
5+
}
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
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+
}

src/main/java/com/devoxx/genie/ui/CommandHandler.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,16 @@ private void initializeCommands() {
3333
commandMap.put(COMMAND_HELP, listener::showHelp);
3434

3535
commandMap.put(COMMAND_TEST,
36-
() -> listener.executePrompt(settings.getTestPrompt()));
36+
() -> listener.executePrompt(COMMAND_TEST, settings.getTestPrompt()));
3737

3838
commandMap.put(COMMAND_REVIEW,
39-
() -> listener.executePrompt(settings.getReviewPrompt()));
39+
() -> listener.executePrompt(COMMAND_REVIEW, settings.getReviewPrompt()));
4040

4141
commandMap.put(COMMAND_EXPLAIN,
42-
() -> listener.executePrompt(settings.getExplainPrompt()));
42+
() -> listener.executePrompt(COMMAND_EXPLAIN, settings.getExplainPrompt()));
4343

4444
commandMap.put(COMMAND_CUSTOM,
45-
() -> listener.executePrompt(settings.getCustomPrompt()));
45+
() -> listener.executePrompt(COMMAND_CUSTOM, settings.getCustomPrompt()));
4646
}
4747

4848
/**
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.devoxx.genie.ui;
22

33
public interface CommandHandlerListener {
4-
void executePrompt(String command);
4+
void executePrompt(String command, String query);
55
void showHelp();
66
}

0 commit comments

Comments
 (0)