Skip to content

Commit

Permalink
Fetch customizations from LSP server (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
pras0131 authored Oct 4, 2024
1 parent 447230b commit 341fd78
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,19 @@

package software.aws.toolkits.eclipse.amazonq.customization;

import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;

import org.eclipse.lsp4j.DidChangeConfigurationParams;

import software.amazon.awssdk.utils.StringUtils;
import software.aws.toolkits.eclipse.amazonq.exception.AmazonQPluginException;
import software.aws.toolkits.eclipse.amazonq.lsp.model.GetConfigurationFromServerParams;
import software.aws.toolkits.eclipse.amazonq.providers.LspProvider;
import software.aws.toolkits.eclipse.amazonq.util.PluginLogger;
import software.aws.toolkits.eclipse.amazonq.views.model.Customization;

public final class CustomizationUtil {

Expand All @@ -26,4 +34,17 @@ public static void triggerChangeConfigurationNotification(final Map<String, Obje
}
}

public static CompletableFuture<List<Customization>> listCustomizations() {
GetConfigurationFromServerParams params = new GetConfigurationFromServerParams();
params.setSection("aws.q");
return LspProvider.getAmazonQServer()
.thenCompose(server -> server.getConfigurationFromServer(params))
.thenApply(customizations -> customizations.stream()
.filter(customization -> customization != null && StringUtils.isNotBlank(customization.getName()))
.collect(Collectors.toList()))
.exceptionally(throwable -> {
PluginLogger.error("Error occurred while fetching the list of customizations", throwable);
throw new AmazonQPluginException(throwable);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

package software.aws.toolkits.eclipse.amazonq.lsp;

import java.util.List;
import java.util.concurrent.CompletableFuture;

import org.eclipse.lsp4j.jsonrpc.messages.ResponseMessage;
Expand All @@ -12,9 +13,11 @@
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.GetConfigurationFromServerParams;
import software.aws.toolkits.eclipse.amazonq.lsp.model.InlineCompletionParams;
import software.aws.toolkits.eclipse.amazonq.lsp.model.InlineCompletionResponse;
import software.aws.toolkits.eclipse.amazonq.lsp.model.UpdateCredentialsPayload;
import software.aws.toolkits.eclipse.amazonq.views.model.Customization;

public interface AmazonQLspServer extends LanguageServer {

Expand All @@ -33,4 +36,6 @@ public interface AmazonQLspServer extends LanguageServer {
@JsonRequest("aws/credentials/token/update")
CompletableFuture<ResponseMessage> updateTokenCredentials(UpdateCredentialsPayload payload);

@JsonRequest("aws/getConfigurationFromServer")
CompletableFuture<List<Customization>> getConfigurationFromServer(GetConfigurationFromServerParams params);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

package software.aws.toolkits.eclipse.amazonq.lsp.model;

public class GetConfigurationFromServerParams {
private String section;

public final String getSection() {
return this.section;
}

public final void setSection(final String section) {
this.section = section;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@

package software.aws.toolkits.eclipse.amazonq.views;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.swt.SWT;
Expand All @@ -20,11 +24,13 @@
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import software.amazon.awssdk.utils.StringUtils;
import software.aws.toolkits.eclipse.amazonq.configuration.PluginStore;
import software.aws.toolkits.eclipse.amazonq.customization.CustomizationUtil;
import software.aws.toolkits.eclipse.amazonq.exception.AmazonQPluginException;
import software.aws.toolkits.eclipse.amazonq.util.Constants;
import software.aws.toolkits.eclipse.amazonq.util.PluginLogger;
import software.aws.toolkits.eclipse.amazonq.util.ThreadingUtils;
Expand Down Expand Up @@ -80,7 +86,7 @@ public CustomizationDialog(final Shell parentShell) {
super(parentShell);
}

public void setCustomisationResponse(final List<Customization> customizationsResponse) {
public void setCustomizationResponse(final List<Customization> customizationsResponse) {
this.customizationsResponse = customizationsResponse;
}

Expand Down Expand Up @@ -115,7 +121,7 @@ protected void okPressed() {
PluginLogger.info(String.format("Select pressed with responseSelection:%s and selectedArn:%s", this.responseSelection, this.selectedCustomisationArn));
if (this.responseSelection.equals(ResponseSelection.AMAZON_Q_FOUNDATION_DEFAULT)) {
PluginStore.remove(Constants.CUSTOMIZATION_STORAGE_INTERNAL_KEY);
} else {
} else if (this.selectedCustomisationArn != null) {
PluginStore.put(Constants.CUSTOMIZATION_STORAGE_INTERNAL_KEY, this.selectedCustomisationArn);
Map<String, Object> updatedSettings = new HashMap<>();
Map<String, String> internalMap = new HashMap<>();
Expand Down Expand Up @@ -157,22 +163,19 @@ private static void addFormattedOption(final Combo combo, final String name, fin
combo.add(formattedText);
}

private void createDropdownForCustomizations(final Composite parent) {
Composite contentComposite = new Composite(parent, SWT.NONE);
GridLayout layout = new GridLayout(2, false);
layout.marginLeft = 18;
contentComposite.setLayout(layout);
GridData layoutData = new GridData(SWT.FILL, SWT.CENTER, true, false);
layoutData.horizontalSpan = 2;
contentComposite.setLayoutData(layoutData);
combo = new Combo(contentComposite, SWT.READ_ONLY);
combo.setLayout(new GridLayout());
GridData comboGridData = new GridData(SWT.FILL, SWT.CENTER, true, false);
comboGridData.horizontalAlignment = GridData.FILL;
comboGridData.grabExcessHorizontalSpace = true;
comboGridData.horizontalSpan = 2;
combo.setLayoutData(comboGridData);
List<Customization> customizations = this.customizationsResponse;
private List<Customization> getCustomizations() {
List<Customization> customizations = new ArrayList<>();
try {
customizations = CustomizationUtil.listCustomizations().get();
} catch (InterruptedException | ExecutionException e) {
PluginLogger.error("Error occurred in getCustomizations", e);
throw new AmazonQPluginException(e);
}
return customizations;
}

private void updateComboOnUIThread(final List<Customization> customizations) {
combo.removeAll();
int defaultSelectedDropdownIndex = -1;
for (int index = 0; index < customizations.size(); index++) {
addFormattedOption(combo, customizations.get(index).getName(), customizations.get(index).getDescription());
Expand All @@ -186,6 +189,8 @@ private void createDropdownForCustomizations(final Composite parent) {
combo.select(defaultSelectedDropdownIndex);
if (this.responseSelection.equals(ResponseSelection.AMAZON_Q_FOUNDATION_DEFAULT)) {
combo.setEnabled(false);
} else {
combo.setEnabled(true);
}
combo.addSelectionListener(new SelectionAdapter() {
@Override
Expand All @@ -199,6 +204,30 @@ public void widgetSelected(final SelectionEvent e) {
});
}

private void createDropdownForCustomizations(final Composite parent) {
Composite contentComposite = new Composite(parent, SWT.NONE);
GridLayout layout = new GridLayout(2, false);
layout.marginLeft = 18;
contentComposite.setLayout(layout);
GridData layoutData = new GridData(SWT.FILL, SWT.CENTER, true, false);
layoutData.horizontalSpan = 2;
contentComposite.setLayoutData(layoutData);
combo = new Combo(contentComposite, SWT.READ_ONLY);
combo.setLayout(new GridLayout());
GridData comboGridData = new GridData(SWT.FILL, SWT.CENTER, true, false);
comboGridData.horizontalAlignment = GridData.FILL;
comboGridData.grabExcessHorizontalSpace = true;
comboGridData.horizontalSpan = 2;
combo.setLayoutData(comboGridData);

combo.setItems(new String[]{"Loading..."});
combo.select(0);
combo.setEnabled(false);

CompletableFuture.supplyAsync(() -> getCustomizations())
.thenAcceptAsync(customizations -> updateComboOnUIThread(customizations), Display.getDefault()::asyncExec);
}

@Override
protected Control createDialogArea(final Composite parent) {
container = (Composite) super.createDialogArea(parent);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

package software.aws.toolkits.eclipse.amazonq.views.actions;

import java.util.ArrayList;
import java.util.List;
import org.eclipse.jface.action.ContributionItem;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
Expand All @@ -21,7 +19,6 @@
import software.aws.toolkits.eclipse.amazonq.util.Constants;
import software.aws.toolkits.eclipse.amazonq.views.CustomizationDialog;
import software.aws.toolkits.eclipse.amazonq.views.CustomizationDialog.ResponseSelection;
import software.aws.toolkits.eclipse.amazonq.views.model.Customization;

public final class CustomizationDialogContributionItem extends ContributionItem implements AuthStatusChangedListener {
private static final String CUSTOMIZATION_MENU_ITEM_TEXT = "Select Customization";
Expand All @@ -48,14 +45,6 @@ public void onAuthStatusChanged(final boolean isLoggedIn) {
updateVisibility(isLoggedIn);
}

private List<Customization> getCustomizations() {
List<Customization> customizations = new ArrayList<>();
customizations.add(new Customization("customization-arn-1", "Customization 1", "Code Whisperer customization 1"));
customizations.add(new Customization("customization-arn-2", "Customization 2", "Code Whisperer customization 2"));
customizations.add(new Customization("customization-arn-3", "Customization 3", "Code Whisperer customization 3"));
return customizations;
}

@Override
public void fill(final Menu menu, final int index) {
MenuItem menuItem = new MenuItem(menu, SWT.NONE, index);
Expand All @@ -64,8 +53,6 @@ public void fill(final Menu menu, final int index) {
@Override
public void widgetSelected(final SelectionEvent e) {
CustomizationDialog dialog = new CustomizationDialog(shell);
// TODO: This mock will be replaced by an actual call to LSP
dialog.setCustomisationResponse(getCustomizations());
String storedCustomizationArn = PluginStore.get(Constants.CUSTOMIZATION_STORAGE_INTERNAL_KEY);
if (StringUtils.isBlank(storedCustomizationArn)) {
dialog.setResponseSelection(ResponseSelection.AMAZON_Q_FOUNDATION_DEFAULT);
Expand Down

0 comments on commit 341fd78

Please sign in to comment.