cactus-react-native 1.2.1 → 1.4.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 (214) hide show
  1. package/README.md +555 -8
  2. package/android/CMakeLists.txt +4 -3
  3. package/android/src/main/java/com/margelo/nitro/cactus/HybridCactusFileSystem.kt +20 -1
  4. package/android/src/main/jniLibs/arm64-v8a/libcactus.a +0 -0
  5. package/android/src/main/jniLibs/arm64-v8a/libcactus_util.a +0 -0
  6. package/cpp/HybridCactus.cpp +112 -19
  7. package/cpp/HybridCactus.hpp +12 -3
  8. package/cpp/HybridCactusIndex.cpp +325 -0
  9. package/cpp/HybridCactusIndex.hpp +43 -0
  10. package/cpp/HybridCactusUtil.cpp +3 -3
  11. package/cpp/HybridCactusUtil.hpp +2 -1
  12. package/cpp/cactus_ffi.h +83 -2
  13. package/cpp/cactus_util.h +1 -1
  14. package/ios/HybridCactusFileSystem.swift +23 -2
  15. package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/cactus.h +2 -0
  16. package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/cactus_ffi.h +83 -2
  17. package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/cactus_telemetry.h +656 -0
  18. package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/{ffi_utils.h → cactus_utils.h} +104 -17
  19. package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/engine.h +117 -7
  20. package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/graph.h +91 -5
  21. package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/kernel.h +15 -6
  22. package/ios/cactus.xcframework/ios-arm64/cactus.framework/Info.plist +0 -0
  23. package/ios/cactus.xcframework/ios-arm64/cactus.framework/cactus +0 -0
  24. package/ios/cactus.xcframework/ios-arm64-simulator/cactus.framework/Headers/cactus.h +2 -0
  25. package/ios/cactus.xcframework/ios-arm64-simulator/cactus.framework/Headers/cactus_ffi.h +83 -2
  26. package/ios/cactus.xcframework/ios-arm64-simulator/cactus.framework/Headers/cactus_telemetry.h +656 -0
  27. package/ios/cactus.xcframework/ios-arm64-simulator/cactus.framework/Headers/{ffi_utils.h → cactus_utils.h} +104 -17
  28. package/ios/cactus.xcframework/ios-arm64-simulator/cactus.framework/Headers/engine.h +117 -7
  29. package/ios/cactus.xcframework/ios-arm64-simulator/cactus.framework/Headers/graph.h +91 -5
  30. package/ios/cactus.xcframework/ios-arm64-simulator/cactus.framework/Headers/kernel.h +15 -6
  31. package/ios/cactus.xcframework/ios-arm64-simulator/cactus.framework/Info.plist +0 -0
  32. package/ios/cactus.xcframework/ios-arm64-simulator/cactus.framework/_CodeSignature/CodeResources +1 -1
  33. package/ios/cactus.xcframework/ios-arm64-simulator/cactus.framework/cactus +0 -0
  34. package/ios/cactus_util.xcframework/Info.plist +4 -4
  35. package/ios/cactus_util.xcframework/ios-arm64/cactus_util.framework/Headers/cactus_util.h +1 -1
  36. package/ios/cactus_util.xcframework/ios-arm64/cactus_util.framework/Headers/database.h +27 -0
  37. package/ios/cactus_util.xcframework/ios-arm64/cactus_util.framework/Info.plist +0 -0
  38. package/ios/cactus_util.xcframework/ios-arm64/cactus_util.framework/cactus_util +0 -0
  39. package/ios/cactus_util.xcframework/ios-arm64-simulator/cactus_util.framework/Headers/cactus_util.h +1 -1
  40. package/ios/cactus_util.xcframework/ios-arm64-simulator/cactus_util.framework/Headers/database.h +27 -0
  41. package/ios/cactus_util.xcframework/ios-arm64-simulator/cactus_util.framework/Info.plist +0 -0
  42. package/ios/cactus_util.xcframework/ios-arm64-simulator/cactus_util.framework/_CodeSignature/CodeResources +3 -3
  43. package/ios/cactus_util.xcframework/ios-arm64-simulator/cactus_util.framework/cactus_util +0 -0
  44. package/lib/module/api/Database.js +12 -3
  45. package/lib/module/api/Database.js.map +1 -1
  46. package/lib/module/classes/CactusIndex.js +45 -0
  47. package/lib/module/classes/CactusIndex.js.map +1 -0
  48. package/lib/module/classes/CactusLM.js +35 -5
  49. package/lib/module/classes/CactusLM.js.map +1 -1
  50. package/lib/module/classes/CactusSTT.js +17 -5
  51. package/lib/module/classes/CactusSTT.js.map +1 -1
  52. package/lib/module/config/CactusConfig.js +2 -0
  53. package/lib/module/config/CactusConfig.js.map +1 -1
  54. package/lib/module/constants/packageVersion.js +1 -1
  55. package/lib/module/hooks/useCactusIndex.js +175 -0
  56. package/lib/module/hooks/useCactusIndex.js.map +1 -0
  57. package/lib/module/hooks/useCactusLM.js +54 -2
  58. package/lib/module/hooks/useCactusLM.js.map +1 -1
  59. package/lib/module/hooks/useCactusSTT.js +2 -2
  60. package/lib/module/hooks/useCactusSTT.js.map +1 -1
  61. package/lib/module/index.js +2 -0
  62. package/lib/module/index.js.map +1 -1
  63. package/lib/module/native/Cactus.js +24 -13
  64. package/lib/module/native/Cactus.js.map +1 -1
  65. package/lib/module/native/CactusFileSystem.js +3 -0
  66. package/lib/module/native/CactusFileSystem.js.map +1 -1
  67. package/lib/module/native/CactusIndex.js +32 -0
  68. package/lib/module/native/CactusIndex.js.map +1 -0
  69. package/lib/module/native/CactusUtil.js +16 -3
  70. package/lib/module/native/CactusUtil.js.map +1 -1
  71. package/lib/module/native/index.js +1 -0
  72. package/lib/module/native/index.js.map +1 -1
  73. package/lib/module/specs/CactusIndex.nitro.js +4 -0
  74. package/lib/module/specs/CactusIndex.nitro.js.map +1 -0
  75. package/lib/module/telemetry/Telemetry.js +3 -1
  76. package/lib/module/telemetry/Telemetry.js.map +1 -1
  77. package/lib/module/types/CactusIndex.js +2 -0
  78. package/lib/module/types/CactusIndex.js.map +1 -0
  79. package/lib/typescript/src/api/Database.d.ts +4 -1
  80. package/lib/typescript/src/api/Database.d.ts.map +1 -1
  81. package/lib/typescript/src/classes/CactusIndex.d.ts +15 -0
  82. package/lib/typescript/src/classes/CactusIndex.d.ts.map +1 -0
  83. package/lib/typescript/src/classes/CactusLM.d.ts +5 -2
  84. package/lib/typescript/src/classes/CactusLM.d.ts.map +1 -1
  85. package/lib/typescript/src/classes/CactusSTT.d.ts +2 -1
  86. package/lib/typescript/src/classes/CactusSTT.d.ts.map +1 -1
  87. package/lib/typescript/src/config/CactusConfig.d.ts +1 -0
  88. package/lib/typescript/src/config/CactusConfig.d.ts.map +1 -1
  89. package/lib/typescript/src/constants/packageVersion.d.ts +1 -1
  90. package/lib/typescript/src/hooks/useCactusIndex.d.ts +14 -0
  91. package/lib/typescript/src/hooks/useCactusIndex.d.ts.map +1 -0
  92. package/lib/typescript/src/hooks/useCactusLM.d.ts +4 -2
  93. package/lib/typescript/src/hooks/useCactusLM.d.ts.map +1 -1
  94. package/lib/typescript/src/hooks/useCactusSTT.d.ts +1 -1
  95. package/lib/typescript/src/hooks/useCactusSTT.d.ts.map +1 -1
  96. package/lib/typescript/src/index.d.ts +4 -1
  97. package/lib/typescript/src/index.d.ts.map +1 -1
  98. package/lib/typescript/src/native/Cactus.d.ts +4 -2
  99. package/lib/typescript/src/native/Cactus.d.ts.map +1 -1
  100. package/lib/typescript/src/native/CactusFileSystem.d.ts +1 -0
  101. package/lib/typescript/src/native/CactusFileSystem.d.ts.map +1 -1
  102. package/lib/typescript/src/native/CactusIndex.d.ts +12 -0
  103. package/lib/typescript/src/native/CactusIndex.d.ts.map +1 -0
  104. package/lib/typescript/src/native/CactusUtil.d.ts.map +1 -1
  105. package/lib/typescript/src/native/index.d.ts +1 -0
  106. package/lib/typescript/src/native/index.d.ts.map +1 -1
  107. package/lib/typescript/src/specs/Cactus.nitro.d.ts +4 -2
  108. package/lib/typescript/src/specs/Cactus.nitro.d.ts.map +1 -1
  109. package/lib/typescript/src/specs/CactusFileSystem.nitro.d.ts +1 -0
  110. package/lib/typescript/src/specs/CactusFileSystem.nitro.d.ts.map +1 -1
  111. package/lib/typescript/src/specs/CactusIndex.nitro.d.ts +24 -0
  112. package/lib/typescript/src/specs/CactusIndex.nitro.d.ts.map +1 -0
  113. package/lib/typescript/src/specs/CactusUtil.nitro.d.ts +1 -1
  114. package/lib/typescript/src/specs/CactusUtil.nitro.d.ts.map +1 -1
  115. package/lib/typescript/src/types/CactusIndex.d.ts +34 -0
  116. package/lib/typescript/src/types/CactusIndex.d.ts.map +1 -0
  117. package/lib/typescript/src/types/CactusLM.d.ts +17 -0
  118. package/lib/typescript/src/types/CactusLM.d.ts.map +1 -1
  119. package/lib/typescript/src/types/CactusSTT.d.ts +1 -1
  120. package/lib/typescript/src/types/CactusSTT.d.ts.map +1 -1
  121. package/nitro.json +3 -0
  122. package/nitrogen/generated/android/c++/JDeviceInfo.hpp +1 -1
  123. package/nitrogen/generated/android/c++/JFunc_void_double.hpp +1 -1
  124. package/nitrogen/generated/android/c++/JHybridCactusCryptoSpec.cpp +1 -1
  125. package/nitrogen/generated/android/c++/JHybridCactusCryptoSpec.hpp +1 -1
  126. package/nitrogen/generated/android/c++/JHybridCactusDeviceInfoSpec.cpp +1 -1
  127. package/nitrogen/generated/android/c++/JHybridCactusDeviceInfoSpec.hpp +1 -1
  128. package/nitrogen/generated/android/c++/JHybridCactusFileSystemSpec.cpp +17 -1
  129. package/nitrogen/generated/android/c++/JHybridCactusFileSystemSpec.hpp +2 -1
  130. package/nitrogen/generated/android/c++/JHybridCactusImageSpec.cpp +1 -1
  131. package/nitrogen/generated/android/c++/JHybridCactusImageSpec.hpp +1 -1
  132. package/nitrogen/generated/android/cactus+autolinking.cmake +2 -1
  133. package/nitrogen/generated/android/cactus+autolinking.gradle +1 -1
  134. package/nitrogen/generated/android/cactusOnLoad.cpp +11 -1
  135. package/nitrogen/generated/android/cactusOnLoad.hpp +1 -1
  136. package/nitrogen/generated/android/kotlin/com/margelo/nitro/cactus/DeviceInfo.kt +1 -1
  137. package/nitrogen/generated/android/kotlin/com/margelo/nitro/cactus/Func_void_double.kt +1 -1
  138. package/nitrogen/generated/android/kotlin/com/margelo/nitro/cactus/HybridCactusCryptoSpec.kt +1 -1
  139. package/nitrogen/generated/android/kotlin/com/margelo/nitro/cactus/HybridCactusDeviceInfoSpec.kt +1 -1
  140. package/nitrogen/generated/android/kotlin/com/margelo/nitro/cactus/HybridCactusFileSystemSpec.kt +5 -1
  141. package/nitrogen/generated/android/kotlin/com/margelo/nitro/cactus/HybridCactusImageSpec.kt +1 -1
  142. package/nitrogen/generated/android/kotlin/com/margelo/nitro/cactus/cactusOnLoad.kt +1 -1
  143. package/nitrogen/generated/ios/Cactus+autolinking.rb +1 -1
  144. package/nitrogen/generated/ios/Cactus-Swift-Cxx-Bridge.cpp +1 -1
  145. package/nitrogen/generated/ios/Cactus-Swift-Cxx-Bridge.hpp +1 -1
  146. package/nitrogen/generated/ios/Cactus-Swift-Cxx-Umbrella.hpp +1 -1
  147. package/nitrogen/generated/ios/CactusAutolinking.mm +11 -1
  148. package/nitrogen/generated/ios/CactusAutolinking.swift +1 -1
  149. package/nitrogen/generated/ios/c++/HybridCactusCryptoSpecSwift.cpp +1 -1
  150. package/nitrogen/generated/ios/c++/HybridCactusCryptoSpecSwift.hpp +1 -1
  151. package/nitrogen/generated/ios/c++/HybridCactusDeviceInfoSpecSwift.cpp +1 -1
  152. package/nitrogen/generated/ios/c++/HybridCactusDeviceInfoSpecSwift.hpp +1 -1
  153. package/nitrogen/generated/ios/c++/HybridCactusFileSystemSpecSwift.cpp +1 -1
  154. package/nitrogen/generated/ios/c++/HybridCactusFileSystemSpecSwift.hpp +9 -1
  155. package/nitrogen/generated/ios/c++/HybridCactusImageSpecSwift.cpp +1 -1
  156. package/nitrogen/generated/ios/c++/HybridCactusImageSpecSwift.hpp +1 -1
  157. package/nitrogen/generated/ios/swift/DeviceInfo.swift +1 -1
  158. package/nitrogen/generated/ios/swift/Func_void.swift +1 -1
  159. package/nitrogen/generated/ios/swift/Func_void_DeviceInfo.swift +1 -1
  160. package/nitrogen/generated/ios/swift/Func_void_bool.swift +1 -1
  161. package/nitrogen/generated/ios/swift/Func_void_double.swift +1 -1
  162. package/nitrogen/generated/ios/swift/Func_void_std__exception_ptr.swift +1 -1
  163. package/nitrogen/generated/ios/swift/Func_void_std__optional_std__string_.swift +1 -1
  164. package/nitrogen/generated/ios/swift/Func_void_std__string.swift +1 -1
  165. package/nitrogen/generated/ios/swift/HybridCactusCryptoSpec.swift +1 -1
  166. package/nitrogen/generated/ios/swift/HybridCactusCryptoSpec_cxx.swift +1 -1
  167. package/nitrogen/generated/ios/swift/HybridCactusDeviceInfoSpec.swift +1 -1
  168. package/nitrogen/generated/ios/swift/HybridCactusDeviceInfoSpec_cxx.swift +1 -1
  169. package/nitrogen/generated/ios/swift/HybridCactusFileSystemSpec.swift +2 -1
  170. package/nitrogen/generated/ios/swift/HybridCactusFileSystemSpec_cxx.swift +20 -1
  171. package/nitrogen/generated/ios/swift/HybridCactusImageSpec.swift +1 -1
  172. package/nitrogen/generated/ios/swift/HybridCactusImageSpec_cxx.swift +1 -1
  173. package/nitrogen/generated/shared/c++/CactusIndexGetResult.hpp +84 -0
  174. package/nitrogen/generated/shared/c++/CactusIndexQueryResult.hpp +79 -0
  175. package/nitrogen/generated/shared/c++/DeviceInfo.hpp +1 -1
  176. package/nitrogen/generated/shared/c++/HybridCactusCryptoSpec.cpp +1 -1
  177. package/nitrogen/generated/shared/c++/HybridCactusCryptoSpec.hpp +1 -1
  178. package/nitrogen/generated/shared/c++/HybridCactusDeviceInfoSpec.cpp +1 -1
  179. package/nitrogen/generated/shared/c++/HybridCactusDeviceInfoSpec.hpp +1 -1
  180. package/nitrogen/generated/shared/c++/HybridCactusFileSystemSpec.cpp +2 -1
  181. package/nitrogen/generated/shared/c++/HybridCactusFileSystemSpec.hpp +2 -1
  182. package/nitrogen/generated/shared/c++/HybridCactusImageSpec.cpp +1 -1
  183. package/nitrogen/generated/shared/c++/HybridCactusImageSpec.hpp +1 -1
  184. package/nitrogen/generated/shared/c++/HybridCactusIndexSpec.cpp +27 -0
  185. package/nitrogen/generated/shared/c++/HybridCactusIndexSpec.hpp +76 -0
  186. package/nitrogen/generated/shared/c++/HybridCactusSpec.cpp +3 -1
  187. package/nitrogen/generated/shared/c++/HybridCactusSpec.hpp +6 -3
  188. package/nitrogen/generated/shared/c++/HybridCactusUtilSpec.cpp +1 -1
  189. package/nitrogen/generated/shared/c++/HybridCactusUtilSpec.hpp +2 -2
  190. package/package.json +2 -2
  191. package/src/api/Database.ts +14 -2
  192. package/src/classes/CactusIndex.ts +58 -0
  193. package/src/classes/CactusLM.ts +41 -5
  194. package/src/classes/CactusSTT.ts +19 -6
  195. package/src/config/CactusConfig.ts +3 -0
  196. package/src/constants/packageVersion.ts +1 -1
  197. package/src/hooks/useCactusIndex.ts +195 -0
  198. package/src/hooks/useCactusLM.ts +63 -3
  199. package/src/hooks/useCactusSTT.ts +2 -2
  200. package/src/index.tsx +16 -0
  201. package/src/native/Cactus.ts +40 -13
  202. package/src/native/CactusFileSystem.ts +4 -0
  203. package/src/native/CactusIndex.ts +54 -0
  204. package/src/native/CactusUtil.ts +19 -3
  205. package/src/native/index.ts +1 -0
  206. package/src/specs/Cactus.nitro.ts +13 -2
  207. package/src/specs/CactusFileSystem.nitro.ts +2 -0
  208. package/src/specs/CactusIndex.nitro.ts +31 -0
  209. package/src/specs/CactusUtil.nitro.ts +1 -1
  210. package/src/telemetry/Telemetry.ts +1 -1
  211. package/src/types/CactusIndex.ts +40 -0
  212. package/src/types/CactusLM.ts +21 -0
  213. package/src/types/CactusSTT.ts +1 -1
  214. package/android/src/main/jniLibs/arm64-v8a/libcactus_util.so +0 -0
package/README.md CHANGED
@@ -298,6 +298,82 @@ const App = () => {
298
298
  };
299
299
  ```
300
300
 
301
+ ### Tokenization
302
+
303
+ Convert text into tokens using the model's tokenizer.
304
+
305
+ #### Class
306
+
307
+ ```typescript
308
+ import { CactusLM } from 'cactus-react-native';
309
+
310
+ const cactusLM = new CactusLM();
311
+
312
+ const result = await cactusLM.tokenize({ text: 'Hello, World!' });
313
+ console.log('Token IDs:', result.tokens);
314
+ ```
315
+
316
+ #### Hook
317
+
318
+ ```tsx
319
+ import { useCactusLM } from 'cactus-react-native';
320
+
321
+ const App = () => {
322
+ const cactusLM = useCactusLM();
323
+
324
+ const handleTokenize = async () => {
325
+ const result = await cactusLM.tokenize({ text: 'Hello, World!' });
326
+ console.log('Token IDs:', result.tokens);
327
+ };
328
+
329
+ return <Button title="Tokenize" onPress={handleTokenize} />;
330
+ };
331
+ ```
332
+
333
+ ### Score Window
334
+
335
+ Calculate perplexity scores for a window of tokens within a sequence.
336
+
337
+ #### Class
338
+
339
+ ```typescript
340
+ import { CactusLM } from 'cactus-react-native';
341
+
342
+ const cactusLM = new CactusLM();
343
+
344
+ const tokens = [123, 456, 789, 101, 112];
345
+ const result = await cactusLM.scoreWindow({
346
+ tokens,
347
+ start: 1,
348
+ end: 3,
349
+ context: 2
350
+ });
351
+ console.log('Score:', result.score);
352
+ ```
353
+
354
+ #### Hook
355
+
356
+ ```tsx
357
+ import { useCactusLM } from 'cactus-react-native';
358
+
359
+ const App = () => {
360
+ const cactusLM = useCactusLM();
361
+
362
+ const handleScoreWindow = async () => {
363
+ const tokens = [123, 456, 789, 101, 112];
364
+ const result = await cactusLM.scoreWindow({
365
+ tokens,
366
+ start: 1,
367
+ end: 3,
368
+ context: 2
369
+ });
370
+ console.log('Score:', result.score);
371
+ };
372
+
373
+ return <Button title="Score Window" onPress={handleScoreWindow} />;
374
+ };
375
+ ```
376
+
301
377
  ### Embedding
302
378
 
303
379
  Convert text and images into numerical vector representations that capture semantic meaning, useful for similarity search and semantic understanding.
@@ -423,7 +499,7 @@ The `CactusSTT` class provides audio transcription and audio embedding capabilit
423
499
 
424
500
  ### Transcription
425
501
 
426
- Transcribe audio files to text with streaming support.
502
+ Transcribe audio to text with streaming support. Accepts either a file path or raw PCM audio samples.
427
503
 
428
504
  #### Class
429
505
 
@@ -434,12 +510,22 @@ const cactusSTT = new CactusSTT({ model: 'whisper-small' });
434
510
 
435
511
  await cactusSTT.init();
436
512
 
513
+ // Transcribe from file path
437
514
  const result = await cactusSTT.transcribe({
438
- audioFilePath: 'path/to/audio.wav',
515
+ audio: 'path/to/audio.wav',
439
516
  onToken: (token) => console.log('Token:', token)
440
517
  });
441
518
 
442
519
  console.log('Transcription:', result.response);
520
+
521
+ // Or transcribe from raw PCM samples
522
+ const pcmSamples: number[] = [/* ... */];
523
+ const result2 = await cactusSTT.transcribe({
524
+ audio: pcmSamples,
525
+ onToken: (token) => console.log('Token:', token)
526
+ });
527
+
528
+ console.log('Transcription:', result2.response);
443
529
  ```
444
530
 
445
531
  #### Hook
@@ -451,10 +537,17 @@ const App = () => {
451
537
  const cactusSTT = useCactusSTT({ model: 'whisper-small' });
452
538
 
453
539
  const handleTranscribe = async () => {
540
+ // Transcribe from file path
454
541
  const result = await cactusSTT.transcribe({
455
- audioFilePath: 'path/to/audio.wav',
542
+ audio: 'path/to/audio.wav',
456
543
  });
457
544
  console.log('Transcription:', result.response);
545
+
546
+ const pcmSamples: number[] = [/* ... */];
547
+ const result2 = await cactusSTT.transcribe({
548
+ audio: pcmSamples,
549
+ });
550
+ console.log('Transcription:', result2.response);
458
551
  };
459
552
 
460
553
  return (
@@ -507,6 +600,251 @@ const App = () => {
507
600
  };
508
601
  ```
509
602
 
603
+ ## Vector Index
604
+
605
+ The `CactusIndex` class provides a vector database for storing and querying embeddings with metadata. Enabling similarity search and retrieval.
606
+
607
+ ### Creating and Initializing an Index
608
+
609
+ #### Class
610
+
611
+ ```typescript
612
+ import { CactusIndex } from 'cactus-react-native';
613
+
614
+ const cactusIndex = new CactusIndex('my-index', 1024);
615
+ await cactusIndex.init();
616
+ ```
617
+
618
+ #### Hook
619
+
620
+ ```tsx
621
+ import { useCactusIndex } from 'cactus-react-native';
622
+
623
+ const App = () => {
624
+ const cactusIndex = useCactusIndex({
625
+ name: 'my-index',
626
+ embeddingDim: 1024
627
+ });
628
+
629
+ const handleInit = async () => {
630
+ await cactusIndex.init();
631
+ };
632
+
633
+ return <Button title="Initialize Index" onPress={handleInit} />
634
+ };
635
+ ```
636
+
637
+ ### Adding Documents
638
+
639
+ Add documents with their embeddings and metadata to the index.
640
+
641
+ #### Class
642
+
643
+ ```typescript
644
+ import { CactusIndex } from 'cactus-react-native';
645
+
646
+ const cactusIndex = new CactusIndex('my-index', 1024);
647
+ await cactusIndex.init();
648
+
649
+ await cactusIndex.add({
650
+ ids: [1, 2, 3],
651
+ documents: ['First document', 'Second document', 'Third document'],
652
+ embeddings: [
653
+ [0.1, 0.2, ...],
654
+ [0.3, 0.4, ...],
655
+ [0.5, 0.6, ...]
656
+ ],
657
+ metadatas: ['metadata1', 'metadata2', 'metadata3']
658
+ });
659
+ ```
660
+
661
+ #### Hook
662
+
663
+ ```tsx
664
+ import { useCactusIndex } from 'cactus-react-native';
665
+
666
+ const App = () => {
667
+ const cactusIndex = useCactusIndex({
668
+ name: 'my-index',
669
+ embeddingDim: 1024
670
+ });
671
+
672
+ const handleAdd = async () => {
673
+ await cactusIndex.add({
674
+ ids: [1, 2, 3],
675
+ documents: ['First document', 'Second document', 'Third document'],
676
+ embeddings: [
677
+ [0.1, 0.2, ...],
678
+ [0.3, 0.4, ...],
679
+ [0.5, 0.6, ...]
680
+ ],
681
+ metadatas: ['metadata1', 'metadata2', 'metadata3']
682
+ });
683
+ };
684
+
685
+ return <Button title="Add Documents" onPress={handleAdd} />;
686
+ };
687
+ ```
688
+
689
+ ### Querying the Index
690
+
691
+ Search for similar documents using embedding vectors.
692
+
693
+ #### Class
694
+
695
+ ```typescript
696
+ import { CactusIndex } from 'cactus-react-native';
697
+
698
+ const cactusIndex = new CactusIndex('my-index', 1024);
699
+ await cactusIndex.init();
700
+
701
+ const result = await cactusIndex.query({
702
+ embeddings: [[0.1, 0.2, ...]],
703
+ options: {
704
+ topK: 5,
705
+ scoreThreshold: 0.7
706
+ }
707
+ });
708
+
709
+ console.log('IDs:', result.ids);
710
+ console.log('Scores:', result.scores);
711
+ ```
712
+
713
+ #### Hook
714
+
715
+ ```tsx
716
+ import { useCactusIndex } from 'cactus-react-native';
717
+
718
+ const App = () => {
719
+ const cactusIndex = useCactusIndex({
720
+ name: 'my-index',
721
+ embeddingDim: 1024
722
+ });
723
+
724
+ const handleQuery = async () => {
725
+ const result = await cactusIndex.query({
726
+ embeddings: [[0.1, 0.2, ...]],
727
+ options: {
728
+ topK: 5,
729
+ scoreThreshold: 0.7
730
+ }
731
+ });
732
+ console.log('IDs:', result.ids);
733
+ console.log('Scores:', result.scores);
734
+ };
735
+
736
+ return <Button title="Query Index" onPress={handleQuery} />;
737
+ };
738
+ ```
739
+
740
+ ### Retrieving Documents
741
+
742
+ Get documents by their IDs.
743
+
744
+ #### Class
745
+
746
+ ```typescript
747
+ import { CactusIndex } from 'cactus-react-native';
748
+
749
+ const cactusIndex = new CactusIndex('my-index', 1024);
750
+ await cactusIndex.init();
751
+
752
+ const result = await cactusIndex.get({ ids: [1, 2, 3] });
753
+ console.log('Documents:', result.documents);
754
+ console.log('Metadatas:', result.metadatas);
755
+ console.log('Embeddings:', result.embeddings);
756
+ ```
757
+
758
+ #### Hook
759
+
760
+ ```tsx
761
+ import { useCactusIndex } from 'cactus-react-native';
762
+
763
+ const App = () => {
764
+ const cactusIndex = useCactusIndex({
765
+ name: 'my-index',
766
+ embeddingDim: 1024
767
+ });
768
+
769
+ const handleGet = async () => {
770
+ const result = await cactusIndex.get({ ids: [1, 2, 3] });
771
+ console.log('Documents:', result.documents);
772
+ console.log('Metadatas:', result.metadatas);
773
+ console.log('Embeddings:', result.embeddings);
774
+ };
775
+
776
+ return <Button title="Get Documents" onPress={handleGet} />;
777
+ };
778
+ ```
779
+
780
+ ### Deleting Documents
781
+
782
+ Mark documents as deleted by their IDs.
783
+
784
+ #### Class
785
+
786
+ ```typescript
787
+ import { CactusIndex } from 'cactus-react-native';
788
+
789
+ const cactusIndex = new CactusIndex('my-index', 1024);
790
+ await cactusIndex.init();
791
+
792
+ await cactusIndex.delete({ ids: [1, 2, 3] });
793
+ ```
794
+
795
+ #### Hook
796
+
797
+ ```tsx
798
+ import { useCactusIndex } from 'cactus-react-native';
799
+
800
+ const App = () => {
801
+ const cactusIndex = useCactusIndex({
802
+ name: 'my-index',
803
+ embeddingDim: 1024
804
+ });
805
+
806
+ const handleDelete = async () => {
807
+ await cactusIndex.delete({ ids: [1, 2, 3] });
808
+ };
809
+
810
+ return <Button title="Delete Documents" onPress={handleDelete} />;
811
+ };
812
+ ```
813
+
814
+ ### Compacting the Index
815
+
816
+ Optimize the index by removing deleted documents and reorganizing data.
817
+
818
+ #### Class
819
+
820
+ ```typescript
821
+ import { CactusIndex } from 'cactus-react-native';
822
+
823
+ const cactusIndex = new CactusIndex('my-index', 1024);
824
+ await cactusIndex.init();
825
+
826
+ await cactusIndex.compact();
827
+ ```
828
+
829
+ #### Hook
830
+
831
+ ```tsx
832
+ import { useCactusIndex } from 'cactus-react-native';
833
+
834
+ const App = () => {
835
+ const cactusIndex = useCactusIndex({
836
+ name: 'my-index',
837
+ embeddingDim: 1024
838
+ });
839
+
840
+ const handleCompact = async () => {
841
+ await cactusIndex.compact();
842
+ };
843
+
844
+ return <Button title="Compact Index" onPress={handleCompact} />;
845
+ };
846
+ ```
847
+
510
848
  ## API Reference
511
849
 
512
850
  ### CactusLM Class
@@ -516,7 +854,7 @@ const App = () => {
516
854
  **`new CactusLM(params?: CactusLMParams)`**
517
855
 
518
856
  **Parameters:**
519
- - `model` - Model slug (default: `'qwen3-0.6'`).
857
+ - `model` - Model slug or absolute path to Cactus model (default: `'qwen3-0.6'`).
520
858
  - `contextSize` - Context window size (default: `2048`).
521
859
  - `corpusDir` - Directory containing text files for RAG (default: `undefined`).
522
860
 
@@ -545,16 +883,35 @@ Performs text completion with optional streaming and tool support. Automatically
545
883
  - `topK` - Top-K sampling limit (default: model-optimized).
546
884
  - `maxTokens` - Maximum number of tokens to generate (default: `512`).
547
885
  - `stopSequences` - Array of strings to stop generation (default: `undefined`).
886
+ - `forceTools` - Force the model to call one of the provided tools (default: `false`).
548
887
  - `tools` - Array of `Tool` objects for function calling (default: `undefined`).
549
888
  - `onToken` - Callback for streaming tokens.
550
889
  - `mode` - Completion mode: `'local'` | `'hybrid'` (default: `'local'`)
551
890
 
891
+ **`tokenize(params: CactusLMTokenizeParams): Promise<CactusLMTokenizeResult>`**
892
+
893
+ Converts text into tokens using the model's tokenizer.
894
+
895
+ **Parameters:**
896
+ - `text` - Text to tokenize.
897
+
898
+ **`scoreWindow(params: CactusLMScoreWindowParams): Promise<CactusLMScoreWindowResult>`**
899
+
900
+ Calculates perplexity scores for a window of tokens within a sequence.
901
+
902
+ **Parameters:**
903
+ - `tokens` - Array of token IDs.
904
+ - `start` - Start index of the window.
905
+ - `end` - End index of the window.
906
+ - `context` - Number of context tokens before the window.
907
+
552
908
  **`embed(params: CactusLMEmbedParams): Promise<CactusLMEmbedResult>`**
553
909
 
554
910
  Generates embeddings for the given text. Automatically calls `init()` if not already initialized. Throws an error if a generation (completion or embedding) is already in progress.
555
911
 
556
912
  **Parameters:**
557
913
  - `text` - Text to embed.
914
+ - `normalize` - Whether to normalize the embedding vector (default: `false`).
558
915
 
559
916
  **`imageEmbed(params: CactusLMImageEmbedParams): Promise<CactusLMImageEmbedResult>`**
560
917
 
@@ -598,6 +955,8 @@ The `useCactusLM` hook manages a `CactusLM` instance with reactive state. When m
598
955
  - `download(params?: CactusLMDownloadParams): Promise<void>` - Downloads the model. Updates `isDownloading` and `downloadProgress` state during download. Sets `isDownloaded` to `true` on success.
599
956
  - `init(): Promise<void>` - Initializes the model for inference. Sets `isInitializing` to `true` during initialization.
600
957
  - `complete(params: CactusLMCompleteParams): Promise<CactusLMCompleteResult>` - Generates text completions. Automatically accumulates tokens in the `completion` state during streaming. Sets `isGenerating` to `true` while generating. Clears `completion` before starting.
958
+ - `tokenize(params: CactusLMTokenizeParams): Promise<CactusLMTokenizeResult>` - Converts text into tokens. Sets `isGenerating` to `true` during operation.
959
+ - `scoreWindow(params: CactusLMScoreWindowParams): Promise<CactusLMScoreWindowResult>` - Calculates perplexity scores for a window of tokens. Sets `isGenerating` to `true` during operation.
601
960
  - `embed(params: CactusLMEmbedParams): Promise<CactusLMEmbedResult>` - Generates embeddings for the given text. Sets `isGenerating` to `true` during operation.
602
961
  - `imageEmbed(params: CactusLMImageEmbedParams): Promise<CactusLMImageEmbedResult>` - Generates embeddings for the given image. Sets `isGenerating` to `true` while generating.
603
962
  - `stop(): Promise<void>` - Stops ongoing generation. Clears any errors.
@@ -612,7 +971,7 @@ The `useCactusLM` hook manages a `CactusLM` instance with reactive state. When m
612
971
  **`new CactusSTT(params?: CactusSTTParams)`**
613
972
 
614
973
  **Parameters:**
615
- - `model` - Model slug (default: `'whisper-small'`).
974
+ - `model` - Model slug or absolute path to Cactus model (default: `'qwen3-0.6'`).
616
975
  - `contextSize` - Context window size (default: `2048`).
617
976
 
618
977
  #### Methods
@@ -630,10 +989,10 @@ Initializes the model and prepares it for inference. Safe to call multiple times
630
989
 
631
990
  **`transcribe(params: CactusSTTTranscribeParams): Promise<CactusSTTTranscribeResult>`**
632
991
 
633
- Transcribes audio to text with optional streaming support. Automatically calls `init()` if not already initialized. Throws an error if a generation is already in progress.
992
+ Transcribes audio to text with optional streaming support. Accepts either a file path or raw PCM audio samples. Automatically calls `init()` if not already initialized. Throws an error if a generation is already in progress.
634
993
 
635
994
  **Parameters:**
636
- - `audioFilePath` - Path to the audio file.
995
+ - `audio` - Path to the audio file or raw PCM samples.
637
996
  - `prompt` - Optional prompt to guide transcription (default: `'<|startoftranscript|><|en|><|transcribe|><|notimestamps|>'`).
638
997
  - `options` - Transcription options:
639
998
  - `temperature` - Sampling temperature (default: model-optimized).
@@ -691,6 +1050,84 @@ The `useCactusSTT` hook manages a `CactusSTT` instance with reactive state. When
691
1050
  - `destroy(): Promise<void>` - Releases all resources associated with the model. Clears the `transcription` state. Automatically called when the component unmounts.
692
1051
  - `getModels(): Promise<CactusSTTModel[]>` - Fetches available STT models from the database and checks their download status.
693
1052
 
1053
+ ### CactusIndex Class
1054
+
1055
+ #### Constructor
1056
+
1057
+ **`new CactusIndex(name: string, embeddingDim: number)`**
1058
+
1059
+ **Parameters:**
1060
+ - `name` - Name of the index.
1061
+ - `embeddingDim` - Dimension of the embedding vectors.
1062
+
1063
+ #### Methods
1064
+
1065
+ **`init(): Promise<void>`**
1066
+
1067
+ Initializes the index and prepares it for operations. Must be called before using any other methods.
1068
+
1069
+ **`add(params: CactusIndexAddParams): Promise<void>`**
1070
+
1071
+ Adds documents with their embeddings and metadata to the index.
1072
+
1073
+ **Parameters:**
1074
+ - `ids` - Array of document IDs.
1075
+ - `documents` - Array of document texts.
1076
+ - `embeddings` - Array of embedding vectors (each vector must match `embeddingDim`).
1077
+ - `metadatas` - Optional array of metadata strings.
1078
+
1079
+ **`query(params: CactusIndexQueryParams): Promise<CactusIndexQueryResult>`**
1080
+
1081
+ Searches for similar documents using embedding vectors.
1082
+
1083
+ **Parameters:**
1084
+ - `embeddings` - Array of query embedding vectors.
1085
+ - `options` - Query options:
1086
+ - `topK` - Number of top results to return (default: 10).
1087
+ - `scoreThreshold` - Minimum similarity score threshold (default: -1.0).
1088
+
1089
+ **`get(params: CactusIndexGetParams): Promise<CactusIndexGetResult>`**
1090
+
1091
+ Retrieves documents by their IDs.
1092
+
1093
+ **Parameters:**
1094
+ - `ids` - Array of document IDs to retrieve.
1095
+
1096
+ **`delete(params: CactusIndexDeleteParams): Promise<void>`**
1097
+
1098
+ Deletes documents from the index by their IDs.
1099
+
1100
+ **Parameters:**
1101
+ - `ids` - Array of document IDs to delete.
1102
+
1103
+ **`compact(): Promise<void>`**
1104
+
1105
+ Optimizes the index by removing deleted documents and reorganizing data for better performance. Call after a series of deletions.
1106
+
1107
+ **`destroy(): Promise<void>`**
1108
+
1109
+ Releases all resources associated with the index from memory.
1110
+
1111
+ ### useCactusIndex Hook
1112
+
1113
+ The `useCactusIndex` hook manages a `CactusIndex` instance with reactive state. When index parameters (`name` or `embeddingDim`) change, the hook creates a new instance and resets all state. The hook automatically cleans up resources when the component unmounts.
1114
+
1115
+ #### State
1116
+
1117
+ - `isInitializing: boolean` - Whether the index is initializing.
1118
+ - `isProcessing: boolean` - Whether the index is processing an operation (add, query, get, delete, or compact).
1119
+ - `error: string | null` - Last error message from any operation, or `null` if there is no error. Cleared before starting new operations.
1120
+
1121
+ #### Methods
1122
+
1123
+ - `init(): Promise<void>` - Initializes the index. Sets `isInitializing` to `true` during initialization.
1124
+ - `add(params: CactusIndexAddParams): Promise<void>` - Adds documents to the index. Sets `isProcessing` to `true` during operation.
1125
+ - `query(params: CactusIndexQueryParams): Promise<CactusIndexQueryResult>` - Searches for similar documents. Sets `isProcessing` to `true` during operation.
1126
+ - `get(params: CactusIndexGetParams): Promise<CactusIndexGetResult>` - Retrieves documents by IDs. Sets `isProcessing` to `true` during operation.
1127
+ - `delete(params: CactusIndexDeleteParams): Promise<void>` - Deletes documents. Sets `isProcessing` to `true` during operation.
1128
+ - `compact(): Promise<void>` - Optimizes the index. Sets `isProcessing` to `true` during operation.
1129
+ - `destroy(): Promise<void>` - Releases all resources. Automatically called when the component unmounts.
1130
+
694
1131
  ## Type Definitions
695
1132
 
696
1133
  ### CactusLMParams
@@ -730,6 +1167,7 @@ interface CompleteOptions {
730
1167
  topK?: number;
731
1168
  maxTokens?: number;
732
1169
  stopSequences?: string[];
1170
+ forceTools?: boolean;
733
1171
  }
734
1172
  ```
735
1173
 
@@ -783,11 +1221,47 @@ interface CactusLMCompleteResult {
783
1221
  }
784
1222
  ```
785
1223
 
1224
+ ### CactusLMTokenizeParams
1225
+
1226
+ ```typescript
1227
+ interface CactusLMTokenizeParams {
1228
+ text: string;
1229
+ }
1230
+ ```
1231
+
1232
+ ### CactusLMTokenizeResult
1233
+
1234
+ ```typescript
1235
+ interface CactusLMTokenizeResult {
1236
+ tokens: number[];
1237
+ }
1238
+ ```
1239
+
1240
+ ### CactusLMScoreWindowParams
1241
+
1242
+ ```typescript
1243
+ interface CactusLMScoreWindowParams {
1244
+ tokens: number[];
1245
+ start: number;
1246
+ end: number;
1247
+ context: number;
1248
+ }
1249
+ ```
1250
+
1251
+ ### CactusLMScoreWindowResult
1252
+
1253
+ ```typescript
1254
+ interface CactusLMScoreWindowResult {
1255
+ score: number;
1256
+ }
1257
+ ```
1258
+
786
1259
  ### CactusLMEmbedParams
787
1260
 
788
1261
  ```typescript
789
1262
  interface CactusLMEmbedParams {
790
1263
  text: string;
1264
+ normalize?: boolean;
791
1265
  }
792
1266
  ```
793
1267
 
@@ -878,7 +1352,7 @@ interface TranscribeOptions {
878
1352
 
879
1353
  ```typescript
880
1354
  interface CactusSTTTranscribeParams {
881
- audioFilePath: string;
1355
+ audio: string | number[];
882
1356
  prompt?: string;
883
1357
  options?: TranscribeOptions;
884
1358
  onToken?: (token: string) => void;
@@ -917,6 +1391,79 @@ interface CactusSTTAudioEmbedResult {
917
1391
  }
918
1392
  ```
919
1393
 
1394
+ ### CactusIndexParams
1395
+
1396
+ ```typescript
1397
+ interface CactusIndexParams {
1398
+ name: string;
1399
+ embeddingDim: number;
1400
+ }
1401
+ ```
1402
+
1403
+ ### CactusIndexAddParams
1404
+
1405
+ ```typescript
1406
+ interface CactusIndexAddParams {
1407
+ ids: number[];
1408
+ documents: string[];
1409
+ embeddings: number[][];
1410
+ metadatas?: string[];
1411
+ }
1412
+ ```
1413
+
1414
+ ### CactusIndexGetParams
1415
+
1416
+ ```typescript
1417
+ interface CactusIndexGetParams {
1418
+ ids: number[];
1419
+ }
1420
+ ```
1421
+
1422
+ ### CactusIndexGetResult
1423
+
1424
+ ```typescript
1425
+ interface CactusIndexGetResult {
1426
+ documents: string[];
1427
+ metadatas: string[];
1428
+ embeddings: number[][];
1429
+ }
1430
+ ```
1431
+
1432
+ ### IndexQueryOptions
1433
+
1434
+ ```typescript
1435
+ interface IndexQueryOptions {
1436
+ topK?: number;
1437
+ scoreThreshold?: number;
1438
+ }
1439
+ ```
1440
+
1441
+ ### CactusIndexQueryParams
1442
+
1443
+ ```typescript
1444
+ interface CactusIndexQueryParams {
1445
+ embeddings: number[][];
1446
+ options?: IndexQueryOptions;
1447
+ }
1448
+ ```
1449
+
1450
+ ### CactusIndexQueryResult
1451
+
1452
+ ```typescript
1453
+ interface CactusIndexQueryResult {
1454
+ ids: number[][];
1455
+ scores: number[][];
1456
+ }
1457
+ ```
1458
+
1459
+ ### CactusIndexDeleteParams
1460
+
1461
+ ```typescript
1462
+ interface CactusIndexDeleteParams {
1463
+ ids: number[];
1464
+ }
1465
+ ```
1466
+
920
1467
  ## Configuration
921
1468
 
922
1469
  ### Telemetry
@@ -6,10 +6,11 @@ set(CMAKE_VERBOSE_MAKEFILE ON)
6
6
  set(CMAKE_CXX_STANDARD 20)
7
7
 
8
8
  # Define C++ library and add all sources
9
- add_library(${PACKAGE_NAME} SHARED
9
+ add_library(${PACKAGE_NAME} SHARED
10
10
  src/main/cpp/cpp-adapter.cpp
11
11
  ../cpp/HybridCactus.cpp
12
12
  ../cpp/HybridCactusUtil.cpp
13
+ ../cpp/HybridCactusIndex.cpp
13
14
  )
14
15
 
15
16
  add_library(libcactus STATIC IMPORTED)
@@ -17,9 +18,9 @@ set_target_properties(libcactus PROPERTIES
17
18
  IMPORTED_LOCATION "${CMAKE_CURRENT_LIST_DIR}/src/main/jniLibs/${ANDROID_ABI}/libcactus.a"
18
19
  )
19
20
 
20
- add_library(libcactus_util SHARED IMPORTED)
21
+ add_library(libcactus_util STATIC IMPORTED)
21
22
  set_target_properties(libcactus_util PROPERTIES
22
- IMPORTED_LOCATION "${CMAKE_CURRENT_LIST_DIR}/src/main/jniLibs/${ANDROID_ABI}/libcactus_util.so"
23
+ IMPORTED_LOCATION "${CMAKE_CURRENT_LIST_DIR}/src/main/jniLibs/${ANDROID_ABI}/libcactus_util.a"
23
24
  )
24
25
 
25
26
  # Add Nitrogen specs :)