@capacitor-mlkit/genai-rewriting 8.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CapacitorMlkitGenaiRewriting.podspec +17 -0
- package/LICENSE +201 -0
- package/README.md +429 -0
- package/android/build.gradle +60 -0
- package/android/src/main/AndroidManifest.xml +2 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/mlkit/genairewriting/GenAiRewriting.java +159 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/mlkit/genairewriting/GenAiRewritingPlugin.java +145 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/mlkit/genairewriting/classes/events/DownloadProgressEvent.java +22 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/mlkit/genairewriting/classes/events/InferenceProgressEvent.java +23 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/mlkit/genairewriting/classes/options/FeatureOptions.java +72 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/mlkit/genairewriting/classes/options/RewriteOptions.java +30 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/mlkit/genairewriting/classes/results/CheckFeatureStatusResult.java +37 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/mlkit/genairewriting/classes/results/RewriteResult.java +29 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/mlkit/genairewriting/interfaces/Callback.java +5 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/mlkit/genairewriting/interfaces/EmptyCallback.java +5 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/mlkit/genairewriting/interfaces/NonEmptyResultCallback.java +7 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/mlkit/genairewriting/interfaces/Result.java +7 -0
- package/android/src/main/res/.gitkeep +0 -0
- package/dist/docs.json +589 -0
- package/dist/esm/definitions.d.ts +251 -0
- package/dist/esm/definitions.js +107 -0
- package/dist/esm/definitions.js.map +1 -0
- package/dist/esm/index.d.ts +4 -0
- package/dist/esm/index.js +7 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/web.d.ts +8 -0
- package/dist/esm/web.js +16 -0
- package/dist/esm/web.js.map +1 -0
- package/dist/plugin.cjs.js +137 -0
- package/dist/plugin.cjs.js.map +1 -0
- package/dist/plugin.js +140 -0
- package/dist/plugin.js.map +1 -0
- package/ios/Plugin/GenAiRewritingPlugin.m +10 -0
- package/ios/Plugin/GenAiRewritingPlugin.swift +21 -0
- package/ios/Plugin/Info.plist +24 -0
- package/package.json +99 -0
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
ext {
|
|
2
|
+
junitVersion = project.hasProperty('junitVersion') ? rootProject.ext.junitVersion : '4.13.2'
|
|
3
|
+
androidxAppCompatVersion = project.hasProperty('androidxAppCompatVersion') ? rootProject.ext.androidxAppCompatVersion : '1.7.1'
|
|
4
|
+
androidxJunitVersion = project.hasProperty('androidxJunitVersion') ? rootProject.ext.androidxJunitVersion : '1.3.0'
|
|
5
|
+
androidxEspressoCoreVersion = project.hasProperty('androidxEspressoCoreVersion') ? rootProject.ext.androidxEspressoCoreVersion : '3.7.0'
|
|
6
|
+
mlkitGenaiRewritingVersion = project.hasProperty('mlkitGenaiRewritingVersion') ? rootProject.ext.mlkitGenaiRewritingVersion : '1.0.0-beta1'
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
buildscript {
|
|
10
|
+
repositories {
|
|
11
|
+
google()
|
|
12
|
+
mavenCentral()
|
|
13
|
+
}
|
|
14
|
+
dependencies {
|
|
15
|
+
classpath 'com.android.tools.build:gradle:8.13.0'
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
apply plugin: 'com.android.library'
|
|
20
|
+
|
|
21
|
+
android {
|
|
22
|
+
namespace = "io.capawesome.capacitorjs.plugins.mlkit.genairewriting"
|
|
23
|
+
compileSdk = project.hasProperty('compileSdkVersion') ? rootProject.ext.compileSdkVersion : 36
|
|
24
|
+
defaultConfig {
|
|
25
|
+
minSdkVersion project.hasProperty('minSdkVersion') ? rootProject.ext.minSdkVersion : 26
|
|
26
|
+
targetSdkVersion project.hasProperty('targetSdkVersion') ? rootProject.ext.targetSdkVersion : 36
|
|
27
|
+
versionCode 1
|
|
28
|
+
versionName "1.0"
|
|
29
|
+
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
|
|
30
|
+
}
|
|
31
|
+
buildTypes {
|
|
32
|
+
release {
|
|
33
|
+
minifyEnabled false
|
|
34
|
+
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
lintOptions {
|
|
38
|
+
abortOnError = false
|
|
39
|
+
}
|
|
40
|
+
compileOptions {
|
|
41
|
+
sourceCompatibility JavaVersion.VERSION_21
|
|
42
|
+
targetCompatibility JavaVersion.VERSION_21
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
repositories {
|
|
47
|
+
google()
|
|
48
|
+
mavenCentral()
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
dependencies {
|
|
53
|
+
implementation fileTree(dir: 'libs', include: ['*.jar'])
|
|
54
|
+
implementation project(':capacitor-android')
|
|
55
|
+
implementation "androidx.appcompat:appcompat:$androidxAppCompatVersion"
|
|
56
|
+
implementation "com.google.mlkit:genai-rewriting:$mlkitGenaiRewritingVersion"
|
|
57
|
+
testImplementation "junit:junit:$junitVersion"
|
|
58
|
+
androidTestImplementation "androidx.test.ext:junit:$androidxJunitVersion"
|
|
59
|
+
androidTestImplementation "androidx.test.espresso:espresso-core:$androidxEspressoCoreVersion"
|
|
60
|
+
}
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
package io.capawesome.capacitorjs.plugins.mlkit.genairewriting;
|
|
2
|
+
|
|
3
|
+
import androidx.annotation.NonNull;
|
|
4
|
+
import androidx.annotation.Nullable;
|
|
5
|
+
import com.google.common.util.concurrent.ListenableFuture;
|
|
6
|
+
import com.google.mlkit.genai.common.DownloadCallback;
|
|
7
|
+
import com.google.mlkit.genai.common.FeatureStatus;
|
|
8
|
+
import com.google.mlkit.genai.common.GenAiException;
|
|
9
|
+
import com.google.mlkit.genai.rewriting.Rewriter;
|
|
10
|
+
import com.google.mlkit.genai.rewriting.RewriterOptions;
|
|
11
|
+
import com.google.mlkit.genai.rewriting.Rewriting;
|
|
12
|
+
import com.google.mlkit.genai.rewriting.RewritingRequest;
|
|
13
|
+
import com.google.mlkit.genai.rewriting.RewritingResult;
|
|
14
|
+
import com.google.mlkit.genai.rewriting.RewritingSuggestion;
|
|
15
|
+
import io.capawesome.capacitorjs.plugins.mlkit.genairewriting.classes.events.DownloadProgressEvent;
|
|
16
|
+
import io.capawesome.capacitorjs.plugins.mlkit.genairewriting.classes.events.InferenceProgressEvent;
|
|
17
|
+
import io.capawesome.capacitorjs.plugins.mlkit.genairewriting.classes.options.FeatureOptions;
|
|
18
|
+
import io.capawesome.capacitorjs.plugins.mlkit.genairewriting.classes.options.RewriteOptions;
|
|
19
|
+
import io.capawesome.capacitorjs.plugins.mlkit.genairewriting.classes.results.CheckFeatureStatusResult;
|
|
20
|
+
import io.capawesome.capacitorjs.plugins.mlkit.genairewriting.classes.results.RewriteResult;
|
|
21
|
+
import io.capawesome.capacitorjs.plugins.mlkit.genairewriting.interfaces.EmptyCallback;
|
|
22
|
+
import io.capawesome.capacitorjs.plugins.mlkit.genairewriting.interfaces.NonEmptyResultCallback;
|
|
23
|
+
import java.util.ArrayList;
|
|
24
|
+
import java.util.List;
|
|
25
|
+
import java.util.concurrent.ExecutionException;
|
|
26
|
+
import java.util.concurrent.Executor;
|
|
27
|
+
import java.util.concurrent.Executors;
|
|
28
|
+
|
|
29
|
+
public class GenAiRewriting {
|
|
30
|
+
|
|
31
|
+
@NonNull
|
|
32
|
+
private final Executor executor = Executors.newSingleThreadExecutor();
|
|
33
|
+
|
|
34
|
+
@NonNull
|
|
35
|
+
private final GenAiRewritingPlugin plugin;
|
|
36
|
+
|
|
37
|
+
@Nullable
|
|
38
|
+
private Rewriter rewriter;
|
|
39
|
+
|
|
40
|
+
@Nullable
|
|
41
|
+
private FeatureOptions rewriterOptions;
|
|
42
|
+
|
|
43
|
+
public GenAiRewriting(@NonNull GenAiRewritingPlugin plugin) {
|
|
44
|
+
this.plugin = plugin;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
public void checkFeatureStatus(@NonNull FeatureOptions options, @NonNull NonEmptyResultCallback<CheckFeatureStatusResult> callback) {
|
|
48
|
+
Rewriter rewriter = getRewriter(options);
|
|
49
|
+
ListenableFuture<Integer> future = rewriter.checkFeatureStatus();
|
|
50
|
+
future.addListener(
|
|
51
|
+
() -> {
|
|
52
|
+
try {
|
|
53
|
+
int featureStatus = future.get();
|
|
54
|
+
callback.success(new CheckFeatureStatusResult(featureStatus));
|
|
55
|
+
} catch (Exception exception) {
|
|
56
|
+
callback.error(unwrapException(exception));
|
|
57
|
+
}
|
|
58
|
+
},
|
|
59
|
+
executor
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
public void downloadFeature(@NonNull FeatureOptions options, @NonNull EmptyCallback callback) {
|
|
64
|
+
Rewriter rewriter = getRewriter(options);
|
|
65
|
+
ListenableFuture<Integer> future = rewriter.checkFeatureStatus();
|
|
66
|
+
future.addListener(
|
|
67
|
+
() -> {
|
|
68
|
+
try {
|
|
69
|
+
int featureStatus = future.get();
|
|
70
|
+
if (featureStatus == FeatureStatus.AVAILABLE) {
|
|
71
|
+
callback.success();
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
rewriter.downloadFeature(
|
|
75
|
+
new DownloadCallback() {
|
|
76
|
+
@Override
|
|
77
|
+
public void onDownloadStarted(long bytesToDownload) {}
|
|
78
|
+
|
|
79
|
+
@Override
|
|
80
|
+
public void onDownloadProgress(long totalBytesDownloaded) {
|
|
81
|
+
plugin.notifyDownloadProgressListeners(new DownloadProgressEvent(totalBytesDownloaded));
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
@Override
|
|
85
|
+
public void onDownloadCompleted() {
|
|
86
|
+
callback.success();
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
@Override
|
|
90
|
+
public void onDownloadFailed(@NonNull GenAiException exception) {
|
|
91
|
+
callback.error(exception);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
);
|
|
95
|
+
} catch (Exception exception) {
|
|
96
|
+
callback.error(unwrapException(exception));
|
|
97
|
+
}
|
|
98
|
+
},
|
|
99
|
+
executor
|
|
100
|
+
);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
public void handleOnDestroy() {
|
|
104
|
+
closeRewriter();
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
public void rewrite(@NonNull RewriteOptions options, @NonNull NonEmptyResultCallback<RewriteResult> callback) {
|
|
108
|
+
Rewriter rewriter = getRewriter(options);
|
|
109
|
+
RewritingRequest request = RewritingRequest.builder(options.getText()).build();
|
|
110
|
+
ListenableFuture<RewritingResult> future = rewriter.runInference(request, additionalText ->
|
|
111
|
+
plugin.notifyInferenceProgressListeners(new InferenceProgressEvent(additionalText))
|
|
112
|
+
);
|
|
113
|
+
future.addListener(
|
|
114
|
+
() -> {
|
|
115
|
+
try {
|
|
116
|
+
RewritingResult result = future.get();
|
|
117
|
+
List<String> results = new ArrayList<>();
|
|
118
|
+
for (RewritingSuggestion suggestion : result.getResults()) {
|
|
119
|
+
results.add(suggestion.getText());
|
|
120
|
+
}
|
|
121
|
+
callback.success(new RewriteResult(results));
|
|
122
|
+
} catch (Exception exception) {
|
|
123
|
+
callback.error(unwrapException(exception));
|
|
124
|
+
}
|
|
125
|
+
},
|
|
126
|
+
executor
|
|
127
|
+
);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
private void closeRewriter() {
|
|
131
|
+
if (rewriter != null) {
|
|
132
|
+
rewriter.close();
|
|
133
|
+
rewriter = null;
|
|
134
|
+
rewriterOptions = null;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
@NonNull
|
|
139
|
+
private Rewriter getRewriter(@NonNull FeatureOptions options) {
|
|
140
|
+
if (rewriter == null || !options.matches(rewriterOptions)) {
|
|
141
|
+
closeRewriter();
|
|
142
|
+
RewriterOptions sdkOptions = RewriterOptions.builder(plugin.getContext())
|
|
143
|
+
.setLanguage(options.getLanguage())
|
|
144
|
+
.setOutputType(options.getOutputType())
|
|
145
|
+
.build();
|
|
146
|
+
rewriter = Rewriting.getClient(sdkOptions);
|
|
147
|
+
rewriterOptions = options;
|
|
148
|
+
}
|
|
149
|
+
return rewriter;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
@NonNull
|
|
153
|
+
private static Exception unwrapException(@NonNull Exception exception) {
|
|
154
|
+
if (exception instanceof ExecutionException && exception.getCause() instanceof Exception) {
|
|
155
|
+
return (Exception) exception.getCause();
|
|
156
|
+
}
|
|
157
|
+
return exception;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
package io.capawesome.capacitorjs.plugins.mlkit.genairewriting;
|
|
2
|
+
|
|
3
|
+
import androidx.annotation.NonNull;
|
|
4
|
+
import androidx.annotation.Nullable;
|
|
5
|
+
import com.getcapacitor.Logger;
|
|
6
|
+
import com.getcapacitor.Plugin;
|
|
7
|
+
import com.getcapacitor.PluginCall;
|
|
8
|
+
import com.getcapacitor.PluginMethod;
|
|
9
|
+
import com.getcapacitor.annotation.CapacitorPlugin;
|
|
10
|
+
import io.capawesome.capacitorjs.plugins.mlkit.genairewriting.classes.events.DownloadProgressEvent;
|
|
11
|
+
import io.capawesome.capacitorjs.plugins.mlkit.genairewriting.classes.events.InferenceProgressEvent;
|
|
12
|
+
import io.capawesome.capacitorjs.plugins.mlkit.genairewriting.classes.options.FeatureOptions;
|
|
13
|
+
import io.capawesome.capacitorjs.plugins.mlkit.genairewriting.classes.options.RewriteOptions;
|
|
14
|
+
import io.capawesome.capacitorjs.plugins.mlkit.genairewriting.classes.results.CheckFeatureStatusResult;
|
|
15
|
+
import io.capawesome.capacitorjs.plugins.mlkit.genairewriting.classes.results.RewriteResult;
|
|
16
|
+
import io.capawesome.capacitorjs.plugins.mlkit.genairewriting.interfaces.EmptyCallback;
|
|
17
|
+
import io.capawesome.capacitorjs.plugins.mlkit.genairewriting.interfaces.NonEmptyResultCallback;
|
|
18
|
+
import io.capawesome.capacitorjs.plugins.mlkit.genairewriting.interfaces.Result;
|
|
19
|
+
|
|
20
|
+
@CapacitorPlugin(name = "GenAiRewriting")
|
|
21
|
+
public class GenAiRewritingPlugin extends Plugin {
|
|
22
|
+
|
|
23
|
+
public static final String TAG = "GenAiRewriting";
|
|
24
|
+
|
|
25
|
+
public static final String EVENT_DOWNLOAD_PROGRESS = "downloadProgress";
|
|
26
|
+
public static final String EVENT_INFERENCE_PROGRESS = "inferenceProgress";
|
|
27
|
+
|
|
28
|
+
public static final String ERROR_LANGUAGE_INVALID = "language is invalid.";
|
|
29
|
+
public static final String ERROR_OUTPUT_TYPE_INVALID = "outputType is invalid.";
|
|
30
|
+
public static final String ERROR_TEXT_MISSING = "text must be provided.";
|
|
31
|
+
public static final String ERROR_UNKNOWN_ERROR = "An unknown error occurred.";
|
|
32
|
+
|
|
33
|
+
@Nullable
|
|
34
|
+
private GenAiRewriting implementation;
|
|
35
|
+
|
|
36
|
+
@PluginMethod
|
|
37
|
+
public void checkFeatureStatus(PluginCall call) {
|
|
38
|
+
try {
|
|
39
|
+
FeatureOptions options = new FeatureOptions(call);
|
|
40
|
+
NonEmptyResultCallback<CheckFeatureStatusResult> callback = new NonEmptyResultCallback<>() {
|
|
41
|
+
@Override
|
|
42
|
+
public void success(@NonNull CheckFeatureStatusResult result) {
|
|
43
|
+
resolveCall(call, result);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
@Override
|
|
47
|
+
public void error(Exception exception) {
|
|
48
|
+
rejectCall(call, exception);
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
assert implementation != null;
|
|
53
|
+
implementation.checkFeatureStatus(options, callback);
|
|
54
|
+
} catch (Exception exception) {
|
|
55
|
+
rejectCall(call, exception);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
@PluginMethod
|
|
60
|
+
public void downloadFeature(PluginCall call) {
|
|
61
|
+
try {
|
|
62
|
+
FeatureOptions options = new FeatureOptions(call);
|
|
63
|
+
EmptyCallback callback = new EmptyCallback() {
|
|
64
|
+
@Override
|
|
65
|
+
public void success() {
|
|
66
|
+
resolveCall(call);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
@Override
|
|
70
|
+
public void error(Exception exception) {
|
|
71
|
+
rejectCall(call, exception);
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
assert implementation != null;
|
|
76
|
+
implementation.downloadFeature(options, callback);
|
|
77
|
+
} catch (Exception exception) {
|
|
78
|
+
rejectCall(call, exception);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
@Override
|
|
83
|
+
public void load() {
|
|
84
|
+
implementation = new GenAiRewriting(this);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
public void notifyDownloadProgressListeners(@NonNull DownloadProgressEvent event) {
|
|
88
|
+
notifyListeners(EVENT_DOWNLOAD_PROGRESS, event.toJSObject());
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
public void notifyInferenceProgressListeners(@NonNull InferenceProgressEvent event) {
|
|
92
|
+
notifyListeners(EVENT_INFERENCE_PROGRESS, event.toJSObject());
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
@PluginMethod
|
|
96
|
+
public void rewrite(PluginCall call) {
|
|
97
|
+
try {
|
|
98
|
+
RewriteOptions options = new RewriteOptions(call);
|
|
99
|
+
NonEmptyResultCallback<RewriteResult> callback = new NonEmptyResultCallback<>() {
|
|
100
|
+
@Override
|
|
101
|
+
public void success(@NonNull RewriteResult result) {
|
|
102
|
+
resolveCall(call, result);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
@Override
|
|
106
|
+
public void error(Exception exception) {
|
|
107
|
+
rejectCall(call, exception);
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
assert implementation != null;
|
|
112
|
+
implementation.rewrite(options, callback);
|
|
113
|
+
} catch (Exception exception) {
|
|
114
|
+
rejectCall(call, exception);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
@Override
|
|
119
|
+
protected void handleOnDestroy() {
|
|
120
|
+
if (implementation != null) {
|
|
121
|
+
implementation.handleOnDestroy();
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
private void rejectCall(@NonNull PluginCall call, @NonNull Exception exception) {
|
|
126
|
+
String message = exception.getMessage();
|
|
127
|
+
if (message == null) {
|
|
128
|
+
message = ERROR_UNKNOWN_ERROR;
|
|
129
|
+
}
|
|
130
|
+
Logger.error(TAG, message, exception);
|
|
131
|
+
call.reject(message);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
private void resolveCall(@NonNull PluginCall call) {
|
|
135
|
+
call.resolve();
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
private void resolveCall(@NonNull PluginCall call, @Nullable Result result) {
|
|
139
|
+
if (result == null) {
|
|
140
|
+
call.resolve();
|
|
141
|
+
} else {
|
|
142
|
+
call.resolve(result.toJSObject());
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
package io.capawesome.capacitorjs.plugins.mlkit.genairewriting.classes.events;
|
|
2
|
+
|
|
3
|
+
import androidx.annotation.NonNull;
|
|
4
|
+
import com.getcapacitor.JSObject;
|
|
5
|
+
import io.capawesome.capacitorjs.plugins.mlkit.genairewriting.interfaces.Result;
|
|
6
|
+
|
|
7
|
+
public class DownloadProgressEvent implements Result {
|
|
8
|
+
|
|
9
|
+
private final long totalBytesDownloaded;
|
|
10
|
+
|
|
11
|
+
public DownloadProgressEvent(long totalBytesDownloaded) {
|
|
12
|
+
this.totalBytesDownloaded = totalBytesDownloaded;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
@Override
|
|
16
|
+
@NonNull
|
|
17
|
+
public JSObject toJSObject() {
|
|
18
|
+
JSObject result = new JSObject();
|
|
19
|
+
result.put("totalBytesDownloaded", totalBytesDownloaded);
|
|
20
|
+
return result;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
package io.capawesome.capacitorjs.plugins.mlkit.genairewriting.classes.events;
|
|
2
|
+
|
|
3
|
+
import androidx.annotation.NonNull;
|
|
4
|
+
import com.getcapacitor.JSObject;
|
|
5
|
+
import io.capawesome.capacitorjs.plugins.mlkit.genairewriting.interfaces.Result;
|
|
6
|
+
|
|
7
|
+
public class InferenceProgressEvent implements Result {
|
|
8
|
+
|
|
9
|
+
@NonNull
|
|
10
|
+
private final String text;
|
|
11
|
+
|
|
12
|
+
public InferenceProgressEvent(@NonNull String text) {
|
|
13
|
+
this.text = text;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
@Override
|
|
17
|
+
@NonNull
|
|
18
|
+
public JSObject toJSObject() {
|
|
19
|
+
JSObject result = new JSObject();
|
|
20
|
+
result.put("text", text);
|
|
21
|
+
return result;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
package io.capawesome.capacitorjs.plugins.mlkit.genairewriting.classes.options;
|
|
2
|
+
|
|
3
|
+
import androidx.annotation.NonNull;
|
|
4
|
+
import androidx.annotation.Nullable;
|
|
5
|
+
import com.getcapacitor.PluginCall;
|
|
6
|
+
import com.google.mlkit.genai.rewriting.RewriterOptions;
|
|
7
|
+
import io.capawesome.capacitorjs.plugins.mlkit.genairewriting.GenAiRewritingPlugin;
|
|
8
|
+
|
|
9
|
+
public class FeatureOptions {
|
|
10
|
+
|
|
11
|
+
private final int language;
|
|
12
|
+
private final int outputType;
|
|
13
|
+
|
|
14
|
+
public FeatureOptions(@NonNull PluginCall call) throws Exception {
|
|
15
|
+
this.language = FeatureOptions.getLanguageFromCall(call);
|
|
16
|
+
this.outputType = FeatureOptions.getOutputTypeFromCall(call);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
public int getLanguage() {
|
|
20
|
+
return language;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
public int getOutputType() {
|
|
24
|
+
return outputType;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
public boolean matches(@Nullable FeatureOptions other) {
|
|
28
|
+
return other != null && language == other.language && outputType == other.outputType;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
private static int getLanguageFromCall(@NonNull PluginCall call) throws Exception {
|
|
32
|
+
String language = call.getString("language", "ENGLISH");
|
|
33
|
+
switch (language) {
|
|
34
|
+
case "ENGLISH":
|
|
35
|
+
return RewriterOptions.Language.ENGLISH;
|
|
36
|
+
case "FRENCH":
|
|
37
|
+
return RewriterOptions.Language.FRENCH;
|
|
38
|
+
case "GERMAN":
|
|
39
|
+
return RewriterOptions.Language.GERMAN;
|
|
40
|
+
case "ITALIAN":
|
|
41
|
+
return RewriterOptions.Language.ITALIAN;
|
|
42
|
+
case "JAPANESE":
|
|
43
|
+
return RewriterOptions.Language.JAPANESE;
|
|
44
|
+
case "KOREAN":
|
|
45
|
+
return RewriterOptions.Language.KOREAN;
|
|
46
|
+
case "SPANISH":
|
|
47
|
+
return RewriterOptions.Language.SPANISH;
|
|
48
|
+
default:
|
|
49
|
+
throw new Exception(GenAiRewritingPlugin.ERROR_LANGUAGE_INVALID);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
private static int getOutputTypeFromCall(@NonNull PluginCall call) throws Exception {
|
|
54
|
+
String outputType = call.getString("outputType", "ELABORATE");
|
|
55
|
+
switch (outputType) {
|
|
56
|
+
case "ELABORATE":
|
|
57
|
+
return RewriterOptions.OutputType.ELABORATE;
|
|
58
|
+
case "EMOJIFY":
|
|
59
|
+
return RewriterOptions.OutputType.EMOJIFY;
|
|
60
|
+
case "SHORTEN":
|
|
61
|
+
return RewriterOptions.OutputType.SHORTEN;
|
|
62
|
+
case "FRIENDLY":
|
|
63
|
+
return RewriterOptions.OutputType.FRIENDLY;
|
|
64
|
+
case "PROFESSIONAL":
|
|
65
|
+
return RewriterOptions.OutputType.PROFESSIONAL;
|
|
66
|
+
case "REPHRASE":
|
|
67
|
+
return RewriterOptions.OutputType.REPHRASE;
|
|
68
|
+
default:
|
|
69
|
+
throw new Exception(GenAiRewritingPlugin.ERROR_OUTPUT_TYPE_INVALID);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
package io.capawesome.capacitorjs.plugins.mlkit.genairewriting.classes.options;
|
|
2
|
+
|
|
3
|
+
import androidx.annotation.NonNull;
|
|
4
|
+
import com.getcapacitor.PluginCall;
|
|
5
|
+
import io.capawesome.capacitorjs.plugins.mlkit.genairewriting.GenAiRewritingPlugin;
|
|
6
|
+
|
|
7
|
+
public class RewriteOptions extends FeatureOptions {
|
|
8
|
+
|
|
9
|
+
@NonNull
|
|
10
|
+
private final String text;
|
|
11
|
+
|
|
12
|
+
public RewriteOptions(@NonNull PluginCall call) throws Exception {
|
|
13
|
+
super(call);
|
|
14
|
+
this.text = RewriteOptions.getTextFromCall(call);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
@NonNull
|
|
18
|
+
public String getText() {
|
|
19
|
+
return text;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
@NonNull
|
|
23
|
+
private static String getTextFromCall(@NonNull PluginCall call) throws Exception {
|
|
24
|
+
String text = call.getString("text");
|
|
25
|
+
if (text == null) {
|
|
26
|
+
throw new Exception(GenAiRewritingPlugin.ERROR_TEXT_MISSING);
|
|
27
|
+
}
|
|
28
|
+
return text;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
package io.capawesome.capacitorjs.plugins.mlkit.genairewriting.classes.results;
|
|
2
|
+
|
|
3
|
+
import androidx.annotation.NonNull;
|
|
4
|
+
import com.getcapacitor.JSObject;
|
|
5
|
+
import com.google.mlkit.genai.common.FeatureStatus;
|
|
6
|
+
import io.capawesome.capacitorjs.plugins.mlkit.genairewriting.interfaces.Result;
|
|
7
|
+
|
|
8
|
+
public class CheckFeatureStatusResult implements Result {
|
|
9
|
+
|
|
10
|
+
private final int featureStatus;
|
|
11
|
+
|
|
12
|
+
public CheckFeatureStatusResult(int featureStatus) {
|
|
13
|
+
this.featureStatus = featureStatus;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
@Override
|
|
17
|
+
@NonNull
|
|
18
|
+
public JSObject toJSObject() {
|
|
19
|
+
JSObject result = new JSObject();
|
|
20
|
+
result.put("featureStatus", convertFeatureStatusToString(featureStatus));
|
|
21
|
+
return result;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
@NonNull
|
|
25
|
+
private static String convertFeatureStatusToString(int featureStatus) {
|
|
26
|
+
switch (featureStatus) {
|
|
27
|
+
case FeatureStatus.AVAILABLE:
|
|
28
|
+
return "AVAILABLE";
|
|
29
|
+
case FeatureStatus.DOWNLOADABLE:
|
|
30
|
+
return "DOWNLOADABLE";
|
|
31
|
+
case FeatureStatus.DOWNLOADING:
|
|
32
|
+
return "DOWNLOADING";
|
|
33
|
+
default:
|
|
34
|
+
return "UNAVAILABLE";
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
package io.capawesome.capacitorjs.plugins.mlkit.genairewriting.classes.results;
|
|
2
|
+
|
|
3
|
+
import androidx.annotation.NonNull;
|
|
4
|
+
import com.getcapacitor.JSArray;
|
|
5
|
+
import com.getcapacitor.JSObject;
|
|
6
|
+
import io.capawesome.capacitorjs.plugins.mlkit.genairewriting.interfaces.Result;
|
|
7
|
+
import java.util.List;
|
|
8
|
+
|
|
9
|
+
public class RewriteResult implements Result {
|
|
10
|
+
|
|
11
|
+
@NonNull
|
|
12
|
+
private final List<String> results;
|
|
13
|
+
|
|
14
|
+
public RewriteResult(@NonNull List<String> results) {
|
|
15
|
+
this.results = results;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
@Override
|
|
19
|
+
@NonNull
|
|
20
|
+
public JSObject toJSObject() {
|
|
21
|
+
JSArray resultsArray = new JSArray();
|
|
22
|
+
for (String result : results) {
|
|
23
|
+
resultsArray.put(result);
|
|
24
|
+
}
|
|
25
|
+
JSObject result = new JSObject();
|
|
26
|
+
result.put("results", resultsArray);
|
|
27
|
+
return result;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
File without changes
|