chatbot improvements

This commit is contained in:
Bane Stojkovic
2025-04-04 21:49:52 +02:00
parent acc28c2e54
commit d097bcd740
23 changed files with 437 additions and 139 deletions
@@ -106,5 +106,13 @@ public enum EventType {
/**
* Signals the listeners to scroll to selection.
*/
SCROLL_TO;
SCROLL_TO,
/**
* Signals that the Cloud account is logged in.
*/
CLOUD_LOGGED_IN,
/**
* Signals that the Cloud account is logged out.
*/
CLOUD_LOGGED_OUT;
}
@@ -57,7 +57,7 @@ import org.diylc.test.ITestProcessor;
* @see EventType
*/
public interface IPlugInPort extends ISelectionProcessor, IMouseProcessor, IKeyProcessor, IVariantProcessor,
IBlockProcessor, INetlistProcessor, ITestProcessor {
IBlockProcessor, INetlistProcessor, ITestProcessor, IServiceManager {
public static final String ANTI_ALIASING_KEY = "antiAliasing";
public static final String HI_QUALITY_RENDER_KEY = "hiQualityRender";
@@ -0,0 +1,14 @@
package org.diylc.common;
import org.diylc.appframework.simplemq.MessageDispatcher;
import org.diylc.plugins.chatbot.service.ChatbotService;
import org.diylc.plugins.cloud.service.CloudService;
public interface IServiceManager {
CloudService getCloudService();
ChatbotService getChatbotService();
MessageDispatcher<EventType> getMessageDispatcher();
}
@@ -0,0 +1,76 @@
/*
DIY Layout Creator (DIYLC).
Copyright (c) 2009-2018 held jointly by the individual authors.
This file is part of DIYLC.
DIYLC is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
DIYLC is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with DIYLC. If not, see <http://www.gnu.org/licenses/>.
*/
package org.diylc.plugins.chatbot.model;
import java.io.Serializable;
import java.util.Objects;
public class ChatMessageEntity implements Serializable {
private String prompt;
private String response;
private String createdOn;
public ChatMessageEntity(String prompt, String response, String createdOn) {
this.prompt = prompt;
this.response = response;
this.createdOn = createdOn;
}
public String getPrompt() {
return prompt;
}
public void setPrompt(String prompt) {
this.prompt = prompt;
}
public String getResponse() {
return response;
}
public void setResponse(String response) {
this.response = response;
}
public String getCreatedOn() {
return createdOn;
}
public void setCreatedOn(String createdOn) {
this.createdOn = createdOn;
}
@Override
public boolean equals(Object o) {
if (o == null || getClass() != o.getClass())
return false;
ChatMessageEntity that = (ChatMessageEntity) o;
return Objects.equals(prompt, that.prompt) && Objects.equals(response,
that.response) && Objects.equals(createdOn, that.createdOn);
}
@Override
public int hashCode() {
return Objects.hash(prompt, response, createdOn);
}
}
@@ -23,6 +23,8 @@ package org.diylc.plugins.chatbot.model;
import com.diyfever.httpproxy.ParamName;
import java.util.List;
public interface IChatbotAPI {
/**
@@ -39,4 +41,13 @@ public interface IChatbotAPI {
String promptChatbot(@ParamName("username") String username, @ParamName("token") String token,
@ParamName("machineId") String machineId, @ParamName("project") String project,
@ParamName("netlist") String netlist, @ParamName("prompt") String prompt);
SubscriptionEntity getSubscriptionInfo(@ParamName("username") String username, @ParamName("token") String token,
@ParamName("machineId") String machineId);
String deleteChatHistory(@ParamName("username") String username, @ParamName("token") String token,
@ParamName("machineId") String machineId, @ParamName("project") String project);
List<ChatMessageEntity> getChatHistory(@ParamName("username") String username, @ParamName("token") String token,
@ParamName("machineId") String machineId, @ParamName("project") String project);
}
@@ -0,0 +1,75 @@
/*
DIY Layout Creator (DIYLC).
Copyright (c) 2009-2018 held jointly by the individual authors.
This file is part of DIYLC.
DIYLC is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
DIYLC is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with DIYLC. If not, see <http://www.gnu.org/licenses/>.
*/
package org.diylc.plugins.chatbot.model;
import java.io.Serializable;
import java.util.Objects;
public class SubscriptionEntity implements Serializable {
private String tier;
private String endDate;
private int remainingCredits;
public SubscriptionEntity(String tier, String endDate, int remainingCredits) {
this.tier = tier;
this.endDate = endDate;
this.remainingCredits = remainingCredits;
}
public String getTier() {
return tier;
}
public void setTier(String tier) {
this.tier = tier;
}
public String getEndDate() {
return endDate;
}
public void setEndDate(String endDate) {
this.endDate = endDate;
}
public int getRemainingCredits() {
return remainingCredits;
}
public void setRemainingCredits(int remainingCredits) {
this.remainingCredits = remainingCredits;
}
@Override
public boolean equals(Object o) {
if (o == null || getClass() != o.getClass())
return false;
SubscriptionEntity that = (SubscriptionEntity) o;
return remainingCredits == that.remainingCredits && Objects.equals(tier,
that.tier) && Objects.equals(endDate, that.endDate);
}
@Override
public int hashCode() {
return Objects.hash(tier, endDate, remainingCredits);
}
}
@@ -19,7 +19,7 @@
along with DIYLC. If not, see <http://www.gnu.org/licenses/>.
*/
package org.diylc.plugins.chatbot.presenter;
package org.diylc.plugins.chatbot.service;
public class ChatbotException extends Exception {
@@ -19,15 +19,12 @@
along with DIYLC. If not, see <http://www.gnu.org/licenses/>.
*/
package org.diylc.plugins.chatbot.presenter;
package org.diylc.plugins.chatbot.service;
import com.diyfever.httpproxy.PhpFlatProxy;
import com.diyfever.httpproxy.ProxyFactory;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.apache.log4j.Logger;
import org.checkerframework.checker.units.qual.C;
import org.checkerframework.checker.units.qual.N;
import org.diylc.appframework.miscutils.ConfigurationManager;
import org.diylc.common.ComponentType;
import org.diylc.common.IPlugInPort;
@@ -37,28 +34,15 @@ import org.diylc.netlist.Group;
import org.diylc.netlist.Netlist;
import org.diylc.netlist.NetlistException;
import org.diylc.netlist.Node;
import org.diylc.plugins.chatbot.model.ChatMessageEntity;
import org.diylc.plugins.chatbot.model.IChatbotAPI;
import org.diylc.plugins.cloud.model.CommentEntity;
import org.diylc.plugins.chatbot.model.SubscriptionEntity;
import org.diylc.plugins.cloud.model.IServiceAPI;
import org.diylc.plugins.cloud.model.ProjectEntity;
import org.diylc.plugins.cloud.model.UserEntity;
import org.diylc.plugins.cloud.presenter.CloudException;
import org.diylc.plugins.cloud.presenter.CloudPresenter;
import org.diylc.plugins.cloud.presenter.NotLoggedInException;
import org.diylc.presenter.ComparatorFactory;
import org.diylc.plugins.cloud.service.NotLoggedInException;
import org.diylc.presenter.ComponentProcessor;
import org.diylc.utils.FileUtils;
import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.util.*;
import java.util.List;
import java.util.stream.Collectors;
@@ -66,29 +50,30 @@ import java.util.stream.Collectors;
import static org.diylc.utils.FileUtils.extractFileName;
/**
* Contains all the back-end logic for using the cloud and manipulating projects on the cloud.
* Contains all the back-end logic for using the chatbot.
*
* @author Branislav Stojkovic
*/
public class ChatbotPresenter {
public class ChatbotService {
private static String ERROR = "Error";
public static final String LOG_IN_MESSAGE =
"Please log into your DIYLC Cloud account in order to use the AI Assistant";
private final static Logger LOG = Logger.getLogger(ChatbotPresenter.class);
private static final Object SUCCESS = "Success";
private final IPlugInPort plugInPort;
private final static Logger LOG = Logger.getLogger(ChatbotService.class);
private static final ObjectMapper MAPPER = new ObjectMapper();
public static final String WELCOME_MESSAGE = "Welcome to the DIYLC AI Assistant!";
private final IPlugInPort plugInPort;
private IChatbotAPI service;
private String serviceUrl;
public ChatbotPresenter(IPlugInPort plugInPort) {
public ChatbotService(IPlugInPort plugInPort) {
this.plugInPort = plugInPort;
}
private IChatbotAPI getService() {
if (service == null) {
serviceUrl =
String serviceUrl =
ConfigurationManager.getInstance().readString(IServiceAPI.URL_KEY, "http://www.diy-fever.com/diylc/api/v1/ai");
ProxyFactory factory = new ProxyFactory(new PhpFlatProxy());
service = factory.createProxy(IChatbotAPI.class, serviceUrl);
@@ -97,7 +82,7 @@ public class ChatbotPresenter {
}
public String promptChatbot(String prompt) throws NotLoggedInException {
if (!CloudPresenter.Instance.isLoggedIn())
if (!plugInPort.getCloudService().isLoggedIn())
throw new NotLoggedInException();
String currentFile = plugInPort.getCurrentFileName();
@@ -111,9 +96,10 @@ public class ChatbotPresenter {
netlist = null;
}
return getService().promptChatbot(CloudPresenter.Instance.getCurrentUsername(),
CloudPresenter.Instance.getCurrentToken(),
CloudPresenter.Instance.getMachineId(), fileName, netlist, prompt);
return getService().promptChatbot(
plugInPort.getCloudService().getCurrentUsername(),
plugInPort.getCloudService().getCurrentToken(),
plugInPort.getCloudService().getMachineId(), fileName, netlist, prompt);
}
private static final Set<Class<?>> PROPERTY_TYPES_TO_SKIP = Set.of(Font.class, Color.class);
@@ -122,6 +108,10 @@ public class ChatbotPresenter {
private String extractNetlists() throws NetlistException {
List<Netlist> netlists = plugInPort.extractNetlists(true);
if (netlists == null) {
return null;
}
StringBuilder sb = new StringBuilder();
sb.append("Components: ");
@@ -183,7 +173,7 @@ public class ChatbotPresenter {
Random r = new Random();
String chatHtml =
"<div class='system'>Welcome to the DIYLC AI Assistant!</div><br><br>\n" +
"<div class='system'>" + 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" +
@@ -195,13 +185,35 @@ public class ChatbotPresenter {
"<br>\n" +
"<div class='assistant'>Feel free to ask your own question or use one of these examples to get started!</div><br>\n";
if (!CloudPresenter.Instance.isLoggedIn()) {
chatHtml = chatHtml + "<div class='system' id='login'>Please log into your DIYLC Cloud account in order to use the AI Assistant.</div><br>\n";
}
return chatHtml;
}
public SubscriptionEntity getSubscriptionInfo() throws NotLoggedInException {
if (!plugInPort.getCloudService().isLoggedIn())
throw new NotLoggedInException();
return getService().getSubscriptionInfo(plugInPort.getCloudService().getCurrentUsername(),
plugInPort.getCloudService().getCurrentToken(), plugInPort.getCloudService().getMachineId());
}
public void deleteChatHistory() throws NotLoggedInException {
if (!plugInPort.getCloudService().isLoggedIn())
throw new NotLoggedInException();
getService().deleteChatHistory(plugInPort.getCloudService().getCurrentUsername(),
plugInPort.getCloudService().getCurrentToken(), plugInPort.getCloudService().getMachineId(),
FileUtils.extractFileName(plugInPort.getCurrentFileName()));
}
public List<ChatMessageEntity> getChatHistory() throws NotLoggedInException {
if (!plugInPort.getCloudService().isLoggedIn())
throw new NotLoggedInException();
return getService().getChatHistory(plugInPort.getCloudService().getCurrentUsername(),
plugInPort.getCloudService().getCurrentToken(), plugInPort.getCloudService().getMachineId(),
FileUtils.extractFileName(plugInPort.getCurrentFileName()));
}
public static final String ASSISTANT = "assistant";
public static final String SYSTEM = "system";
public static final String USER = "user";
@@ -19,7 +19,7 @@
along with DIYLC. If not, see <http://www.gnu.org/licenses/>.
*/
package org.diylc.plugins.cloud.presenter;
package org.diylc.plugins.cloud.service;
public class CloudException extends Exception {
@@ -19,7 +19,7 @@
along with DIYLC. If not, see <http://www.gnu.org/licenses/>.
*/
package org.diylc.plugins.cloud.presenter;
package org.diylc.plugins.cloud.service;
public interface CloudListener {
@@ -19,7 +19,7 @@
along with DIYLC. If not, see <http://www.gnu.org/licenses/>.
*/
package org.diylc.plugins.cloud.presenter;
package org.diylc.plugins.cloud.service;
import java.io.File;
import java.io.FileOutputStream;
@@ -37,6 +37,8 @@ import org.diylc.appframework.miscutils.ConfigurationManager;
import com.diyfever.httpproxy.PhpFlatProxy;
import com.diyfever.httpproxy.ProxyFactory;
import org.diylc.common.EventType;
import org.diylc.common.IPlugInPort;
import org.diylc.common.PropertyWrapper;
import org.diylc.plugins.cloud.model.CommentEntity;
import org.diylc.plugins.cloud.model.IServiceAPI;
@@ -50,17 +52,16 @@ import org.diylc.presenter.ComponentProcessor;
*
* @author Branislav Stojkovic
*/
public class CloudPresenter {
public class CloudService {
public static final CloudPresenter Instance = new CloudPresenter();
private static String USERNAME_KEY = "cloud.Username";
private static String TOKEN_KEY = "cloud.token";
private static String ERROR = "Error";
private final static Logger LOG = Logger.getLogger(CloudPresenter.class);
private final static Logger LOG = Logger.getLogger(CloudService.class);
private static final Object SUCCESS = "Success";
private final IPlugInPort plugInPort;
private IServiceAPI service;
private String serviceUrl;
@@ -69,7 +70,9 @@ public class CloudPresenter {
private boolean loggedIn = false;
private CloudPresenter() {}
public CloudService(IPlugInPort plugInPort) {
this.plugInPort = plugInPort;
}
private IServiceAPI getService() {
if (service == null) {
@@ -99,6 +102,7 @@ public class CloudPresenter {
ConfigurationManager.getInstance().writeValue(USERNAME_KEY, username);
ConfigurationManager.getInstance().writeValue(TOKEN_KEY, res);
this.loggedIn = true;
this.plugInPort.getMessageDispatcher().dispatchMessage(EventType.CLOUD_LOGGED_IN);
return true;
}
}
@@ -121,6 +125,7 @@ public class CloudPresenter {
} else {
LOG.info("Login success");
this.loggedIn = true;
this.plugInPort.getMessageDispatcher().dispatchMessage(EventType.CLOUD_LOGGED_IN);
return true;
}
} else
@@ -130,6 +135,7 @@ public class CloudPresenter {
public void logOut() {
LOG.info("Logged out");
ConfigurationManager.getInstance().writeValue(TOKEN_KEY, null);
this.plugInPort.getMessageDispatcher().dispatchMessage(EventType.CLOUD_LOGGED_OUT);
this.loggedIn = false;
}
@@ -19,7 +19,7 @@
along with DIYLC. If not, see <http://www.gnu.org/licenses/>.
*/
package org.diylc.plugins.cloud.presenter;
package org.diylc.plugins.cloud.service;
public class NotConnectedException extends Exception {
@@ -19,7 +19,7 @@
along with DIYLC. If not, see <http://www.gnu.org/licenses/>.
*/
package org.diylc.plugins.cloud.presenter;
package org.diylc.plugins.cloud.service;
public class NotExistsException extends Exception {
@@ -19,7 +19,7 @@
along with DIYLC. If not, see <http://www.gnu.org/licenses/>.
*/
package org.diylc.plugins.cloud.presenter;
package org.diylc.plugins.cloud.service;
public class NotLoggedInException extends Exception {
@@ -19,10 +19,11 @@
along with DIYLC. If not, see <http://www.gnu.org/licenses/>.
*/
package org.diylc.plugins.cloud.presenter;
package org.diylc.plugins.cloud.service;
import java.util.List;
import org.diylc.common.IPlugInPort;
import org.diylc.plugins.cloud.model.ProjectEntity;
/**
@@ -34,7 +35,8 @@ import org.diylc.plugins.cloud.model.ProjectEntity;
* @author Branislav Stojkovic
*/
public class SearchSession {
private final IPlugInPort plugInPort;
private String searchFor;
private String category;
private String sort;
@@ -43,7 +45,8 @@ public class SearchSession {
private int currentPage;
private List<ProjectEntity> currentResults;
public SearchSession() {
public SearchSession(IPlugInPort plugInPort) {
this.plugInPort = plugInPort;
}
public List<ProjectEntity> startSession(String searchFor, String category, String sort) throws CloudException {
@@ -52,7 +55,7 @@ public class SearchSession {
this.sort = sort;
this.currentPage = 1;
return currentResults = CloudPresenter.Instance.search(searchFor, category, sort, currentPage, itemsPerPage);
return currentResults = plugInPort.getCloudService().search(searchFor, category, sort, currentPage, itemsPerPage);
}
public boolean hasMoreData() {
@@ -61,6 +64,6 @@ public class SearchSession {
public List<ProjectEntity> requestMoreData() throws CloudException {
currentPage++;
return currentResults = CloudPresenter.Instance.search(searchFor, category, sort, currentPage, itemsPerPage);
return currentResults = plugInPort.getCloudService().search(searchFor, category, sort, currentPage, itemsPerPage);
}
}
@@ -47,7 +47,6 @@ import java.util.function.Supplier;
import java.util.stream.Collectors;
import javax.swing.JOptionPane;
import org.apache.log4j.Logger;
import org.diylc.appframework.miscutils.IConfigListener;
import org.diylc.appframework.miscutils.IConfigurationManager;
import org.diylc.appframework.miscutils.Utils;
import org.diylc.appframework.simplemq.MessageDispatcher;
@@ -78,6 +77,8 @@ import org.diylc.netlist.Netlist;
import org.diylc.netlist.AbstractNetlistAnalyzer;
import org.diylc.netlist.NetlistBuilder;
import org.diylc.netlist.NetlistException;
import org.diylc.plugins.chatbot.service.ChatbotService;
import org.diylc.plugins.cloud.service.CloudService;
import org.diylc.serialization.ProjectFileManager;
import org.diylc.test.DIYTest;
import org.diylc.test.Snapshot;
@@ -2810,7 +2811,27 @@ public class Presenter implements IPlugInPort {
messageDispatcher.dispatchMessage(EventType.STATUS_MESSAGE_CHANGED);
}
protected MessageDispatcher<EventType> getMessageDispatcher() {
private CloudService cloudService;
@Override
public CloudService getCloudService() {
if (cloudService == null) {
cloudService = new CloudService(this);
}
return cloudService;
}
private ChatbotService chatbotService;
@Override
public ChatbotService getChatbotService() {
if (chatbotService == null) {
chatbotService = new ChatbotService(this);
}
return chatbotService;
}
public MessageDispatcher<EventType> getMessageDispatcher() {
return messageDispatcher;
}
}