Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send chat prompt message to lsp #26

Merged
merged 8 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@breedloj Throwing the PluginException as you mentioned. You also mentioned throwing the exception to avoid returning null - I moved the return null statements to the associated cases to be more explicit about which cases null is being returned on.

}
}
}
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());
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@breedloj Also updated the ActionHandler to a PluginException to match the unhandled command case in ChatCommunicationManager

}
}
}
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