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

Add support to connect to the LoginFlow AI service #6260

Open
wants to merge 31 commits into
base: master
Choose a base branch
from

Conversation

sahandilshan
Copy link
Contributor

Proposed changes in this pull request

With this PR, the product-is will be able to connect the loginflow AI microservice and generate AI results accordingly

Related Issue(s)

# Conflicts:
#	components/application-mgt/org.wso2.carbon.identity.application.mgt/pom.xml
#	pom.xml
- Define constants for reusable keys
- Remove token decoding and get the clientId from the AI KEY
# Conflicts:
#	components/ai-services-mgt/org.wso2.carbon.ai.service.mgt/src/main/java/org/wso2/carbon/ai/service/mgt/constants/AIConstants.java
#	components/ai-services-mgt/org.wso2.carbon.ai.service.mgt/src/main/java/org/wso2/carbon/ai/service/mgt/token/AIAccessTokenManager.java
#	components/ai-services-mgt/org.wso2.carbon.ai.service.mgt/src/main/java/org/wso2/carbon/ai/service/mgt/util/AIHttpClientUtil.java
#	components/application-mgt/org.wso2.carbon.identity.application.mgt/pom.xml
#	pom.xml
@sahandilshan sahandilshan force-pushed the loginflow-ai-2 branch 3 times, most recently from 92b41bd to 73a9b36 Compare January 8, 2025 08:39
*/
public enum ErrorMessages {

MAXIMUM_RETRIES_EXCEEDED("AI_10000", "Maximum retries exceeded to retrieve the access token."),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make the error code consistent with other components. IIRC we don't us "_" instead use "-"

public class AIServerException extends Exception {

private String errorCode;
private AIHttpClientUtil.HttpResponseWrapper loginFlowAIResponse;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's stop transmitting the error details as a object and include the details directly into the exception

return new AccessTokenRequestHelper(AI_KEY, AI_TOKEN_ENDPOINT,
// Here we keep the default HTTP client to send the token request.
// We open and close it for each request.
HttpAsyncClients.createDefault());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see a reason to use the Async Client here. Lets check and switch to closable http client

Comment on lines 71 to 75
HTTP_CONNECTION_POOL_SIZE_PROPERTY_NAME) != null ? Integer.parseInt(IdentityUtil.getProperty(
HTTP_CONNECTION_POOL_SIZE_PROPERTY_NAME)) : 20;
private static final int HTTP_CONNECTION_TIMEOUT = IdentityUtil.getProperty(
HTTP_CONNECTION_TIMEOUT_PROPERTY_NAME) != null ? Integer.parseInt(IdentityUtil.getProperty(
HTTP_CONNECTION_TIMEOUT_PROPERTY_NAME)) : 60000; // Making the default timeout 60 seconds.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move the defaults to a constant.


try {
String accessToken = AIAccessTokenManager.getInstance().getAccessToken(false);
String orgName = AIAccessTokenManager.getInstance().getClientId();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lets rename to clientId

Map<String, Object> requestBody = new HashMap<>();
requestBody.put(USER_QUERY_PROPERTY, userQuery);
try {
// Convert JSONArray to List.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets remove unnecessary comments.

* @throws AIClientException When an error occurs while generating the authentication sequence.
*/
@Override
public String generateAuthenticationSequence(String userQuery, JSONArray userClaims,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lets rename userClaims to mean the what we are actually getting (the claim metadata)

Comment on lines 37 to 39
MAXIMUM_RETRIES_EXCEEDED("AILF_10000", "Maximum retries exceeded to retrieve the access token."),
UNABLE_TO_ACCESS_AI_SERVICE_WITH_RENEW_ACCESS_TOKEN("AILF_10003", "Unable to access the " +
"AI service with the renewed access token."),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these being used?

import static org.wso2.carbon.base.MultitenantConstants.SUPER_TENANT_DOMAIN_NAME;
import static org.wso2.carbon.base.MultitenantConstants.SUPER_TENANT_ID;

public class LoginFlowAIManagerTest {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing class comment

- Make the component as identity component (Rename from carbon to carbon.identity)
- Remove usage Async http client
- Clean the code
*/
public interface LoginFlowAIManager {

String generateAuthenticationSequence(String userQuery, JSONArray userClaims, JSONObject availableAuthenticators)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add method comments

@sahandilshan sahandilshan force-pushed the loginflow-ai-2 branch 4 times, most recently from c31c746 to e61ae0d Compare January 16, 2025 12:57
janakamarasena
janakamarasena previously approved these changes Jan 16, 2025
@jenkins-is-staging
Copy link

PR builder started
Link: https://github.com/wso2/product-is/actions/runs/12810307037

@jenkins-is-staging
Copy link

PR builder completed
Link: https://github.com/wso2/product-is/actions/runs/12810307037
Status: failure

@jenkins-is-staging
Copy link

PR builder started
Link: https://github.com/wso2/product-is/actions/runs/12812386496

@jenkins-is-staging
Copy link

PR builder completed
Link: https://github.com/wso2/product-is/actions/runs/12812386496
Status: failure

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants