@independo/capacitor-inderun 1.1.0-dev.1 → 1.1.0-dev.10
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/Package.swift +19 -6
- package/README.md +126 -23
- package/android/build.gradle.kts +12 -6
- package/android/settings.gradle +7 -0
- package/android/src/main/kotlin/app/independo/inderun/capacitor/IndeRunCapacitorPlugin.kt +35 -0
- package/android/src/main/kotlin/app/independo/inderun/capacitor/IndeRunSerializer.kt +97 -0
- package/android/src/test/kotlin/app/independo/inderun/capacitor/IndeRunSerializerCapabilitiesTest.kt +253 -0
- package/android/src/test/kotlin/app/independo/inderun/capacitor/IndeRunSerializerTest.kt +79 -0
- package/dist/definitions.d.ts +122 -1
- package/dist/definitions.d.ts.map +1 -1
- package/dist/index.d.ts +6 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +14 -0
- package/dist/web.d.ts +2 -1
- package/dist/web.d.ts.map +1 -1
- package/dist/web.js +66 -7
- package/ios/Sources/IndeRunCapacitorPlugin/IndeRunCapacitorBridge.swift +60 -0
- package/ios/Sources/IndeRunCapacitorPlugin/IndeRunCapacitorPlugin.swift +25 -4
- package/ios/Tests/IndeRunCapacitorTests/IndeRunCapacitorBridgeTests.swift +234 -0
- package/package.json +21 -9
package/Package.swift
CHANGED
|
@@ -9,22 +9,35 @@ import PackageDescription
|
|
|
9
9
|
let package = Package(
|
|
10
10
|
name: "IndeRunCapacitor",
|
|
11
11
|
platforms: [
|
|
12
|
-
.iOS(.
|
|
12
|
+
.iOS(.v16),
|
|
13
13
|
.macOS(.v14)
|
|
14
14
|
],
|
|
15
15
|
products: [
|
|
16
16
|
.library(
|
|
17
17
|
name: "IndeRunCapacitor",
|
|
18
18
|
targets: ["IndeRunCapacitorPlugin"]
|
|
19
|
+
),
|
|
20
|
+
// The Capacitor CLI derives a product name from the npm package name
|
|
21
|
+
// (`@independo/capacitor-inderun` -> `IndependoCapacitorInderun`) and writes exactly
|
|
22
|
+
// that into the app's generated CapApp-SPM manifest. Without a product under this
|
|
23
|
+
// name, `cap add ios` produces a project that cannot resolve its dependencies at all.
|
|
24
|
+
// Kept alongside the original name rather than replacing it, so anything already
|
|
25
|
+
// depending on `IndeRunCapacitor` by URL keeps resolving.
|
|
26
|
+
.library(
|
|
27
|
+
name: "IndependoCapacitorInderun",
|
|
28
|
+
targets: ["IndeRunCapacitorPlugin"]
|
|
19
29
|
)
|
|
20
30
|
],
|
|
21
31
|
dependencies: [
|
|
22
32
|
.package(url: "https://github.com/ionic-team/capacitor-swift-pm.git", from: "8.0.0"),
|
|
23
|
-
//
|
|
24
|
-
//
|
|
25
|
-
//
|
|
26
|
-
//
|
|
27
|
-
|
|
33
|
+
// Ranged rather than `exact:` so a consumer that also depends on `inderun`
|
|
34
|
+
// directly can unify the graph. `.upToNextMinor` rather than `from:` because
|
|
35
|
+
// `from:` on a 0.x means `<1.0.0` — it would let SwiftPM float across a minor
|
|
36
|
+
// (where an 0.x SDK's breaking changes live) while package.json and
|
|
37
|
+
// android/build.gradle.kts stay pinned, which is exactly the three-platform
|
|
38
|
+
// drift the versioning policy forbids. Both operators exclude prereleases:
|
|
39
|
+
// tracking a `-dev.N` again requires going back to `exact:`.
|
|
40
|
+
.package(url: "https://github.com/independo-gmbh/inderun.git", .upToNextMinor(from: "0.3.0"))
|
|
28
41
|
],
|
|
29
42
|
targets: [
|
|
30
43
|
.target(
|
package/README.md
CHANGED
|
@@ -14,10 +14,15 @@
|
|
|
14
14
|
|
|
15
15
|
<p align="center">Built and maintained by <a href="https://www.independo.app/">Independo</a>.</p>
|
|
16
16
|
|
|
17
|
-
|
|
17
|
+
**On-device AI with cloud fallback, for Capacitor apps.**
|
|
18
18
|
|
|
19
|
-
|
|
20
|
-
|
|
19
|
+
Runs a prompt on the device when the device can — Apple Foundation Models on iOS, ML Kit GenAI
|
|
20
|
+
(Gemini Nano) on Android — and falls back to an OpenAI-compatible cloud endpoint when it cannot.
|
|
21
|
+
One API across iOS, Android and the web, with streaming and cancellation. MIT.
|
|
22
|
+
|
|
23
|
+
It is a thin bridge, not a second engine. The package delegates to the released
|
|
24
|
+
[IndeRun](https://github.com/independo-gmbh/inderun) platform SDKs instead of re-implementing
|
|
25
|
+
routing, provider logic, or normalized error handling:
|
|
21
26
|
|
|
22
27
|
- web: [`@independo/inderun-web`](https://www.npmjs.com/package/@independo/inderun-web) (npm)
|
|
23
28
|
- iOS: `IndeRun` Swift package (`github.com/independo-gmbh/inderun`, SwiftPM)
|
|
@@ -38,10 +43,72 @@ Then sync native projects with your normal Capacitor workflow.
|
|
|
38
43
|
|
|
39
44
|
| Platform | Minimum version |
|
|
40
45
|
|----------|------------------------|
|
|
41
|
-
| iOS |
|
|
46
|
+
| iOS | 16.0 |
|
|
42
47
|
| Android | API 26 (Android 8.0) |
|
|
43
48
|
| Web | Any modern browser |
|
|
44
49
|
|
|
50
|
+
The iOS floor comes from the IndeRun Swift package this bridge depends on, not from the bridge
|
|
51
|
+
itself. On-device execution needs more than the floor: Apple Foundation Models requires an Apple
|
|
52
|
+
Intelligence–capable device on iOS 26+, and ML Kit GenAI requires AICore / Gemini Nano support.
|
|
53
|
+
Availability is checked at runtime and the cloud provider serves the request when it is missing.
|
|
54
|
+
|
|
55
|
+
## Host Project Requirements
|
|
56
|
+
|
|
57
|
+
The IndeRun SDKs this bridge wraps are newer than a freshly generated Capacitor app's
|
|
58
|
+
defaults, so `cap add` alone is not enough. Each of these is a hard requirement — the
|
|
59
|
+
corresponding build failure is named so it is searchable:
|
|
60
|
+
|
|
61
|
+
**Android** (`android/variables.gradle` and `android/build.gradle` in your app):
|
|
62
|
+
|
|
63
|
+
| Setting | Value | Failure if unset |
|
|
64
|
+
|---|---|---|
|
|
65
|
+
| `compileSdkVersion` | `37` | *"requires libraries and applications that depend on it to compile against version 37 or later"* |
|
|
66
|
+
| `minSdkVersion` | `26` | manifest merger conflict |
|
|
67
|
+
| Android Gradle Plugin | `9.1.0`+ (Gradle 9.x) | *"requires Android Gradle plugin 9.1.0 or higher"* |
|
|
68
|
+
| `org.jetbrains.kotlin:kotlin-gradle-plugin:2.4.10` on the app's `buildscript` classpath | — | *"Module was compiled with an incompatible version of Kotlin … metadata is 2.4.0, expected version is 2.2.0"* |
|
|
69
|
+
|
|
70
|
+
The Kotlin one is the surprising entry: the `inderun-*` artifacts carry Kotlin 2.4.x
|
|
71
|
+
metadata, which the Kotlin plugin AGP brings by default cannot read. The plugin's own build
|
|
72
|
+
hoists a newer KGP for its standalone build, but a consuming app resolves KGP from its own
|
|
73
|
+
buildscript classpath, so the app has to add it too:
|
|
74
|
+
|
|
75
|
+
```groovy
|
|
76
|
+
// android/build.gradle
|
|
77
|
+
buildscript {
|
|
78
|
+
dependencies {
|
|
79
|
+
classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:2.4.10'
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
AGP 9 also rejects `getDefaultProguardFile('proguard-android.txt')`; switch to
|
|
85
|
+
`proguard-android-optimize.txt`.
|
|
86
|
+
|
|
87
|
+
**iOS**: set `IPHONEOS_DEPLOYMENT_TARGET` to `16.0` in your Xcode project **before**
|
|
88
|
+
`npx cap sync ios`. The Capacitor CLI reads that value to generate `CapApp-SPM/Package.swift`,
|
|
89
|
+
so syncing with the default leaves a manifest pinned below this package's floor and the build
|
|
90
|
+
fails with *"requires minimum platform version 16.0 for the iOS platform, but this target
|
|
91
|
+
supports 15.0"*.
|
|
92
|
+
|
|
93
|
+
## Supported IndeRun Version
|
|
94
|
+
|
|
95
|
+
This release tracks **IndeRun 0.3.0** on all three platforms:
|
|
96
|
+
|
|
97
|
+
| Platform | Artifact | Constraint |
|
|
98
|
+
|----------|----------|------------|
|
|
99
|
+
| Web | `@independo/inderun-web`, `@independo/inderun-contracts` | `0.3.0` (exact) |
|
|
100
|
+
| iOS | `inderun` SwiftPM package | `>=0.3.0 <0.4.0` |
|
|
101
|
+
| Android | `app.independo.inderun:inderun-*` | `0.3.0` (exact) |
|
|
102
|
+
|
|
103
|
+
The npm and Gradle pins are exact and all three are bumped together — a partial bump is how the
|
|
104
|
+
platforms drift apart. The SwiftPM constraint is ranged rather than exact so an app that also depends
|
|
105
|
+
on `inderun` directly can still unify its package graph; it stops at the next minor because that is
|
|
106
|
+
where an 0.x SDK's breaking changes live.
|
|
107
|
+
|
|
108
|
+
This package versions **independently** of the IndeRun monorepo under plain semver — the numbers are
|
|
109
|
+
unrelated, and this bridge's own version says nothing about which IndeRun release it wraps. Read that
|
|
110
|
+
off the table above.
|
|
111
|
+
|
|
45
112
|
## Usage
|
|
46
113
|
|
|
47
114
|
```ts
|
|
@@ -96,42 +163,78 @@ for await (const event of run.events) {
|
|
|
96
163
|
run.cancel("user navigated away"); // idempotent; a no-op after the terminal
|
|
97
164
|
```
|
|
98
165
|
|
|
99
|
-
|
|
166
|
+
Event semantics, ordering, the terminal guarantees, cancellation and fallback are the engines'
|
|
167
|
+
contract, identical on every platform, and documented once in
|
|
168
|
+
[Streaming (Mode 2)](https://github.com/independo-gmbh/inderun/blob/dev/docs/streaming.md). Two
|
|
169
|
+
things are specific to reaching them through a bridge:
|
|
100
170
|
|
|
101
|
-
- **
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
- **A failed run is not a rejected promise
|
|
105
|
-
|
|
106
|
-
the cumulative text rather than appending to it, and an empty one is how a provider
|
|
107
|
-
retracts content it already delivered — for example when an on-device safety check
|
|
108
|
-
rejects a half-generated response.
|
|
171
|
+
- **The bridge hop is not order-preserving.** That is why `sequence` rather than arrival is the
|
|
172
|
+
contract's ordering authority. `events` already yields strictly by it; if you attach your own
|
|
173
|
+
listener to `STREAM_EVENT_NAME` instead, ordering is yours to enforce.
|
|
174
|
+
- **A failed run is not a rejected promise**, and a bridge transport fault is a third thing again.
|
|
175
|
+
See [Error Handling](#error-handling).
|
|
109
176
|
|
|
110
177
|
## API
|
|
111
178
|
|
|
112
179
|
- `createIndeRunCapacitor(options)` — returns a handle that lazily `configure()`s on the first `run()` or `stream()` and memoizes it. Safe to call once at app startup.
|
|
113
180
|
- `run(request)` — Mode 1. Resolves with the canonical IndeRun `TaskResult`.
|
|
114
181
|
- `stream(request)` — Mode 2. Resolves with a `StreamRun` (`handle`, `events`, `cancel`). `events` is single-use.
|
|
182
|
+
- `checkCapabilities()` — every registered provider's static declaration and live availability, without executing a task. Resolves with `ProviderCapabilitySnapshot[]`. Use it for a provider or settings screen; availability changes between calls, so do not cache it across a `run()` or `stream()`.
|
|
115
183
|
- The low-level plugin methods `configure(options)`, `startStream(options)` and `cancelStream(options)` are also exported, along with the listener event names `STREAM_EVENT_NAME` (`"indeRunStreamEvent"`) and `STREAM_ERROR_NAME` (`"indeRunStreamError"`).
|
|
116
184
|
|
|
117
185
|
> The two listener event names are **public contract**. Native emits exactly these, and an
|
|
118
186
|
> app may attach its own listener to them; renaming one is a breaking change.
|
|
119
187
|
|
|
120
|
-
The `IndeRunCapacitorPlugin
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
188
|
+
The `IndeRunCapacitorPlugin` and `ConfigureOptions` contracts — including the `openAI`,
|
|
189
|
+
`systemModel` and `onnx` bootstrap configs and the `allowDirectOpenAIEndpoint` flag — are
|
|
190
|
+
defined and documented in `src/definitions.ts`.
|
|
191
|
+
|
|
192
|
+
`systemModel`, `onnx` and `allowDirectOpenAIEndpoint` are **web-only** and ignored on iOS
|
|
193
|
+
and Android, which register their own on-device provider from `configure()` regardless.
|
|
194
|
+
`systemModel` registers the browser-managed on-device provider (Chrome's Prompt API) and is
|
|
195
|
+
what makes `constraints.privacy = "local_required"` routable in a browser. Both on-device
|
|
196
|
+
web providers are Mode 1 only.
|
|
197
|
+
|
|
198
|
+
`onnx` carries two caveats. It needs the consumer to install the optional
|
|
199
|
+
`@huggingface/transformers` peer dependency — this bridge does not declare it — and to
|
|
200
|
+
supply real model weights, because the web SDK's `runtime` injection seam is a function and
|
|
201
|
+
so cannot cross a JSON bridge hop: only the default Transformers.js runtime is reachable
|
|
202
|
+
through `configure()`, never the fixture runtime the upstream demos use offline. Register it
|
|
203
|
+
only when the weights are there; a provider that cannot load turns a clean routing refusal
|
|
204
|
+
into a provider error.
|
|
205
|
+
|
|
206
|
+
Two asymmetries in the low-level surface, both hidden by the ergonomic API:
|
|
207
|
+
|
|
208
|
+
- `run(request)` passes the request at the options root, while
|
|
209
|
+
`startStream({ streamId, request })` nests it, because that envelope also carries the
|
|
210
|
+
bridge-local correlation id. `stream()` hides this.
|
|
211
|
+
- The plugin method `checkCapabilities()` resolves `{ providers: [...] }` rather than the
|
|
212
|
+
array itself, because a Capacitor plugin method cannot resolve a top-level array on
|
|
213
|
+
either native platform. `IndeRunCapacitor.checkCapabilities()` unwraps it, so app code
|
|
214
|
+
sees the same array the three platform SDKs return.
|
|
215
|
+
|
|
216
|
+
`ProviderCapabilitySnapshot` and the `ProviderDescriptor` /
|
|
217
|
+
`ProviderDynamicCapabilities` it contains are declared in `src/definitions.ts` rather than
|
|
218
|
+
imported, because — unlike `TaskRequest` or `StreamEvent` — they are not generated
|
|
219
|
+
contracts: each platform SDK declares its own copy and there is no schema or validator for
|
|
220
|
+
them upstream. `src/web.ts` returns the web SDK's snapshots into the bridge's type uncast,
|
|
221
|
+
so the shapes staying identical is a compile error rather than a convention. Note
|
|
222
|
+
`capabilities.streamingAvailable` and `cancellationAvailable` are **absent**, not `null`,
|
|
223
|
+
when the runtime has nothing to add: absence means *inherit the static declaration*.
|
|
128
224
|
|
|
129
225
|
## Platform Notes
|
|
130
226
|
|
|
131
|
-
- Web requires
|
|
227
|
+
- Web requires at least one provider to be registered from `configure()`. The web SDK ships an
|
|
228
|
+
OpenAI-compatible provider, a Web ONNX Runtime provider and a browser system-model provider, but
|
|
229
|
+
only the OpenAI-compatible one declares streaming support — so a `local_required` **stream** in a
|
|
230
|
+
browser is refused at routing time, while a `local_required` **run** can be served on-device.
|
|
132
231
|
- iOS always registers the Apple on-device provider and optionally registers OpenAI when configured.
|
|
133
232
|
- Android always registers the ML Kit on-device provider and optionally registers OpenAI when configured.
|
|
134
|
-
- Keep credentials behind `authContextRef`.
|
|
233
|
+
- Keep credentials behind `authContextRef`. That keeps a secret out of the request payload and out
|
|
234
|
+
of source; it does not make a key safe to ship, since anything an installed app or a browser can
|
|
235
|
+
read, someone with that app or browser can read. For a key you own, put it behind a backend you
|
|
236
|
+
control and point `endpointUrl` at that — for browser apps, a same-origin proxy with
|
|
237
|
+
`auth: "none"`.
|
|
135
238
|
|
|
136
239
|
## Current Limitations
|
|
137
240
|
|
package/android/build.gradle.kts
CHANGED
|
@@ -10,7 +10,13 @@ buildscript {
|
|
|
10
10
|
}
|
|
11
11
|
|
|
12
12
|
plugins {
|
|
13
|
-
|
|
13
|
+
// No version here on purpose. When a Capacitor app includes this module, AGP is already
|
|
14
|
+
// on the app's classpath and a versioned request fails with "the plugin is already on the
|
|
15
|
+
// classpath with an unknown version, so compatibility cannot be checked" — i.e. the plugin
|
|
16
|
+
// could not be consumed at all. The version for a standalone build of this directory is
|
|
17
|
+
// declared in settings.gradle's pluginManagement instead, which a consuming app's own
|
|
18
|
+
// settings file replaces.
|
|
19
|
+
id("com.android.library")
|
|
14
20
|
}
|
|
15
21
|
|
|
16
22
|
android {
|
|
@@ -37,11 +43,11 @@ dependencies {
|
|
|
37
43
|
testImplementation("com.capacitorjs:core:8.0.0")
|
|
38
44
|
implementation("androidx.appcompat:appcompat:1.7.1")
|
|
39
45
|
implementation("androidx.core:core-ktx:1.17.0")
|
|
40
|
-
implementation("app.independo.inderun:inderun-contracts:0.3.0
|
|
41
|
-
implementation("app.independo.inderun:inderun-core:0.3.0
|
|
42
|
-
implementation("app.independo.inderun:inderun-kotlin:0.3.0
|
|
43
|
-
implementation("app.independo.inderun:inderun-mlkit-providers:0.3.0
|
|
44
|
-
implementation("app.independo.inderun:inderun-openai-providers:0.3.0
|
|
46
|
+
implementation("app.independo.inderun:inderun-contracts:0.3.0")
|
|
47
|
+
implementation("app.independo.inderun:inderun-core:0.3.0")
|
|
48
|
+
implementation("app.independo.inderun:inderun-kotlin:0.3.0")
|
|
49
|
+
implementation("app.independo.inderun:inderun-mlkit-providers:0.3.0")
|
|
50
|
+
implementation("app.independo.inderun:inderun-openai-providers:0.3.0")
|
|
45
51
|
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.10.2")
|
|
46
52
|
|
|
47
53
|
testImplementation("junit:junit:4.13.2")
|
package/android/settings.gradle
CHANGED
|
@@ -4,6 +4,13 @@ pluginManagement {
|
|
|
4
4
|
mavenCentral()
|
|
5
5
|
gradlePluginPortal()
|
|
6
6
|
}
|
|
7
|
+
|
|
8
|
+
// Supplies the version build.gradle.kts deliberately omits, for a standalone build of
|
|
9
|
+
// this directory (pnpm test:android / verify:android). A Capacitor app that includes the
|
|
10
|
+
// module uses its own settings file, so AGP comes from the app's classpath there.
|
|
11
|
+
plugins {
|
|
12
|
+
id("com.android.library") version "9.3.1"
|
|
13
|
+
}
|
|
7
14
|
}
|
|
8
15
|
|
|
9
16
|
dependencyResolutionManagement {
|
|
@@ -87,6 +87,41 @@ class IndeRunCapacitorPlugin : Plugin() {
|
|
|
87
87
|
}
|
|
88
88
|
}
|
|
89
89
|
|
|
90
|
+
/**
|
|
91
|
+
* Reports every registered provider's static declaration and live availability without
|
|
92
|
+
* executing a task. Availability changes between calls — a local model can unload,
|
|
93
|
+
* cloud credentials can expire — so callers must not cache it across a run.
|
|
94
|
+
*/
|
|
95
|
+
@PluginMethod
|
|
96
|
+
fun checkCapabilities(call: PluginCall) {
|
|
97
|
+
scope.launch {
|
|
98
|
+
try {
|
|
99
|
+
val registry = configuredRegistry
|
|
100
|
+
?: throw toIndeRunException(IllegalStateException("Capacitor IndeRun has not been configured. Configure providers before calling checkCapabilities()."))
|
|
101
|
+
// IndeRun is stateless; new per call is intentional — registry is cached after configure().
|
|
102
|
+
val snapshots = IndeRun.initialize(context.applicationContext, registry).checkCapabilities()
|
|
103
|
+
call.resolve(IndeRunSerializer.encodeCapabilitySnapshots(snapshots))
|
|
104
|
+
} catch (error: IndeRunException) {
|
|
105
|
+
val contractError = error.toContractError()
|
|
106
|
+
call.reject(
|
|
107
|
+
contractError.message,
|
|
108
|
+
contractError.errorClass.rawValue,
|
|
109
|
+
null,
|
|
110
|
+
runCatching { IndeRunSerializer.encodeError(contractError) }.getOrNull()
|
|
111
|
+
)
|
|
112
|
+
} catch (error: Throwable) {
|
|
113
|
+
val normalized = toIndeRunException(error)
|
|
114
|
+
val contractError = normalized.toContractError()
|
|
115
|
+
call.reject(
|
|
116
|
+
contractError.message,
|
|
117
|
+
contractError.errorClass.rawValue,
|
|
118
|
+
null,
|
|
119
|
+
runCatching { IndeRunSerializer.encodeError(contractError) }.getOrNull()
|
|
120
|
+
)
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
90
125
|
/**
|
|
91
126
|
* Resolves with the run handle. Only validation and route-selection failures reject;
|
|
92
127
|
* a provider failure, a cancellation, or completion all arrive as the single terminal
|
|
@@ -28,6 +28,9 @@ import app.independo.inderun.contracts.TaskRequestTelemetry
|
|
|
28
28
|
import app.independo.inderun.contracts.TaskResult
|
|
29
29
|
import app.independo.inderun.contracts.TaskResultTelemetry
|
|
30
30
|
import app.independo.inderun.contracts.TelemetryLevel
|
|
31
|
+
import app.independo.inderun.core.ProviderCapabilitySnapshot
|
|
32
|
+
import app.independo.inderun.core.ProviderDescriptor
|
|
33
|
+
import app.independo.inderun.core.ProviderDynamicCapabilities
|
|
31
34
|
import com.getcapacitor.JSArray
|
|
32
35
|
import com.getcapacitor.JSObject
|
|
33
36
|
import org.json.JSONArray
|
|
@@ -113,6 +116,100 @@ object IndeRunSerializer {
|
|
|
113
116
|
}
|
|
114
117
|
}
|
|
115
118
|
|
|
119
|
+
/**
|
|
120
|
+
* Wire encoding for `checkCapabilities()`.
|
|
121
|
+
*
|
|
122
|
+
* Note the enums are written with `.name`, which is the opposite of what
|
|
123
|
+
* [encodeOutcome] and [encodePhase] below have to do. Those contract enums are
|
|
124
|
+
* generated without raw values, so their Kotlin constant names are camelCase and the
|
|
125
|
+
* snake_case wire spelling has to be written by hand. `ProviderDescriptor`'s enums are
|
|
126
|
+
* declared with the wire spelling *as* the constant name (`in_process`,
|
|
127
|
+
* `system_service`), so `.name` already is the wire value — and writing them by hand
|
|
128
|
+
* here would be the thing that eventually drifts.
|
|
129
|
+
*
|
|
130
|
+
* The array is wrapped in a `providers` object because a Capacitor `PluginCall` cannot
|
|
131
|
+
* resolve a top-level array. `IndeRunCapacitor` unwraps it on the JS side.
|
|
132
|
+
*/
|
|
133
|
+
fun encodeCapabilitySnapshots(snapshots: List<ProviderCapabilitySnapshot>): JSObject {
|
|
134
|
+
return JSObject().apply {
|
|
135
|
+
put(
|
|
136
|
+
"providers",
|
|
137
|
+
JSArray().apply {
|
|
138
|
+
snapshots.forEach { snapshot ->
|
|
139
|
+
put(
|
|
140
|
+
JSObject().apply {
|
|
141
|
+
put("providerId", snapshot.providerId)
|
|
142
|
+
put("descriptor", encodeProviderDescriptor(snapshot.descriptor))
|
|
143
|
+
put("capabilities", encodeProviderDynamicCapabilities(snapshot.capabilities))
|
|
144
|
+
}
|
|
145
|
+
)
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
)
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
private fun encodeProviderDescriptor(descriptor: ProviderDescriptor): JSObject {
|
|
153
|
+
return JSObject().apply {
|
|
154
|
+
put("id", descriptor.id)
|
|
155
|
+
put("type", descriptor.type.name)
|
|
156
|
+
put("transport", descriptor.transport.name)
|
|
157
|
+
descriptor.streamingStyle?.let { put("streamingStyle", it.name) }
|
|
158
|
+
put(
|
|
159
|
+
"supports",
|
|
160
|
+
JSObject().apply {
|
|
161
|
+
put("run", descriptor.supports.run)
|
|
162
|
+
put("streaming", descriptor.supports.streaming)
|
|
163
|
+
put("realtime", descriptor.supports.realtime)
|
|
164
|
+
put("tools", descriptor.supports.tools)
|
|
165
|
+
put("reasoningEvents", descriptor.supports.reasoningEvents)
|
|
166
|
+
put("structuredOutput", descriptor.supports.structuredOutput)
|
|
167
|
+
put("multimodal", descriptor.supports.multimodal)
|
|
168
|
+
}
|
|
169
|
+
)
|
|
170
|
+
put("cancel", descriptor.cancel.name)
|
|
171
|
+
put("tasks", JSArray().apply { descriptor.tasks.forEach { put(it) } })
|
|
172
|
+
descriptor.limits?.let { limits ->
|
|
173
|
+
put(
|
|
174
|
+
"limits",
|
|
175
|
+
JSObject().apply {
|
|
176
|
+
limits.maxInputTokens?.let { put("maxInputTokens", it) }
|
|
177
|
+
limits.maxOutputTokens?.let { put("maxOutputTokens", it) }
|
|
178
|
+
limits.maxImageBytes?.let { put("maxImageBytes", it) }
|
|
179
|
+
limits.maxAudioSeconds?.let { put("maxAudioSeconds", it) }
|
|
180
|
+
}
|
|
181
|
+
)
|
|
182
|
+
}
|
|
183
|
+
descriptor.privacy?.let { privacy ->
|
|
184
|
+
put(
|
|
185
|
+
"privacy",
|
|
186
|
+
JSObject().apply {
|
|
187
|
+
put("dataLeavesDevice", privacy.dataLeavesDevice)
|
|
188
|
+
privacy.regions?.let { regions ->
|
|
189
|
+
put("regions", JSArray().apply { regions.forEach { put(it) } })
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
)
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* The three nullable flags are **omitted** rather than written as null. Absence is the
|
|
199
|
+
* contract's "inherit the static declaration" state — `descriptor.supports.streaming`
|
|
200
|
+
* for streaming, `descriptor.cancel != none` for cancellation — and a null would be a
|
|
201
|
+
* third state no consumer has.
|
|
202
|
+
*/
|
|
203
|
+
private fun encodeProviderDynamicCapabilities(capabilities: ProviderDynamicCapabilities): JSObject {
|
|
204
|
+
return JSObject().apply {
|
|
205
|
+
put("available", capabilities.available)
|
|
206
|
+
capabilities.reason?.let { put("reason", it) }
|
|
207
|
+
capabilities.streamingAvailable?.let { put("streamingAvailable", it) }
|
|
208
|
+
capabilities.streamingUnavailableReason?.let { put("streamingUnavailableReason", it) }
|
|
209
|
+
capabilities.cancellationAvailable?.let { put("cancellationAvailable", it) }
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
116
213
|
fun encodeStreamRunHandle(handle: StreamRunHandle): JSObject {
|
|
117
214
|
return JSObject().apply {
|
|
118
215
|
put("schemaVersion", handle.schemaVersion.rawValue)
|