Skip to content

Commit

Permalink
MARP-1546 Add error message for popup
Browse files Browse the repository at this point in the history
  • Loading branch information
tvtphuc-axonivy committed Dec 5, 2024
1 parent fa51149 commit e3b8c2d
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.primefaces.PrimeFaces;

import com.axonivy.connector.vertexai.entities.*;
import com.axonivy.connector.vertexai.enums.Model;
import com.axonivy.connector.vertexai.enums.Role;
Expand All @@ -25,11 +27,13 @@ public class GeminiDataBean {
private Model model;
private List<Conversation> conversations;
private GeminiDataRequestService geminiDataRequestService = new GeminiDataRequestService();

private String errorMessage;

private static final String CODE_RESPONSE_PATTERN = "```(.*?)```";
private static final String PRE_TAG_PATTERN = "(<pre.*?>.*?</pre>)";
private static final String EMOJI_PATTERN = "[\\uD83C-\\uDBFF\\uDC00-\\uDFFF]";
private static final Set<String> PREFIXES = Set.of("html", "xml", "xhtml");
private static final String OPEN_ERROR_DIALOG_SCRIPT = "PF('errorDialog').show();";
private static String PRE_TAG_MAPPING = "<pre style=\"background-color: black;\"> <code>%s</code> </pre>";

@PostConstruct
Expand All @@ -39,9 +43,14 @@ public void init() {
}

public void onSendRequest() throws Exception {
conversations = geminiDataRequestService.sendRequestToGemini(inputtedMessage, model);
addCodesToPreTagIfPresent(conversations);
inputtedMessage = StringUtils.EMPTY;
try {
conversations = geminiDataRequestService.sendRequestToGemini(inputtedMessage, model);
addCodesToPreTagIfPresent(conversations);
inputtedMessage = StringUtils.EMPTY;
} catch (Exception e) {
errorMessage = e.getMessage();
PrimeFaces.current().executeScript(OPEN_ERROR_DIALOG_SCRIPT);
}
}

public void addCodesToPreTagIfPresent(List<Conversation> conversations) {
Expand Down Expand Up @@ -154,4 +163,13 @@ public List<Conversation> getConversations() {
public void setConversations(List<Conversation> conversations) {
this.conversations = conversations;
}

public String getErrorMessage() {
return errorMessage;
}

public void setErrorMessage(String errorMessage) {
this.errorMessage = errorMessage;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
<ui:define name="content">
<h:outputStylesheet name="layouts/styles/chatwithai.css" />
<h:form id="form" styleClass="form-container">
<p:dialog header="Error" widgetVar="errorDialog" modal="true"
closable="true">
<h:outputText value="#{managedBean.errorMessage}" />
</p:dialog>
<p:panelGrid columns="1" layout="grid">
<div id="model-option">
<p:outputLabel for="option" value="Model:" />
Expand Down
18 changes: 9 additions & 9 deletions vertexai-google-demo/webContent/layouts/styles/chatwithai.css
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#article-content {
width: 100%; /* Set the desired width */
overflow-x: auto; /* Enable horizontal scrolling if needed */
overflow-y: auto; /* Enable vertical scrolling if needed */
max-height: 300px; /* Set the maximum height for the div */
width: 100%;
overflow-x: auto;
overflow-y: auto;
max-height: 300px;
}

#editor {
Expand Down Expand Up @@ -37,22 +37,22 @@
.form-container {
display: flex;
flex-direction: column;
height: 100vh; /* Ensure it takes the full height of the viewport */
height: 95vh;
}

.ui-panelgrid-blank {
flex: 1; /* Make the panel grid take available space */
overflow-y: auto; /* Add scroll if content overflows */
flex: 1;
overflow-y: auto;
}

.form-bottom {
display: flex;
flex-direction: column;
margin-top: auto; /* Push this section to the bottom */
margin-top: auto;
}

.command-btns {
display: flex;
justify-content: space-between;
margin-top: 10px; /* Add some spacing between the editor and buttons */
margin-top: 10px;
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,16 @@ public class GeminiDataRequestServiceUtils {
public static final String IMG_SRC_ATTR_PATTERN = "data:image\\/[^;]+;base64,([^\"]+)";

public static InputStream getInputStream(String keyFilePath) throws IOException {
return new FileInputStream(keyFilePath);
if (StringUtils.isBlank(keyFilePath)) {
throw new IOException("VertexAi credential file path is empty");
}

try {
return new FileInputStream(keyFilePath);
} catch (IOException exception) {
throw new IOException("Could not find VertexAi credential file by path " + keyFilePath);
}

}

public Content formatRequest(String message) {
Expand Down

0 comments on commit e3b8c2d

Please sign in to comment.