diff --git a/vertexai-google-demo/src/com/axonivy/connector/vertexai/managedBean/GeminiDataBean.java b/vertexai-google-demo/src/com/axonivy/connector/vertexai/managedBean/GeminiDataBean.java index 1e3e6ed..8f00440 100644 --- a/vertexai-google-demo/src/com/axonivy/connector/vertexai/managedBean/GeminiDataBean.java +++ b/vertexai-google-demo/src/com/axonivy/connector/vertexai/managedBean/GeminiDataBean.java @@ -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; @@ -25,11 +27,13 @@ public class GeminiDataBean { private Model model; private List conversations; private GeminiDataRequestService geminiDataRequestService = new GeminiDataRequestService(); - + private String errorMessage; + private static final String CODE_RESPONSE_PATTERN = "```(.*?)```"; private static final String PRE_TAG_PATTERN = "(.*?)"; private static final String EMOJI_PATTERN = "[\\uD83C-\\uDBFF\\uDC00-\\uDFFF]"; private static final Set PREFIXES = Set.of("html", "xml", "xhtml"); + private static final String OPEN_ERROR_DIALOG_SCRIPT = "PF('errorDialog').show();"; private static String PRE_TAG_MAPPING = "
 %s 
"; @PostConstruct @@ -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 conversations) { @@ -154,4 +163,13 @@ public List getConversations() { public void setConversations(List conversations) { this.conversations = conversations; } + + public String getErrorMessage() { + return errorMessage; + } + + public void setErrorMessage(String errorMessage) { + this.errorMessage = errorMessage; + } + } diff --git a/vertexai-google-demo/src_hd/com/axonivy/connector/vertexai/chatWithAi/chatWithAi.xhtml b/vertexai-google-demo/src_hd/com/axonivy/connector/vertexai/chatWithAi/chatWithAi.xhtml index 962d2fd..b82357a 100644 --- a/vertexai-google-demo/src_hd/com/axonivy/connector/vertexai/chatWithAi/chatWithAi.xhtml +++ b/vertexai-google-demo/src_hd/com/axonivy/connector/vertexai/chatWithAi/chatWithAi.xhtml @@ -13,6 +13,10 @@ + + +
diff --git a/vertexai-google-demo/webContent/layouts/styles/chatwithai.css b/vertexai-google-demo/webContent/layouts/styles/chatwithai.css index d80b821..ef81f0c 100644 --- a/vertexai-google-demo/webContent/layouts/styles/chatwithai.css +++ b/vertexai-google-demo/webContent/layouts/styles/chatwithai.css @@ -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 { @@ -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; } \ No newline at end of file diff --git a/vertexai-google/src/com/axonivy/connector/vertexai/utils/GeminiDataRequestServiceUtils.java b/vertexai-google/src/com/axonivy/connector/vertexai/utils/GeminiDataRequestServiceUtils.java index 42ace10..f592e07 100644 --- a/vertexai-google/src/com/axonivy/connector/vertexai/utils/GeminiDataRequestServiceUtils.java +++ b/vertexai-google/src/com/axonivy/connector/vertexai/utils/GeminiDataRequestServiceUtils.java @@ -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) {