Skip to content

Commit

Permalink
Send chat prompt message to LSP (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
angjordn authored Sep 20, 2024
1 parent a9d995b commit 3b09977
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -19,6 +21,9 @@ public interface AmazonQLspServer extends LanguageServer {
@JsonRequest("aws/textDocument/inlineCompletionWithReferences")
CompletableFuture<InlineCompletionResponse> inlineCompletionWithReferences(InlineCompletionParams params);

@JsonRequest("aws/chat/sendChatPrompt")
CompletableFuture<ChatResult> sendChatPrompt(ChatRequestParams chatRequestParams);

@JsonNotification("aws/chat/tabAdd")
void tabAdd(GenericTabParams params);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3b09977

Please sign in to comment.