Skip to content

Commit

Permalink
Merge pull request #376 from devoxx/issue-375
Browse files Browse the repository at this point in the history
Fix #375 : We can now start/stop prompts without CompletionException …
  • Loading branch information
stephanj authored Dec 12, 2024
2 parents 5372ff9 + d56b15c commit 4f45144
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 30 deletions.
29 changes: 28 additions & 1 deletion src/main/java/com/devoxx/genie/service/ChatPromptExecutor.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.devoxx.genie.service;

import com.devoxx.genie.error.ErrorHandler;
import com.devoxx.genie.model.CustomPrompt;
import com.devoxx.genie.model.request.ChatMessageContext;
import com.devoxx.genie.model.request.EditorInfo;
Expand All @@ -11,6 +12,7 @@
import com.devoxx.genie.ui.util.NotificationUtil;
import com.devoxx.genie.util.FileTypeUtil;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.progress.ProgressIndicator;
Expand All @@ -21,12 +23,15 @@

import java.util.Arrays;
import java.util.Optional;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ConcurrentHashMap;

import static com.devoxx.genie.model.Constant.FIND_COMMAND;

public class ChatPromptExecutor {

private static final Logger LOG = Logger.getInstance(ChatPromptExecutor.class);

private final StreamingPromptExecutor streamingPromptExecutor;
private final NonStreamingPromptExecutor nonStreamingPromptExecutor;
private final PromptInputArea promptInputArea;
Expand All @@ -40,6 +45,7 @@ public ChatPromptExecutor(PromptInputArea promptInputArea) {

/**
* Execute the prompt.
*
* @param chatMessageContext the chat message context
* @param promptOutputPanel the prompt output panel
* @param enableButtons the Enable buttons
Expand Down Expand Up @@ -88,11 +94,29 @@ public void run(@NotNull ProgressIndicator progressIndicator) {
});
}
}

@Override
public void onCancel() {
super.onCancel();
// Handle cancellation if needed
LOG.info("Prompt execution was cancelled.");
}

@Override
public void onThrowable(@NotNull Throwable error) {
super.onThrowable(error);
// Handle other exceptions
if (!(error instanceof CancellationException)) {
LOG.error("Error occurred while processing chat message", error);
ErrorHandler.handleError(chatMessageContext.getProject(), error);
}
}
}.queue();
}

/**
* Process possible command prompt.
*
* @param chatMessageContext the chat message context
* @param promptOutputPanel the prompt output panel
*/
Expand All @@ -111,6 +135,7 @@ public Optional<String> updatePromptWithCommandIfPresent(@NotNull ChatMessageCon

/**
* Get the editor info.
*
* @param project the project
* @return the editor info
*/
Expand All @@ -137,6 +162,7 @@ public Optional<String> updatePromptWithCommandIfPresent(@NotNull ChatMessageCon

/**
* Stop streaming or the non-streaming prompt execution
*
* @param project the project
*/
public void stopPromptExecution(Project project) {
Expand All @@ -149,8 +175,9 @@ public void stopPromptExecution(Project project) {

/**
* Get the command from the prompt.
*
* @param chatMessageContext the chat message context
* @param promptOutputPanel the prompt output panel
* @param promptOutputPanel the prompt output panel
* @return the command
*/
private Optional<String> getCommandFromPrompt(@NotNull ChatMessageContext chatMessageContext,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import java.util.List;
import java.util.Map;
import java.util.concurrent.CancellationException;
import java.util.concurrent.Future;

import static com.devoxx.genie.model.Constant.FIND_COMMAND;
Expand Down Expand Up @@ -55,44 +56,49 @@ public void execute(ChatMessageContext chatMessageContext,

/**
* Execute the prompt.
*
* @param chatMessageContext the chat message context
* @param promptOutputPanel the prompt output panel
* @param enableButtons the enable buttons
* @param promptOutputPanel the prompt output panel
* @param enableButtons the enable buttons
*/
private void prompt(ChatMessageContext chatMessageContext,
@NotNull PromptOutputPanel promptOutputPanel,
Runnable enableButtons) {
currentTask = promptExecutionService.executeQuery(chatMessageContext)
.thenAccept(response -> {
if (!isCancelled && response != null) {
LOG.debug(">>>> Adding AI message to prompt output panel");
chatMessageContext.setAiMessage(response.content());

// Set token usage and cost
chatMessageContext.setTokenUsageAndCost(response.tokenUsage());

// Add the conversation to the chat service
ApplicationManager.getApplication().getMessageBus()
.syncPublisher(AppTopics.CONVERSATION_TOPIC)
.onNewConversation(chatMessageContext);

promptOutputPanel.addChatResponse(chatMessageContext);
} else if (isCancelled) {
LOG.debug(">>>> Prompt execution cancelled");
promptOutputPanel.removeLastUserPrompt(chatMessageContext);
}
})
.exceptionally(throwable -> {
ErrorHandler.handleError(chatMessageContext.getProject(), throwable);
return null;
})
.whenComplete((result, throwable) -> enableButtons.run());
.thenAccept(response -> {
if (!isCancelled && response != null) {
LOG.debug(">>>> Adding AI message to prompt output panel");
chatMessageContext.setAiMessage(response.content());

// Set token usage and cost
chatMessageContext.setTokenUsageAndCost(response.tokenUsage());

// Add the conversation to the chat service
ApplicationManager.getApplication().getMessageBus()
.syncPublisher(AppTopics.CONVERSATION_TOPIC)
.onNewConversation(chatMessageContext);

promptOutputPanel.addChatResponse(chatMessageContext);
} else if (isCancelled) {
LOG.debug(">>>> Prompt execution cancelled");
promptOutputPanel.removeLastUserPrompt(chatMessageContext);
}
})
.exceptionally(throwable -> {
if (!(throwable.getCause() instanceof CancellationException)) {
LOG.error("Error occurred while processing chat message", throwable);
ErrorHandler.handleError(chatMessageContext.getProject(), throwable);
}
return null;
})
.whenComplete((result, throwable) -> enableButtons.run());
}

/**
* Perform semantic search.
*
* @param chatMessageContext the chat message context
* @param promptOutputPanel the prompt output panel
* @param promptOutputPanel the prompt output panel
*/
private static void semanticSearch(ChatMessageContext chatMessageContext,
@NotNull PromptOutputPanel promptOutputPanel,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public ActionButtonsPanel(Project project,
ComboBox<LanguageModel> modelNameComboBox,
DevoxxGenieToolWindowContent devoxxGenieToolWindowContent) {
setLayout(new BorderLayout());
setBorder(JBUI.Borders.empty(10));
// setBorder(JBUI.Borders.empty(10));

// Initialize fields and components
this.project = project;
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#Thu Dec 12 13:52:02 CET 2024
#Thu Dec 12 14:42:19 CET 2024
version=0.4.3

0 comments on commit 4f45144

Please sign in to comment.