-
Notifications
You must be signed in to change notification settings - Fork 376
Expand file tree
/
Copy pathConversationService.java
More file actions
148 lines (125 loc) · 5.05 KB
/
ConversationService.java
File metadata and controls
148 lines (125 loc) · 5.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package ee.carlrobert.codegpt.conversations;
import com.intellij.openapi.components.Service;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.Project;
import ee.carlrobert.codegpt.completions.ChatCompletionParameters;
import ee.carlrobert.codegpt.conversations.message.Message;
import java.time.LocalDateTime;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@Service(Service.Level.PROJECT)
public final class ConversationService {
private static final Logger LOG = Logger.getInstance(ConversationService.class);
private final Project project;
private final ConversationsState conversationState;
public ConversationService(Project project) {
this.project = project;
this.conversationState = ConversationsState.getInstance(project);
}
public static ConversationService getInstance(@NotNull Project project) {
return project.getService(ConversationService.class);
}
public List<Conversation> getSortedConversations() {
return conversationState.conversations
.stream()
.sorted(Comparator.comparing(Conversation::getUpdatedOn).reversed())
.toList();
}
public Conversation createConversation() {
var conversation = new Conversation();
conversation.setId(UUID.randomUUID());
conversation.setCreatedOn(LocalDateTime.now());
conversation.setUpdatedOn(LocalDateTime.now());
return conversation;
}
public void addConversation(Conversation conversation) {
conversationState.conversations.add(conversation);
}
public void saveMessage(String response, ChatCompletionParameters callParameters) {
var conversation = callParameters.getConversation();
var message = callParameters.getMessage();
var conversationMessages = conversation.getMessages();
if (callParameters.getRetry() && !conversationMessages.isEmpty()) {
var messageToBeSaved = conversationMessages.stream()
.filter(item -> item.getId().equals(message.getId()))
.findFirst().orElseThrow();
messageToBeSaved.setResponse(response);
saveConversation(conversation);
return;
}
message.setResponse(response);
conversation.addMessage(message);
saveConversation(conversation);
}
public void saveMessage(@NotNull Conversation conversation, @NotNull Message message) {
conversation.setUpdatedOn(LocalDateTime.now());
conversation.addMessage(message);
saveConversation(conversation);
}
public void saveConversation(Conversation conversation) {
conversation.setUpdatedOn(LocalDateTime.now());
conversationState.setCurrentConversation(conversation);
}
public Conversation startConversation() {
var projectPath = project.getBasePath();
var conversation = createConversation();
conversation.setProjectPath(projectPath);
conversationState.setCurrentConversation(conversation);
addConversation(conversation);
return conversation;
}
public void clearAll() {
conversationState.conversations.clear();
conversationState.setCurrentConversation(null);
}
public void deleteConversation(Conversation conversation) {
conversationState.conversations.removeIf(it -> it.getId() == conversation.getId());
}
public void deleteSelectedConversation() {
var nextConversation = getPreviousConversation();
if (nextConversation.isEmpty()) {
nextConversation = getNextConversation();
}
var currentConversation = getCurrentConversation();
if (currentConversation != null) {
deleteConversation(currentConversation);
nextConversation.ifPresent(conversationState::setCurrentConversation);
} else {
throw new RuntimeException("Tried to delete a conversation that hasn't been set");
}
}
public void discardTokenLimits(Conversation conversation) {
conversation.discardTokenLimits();
saveConversation(conversation);
}
public @Nullable Conversation getCurrentConversation() {
return conversationState.getCurrentConversation();
}
public Optional<Conversation> getPreviousConversation() {
return tryGetNextOrPreviousConversation(true);
}
public Optional<Conversation> getNextConversation() {
return tryGetNextOrPreviousConversation(false);
}
private Optional<Conversation> tryGetNextOrPreviousConversation(boolean isPrevious) {
var currentConversation = getCurrentConversation();
if (currentConversation != null) {
var sortedConversations = getSortedConversations();
for (int i = 0; i < sortedConversations.size(); i++) {
var conversation = sortedConversations.get(i);
if (conversation != null && conversation.getId().equals(currentConversation.getId())) {
// higher index indicates older conversation
var previousIndex = isPrevious ? i + 1 : i - 1;
if (isPrevious ? previousIndex < sortedConversations.size() : previousIndex != -1) {
return Optional.of(sortedConversations.get(previousIndex));
}
}
}
}
return Optional.empty();
}
}