cactus-react-native 1.5.0 → 1.10.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 (221) hide show
  1. package/Cactus.podspec +1 -1
  2. package/README.md +347 -241
  3. package/android/CMakeLists.txt +24 -5
  4. package/android/src/main/jniLibs/arm64-v8a/libcactus.a +0 -0
  5. package/android/src/main/jniLibs/arm64-v8a/libcurl.a +0 -0
  6. package/android/src/main/jniLibs/arm64-v8a/libmbedcrypto.a +0 -0
  7. package/android/src/main/jniLibs/arm64-v8a/libmbedtls.a +0 -0
  8. package/android/src/main/jniLibs/arm64-v8a/libmbedx509.a +0 -0
  9. package/cpp/HybridCactus.cpp +197 -117
  10. package/cpp/HybridCactus.hpp +18 -9
  11. package/cpp/cactus_ffi.h +66 -42
  12. package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/cactus.h +0 -1
  13. package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/cactus_cloud.h +48 -0
  14. package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/cactus_ffi.h +66 -42
  15. package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/cactus_utils.h +568 -135
  16. package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/engine.h +148 -17
  17. package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/graph.h +145 -36
  18. package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/kernel.h +187 -6
  19. package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/kernel_utils.h +49 -149
  20. package/ios/cactus.xcframework/ios-arm64/cactus.framework/Info.plist +0 -0
  21. package/ios/cactus.xcframework/ios-arm64/cactus.framework/cactus +0 -0
  22. package/ios/cactus.xcframework/ios-arm64-simulator/cactus.framework/Headers/cactus.h +0 -1
  23. package/ios/cactus.xcframework/ios-arm64-simulator/cactus.framework/Headers/cactus_cloud.h +48 -0
  24. package/ios/cactus.xcframework/ios-arm64-simulator/cactus.framework/Headers/cactus_ffi.h +66 -42
  25. package/ios/cactus.xcframework/ios-arm64-simulator/cactus.framework/Headers/cactus_utils.h +568 -135
  26. package/ios/cactus.xcframework/ios-arm64-simulator/cactus.framework/Headers/engine.h +148 -17
  27. package/ios/cactus.xcframework/ios-arm64-simulator/cactus.framework/Headers/graph.h +145 -36
  28. package/ios/cactus.xcframework/ios-arm64-simulator/cactus.framework/Headers/kernel.h +187 -6
  29. package/ios/cactus.xcframework/ios-arm64-simulator/cactus.framework/Headers/kernel_utils.h +49 -149
  30. package/ios/cactus.xcframework/ios-arm64-simulator/cactus.framework/Info.plist +0 -0
  31. package/ios/cactus.xcframework/ios-arm64-simulator/cactus.framework/_CodeSignature/CodeResources +1 -1
  32. package/ios/cactus.xcframework/ios-arm64-simulator/cactus.framework/cactus +0 -0
  33. package/lib/module/classes/CactusLM.js +16 -49
  34. package/lib/module/classes/CactusLM.js.map +1 -1
  35. package/lib/module/classes/CactusSTT.js +41 -75
  36. package/lib/module/classes/CactusSTT.js.map +1 -1
  37. package/lib/module/classes/CactusVAD.js +95 -0
  38. package/lib/module/classes/CactusVAD.js.map +1 -0
  39. package/lib/module/hooks/useCactusLM.js +10 -11
  40. package/lib/module/hooks/useCactusLM.js.map +1 -1
  41. package/lib/module/hooks/useCactusSTT.js +23 -62
  42. package/lib/module/hooks/useCactusSTT.js.map +1 -1
  43. package/lib/module/hooks/useCactusVAD.js +171 -0
  44. package/lib/module/hooks/useCactusVAD.js.map +1 -0
  45. package/lib/module/index.js +2 -3
  46. package/lib/module/index.js.map +1 -1
  47. package/lib/module/modelRegistry.js +52 -0
  48. package/lib/module/modelRegistry.js.map +1 -0
  49. package/lib/module/native/Cactus.js +103 -23
  50. package/lib/module/native/Cactus.js.map +1 -1
  51. package/lib/module/native/CactusIndex.js.map +1 -1
  52. package/lib/module/native/index.js +0 -3
  53. package/lib/module/native/index.js.map +1 -1
  54. package/lib/module/types/CactusVAD.js +4 -0
  55. package/lib/module/{specs/CactusUtil.nitro.js.map → types/CactusVAD.js.map} +1 -1
  56. package/lib/typescript/src/classes/CactusLM.d.ts +5 -7
  57. package/lib/typescript/src/classes/CactusLM.d.ts.map +1 -1
  58. package/lib/typescript/src/classes/CactusSTT.d.ts +9 -12
  59. package/lib/typescript/src/classes/CactusSTT.d.ts.map +1 -1
  60. package/lib/typescript/src/classes/CactusVAD.d.ts +20 -0
  61. package/lib/typescript/src/classes/CactusVAD.d.ts.map +1 -0
  62. package/lib/typescript/src/hooks/useCactusLM.d.ts +2 -2
  63. package/lib/typescript/src/hooks/useCactusLM.d.ts.map +1 -1
  64. package/lib/typescript/src/hooks/useCactusSTT.d.ts +6 -8
  65. package/lib/typescript/src/hooks/useCactusSTT.d.ts.map +1 -1
  66. package/lib/typescript/src/hooks/useCactusVAD.d.ts +15 -0
  67. package/lib/typescript/src/hooks/useCactusVAD.d.ts.map +1 -0
  68. package/lib/typescript/src/index.d.ts +7 -5
  69. package/lib/typescript/src/index.d.ts.map +1 -1
  70. package/lib/typescript/src/modelRegistry.d.ts +5 -0
  71. package/lib/typescript/src/modelRegistry.d.ts.map +1 -0
  72. package/lib/typescript/src/native/Cactus.d.ts +13 -11
  73. package/lib/typescript/src/native/Cactus.d.ts.map +1 -1
  74. package/lib/typescript/src/native/CactusIndex.d.ts +2 -2
  75. package/lib/typescript/src/native/CactusIndex.d.ts.map +1 -1
  76. package/lib/typescript/src/native/index.d.ts +0 -3
  77. package/lib/typescript/src/native/index.d.ts.map +1 -1
  78. package/lib/typescript/src/specs/Cactus.nitro.d.ts +7 -6
  79. package/lib/typescript/src/specs/Cactus.nitro.d.ts.map +1 -1
  80. package/lib/typescript/src/types/CactusIndex.d.ts +2 -2
  81. package/lib/typescript/src/types/CactusIndex.d.ts.map +1 -1
  82. package/lib/typescript/src/types/CactusLM.d.ts +19 -11
  83. package/lib/typescript/src/types/CactusLM.d.ts.map +1 -1
  84. package/lib/typescript/src/types/CactusSTT.d.ts +44 -12
  85. package/lib/typescript/src/types/CactusSTT.d.ts.map +1 -1
  86. package/lib/typescript/src/types/CactusVAD.d.ts +34 -0
  87. package/lib/typescript/src/types/CactusVAD.d.ts.map +1 -0
  88. package/lib/typescript/src/types/common.d.ts +1 -6
  89. package/lib/typescript/src/types/common.d.ts.map +1 -1
  90. package/nitro.json +0 -11
  91. package/nitrogen/generated/android/cactus+autolinking.cmake +0 -5
  92. package/nitrogen/generated/android/cactusOnLoad.cpp +0 -30
  93. package/nitrogen/generated/ios/Cactus-Swift-Cxx-Bridge.cpp +0 -50
  94. package/nitrogen/generated/ios/Cactus-Swift-Cxx-Bridge.hpp +9 -147
  95. package/nitrogen/generated/ios/Cactus-Swift-Cxx-Umbrella.hpp +0 -13
  96. package/nitrogen/generated/ios/CactusAutolinking.mm +0 -26
  97. package/nitrogen/generated/ios/CactusAutolinking.swift +0 -30
  98. package/nitrogen/generated/shared/c++/HybridCactusSpec.cpp +5 -4
  99. package/nitrogen/generated/shared/c++/HybridCactusSpec.hpp +7 -6
  100. package/package.json +3 -3
  101. package/src/classes/CactusLM.ts +18 -65
  102. package/src/classes/CactusSTT.ts +52 -90
  103. package/src/classes/CactusVAD.ts +129 -0
  104. package/src/hooks/useCactusLM.ts +14 -17
  105. package/src/hooks/useCactusSTT.ts +47 -98
  106. package/src/hooks/useCactusVAD.ts +215 -0
  107. package/src/index.tsx +21 -12
  108. package/src/modelRegistry.ts +65 -0
  109. package/src/native/Cactus.ts +131 -38
  110. package/src/native/CactusIndex.ts +2 -2
  111. package/src/native/index.ts +0 -3
  112. package/src/specs/Cactus.nitro.ts +16 -7
  113. package/src/types/CactusIndex.ts +2 -2
  114. package/src/types/CactusLM.ts +19 -11
  115. package/src/types/CactusSTT.ts +47 -13
  116. package/src/types/CactusVAD.ts +39 -0
  117. package/src/types/common.ts +1 -6
  118. package/android/src/main/java/com/margelo/nitro/cactus/HybridCactusCrypto.kt +0 -46
  119. package/android/src/main/java/com/margelo/nitro/cactus/HybridCactusDeviceInfo.kt +0 -27
  120. package/android/src/main/jniLibs/arm64-v8a/libcactus_util.a +0 -0
  121. package/cpp/HybridCactusUtil.cpp +0 -47
  122. package/cpp/HybridCactusUtil.hpp +0 -27
  123. package/cpp/cactus_util.h +0 -25
  124. package/ios/HybridCactusCrypto.swift +0 -37
  125. package/ios/HybridCactusDeviceInfo.swift +0 -32
  126. package/ios/cactus.xcframework/ios-arm64/cactus.framework/Headers/cactus_telemetry.h +0 -656
  127. package/ios/cactus.xcframework/ios-arm64-simulator/cactus.framework/Headers/cactus_telemetry.h +0 -656
  128. package/ios/cactus_util.xcframework/Info.plist +0 -39
  129. package/ios/cactus_util.xcframework/ios-arm64/cactus_util.framework/Headers/cactus_util.h +0 -25
  130. package/ios/cactus_util.xcframework/ios-arm64/cactus_util.framework/Headers/database.h +0 -27
  131. package/ios/cactus_util.xcframework/ios-arm64/cactus_util.framework/Headers/ios_utils.h +0 -10
  132. package/ios/cactus_util.xcframework/ios-arm64/cactus_util.framework/Headers/logging.h +0 -25
  133. package/ios/cactus_util.xcframework/ios-arm64/cactus_util.framework/Info.plist +0 -0
  134. package/ios/cactus_util.xcframework/ios-arm64/cactus_util.framework/cactus_util +0 -0
  135. package/ios/cactus_util.xcframework/ios-arm64-simulator/cactus_util.framework/Headers/cactus_util.h +0 -25
  136. package/ios/cactus_util.xcframework/ios-arm64-simulator/cactus_util.framework/Headers/database.h +0 -27
  137. package/ios/cactus_util.xcframework/ios-arm64-simulator/cactus_util.framework/Headers/ios_utils.h +0 -10
  138. package/ios/cactus_util.xcframework/ios-arm64-simulator/cactus_util.framework/Headers/logging.h +0 -25
  139. package/ios/cactus_util.xcframework/ios-arm64-simulator/cactus_util.framework/Info.plist +0 -0
  140. package/ios/cactus_util.xcframework/ios-arm64-simulator/cactus_util.framework/_CodeSignature/CodeResources +0 -135
  141. package/ios/cactus_util.xcframework/ios-arm64-simulator/cactus_util.framework/cactus_util +0 -0
  142. package/lib/module/api/Database.js +0 -45
  143. package/lib/module/api/Database.js.map +0 -1
  144. package/lib/module/api/RemoteLM.js +0 -201
  145. package/lib/module/api/RemoteLM.js.map +0 -1
  146. package/lib/module/config/CactusConfig.js +0 -12
  147. package/lib/module/config/CactusConfig.js.map +0 -1
  148. package/lib/module/models.js +0 -336
  149. package/lib/module/models.js.map +0 -1
  150. package/lib/module/native/CactusCrypto.js +0 -10
  151. package/lib/module/native/CactusCrypto.js.map +0 -1
  152. package/lib/module/native/CactusDeviceInfo.js +0 -13
  153. package/lib/module/native/CactusDeviceInfo.js.map +0 -1
  154. package/lib/module/native/CactusUtil.js +0 -36
  155. package/lib/module/native/CactusUtil.js.map +0 -1
  156. package/lib/module/specs/CactusCrypto.nitro.js +0 -4
  157. package/lib/module/specs/CactusCrypto.nitro.js.map +0 -1
  158. package/lib/module/specs/CactusDeviceInfo.nitro.js +0 -4
  159. package/lib/module/specs/CactusDeviceInfo.nitro.js.map +0 -1
  160. package/lib/module/specs/CactusUtil.nitro.js +0 -4
  161. package/lib/module/telemetry/Telemetry.js +0 -154
  162. package/lib/module/telemetry/Telemetry.js.map +0 -1
  163. package/lib/typescript/src/api/Database.d.ts +0 -12
  164. package/lib/typescript/src/api/Database.d.ts.map +0 -1
  165. package/lib/typescript/src/api/RemoteLM.d.ts +0 -14
  166. package/lib/typescript/src/api/RemoteLM.d.ts.map +0 -1
  167. package/lib/typescript/src/config/CactusConfig.d.ts +0 -7
  168. package/lib/typescript/src/config/CactusConfig.d.ts.map +0 -1
  169. package/lib/typescript/src/models.d.ts +0 -6
  170. package/lib/typescript/src/models.d.ts.map +0 -1
  171. package/lib/typescript/src/native/CactusCrypto.d.ts +0 -5
  172. package/lib/typescript/src/native/CactusCrypto.d.ts.map +0 -1
  173. package/lib/typescript/src/native/CactusDeviceInfo.d.ts +0 -7
  174. package/lib/typescript/src/native/CactusDeviceInfo.d.ts.map +0 -1
  175. package/lib/typescript/src/native/CactusUtil.d.ts +0 -6
  176. package/lib/typescript/src/native/CactusUtil.d.ts.map +0 -1
  177. package/lib/typescript/src/specs/CactusCrypto.nitro.d.ts +0 -8
  178. package/lib/typescript/src/specs/CactusCrypto.nitro.d.ts.map +0 -1
  179. package/lib/typescript/src/specs/CactusDeviceInfo.nitro.d.ts +0 -16
  180. package/lib/typescript/src/specs/CactusDeviceInfo.nitro.d.ts.map +0 -1
  181. package/lib/typescript/src/specs/CactusUtil.nitro.d.ts +0 -10
  182. package/lib/typescript/src/specs/CactusUtil.nitro.d.ts.map +0 -1
  183. package/lib/typescript/src/telemetry/Telemetry.d.ts +0 -34
  184. package/lib/typescript/src/telemetry/Telemetry.d.ts.map +0 -1
  185. package/nitrogen/generated/android/c++/JDeviceInfo.hpp +0 -74
  186. package/nitrogen/generated/android/c++/JHybridCactusCryptoSpec.cpp +0 -65
  187. package/nitrogen/generated/android/c++/JHybridCactusCryptoSpec.hpp +0 -65
  188. package/nitrogen/generated/android/c++/JHybridCactusDeviceInfoSpec.cpp +0 -85
  189. package/nitrogen/generated/android/c++/JHybridCactusDeviceInfoSpec.hpp +0 -66
  190. package/nitrogen/generated/android/kotlin/com/margelo/nitro/cactus/DeviceInfo.kt +0 -50
  191. package/nitrogen/generated/android/kotlin/com/margelo/nitro/cactus/HybridCactusCryptoSpec.kt +0 -58
  192. package/nitrogen/generated/android/kotlin/com/margelo/nitro/cactus/HybridCactusDeviceInfoSpec.kt +0 -62
  193. package/nitrogen/generated/ios/c++/HybridCactusCryptoSpecSwift.cpp +0 -11
  194. package/nitrogen/generated/ios/c++/HybridCactusCryptoSpecSwift.hpp +0 -77
  195. package/nitrogen/generated/ios/c++/HybridCactusDeviceInfoSpecSwift.cpp +0 -11
  196. package/nitrogen/generated/ios/c++/HybridCactusDeviceInfoSpecSwift.hpp +0 -88
  197. package/nitrogen/generated/ios/swift/DeviceInfo.swift +0 -98
  198. package/nitrogen/generated/ios/swift/Func_void_DeviceInfo.swift +0 -47
  199. package/nitrogen/generated/ios/swift/Func_void_std__optional_std__string_.swift +0 -54
  200. package/nitrogen/generated/ios/swift/HybridCactusCryptoSpec.swift +0 -57
  201. package/nitrogen/generated/ios/swift/HybridCactusCryptoSpec_cxx.swift +0 -139
  202. package/nitrogen/generated/ios/swift/HybridCactusDeviceInfoSpec.swift +0 -58
  203. package/nitrogen/generated/ios/swift/HybridCactusDeviceInfoSpec_cxx.swift +0 -164
  204. package/nitrogen/generated/shared/c++/DeviceInfo.hpp +0 -92
  205. package/nitrogen/generated/shared/c++/HybridCactusCryptoSpec.cpp +0 -21
  206. package/nitrogen/generated/shared/c++/HybridCactusCryptoSpec.hpp +0 -63
  207. package/nitrogen/generated/shared/c++/HybridCactusDeviceInfoSpec.cpp +0 -22
  208. package/nitrogen/generated/shared/c++/HybridCactusDeviceInfoSpec.hpp +0 -67
  209. package/nitrogen/generated/shared/c++/HybridCactusUtilSpec.cpp +0 -23
  210. package/nitrogen/generated/shared/c++/HybridCactusUtilSpec.hpp +0 -66
  211. package/src/api/Database.ts +0 -55
  212. package/src/api/RemoteLM.ts +0 -273
  213. package/src/config/CactusConfig.ts +0 -11
  214. package/src/models.ts +0 -344
  215. package/src/native/CactusCrypto.ts +0 -11
  216. package/src/native/CactusDeviceInfo.ts +0 -18
  217. package/src/native/CactusUtil.ts +0 -43
  218. package/src/specs/CactusCrypto.nitro.ts +0 -6
  219. package/src/specs/CactusDeviceInfo.nitro.ts +0 -15
  220. package/src/specs/CactusUtil.nitro.ts +0 -8
  221. package/src/telemetry/Telemetry.ts +0 -236
@@ -9,7 +9,6 @@ set(CMAKE_CXX_STANDARD 20)
9
9
  add_library(${PACKAGE_NAME} SHARED
10
10
  src/main/cpp/cpp-adapter.cpp
11
11
  ../cpp/HybridCactus.cpp
12
- ../cpp/HybridCactusUtil.cpp
13
12
  ../cpp/HybridCactusIndex.cpp
14
13
  )
15
14
 
@@ -18,9 +17,26 @@ set_target_properties(libcactus PROPERTIES
18
17
  IMPORTED_LOCATION "${CMAKE_CURRENT_LIST_DIR}/src/main/jniLibs/${ANDROID_ABI}/libcactus.a"
19
18
  )
20
19
 
21
- add_library(libcactus_util STATIC IMPORTED)
22
- set_target_properties(libcactus_util PROPERTIES
23
- IMPORTED_LOCATION "${CMAKE_CURRENT_LIST_DIR}/src/main/jniLibs/${ANDROID_ABI}/libcactus_util.a"
20
+ set(JNILIBS_DIR "${CMAKE_CURRENT_LIST_DIR}/src/main/jniLibs/${ANDROID_ABI}")
21
+
22
+ add_library(libcurl STATIC IMPORTED)
23
+ set_target_properties(libcurl PROPERTIES
24
+ IMPORTED_LOCATION "${JNILIBS_DIR}/libcurl.a"
25
+ )
26
+
27
+ add_library(libmbedtls STATIC IMPORTED)
28
+ set_target_properties(libmbedtls PROPERTIES
29
+ IMPORTED_LOCATION "${JNILIBS_DIR}/libmbedtls.a"
30
+ )
31
+
32
+ add_library(libmbedx509 STATIC IMPORTED)
33
+ set_target_properties(libmbedx509 PROPERTIES
34
+ IMPORTED_LOCATION "${JNILIBS_DIR}/libmbedx509.a"
35
+ )
36
+
37
+ add_library(libmbedcrypto STATIC IMPORTED)
38
+ set_target_properties(libmbedcrypto PROPERTIES
39
+ IMPORTED_LOCATION "${JNILIBS_DIR}/libmbedcrypto.a"
24
40
  )
25
41
 
26
42
  # Add Nitrogen specs :)
@@ -37,5 +53,8 @@ target_link_libraries(
37
53
  ${LOG_LIB}
38
54
  android # <-- Android core
39
55
  libcactus
40
- libcactus_util
56
+ libcurl
57
+ libmbedtls
58
+ libmbedx509
59
+ libmbedcrypto
41
60
  )
@@ -5,10 +5,11 @@ namespace margelo::nitro::cactus {
5
5
  HybridCactus::HybridCactus() : HybridObject(TAG) {}
6
6
 
7
7
  std::shared_ptr<Promise<void>>
8
- HybridCactus::init(const std::string &modelPath, double contextSize,
9
- const std::optional<std::string> &corpusDir) {
8
+ HybridCactus::init(const std::string &modelPath,
9
+ const std::optional<std::string> &corpusDir,
10
+ std::optional<bool> cacheIndex) {
10
11
  return Promise<void>::async(
11
- [this, modelPath, contextSize, corpusDir]() -> void {
12
+ [this, modelPath, corpusDir, cacheIndex]() -> void {
12
13
  std::lock_guard<std::mutex> lock(this->_modelMutex);
13
14
 
14
15
  if (this->_model) {
@@ -16,8 +17,9 @@ HybridCactus::init(const std::string &modelPath, double contextSize,
16
17
  }
17
18
 
18
19
  const cactus_model_t model =
19
- cactus_init(modelPath.c_str(), contextSize,
20
- corpusDir ? corpusDir->c_str() : nullptr);
20
+ cactus_init(modelPath.c_str(),
21
+ corpusDir ? corpusDir->c_str() : nullptr,
22
+ cacheIndex.value_or(false));
21
23
 
22
24
  if (!model) {
23
25
  throw std::runtime_error("Cactus init failed: " +
@@ -25,7 +27,6 @@ HybridCactus::init(const std::string &modelPath, double contextSize,
25
27
  }
26
28
 
27
29
  this->_model = model;
28
- this->_contextSize = contextSize;
29
30
  });
30
31
  }
31
32
 
@@ -211,6 +212,192 @@ std::shared_ptr<Promise<std::string>> HybridCactus::transcribe(
211
212
  });
212
213
  }
213
214
 
215
+ std::shared_ptr<Promise<std::string>> HybridCactus::detectLanguage(
216
+ const std::variant<std::vector<double>, std::string> &audio,
217
+ double responseBufferSize,
218
+ const std::optional<std::string> &optionsJson) {
219
+ return Promise<std::string>::async(
220
+ [this, audio, optionsJson, responseBufferSize]() -> std::string {
221
+ std::lock_guard<std::mutex> lock(this->_modelMutex);
222
+
223
+ if (!this->_model) {
224
+ throw std::runtime_error("Cactus model is not initialized");
225
+ }
226
+
227
+ std::string responseBuffer;
228
+ responseBuffer.resize(responseBufferSize);
229
+
230
+ int result;
231
+ if (std::holds_alternative<std::string>(audio)) {
232
+ result = cactus_detect_language(
233
+ this->_model, std::get<std::string>(audio).c_str(),
234
+ responseBuffer.data(), responseBufferSize,
235
+ optionsJson ? optionsJson->c_str() : nullptr, nullptr, 0);
236
+ } else {
237
+ const auto &audioDoubles = std::get<std::vector<double>>(audio);
238
+
239
+ std::vector<uint8_t> audioBytes;
240
+ audioBytes.reserve(audioDoubles.size());
241
+
242
+ for (double d : audioDoubles) {
243
+ d = std::clamp(d, 0.0, 255.0);
244
+ audioBytes.emplace_back(static_cast<uint8_t>(d));
245
+ }
246
+
247
+ result = cactus_detect_language(
248
+ this->_model, nullptr, responseBuffer.data(), responseBufferSize,
249
+ optionsJson ? optionsJson->c_str() : nullptr, audioBytes.data(),
250
+ audioBytes.size());
251
+ }
252
+
253
+ if (result < 0) {
254
+ throw std::runtime_error("Cactus detect language failed: " +
255
+ std::string(cactus_get_last_error()));
256
+ }
257
+
258
+ responseBuffer.resize(strlen(responseBuffer.c_str()));
259
+ return responseBuffer;
260
+ });
261
+ }
262
+
263
+ std::shared_ptr<Promise<void>> HybridCactus::streamTranscribeStart(
264
+ const std::optional<std::string> &optionsJson) {
265
+ return Promise<void>::async([this, optionsJson]() -> void {
266
+ std::lock_guard<std::mutex> lock(this->_modelMutex);
267
+
268
+ if (!this->_model) {
269
+ throw std::runtime_error("Cactus model is not initialized");
270
+ }
271
+
272
+ if (this->_streamTranscribe) {
273
+ throw std::runtime_error(
274
+ "Cactus stream transcribe is already initialized");
275
+ }
276
+
277
+ this->_streamTranscribe = cactus_stream_transcribe_start(
278
+ this->_model, optionsJson ? optionsJson->c_str() : nullptr);
279
+ if (!this->_streamTranscribe) {
280
+ throw std::runtime_error("Cactus stream transcribe start failed: " +
281
+ std::string(cactus_get_last_error()));
282
+ }
283
+ });
284
+ }
285
+
286
+ std::shared_ptr<Promise<std::string>>
287
+ HybridCactus::streamTranscribeProcess(const std::vector<double> &audio) {
288
+ return Promise<std::string>::async([this, audio]() -> std::string {
289
+ std::lock_guard<std::mutex> lock(this->_modelMutex);
290
+
291
+ if (!this->_streamTranscribe) {
292
+ throw std::runtime_error("Cactus stream transcribe is not initialized");
293
+ }
294
+
295
+ std::vector<uint8_t> audioBytes;
296
+ audioBytes.reserve(audio.size());
297
+ for (double d : audio) {
298
+ d = std::clamp(d, 0.0, 255.0);
299
+ audioBytes.emplace_back(static_cast<uint8_t>(d));
300
+ }
301
+
302
+ std::string responseBuffer;
303
+ responseBuffer.resize(32768);
304
+
305
+ int result = cactus_stream_transcribe_process(
306
+ this->_streamTranscribe, audioBytes.data(), audioBytes.size(),
307
+ responseBuffer.data(), responseBuffer.size());
308
+
309
+ if (result < 0) {
310
+ throw std::runtime_error("Cactus stream transcribe process failed: " +
311
+ std::string(cactus_get_last_error()));
312
+ }
313
+
314
+ // Remove null terminator
315
+ responseBuffer.resize(strlen(responseBuffer.c_str()));
316
+
317
+ return responseBuffer;
318
+ });
319
+ }
320
+
321
+ std::shared_ptr<Promise<std::string>> HybridCactus::streamTranscribeStop() {
322
+ return Promise<std::string>::async([this]() -> std::string {
323
+ std::lock_guard<std::mutex> lock(this->_modelMutex);
324
+
325
+ if (!this->_streamTranscribe) {
326
+ throw std::runtime_error("Cactus stream transcribe is not initialized");
327
+ }
328
+
329
+ std::string responseBuffer;
330
+ responseBuffer.resize(32768);
331
+
332
+ int result = cactus_stream_transcribe_stop(
333
+ this->_streamTranscribe, responseBuffer.data(), responseBuffer.size());
334
+
335
+ this->_streamTranscribe = nullptr;
336
+
337
+ if (result < 0) {
338
+ throw std::runtime_error("Cactus stream transcribe stop failed: " +
339
+ std::string(cactus_get_last_error()));
340
+ }
341
+
342
+ // Remove null terminator
343
+ responseBuffer.resize(strlen(responseBuffer.c_str()));
344
+
345
+ return responseBuffer;
346
+ });
347
+ }
348
+
349
+ std::shared_ptr<Promise<std::string>>
350
+ HybridCactus::vad(const std::variant<std::vector<double>, std::string> &audio,
351
+ double responseBufferSize,
352
+ const std::optional<std::string> &optionsJson) {
353
+ return Promise<std::string>::async(
354
+ [this, audio, responseBufferSize, optionsJson]() -> std::string {
355
+ std::lock_guard<std::mutex> lock(this->_modelMutex);
356
+
357
+ if (!this->_model) {
358
+ throw std::runtime_error("Cactus model is not initialized");
359
+ }
360
+
361
+ std::string responseBuffer;
362
+ responseBuffer.resize(responseBufferSize);
363
+
364
+ int result;
365
+ if (std::holds_alternative<std::string>(audio)) {
366
+ result =
367
+ cactus_vad(this->_model,
368
+ std::get<std::string>(audio).c_str(),
369
+ responseBuffer.data(), responseBufferSize,
370
+ optionsJson ? optionsJson->c_str() : nullptr,
371
+ nullptr, 0);
372
+ } else {
373
+ const auto &audioDoubles = std::get<std::vector<double>>(audio);
374
+
375
+ std::vector<uint8_t> audioBytes;
376
+ audioBytes.reserve(audioDoubles.size());
377
+ for (double d : audioDoubles) {
378
+ d = std::clamp(d, 0.0, 255.0);
379
+ audioBytes.emplace_back(static_cast<uint8_t>(d));
380
+ }
381
+
382
+ result =
383
+ cactus_vad(this->_model, nullptr,
384
+ responseBuffer.data(), responseBufferSize,
385
+ optionsJson ? optionsJson->c_str() : nullptr,
386
+ audioBytes.data(), audioBytes.size());
387
+ }
388
+
389
+ if (result < 0) {
390
+ throw std::runtime_error("Cactus VAD failed: " +
391
+ std::string(cactus_get_last_error()));
392
+ }
393
+
394
+ // Remove null terminator
395
+ responseBuffer.resize(strlen(responseBuffer.c_str()));
396
+
397
+ return responseBuffer;
398
+ });
399
+ }
400
+
214
401
  std::shared_ptr<Promise<std::vector<double>>>
215
402
  HybridCactus::embed(const std::string &text, double embeddingBufferSize,
216
403
  bool normalize) {
@@ -326,7 +513,7 @@ std::shared_ptr<Promise<void>> HybridCactus::destroy() {
326
513
  }
327
514
 
328
515
  if (this->_streamTranscribe) {
329
- cactus_stream_transcribe_destroy(this->_streamTranscribe);
516
+ cactus_stream_transcribe_stop(this->_streamTranscribe, nullptr, 0);
330
517
  this->_streamTranscribe = nullptr;
331
518
  }
332
519
 
@@ -335,117 +522,10 @@ std::shared_ptr<Promise<void>> HybridCactus::destroy() {
335
522
  });
336
523
  }
337
524
 
338
- std::shared_ptr<Promise<void>> HybridCactus::streamTranscribeInit() {
339
- return Promise<void>::async([this]() -> void {
340
- std::lock_guard<std::mutex> lock(this->_modelMutex);
341
-
342
- if (!this->_model) {
343
- throw std::runtime_error("Cactus model is not initialized");
344
- }
345
-
346
- if (this->_streamTranscribe) {
347
- throw std::runtime_error(
348
- "Cactus stream transcribe is already initialized");
349
- }
350
-
351
- this->_streamTranscribe = cactus_stream_transcribe_init(this->_model);
352
- if (!this->_streamTranscribe) {
353
- throw std::runtime_error("Cactus stream transcribe init failed: " +
354
- std::string(cactus_get_last_error()));
355
- }
356
- });
357
- }
358
-
359
525
  std::shared_ptr<Promise<void>>
360
- HybridCactus::streamTranscribeInsert(const std::vector<double> &audio) {
361
- return Promise<void>::async([this, audio]() -> void {
362
- std::lock_guard<std::mutex> lock(this->_modelMutex);
363
-
364
- if (!this->_streamTranscribe) {
365
- throw std::runtime_error("Cactus stream transcribe is not initialized");
366
- }
367
-
368
- std::vector<uint8_t> audioBytes;
369
- audioBytes.reserve(audio.size());
370
- for (double d : audio) {
371
- d = std::clamp(d, 0.0, 255.0);
372
- audioBytes.emplace_back(static_cast<uint8_t>(d));
373
- }
374
-
375
- int result = cactus_stream_transcribe_insert(
376
- this->_streamTranscribe, audioBytes.data(), audioBytes.size());
377
-
378
- if (result < 0) {
379
- throw std::runtime_error("Cactus stream transcribe insert failed: " +
380
- std::string(cactus_get_last_error()));
381
- }
382
- });
383
- }
384
-
385
- std::shared_ptr<Promise<std::string>> HybridCactus::streamTranscribeProcess(
386
- const std::optional<std::string> &optionsJson) {
387
- return Promise<std::string>::async([this, optionsJson]() -> std::string {
388
- std::lock_guard<std::mutex> lock(this->_modelMutex);
389
-
390
- if (!this->_streamTranscribe) {
391
- throw std::runtime_error("Cactus stream transcribe is not initialized");
392
- }
393
-
394
- std::string responseBuffer;
395
- responseBuffer.resize(32768);
396
-
397
- int result = cactus_stream_transcribe_process(
398
- this->_streamTranscribe, responseBuffer.data(), responseBuffer.size(),
399
- optionsJson ? optionsJson->c_str() : nullptr);
400
-
401
- if (result < 0) {
402
- throw std::runtime_error("Cactus stream transcribe process failed: " +
403
- std::string(cactus_get_last_error()));
404
- }
405
-
406
- // Remove null terminator
407
- responseBuffer.resize(strlen(responseBuffer.c_str()));
408
-
409
- return responseBuffer;
410
- });
411
- }
412
-
413
- std::shared_ptr<Promise<std::string>> HybridCactus::streamTranscribeFinalize() {
414
- return Promise<std::string>::async([this]() -> std::string {
415
- std::lock_guard<std::mutex> lock(this->_modelMutex);
416
-
417
- if (!this->_streamTranscribe) {
418
- throw std::runtime_error("Cactus stream transcribe is not initialized");
419
- }
420
-
421
- std::string responseBuffer;
422
- responseBuffer.resize(32768);
423
-
424
- int result = cactus_stream_transcribe_finalize(
425
- this->_streamTranscribe, responseBuffer.data(), responseBuffer.size());
426
-
427
- if (result < 0) {
428
- throw std::runtime_error("Cactus stream transcribe finalize failed: " +
429
- std::string(cactus_get_last_error()));
430
- }
431
-
432
- // Remove null terminator
433
- responseBuffer.resize(strlen(responseBuffer.c_str()));
434
-
435
- return responseBuffer;
436
- });
437
- }
438
-
439
- std::shared_ptr<Promise<void>> HybridCactus::streamTranscribeDestroy() {
440
- return Promise<void>::async([this]() -> void {
441
- std::lock_guard<std::mutex> lock(this->_modelMutex);
442
-
443
- if (!this->_streamTranscribe) {
444
- throw std::runtime_error("Cactus stream transcribe is not initialized");
445
- }
446
-
447
- cactus_stream_transcribe_destroy(this->_streamTranscribe);
448
- this->_streamTranscribe = nullptr;
526
+ HybridCactus::setTelemetryEnvironment(const std::string &cacheDir) {
527
+ return Promise<void>::async([cacheDir]() -> void {
528
+ cactus_set_telemetry_environment("react-native", cacheDir.c_str(), "1.10.0");
449
529
  });
450
530
  }
451
531
 
@@ -12,8 +12,9 @@ public:
12
12
  HybridCactus();
13
13
 
14
14
  std::shared_ptr<Promise<void>>
15
- init(const std::string &modelPath, double contextSize,
16
- const std::optional<std::string> &corpusDir) override;
15
+ init(const std::string &modelPath,
16
+ const std::optional<std::string> &corpusDir,
17
+ std::optional<bool> cacheIndex) override;
17
18
 
18
19
  std::shared_ptr<Promise<std::string>> complete(
19
20
  const std::string &messagesJson, double responseBufferSize,
@@ -38,17 +39,23 @@ public:
38
39
  double /* tokenId */)>> &callback)
39
40
  override;
40
41
 
41
- std::shared_ptr<Promise<void>> streamTranscribeInit() override;
42
+ std::shared_ptr<Promise<std::string>>
43
+ detectLanguage(const std::variant<std::vector<double>, std::string> &audio,
44
+ double responseBufferSize,
45
+ const std::optional<std::string> &optionsJson) override;
42
46
 
43
47
  std::shared_ptr<Promise<void>>
44
- streamTranscribeInsert(const std::vector<double> &audio) override;
48
+ streamTranscribeStart(const std::optional<std::string> &optionsJson) override;
45
49
 
46
- std::shared_ptr<Promise<std::string>> streamTranscribeProcess(
47
- const std::optional<std::string> &optionsJson) override;
50
+ std::shared_ptr<Promise<std::string>>
51
+ streamTranscribeProcess(const std::vector<double> &audio) override;
48
52
 
49
- std::shared_ptr<Promise<std::string>> streamTranscribeFinalize() override;
53
+ std::shared_ptr<Promise<std::string>> streamTranscribeStop() override;
50
54
 
51
- std::shared_ptr<Promise<void>> streamTranscribeDestroy() override;
55
+ std::shared_ptr<Promise<std::string>>
56
+ vad(const std::variant<std::vector<double>, std::string> &audio,
57
+ double responseBufferSize,
58
+ const std::optional<std::string> &optionsJson) override;
52
59
 
53
60
  std::shared_ptr<Promise<std::vector<double>>>
54
61
  embed(const std::string &text, double embeddingBufferSize,
@@ -66,10 +73,12 @@ public:
66
73
 
67
74
  std::shared_ptr<Promise<void>> destroy() override;
68
75
 
76
+ std::shared_ptr<Promise<void>>
77
+ setTelemetryEnvironment(const std::string &cacheDir) override;
78
+
69
79
  private:
70
80
  cactus_model_t _model = nullptr;
71
81
  cactus_stream_transcribe_t _streamTranscribe = nullptr;
72
- size_t _contextSize;
73
82
 
74
83
  std::mutex _modelMutex;
75
84
  };
package/cpp/cactus_ffi.h CHANGED
@@ -6,11 +6,11 @@
6
6
  #include <stdbool.h>
7
7
 
8
8
  #if __GNUC__ >= 4
9
- #define CACTUS_FFI_EXPORT __attribute__ ((visibility ("default")))
10
- #define CACTUS_FFI_LOCAL __attribute__ ((visibility ("hidden")))
9
+ #define CACTUS_FFI_EXPORT __attribute__((visibility("default")))
10
+ #define CACTUS_FFI_LOCAL __attribute__((visibility("hidden")))
11
11
  #else
12
- #define CACTUS_FFI_EXPORT
13
- #define CACTUS_FFI_LOCAL
12
+ #define CACTUS_FFI_EXPORT
13
+ #define CACTUS_FFI_LOCAL
14
14
  #endif
15
15
 
16
16
  #ifdef __cplusplus
@@ -18,20 +18,30 @@ extern "C" {
18
18
  #endif
19
19
 
20
20
  typedef void* cactus_model_t;
21
+ typedef void* cactus_index_t;
22
+ typedef void* cactus_stream_transcribe_t;
21
23
 
22
24
  typedef void (*cactus_token_callback)(const char* token, uint32_t token_id, void* user_data);
23
25
 
24
- CACTUS_FFI_EXPORT cactus_model_t cactus_init(const char* model_path, size_t context_size, const char* corpus_dir);
26
+ CACTUS_FFI_EXPORT cactus_model_t cactus_init(
27
+ const char* model_path,
28
+ const char* corpus_dir, // optional: NULL if no RAG corpus
29
+ bool cache_index // false = always rebuild index, true = load cached if available
30
+ );
31
+
32
+ CACTUS_FFI_EXPORT void cactus_destroy(cactus_model_t model);
33
+ CACTUS_FFI_EXPORT void cactus_reset(cactus_model_t model);
34
+ CACTUS_FFI_EXPORT void cactus_stop(cactus_model_t model);
25
35
 
26
36
  CACTUS_FFI_EXPORT int cactus_complete(
27
37
  cactus_model_t model,
28
38
  const char* messages_json,
29
39
  char* response_buffer,
30
40
  size_t buffer_size,
31
- const char* options_json,
32
- const char* tools_json,
33
- cactus_token_callback callback,
34
- void* user_data
41
+ const char* options_json, // optional
42
+ const char* tools_json, // optional
43
+ cactus_token_callback callback, // optional
44
+ void* user_data // optional
35
45
  );
36
46
 
37
47
  CACTUS_FFI_EXPORT int cactus_tokenize(
@@ -53,45 +63,48 @@ CACTUS_FFI_EXPORT int cactus_score_window(
53
63
  size_t buffer_size
54
64
  );
55
65
 
56
-
57
66
  CACTUS_FFI_EXPORT int cactus_transcribe(
58
67
  cactus_model_t model,
59
- const char* audio_file_path,
68
+ const char* audio_file_path, // NULL if using pcm_buffer
60
69
  const char* prompt,
61
70
  char* response_buffer,
62
71
  size_t buffer_size,
63
- const char* options_json,
64
- cactus_token_callback callback,
65
- void* user_data,
66
- const uint8_t* pcm_buffer,
72
+ const char* options_json, // optional
73
+ cactus_token_callback callback, // optional
74
+ void* user_data, // optional
75
+ const uint8_t* pcm_buffer, // NULL if using audio_file_path
67
76
  size_t pcm_buffer_size
68
77
  );
69
78
 
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,
79
+ CACTUS_FFI_EXPORT int cactus_detect_language(
80
+ cactus_model_t model,
81
+ const char* audio_file_path, // NULL if using pcm_buffer
82
+ char* response_buffer,
83
+ size_t buffer_size,
84
+ const char* options_json, // optional
85
+ const uint8_t* pcm_buffer, // NULL if using audio_file_path
77
86
  size_t pcm_buffer_size
78
87
  );
79
88
 
89
+ CACTUS_FFI_EXPORT cactus_stream_transcribe_t cactus_stream_transcribe_start(
90
+ cactus_model_t model,
91
+ const char* options_json // optional
92
+ );
93
+
80
94
  CACTUS_FFI_EXPORT int cactus_stream_transcribe_process(
81
95
  cactus_stream_transcribe_t stream,
96
+ const uint8_t* pcm_buffer,
97
+ size_t pcm_buffer_size,
82
98
  char* response_buffer,
83
- size_t buffer_size,
84
- const char* options_json
99
+ size_t buffer_size
85
100
  );
86
101
 
87
- CACTUS_FFI_EXPORT int cactus_stream_transcribe_finalize(
102
+ CACTUS_FFI_EXPORT int cactus_stream_transcribe_stop(
88
103
  cactus_stream_transcribe_t stream,
89
104
  char* response_buffer,
90
105
  size_t buffer_size
91
106
  );
92
107
 
93
- CACTUS_FFI_EXPORT void cactus_stream_transcribe_destroy(cactus_stream_transcribe_t stream);
94
-
95
108
  CACTUS_FFI_EXPORT int cactus_embed(
96
109
  cactus_model_t model,
97
110
  const char* text,
@@ -117,19 +130,24 @@ CACTUS_FFI_EXPORT int cactus_audio_embed(
117
130
  size_t* embedding_dim
118
131
  );
119
132
 
120
- CACTUS_FFI_EXPORT void cactus_reset(cactus_model_t model);
121
-
122
- CACTUS_FFI_EXPORT void cactus_stop(cactus_model_t model);
123
-
124
- CACTUS_FFI_EXPORT void cactus_destroy(cactus_model_t model);
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);
133
+ CACTUS_FFI_EXPORT int cactus_vad(
134
+ cactus_model_t model,
135
+ const char* audio_file_path,
136
+ char* response_buffer,
137
+ size_t buffer_size,
138
+ const char* options_json,
139
+ const uint8_t* pcm_buffer,
140
+ size_t pcm_buffer_size
141
+ );
129
142
 
130
- CACTUS_FFI_EXPORT void cactus_set_pro_key(const char* pro_key);
143
+ CACTUS_FFI_EXPORT int cactus_rag_query(
144
+ cactus_model_t model,
145
+ const char* query,
146
+ char* response_buffer,
147
+ size_t buffer_size,
148
+ size_t top_k
149
+ );
131
150
 
132
- typedef void* cactus_index_t;
133
151
 
134
152
  CACTUS_FFI_EXPORT cactus_index_t cactus_index_init(
135
153
  const char* index_dir,
@@ -140,7 +158,7 @@ CACTUS_FFI_EXPORT int cactus_index_add(
140
158
  cactus_index_t index,
141
159
  const int* ids,
142
160
  const char** documents,
143
- const char** metadatas,
161
+ const char** metadatas, // optional: can be NULL
144
162
  const float** embeddings,
145
163
  size_t count,
146
164
  size_t embedding_dim
@@ -169,7 +187,7 @@ CACTUS_FFI_EXPORT int cactus_index_query(
169
187
  const float** embeddings,
170
188
  size_t embeddings_count,
171
189
  size_t embedding_dim,
172
- const char* options_json,
190
+ const char* options_json, // optional
173
191
  int** id_buffers,
174
192
  size_t* id_buffer_sizes,
175
193
  float** score_buffers,
@@ -177,11 +195,17 @@ CACTUS_FFI_EXPORT int cactus_index_query(
177
195
  );
178
196
 
179
197
  CACTUS_FFI_EXPORT int cactus_index_compact(cactus_index_t index);
180
-
181
198
  CACTUS_FFI_EXPORT void cactus_index_destroy(cactus_index_t index);
182
199
 
200
+ CACTUS_FFI_EXPORT const char* cactus_get_last_error(void);
201
+
202
+ CACTUS_FFI_EXPORT void cactus_set_telemetry_environment(const char* framework, const char* cache_location, const char* version);
203
+ CACTUS_FFI_EXPORT void cactus_set_app_id(const char* app_id);
204
+ CACTUS_FFI_EXPORT void cactus_telemetry_flush(void);
205
+ CACTUS_FFI_EXPORT void cactus_telemetry_shutdown(void);
206
+
183
207
  #ifdef __cplusplus
184
208
  }
185
209
  #endif
186
210
 
187
- #endif
211
+ #endif // CACTUS_FFI_H
@@ -7,7 +7,6 @@
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
10
  #include "npu/npu.h"
12
11
 
13
12
  #endif // CACTUS_H