cactus-react-native 1.2.1 → 1.5.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 (238) hide show
  1. package/README.md +765 -33
  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 +231 -19
  7. package/cpp/HybridCactus.hpp +25 -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 +107 -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 +107 -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} +145 -18
  19. package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/engine.h +135 -7
  20. package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/gemma_tools.h +549 -0
  21. package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/graph.h +193 -26
  22. package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/kernel.h +54 -195
  23. package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/kernel_utils.h +399 -140
  24. package/ios/cactus.xcframework/ios-arm64/cactus.framework/Info.plist +0 -0
  25. package/ios/cactus.xcframework/ios-arm64/cactus.framework/cactus +0 -0
  26. package/ios/cactus.xcframework/ios-arm64-simulator/cactus.framework/Headers/cactus.h +2 -0
  27. package/ios/cactus.xcframework/ios-arm64-simulator/cactus.framework/Headers/cactus_ffi.h +107 -2
  28. package/ios/cactus.xcframework/ios-arm64-simulator/cactus.framework/Headers/cactus_telemetry.h +656 -0
  29. package/ios/cactus.xcframework/ios-arm64-simulator/cactus.framework/Headers/{ffi_utils.h → cactus_utils.h} +145 -18
  30. package/ios/cactus.xcframework/ios-arm64-simulator/cactus.framework/Headers/engine.h +135 -7
  31. package/ios/cactus.xcframework/ios-arm64-simulator/cactus.framework/Headers/gemma_tools.h +549 -0
  32. package/ios/cactus.xcframework/ios-arm64-simulator/cactus.framework/Headers/graph.h +193 -26
  33. package/ios/cactus.xcframework/ios-arm64-simulator/cactus.framework/Headers/kernel.h +54 -195
  34. package/ios/cactus.xcframework/ios-arm64-simulator/cactus.framework/Headers/kernel_utils.h +399 -140
  35. package/ios/cactus.xcframework/ios-arm64-simulator/cactus.framework/Info.plist +0 -0
  36. package/ios/cactus.xcframework/ios-arm64-simulator/cactus.framework/_CodeSignature/CodeResources +1 -1
  37. package/ios/cactus.xcframework/ios-arm64-simulator/cactus.framework/cactus +0 -0
  38. package/ios/cactus_util.xcframework/Info.plist +4 -4
  39. package/ios/cactus_util.xcframework/ios-arm64/cactus_util.framework/Headers/cactus_util.h +1 -1
  40. package/ios/cactus_util.xcframework/ios-arm64/cactus_util.framework/Headers/database.h +27 -0
  41. package/ios/cactus_util.xcframework/ios-arm64/cactus_util.framework/Info.plist +0 -0
  42. package/ios/cactus_util.xcframework/ios-arm64/cactus_util.framework/cactus_util +0 -0
  43. package/ios/cactus_util.xcframework/ios-arm64-simulator/cactus_util.framework/Headers/cactus_util.h +1 -1
  44. package/ios/cactus_util.xcframework/ios-arm64-simulator/cactus_util.framework/Headers/database.h +27 -0
  45. package/ios/cactus_util.xcframework/ios-arm64-simulator/cactus_util.framework/Info.plist +0 -0
  46. package/ios/cactus_util.xcframework/ios-arm64-simulator/cactus_util.framework/_CodeSignature/CodeResources +3 -3
  47. package/ios/cactus_util.xcframework/ios-arm64-simulator/cactus_util.framework/cactus_util +0 -0
  48. package/lib/module/api/Database.js +12 -95
  49. package/lib/module/api/Database.js.map +1 -1
  50. package/lib/module/classes/CactusIndex.js +45 -0
  51. package/lib/module/classes/CactusIndex.js.map +1 -0
  52. package/lib/module/classes/CactusLM.js +65 -17
  53. package/lib/module/classes/CactusLM.js.map +1 -1
  54. package/lib/module/classes/CactusSTT.js +104 -17
  55. package/lib/module/classes/CactusSTT.js.map +1 -1
  56. package/lib/module/config/CactusConfig.js +2 -0
  57. package/lib/module/config/CactusConfig.js.map +1 -1
  58. package/lib/module/constants/packageVersion.js +1 -1
  59. package/lib/module/hooks/useCactusIndex.js +175 -0
  60. package/lib/module/hooks/useCactusIndex.js.map +1 -0
  61. package/lib/module/hooks/useCactusLM.js +68 -7
  62. package/lib/module/hooks/useCactusLM.js.map +1 -1
  63. package/lib/module/hooks/useCactusSTT.js +102 -6
  64. package/lib/module/hooks/useCactusSTT.js.map +1 -1
  65. package/lib/module/index.js +2 -0
  66. package/lib/module/index.js.map +1 -1
  67. package/lib/module/models.js +336 -0
  68. package/lib/module/models.js.map +1 -0
  69. package/lib/module/native/Cactus.js +61 -13
  70. package/lib/module/native/Cactus.js.map +1 -1
  71. package/lib/module/native/CactusFileSystem.js +3 -0
  72. package/lib/module/native/CactusFileSystem.js.map +1 -1
  73. package/lib/module/native/CactusIndex.js +32 -0
  74. package/lib/module/native/CactusIndex.js.map +1 -0
  75. package/lib/module/native/CactusUtil.js +16 -3
  76. package/lib/module/native/CactusUtil.js.map +1 -1
  77. package/lib/module/native/index.js +1 -0
  78. package/lib/module/native/index.js.map +1 -1
  79. package/lib/module/specs/CactusIndex.nitro.js +4 -0
  80. package/lib/module/specs/CactusIndex.nitro.js.map +1 -0
  81. package/lib/module/telemetry/Telemetry.js +3 -1
  82. package/lib/module/telemetry/Telemetry.js.map +1 -1
  83. package/lib/module/types/CactusIndex.js +2 -0
  84. package/lib/module/types/{CactusModel.js.map → CactusIndex.js.map} +1 -1
  85. package/lib/module/types/CactusLM.js +2 -0
  86. package/lib/module/types/CactusSTT.js +2 -0
  87. package/lib/module/types/common.js +2 -0
  88. package/lib/module/types/{CactusSTTModel.js.map → common.js.map} +1 -1
  89. package/lib/typescript/src/api/Database.d.ts +4 -7
  90. package/lib/typescript/src/api/Database.d.ts.map +1 -1
  91. package/lib/typescript/src/classes/CactusIndex.d.ts +15 -0
  92. package/lib/typescript/src/classes/CactusIndex.d.ts.map +1 -0
  93. package/lib/typescript/src/classes/CactusLM.d.ts +12 -5
  94. package/lib/typescript/src/classes/CactusLM.d.ts.map +1 -1
  95. package/lib/typescript/src/classes/CactusSTT.d.ts +15 -5
  96. package/lib/typescript/src/classes/CactusSTT.d.ts.map +1 -1
  97. package/lib/typescript/src/config/CactusConfig.d.ts +1 -0
  98. package/lib/typescript/src/config/CactusConfig.d.ts.map +1 -1
  99. package/lib/typescript/src/constants/packageVersion.d.ts +1 -1
  100. package/lib/typescript/src/hooks/useCactusIndex.d.ts +14 -0
  101. package/lib/typescript/src/hooks/useCactusIndex.d.ts.map +1 -0
  102. package/lib/typescript/src/hooks/useCactusLM.d.ts +6 -4
  103. package/lib/typescript/src/hooks/useCactusLM.d.ts.map +1 -1
  104. package/lib/typescript/src/hooks/useCactusSTT.d.ts +13 -5
  105. package/lib/typescript/src/hooks/useCactusSTT.d.ts.map +1 -1
  106. package/lib/typescript/src/index.d.ts +6 -4
  107. package/lib/typescript/src/index.d.ts.map +1 -1
  108. package/lib/typescript/src/models.d.ts +6 -0
  109. package/lib/typescript/src/models.d.ts.map +1 -0
  110. package/lib/typescript/src/native/Cactus.d.ts +10 -3
  111. package/lib/typescript/src/native/Cactus.d.ts.map +1 -1
  112. package/lib/typescript/src/native/CactusFileSystem.d.ts +1 -0
  113. package/lib/typescript/src/native/CactusFileSystem.d.ts.map +1 -1
  114. package/lib/typescript/src/native/CactusIndex.d.ts +12 -0
  115. package/lib/typescript/src/native/CactusIndex.d.ts.map +1 -0
  116. package/lib/typescript/src/native/CactusUtil.d.ts.map +1 -1
  117. package/lib/typescript/src/native/index.d.ts +1 -0
  118. package/lib/typescript/src/native/index.d.ts.map +1 -1
  119. package/lib/typescript/src/specs/Cactus.nitro.d.ts +9 -2
  120. package/lib/typescript/src/specs/Cactus.nitro.d.ts.map +1 -1
  121. package/lib/typescript/src/specs/CactusFileSystem.nitro.d.ts +1 -0
  122. package/lib/typescript/src/specs/CactusFileSystem.nitro.d.ts.map +1 -1
  123. package/lib/typescript/src/specs/CactusIndex.nitro.d.ts +24 -0
  124. package/lib/typescript/src/specs/CactusIndex.nitro.d.ts.map +1 -0
  125. package/lib/typescript/src/specs/CactusUtil.nitro.d.ts +1 -1
  126. package/lib/typescript/src/specs/CactusUtil.nitro.d.ts.map +1 -1
  127. package/lib/typescript/src/types/CactusIndex.d.ts +34 -0
  128. package/lib/typescript/src/types/CactusIndex.d.ts.map +1 -0
  129. package/lib/typescript/src/types/CactusLM.d.ts +19 -0
  130. package/lib/typescript/src/types/CactusLM.d.ts.map +1 -1
  131. package/lib/typescript/src/types/CactusSTT.d.ts +21 -1
  132. package/lib/typescript/src/types/CactusSTT.d.ts.map +1 -1
  133. package/lib/typescript/src/types/common.d.ts +28 -0
  134. package/lib/typescript/src/types/common.d.ts.map +1 -0
  135. package/nitro.json +3 -0
  136. package/nitrogen/generated/android/c++/JDeviceInfo.hpp +1 -1
  137. package/nitrogen/generated/android/c++/JFunc_void_double.hpp +1 -1
  138. package/nitrogen/generated/android/c++/JHybridCactusCryptoSpec.cpp +1 -1
  139. package/nitrogen/generated/android/c++/JHybridCactusCryptoSpec.hpp +1 -1
  140. package/nitrogen/generated/android/c++/JHybridCactusDeviceInfoSpec.cpp +1 -1
  141. package/nitrogen/generated/android/c++/JHybridCactusDeviceInfoSpec.hpp +1 -1
  142. package/nitrogen/generated/android/c++/JHybridCactusFileSystemSpec.cpp +17 -1
  143. package/nitrogen/generated/android/c++/JHybridCactusFileSystemSpec.hpp +2 -1
  144. package/nitrogen/generated/android/c++/JHybridCactusImageSpec.cpp +1 -1
  145. package/nitrogen/generated/android/c++/JHybridCactusImageSpec.hpp +1 -1
  146. package/nitrogen/generated/android/cactus+autolinking.cmake +2 -1
  147. package/nitrogen/generated/android/cactus+autolinking.gradle +1 -1
  148. package/nitrogen/generated/android/cactusOnLoad.cpp +11 -1
  149. package/nitrogen/generated/android/cactusOnLoad.hpp +1 -1
  150. package/nitrogen/generated/android/kotlin/com/margelo/nitro/cactus/DeviceInfo.kt +1 -1
  151. package/nitrogen/generated/android/kotlin/com/margelo/nitro/cactus/Func_void_double.kt +1 -1
  152. package/nitrogen/generated/android/kotlin/com/margelo/nitro/cactus/HybridCactusCryptoSpec.kt +1 -1
  153. package/nitrogen/generated/android/kotlin/com/margelo/nitro/cactus/HybridCactusDeviceInfoSpec.kt +1 -1
  154. package/nitrogen/generated/android/kotlin/com/margelo/nitro/cactus/HybridCactusFileSystemSpec.kt +5 -1
  155. package/nitrogen/generated/android/kotlin/com/margelo/nitro/cactus/HybridCactusImageSpec.kt +1 -1
  156. package/nitrogen/generated/android/kotlin/com/margelo/nitro/cactus/cactusOnLoad.kt +1 -1
  157. package/nitrogen/generated/ios/Cactus+autolinking.rb +1 -1
  158. package/nitrogen/generated/ios/Cactus-Swift-Cxx-Bridge.cpp +1 -1
  159. package/nitrogen/generated/ios/Cactus-Swift-Cxx-Bridge.hpp +1 -1
  160. package/nitrogen/generated/ios/Cactus-Swift-Cxx-Umbrella.hpp +1 -1
  161. package/nitrogen/generated/ios/CactusAutolinking.mm +11 -1
  162. package/nitrogen/generated/ios/CactusAutolinking.swift +1 -1
  163. package/nitrogen/generated/ios/c++/HybridCactusCryptoSpecSwift.cpp +1 -1
  164. package/nitrogen/generated/ios/c++/HybridCactusCryptoSpecSwift.hpp +1 -1
  165. package/nitrogen/generated/ios/c++/HybridCactusDeviceInfoSpecSwift.cpp +1 -1
  166. package/nitrogen/generated/ios/c++/HybridCactusDeviceInfoSpecSwift.hpp +1 -1
  167. package/nitrogen/generated/ios/c++/HybridCactusFileSystemSpecSwift.cpp +1 -1
  168. package/nitrogen/generated/ios/c++/HybridCactusFileSystemSpecSwift.hpp +9 -1
  169. package/nitrogen/generated/ios/c++/HybridCactusImageSpecSwift.cpp +1 -1
  170. package/nitrogen/generated/ios/c++/HybridCactusImageSpecSwift.hpp +1 -1
  171. package/nitrogen/generated/ios/swift/DeviceInfo.swift +1 -1
  172. package/nitrogen/generated/ios/swift/Func_void.swift +1 -1
  173. package/nitrogen/generated/ios/swift/Func_void_DeviceInfo.swift +1 -1
  174. package/nitrogen/generated/ios/swift/Func_void_bool.swift +1 -1
  175. package/nitrogen/generated/ios/swift/Func_void_double.swift +1 -1
  176. package/nitrogen/generated/ios/swift/Func_void_std__exception_ptr.swift +1 -1
  177. package/nitrogen/generated/ios/swift/Func_void_std__optional_std__string_.swift +1 -1
  178. package/nitrogen/generated/ios/swift/Func_void_std__string.swift +1 -1
  179. package/nitrogen/generated/ios/swift/HybridCactusCryptoSpec.swift +1 -1
  180. package/nitrogen/generated/ios/swift/HybridCactusCryptoSpec_cxx.swift +1 -1
  181. package/nitrogen/generated/ios/swift/HybridCactusDeviceInfoSpec.swift +1 -1
  182. package/nitrogen/generated/ios/swift/HybridCactusDeviceInfoSpec_cxx.swift +1 -1
  183. package/nitrogen/generated/ios/swift/HybridCactusFileSystemSpec.swift +2 -1
  184. package/nitrogen/generated/ios/swift/HybridCactusFileSystemSpec_cxx.swift +20 -1
  185. package/nitrogen/generated/ios/swift/HybridCactusImageSpec.swift +1 -1
  186. package/nitrogen/generated/ios/swift/HybridCactusImageSpec_cxx.swift +1 -1
  187. package/nitrogen/generated/shared/c++/CactusIndexGetResult.hpp +84 -0
  188. package/nitrogen/generated/shared/c++/CactusIndexQueryResult.hpp +79 -0
  189. package/nitrogen/generated/shared/c++/DeviceInfo.hpp +1 -1
  190. package/nitrogen/generated/shared/c++/HybridCactusCryptoSpec.cpp +1 -1
  191. package/nitrogen/generated/shared/c++/HybridCactusCryptoSpec.hpp +1 -1
  192. package/nitrogen/generated/shared/c++/HybridCactusDeviceInfoSpec.cpp +1 -1
  193. package/nitrogen/generated/shared/c++/HybridCactusDeviceInfoSpec.hpp +1 -1
  194. package/nitrogen/generated/shared/c++/HybridCactusFileSystemSpec.cpp +2 -1
  195. package/nitrogen/generated/shared/c++/HybridCactusFileSystemSpec.hpp +2 -1
  196. package/nitrogen/generated/shared/c++/HybridCactusImageSpec.cpp +1 -1
  197. package/nitrogen/generated/shared/c++/HybridCactusImageSpec.hpp +1 -1
  198. package/nitrogen/generated/shared/c++/HybridCactusIndexSpec.cpp +27 -0
  199. package/nitrogen/generated/shared/c++/HybridCactusIndexSpec.hpp +76 -0
  200. package/nitrogen/generated/shared/c++/HybridCactusSpec.cpp +8 -1
  201. package/nitrogen/generated/shared/c++/HybridCactusSpec.hpp +11 -3
  202. package/nitrogen/generated/shared/c++/HybridCactusUtilSpec.cpp +1 -1
  203. package/nitrogen/generated/shared/c++/HybridCactusUtilSpec.hpp +2 -2
  204. package/package.json +2 -2
  205. package/src/api/Database.ts +14 -135
  206. package/src/classes/CactusIndex.ts +58 -0
  207. package/src/classes/CactusLM.ts +87 -19
  208. package/src/classes/CactusSTT.ts +134 -20
  209. package/src/config/CactusConfig.ts +3 -0
  210. package/src/constants/packageVersion.ts +1 -1
  211. package/src/hooks/useCactusIndex.ts +195 -0
  212. package/src/hooks/useCactusLM.ts +88 -8
  213. package/src/hooks/useCactusSTT.ts +119 -7
  214. package/src/index.tsx +22 -2
  215. package/src/models.ts +344 -0
  216. package/src/native/Cactus.ts +95 -13
  217. package/src/native/CactusFileSystem.ts +4 -0
  218. package/src/native/CactusIndex.ts +54 -0
  219. package/src/native/CactusUtil.ts +19 -3
  220. package/src/native/index.ts +1 -0
  221. package/src/specs/Cactus.nitro.ts +18 -2
  222. package/src/specs/CactusFileSystem.nitro.ts +2 -0
  223. package/src/specs/CactusIndex.nitro.ts +31 -0
  224. package/src/specs/CactusUtil.nitro.ts +1 -1
  225. package/src/telemetry/Telemetry.ts +1 -1
  226. package/src/types/CactusIndex.ts +40 -0
  227. package/src/types/CactusLM.ts +24 -0
  228. package/src/types/CactusSTT.ts +27 -1
  229. package/src/types/common.ts +28 -0
  230. package/android/src/main/jniLibs/arm64-v8a/libcactus_util.so +0 -0
  231. package/lib/module/types/CactusModel.js +0 -2
  232. package/lib/module/types/CactusSTTModel.js +0 -2
  233. package/lib/typescript/src/types/CactusModel.d.ts +0 -13
  234. package/lib/typescript/src/types/CactusModel.d.ts.map +0 -1
  235. package/lib/typescript/src/types/CactusSTTModel.d.ts +0 -8
  236. package/lib/typescript/src/types/CactusSTTModel.d.ts.map +0 -1
  237. package/src/types/CactusModel.ts +0 -15
  238. package/src/types/CactusSTTModel.ts +0 -10
@@ -0,0 +1,325 @@
1
+ #include "HybridCactusIndex.hpp"
2
+
3
+ namespace margelo::nitro::cactus {
4
+
5
+ HybridCactusIndex::HybridCactusIndex() : HybridObject(TAG) {}
6
+
7
+ std::shared_ptr<Promise<void>>
8
+ HybridCactusIndex::init(const std::string &indexPath, double embeddingDim) {
9
+ return Promise<void>::async([this, indexPath, embeddingDim]() -> void {
10
+ std::lock_guard<std::mutex> lock(this->_indexMutex);
11
+
12
+ if (this->_index) {
13
+ throw std::runtime_error("Cactus index is already initialized");
14
+ }
15
+
16
+ const cactus_index_t index =
17
+ cactus_index_init(indexPath.c_str(), embeddingDim);
18
+
19
+ if (!index) {
20
+ throw std::runtime_error("Cactus index init failed: " +
21
+ std::string(cactus_get_last_error()));
22
+ }
23
+
24
+ this->_index = index;
25
+ this->_embeddingDim = static_cast<size_t>(embeddingDim);
26
+ });
27
+ }
28
+
29
+ std::shared_ptr<Promise<void>> HybridCactusIndex::add(
30
+ const std::vector<double> &ids, const std::vector<std::string> &documents,
31
+ const std::vector<std::vector<double>> &embeddings,
32
+ const std::optional<std::vector<std::string>> &metadatas) {
33
+ return Promise<void>::async([this, ids, documents, embeddings,
34
+ metadatas]() -> void {
35
+ std::lock_guard<std::mutex> lock(this->_indexMutex);
36
+
37
+ if (!this->_index) {
38
+ throw std::runtime_error("Cactus index is not initialized");
39
+ }
40
+
41
+ const size_t count = ids.size();
42
+ if (documents.size() != count || embeddings.size() != count) {
43
+ throw std::runtime_error(
44
+ "ids, documents, and embeddings must have the same length");
45
+ }
46
+
47
+ if (metadatas.has_value() && metadatas->size() != count) {
48
+ throw std::runtime_error(
49
+ "metadatas must have the same length as other vectors");
50
+ }
51
+
52
+ std::vector<int> intIds;
53
+ intIds.reserve(count);
54
+ for (size_t i = 0; i < count; ++i) {
55
+ intIds.emplace_back(static_cast<int>(ids[i]));
56
+ }
57
+
58
+ std::vector<const char *> documentPtrs;
59
+ documentPtrs.reserve(count);
60
+ for (size_t i = 0; i < count; ++i) {
61
+ documentPtrs.emplace_back(documents[i].c_str());
62
+ }
63
+
64
+ std::vector<std::vector<float>> embeddingsFloat(count);
65
+ std::vector<const float *> embeddingPtrs;
66
+ embeddingPtrs.reserve(count);
67
+ for (size_t i = 0; i < count; ++i) {
68
+ embeddingsFloat[i].resize(embeddings[i].size());
69
+ for (size_t j = 0; j < embeddings[i].size(); ++j) {
70
+ embeddingsFloat[i][j] = static_cast<float>(embeddings[i][j]);
71
+ }
72
+ embeddingPtrs.emplace_back(embeddingsFloat[i].data());
73
+ }
74
+
75
+ int result;
76
+ if (metadatas.has_value()) {
77
+ std::vector<const char *> metadataPtrs;
78
+ metadataPtrs.reserve(count);
79
+ for (size_t i = 0; i < count; ++i) {
80
+ metadataPtrs.emplace_back((*metadatas)[i].c_str());
81
+ }
82
+ result = cactus_index_add(
83
+ this->_index, intIds.data(), documentPtrs.data(), metadataPtrs.data(),
84
+ embeddingPtrs.data(), count, this->_embeddingDim);
85
+ } else {
86
+ result = cactus_index_add(
87
+ this->_index, intIds.data(), documentPtrs.data(), nullptr,
88
+ embeddingPtrs.data(), count, this->_embeddingDim);
89
+ }
90
+
91
+ if (result < 0) {
92
+ throw std::runtime_error("Cactus index add failed: " +
93
+ std::string(cactus_get_last_error()));
94
+ }
95
+ });
96
+ }
97
+
98
+ std::shared_ptr<Promise<void>>
99
+ HybridCactusIndex::_delete(const std::vector<double> &ids) {
100
+ return Promise<void>::async([this, ids]() -> void {
101
+ std::lock_guard<std::mutex> lock(this->_indexMutex);
102
+
103
+ if (!this->_index) {
104
+ throw std::runtime_error("Cactus index is not initialized");
105
+ }
106
+
107
+ std::vector<int> intIds;
108
+ intIds.reserve(ids.size());
109
+ for (size_t i = 0; i < ids.size(); ++i) {
110
+ intIds.emplace_back(static_cast<int>(ids[i]));
111
+ }
112
+
113
+ int result = cactus_index_delete(this->_index, intIds.data(), ids.size());
114
+
115
+ if (result < 0) {
116
+ throw std::runtime_error("Cactus index delete failed: " +
117
+ std::string(cactus_get_last_error()));
118
+ }
119
+ });
120
+ }
121
+
122
+ std::shared_ptr<Promise<CactusIndexGetResult>>
123
+ HybridCactusIndex::get(const std::vector<double> &ids) {
124
+ return Promise<CactusIndexGetResult>::async([this,
125
+ ids]() -> CactusIndexGetResult {
126
+ std::lock_guard<std::mutex> lock(this->_indexMutex);
127
+
128
+ if (!this->_index) {
129
+ throw std::runtime_error("Cactus index is not initialized");
130
+ }
131
+
132
+ const size_t count = ids.size();
133
+
134
+ std::vector<int> intIds;
135
+ intIds.reserve(count);
136
+ for (size_t i = 0; i < count; ++i) {
137
+ intIds.emplace_back(static_cast<int>(ids[i]));
138
+ }
139
+
140
+ std::vector<std::unique_ptr<char[]>> documentBuffers;
141
+ documentBuffers.reserve(count);
142
+ std::vector<std::unique_ptr<char[]>> metadataBuffers;
143
+ metadataBuffers.reserve(count);
144
+ std::vector<std::unique_ptr<float[]>> embeddingBuffers;
145
+ embeddingBuffers.reserve(count);
146
+
147
+ const size_t maxStringSize = 65535;
148
+ std::vector<size_t> documentBufferSizes(count, maxStringSize);
149
+ std::vector<size_t> metadataBufferSizes(count, maxStringSize);
150
+ std::vector<size_t> embeddingBufferSizes(count, this->_embeddingDim);
151
+
152
+ std::vector<char *> documentPtrs;
153
+ documentPtrs.reserve(count);
154
+ std::vector<char *> metadataPtrs;
155
+ metadataPtrs.reserve(count);
156
+ std::vector<float *> embeddingPtrs;
157
+ embeddingPtrs.reserve(count);
158
+
159
+ for (size_t i = 0; i < count; ++i) {
160
+ documentBuffers.emplace_back(std::make_unique<char[]>(maxStringSize));
161
+ documentPtrs.emplace_back(documentBuffers[i].get());
162
+
163
+ metadataBuffers.emplace_back(std::make_unique<char[]>(maxStringSize));
164
+ metadataPtrs.emplace_back(metadataBuffers[i].get());
165
+
166
+ embeddingBuffers.emplace_back(
167
+ std::make_unique<float[]>(this->_embeddingDim));
168
+ embeddingPtrs.emplace_back(embeddingBuffers[i].get());
169
+ }
170
+
171
+ int result =
172
+ cactus_index_get(this->_index, intIds.data(), count,
173
+ documentPtrs.data(), documentBufferSizes.data(),
174
+ metadataPtrs.data(), metadataBufferSizes.data(),
175
+ embeddingPtrs.data(), embeddingBufferSizes.data());
176
+
177
+ if (result < 0) {
178
+ throw std::runtime_error("Cactus index get failed: " +
179
+ std::string(cactus_get_last_error()));
180
+ }
181
+
182
+ CactusIndexGetResult resultObj;
183
+ resultObj.documents.reserve(count);
184
+ resultObj.metadatas.reserve(count);
185
+ resultObj.embeddings = std::vector<std::vector<double>>(count);
186
+
187
+ for (size_t i = 0; i < count; ++i) {
188
+ resultObj.documents.emplace_back(std::string(documentBuffers[i].get()));
189
+ resultObj.metadatas.emplace_back(std::string(metadataBuffers[i].get()));
190
+
191
+ resultObj.embeddings[i].reserve(this->_embeddingDim);
192
+ for (size_t j = 0; j < this->_embeddingDim; ++j) {
193
+ resultObj.embeddings[i].emplace_back(
194
+ static_cast<double>(embeddingBuffers[i].get()[j]));
195
+ }
196
+ }
197
+
198
+ return resultObj;
199
+ });
200
+ }
201
+
202
+ std::shared_ptr<Promise<CactusIndexQueryResult>>
203
+ HybridCactusIndex::query(const std::vector<std::vector<double>> &embeddings,
204
+ const std::optional<std::string> &optionsJson) {
205
+ return Promise<CactusIndexQueryResult>::async(
206
+ [this, embeddings, optionsJson]() -> CactusIndexQueryResult {
207
+ std::lock_guard<std::mutex> lock(this->_indexMutex);
208
+
209
+ if (!this->_index) {
210
+ throw std::runtime_error("Cactus index is not initialized");
211
+ }
212
+
213
+ const size_t count = embeddings.size();
214
+
215
+ std::vector<std::vector<float>> embeddingsFloat(count);
216
+ std::vector<const float *> embeddingPtrs;
217
+ embeddingPtrs.reserve(count);
218
+ for (size_t i = 0; i < count; ++i) {
219
+ embeddingsFloat[i].resize(embeddings[i].size());
220
+ for (size_t j = 0; j < embeddings[i].size(); ++j) {
221
+ embeddingsFloat[i][j] = static_cast<float>(embeddings[i][j]);
222
+ }
223
+ embeddingPtrs.emplace_back(embeddingsFloat[i].data());
224
+ }
225
+
226
+ size_t maxResults = 10;
227
+ if (optionsJson.has_value()) {
228
+ const std::string &json = *optionsJson;
229
+ size_t pos = json.find("\"top_k\"");
230
+ if (pos != std::string::npos) {
231
+ size_t colonPos = json.find(':', pos);
232
+ if (colonPos != std::string::npos) {
233
+ size_t numStart = json.find_first_of("0123456789", colonPos);
234
+ if (numStart != std::string::npos) {
235
+ size_t numEnd = json.find_first_not_of("0123456789", numStart);
236
+ std::string numStr = json.substr(numStart, numEnd - numStart);
237
+ maxResults = std::stoul(numStr);
238
+ }
239
+ }
240
+ }
241
+ }
242
+
243
+ std::vector<size_t> idBufferSizes(count, maxResults);
244
+ std::vector<size_t> scoreBufferSizes(count, maxResults);
245
+
246
+ std::vector<std::unique_ptr<int[]>> idBuffers;
247
+ idBuffers.reserve(count);
248
+ std::vector<std::unique_ptr<float[]>> scoreBuffers;
249
+ scoreBuffers.reserve(count);
250
+
251
+ std::vector<int *> idPtrs;
252
+ idPtrs.reserve(count);
253
+ std::vector<float *> scorePtrs;
254
+ scorePtrs.reserve(count);
255
+
256
+ for (size_t i = 0; i < count; ++i) {
257
+ idBuffers.emplace_back(std::make_unique<int[]>(maxResults));
258
+ idPtrs.emplace_back(idBuffers[i].get());
259
+
260
+ scoreBuffers.emplace_back(std::make_unique<float[]>(maxResults));
261
+ scorePtrs.emplace_back(scoreBuffers[i].get());
262
+ }
263
+
264
+ int result = cactus_index_query(
265
+ this->_index, embeddingPtrs.data(), count, this->_embeddingDim,
266
+ optionsJson ? optionsJson->c_str() : nullptr, idPtrs.data(),
267
+ idBufferSizes.data(), scorePtrs.data(), scoreBufferSizes.data());
268
+
269
+ if (result < 0) {
270
+ throw std::runtime_error("Cactus index query failed: " +
271
+ std::string(cactus_get_last_error()));
272
+ }
273
+
274
+ CactusIndexQueryResult resultObj;
275
+ resultObj.ids = std::vector<std::vector<double>>(count);
276
+ resultObj.scores = std::vector<std::vector<double>>(count);
277
+
278
+ for (size_t i = 0; i < count; ++i) {
279
+ const size_t resultCount = idBufferSizes[i];
280
+ resultObj.ids[i].reserve(resultCount);
281
+ resultObj.scores[i].reserve(resultCount);
282
+
283
+ for (size_t j = 0; j < resultCount; ++j) {
284
+ resultObj.ids[i].emplace_back(
285
+ static_cast<double>(idBuffers[i].get()[j]));
286
+ resultObj.scores[i].emplace_back(
287
+ static_cast<double>(scoreBuffers[i].get()[j]));
288
+ }
289
+ }
290
+
291
+ return resultObj;
292
+ });
293
+ }
294
+
295
+ std::shared_ptr<Promise<void>> HybridCactusIndex::compact() {
296
+ return Promise<void>::async([this]() -> void {
297
+ std::lock_guard<std::mutex> lock(this->_indexMutex);
298
+
299
+ if (!this->_index) {
300
+ throw std::runtime_error("Cactus index is not initialized");
301
+ }
302
+
303
+ int result = cactus_index_compact(this->_index);
304
+
305
+ if (result < 0) {
306
+ throw std::runtime_error("Cactus index compact failed: " +
307
+ std::string(cactus_get_last_error()));
308
+ }
309
+ });
310
+ }
311
+
312
+ std::shared_ptr<Promise<void>> HybridCactusIndex::destroy() {
313
+ return Promise<void>::async([this]() -> void {
314
+ std::lock_guard<std::mutex> lock(this->_indexMutex);
315
+
316
+ if (!this->_index) {
317
+ throw std::runtime_error("Cactus index is not initialized");
318
+ }
319
+
320
+ cactus_index_destroy(this->_index);
321
+ this->_index = nullptr;
322
+ });
323
+ }
324
+
325
+ } // namespace margelo::nitro::cactus
@@ -0,0 +1,43 @@
1
+ #pragma once
2
+ #include "HybridCactusIndexSpec.hpp"
3
+
4
+ #include "cactus_ffi.h"
5
+
6
+ #include <mutex>
7
+
8
+ namespace margelo::nitro::cactus {
9
+
10
+ class HybridCactusIndex : public HybridCactusIndexSpec {
11
+ public:
12
+ HybridCactusIndex();
13
+
14
+ std::shared_ptr<Promise<void>> init(const std::string &indexPath,
15
+ double embeddingDim) override;
16
+
17
+ std::shared_ptr<Promise<void>>
18
+ add(const std::vector<double> &ids, const std::vector<std::string> &documents,
19
+ const std::vector<std::vector<double>> &embeddings,
20
+ const std::optional<std::vector<std::string>> &metadatas) override;
21
+
22
+ std::shared_ptr<Promise<void>>
23
+ _delete(const std::vector<double> &ids) override;
24
+
25
+ std::shared_ptr<Promise<CactusIndexGetResult>>
26
+ get(const std::vector<double> &ids) override;
27
+
28
+ std::shared_ptr<Promise<CactusIndexQueryResult>>
29
+ query(const std::vector<std::vector<double>> &embeddings,
30
+ const std::optional<std::string> &optionsJson) override;
31
+
32
+ std::shared_ptr<Promise<void>> compact() override;
33
+
34
+ std::shared_ptr<Promise<void>> destroy() override;
35
+
36
+ private:
37
+ cactus_index_t _index = nullptr;
38
+ size_t _embeddingDim;
39
+
40
+ std::mutex _indexMutex;
41
+ };
42
+
43
+ } // namespace margelo::nitro::cactus
@@ -23,12 +23,12 @@ HybridCactusUtil::registerApp(const std::string &encryptedData) {
23
23
  }
24
24
 
25
25
  std::shared_ptr<Promise<std::optional<std::string>>>
26
- HybridCactusUtil::getDeviceId() {
26
+ HybridCactusUtil::getDeviceId(const std::optional<std::string> &token) {
27
27
  return Promise<std::optional<std::string>>::async(
28
- [this]() -> std::optional<std::string> {
28
+ [this, token]() -> std::optional<std::string> {
29
29
  std::lock_guard<std::mutex> lock(this->_mutex);
30
30
 
31
- const char *deviceId = get_device_id();
31
+ const char *deviceId = get_device_id(token ? token->c_str() : nullptr);
32
32
  return deviceId ? std::optional<std::string>(deviceId) : std::nullopt;
33
33
  });
34
34
  }
@@ -14,7 +14,8 @@ public:
14
14
  std::shared_ptr<Promise<std::string>>
15
15
  registerApp(const std::string &encryptedData) override;
16
16
 
17
- std::shared_ptr<Promise<std::optional<std::string>>> getDeviceId() override;
17
+ std::shared_ptr<Promise<std::optional<std::string>>>
18
+ getDeviceId(const std::optional<std::string> &token) override;
18
19
 
19
20
  std::shared_ptr<Promise<void>>
20
21
  setAndroidDataDirectory(const std::string &dataDir) override;
package/cpp/cactus_ffi.h CHANGED
@@ -3,6 +3,7 @@
3
3
 
4
4
  #include <stddef.h>
5
5
  #include <stdint.h>
6
+ #include <stdbool.h>
6
7
 
7
8
  #if __GNUC__ >= 4
8
9
  #define CACTUS_FFI_EXPORT __attribute__ ((visibility ("default")))
@@ -33,6 +34,26 @@ CACTUS_FFI_EXPORT int cactus_complete(
33
34
  void* user_data
34
35
  );
35
36
 
37
+ CACTUS_FFI_EXPORT int cactus_tokenize(
38
+ cactus_model_t model,
39
+ const char* text,
40
+ uint32_t* token_buffer,
41
+ size_t token_buffer_len,
42
+ size_t* out_token_len
43
+ );
44
+
45
+ CACTUS_FFI_EXPORT int cactus_score_window(
46
+ cactus_model_t model,
47
+ const uint32_t* tokens,
48
+ size_t token_len,
49
+ size_t start,
50
+ size_t end,
51
+ size_t context,
52
+ char* response_buffer,
53
+ size_t buffer_size
54
+ );
55
+
56
+
36
57
  CACTUS_FFI_EXPORT int cactus_transcribe(
37
58
  cactus_model_t model,
38
59
  const char* audio_file_path,
@@ -41,16 +62,43 @@ CACTUS_FFI_EXPORT int cactus_transcribe(
41
62
  size_t buffer_size,
42
63
  const char* options_json,
43
64
  cactus_token_callback callback,
44
- void* user_data
65
+ void* user_data,
66
+ const uint8_t* pcm_buffer,
67
+ size_t pcm_buffer_size
45
68
  );
46
69
 
70
+ typedef void* cactus_stream_transcribe_t;
71
+
72
+ CACTUS_FFI_EXPORT cactus_stream_transcribe_t cactus_stream_transcribe_init(cactus_model_t model);
73
+
74
+ CACTUS_FFI_EXPORT int cactus_stream_transcribe_insert(
75
+ cactus_stream_transcribe_t stream,
76
+ const uint8_t* pcm_buffer,
77
+ size_t pcm_buffer_size
78
+ );
79
+
80
+ CACTUS_FFI_EXPORT int cactus_stream_transcribe_process(
81
+ cactus_stream_transcribe_t stream,
82
+ char* response_buffer,
83
+ size_t buffer_size,
84
+ const char* options_json
85
+ );
86
+
87
+ CACTUS_FFI_EXPORT int cactus_stream_transcribe_finalize(
88
+ cactus_stream_transcribe_t stream,
89
+ char* response_buffer,
90
+ size_t buffer_size
91
+ );
92
+
93
+ CACTUS_FFI_EXPORT void cactus_stream_transcribe_destroy(cactus_stream_transcribe_t stream);
47
94
 
48
95
  CACTUS_FFI_EXPORT int cactus_embed(
49
96
  cactus_model_t model,
50
97
  const char* text,
51
98
  float* embeddings_buffer,
52
99
  size_t buffer_size,
53
- size_t* embedding_dim
100
+ size_t* embedding_dim,
101
+ bool normalize
54
102
  );
55
103
 
56
104
  CACTUS_FFI_EXPORT int cactus_image_embed(
@@ -75,6 +123,63 @@ CACTUS_FFI_EXPORT void cactus_stop(cactus_model_t model);
75
123
 
76
124
  CACTUS_FFI_EXPORT void cactus_destroy(cactus_model_t model);
77
125
 
126
+ CACTUS_FFI_EXPORT const char* cactus_get_last_error(void);
127
+
128
+ CACTUS_FFI_EXPORT void cactus_set_telemetry_token(const char* token);
129
+
130
+ CACTUS_FFI_EXPORT void cactus_set_pro_key(const char* pro_key);
131
+
132
+ typedef void* cactus_index_t;
133
+
134
+ CACTUS_FFI_EXPORT cactus_index_t cactus_index_init(
135
+ const char* index_dir,
136
+ size_t embedding_dim
137
+ );
138
+
139
+ CACTUS_FFI_EXPORT int cactus_index_add(
140
+ cactus_index_t index,
141
+ const int* ids,
142
+ const char** documents,
143
+ const char** metadatas,
144
+ const float** embeddings,
145
+ size_t count,
146
+ size_t embedding_dim
147
+ );
148
+
149
+ CACTUS_FFI_EXPORT int cactus_index_delete(
150
+ cactus_index_t index,
151
+ const int* ids,
152
+ size_t ids_count
153
+ );
154
+
155
+ CACTUS_FFI_EXPORT int cactus_index_get(
156
+ cactus_index_t index,
157
+ const int* ids,
158
+ size_t ids_count,
159
+ char** document_buffers,
160
+ size_t* document_buffer_sizes,
161
+ char** metadata_buffers,
162
+ size_t* metadata_buffer_sizes,
163
+ float** embedding_buffers,
164
+ size_t* embedding_buffer_sizes
165
+ );
166
+
167
+ CACTUS_FFI_EXPORT int cactus_index_query(
168
+ cactus_index_t index,
169
+ const float** embeddings,
170
+ size_t embeddings_count,
171
+ size_t embedding_dim,
172
+ const char* options_json,
173
+ int** id_buffers,
174
+ size_t* id_buffer_sizes,
175
+ float** score_buffers,
176
+ size_t* score_buffer_sizes
177
+ );
178
+
179
+ CACTUS_FFI_EXPORT int cactus_index_compact(cactus_index_t index);
180
+
181
+ CACTUS_FFI_EXPORT void cactus_index_destroy(cactus_index_t index);
182
+
78
183
  #ifdef __cplusplus
79
184
  }
80
185
  #endif
package/cpp/cactus_util.h CHANGED
@@ -7,7 +7,7 @@ extern "C" {
7
7
 
8
8
  const char* register_app(const char* encrypted_data);
9
9
 
10
- const char* get_device_id();
10
+ const char* get_device_id(const char* current_token);
11
11
 
12
12
  // Helper function to free memory allocated by register_app
13
13
  void free_string(const char* str);
@@ -133,7 +133,11 @@ class HybridCactusFileSystem: HybridCactusFileSystemSpec {
133
133
  try FileManager.default.removeItem(at: modelURL)
134
134
  }
135
135
  }
136
-
136
+
137
+ func getIndexPath(name: String) throws -> Promise<String> {
138
+ return Promise.async { try self.indexURL(name: name).path }
139
+ }
140
+
137
141
  private func cactusURL() throws -> URL {
138
142
  let documentsURL = try FileManager.default.url(
139
143
  for: .documentDirectory,
@@ -156,7 +160,24 @@ class HybridCactusFileSystem: HybridCactusFileSystemSpec {
156
160
 
157
161
  private func modelURL(model: String) throws -> URL {
158
162
  let cactusURL = try self.cactusURL()
159
- return cactusURL.appendingPathComponent("models/\(model)")
163
+ let modelsURL = cactusURL.appendingPathComponent("models", isDirectory: true)
164
+
165
+ if !FileManager.default.fileExists(atPath: modelsURL.path) {
166
+ try FileManager.default.createDirectory(at: modelsURL, withIntermediateDirectories: true)
167
+ }
168
+
169
+ return modelsURL.appendingPathComponent(model)
170
+ }
171
+
172
+ private func indexURL(name: String) throws -> URL {
173
+ let cactusURL = try self.cactusURL()
174
+ let finalURL = cactusURL.appendingPathComponent("indexes/\(name)", isDirectory: true)
175
+
176
+ if !FileManager.default.fileExists(atPath: finalURL.path) {
177
+ try FileManager.default.createDirectory(at: finalURL, withIntermediateDirectories: true)
178
+ }
179
+
180
+ return finalURL
160
181
  }
161
182
  }
162
183
 
@@ -7,5 +7,7 @@
7
7
  #include "engine/engine.h"
8
8
  #include "models/model.h"
9
9
  #include "ffi/cactus_ffi.h"
10
+ #include "ffi/cactus_telemetry.h"
11
+ #include "npu/npu.h"
10
12
 
11
13
  #endif // CACTUS_H