Skip to content

Commit 1d952de

Browse files
authored
Merge pull request #12357 from wso2/ai-features
Add API Chat and Marketplace Assistant feature implementations
2 parents 3a1a822 + 9bc1f11 commit 1d952de

File tree

49 files changed

+3609
-24
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+3609
-24
lines changed

Diff for: components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/APIConsumer.java

+22
Original file line numberDiff line numberDiff line change
@@ -799,6 +799,28 @@ Set<SubscribedAPI> getPaginatedSubscribedAPIsByApplication(Application applicati
799799
*/
800800
Tier getThrottlePolicyByName(String name, int policyType, String organization) throws APIManagementException;
801801

802+
/**
803+
* Returns the API Chat execute call response as a string
804+
*
805+
* @param apiChatRequestId Request UUID
806+
* @param requestPayload Request payload to be used for the AI service execute call
807+
* @return execution response as a string
808+
* @throws APIManagementException if execute call failed
809+
*/
810+
String invokeApiChatExecute(String apiChatRequestId, String requestPayload) throws APIManagementException;
811+
812+
/**
813+
* Returns the API Chat prepare call response as a string
814+
*
815+
* @param apiId ID of the API
816+
* @param apiChatRequestId Request UUID
817+
* @param organization Identifier of an organization
818+
* @return prepare response
819+
* @throws APIManagementException if prepare call failed
820+
*/
821+
String invokeApiChatPrepare(String apiId, String apiChatRequestId, String organization)
822+
throws APIManagementException;
823+
802824
/**
803825
* This method used to retrieve key manager configurations for tenant
804826
* @param organization organization of the key manager

Diff for: components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/ExceptionCodes.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,11 @@ public enum ExceptionCodes implements ErrorHandler {
548548
KEY_MANAGER_RESTRICTED_FOR_USER(902013, "Unauthorized Access to Key Manager", 403, "Key Manager is Restricted for this user"),
549549
// Admin portal get apis and api provider change related errors
550550
CHANGE_API_PROVIDER_FAILED(903011, "Error while changing the API provider", 500, "Error while changing the API provider in the registry or DB"),
551-
GET_SEARCH_APIS_IN_ADMIN_FAILED(903012, "Error while getting the apis", 500, "Error while getting/searching the apis from registry");
551+
GET_SEARCH_APIS_IN_ADMIN_FAILED(903012, "Error while getting the apis", 500, "Error while getting/searching the apis from registry"),
552+
553+
// AI service invocation related exceptions
554+
AI_SERVICE_INVALID_RESPONSE(903100, "Invalid response from AI service", 500, "Error while invoking AI service. %s", false),
555+
AI_SERVICE_INVALID_ACCESS_TOKEN(900906, "Invalid access token provided for AI service", 401, "Invalid access token provided for AI service");
552556

553557
private final long errorCode;
554558
private final String errorMessage;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright (c) 2024, WSO2 LLC. (http://www.wso2.org) All Rights Reserved.
3+
*
4+
* WSO2 Inc. licenses this file to you under the Apache License,
5+
* Version 2.0 (the "License"); you may not use this file except
6+
* in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing,
12+
* software distributed under the License is distributed on an
13+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
* KIND, either express or implied. See the License for the
15+
* specific language governing permissions and limitations
16+
* under the License.
17+
*/
18+
19+
package org.wso2.carbon.apimgt.api.model;
20+
21+
import org.json.simple.JSONArray;
22+
23+
public class APIChatAPISpec {
24+
private String serviceUrl = null;
25+
private JSONArray tools;
26+
27+
public String getServiceUrl() {
28+
return serviceUrl;
29+
}
30+
31+
public void setServiceUrl(String serviceUrl) {
32+
this.serviceUrl = serviceUrl;
33+
}
34+
35+
public JSONArray getTools() {
36+
return tools;
37+
}
38+
39+
public void setTools(JSONArray tools) {
40+
this.tools = tools;
41+
}
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright (c) 2024, WSO2 LLC. (http://www.wso2.org) All Rights Reserved.
3+
*
4+
* WSO2 Inc. licenses this file to you under the Apache License,
5+
* Version 2.0 (the "License"); you may not use this file except
6+
* in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing,
12+
* software distributed under the License is distributed on an
13+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
* KIND, either express or implied. See the License for the
15+
* specific language governing permissions and limitations
16+
* under the License.
17+
*/
18+
19+
package org.wso2.carbon.apimgt.api.model;
20+
21+
public class APIChatExecutionResponse {
22+
private Integer code = null;
23+
private String path = null;
24+
private Object headers = null;
25+
private Object body = null;
26+
27+
public Integer getCode() {
28+
return code;
29+
}
30+
31+
public void setCode(Integer code) {
32+
this.code = code;
33+
}
34+
35+
public String getPath() {
36+
return path;
37+
}
38+
39+
public void setPath(String path) {
40+
this.path = path;
41+
}
42+
43+
public Object getHeaders() {
44+
return headers;
45+
}
46+
47+
public void setHeaders(Object headers) {
48+
this.headers = headers;
49+
}
50+
51+
public Object getBody() {
52+
return body;
53+
}
54+
55+
public void setBody(Object body) {
56+
this.body = body;
57+
}
58+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright (c) 2024, WSO2 LLC. (http://www.wso2.org) All Rights Reserved.
3+
*
4+
* WSO2 Inc. licenses this file to you under the Apache License,
5+
* Version 2.0 (the "License"); you may not use this file except
6+
* in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing,
12+
* software distributed under the License is distributed on an
13+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
* KIND, either express or implied. See the License for the
15+
* specific language governing permissions and limitations
16+
* under the License.
17+
*/
18+
19+
package org.wso2.carbon.apimgt.api.model;
20+
21+
/**
22+
* Details related to API Chat test execution
23+
*/
24+
public class APIChatTestExecutionInfo {
25+
private APIChatExecutionResponse response = null;
26+
27+
public APIChatExecutionResponse getResponse() {
28+
return response;
29+
}
30+
31+
public void setResponse(APIChatExecutionResponse response) {
32+
this.response = response;
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright (c) 2024, WSO2 LLC. (http://www.wso2.org) All Rights Reserved.
3+
*
4+
* WSO2 Inc. licenses this file to you under the Apache License,
5+
* Version 2.0 (the "License"); you may not use this file except
6+
* in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing,
12+
* software distributed under the License is distributed on an
13+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
* KIND, either express or implied. See the License for the
15+
* specific language governing permissions and limitations
16+
* under the License.
17+
*/
18+
19+
package org.wso2.carbon.apimgt.api.model;
20+
21+
import org.json.simple.JSONObject;
22+
23+
/**
24+
* Details related to API Chat test initialization
25+
*/
26+
public class APIChatTestInitializerInfo {
27+
private String command = null;
28+
private APIChatAPISpec apiSpec = null;
29+
30+
public String getCommand() {
31+
return command;
32+
}
33+
34+
public void setCommand(String command) {
35+
this.command = command;
36+
}
37+
38+
public APIChatAPISpec getApiSpec() {
39+
return apiSpec;
40+
}
41+
42+
public void setApiSpec(APIChatAPISpec apiSpec) {
43+
this.apiSpec = apiSpec;
44+
}
45+
}

Diff for: components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIConstants.java

+56
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,23 @@ public final class APIConstants {
501501
public static final String DIGEST = "x5t#S256";
502502
public static final String CNF = "cnf";
503503

504+
// Constants related to AI features: API chat and Marketplace Assistant
505+
public static class AI {
506+
507+
public static final String API_CHAT = "APIChat.";
508+
public static final String API_CHAT_ENABLED = API_CHAT + "Enabled";
509+
public static final String API_CHAT_AUTH_TOKEN = API_CHAT + "AuthToken";
510+
public static final String API_CHAT_ENDPOINT = API_CHAT + "Endpoint";
511+
public static final String API_CHAT_PREPARE_RESOURCE = "/prepare";
512+
public static final String API_CHAT_EXECUTE_RESOURCE = "/chat";
513+
public static final String API_CHAT_ACTION_PREPARE = "PREPARE";
514+
public static final String API_CHAT_ACTION_EXECUTE = "EXECUTE";
515+
516+
private AI() {
517+
518+
}
519+
}
520+
504521
//documentation rxt
505522

506523
public static final String DOC_NAME = "overview_name";
@@ -531,6 +548,17 @@ public final class APIConstants {
531548
public static final String FIRST_NAME = DEFAULT_CARBON_DIALECT + "/givenname";
532549
public static final String LAST_NAME = DEFAULT_CARBON_DIALECT + "/lastname";
533550

551+
// constants for marketplace assistant
552+
public static final String MARKETPLACE_ASSISTANT = "MarketplaceAssistant";
553+
public static final String MARKETPLACE_ASSISTANT_ENABLED = "Enabled";
554+
public static final String MARKETPLACE_ASSISTANT_AUTH_TOKEN = "AuthToken";
555+
public static final String MARKETPLACE_ASSISTANT_ENDPOINT = "Endpoint";
556+
public static final String MARKETPLACE_ASSISTANT_CHAT_RESOURCE = "ChatResource";
557+
public static final String MARKETPLACE_ASSISTANT_PUBLISH_API_RESOURCE = "ApiPublishResource";
558+
public static final String MARKETPLACE_ASSISTANT_DELETE_API_RESOURCE = "ApiDeleteResource";
559+
public static final String MARKETPLACE_ASSISTANT_API_COUNT_RESOURCE = "ApiCountResource";
560+
561+
534562
//Overview constants for CORS configuration
535563
public static final String API_OVERVIEW_CORS_CONFIGURATION = "overview_corsConfiguration";
536564
//Registry lifecycle related info
@@ -1678,6 +1706,34 @@ private ConfigParameters() {
16781706
public static final String API_DATA_URL = "url";
16791707
public static final String API_UUID = "apiUUID";
16801708

1709+
1710+
public static final String UUID = "uuid";
1711+
public static final String API_SPEC_TYPE = "api_type";
1712+
public static final String API_SPEC_NAME = "api_name";
1713+
public static final String TENANT_DOMAIN = "tenant_domain";
1714+
public static final String QUERY = "query";
1715+
public static final String HISTORY = "history";
1716+
public static final String VERSION = "version";
1717+
public static final String DESCRIPTION = "description";
1718+
1719+
public static final String DEMOTE_TO_CREATED= "Demote to Created";
1720+
public static final String BLOCK = "Block";
1721+
public static final String DEPRECATE = "Deprecate";
1722+
public static final String PUBLISH = "Publish";
1723+
public static final String DEPLOY_AS_A_PROTOTYPE = "Deploy as a Prototype";
1724+
public static final String REPUBLISH = "Re-Publish";
1725+
1726+
public static final String API_SPEC_TYPE_REST = "api_spec";
1727+
public static final String API_SPEC_TYPE_GRAPHQL = "sdl_schema";
1728+
public static final String API_SPEC_TYPE_ASYNC = "async_spec";
1729+
public static final String API_TYPE_HTTP = "HTTP";
1730+
public static final String API_TYPE_WEBHOOK = "WEBHOOK";
1731+
1732+
public static final String API_TYPE_REST = "REST";
1733+
1734+
public static final String API_TYPE_GRAPHQL = "GRAPHQL";
1735+
public static final String API_TYPE_ASYNC = "ASYNC";
1736+
16811737
public static final String TRANSPORT_URL_IN = "TransportInURL";
16821738

16831739
// mock response generation

Diff for: components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIConsumerImpl.java

+18
Original file line numberDiff line numberDiff line change
@@ -3308,6 +3308,24 @@ public String getOpenAPIDefinition(String apiId, String organization) throws API
33083308
return APIUtil.removeXMediationScriptsFromSwagger(definition);
33093309
}
33103310

3311+
@Override
3312+
public String invokeApiChatExecute(String apiChatRequestId, String requestPayload) throws APIManagementException {
3313+
return APIUtil.invokeAIService(APIConstants.AI.API_CHAT_ENDPOINT,
3314+
APIConstants.AI.API_CHAT_AUTH_TOKEN, APIConstants.AI.API_CHAT_EXECUTE_RESOURCE, requestPayload,
3315+
apiChatRequestId);
3316+
}
3317+
3318+
@Override
3319+
public String invokeApiChatPrepare(String apiId, String apiChatRequestId, String organization)
3320+
throws APIManagementException {
3321+
String swaggerDefinition = getOpenAPIDefinition(apiId, organization);
3322+
String payload = "{\"openapi\": " + swaggerDefinition + "}";
3323+
String prepareResponse = APIUtil.invokeAIService(APIConstants.AI.API_CHAT_ENDPOINT,
3324+
APIConstants.AI.API_CHAT_AUTH_TOKEN, APIConstants.AI.API_CHAT_PREPARE_RESOURCE, payload,
3325+
apiChatRequestId);
3326+
return prepareResponse;
3327+
}
3328+
33113329
@Override
33123330
public String getOpenAPIDefinitionForEnvironment(API api, String environmentName)
33133331
throws APIManagementException {

0 commit comments

Comments
 (0)