Cloud login improvements

This commit is contained in:
Bane Stojkovic
2025-04-05 10:36:55 +02:00
parent d097bcd740
commit a5cc9c1374
5 changed files with 161 additions and 114 deletions
@@ -8,11 +8,13 @@ import org.diylc.plugins.chatbot.model.SubscriptionEntity;
import org.diylc.plugins.chatbot.service.ChatbotService;
import org.diylc.plugins.cloud.service.NotLoggedInException;
import org.diylc.swing.ISwingUI;
import org.diylc.utils.FileUtils;
import javax.swing.*;
import java.awt.*;
import java.util.List;
import java.util.Objects;
import java.util.Random;
public class ChatbotPane extends JPanel {
@@ -43,7 +45,7 @@ public class ChatbotPane extends JPanel {
private JButton clearButton;
private boolean loggedIn = false;
private String projectFileName = null;
private String projectFileName = "initial";
public ChatbotPane(ISwingUI swingUI, IPlugInPort plugInPort) {
super();
@@ -202,7 +204,7 @@ public class ChatbotPane extends JPanel {
public void refreshChat() {
boolean currentLoggedIn = plugInPort.getCloudService().isLoggedIn();
String currentProjectFileName = plugInPort.getCurrentFileName();
String currentProjectFileName = FileUtils.extractFileName(plugInPort.getCurrentFileName());
if (this.loggedIn == currentLoggedIn && Objects.equals(currentProjectFileName, this.projectFileName))
return;
@@ -210,17 +212,26 @@ public class ChatbotPane extends JPanel {
this.loggedIn = currentLoggedIn;
this.projectFileName = currentProjectFileName;
resetChat();
try {
SubscriptionEntity subscriptionInfo = plugInPort.getChatbotService().getSubscriptionInfo();
appendSection(ChatbotService.SYSTEM, "Subscribed to " + subscriptionInfo.getTier() +
" tier with " + subscriptionInfo.getRemainingCredits() + " remaining credits.");
} catch (NotLoggedInException e) {
LOG.error("Error getting subscription info", e);
appendSection(ChatbotService.SYSTEM, "Error getting subscription info");
clearChat();
if (!currentLoggedIn) {
appendSection(ChatbotService.SYSTEM, "Please log into your cloud account to use the AI assistant");
getAskButton().setEnabled(false);
getClearButton().setEnabled(false);
} else {
try {
SubscriptionEntity subscriptionInfo = plugInPort.getChatbotService().getSubscriptionInfo();
appendSection(ChatbotService.SYSTEM,
"Your current subscription tier is " + subscriptionInfo.getTier() +
" expiring on " + subscriptionInfo.getEndDate() + " with " +
subscriptionInfo.getRemainingCredits() + " credits remaining.");
} catch (NotLoggedInException e) {
LOG.error("Error getting subscription info", e);
appendSection(ChatbotService.SYSTEM, "Error getting subscription info");
}
fetchChatHistory();
getAskButton().setEnabled(true);
getClearButton().setEnabled(true);
}
fetchChatHistory();
getAskButton().setEnabled(true);
}
private void fetchChatHistory() {
@@ -250,13 +261,32 @@ public class ChatbotPane extends JPanel {
return chatEditorPane;
}
private void resetChat() {
String sampleChat = chatbotService.getInitialChatContents();
private void clearChat() {
String sampleChat = getInitialChatContents();
chatEditorPane.setContentType("text/html");
chatEditorPane.setText(HTML_STYLE + "<body>" + sampleChat + "</body>");
}
private String getInitialChatContents() {
Random r = new Random();
String chatHtml =
"<div class='system'>" + ChatbotService.WELCOME_MESSAGE + "</div><br><br>\n" +
"<div class='assistant'>I'm here to help you design, build, and troubleshoot your electronics projects using DIY Layout Creator (DIYLC). You can ask me questions about:</div><br>\n" +
// "<ul>\n" +
"\n" +
"- <b>Electronics theory or concepts:</b> <i>e.g.,</i> '<span class='user'>" +ChatbotService.electronicsQuestions[r.nextInt(
ChatbotService.electronicsQuestions.length)] + "</span>'<br>\n" +
"- <b>DIYLC features and usage:</b> <i>e.g.,</i> '<span class='user'>" + ChatbotService.diylcQuestions[r.nextInt(ChatbotService.diylcQuestions.length)] + "</span>'<br>\n" +
"- <b>Your current circuit project:</b> <i>e.g.,</i> '<span class='user'>" + ChatbotService.circuitQuestions[r.nextInt(ChatbotService.circuitQuestions.length)] + "</span>'<br>\n" +
// "</ul><br>\n" +
"<br>\n" +
"<div class='assistant'>Feel free to ask your own question or use one of these examples to get started!</div><br>\n";
return chatHtml;
}
public JButton getAskButton() {
if (askButton == null) {
askButton = new JButton("Ask");
@@ -337,7 +367,7 @@ public class ChatbotPane extends JPanel {
@Override
public void complete(Void result) {
resetChat();
clearChat();
}
}, false);
});
@@ -43,35 +43,18 @@ public class ChatbotPlugin implements IPlugIn {
public ChatbotPlugin(ISwingUI swingUI) {
super();
this.swingUI = swingUI;
// Delay a test of logged in state to give it time to complete
Timer timer = new Timer(2000, e -> {
if (!plugInPort.getCloudService().isLoggedIn()) {
getChatbotPane().refreshChat();
}
});
timer.setRepeats(false); // Make it one-shot
timer.start();
}
@Override
public EnumSet<EventType> getSubscribedEventTypes() {
return EnumSet.of(EventType.FILE_STATUS_CHANGED, EventType.PROJECT_LOADED,
EventType.PROJECT_MODIFIED, EventType.SELECTION_CHANGED, EventType.CLOUD_LOGGED_IN,
EventType.CLOUD_LOGGED_OUT);
EventType.CLOUD_LOGGED_IN, EventType.CLOUD_LOGGED_OUT);
}
@SuppressWarnings("unchecked")
@Override
public void processMessage(EventType eventType, Object... params) {
switch (eventType) {
case PROJECT_MODIFIED:
// getChatbotPane().setComponents(((Project) params[1]).getComponents(),
// plugInPort.getSelectedComponents());
break;
case SELECTION_CHANGED:
// getChatbotPane().setSelection((Set<IDIYComponent<?>>) params[0]);
break;
case FILE_STATUS_CHANGED:
case PROJECT_LOADED:
case CLOUD_LOGGED_IN:
@@ -108,33 +108,6 @@ public class CloudPlugIn implements IPlugIn {
public void connect(IPlugInPort plugInPort) {
this.plugInPort = plugInPort;
this.cloudService = plugInPort.getCloudService();
initialize();
}
private void initialize() {
this.swingUI.executeBackgroundTask(new ITask<Boolean>() {
@Override
public Boolean doInBackground() throws Exception {
return cloudService.tryLogInWithToken();
}
@Override
public void failed(Exception e) {
LOG.error("Error while trying to login using token");
}
@Override
public void complete(Boolean result) {
try {
if (result)
loggedIn();
} catch (Exception e) {
LOG.error("Error while trying to login with token", e);
}
}
}, false);
}
public CloudBrowserFrame getCloudBrowser() {
@@ -206,11 +179,20 @@ public class CloudPlugIn implements IPlugIn {
@Override
public EnumSet<EventType> getSubscribedEventTypes() {
return null;
return EnumSet.of(EventType.CLOUD_LOGGED_IN, EventType.CLOUD_LOGGED_OUT);
}
@Override
public void processMessage(EventType eventType, Object... params) {}
public void processMessage(EventType eventType, Object... params) {
switch (eventType) {
case CLOUD_LOGGED_IN:
loggedIn();
break;
case CLOUD_LOGGED_OUT:
loggedOut();
break;
}
}
class LibraryAction extends AbstractAction {