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 API Chat and Marketplace Assistant feature implementations #12357

Merged
merged 15 commits into from
Mar 23, 2024
Merged
Show file tree
Hide file tree
Changes from 11 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
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,28 @@ Set<SubscribedAPI> getPaginatedSubscribedAPIsByApplication(Application applicati
*/
Tier getThrottlePolicyByName(String name, int policyType, String organization) throws APIManagementException;

/**
* Returns the API Chat execute call response as a string
*
* @param apiChatRequestId Request UUID
* @param requestPayload Request payload to be used for the AI service execute call
* @return execution response as a string
* @throws APIManagementException if execute call failed
*/
String invokeApiChatExecute(String apiChatRequestId, String requestPayload) throws APIManagementException;

/**
* Returns the API Chat prepare call response as a string
*
* @param apiId ID of the API
* @param apiChatRequestId Request UUID
* @param organization Identifier of an organization
* @return prepare response
* @throws APIManagementException if prepare call failed
*/
String invokeApiChatPrepare(String apiId, String apiChatRequestId, String organization)
throws APIManagementException;

/**
* This method used to retrieve key manager configurations for tenant
* @param organization organization of the key manager
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,11 @@ public enum ExceptionCodes implements ErrorHandler {
KEY_MANAGER_RESTRICTED_FOR_USER(902013, "Unauthorized Access to Key Manager", 403, "Key Manager is Restricted for this user"),
// Admin portal get apis and api provider change related errors
CHANGE_API_PROVIDER_FAILED(903011, "Error while changing the API provider", 500, "Error while changing the API provider in the registry or DB"),
GET_SEARCH_APIS_IN_ADMIN_FAILED(903012, "Error while getting the apis", 500, "Error while getting/searching the apis from registry");
GET_SEARCH_APIS_IN_ADMIN_FAILED(903012, "Error while getting the apis", 500, "Error while getting/searching the apis from registry"),

// AI service invocation related exceptions
AI_SERVICE_INVALID_RESPONSE(903100, "Invalid response from AI service", 500, "Error while invoking AI service. %s", false),
AI_SERVICE_INVALID_ACCESS_TOKEN(900906, "Invalid access token provided for AI service", 401, "Invalid access token provided for AI service");

private final long errorCode;
private final String errorMessage;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright (c) 2024, WSO2 LLC. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.wso2.carbon.apimgt.api.model;

import org.json.simple.JSONArray;

public class APIChatAPISpec {
private String serviceUrl = null;

Check warning on line 24 in components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/APIChatAPISpec.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/APIChatAPISpec.java#L23-L24

Added lines #L23 - L24 were not covered by tests
private JSONArray tools;
ashera96 marked this conversation as resolved.
Show resolved Hide resolved

public String getServiceUrl() {
return serviceUrl;

Check warning on line 28 in components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/APIChatAPISpec.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/APIChatAPISpec.java#L28

Added line #L28 was not covered by tests
}

public void setServiceUrl(String serviceUrl) {
this.serviceUrl = serviceUrl;
}

Check warning on line 33 in components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/APIChatAPISpec.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/APIChatAPISpec.java#L32-L33

Added lines #L32 - L33 were not covered by tests

public JSONArray getTools() {
return tools;

Check warning on line 36 in components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/APIChatAPISpec.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/APIChatAPISpec.java#L36

Added line #L36 was not covered by tests
}

public void setTools(JSONArray tools) {
this.tools = tools;
}

Check warning on line 41 in components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/APIChatAPISpec.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/APIChatAPISpec.java#L40-L41

Added lines #L40 - L41 were not covered by tests
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright (c) 2024, WSO2 LLC. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.wso2.carbon.apimgt.api.model;

public class APIChatExecutionResponse {
private Integer code = null;
private String path = null;
private Object headers = null;
private Object body = null;

Check warning on line 25 in components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/APIChatExecutionResponse.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/APIChatExecutionResponse.java#L21-L25

Added lines #L21 - L25 were not covered by tests

public Integer getCode() {
return code;

Check warning on line 28 in components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/APIChatExecutionResponse.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/APIChatExecutionResponse.java#L28

Added line #L28 was not covered by tests
}

public void setCode(Integer code) {
this.code = code;
}

Check warning on line 33 in components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/APIChatExecutionResponse.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/APIChatExecutionResponse.java#L32-L33

Added lines #L32 - L33 were not covered by tests

public String getPath() {
return path;

Check warning on line 36 in components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/APIChatExecutionResponse.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/APIChatExecutionResponse.java#L36

Added line #L36 was not covered by tests
}

public void setPath(String path) {
this.path = path;
}

Check warning on line 41 in components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/APIChatExecutionResponse.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/APIChatExecutionResponse.java#L40-L41

Added lines #L40 - L41 were not covered by tests

public Object getHeaders() {
return headers;

Check warning on line 44 in components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/APIChatExecutionResponse.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/APIChatExecutionResponse.java#L44

Added line #L44 was not covered by tests
}

public void setHeaders(Object headers) {
this.headers = headers;
}

Check warning on line 49 in components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/APIChatExecutionResponse.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/APIChatExecutionResponse.java#L48-L49

Added lines #L48 - L49 were not covered by tests

public Object getBody() {
return body;

Check warning on line 52 in components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/APIChatExecutionResponse.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/APIChatExecutionResponse.java#L52

Added line #L52 was not covered by tests
}

public void setBody(Object body) {
this.body = body;
}

Check warning on line 57 in components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/APIChatExecutionResponse.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/APIChatExecutionResponse.java#L56-L57

Added lines #L56 - L57 were not covered by tests
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright (c) 2024, WSO2 LLC. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.wso2.carbon.apimgt.api.model;

/**
* Details related to API Chat test execution
*/
public class APIChatTestExecutionInfo {
private APIChatExecutionResponse response = null;

Check warning on line 25 in components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/APIChatTestExecutionInfo.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/APIChatTestExecutionInfo.java#L24-L25

Added lines #L24 - L25 were not covered by tests

public APIChatExecutionResponse getResponse() {
return response;

Check warning on line 28 in components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/APIChatTestExecutionInfo.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/APIChatTestExecutionInfo.java#L28

Added line #L28 was not covered by tests
}

public void setResponse(APIChatExecutionResponse response) {
this.response = response;
}

Check warning on line 33 in components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/APIChatTestExecutionInfo.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/APIChatTestExecutionInfo.java#L32-L33

Added lines #L32 - L33 were not covered by tests
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright (c) 2024, WSO2 LLC. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.wso2.carbon.apimgt.api.model;

import org.json.simple.JSONObject;

/**
* Details related to API Chat test initialization
*/
public class APIChatTestInitializerInfo {
private String command = null;
private APIChatAPISpec apiSpec = null;

Check warning on line 28 in components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/APIChatTestInitializerInfo.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/APIChatTestInitializerInfo.java#L26-L28

Added lines #L26 - L28 were not covered by tests

public String getCommand() {
return command;

Check warning on line 31 in components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/APIChatTestInitializerInfo.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/APIChatTestInitializerInfo.java#L31

Added line #L31 was not covered by tests
}

public void setCommand(String command) {
this.command = command;
}

Check warning on line 36 in components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/APIChatTestInitializerInfo.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/APIChatTestInitializerInfo.java#L35-L36

Added lines #L35 - L36 were not covered by tests

public APIChatAPISpec getApiSpec() {
return apiSpec;

Check warning on line 39 in components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/APIChatTestInitializerInfo.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/APIChatTestInitializerInfo.java#L39

Added line #L39 was not covered by tests
}

public void setApiSpec(APIChatAPISpec apiSpec) {
this.apiSpec = apiSpec;
}

Check warning on line 44 in components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/APIChatTestInitializerInfo.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/APIChatTestInitializerInfo.java#L43-L44

Added lines #L43 - L44 were not covered by tests
}
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,23 @@
public static final String DIGEST = "x5t#S256";
public static final String CNF = "cnf";

// Constants related to AI features: API chat and Marketplace Assistant
public static class AI {

public static final String API_CHAT = "APIChat.";
public static final String API_CHAT_ENABLED = API_CHAT + "Enabled";
public static final String API_CHAT_AUTH_TOKEN = API_CHAT + "AuthToken";
public static final String API_CHAT_ENDPOINT = API_CHAT + "Endpoint";
public static final String API_CHAT_PREPARE_RESOURCE = "/prepare";
public static final String API_CHAT_EXECUTE_RESOURCE = "/chat";
public static final String API_CHAT_ACTION_PREPARE = "PREPARE";
public static final String API_CHAT_ACTION_EXECUTE = "EXECUTE";

private AI() {

Check warning on line 516 in components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIConstants.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIConstants.java#L516

Added line #L516 was not covered by tests

}

Check warning on line 518 in components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIConstants.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIConstants.java#L518

Added line #L518 was not covered by tests
}

//documentation rxt

public static final String DOC_NAME = "overview_name";
Expand Down Expand Up @@ -531,6 +548,17 @@
public static final String FIRST_NAME = DEFAULT_CARBON_DIALECT + "/givenname";
public static final String LAST_NAME = DEFAULT_CARBON_DIALECT + "/lastname";

// constants for marketplace assistant
public static final String MARKETPLACE_ASSISTANT = "MarketplaceAssistant";
public static final String MARKETPLACE_ASSISTANT_ENABLED = "Enabled";
public static final String MARKETPLACE_ASSISTANT_AUTH_TOKEN = "AuthToken";
public static final String MARKETPLACE_ASSISTANT_ENDPOINT = "Endpoint";
public static final String MARKETPLACE_ASSISTANT_CHAT_RESOURCE = "ChatResource";
public static final String MARKETPLACE_ASSISTANT_PUBLISH_API_RESOURCE = "ApiPublishResource";
public static final String MARKETPLACE_ASSISTANT_DELETE_API_RESOURCE = "ApiDeleteResource";
public static final String MARKETPLACE_ASSISTANT_API_COUNT_RESOURCE = "ApiCountResource";


//Overview constants for CORS configuration
public static final String API_OVERVIEW_CORS_CONFIGURATION = "overview_corsConfiguration";
//Registry lifecycle related info
Expand Down Expand Up @@ -1678,6 +1706,34 @@
public static final String API_DATA_URL = "url";
public static final String API_UUID = "apiUUID";


public static final String UUID = "uuid";
public static final String API_SPEC_TYPE = "api_type";
public static final String API_SPEC_NAME = "api_name";
public static final String TENANT_DOMAIN = "tenant_domain";
public static final String QUERY = "query";
public static final String HISTORY = "history";
public static final String VERSION = "version";
public static final String DESCRIPTION = "description";

public static final String DEMOTE_TO_CREATED= "Demote to Created";
public static final String BLOCK = "Block";
public static final String DEPRECATE = "Deprecate";
public static final String PUBLISH = "Publish";
public static final String DEPLOY_AS_A_PROTOTYPE = "Deploy as a Prototype";
public static final String REPUBLISH = "Re-Publish";

public static final String API_SPEC_TYPE_REST = "api_spec";
public static final String API_SPEC_TYPE_GRAPHQL = "sdl_schema";
public static final String API_SPEC_TYPE_ASYNC = "async_spec";
public static final String API_TYPE_HTTP = "HTTP";
public static final String API_TYPE_WEBHOOK = "WEBHOOK";

public static final String API_TYPE_REST = "REST";

public static final String API_TYPE_GRAPHQL = "GRAPHQL";
public static final String API_TYPE_ASYNC = "ASYNC";

public static final String TRANSPORT_URL_IN = "TransportInURL";

// mock response generation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3308,6 +3308,24 @@
return APIUtil.removeXMediationScriptsFromSwagger(definition);
}

@Override
public String invokeApiChatExecute(String apiChatRequestId, String requestPayload) throws APIManagementException {
return APIUtil.invokeAIService(APIConstants.AI.API_CHAT_ENDPOINT,

Check warning on line 3313 in components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIConsumerImpl.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIConsumerImpl.java#L3313

Added line #L3313 was not covered by tests
APIConstants.AI.API_CHAT_AUTH_TOKEN, APIConstants.AI.API_CHAT_EXECUTE_RESOURCE, requestPayload,
apiChatRequestId);
}

@Override
public String invokeApiChatPrepare(String apiId, String apiChatRequestId, String organization)
throws APIManagementException {
String swaggerDefinition = getOpenAPIDefinition(apiId, organization);
String payload = "{\"openapi\": " + swaggerDefinition + "}";
String prepareResponse = APIUtil.invokeAIService(APIConstants.AI.API_CHAT_ENDPOINT,

Check warning on line 3323 in components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIConsumerImpl.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIConsumerImpl.java#L3321-L3323

Added lines #L3321 - L3323 were not covered by tests
APIConstants.AI.API_CHAT_AUTH_TOKEN, APIConstants.AI.API_CHAT_PREPARE_RESOURCE, payload,
apiChatRequestId);
return prepareResponse;

Check warning on line 3326 in components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIConsumerImpl.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIConsumerImpl.java#L3326

Added line #L3326 was not covered by tests
}

@Override
public String getOpenAPIDefinitionForEnvironment(API api, String environmentName)
throws APIManagementException {
Expand Down
Loading
Loading