diff --git a/src/main/java/com/devoxx/genie/service/ChatPromptExecutor.java b/src/main/java/com/devoxx/genie/service/ChatPromptExecutor.java index 99cc01d..df14ab2 100644 --- a/src/main/java/com/devoxx/genie/service/ChatPromptExecutor.java +++ b/src/main/java/com/devoxx/genie/service/ChatPromptExecutor.java @@ -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; @@ -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; @@ -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; @@ -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 @@ -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 */ @@ -111,6 +135,7 @@ public Optional updatePromptWithCommandIfPresent(@NotNull ChatMessageCon /** * Get the editor info. + * * @param project the project * @return the editor info */ @@ -137,6 +162,7 @@ public Optional updatePromptWithCommandIfPresent(@NotNull ChatMessageCon /** * Stop streaming or the non-streaming prompt execution + * * @param project the project */ public void stopPromptExecution(Project project) { @@ -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 getCommandFromPrompt(@NotNull ChatMessageContext chatMessageContext, diff --git a/src/main/java/com/devoxx/genie/service/NonStreamingPromptExecutor.java b/src/main/java/com/devoxx/genie/service/NonStreamingPromptExecutor.java index c9ff391..645505a 100644 --- a/src/main/java/com/devoxx/genie/service/NonStreamingPromptExecutor.java +++ b/src/main/java/com/devoxx/genie/service/NonStreamingPromptExecutor.java @@ -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; @@ -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, diff --git a/src/main/java/com/devoxx/genie/ui/panel/ActionButtonsPanel.java b/src/main/java/com/devoxx/genie/ui/panel/ActionButtonsPanel.java index db60b24..a41337c 100644 --- a/src/main/java/com/devoxx/genie/ui/panel/ActionButtonsPanel.java +++ b/src/main/java/com/devoxx/genie/ui/panel/ActionButtonsPanel.java @@ -75,7 +75,7 @@ public ActionButtonsPanel(Project project, ComboBox modelNameComboBox, DevoxxGenieToolWindowContent devoxxGenieToolWindowContent) { setLayout(new BorderLayout()); - setBorder(JBUI.Borders.empty(10)); + // setBorder(JBUI.Borders.empty(10)); // Initialize fields and components this.project = project; diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 5e726a6..e8fd713 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,2 +1,2 @@ -#Thu Dec 12 13:52:02 CET 2024 +#Thu Dec 12 14:42:19 CET 2024 version=0.4.3