Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Removal of the AI feature dependency/Decoupling the AI container #805 #806

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading