mirror of
https://github.com/carlrobertoh/ProxyAI.git
synced 2026-05-13 07:02:34 +00:00
fix: UI concurrency issues (run completion events on EDT)
This commit is contained in:
parent
fde20109cd
commit
dc2cc3c5a1
2 changed files with 48 additions and 41 deletions
|
|
@ -25,6 +25,7 @@ import java.util.HashMap;
|
|||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import javax.swing.SwingUtilities;
|
||||
|
||||
abstract class ToolWindowCompletionResponseEventListener implements
|
||||
CompletionResponseEventListener {
|
||||
|
|
@ -80,36 +81,40 @@ abstract class ToolWindowCompletionResponseEventListener implements
|
|||
|
||||
@Override
|
||||
public void handleError(ErrorDetails error, Throwable ex) {
|
||||
try {
|
||||
if ("insufficient_quota".equals(error.getCode())) {
|
||||
if (SettingsState.getInstance().getSelectedService() == ServiceType.OPENAI) {
|
||||
OpenAISettingsState.getInstance().setOpenAIQuotaExceeded(true);
|
||||
SwingUtilities.invokeLater(() -> {
|
||||
try {
|
||||
if ("insufficient_quota".equals(error.getCode())) {
|
||||
if (SettingsState.getInstance().getSelectedService() == ServiceType.OPENAI) {
|
||||
OpenAISettingsState.getInstance().setOpenAIQuotaExceeded(true);
|
||||
}
|
||||
responseContainer.displayQuotaExceeded();
|
||||
} else {
|
||||
responseContainer.displayError(error.getMessage());
|
||||
}
|
||||
responseContainer.displayQuotaExceeded();
|
||||
} else {
|
||||
responseContainer.displayError(error.getMessage());
|
||||
} finally {
|
||||
LOG.error(error.getMessage(), ex);
|
||||
responsePanel.enableActions();
|
||||
stopStreaming(responseContainer);
|
||||
}
|
||||
} finally {
|
||||
LOG.error(error.getMessage(), ex);
|
||||
responsePanel.enableActions();
|
||||
stopStreaming(responseContainer);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleTokensExceeded(Conversation conversation, Message message) {
|
||||
var answer = OverlayUtil.showTokenLimitExceededDialog();
|
||||
if (answer == OK) {
|
||||
TelemetryAction.IDE_ACTION.createActionMessage()
|
||||
.property("action", "DISCARD_TOKEN_LIMIT")
|
||||
.property("model", conversation.getModel())
|
||||
.send();
|
||||
SwingUtilities.invokeLater(() -> {
|
||||
var answer = OverlayUtil.showTokenLimitExceededDialog();
|
||||
if (answer == OK) {
|
||||
TelemetryAction.IDE_ACTION.createActionMessage()
|
||||
.property("action", "DISCARD_TOKEN_LIMIT")
|
||||
.property("model", conversation.getModel())
|
||||
.send();
|
||||
|
||||
conversationService.discardTokenLimits(conversation);
|
||||
handleTokensExceededPolicyAccepted();
|
||||
} else {
|
||||
stopStreaming(responseContainer);
|
||||
}
|
||||
conversationService.discardTokenLimits(conversation);
|
||||
handleTokensExceededPolicyAccepted();
|
||||
} else {
|
||||
stopStreaming(responseContainer);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -118,25 +123,27 @@ abstract class ToolWindowCompletionResponseEventListener implements
|
|||
Message message,
|
||||
Conversation conversation,
|
||||
boolean retry) {
|
||||
try {
|
||||
responsePanel.enableActions();
|
||||
conversationService.saveMessage(fullMessage, message, conversation, retry);
|
||||
conversationService.saveMessage(fullMessage, message, conversation, retry);
|
||||
|
||||
var serpResults = serpResultsMapping.get(message.getId());
|
||||
var containsResults = serpResults != null && !serpResults.isEmpty();
|
||||
if (YouSettingsState.getInstance().isDisplayWebSearchResults() && containsResults) {
|
||||
responseContainer.displaySerpResults(serpResults);
|
||||
}
|
||||
|
||||
if (containsResults) {
|
||||
message.setSerpResults(serpResults);
|
||||
}
|
||||
|
||||
totalTokensPanel.updateUserPromptTokens(userPromptTextArea.getText());
|
||||
totalTokensPanel.updateConversationTokens(conversation);
|
||||
} finally {
|
||||
stopStreaming(responseContainer);
|
||||
var serpResults = serpResultsMapping.get(message.getId());
|
||||
var containsResults = serpResults != null && !serpResults.isEmpty();
|
||||
if (containsResults) {
|
||||
message.setSerpResults(serpResults);
|
||||
}
|
||||
var displayResults = YouSettingsState.getInstance().isDisplayWebSearchResults();
|
||||
|
||||
SwingUtilities.invokeLater(() -> {
|
||||
try {
|
||||
responsePanel.enableActions();
|
||||
if (displayResults && containsResults) {
|
||||
responseContainer.displaySerpResults(serpResults);
|
||||
}
|
||||
totalTokensPanel.updateUserPromptTokens(userPromptTextArea.getText());
|
||||
totalTokensPanel.updateConversationTokens(conversation);
|
||||
} finally {
|
||||
stopStreaming(responseContainer);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
|
|
@ -62,7 +62,7 @@ public class StandardChatToolWindowTabPanelTest extends IntegrationTest {
|
|||
"highlightedTokens")
|
||||
.containsExactly(
|
||||
encodingManager.countTokens(COMPLETION_SYSTEM_PROMPT),
|
||||
encodingManager.countConversationTokens(panel.getConversation()),
|
||||
encodingManager.countTokens(message.getPrompt()),
|
||||
0,
|
||||
0);
|
||||
assertThat(panel.getConversation())
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue