-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Log partial result * Reaching partial result handler * Rendering partial responses successfully * Add comments * Cleaned up code and added comments * Update comment * Merged with latest changes * Move ignore null serialization to ChatResult record * Fix checkstyle errors * Fix checkstyle after merge
- Loading branch information
Showing
9 changed files
with
304 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
plugin/src/software/aws/toolkits/eclipse/amazonq/chat/ChatMessage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
|
||
package software.aws.toolkits.eclipse.amazonq.chat; | ||
|
||
import java.util.concurrent.CompletableFuture; | ||
|
||
import org.eclipse.swt.browser.Browser; | ||
|
||
import software.aws.toolkits.eclipse.amazonq.chat.models.ChatRequestParams; | ||
import software.aws.toolkits.eclipse.amazonq.chat.models.ChatResult; | ||
import software.aws.toolkits.eclipse.amazonq.lsp.AmazonQLspServer; | ||
|
||
public final class ChatMessage { | ||
private final Browser browser; | ||
private final ChatRequestParams chatRequestParams; | ||
private final AmazonQLspServer amazonQLspServer; | ||
private final ChatCommunicationManager chatCommunicationManager; | ||
|
||
public ChatMessage(final AmazonQLspServer amazonQLspServer, final Browser browser, final ChatRequestParams chatRequestParams) { | ||
this.amazonQLspServer = amazonQLspServer; | ||
this.browser = browser; | ||
this.chatRequestParams = chatRequestParams; | ||
this.chatCommunicationManager = ChatCommunicationManager.getInstance(); | ||
} | ||
|
||
public Browser getBrowser() { | ||
return browser; | ||
} | ||
|
||
public ChatRequestParams getChatRequestParams() { | ||
return chatRequestParams; | ||
} | ||
|
||
public String getPartialResultToken() { | ||
return chatRequestParams.getPartialResultToken(); | ||
} | ||
|
||
public CompletableFuture<ChatResult> sendChatMessageWithProgress() { | ||
// Retrieving the chat result is expected to be a long-running process with intermittent progress notifications being sent | ||
// from the LSP server. The progress notifications provide a token and a partial result Object - we are utilizing a token to | ||
// ChatMessage mapping to acquire the associated ChatMessage so we can formulate a message for the UI. | ||
String partialResultToken = chatCommunicationManager.addPartialChatMessage(this); | ||
|
||
CompletableFuture<ChatResult> chatResult = amazonQLspServer.sendChatPrompt(chatRequestParams) | ||
.thenApply(result -> { | ||
// The mapping entry no longer needs to be maintained once the final result is retrieved. | ||
chatCommunicationManager.removePartialChatMessage(partialResultToken); | ||
return result; | ||
}); | ||
|
||
return chatResult; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
plugin/src/software/aws/toolkits/eclipse/amazonq/chat/ChatPartialResultMap.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
|
||
package software.aws.toolkits.eclipse.amazonq.chat; | ||
|
||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
|
||
/** | ||
* ChatPartialResultMap is a utility class responsible for managing the mapping between | ||
* partial result tokens and their corresponding ChatMessage objects in the Amazon Q plugin for Eclipse. | ||
* | ||
* The Language Server Protocol (LSP) server sends progress notifications during long-running operations, | ||
* such as processing chat requests. These notifications include a token that identifies the specific operation | ||
* and a partial result object containing the progress information. | ||
* | ||
* This class maintains a concurrent map (tokenToChatMessageMap) that associates each token with | ||
* its respective ChatMessage object. This mapping is crucial for correctly updating the chat UI | ||
* with the latest progress information as it becomes available from the LSP server. | ||
* | ||
* The progress notifications are handled by the {@link AmazonQLspClientImpl#notifyProgress(ProgressParams)} | ||
* method, which retrieves the corresponding ChatMessage object from the tokenToChatMessageMap using | ||
* the token provided in the ProgressParams. The ChatMessage can then be updated with the partial result. | ||
*/ | ||
public final class ChatPartialResultMap { | ||
|
||
private final Map<String, ChatMessage> tokenToChatMessageMap; | ||
|
||
public ChatPartialResultMap() { | ||
tokenToChatMessageMap = new ConcurrentHashMap<String, ChatMessage>(); | ||
} | ||
|
||
public void setEntry(final String token, final ChatMessage chatMessage) { | ||
tokenToChatMessageMap.put(token, chatMessage); | ||
} | ||
|
||
public void removeEntry(final String token) { | ||
tokenToChatMessageMap.remove(token); | ||
} | ||
|
||
public ChatMessage getValue(final String token) { | ||
return tokenToChatMessageMap.getOrDefault(token, null); | ||
} | ||
|
||
public Boolean hasKey(final String token) { | ||
return tokenToChatMessageMap.containsKey(token); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
plugin/src/software/aws/toolkits/eclipse/amazonq/util/ProgressNotficationUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
|
||
package software.aws.toolkits.eclipse.amazonq.util; | ||
|
||
import org.eclipse.lsp4j.ProgressParams; | ||
|
||
import com.google.gson.Gson; | ||
import com.google.gson.JsonElement; | ||
|
||
|
||
public final class ProgressNotficationUtils { | ||
private ProgressNotficationUtils() { | ||
// Prevent instantiation | ||
} | ||
|
||
/* | ||
* Get the token from the ProgressParams value | ||
* @return The token as a String | ||
*/ | ||
public static String getToken(final ProgressParams params) { | ||
String token; | ||
|
||
if (params.getToken().isLeft()) { | ||
token = params.getToken().getLeft(); | ||
} else { | ||
token = params.getToken().getRight().toString(); | ||
} | ||
|
||
return token; | ||
} | ||
|
||
/* | ||
* Get the object from the ProgressParams value | ||
* @param cls The class of the object to be deserialized | ||
* @return The deserialized object, or null if the value is not a JsonElement | ||
*/ | ||
public static <T> T getObject(final ProgressParams params, final Class<T> cls) { | ||
Object val = params.getValue().getRight(); | ||
|
||
if (!(val instanceof JsonElement)) { | ||
return null; | ||
} | ||
|
||
Gson gson = new Gson(); | ||
JsonElement element = (JsonElement) val; | ||
T obj = gson.fromJson(element, cls); | ||
|
||
return obj; | ||
} | ||
} |
Oops, something went wrong.