Skip to content

Commit 0a6fa79

Browse files
committed
Merged with latest changes
1 parent c4b955f commit 0a6fa79

File tree

5 files changed

+44
-42
lines changed

5 files changed

+44
-42
lines changed

plugin/src/software/aws/toolkits/eclipse/amazonq/chat/ChatCommunicationManager.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,27 @@
1313
import software.aws.toolkits.eclipse.amazonq.chat.models.GenericTabParams;
1414
import software.aws.toolkits.eclipse.amazonq.exception.AmazonQPluginException;
1515
import software.aws.toolkits.eclipse.amazonq.util.JsonHandler;
16+
import software.aws.toolkits.eclipse.amazonq.util.PluginLogger;
1617
import software.aws.toolkits.eclipse.amazonq.views.model.Command;
1718

1819
/**
19-
* ChatCommunicationManager is responsible for managing communication between
20-
* the Amazon Q Eclipse Plugin and the LSP server as well as communication
21-
* between the Amazon Q Eclipse Plugin and the webview. It is implemented
22-
* as a singleton to centralize control of all communication in the plugin.
20+
* ChatCommunicationManager is a central component of the Amazon Q Eclipse Plugin that
21+
* acts as a bridge between the plugin's UI and the LSP server. It is also responsible
22+
* for managing communication between the plugin and the webview used for displaying
23+
* chat conversations. It is implemented as a singleton to centralize control of all
24+
* communication in the plugin.
2325
*/
2426
public final class ChatCommunicationManager {
2527
private static ChatCommunicationManager instance;
2628

2729
private final JsonHandler jsonHandler;
2830
private final CompletableFuture<ChatMessageProvider> chatMessageProvider;
29-
private final ChatPartialResultManager chatPartialResultManager;
31+
private final ChatPartialResultMap chatPartialResultMap;
3032

3133
private ChatCommunicationManager() {
3234
this.jsonHandler = new JsonHandler();
3335
this.chatMessageProvider = ChatMessageProvider.createAsync();
34-
this.chatPartialResultManager = ChatPartialResultManager.getInstance();
36+
this.chatPartialResultMap = new ChatPartialResultMap();
3537
}
3638

3739
public static synchronized ChatCommunicationManager getInstance() {
@@ -41,13 +43,13 @@ public static synchronized ChatCommunicationManager getInstance() {
4143
return instance;
4244
}
4345

44-
public CompletableFuture<ChatResult> sendMessageToChatServer(final Command command, final Object params) {
46+
public CompletableFuture<ChatResult> sendMessageToChatServer(final Browser browser, final Command command, final Object params) {
4547
return chatMessageProvider.thenCompose(chatMessageProvider -> {
4648
try {
4749
switch (command) {
4850
case CHAT_SEND_PROMPT:
4951
ChatRequestParams chatRequestParams = jsonHandler.convertObject(params, ChatRequestParams.class);
50-
return chatMessageProvider.sendChatPrompt(chatRequestParams);
52+
return chatMessageProvider.sendChatPrompt(browser, chatRequestParams);
5153
case CHAT_READY:
5254
chatMessageProvider.sendChatReady();
5355
return CompletableFuture.completedFuture(null);
@@ -68,7 +70,7 @@ public CompletableFuture<ChatResult> sendMessageToChatServer(final Command comma
6870
public void sendMessageToChatUI(final Browser browser, final ChatUIInboundCommand command) {
6971
// Mynah-ui will not render the partial result if null values are included. Must serialize with ignoreNulls set to True.
7072
Boolean ignoreNull = true;
71-
String message = this.jsonHandler.serialize(command, ignoreNull);
73+
String message = jsonHandler.serialize(command, ignoreNull);
7274

7375
String script = "window.postMessage(" + message + ");";
7476
browser.getDisplay().asyncExec(() -> {

plugin/src/software/aws/toolkits/eclipse/amazonq/chat/ChatMessage.java

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,13 @@
22

33
package software.aws.toolkits.eclipse.amazonq.chat;
44

5-
import java.util.concurrent.ExecutionException;
5+
import java.util.concurrent.CompletableFuture;
66

77
import org.eclipse.swt.browser.Browser;
88

99
import software.aws.toolkits.eclipse.amazonq.chat.models.ChatRequestParams;
1010
import software.aws.toolkits.eclipse.amazonq.chat.models.ChatResult;
11-
import software.aws.toolkits.eclipse.amazonq.exception.AmazonQPluginException;
1211
import software.aws.toolkits.eclipse.amazonq.lsp.AmazonQLspServer;
13-
import software.aws.toolkits.eclipse.amazonq.util.PluginLogger;
14-
import software.aws.toolkits.eclipse.amazonq.views.model.Command;
1512

1613
public class ChatMessage {
1714
private final Browser browser;
@@ -38,23 +35,19 @@ public String getPartialResultToken() {
3835
return chatRequestParams.getPartialResultToken();
3936
}
4037

41-
public ChatResult sendChatMessageWithProgress() {
42-
try {
43-
// Retrieving the chat result is expected to be a long-running process with intermittent progress notifications being sent
44-
// from the LSP server. The progress notifications provide a token and a result - we are utilizing this token to
45-
// ChatMessage mapping to acquire the associated ChatMessage.
46-
String partialResultToken = chatCommunicationManager.addPartialChatMessage(this);
47-
48-
PluginLogger.info("Sending " + Command.CHAT_SEND_PROMPT + " message to Amazon Q LSP server");
49-
ChatResult chatResult = amazonQLspServer.sendChatPrompt(chatRequestParams).get();
50-
51-
// The mapping entry no longer needs to be maintained once the final result is retrieved.
52-
chatCommunicationManager.removePartialChatMessage(partialResultToken);
53-
54-
return chatResult;
55-
} catch (InterruptedException | ExecutionException e) {
56-
PluginLogger.error("Error occurred while sending " + Command.CHAT_SEND_PROMPT + " message to Amazon Q LSP server", e);
57-
throw new AmazonQPluginException(e);
58-
}
38+
public CompletableFuture<ChatResult> sendChatMessageWithProgress() {
39+
// Retrieving the chat result is expected to be a long-running process with intermittent progress notifications being sent
40+
// from the LSP server. The progress notifications provide a token and a partial result Object - we are utilizing a token to
41+
// ChatMessage mapping to acquire the associated ChatMessage so we can formulate a message for the UI.
42+
String partialResultToken = chatCommunicationManager.addPartialChatMessage(this);
43+
44+
CompletableFuture<ChatResult> chatResult = amazonQLspServer.sendChatPrompt(chatRequestParams)
45+
.thenApply(result -> {
46+
// The mapping entry no longer needs to be maintained once the final result is retrieved.
47+
chatCommunicationManager.removePartialChatMessage(partialResultToken);
48+
return result;
49+
});
50+
51+
return chatResult;
5952
}
6053
}

plugin/src/software/aws/toolkits/eclipse/amazonq/chat/ChatMessageProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ private ChatMessageProvider(AmazonQLspServer amazonQLspServer) {
3131
}
3232

3333
public CompletableFuture<ChatResult> sendChatPrompt(Browser browser, final ChatRequestParams chatRequestParams) {
34-
ChatMessage chatMessage = new ChatMessage(amazonQLspServer, chatRequestParams);
34+
ChatMessage chatMessage = new ChatMessage(amazonQLspServer, browser, chatRequestParams);
3535
return chatMessage.sendChatMessageWithProgress();
3636
}
3737

plugin/src/software/aws/toolkits/eclipse/amazonq/chat/ChatPartialResultMap.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,24 @@
66
import java.util.concurrent.ConcurrentHashMap;
77

88
import software.aws.toolkits.eclipse.amazonq.chat.models.ChatResult;
9+
import software.aws.toolkits.eclipse.amazonq.lsp.AmazonQLspClientImpl;
910
import software.aws.toolkits.eclipse.amazonq.util.PluginLogger;
1011

1112
/**
12-
* ChatPartialResultMap is responsible for maintaining a mapping between
13-
* partial result tokens and the associated ChatMessage objects.
14-
*
15-
* $/progress notifications are caught and handled in the AmazonQLspClientImpl
16-
* notifyProgress method. Within a progress notification, we are provided ProgressParams
17-
* containing a token and a partial result object. The tokenToChatMessage map in
18-
* this class allows us to find the associated ChatMessage associated with the token.
13+
* ChatPartialResultMap is a utility class responsible for managing the mapping between
14+
* partial result tokens and their corresponding ChatMessage objects in the Amazon Q plugin for Eclipse.
1915
*
20-
* @see AmazonQLspClientImpl#notifyProgress(ProgressParams)
16+
* The Language Server Protocol (LSP) server sends progress notifications during long-running operations,
17+
* such as processing chat requests. These notifications include a token that identifies the specific operation
18+
* and a partial result object containing the progress information.
19+
*
20+
* This class maintains a concurrent map (tokenToChatMessageMap) that associates each token with
21+
* its respective ChatMessage object. This mapping is crucial for correctly updating the chat UI
22+
* with the latest progress information as it becomes available from the LSP server.
23+
*
24+
* The progress notifications are handled by the {@link AmazonQLspClientImpl#notifyProgress(ProgressParams)}
25+
* method, which retrieves the corresponding ChatMessage object from the tokenToChatMessageMap using
26+
* the token provided in the ProgressParams. The ChatMessage can then be updated with the partial result.
2127
*/
2228
public class ChatPartialResultMap {
2329

plugin/src/software/aws/toolkits/eclipse/amazonq/views/AmazonQChatViewActionHandler.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import software.aws.toolkits.eclipse.amazonq.chat.ChatCommunicationManager;
1212
import software.aws.toolkits.eclipse.amazonq.chat.ChatMessage;
1313
import software.aws.toolkits.eclipse.amazonq.chat.models.ChatRequestParams;
14+
import software.aws.toolkits.eclipse.amazonq.chat.models.ChatResult;
1415
import software.aws.toolkits.eclipse.amazonq.chat.models.ChatUIInboundCommand;
1516
import software.aws.toolkits.eclipse.amazonq.chat.models.ChatUIInboundCommandName;
1617
import software.aws.toolkits.eclipse.amazonq.exception.AmazonQPluginException;
@@ -41,12 +42,12 @@ public final void handleCommand(final ParsedCommand parsedCommand, final Browser
4142

4243
switch (command) {
4344
case CHAT_SEND_PROMPT:
44-
chatCommunicationManager.sendMessageToChatServer(command, params)
45+
chatCommunicationManager.sendMessageToChatServer(browser, command, params)
4546
.thenAccept(chatResult -> {
4647
ChatRequestParams chatRequestParams = jsonHandler.convertObject(params, ChatRequestParams.class);
4748
ChatUIInboundCommand chatUIInboundCommand = new ChatUIInboundCommand(
4849
ChatUIInboundCommandName.ChatPrompt.toString(),
49-
chatRequestParams.tabId(),
50+
chatRequestParams.getTabId(),
5051
chatResult,
5152
false
5253
);

0 commit comments

Comments
 (0)