react-native-litert-lm 0.2.2 → 0.3.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.
Files changed (35) hide show
  1. package/README.md +270 -186
  2. package/android/build.gradle +1 -1
  3. package/android/src/main/java/com/margelo/nitro/dev/litert/litertlm/HybridLiteRTLM.kt +93 -37
  4. package/app.plugin.js +33 -0
  5. package/cpp/HybridLiteRTLM.cpp +571 -451
  6. package/cpp/HybridLiteRTLM.hpp +54 -23
  7. package/cpp/IOSDownloadHelper.h +24 -0
  8. package/cpp/cpp-adapter.cpp +2 -2
  9. package/cpp/include/litert_lm_engine.h +502 -0
  10. package/ios/IOSDownloadHelper.mm +129 -0
  11. package/ios/LiteRTLMAutolinking.mm +30 -0
  12. package/lib/hooks.d.ts +9 -4
  13. package/lib/hooks.js +34 -20
  14. package/lib/index.d.ts +1 -0
  15. package/lib/index.js +2 -5
  16. package/lib/memoryTracker.d.ts +1 -1
  17. package/lib/memoryTracker.js +1 -1
  18. package/lib/modelFactory.d.ts +11 -5
  19. package/lib/modelFactory.js +9 -4
  20. package/nitrogen/generated/android/LiteRTLMOnLoad.cpp +11 -4
  21. package/nitrogen/generated/android/c++/JHybridLiteRTLMSpec.cpp +31 -37
  22. package/nitrogen/generated/android/c++/JHybridLiteRTLMSpec.hpp +19 -22
  23. package/nitrogen/generated/android/kotlin/com/margelo/nitro/dev/litert/litertlm/HybridLiteRTLMSpec.kt +15 -18
  24. package/package.json +12 -5
  25. package/react-native-litert-lm.podspec +20 -7
  26. package/scripts/build-ios-engine.sh +283 -0
  27. package/scripts/download-ios-frameworks.sh +72 -0
  28. package/scripts/postinstall.js +116 -0
  29. package/scripts/stubs/cxx_bridge_stubs.cc +224 -0
  30. package/scripts/stubs/gemma_model_constraint_provider.cc +46 -0
  31. package/scripts/stubs/llguidance_stubs.c +101 -0
  32. package/src/hooks.ts +62 -39
  33. package/src/index.ts +4 -7
  34. package/src/memoryTracker.ts +1 -1
  35. package/src/modelFactory.ts +30 -5
package/src/index.ts CHANGED
@@ -28,7 +28,7 @@ export {
28
28
  applyLlamaTemplate,
29
29
  } from "./templates";
30
30
 
31
- // Re-export memory tracking utilities (uses NitroModules.createNativeArrayBuffer v0.34+)
31
+ // Re-export memory tracking utilities (uses NitroModules.createNativeArrayBuffer v0.35+)
32
32
  export type {
33
33
  MemorySnapshot,
34
34
  MemoryTracker,
@@ -36,6 +36,7 @@ export type {
36
36
  } from "./memoryTracker";
37
37
  export { createMemoryTracker, createNativeBuffer } from "./memoryTracker";
38
38
 
39
+ export type { LiteRTLMInstance } from "./modelFactory";
39
40
  export * from "./hooks";
40
41
 
41
42
  /**
@@ -131,14 +132,10 @@ export function checkBackendSupport(backend: Backend): string | undefined {
131
132
  return "NPU backend requires compatible hardware (Qualcomm Hexagon, MediaTek APU, etc.). Will fall back to GPU if unavailable.";
132
133
  }
133
134
  if (Platform.OS === "ios") {
134
- return "NPU/CoreML is not yet supported on iOS. LiteRT-LM iOS support is pending.";
135
+ return "NPU (Neural Engine) is not yet supported on iOS. Use 'gpu' (Metal) or 'cpu' instead.";
135
136
  }
136
137
  }
137
138
 
138
- if (Platform.OS === "ios" && backend !== "cpu") {
139
- return "LiteRT-LM iOS is not yet released. Only CPU backend may work via fallback.";
140
- }
141
-
142
139
  return undefined;
143
140
  }
144
141
 
@@ -161,7 +158,7 @@ export function checkBackendSupport(backend: Backend): string | undefined {
161
158
  */
162
159
  export function checkMultimodalSupport(): string | undefined {
163
160
  if (Platform.OS === "ios") {
164
- return "Multimodal (image/audio) is not yet supported on iOS. LiteRT-LM iOS SDK is pending.";
161
+ return "Multimodal (image/audio) is experimental on iOS. Vision and audio executors may not be available in the current build.";
165
162
  }
166
163
  return undefined;
167
164
  }
@@ -3,7 +3,7 @@
3
3
  *
4
4
  * Records real memory usage from OS-level APIs via `getMemoryUsage()`,
5
5
  * and stores snapshots in a native-backed ArrayBuffer allocated via
6
- * `NitroModules.createNativeArrayBuffer()` (v0.34+) for zero-copy interop.
6
+ * `NitroModules.createNativeArrayBuffer()` (v0.35+) for zero-copy interop.
7
7
  *
8
8
  * @example
9
9
  * ```typescript
@@ -2,11 +2,24 @@ import { NitroModules } from "react-native-nitro-modules";
2
2
  import { LiteRTLM, LLMConfig } from "./specs/LiteRTLM.nitro";
3
3
  import { createMemoryTracker, MemoryTracker } from "./memoryTracker";
4
4
 
5
+ /**
6
+ * Extended LiteRT-LM instance with optional memory tracking and
7
+ * augmented loadModel that accepts a download progress callback.
8
+ */
9
+ export type LiteRTLMInstance = Omit<LiteRTLM, "loadModel"> & {
10
+ memoryTracker?: MemoryTracker;
11
+ loadModel: (
12
+ pathOrUrl: string,
13
+ config?: LLMConfig,
14
+ onDownloadProgress?: (progress: number) => void,
15
+ ) => Promise<void>;
16
+ };
17
+
5
18
  /**
6
19
  * Creates a new LiteRT-LM inference engine instance.
7
20
  *
8
21
  * Optionally creates a native-backed memory tracker using
9
- * `NitroModules.createNativeArrayBuffer()` (v0.34+) for efficient
22
+ * `NitroModules.createNativeArrayBuffer()` (v0.35+) for efficient
10
23
  * zero-copy memory usage tracking.
11
24
  *
12
25
  * @param options.enableMemoryTracking Enable automatic memory tracking (default: false)
@@ -15,7 +28,7 @@ import { createMemoryTracker, MemoryTracker } from "./memoryTracker";
15
28
  export function createLLM(options?: {
16
29
  enableMemoryTracking?: boolean;
17
30
  maxMemorySnapshots?: number;
18
- }): LiteRTLM & { memoryTracker?: MemoryTracker } {
31
+ }): LiteRTLMInstance {
19
32
  const native = NitroModules.createHybridObject<LiteRTLM>("LiteRTLM");
20
33
 
21
34
  const enableTracking = options?.enableMemoryTracking ?? false;
@@ -44,11 +57,23 @@ export function createLLM(options?: {
44
57
  return {
45
58
  ...native,
46
59
  memoryTracker: tracker,
47
- loadModel: async (pathOrUrl: string, config?: LLMConfig) => {
60
+ loadModel: async (
61
+ pathOrUrl: string,
62
+ config?: LLMConfig,
63
+ onDownloadProgress?: (progress: number) => void,
64
+ ) => {
48
65
  let modelPath = pathOrUrl;
49
66
 
50
- // Check if it's a URL
67
+ // Check if it's a URL — enforce HTTPS for model downloads
51
68
  if (pathOrUrl.startsWith("http://") || pathOrUrl.startsWith("https://")) {
69
+ if (pathOrUrl.startsWith("http://")) {
70
+ throw new Error(
71
+ "Insecure HTTP URLs are not allowed for model downloads. " +
72
+ "Use HTTPS instead: " +
73
+ pathOrUrl.replace("http://", "https://"),
74
+ );
75
+ }
76
+
52
77
  // Extract filename from URL
53
78
  const fileName = pathOrUrl.split("/").pop();
54
79
  if (!fileName) {
@@ -60,7 +85,7 @@ export function createLLM(options?: {
60
85
  pathOrUrl,
61
86
  fileName,
62
87
  (progress) => {
63
- console.log(`Download progress: ${progress}`);
88
+ onDownloadProgress?.(progress);
64
89
  },
65
90
  );
66
91
  console.log(`Model downloaded to: ${modelPath}`);