-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
25 changed files
with
530 additions
and
29 deletions.
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
src/main/java/com/devoxx/genie/model/conversation/ChatMessage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.devoxx.genie.model.conversation; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
|
||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
@Getter | ||
@Setter | ||
public class ChatMessage { | ||
private boolean isUser; | ||
private String content; | ||
private String timestamp; | ||
} |
26 changes: 26 additions & 0 deletions
26
src/main/java/com/devoxx/genie/model/conversation/Conversation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.devoxx.genie.model.conversation; | ||
|
||
|
||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
@NoArgsConstructor | ||
@Setter | ||
@Getter | ||
public class Conversation { | ||
private String id; | ||
private String timestamp; | ||
private String title; | ||
private String llmProvider; | ||
private String modelName; | ||
private Boolean apiKeyUsed; | ||
private Long inputCost; | ||
private Long outputCost; | ||
private Integer contextWindow; | ||
private long executionTimeMs; | ||
private List<ChatMessage> messages = new ArrayList<>(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package com.devoxx.genie.service; | ||
|
||
import com.devoxx.genie.model.conversation.ChatMessage; | ||
import com.devoxx.genie.model.conversation.Conversation; | ||
import com.devoxx.genie.model.request.ChatMessageContext; | ||
import com.devoxx.genie.ui.listener.ConversationEventListener; | ||
import com.devoxx.genie.ui.topic.AppTopics; | ||
import com.intellij.openapi.application.ApplicationManager; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.UUID; | ||
|
||
public class ChatService implements ConversationEventListener { | ||
|
||
private final ConversationStorageService storageService; | ||
|
||
public ChatService(ConversationStorageService storageService) { | ||
this.storageService = storageService; | ||
|
||
ApplicationManager.getApplication().getMessageBus() | ||
.connect() | ||
.subscribe(AppTopics.CONVERSATION_TOPIC, this); | ||
} | ||
|
||
@Override | ||
public void onNewConversation(@NotNull ChatMessageContext chatMessageContext) { | ||
saveConversation(chatMessageContext); | ||
} | ||
|
||
private void saveConversation(@NotNull ChatMessageContext chatMessageContext) { | ||
Conversation conversation = new Conversation(); | ||
conversation.setId(UUID.randomUUID().toString()); | ||
conversation.setTitle(generateTitle(chatMessageContext.getUserPrompt())); | ||
conversation.setTimestamp(LocalDateTime.now().toString()); | ||
conversation.setModelName(chatMessageContext.getLanguageModel().getModelName()); | ||
conversation.setExecutionTimeMs(chatMessageContext.getExecutionTimeMs()); | ||
conversation.setApiKeyUsed(chatMessageContext.getLanguageModel().isApiKeyUsed()); | ||
conversation.setLlmProvider(chatMessageContext.getLanguageModel().getProvider().name()); | ||
|
||
conversation.getMessages().add(new ChatMessage(true, chatMessageContext.getUserPrompt(), LocalDateTime.now().toString())); | ||
conversation.getMessages().add(new ChatMessage(false, chatMessageContext.getAiMessage().text(), LocalDateTime.now().toString())); | ||
|
||
storageService.addConversation(conversation); | ||
} | ||
|
||
private String generateTitle(@NotNull String userPrompt) { | ||
return userPrompt.length() > 30 ? userPrompt.substring(0, 30) + "..." : userPrompt; | ||
} | ||
|
||
public void startNewConversation(String title) { | ||
Conversation conversation = new Conversation(); | ||
conversation.setTitle(title); | ||
conversation.setTimestamp(LocalDateTime.now().toString()); | ||
conversation.setMessages(new ArrayList<>()); | ||
storageService.addConversation(conversation); | ||
} | ||
|
||
// TODO: Implement the loadConversations method | ||
public List<Conversation> loadConversations() { | ||
return storageService.getConversations(); | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
src/main/java/com/devoxx/genie/service/ConversationStorageService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package com.devoxx.genie.service; | ||
|
||
import com.devoxx.genie.model.conversation.Conversation; | ||
import com.intellij.openapi.application.ApplicationManager; | ||
import com.intellij.openapi.components.PersistentStateComponent; | ||
import com.intellij.openapi.components.Service; | ||
import com.intellij.openapi.components.State; | ||
import com.intellij.openapi.components.Storage; | ||
import com.intellij.util.xmlb.XmlSerializerUtil; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import javax.xml.bind.annotation.XmlAccessType; | ||
import javax.xml.bind.annotation.XmlAccessorType; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
@Service | ||
@State(name = "DevoxxGenieConversationStorage", storages = @Storage("devoxxgenie-conversations.xml")) | ||
public final class ConversationStorageService implements PersistentStateComponent<ConversationStorageService.State> { | ||
|
||
private final State myState = new State(); | ||
|
||
public static ConversationStorageService getInstance() { | ||
return ApplicationManager.getApplication().getService(ConversationStorageService.class); | ||
} | ||
|
||
@Override | ||
public State getState() { | ||
return myState; | ||
} | ||
|
||
@Override | ||
public void loadState(@NotNull State state) { | ||
XmlSerializerUtil.copyBean(state, myState); | ||
} | ||
|
||
public void addConversation(Conversation conversation) { | ||
myState.conversations.add(conversation); | ||
saveState(); | ||
} | ||
|
||
public @NotNull List<Conversation> getConversations() { | ||
return new ArrayList<>(myState.conversations); | ||
} | ||
|
||
public void removeConversation(Conversation conversation) { | ||
myState.conversations.remove(conversation); | ||
saveState(); | ||
} | ||
|
||
public void clearAllConversations() { | ||
myState.conversations.clear(); | ||
saveState(); | ||
} | ||
|
||
private void saveState() { | ||
ApplicationManager.getApplication().invokeLater(() -> | ||
ApplicationManager.getApplication().saveSettings() | ||
); | ||
} | ||
|
||
@XmlAccessorType(XmlAccessType.FIELD) | ||
public static class State { | ||
public List<Conversation> conversations = new ArrayList<>(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
src/main/java/com/devoxx/genie/ui/listener/ConversationEventListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.devoxx.genie.ui.listener; | ||
|
||
import com.devoxx.genie.model.request.ChatMessageContext; | ||
|
||
public interface ConversationEventListener { | ||
void onNewConversation(ChatMessageContext chatMessageContext); | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/com/devoxx/genie/ui/listener/ConversationSelectionListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.devoxx.genie.ui.listener; | ||
|
||
import com.devoxx.genie.model.conversation.Conversation; | ||
|
||
public interface ConversationSelectionListener { | ||
void onConversationSelected(Conversation conversation); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.