Skip to content

Commit

Permalink
Fix: Removal of the AI feature dependency/Decoupling the AI container #…
Browse files Browse the repository at this point in the history
…805  (#806)

* fix: add ai/.env

* fix: quick fix to decouple the AI feature from Diveni

* Google Java Format

---------

Co-authored-by: github-actions <>
  • Loading branch information
Dan1elBox authored Aug 22, 2024
1 parent 8c02e18 commit 8a1ce3d
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 13 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ docs/.vitepress/dist
.env.local
.env.*.local
backend/.env
ai/.env

# Log files
npm-debug.log*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.diveni.backend.controller;

import io.diveni.backend.dto.AiServiceResponse;
import io.diveni.backend.dto.GptConfidentialData;
import io.diveni.backend.service.ai.AiService;
import org.slf4j.Logger;
Expand Down Expand Up @@ -64,9 +65,9 @@ public ResponseEntity<String> markDescription(@RequestBody GptConfidentialData d
}

@GetMapping("check-api-key")
public ResponseEntity<String> checkApiKey() {
public ResponseEntity<AiServiceResponse> ensureServiceAndApiKey() {
LOGGER.debug("--> checkApiKey()");
ResponseEntity<String> response = aiService.checkApiKey();
ResponseEntity<AiServiceResponse> response = aiService.ensureServiceAndApiKey();
LOGGER.debug("<-- checkApiKey()");
return response;
}
Expand Down
15 changes: 15 additions & 0 deletions backend/src/main/java/io/diveni/backend/dto/AiServiceResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package io.diveni.backend.dto;

import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.Getter;

@Getter
@EqualsAndHashCode
@Builder
public class AiServiceResponse {

private final Boolean apiKeyValid;

private final Boolean serviceAvailable;
}
29 changes: 22 additions & 7 deletions backend/src/main/java/io/diveni/backend/service/ai/AiService.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package io.diveni.backend.service.ai;

import com.google.gson.Gson;
import io.diveni.backend.dto.AiServiceResponse;
import io.diveni.backend.dto.GptConfidentialData;
import jakarta.annotation.PostConstruct;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
import org.springframework.beans.factory.annotation.Value;

Expand All @@ -25,7 +28,8 @@ public void logConfig() {
LOGGER.info("Url to Server is: " + aiUrl);
}

public ResponseEntity<String> executeRequest(String url, HttpMethod method, Object body) {
public ResponseEntity<String> executeRequest(String url, HttpMethod method, Object body)
throws RestClientException {
LOGGER.debug("--> executeRequest()");
// Create a RestTemplate object
RestTemplate restTemplate = new RestTemplate();
Expand Down Expand Up @@ -114,11 +118,22 @@ public ResponseEntity<String> markDescription(GptConfidentialData data) {
return response;
}

public ResponseEntity<String> checkApiKey() {
LOGGER.debug("--> checkApiKey()");
ResponseEntity<String> response =
executeRequest(aiUrl + "/check-api-key", HttpMethod.GET, null);
LOGGER.debug("<-- checkApiKey()");
return response;
public ResponseEntity<AiServiceResponse> ensureServiceAndApiKey() {
LOGGER.debug("--> ensureServiceAndApiKey()");
AiServiceResponse result;
try {
ResponseEntity<String> response =
executeRequest(aiUrl + "/check-api-key", HttpMethod.GET, null);
result =
AiServiceResponse.builder()
.apiKeyValid(new JSONObject(response.getBody()).getBoolean("has_api_key"))
.serviceAvailable(response.getStatusCode().is2xxSuccessful())
.build();
} catch (RestClientException rce) {
LOGGER.debug("AI Service is offline/unavailable: {}", rce.getMessage());
result = AiServiceResponse.builder().apiKeyValid(false).serviceAvailable(false).build();
}
LOGGER.debug("<-- ensureServiceAndApiKey()");
return new ResponseEntity<>(result, HttpStatus.OK);
}
}
17 changes: 14 additions & 3 deletions frontend/src/services/api.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,9 +310,20 @@ class ApiService {
return response.data.description;
}

public async checkApiKey() {
const response = await axios.get(`${constants.backendURL}/ai/check-api-key`);
return response.data.has_api_key;
public async ensureServiceAndApiKey() {
try {
const response = await axios.get(`${constants.backendURL}/ai/check-api-key`);
const { apiKeyValid, serviceAvailable } = response.data;
if (!serviceAvailable) {
console.log("AI Service is not available!");
return false;
}

return apiKeyValid;
} catch (error) {
console.log("Error checking Service or API key:", error);
return false;
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion frontend/src/views/SessionPage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ export default defineComponent({
},
async created() {
this.store.clearStoreWithoutUserStories();
this.hasApiKey = await apiService.checkApiKey();
this.hasApiKey = await apiService.ensureServiceAndApiKey();
if (!this.sessionID || !this.adminID) {
//check for cookie
await this.checkAdminCookie();
Expand Down

0 comments on commit 8a1ce3d

Please sign in to comment.