@xybrid/react-native 0.10.0-rc1
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/LICENSE +201 -0
- package/README.md +298 -0
- package/android/build.gradle +61 -0
- package/android/src/main/AndroidManifest.xml +1 -0
- package/android/src/main/java/ai/xybrid/reactnative/XybridCodec.kt +481 -0
- package/android/src/main/java/ai/xybrid/reactnative/XybridHandles.kt +139 -0
- package/android/src/main/java/ai/xybrid/reactnative/XybridModule.kt +665 -0
- package/android/src/main/java/ai/xybrid/reactnative/XybridPackage.kt +27 -0
- package/ios/XybridCodec.swift +508 -0
- package/ios/XybridHandles.swift +191 -0
- package/ios/XybridModule.mm +494 -0
- package/ios/XybridModuleImpl.swift +887 -0
- package/ios/XybridSwift/Xybrid.swift +1401 -0
- package/ios/XybridSwift/xybrid_bolt.swift +3198 -0
- package/ios/xybrid_natives.rb +178 -0
- package/lib/commonjs/NativeXybrid.js +24 -0
- package/lib/commonjs/base64.js +45 -0
- package/lib/commonjs/cancel.js +35 -0
- package/lib/commonjs/context.js +67 -0
- package/lib/commonjs/envelope.js +79 -0
- package/lib/commonjs/errors.js +61 -0
- package/lib/commonjs/index.js +59 -0
- package/lib/commonjs/model.js +307 -0
- package/lib/commonjs/pipeline.js +55 -0
- package/lib/commonjs/presets.js +32 -0
- package/lib/commonjs/session.js +76 -0
- package/lib/commonjs/types.js +10 -0
- package/lib/commonjs/wire.js +285 -0
- package/lib/commonjs/xybrid.js +133 -0
- package/lib/module/NativeXybrid.js +22 -0
- package/lib/module/base64.js +41 -0
- package/lib/module/cancel.js +29 -0
- package/lib/module/context.js +60 -0
- package/lib/module/envelope.js +73 -0
- package/lib/module/errors.js +56 -0
- package/lib/module/index.js +36 -0
- package/lib/module/model.js +298 -0
- package/lib/module/pipeline.js +48 -0
- package/lib/module/presets.js +27 -0
- package/lib/module/session.js +69 -0
- package/lib/module/types.js +9 -0
- package/lib/module/wire.js +271 -0
- package/lib/module/xybrid.js +127 -0
- package/lib/typescript/NativeXybrid.d.ts +68 -0
- package/lib/typescript/NativeXybrid.d.ts.map +1 -0
- package/lib/typescript/base64.d.ts +5 -0
- package/lib/typescript/base64.d.ts.map +1 -0
- package/lib/typescript/cancel.d.ts +24 -0
- package/lib/typescript/cancel.d.ts.map +1 -0
- package/lib/typescript/context.d.ts +40 -0
- package/lib/typescript/context.d.ts.map +1 -0
- package/lib/typescript/envelope.d.ts +51 -0
- package/lib/typescript/envelope.d.ts.map +1 -0
- package/lib/typescript/errors.d.ts +12 -0
- package/lib/typescript/errors.d.ts.map +1 -0
- package/lib/typescript/index.d.ts +28 -0
- package/lib/typescript/index.d.ts.map +1 -0
- package/lib/typescript/model.d.ts +211 -0
- package/lib/typescript/model.d.ts.map +1 -0
- package/lib/typescript/pipeline.d.ts +35 -0
- package/lib/typescript/pipeline.d.ts.map +1 -0
- package/lib/typescript/presets.d.ts +21 -0
- package/lib/typescript/presets.d.ts.map +1 -0
- package/lib/typescript/session.d.ts +50 -0
- package/lib/typescript/session.d.ts.map +1 -0
- package/lib/typescript/types.d.ts +313 -0
- package/lib/typescript/types.d.ts.map +1 -0
- package/lib/typescript/wire.d.ts +130 -0
- package/lib/typescript/wire.d.ts.map +1 -0
- package/lib/typescript/xybrid.d.ts +89 -0
- package/lib/typescript/xybrid.d.ts.map +1 -0
- package/package.json +88 -0
- package/react-native-xybrid.podspec +50 -0
- package/src/NativeXybrid.ts +131 -0
- package/src/base64.ts +43 -0
- package/src/cancel.ts +47 -0
- package/src/context.ts +70 -0
- package/src/envelope.ts +115 -0
- package/src/errors.ts +67 -0
- package/src/index.ts +91 -0
- package/src/model.ts +408 -0
- package/src/pipeline.ts +66 -0
- package/src/presets.ts +36 -0
- package/src/session.ts +76 -0
- package/src/types.ts +362 -0
- package/src/wire.ts +408 -0
- package/src/xybrid.ts +176 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to the Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2026 Xybrid, Inc.
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,298 @@
|
|
|
1
|
+
# @xybrid/react-native
|
|
2
|
+
|
|
3
|
+
On-device AI for React Native — LLMs, speech recognition, text-to-speech,
|
|
4
|
+
embeddings and vision — backed by the same Rust SDK as Xybrid's Swift, Kotlin,
|
|
5
|
+
Flutter and Unity bindings. Runs offline with no account; add an API key to
|
|
6
|
+
light up cloud fallback and telemetry.
|
|
7
|
+
|
|
8
|
+
```ts
|
|
9
|
+
import { Envelope, ModelLoader } from '@xybrid/react-native';
|
|
10
|
+
|
|
11
|
+
const model = await ModelLoader.fromRegistry('qwen3.5-0.8b').load();
|
|
12
|
+
const result = await model.run(Envelope.text('Write a haiku about the sea.'));
|
|
13
|
+
console.log(result.text);
|
|
14
|
+
await model.release();
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
> **Status: preview.** The API mirrors the Swift and Kotlin SDKs and is
|
|
18
|
+
> covered by tests on every change, but it may still move before 1.0.
|
|
19
|
+
|
|
20
|
+
## Requirements
|
|
21
|
+
|
|
22
|
+
| | |
|
|
23
|
+
|---|---|
|
|
24
|
+
| React Native | 0.76 or newer, **New Architecture** (the default since 0.76; the only option since 0.82) |
|
|
25
|
+
| iOS | 16.0+. Simulator builds need an Apple Silicon Mac (there is no x86_64 simulator slice) |
|
|
26
|
+
| Android | API 24+, `arm64-v8a`, `armeabi-v7a`, `x86_64` |
|
|
27
|
+
| Expo | SDK 52+ with a [development build](https://docs.expo.dev/develop/development-builds/introduction/) — not Expo Go |
|
|
28
|
+
|
|
29
|
+
## Install
|
|
30
|
+
|
|
31
|
+
```sh
|
|
32
|
+
npm install @xybrid/react-native
|
|
33
|
+
cd ios && pod install
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
- **iOS** — `pod install` downloads the Rust core (`XybridFFI.xcframework`)
|
|
37
|
+
from this version's GitHub Release, checks it against the SHA-256 pinned in
|
|
38
|
+
the package, and caches it in `~/.xybrid/cache/xcframework`. Behind a proxy or
|
|
39
|
+
offline, point `XYBRID_NATIVES_BASE_URL` at a mirror, or
|
|
40
|
+
`XYBRID_XCFRAMEWORK_PATH` at a local copy (directory or `.zip`). If your app
|
|
41
|
+
targets iOS below 16.0, raise it in the Podfile (`platform :ios, '16.0'`).
|
|
42
|
+
- **Android** — nothing to do: Gradle pulls `ai.xybrid:xybrid-kotlin` (the
|
|
43
|
+
Kotlin SDK with the native libraries) from Maven Central.
|
|
44
|
+
- **Expo** — `npx expo install @xybrid/react-native`, then
|
|
45
|
+
`npx expo prebuild` / `npx expo run:ios|android`. No config plugin needed.
|
|
46
|
+
|
|
47
|
+
## Quick tour
|
|
48
|
+
|
|
49
|
+
### Configure (optional)
|
|
50
|
+
|
|
51
|
+
Local inference needs no setup. Call `initialize` first only when you have
|
|
52
|
+
options — an API key enables the platform features on top of the same local
|
|
53
|
+
runtime: cloud fallback, speculative cloud serving and dashboard telemetry.
|
|
54
|
+
|
|
55
|
+
```ts
|
|
56
|
+
import { Xybrid } from '@xybrid/react-native';
|
|
57
|
+
|
|
58
|
+
await Xybrid.initialize({ apiKey: XYBRID_API_KEY });
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
Options apply once per app process; calling again with different options
|
|
62
|
+
rejects with `xybrid_config_error` (restart the app to change them).
|
|
63
|
+
|
|
64
|
+
### Load and run
|
|
65
|
+
|
|
66
|
+
```ts
|
|
67
|
+
import { Envelope, GenerationConfigs, ModelLoader } from '@xybrid/react-native';
|
|
68
|
+
|
|
69
|
+
const model = await ModelLoader.fromRegistry('qwen3.5-0.8b').load();
|
|
70
|
+
await model.warmup(); // optional: pay cold start now, not on the first run
|
|
71
|
+
|
|
72
|
+
const result = await model.run(Envelope.text('Summarise: …'), {
|
|
73
|
+
generationConfig: GenerationConfigs.greedy({ maxTokens: 128 }),
|
|
74
|
+
});
|
|
75
|
+
result.text; // the answer
|
|
76
|
+
result.reasoningContent; // <think> text, if the model emitted any
|
|
77
|
+
result.metrics; // totalMs, ttftMs, tokensPerSecond, …
|
|
78
|
+
result.executionTarget; // 'local' or 'cloud'
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
Other sources: `ModelLoader.fromBundle(path)`, `fromDirectory(path)`,
|
|
82
|
+
`fromHuggingFace('org/repo', { revision })`, `fromModelFile('model.gguf')`
|
|
83
|
+
(`file://` URLs work too). `model.info()` returns the model id, output type
|
|
84
|
+
and capabilities; `model.unload()` frees the weights but keeps the handle;
|
|
85
|
+
`model.release()` frees everything.
|
|
86
|
+
|
|
87
|
+
### Stream tokens, with a stop button
|
|
88
|
+
|
|
89
|
+
```ts
|
|
90
|
+
const controller = new AbortController();
|
|
91
|
+
|
|
92
|
+
for await (const token of model.runStreaming(Envelope.text('Tell me a story'), {
|
|
93
|
+
signal: controller.signal,
|
|
94
|
+
})) {
|
|
95
|
+
setText((previous) => previous + token.token);
|
|
96
|
+
}
|
|
97
|
+
// Elsewhere (a Stop button, unmount): controller.abort();
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
Generation stops natively when the loop ends — completion, `break`, an
|
|
101
|
+
error, or `abort()`. An aborted run rejects with `xybrid_cancelled`. A loop that
|
|
102
|
+
is simply abandoned is never cleaned up (JavaScript runs no `finally` on
|
|
103
|
+
garbage collection), so abort on unmount. The final `InferenceResult` is the
|
|
104
|
+
generator's return value.
|
|
105
|
+
|
|
106
|
+
### Conversations
|
|
107
|
+
|
|
108
|
+
```ts
|
|
109
|
+
import { ConversationContext, Envelope } from '@xybrid/react-native';
|
|
110
|
+
|
|
111
|
+
const chat = await ConversationContext.create();
|
|
112
|
+
await chat.setSystem('You are concise.');
|
|
113
|
+
|
|
114
|
+
const question = Envelope.user('What is the capital of France?');
|
|
115
|
+
const reply = await model.run(question, { context: chat });
|
|
116
|
+
await chat.push(question); // the run never changes the context:
|
|
117
|
+
await chat.push(reply.envelope); // push both turns yourself, after the run
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
### Structured output
|
|
121
|
+
|
|
122
|
+
```ts
|
|
123
|
+
import { jsonSchemaToGbnf } from '@xybrid/react-native';
|
|
124
|
+
|
|
125
|
+
const grammar = await jsonSchemaToGbnf({
|
|
126
|
+
type: 'object',
|
|
127
|
+
properties: { name: { type: 'string' }, total: { type: 'number' } },
|
|
128
|
+
required: ['name', 'total'],
|
|
129
|
+
});
|
|
130
|
+
const result = await model.run(Envelope.text('Extract: 2x espresso, 8.40 EUR'), {
|
|
131
|
+
generationConfig: { grammar, maxTokens: 128 },
|
|
132
|
+
});
|
|
133
|
+
JSON.parse(result.text!);
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
### Tool calling
|
|
137
|
+
|
|
138
|
+
One `run` is one model turn; the loop is yours.
|
|
139
|
+
|
|
140
|
+
```ts
|
|
141
|
+
const tools = [{ name: 'weather', description: 'Current weather', parameters: { type: 'object', properties: { city: { type: 'string' } } } }];
|
|
142
|
+
const prompt = 'Weather in Paris?';
|
|
143
|
+
|
|
144
|
+
const first = await model.run(Envelope.text(prompt), { generationConfig: { tools } });
|
|
145
|
+
if (first.toolCalls.length > 0) {
|
|
146
|
+
const results = await Promise.all(first.toolCalls.map(async (call) => ({
|
|
147
|
+
callId: call.id,
|
|
148
|
+
name: call.name,
|
|
149
|
+
content: await runTool(call.name, JSON.parse(call.argumentsJson)),
|
|
150
|
+
})));
|
|
151
|
+
const next = await Envelope.toolResults(prompt, first.text ?? '', results);
|
|
152
|
+
const answer = await model.run(next, { generationConfig: { tools } });
|
|
153
|
+
}
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
When streaming, the terminal token carries `toolCalls` and `rawText` (use
|
|
157
|
+
`rawText` as `priorAssistantText`).
|
|
158
|
+
|
|
159
|
+
### Vision
|
|
160
|
+
|
|
161
|
+
```ts
|
|
162
|
+
const message = Envelope.userMessage('What is in this photo?', [
|
|
163
|
+
Envelope.image(photoBase64, 'jpeg'),
|
|
164
|
+
]);
|
|
165
|
+
const result = await visionModel.run(message);
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
### Text-to-speech
|
|
169
|
+
|
|
170
|
+
```ts
|
|
171
|
+
const tts = await ModelLoader.fromRegistry('kokoro-82m').load();
|
|
172
|
+
const voices = await tts.voices();
|
|
173
|
+
const speech = await tts.run(Envelope.text('Hello!', { voiceId: voices[0].id, speed: 1.1 }));
|
|
174
|
+
speech.audioBytesBase64; // WAV/PCM, base64
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
### Speech recognition
|
|
178
|
+
|
|
179
|
+
Batch — transcribe a recording:
|
|
180
|
+
|
|
181
|
+
```ts
|
|
182
|
+
const asr = await ModelLoader.fromRegistry('whisper-tiny-ggml').load();
|
|
183
|
+
const { text } = await asr.run(Envelope.audio(wavBase64)); // 16 kHz mono by default
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
Live — feed microphone audio as it arrives (Float32, mono, 16 kHz):
|
|
187
|
+
|
|
188
|
+
```ts
|
|
189
|
+
const session = await asr.stream({ language: 'en' });
|
|
190
|
+
(async () => {
|
|
191
|
+
for await (const partial of session.partials()) setCaption(partial.text);
|
|
192
|
+
})();
|
|
193
|
+
await session.feed(float32Samples); // from your audio callback
|
|
194
|
+
const transcript = await session.flush(); // when the user stops talking
|
|
195
|
+
await session.release();
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
### Pipelines
|
|
199
|
+
|
|
200
|
+
```ts
|
|
201
|
+
import { Pipeline } from '@xybrid/react-native';
|
|
202
|
+
|
|
203
|
+
const pipeline = await Pipeline.fromFile(`${documentDirectory}voice-assistant.yaml`);
|
|
204
|
+
const result = await pipeline.run(Envelope.audio(recordingBase64));
|
|
205
|
+
const heard = result.stages.find((stage) => stage.stageId === 'asr')?.text;
|
|
206
|
+
play(result.audioBytesBase64);
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
### Downloads, speculative cloud, cache and memory
|
|
210
|
+
|
|
211
|
+
```ts
|
|
212
|
+
// Download with a progress bar, then load instantly from cache.
|
|
213
|
+
const loader = ModelLoader.fromRegistry('qwen3.5-0.8b');
|
|
214
|
+
const download = await loader.download();
|
|
215
|
+
for await (const status of download!.progress()) setProgress(status.progress);
|
|
216
|
+
const model = await loader.load();
|
|
217
|
+
|
|
218
|
+
// Answer from the cloud while the weights download (needs an API key).
|
|
219
|
+
const fast = await ModelLoader.fromRegistrySpeculative('qwen3.5-0.8b').load();
|
|
220
|
+
await fast.isCloudServing(); // true until the local weights land
|
|
221
|
+
|
|
222
|
+
await Xybrid.modelCacheStatus(); // bytes and models on disk
|
|
223
|
+
await Xybrid.removeCachedModel('qwen3.5-0.8b');
|
|
224
|
+
Xybrid.releaseMemoryOnWarning(); // free idle models on OS memory warnings
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
### Errors
|
|
228
|
+
|
|
229
|
+
Every rejection has a stable `code` (`XybridErrorCodes` lists them):
|
|
230
|
+
|
|
231
|
+
```ts
|
|
232
|
+
import { isRetryable, isXybridError } from '@xybrid/react-native';
|
|
233
|
+
|
|
234
|
+
try {
|
|
235
|
+
await model.run(envelope);
|
|
236
|
+
} catch (error) {
|
|
237
|
+
if (isXybridError(error) && error.code === 'xybrid_model_not_found') { /* … */ }
|
|
238
|
+
if (isRetryable(error)) { /* network, rate limit, timeout, offline */ }
|
|
239
|
+
}
|
|
240
|
+
```
|
|
241
|
+
|
|
242
|
+
`xybrid_handle` means the object was released; `xybrid_invalid_argument`
|
|
243
|
+
means a malformed argument (bad envelope, options, base64).
|
|
244
|
+
|
|
245
|
+
## How it works
|
|
246
|
+
|
|
247
|
+
```
|
|
248
|
+
JS / TS (src/) ── TurboModule (src/NativeXybrid.ts, Codegen)
|
|
249
|
+
├── iOS ios/XybridModule.mm → XybridModuleImpl.swift → Swift SDK (Xybrid.swift) ─┐
|
|
250
|
+
└── Android XybridModule.kt → Kotlin SDK (ai.xybrid:xybrid-kotlin) ─┤
|
|
251
|
+
xybrid-bolt (BoltFFI)
|
|
252
|
+
→ xybrid-ffi-facade → xybrid-sdk → xybrid-core
|
|
253
|
+
```
|
|
254
|
+
|
|
255
|
+
- Native objects (models, contexts, streams, sessions…) are opaque handles in
|
|
256
|
+
one registry per platform; `release()` frees them, and a model's release
|
|
257
|
+
also stops the streams started from it.
|
|
258
|
+
- Envelopes cross as a payload plus string metadata, exactly like the Rust
|
|
259
|
+
type; convenience fields (`voiceId`, `sampleRate`, `role`…) are folded in on
|
|
260
|
+
the JS side (`src/wire.ts`), so both native shims stay generic.
|
|
261
|
+
- Binary payloads (audio, images) cross as base64 strings: TurboModules have no
|
|
262
|
+
zero-copy buffer type. Fine for prompts, clips and TTS output; a JSI buffer
|
|
263
|
+
path is on the roadmap for high-rate audio.
|
|
264
|
+
|
|
265
|
+
## Developing in this repo
|
|
266
|
+
|
|
267
|
+
```sh
|
|
268
|
+
npm install && npm test # types, unit tests, spec conformance, SDK parity
|
|
269
|
+
npm run test:ios # Swift type-check + codec tests (macOS, Xcode)
|
|
270
|
+
npm run test:android # Kotlin codec tests on the JVM (kotlinc + java)
|
|
271
|
+
```
|
|
272
|
+
|
|
273
|
+
Run the example app (`example/`) against the working tree:
|
|
274
|
+
|
|
275
|
+
```sh
|
|
276
|
+
# iOS: build the Rust core; `pod install` picks up bazel-bin automatically.
|
|
277
|
+
bazel build --config=ios //bindings/apple:XybridFFI
|
|
278
|
+
cd example && npm install && npx expo run:ios
|
|
279
|
+
|
|
280
|
+
# Android: build the AAR, publish the Kotlin SDK to mavenLocal (the example
|
|
281
|
+
# prefers mavenLocal), then run.
|
|
282
|
+
bazel build -c opt --config=remote //bindings/kotlin:xybrid_kotlin_aar
|
|
283
|
+
# stage its jni/* into bindings/kotlin/libs/ — see bindings/kotlin/README.md
|
|
284
|
+
(cd ../kotlin && ./gradlew publishToMavenLocal)
|
|
285
|
+
cd example && npm install && npx expo run:android
|
|
286
|
+
```
|
|
287
|
+
|
|
288
|
+
The example runs a smoke test on launch and logs each step with a
|
|
289
|
+
`[xybrid-smoke]` prefix.
|
|
290
|
+
|
|
291
|
+
**Adding SDK surface.** `tests/parity.test.mjs` reads every export of
|
|
292
|
+
`crates/xybrid-bolt/src/lib.rs` and fails until each new function, method,
|
|
293
|
+
field, enum variant or error variant is mapped in `parity.json` (or excluded
|
|
294
|
+
with a reason). To map one: add the method to `src/NativeXybrid.ts`, implement
|
|
295
|
+
it in `ios/XybridModuleImpl.swift` (+ its forwarder in `ios/XybridModule.mm`)
|
|
296
|
+
and `android/.../XybridModule.kt`, expose it in `src/`, and record it in
|
|
297
|
+
`parity.json`. `tests/spec-conformance.test.mjs` checks the iOS selectors and
|
|
298
|
+
Kotlin overrides against the spec.
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
// The Android half of @xybrid/react-native: a TurboModule (XybridModule.kt)
|
|
2
|
+
// over the Kotlin SDK. The SDK — Kotlin classes plus libxybrid_bolt.so and the
|
|
3
|
+
// ONNX Runtime — comes from the `ai.xybrid:xybrid-kotlin` AAR on Maven
|
|
4
|
+
// Central, so nothing native is built or staged here.
|
|
5
|
+
|
|
6
|
+
buildscript {
|
|
7
|
+
ext.xybridExt = { name, fallback ->
|
|
8
|
+
rootProject.ext.has(name) ? rootProject.ext.get(name) : fallback
|
|
9
|
+
}
|
|
10
|
+
repositories {
|
|
11
|
+
google()
|
|
12
|
+
mavenCentral()
|
|
13
|
+
}
|
|
14
|
+
dependencies {
|
|
15
|
+
// The host app's versions win when it already has these on its classpath.
|
|
16
|
+
classpath "com.android.tools.build:gradle:8.7.3"
|
|
17
|
+
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:${xybridExt('kotlinVersion', '2.0.21')}"
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
apply plugin: "com.android.library"
|
|
22
|
+
apply plugin: "kotlin-android"
|
|
23
|
+
// Runs Codegen for this library's spec (`codegenConfig` in package.json):
|
|
24
|
+
// generates the `NativeXybridSpec` base class XybridModule extends, and the
|
|
25
|
+
// `react_codegen_RNXybridSpec` JNI target autolinking wires into the app.
|
|
26
|
+
// The host app's settings.gradle makes the plugin available.
|
|
27
|
+
apply plugin: "com.facebook.react"
|
|
28
|
+
|
|
29
|
+
android {
|
|
30
|
+
namespace "ai.xybrid.reactnative"
|
|
31
|
+
compileSdkVersion xybridExt("compileSdkVersion", 35)
|
|
32
|
+
|
|
33
|
+
defaultConfig {
|
|
34
|
+
minSdkVersion xybridExt("minSdkVersion", 24)
|
|
35
|
+
targetSdkVersion xybridExt("targetSdkVersion", 35)
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
compileOptions {
|
|
39
|
+
sourceCompatibility JavaVersion.VERSION_17
|
|
40
|
+
targetCompatibility JavaVersion.VERSION_17
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
kotlinOptions {
|
|
44
|
+
jvmTarget = "17"
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
repositories {
|
|
49
|
+
google()
|
|
50
|
+
mavenCentral()
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
dependencies {
|
|
54
|
+
// Versioned by the host app's React Native Gradle plugin.
|
|
55
|
+
implementation "com.facebook.react:react-android"
|
|
56
|
+
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.3"
|
|
57
|
+
|
|
58
|
+
// The Kotlin SDK. Kept in lockstep with the workspace version by
|
|
59
|
+
// tools/scripts/version-sync.sh (`just bump-version`).
|
|
60
|
+
implementation "ai.xybrid:xybrid-kotlin:0.10.0-rc1"
|
|
61
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
<manifest xmlns:android="http://schemas.android.com/apk/res/android" />
|