From 7fa738435d83fbd9d11f8b0a073b9c82e72e2e60 Mon Sep 17 00:00:00 2001 From: Shruti Sinha <44882001+shruti0085@users.noreply.github.com> Date: Tue, 1 Oct 2024 11:08:41 -0700 Subject: [PATCH] Support link click actions within Q chat (#44) This PR handles the link click notifications, which includes aws/chat/linkClick, aws/chat/infoLinkClick and aws/chat/sourceLinkClick. We are showing a confirmation message box before opening links that are present in Chat UI. --- .../chat/models/InfoLinkClickParams.java | 40 +++++++++++++++++++ .../eclipse/amazonq/util/PluginUtils.java | 17 ++++++++ .../views/AmazonQChatViewActionHandler.java | 22 ++++++++++ .../eclipse/amazonq/views/model/Command.java | 3 ++ 4 files changed, 82 insertions(+) create mode 100644 plugin/src/software/aws/toolkits/eclipse/amazonq/chat/models/InfoLinkClickParams.java diff --git a/plugin/src/software/aws/toolkits/eclipse/amazonq/chat/models/InfoLinkClickParams.java b/plugin/src/software/aws/toolkits/eclipse/amazonq/chat/models/InfoLinkClickParams.java new file mode 100644 index 00000000..7a603f03 --- /dev/null +++ b/plugin/src/software/aws/toolkits/eclipse/amazonq/chat/models/InfoLinkClickParams.java @@ -0,0 +1,40 @@ +// Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved. + +package software.aws.toolkits.eclipse.amazonq.chat.models; + +import com.fasterxml.jackson.annotation.JsonProperty; + +public class InfoLinkClickParams { + @JsonProperty("tabId") + private String tabId; + + @JsonProperty("link") + private String link; + + @JsonProperty("eventId") + private String eventId; + + public final String getTabId() { + return tabId; + } + + public final void setTabId(final String tabId) { + this.tabId = tabId; + } + + public final String getLink() { + return link; + } + + public final void setLink(final String link) { + this.link = link; + } + + public final String getEventId() { + return eventId; + } + + public final void setEventId(final String eventId) { + this.eventId = eventId; + } +} diff --git a/plugin/src/software/aws/toolkits/eclipse/amazonq/util/PluginUtils.java b/plugin/src/software/aws/toolkits/eclipse/amazonq/util/PluginUtils.java index ce88142d..8190e133 100644 --- a/plugin/src/software/aws/toolkits/eclipse/amazonq/util/PluginUtils.java +++ b/plugin/src/software/aws/toolkits/eclipse/amazonq/util/PluginUtils.java @@ -12,7 +12,9 @@ import org.eclipse.core.runtime.FileLocator; import org.eclipse.core.runtime.Path; import org.eclipse.core.runtime.Platform; +import org.eclipse.jface.dialogs.MessageDialog; import org.eclipse.swt.graphics.Image; +import org.eclipse.swt.widgets.Display; import org.eclipse.ui.PlatformUI; import org.osgi.framework.Bundle; import org.osgi.framework.FrameworkUtil; @@ -98,4 +100,19 @@ public static void openWebpage(final String url) { PluginLogger.warn("Error while trying to open an external web page:", ex); } } + + public static boolean showConfirmDialog(final String title, final String message) { + final boolean[] result = new boolean[] {false}; + try { + Display.getDefault().syncExec(new Runnable() { + @Override + public void run() { + result[0] = MessageDialog.openConfirm(Display.getDefault().getActiveShell(), title, message); + } + }); + } catch (Exception ex) { + PluginLogger.error(ex.getMessage()); + } + return result[0]; + } } 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 46d5a610..3307dd28 100644 --- a/plugin/src/software/aws/toolkits/eclipse/amazonq/views/AmazonQChatViewActionHandler.java +++ b/plugin/src/software/aws/toolkits/eclipse/amazonq/views/AmazonQChatViewActionHandler.java @@ -14,9 +14,11 @@ import software.aws.toolkits.eclipse.amazonq.chat.models.ChatResult; import software.aws.toolkits.eclipse.amazonq.chat.models.ChatUIInboundCommand; import software.aws.toolkits.eclipse.amazonq.chat.models.ChatUIInboundCommandName; +import software.aws.toolkits.eclipse.amazonq.chat.models.InfoLinkClickParams; 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.util.PluginUtils; import software.aws.toolkits.eclipse.amazonq.util.ProgressNotficationUtils; import software.aws.toolkits.eclipse.amazonq.views.model.Command; import software.aws.toolkits.eclipse.amazonq.views.model.ParsedCommand; @@ -54,6 +56,16 @@ public final void handleCommand(final ParsedCommand parsedCommand, final Browser chatCommunicationManager.sendMessageToChatUI(browser, chatUIInboundCommand); }); break; + case CHAT_INFO_LINK_CLICK: + case CHAT_LINK_CLICK: + case CHAT_SOURCE_LINK_CLICK: + InfoLinkClickParams infoLinkClickParams = jsonHandler.convertObject(params, InfoLinkClickParams.class); + var link = infoLinkClickParams.getLink(); + if (link == null || link.isEmpty()) { + throw new IllegalArgumentException("Link parameter cannot be null or empty"); + } + handleExternalLinkClick(link); + break; case CHAT_READY: chatCommunicationManager.sendMessageToChatServer(browser, command, params); break; @@ -67,6 +79,16 @@ public final void handleCommand(final ParsedCommand parsedCommand, final Browser } } + private void handleExternalLinkClick(final String link) { + try { + var result = PluginUtils.showConfirmDialog("Amazon Q", "Do you want to open the external website?\n\n" + link); + if (result) { + PluginUtils.openWebpage(link); + } + } catch (Exception ex) { + PluginLogger.error("Failed to open url in browser", ex); + } + } /* * Handles chat progress notifications from the Amazon Q LSP server. Sends a partial chat prompt message to the webview. 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 ec21b60e..7a979bba 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 @@ -11,6 +11,9 @@ public enum Command { CHAT_READY("aws/chat/ready"), CHAT_TAB_ADD("aws/chat/tabAdd"), CHAT_SEND_PROMPT("aws/chat/sendChatPrompt"), + CHAT_LINK_CLICK("aws/chat/linkClick"), + CHAT_INFO_LINK_CLICK("aws/chat/infoLinkClick"), + CHAT_SOURCE_LINK_CLICK("aws/chat/sourceLinkClick"), TELEMETRY_EVENT("telemetry/event"), // Auth