|
31 | 31 | import java.util.regex.Pattern; |
32 | 32 |
|
33 | 33 | import io.gleap.APPLICATIONTYPE; |
| 34 | +import io.gleap.GleapAiTool; |
| 35 | +import io.gleap.GleapAiToolParameter; |
34 | 36 | import io.gleap.GleapSessionProperties; |
35 | 37 | import io.gleap.SurveyType; |
| 38 | +import io.gleap.callbacks.AiToolExecutedCallback; |
36 | 39 | import io.gleap.callbacks.GetActivityCallback; |
37 | 40 | import io.gleap.Gleap; |
38 | 41 | import io.gleap.GleapActivationMethod; |
@@ -117,6 +120,14 @@ public void invoke() { |
117 | 120 | } |
118 | 121 | }); |
119 | 122 |
|
| 123 | + Gleap.getInstance().setAiToolExecutedCallback(new AiToolExecutedCallback() { |
| 124 | + @Override |
| 125 | + public void aiToolExecuted(JSONObject jsonObject) { |
| 126 | + getReactApplicationContext().getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class) |
| 127 | + .emit("toolExecution", jsonObject); |
| 128 | + } |
| 129 | + }); |
| 130 | + |
120 | 131 | Gleap.getInstance().setWidgetClosedCallback(new WidgetClosedCallback() { |
121 | 132 | @Override |
122 | 133 | public void invoke() { |
@@ -841,6 +852,88 @@ public void setCustomData(String key, String value) { |
841 | 852 | Gleap.getInstance().setCustomData(key, value); |
842 | 853 | } |
843 | 854 |
|
| 855 | + /** |
| 856 | + * Set the ai tools. |
| 857 | + * @param tools |
| 858 | + */ |
| 859 | + @ReactMethod |
| 860 | + public void setAiTools(ReadableArray tools) { |
| 861 | + try { |
| 862 | + if (Gleap.getInstance() == null) { |
| 863 | + return; |
| 864 | + } |
| 865 | + |
| 866 | + ArrayList<GleapAiTool> gleapAiTools = new ArrayList<>(); |
| 867 | + |
| 868 | + // Loop through the tools array |
| 869 | + for (int i = 0; i < tools.size(); i++) { |
| 870 | + ReadableMap tool = tools.getMap(i); |
| 871 | + if (tool == null) continue; |
| 872 | + |
| 873 | + String name = tool.getString("name"); |
| 874 | + String description = tool.getString("description"); |
| 875 | + String response = tool.getString("response"); |
| 876 | + ReadableArray parametersArray = tool.getArray("parameters"); |
| 877 | + ArrayList<GleapAiToolParameter> gleapParameters = new ArrayList<>(); |
| 878 | + |
| 879 | + if (parametersArray != null) { |
| 880 | + // Loop through the parameters array |
| 881 | + for (int j = 0; j < parametersArray.size(); j++) { |
| 882 | + ReadableMap parameter = parametersArray.getMap(j); |
| 883 | + if (parameter == null) continue; |
| 884 | + |
| 885 | + String paramName = parameter.getString("name"); |
| 886 | + String paramDescription = parameter.getString("description"); |
| 887 | + String type = parameter.getString("type"); |
| 888 | + boolean required = parameter.getBoolean("required"); |
| 889 | + String[] enums = null; |
| 890 | + if (parameter.hasKey("enum") && !parameter.isNull("enum")) { |
| 891 | + ReadableArray enumsArray = parameter.getArray("enum"); |
| 892 | + enums = new String[enumsArray.size()]; |
| 893 | + for (int k = 0; k < enumsArray.size(); k++) { |
| 894 | + enums[k] = enumsArray.getString(k); |
| 895 | + } |
| 896 | + } |
| 897 | + |
| 898 | + // Create a new parameter and add it to the list |
| 899 | + GleapAiToolParameter gleapParameter = new GleapAiToolParameter( |
| 900 | + paramName, paramDescription, type, required, enums); |
| 901 | + gleapParameters.add(gleapParameter); |
| 902 | + } |
| 903 | + } |
| 904 | + |
| 905 | + // Create the AI tool with parameters |
| 906 | + GleapAiToolParameter[] paramsArray = new GleapAiToolParameter[gleapParameters.size()]; |
| 907 | + paramsArray = gleapParameters.toArray(paramsArray); |
| 908 | + GleapAiTool gleapAiTool = new GleapAiTool( |
| 909 | + name, description, response, paramsArray); |
| 910 | + |
| 911 | + // Add the AI tool to the list |
| 912 | + gleapAiTools.add(gleapAiTool); |
| 913 | + } |
| 914 | + |
| 915 | + // Convert the list to an array and set the AI tools |
| 916 | + GleapAiTool[] toolsArray = new GleapAiTool[gleapAiTools.size()]; |
| 917 | + toolsArray = gleapAiTools.toArray(toolsArray); |
| 918 | + Gleap.getInstance().setAiTools(toolsArray); |
| 919 | + |
| 920 | + } catch (Exception e) { |
| 921 | + System.out.println("Error setting AI tools: " + e); |
| 922 | + } |
| 923 | + } |
| 924 | + |
| 925 | + /** |
| 926 | + * Set the value for a ticket attribute with key. |
| 927 | + * |
| 928 | + * @param value The value you want to add |
| 929 | + * @param key The key of the attribute |
| 930 | + * @author Gleap |
| 931 | + */ |
| 932 | + @ReactMethod |
| 933 | + public void setTicketAttribute(String key, String value) { |
| 934 | + Gleap.getInstance().setTicketAttribute(key, value); |
| 935 | + } |
| 936 | + |
844 | 937 | /** |
845 | 938 | * Removes one key from existing custom data. |
846 | 939 | * |
|
0 commit comments