chatbot connection

This commit is contained in:
Bane Stojkovic
2025-03-31 23:42:25 +02:00
parent 757ddf3e6d
commit edd0a16079
3 changed files with 73 additions and 3 deletions
@@ -78,9 +78,10 @@ public class ChatbotPresenter {
return service; return service;
} }
String promptChatbot() throws NotLoggedInException { public String promptChatbot(String project, String netlist, String prompt) throws NotLoggedInException {
if (!CloudPresenter.Instance.isLoggedIn()) if (!CloudPresenter.Instance.isLoggedIn())
throw new NotLoggedInException(); throw new NotLoggedInException();
return null; return getService().promptChatbot(CloudPresenter.Instance.getCurrentUsername(), CloudPresenter.Instance.getCurrentToken(),
CloudPresenter.Instance.getMachineId(), project, netlist, prompt);
} }
} }
@@ -291,6 +291,10 @@ public class CloudPresenter {
return ConfigurationManager.getInstance().readString(USERNAME_KEY, null); return ConfigurationManager.getInstance().readString(USERNAME_KEY, null);
} }
public String getCurrentToken() {
return ConfigurationManager.getInstance().readString(TOKEN_KEY, null);
}
public void updatePassword(String oldPassword, String newPassword) throws CloudException { public void updatePassword(String oldPassword, String newPassword) throws CloudException {
String username = ConfigurationManager.getInstance().readString(USERNAME_KEY, null); String username = ConfigurationManager.getInstance().readString(USERNAME_KEY, null);
@@ -416,7 +420,7 @@ public class CloudPresenter {
return properties; return properties;
} }
private String getMachineId() { public String getMachineId() {
if (machineId == null) { if (machineId == null) {
try { try {
InetAddress ip = InetAddress.getLocalHost(); InetAddress ip = InetAddress.getLocalHost();
@@ -1,10 +1,18 @@
package org.diylc.swing.plugins.chatbot; package org.diylc.swing.plugins.chatbot;
import org.diylc.common.IPlugInPort; import org.diylc.common.IPlugInPort;
import org.diylc.common.ITask;
import org.diylc.core.IView;
import org.diylc.plugins.chatbot.presenter.ChatbotPresenter;
import org.diylc.plugins.cloud.presenter.CloudPresenter;
import org.diylc.plugins.cloud.presenter.NotLoggedInException;
import org.diylc.swing.ISwingUI; import org.diylc.swing.ISwingUI;
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
public class ChatbotPane extends JPanel { public class ChatbotPane extends JPanel {
@@ -229,10 +237,67 @@ public class ChatbotPane extends JPanel {
askButton.setForeground(TERMINAL_FG); askButton.setForeground(TERMINAL_FG);
askButton.setFocusPainted(false); askButton.setFocusPainted(false);
askButton.setBorderPainted(true); askButton.setBorderPainted(true);
askButton.addActionListener(e -> {
final String prompt = getPromptArea().getText();
appendSection("user", prompt);
getPromptArea().setText(null);
String currentFile = plugInPort.getCurrentFileName();
String fileName = extractFileName(currentFile);
getAskButton().setEnabled(false);
getClearButton().setEnabled(false);
swingUI.executeBackgroundTask(new ITask<String>() {
@Override
public String doInBackground() throws Exception {
return ChatbotPresenter.Instance.promptChatbot(fileName, "", prompt);
}
@Override
public void failed(Exception e) {
swingUI.showMessage("Failed to ask AI Assistant. Error: " + e.getMessage(), "AI Assistant Error",
IView.ERROR_MESSAGE);
getClearButton().setEnabled(true);
}
@Override
public void complete(String result) {
appendSection("assistant", result);
getClearButton().setEnabled(true);
}
}, true);
});
} }
return askButton; return askButton;
} }
private void appendSection(String style, String insertText) {
String text = getChatEditorPane().getText();
int bodyCloseIndex = text.indexOf("</body>");
text = text.substring(0, bodyCloseIndex) +
"\n<div class='" + style + "'>" + insertText + "</div>\n" +
text.substring(bodyCloseIndex);
getChatEditorPane().setText(text);
}
/**
* Extracts the base file name without extension from a full path.
* For example: "/path/to/myfile.diy" returns "myfile"
*/
private String extractFileName(String path) {
if (path == null || path.isEmpty()) {
return "Untitled";
}
// Get the last part after the last separator
String fileName = path.substring(path.lastIndexOf(File.separator) + 1);
// Remove the extension if present
int dotIndex = fileName.lastIndexOf('.');
if (dotIndex > 0) {
fileName = fileName.substring(0, dotIndex);
}
return fileName;
}
public JButton getClearButton() { public JButton getClearButton() {
if (clearButton == null) { if (clearButton == null) {
clearButton = new JButton("Clear"); clearButton = new JButton("Clear");