Skip to content

Commit

Permalink
Support link click actions within Q chat (#44)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
shruti0085 authored Oct 1, 2024
1 parent 690f6ed commit 7fa7384
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 7fa7384

Please sign in to comment.