Better error handling for AI tools<

This commit is contained in:
Bane Stojkovic
2026-02-21 22:36:32 +01:00
parent 03af702933
commit a7442e14e4
13 changed files with 37 additions and 11 deletions
@@ -38,6 +38,7 @@ import org.diylc.core.ISwitch;
import org.diylc.netlist.*;
import org.diylc.plugins.chatbot.model.*;
import org.diylc.plugins.cloud.model.IServiceAPI;
import org.diylc.plugins.cloud.service.CloudException;
import org.diylc.plugins.cloud.service.NotLoggedInException;
import org.diylc.presenter.ComponentProcessor;
import org.diylc.presenter.ContinuityArea;
@@ -62,6 +63,8 @@ public class ChatbotService {
private final static Logger LOG = Logger.getLogger(ChatbotService.class);
private static final ObjectMapper MAPPER = new ObjectMapper();
public static final String SERVER_ERROR_PLEASE_TRY_AGAIN = "Server error, please try again.";
static {
MAPPER.setSerializationInclusion(JsonInclude.Include.NON_NULL);
}
@@ -114,7 +117,7 @@ public class ChatbotService {
return service;
}
public String promptChatbot(String prompt) throws NotLoggedInException {
public String promptChatbot(String prompt) throws NotLoggedInException, CloudException {
if (!plugInPort.getCloudService().isLoggedIn())
throw new NotLoggedInException();
@@ -126,13 +129,13 @@ public class ChatbotService {
File aiProjectFile = getAiProjectFile();
try {
return getService().promptChatbot(
plugInPort.getCloudService().getCurrentUsername(),
String response = getService().promptChatbot(plugInPort.getCloudService().getCurrentUsername(),
plugInPort.getCloudService().getCurrentToken(),
plugInPort.getCloudService().getMachineId(),
fileName,
aiProjectFile,
prompt);
plugInPort.getCloudService().getMachineId(), fileName, aiProjectFile, prompt);
return Optional.ofNullable(response).orElse(SERVER_ERROR_PLEASE_TRY_AGAIN);
} catch (Exception e) {
LOG.error("Chatbot error", e);
throw new CloudException(SERVER_ERROR_PLEASE_TRY_AGAIN);
} finally {
// Clean up the temp file immediately after use
if (aiProjectFile != null && aiProjectFile.exists()) {
@@ -141,7 +144,7 @@ public class ChatbotService {
}
}
public String analyzeCircuit() throws NotLoggedInException {
public String analyzeCircuit() throws NotLoggedInException, CloudException {
if (!plugInPort.getCloudService().isLoggedIn())
throw new NotLoggedInException();
@@ -156,9 +159,15 @@ public class ChatbotService {
String jsonResult = getService().analyzeCircuit(plugInPort.getCloudService().getCurrentUsername(),
plugInPort.getCloudService().getCurrentToken(),
plugInPort.getCloudService().getMachineId(), fileName, aiProjectFile);
if (jsonResult == null || jsonResult.trim().isEmpty()) {
throw new CloudException(SERVER_ERROR_PLEASE_TRY_AGAIN);
}
return JsonToHtmlConverter.convertToHtml(jsonResult);
} catch (IOException e) {
throw new RuntimeException(e);
} catch (Exception e) {
LOG.error("AI Analyzer error", e);
throw new CloudException(SERVER_ERROR_PLEASE_TRY_AGAIN);
} finally {
// Clean up the temp file immediately after use
if (aiProjectFile != null && aiProjectFile.exists()) {
@@ -365,6 +365,10 @@
<changeType>NEW_FEATURE</changeType>
<description>Added a new section in the output of 'AI Circuit Analyzer' which provides insights into the operation of the circuit and individual components</description>
</org.diylc.appframework.update.Change>
<org.diylc.appframework.update.Change>
<changeType>IMPROVEMENT</changeType>
<description>Better error handling for AI tools</description>
</org.diylc.appframework.update.Change>
</changes>
<url>https://github.com/bancika/diy-layout-creator/releases</url>
</org.diylc.appframework.update.Version>
@@ -13,6 +13,7 @@ import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.util.List;
import java.util.Optional;
public class AiAnalyzeAction extends AbstractAction {
@@ -41,7 +42,9 @@ public class AiAnalyzeAction extends AbstractAction {
@Override
public void failed(Exception e) {
swingUI.showMessage(e.getMessage(), "AI Circuit Analyzer", ISwingUI.ERROR_MESSAGE);
String message =
Optional.ofNullable(e.getCause()).map(Throwable::getMessage).orElse(e.getMessage());
swingUI.showMessage(message, "AI Circuit Analyzer", ISwingUI.ERROR_MESSAGE);
}
@Override
@@ -358,6 +358,7 @@ Selection contains components that cannot be rotated. Do you want to exclude the
Semiconductors|Halbleiter
Send Backward|Absenken
Send Selection to Back|Auswahl nach den Boden senden
Server error, please try again.|Serverfehler, bitte versuchen Sie es erneut.
Seven|Sieben
Shaft Down|Achse nach unten
Shaft Up|Achse nach oben
@@ -346,6 +346,7 @@ Selection contains components that cannot be rotated. Do you want to exclude the
Semiconductors
Send Backward
Send Selection to Back
Server error, please try again.
Seven
Shaft Down
Shaft Up
@@ -346,6 +346,7 @@ Selection contains components that cannot be rotated. Do you want to exclude the
Semiconductors|Semiconductores
Send Backward|Enviar Atrás
Send Selection to Back|Enviar Selección Atrás
Server error, please try again.|Error del servidor, inténtelo de nuevo.
Seven|Siete
Shaft Down|Eje Abajo
Shaft Up|Eje Arriba
@@ -343,6 +343,7 @@ Selection contains components that cannot be rotated. Do you want to exclude the
Semiconductors|Semi-conducteurs
Send Backward|Envoyer vers l'arrière
Send Selection to Back|Envoyer la sélection vers l'arrière
Server error, please try again.|Erreur du serveur, veuillez réessayer.
Seven|Sept
Shaft Down|Tige vers le bas
Shaft Up|Tige vers le haut
@@ -358,6 +358,7 @@ Selection contains components that cannot be rotated. Do you want to exclude the
Semiconductors|Semiconduttori
Send Backward|Invia indietro
Send Selection to Back|Invia selezione indietro
Server error, please try again.|Errore del server, riprova.
Seven|sette
Shaft Down|Shaft Down
Shaft Up|Shaft Up
@@ -358,6 +358,7 @@ Selection contains components that cannot be rotated. Do you want to exclude the
Semiconductors|Transistors
Send Backward|Verlagen
Send Selection to Back|Selectie naar bodem zenden
Server error, please try again.|Serverfout, probeer het opnieuw.
Seven|Zeven
Shaft Down|As naar beneden
Shaft Up|As omhoog
@@ -346,6 +346,7 @@ Selection contains components that cannot be rotated. Do you want to exclude the
Semiconductors|Semicondutores
Send Backward|Enviar para trás
Send Selection to Back|Enviar seleção para trás
Server error, please try again.|Erro do servidor, tente novamente.
Seven|Sete
Shaft Down|Jogar para baixo
Shaft Up|Jogar para cima
@@ -339,6 +339,7 @@ Selection contains components that cannot be rotated. Do you want to exclude the
Semiconductors|Poluprovodnici
Send Backward|Pošalji Nazad
Send Selection to Back|Pošalji Izabrane Nazad
Server error, please try again.|Greška servera, pokušajte ponovo.
Seven|Sedam
Shaft Down|Osovina Dole
Shaft Up|Osovina Gore
@@ -346,6 +346,7 @@ Selection contains components that cannot be rotated. Do you want to exclude the
Semiconductors|सेमीकंडक्टर्स
Send Backward|पीछे भेजें
Send Selection to Back|चयन को पीछे भेजें
Server error, please try again.|सर्वर त्रुटि, कृपया पुनः प्रयास करें।
Seven|सात
Shaft Down|शाफ्ट नीचे
Shaft Up|शाफ्ट ऊपर
@@ -346,6 +346,7 @@ Selection contains components that cannot be rotated. Do you want to exclude the
Semiconductors|半导体
Send Backward|后移一层
Send Selection to Back|将选择移到后面
Server error, please try again.|服务器错误,请重试。
Seven|七
Shaft Down|轴向下
Shaft Up|轴向上