From 3b0997782d2b2ff95f6b6dc36846fba3b7239526 Mon Sep 17 00:00:00 2001 From: Jordan Ang Date: Fri, 20 Sep 2024 11:16:02 -0700 Subject: [PATCH] Send chat prompt message to LSP (#26) --- .../chat/ChatCommunicationManager.java | 16 ++++++--- .../amazonq/chat/ChatMessageProvider.java | 35 ++++++++++++++++--- .../eclipse/amazonq/lsp/AmazonQLspServer.java | 5 +++ .../views/AmazonQChatViewActionHandler.java | 9 +++-- .../amazonq/views/AmazonQChatWebview.java | 14 ++++---- .../eclipse/amazonq/views/model/Command.java | 1 + 6 files changed, 62 insertions(+), 18 deletions(-) diff --git a/plugin/src/software/aws/toolkits/eclipse/amazonq/chat/ChatCommunicationManager.java b/plugin/src/software/aws/toolkits/eclipse/amazonq/chat/ChatCommunicationManager.java index 4d3a919e..1f9b9bac 100644 --- a/plugin/src/software/aws/toolkits/eclipse/amazonq/chat/ChatCommunicationManager.java +++ b/plugin/src/software/aws/toolkits/eclipse/amazonq/chat/ChatCommunicationManager.java @@ -2,9 +2,11 @@ package software.aws.toolkits.eclipse.amazonq.chat; +import software.aws.toolkits.eclipse.amazonq.chat.models.ChatRequestParams; +import software.aws.toolkits.eclipse.amazonq.chat.models.ChatResult; import software.aws.toolkits.eclipse.amazonq.chat.models.GenericTabParams; +import software.aws.toolkits.eclipse.amazonq.exception.AmazonQPluginException; import software.aws.toolkits.eclipse.amazonq.util.JsonHandler; -import software.aws.toolkits.eclipse.amazonq.util.PluginLogger; import software.aws.toolkits.eclipse.amazonq.views.model.Command; public final class ChatCommunicationManager { @@ -17,20 +19,24 @@ public ChatCommunicationManager() { this.chatMessageProvider = new ChatMessageProvider(); } - public void sendMessageToChatServerAsync(final Command command, final Object params) { + public ChatResult sendMessageToChatServerAsync(final Command command, final Object params) { String jsonParams = jsonHandler.serialize(params); switch (command) { + case CHAT_SEND_PROMPT: + ChatRequestParams chatRequestParams = jsonHandler.deserialize(jsonParams, ChatRequestParams.class); + ChatResult result = chatMessageProvider.sendChatPrompt(chatRequestParams); + return result; case CHAT_READY: chatMessageProvider.sendChatReady(); - break; + return null; case CHAT_TAB_ADD: GenericTabParams tabParams = jsonHandler.deserialize(jsonParams, GenericTabParams.class); chatMessageProvider.sendTabAdd(tabParams); - break; + return null; default: - PluginLogger.error("Unhandled chat command: " + command.toString()); + throw new AmazonQPluginException("Unhandled command in ChatCommunicationManager: " + command.toString()); } } } diff --git a/plugin/src/software/aws/toolkits/eclipse/amazonq/chat/ChatMessageProvider.java b/plugin/src/software/aws/toolkits/eclipse/amazonq/chat/ChatMessageProvider.java index 530104e1..d1184b21 100644 --- a/plugin/src/software/aws/toolkits/eclipse/amazonq/chat/ChatMessageProvider.java +++ b/plugin/src/software/aws/toolkits/eclipse/amazonq/chat/ChatMessageProvider.java @@ -4,7 +4,10 @@ import java.util.concurrent.ExecutionException; +import software.aws.toolkits.eclipse.amazonq.chat.models.ChatRequestParams; +import software.aws.toolkits.eclipse.amazonq.chat.models.ChatResult; import software.aws.toolkits.eclipse.amazonq.chat.models.GenericTabParams; +import software.aws.toolkits.eclipse.amazonq.exception.AmazonQPluginException; import software.aws.toolkits.eclipse.amazonq.lsp.AmazonQLspServer; import software.aws.toolkits.eclipse.amazonq.providers.LspProvider; import software.aws.toolkits.eclipse.amazonq.util.PluginLogger; @@ -18,17 +21,39 @@ public ChatMessageProvider() { try { amazonQLspServer = LspProvider.getAmazonQServer().get(); } catch (InterruptedException | ExecutionException e) { - PluginLogger.error("Error occurred while retrieving Amazon Q LSP server. Failed to instantiate ChatMessageProvider."); + PluginLogger.error("Error occurred while retrieving Amazon Q LSP server. Failed to instantiate ChatMessageProvider.", e); + throw new AmazonQPluginException(e); + } + } + + public ChatResult sendChatPrompt(final ChatRequestParams chatRequestParams) { + try { + PluginLogger.info("Sending " + Command.CHAT_SEND_PROMPT + " message to Amazon Q LSP server"); + ChatResult chatResult = amazonQLspServer.sendChatPrompt(chatRequestParams).get(); + return chatResult; + } catch (InterruptedException | ExecutionException e) { + PluginLogger.error("Error occurred while sending " + Command.CHAT_SEND_PROMPT + " message to Amazon Q LSP server", e); + throw new AmazonQPluginException(e); } } public void sendChatReady() { - PluginLogger.info("Sending " + Command.CHAT_READY + " message to Amazon Q LSP server"); - amazonQLspServer.chatReady(); + try { + PluginLogger.info("Sending " + Command.CHAT_READY + " message to Amazon Q LSP server"); + amazonQLspServer.chatReady(); + } catch (Exception e) { + PluginLogger.error("Error occurred while sending " + Command.CHAT_READY + " message to Amazon Q LSP server", e); + throw new AmazonQPluginException(e); + } } public void sendTabAdd(final GenericTabParams tabParams) { - PluginLogger.info("Sending " + Command.CHAT_TAB_ADD + " message to Amazon Q LSP server"); - amazonQLspServer.tabAdd(tabParams); + try { + PluginLogger.info("Sending " + Command.CHAT_TAB_ADD + " message to Amazon Q LSP server"); + amazonQLspServer.tabAdd(tabParams); + } catch (Exception e) { + PluginLogger.error("Error occurred while sending " + Command.CHAT_TAB_ADD + " message to Amazon Q LSP server", e); + throw new AmazonQPluginException(e); + } } } diff --git a/plugin/src/software/aws/toolkits/eclipse/amazonq/lsp/AmazonQLspServer.java b/plugin/src/software/aws/toolkits/eclipse/amazonq/lsp/AmazonQLspServer.java index f07f091c..24ded6bc 100644 --- a/plugin/src/software/aws/toolkits/eclipse/amazonq/lsp/AmazonQLspServer.java +++ b/plugin/src/software/aws/toolkits/eclipse/amazonq/lsp/AmazonQLspServer.java @@ -9,6 +9,8 @@ import org.eclipse.lsp4j.jsonrpc.services.JsonNotification; import org.eclipse.lsp4j.services.LanguageServer; +import software.aws.toolkits.eclipse.amazonq.chat.models.ChatRequestParams; +import software.aws.toolkits.eclipse.amazonq.chat.models.ChatResult; import software.aws.toolkits.eclipse.amazonq.chat.models.GenericTabParams; import software.aws.toolkits.eclipse.amazonq.lsp.model.InlineCompletionParams; import software.aws.toolkits.eclipse.amazonq.lsp.model.InlineCompletionResponse; @@ -19,6 +21,9 @@ public interface AmazonQLspServer extends LanguageServer { @JsonRequest("aws/textDocument/inlineCompletionWithReferences") CompletableFuture inlineCompletionWithReferences(InlineCompletionParams params); + @JsonRequest("aws/chat/sendChatPrompt") + CompletableFuture sendChatPrompt(ChatRequestParams chatRequestParams); + @JsonNotification("aws/chat/tabAdd") void tabAdd(GenericTabParams params); diff --git a/plugin/src/software/aws/toolkits/eclipse/amazonq/views/AmazonQChatViewActionHandler.java b/plugin/src/software/aws/toolkits/eclipse/amazonq/views/AmazonQChatViewActionHandler.java index 21924486..eef277d9 100644 --- a/plugin/src/software/aws/toolkits/eclipse/amazonq/views/AmazonQChatViewActionHandler.java +++ b/plugin/src/software/aws/toolkits/eclipse/amazonq/views/AmazonQChatViewActionHandler.java @@ -6,6 +6,8 @@ import org.eclipse.swt.browser.Browser; import software.aws.toolkits.eclipse.amazonq.chat.ChatCommunicationManager; +import software.aws.toolkits.eclipse.amazonq.chat.models.ChatResult; +import software.aws.toolkits.eclipse.amazonq.exception.AmazonQPluginException; import software.aws.toolkits.eclipse.amazonq.util.PluginLogger; import software.aws.toolkits.eclipse.amazonq.views.model.Command; import software.aws.toolkits.eclipse.amazonq.views.model.ParsedCommand; @@ -25,6 +27,10 @@ public final void handleCommand(final ParsedCommand parsedCommand, final Browser PluginLogger.info(command + " being processed by ActionHandler"); switch (command) { + case CHAT_SEND_PROMPT: + ChatResult chatResult = chatCommunicationManager.sendMessageToChatServerAsync(command, params); + PluginLogger.info("Chat result: " + chatResult); + break; case CHAT_READY: chatCommunicationManager.sendMessageToChatServerAsync(command, params); break; @@ -34,8 +40,7 @@ public final void handleCommand(final ParsedCommand parsedCommand, final Browser case TELEMETRY_EVENT: break; default: - PluginLogger.info("Unhandled command: " + parsedCommand.getCommand()); - break; + throw new AmazonQPluginException("Unhandled command in AmazonQChatViewActionHandler: " + command.toString()); } } } diff --git a/plugin/src/software/aws/toolkits/eclipse/amazonq/views/AmazonQChatWebview.java b/plugin/src/software/aws/toolkits/eclipse/amazonq/views/AmazonQChatWebview.java index 8e9b26d6..783bc2ce 100644 --- a/plugin/src/software/aws/toolkits/eclipse/amazonq/views/AmazonQChatWebview.java +++ b/plugin/src/software/aws/toolkits/eclipse/amazonq/views/AmazonQChatWebview.java @@ -55,12 +55,14 @@ public final void createPartControl(final Composite parent) { new BrowserFunction(browser, "ideCommand") { @Override public Object function(final Object[] arguments) { - try { - commandParser.parseCommand(arguments) - .ifPresent(parsedCommand -> actionHandler.handleCommand(parsedCommand, browser)); - } catch (Exception e) { - PluginLogger.error("Error processing message from Browser", e); - } + ThreadingUtils.executeAsyncTask(() -> { + try { + commandParser.parseCommand(arguments) + .ifPresent(parsedCommand -> actionHandler.handleCommand(parsedCommand, browser)); + } catch (Exception e) { + PluginLogger.error("Error processing message from Browser", e); + } + }); return null; } }; diff --git a/plugin/src/software/aws/toolkits/eclipse/amazonq/views/model/Command.java b/plugin/src/software/aws/toolkits/eclipse/amazonq/views/model/Command.java index 6f0a6183..ec21b60e 100644 --- a/plugin/src/software/aws/toolkits/eclipse/amazonq/views/model/Command.java +++ b/plugin/src/software/aws/toolkits/eclipse/amazonq/views/model/Command.java @@ -10,6 +10,7 @@ public enum Command { // QChat CHAT_READY("aws/chat/ready"), CHAT_TAB_ADD("aws/chat/tabAdd"), + CHAT_SEND_PROMPT("aws/chat/sendChatPrompt"), TELEMETRY_EVENT("telemetry/event"), // Auth