react-native-litert-lm 0.1.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 (53) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +259 -0
  3. package/android/CMakeLists.txt +32 -0
  4. package/android/build.gradle +88 -0
  5. package/android/src/main/AndroidManifest.xml +11 -0
  6. package/android/src/main/java/com/margelo/nitro/dev/litert/litertlm/HybridLiteRTLM.kt +280 -0
  7. package/android/src/main/java/dev/litert/litertlm/LiteRTLMInitProvider.kt +43 -0
  8. package/android/src/main/java/dev/litert/litertlm/LiteRTLMPackage.kt +26 -0
  9. package/cpp/HybridLiteRTLM.cpp +483 -0
  10. package/cpp/HybridLiteRTLM.hpp +120 -0
  11. package/cpp/cpp-adapter.cpp +13 -0
  12. package/cpp/include/README.md +34 -0
  13. package/lib/index.d.ts +82 -0
  14. package/lib/index.js +106 -0
  15. package/lib/specs/LiteRTLM.nitro.d.ts +165 -0
  16. package/lib/specs/LiteRTLM.nitro.js +2 -0
  17. package/nitrogen/generated/.gitattributes +1 -0
  18. package/nitrogen/generated/android/LiteRTLM+autolinking.cmake +81 -0
  19. package/nitrogen/generated/android/LiteRTLM+autolinking.gradle +27 -0
  20. package/nitrogen/generated/android/LiteRTLMOnLoad.cpp +46 -0
  21. package/nitrogen/generated/android/LiteRTLMOnLoad.hpp +25 -0
  22. package/nitrogen/generated/android/c++/JBackend.hpp +61 -0
  23. package/nitrogen/generated/android/c++/JFunc_void_std__string_bool.hpp +76 -0
  24. package/nitrogen/generated/android/c++/JGenerationStats.hpp +77 -0
  25. package/nitrogen/generated/android/c++/JHybridLiteRTLMSpec.cpp +133 -0
  26. package/nitrogen/generated/android/c++/JHybridLiteRTLMSpec.hpp +75 -0
  27. package/nitrogen/generated/android/c++/JLLMConfig.hpp +75 -0
  28. package/nitrogen/generated/android/c++/JMessage.hpp +63 -0
  29. package/nitrogen/generated/android/c++/JRole.hpp +61 -0
  30. package/nitrogen/generated/android/kotlin/com/margelo/nitro/dev/litert/litertlm/Backend.kt +24 -0
  31. package/nitrogen/generated/android/kotlin/com/margelo/nitro/dev/litert/litertlm/Func_void_std__string_bool.kt +80 -0
  32. package/nitrogen/generated/android/kotlin/com/margelo/nitro/dev/litert/litertlm/GenerationStats.kt +53 -0
  33. package/nitrogen/generated/android/kotlin/com/margelo/nitro/dev/litert/litertlm/HybridLiteRTLMSpec.kt +98 -0
  34. package/nitrogen/generated/android/kotlin/com/margelo/nitro/dev/litert/litertlm/LLMConfig.kt +50 -0
  35. package/nitrogen/generated/android/kotlin/com/margelo/nitro/dev/litert/litertlm/LiteRTLMOnLoad.kt +35 -0
  36. package/nitrogen/generated/android/kotlin/com/margelo/nitro/dev/litert/litertlm/Message.kt +41 -0
  37. package/nitrogen/generated/android/kotlin/com/margelo/nitro/dev/litert/litertlm/Role.kt +24 -0
  38. package/nitrogen/generated/ios/LiteRTLM+autolinking.rb +60 -0
  39. package/nitrogen/generated/ios/LiteRTLM-Swift-Cxx-Bridge.cpp +17 -0
  40. package/nitrogen/generated/ios/LiteRTLM-Swift-Cxx-Bridge.hpp +27 -0
  41. package/nitrogen/generated/ios/LiteRTLM-Swift-Cxx-Umbrella.hpp +38 -0
  42. package/nitrogen/generated/shared/c++/Backend.hpp +80 -0
  43. package/nitrogen/generated/shared/c++/GenerationStats.hpp +103 -0
  44. package/nitrogen/generated/shared/c++/HybridLiteRTLMSpec.cpp +30 -0
  45. package/nitrogen/generated/shared/c++/HybridLiteRTLMSpec.hpp +82 -0
  46. package/nitrogen/generated/shared/c++/LLMConfig.hpp +101 -0
  47. package/nitrogen/generated/shared/c++/Message.hpp +89 -0
  48. package/nitrogen/generated/shared/c++/Role.hpp +80 -0
  49. package/package.json +87 -0
  50. package/react-native-litert-lm.podspec +51 -0
  51. package/react-native.config.js +16 -0
  52. package/src/index.ts +125 -0
  53. package/src/specs/LiteRTLM.nitro.ts +187 -0
@@ -0,0 +1,53 @@
1
+ ///
2
+ /// GenerationStats.kt
3
+ /// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
4
+ /// https://github.com/mrousavy/nitro
5
+ /// Copyright © Marc Rousavy @ Margelo
6
+ ///
7
+
8
+ package com.margelo.nitro.dev.litert.litertlm
9
+
10
+ import androidx.annotation.Keep
11
+ import com.facebook.proguard.annotations.DoNotStrip
12
+
13
+
14
+ /**
15
+ * Represents the JavaScript object/struct "GenerationStats".
16
+ */
17
+ @DoNotStrip
18
+ @Keep
19
+ data class GenerationStats(
20
+ @DoNotStrip
21
+ @Keep
22
+ val promptTokens: Double,
23
+ @DoNotStrip
24
+ @Keep
25
+ val completionTokens: Double,
26
+ @DoNotStrip
27
+ @Keep
28
+ val totalTokens: Double,
29
+ @DoNotStrip
30
+ @Keep
31
+ val timeToFirstToken: Double,
32
+ @DoNotStrip
33
+ @Keep
34
+ val totalTime: Double,
35
+ @DoNotStrip
36
+ @Keep
37
+ val tokensPerSecond: Double
38
+ ) {
39
+ /* primary constructor */
40
+
41
+ companion object {
42
+ /**
43
+ * Constructor called from C++
44
+ */
45
+ @DoNotStrip
46
+ @Keep
47
+ @Suppress("unused")
48
+ @JvmStatic
49
+ private fun fromCpp(promptTokens: Double, completionTokens: Double, totalTokens: Double, timeToFirstToken: Double, totalTime: Double, tokensPerSecond: Double): GenerationStats {
50
+ return GenerationStats(promptTokens, completionTokens, totalTokens, timeToFirstToken, totalTime, tokensPerSecond)
51
+ }
52
+ }
53
+ }
@@ -0,0 +1,98 @@
1
+ ///
2
+ /// HybridLiteRTLMSpec.kt
3
+ /// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
4
+ /// https://github.com/mrousavy/nitro
5
+ /// Copyright © Marc Rousavy @ Margelo
6
+ ///
7
+
8
+ package com.margelo.nitro.dev.litert.litertlm
9
+
10
+ import androidx.annotation.Keep
11
+ import com.facebook.jni.HybridData
12
+ import com.facebook.proguard.annotations.DoNotStrip
13
+ import com.margelo.nitro.core.HybridObject
14
+
15
+ /**
16
+ * A Kotlin class representing the LiteRTLM HybridObject.
17
+ * Implement this abstract class to create Kotlin-based instances of LiteRTLM.
18
+ */
19
+ @DoNotStrip
20
+ @Keep
21
+ @Suppress(
22
+ "KotlinJniMissingFunction", "unused",
23
+ "RedundantSuppression", "RedundantUnitReturnType", "SimpleRedundantLet",
24
+ "LocalVariableName", "PropertyName", "PrivatePropertyName", "FunctionName"
25
+ )
26
+ abstract class HybridLiteRTLMSpec: HybridObject() {
27
+ @DoNotStrip
28
+ private var mHybridData: HybridData = initHybrid()
29
+
30
+ init {
31
+ super.updateNative(mHybridData)
32
+ }
33
+
34
+ override fun updateNative(hybridData: HybridData) {
35
+ mHybridData = hybridData
36
+ super.updateNative(hybridData)
37
+ }
38
+
39
+ // Default implementation of `HybridObject.toString()`
40
+ override fun toString(): String {
41
+ return "[HybridObject LiteRTLM]"
42
+ }
43
+
44
+ // Properties
45
+
46
+
47
+ // Methods
48
+ @DoNotStrip
49
+ @Keep
50
+ abstract fun loadModel(modelPath: String, config: LLMConfig?): Unit
51
+
52
+ @DoNotStrip
53
+ @Keep
54
+ abstract fun sendMessage(message: String): String
55
+
56
+ @DoNotStrip
57
+ @Keep
58
+ abstract fun sendMessageWithImage(message: String, imagePath: String): String
59
+
60
+ @DoNotStrip
61
+ @Keep
62
+ abstract fun sendMessageWithAudio(message: String, audioPath: String): String
63
+
64
+ abstract fun sendMessageAsync(message: String, onToken: (token: String, done: Boolean) -> Unit): Unit
65
+
66
+ @DoNotStrip
67
+ @Keep
68
+ private fun sendMessageAsync_cxx(message: String, onToken: Func_void_std__string_bool): Unit {
69
+ val __result = sendMessageAsync(message, onToken)
70
+ return __result
71
+ }
72
+
73
+ @DoNotStrip
74
+ @Keep
75
+ abstract fun getHistory(): Array<Message>
76
+
77
+ @DoNotStrip
78
+ @Keep
79
+ abstract fun resetConversation(): Unit
80
+
81
+ @DoNotStrip
82
+ @Keep
83
+ abstract fun isReady(): Boolean
84
+
85
+ @DoNotStrip
86
+ @Keep
87
+ abstract fun getStats(): GenerationStats
88
+
89
+ @DoNotStrip
90
+ @Keep
91
+ abstract fun close(): Unit
92
+
93
+ private external fun initHybrid(): HybridData
94
+
95
+ companion object {
96
+ protected const val TAG = "HybridLiteRTLMSpec"
97
+ }
98
+ }
@@ -0,0 +1,50 @@
1
+ ///
2
+ /// LLMConfig.kt
3
+ /// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
4
+ /// https://github.com/mrousavy/nitro
5
+ /// Copyright © Marc Rousavy @ Margelo
6
+ ///
7
+
8
+ package com.margelo.nitro.dev.litert.litertlm
9
+
10
+ import androidx.annotation.Keep
11
+ import com.facebook.proguard.annotations.DoNotStrip
12
+
13
+
14
+ /**
15
+ * Represents the JavaScript object/struct "LLMConfig".
16
+ */
17
+ @DoNotStrip
18
+ @Keep
19
+ data class LLMConfig(
20
+ @DoNotStrip
21
+ @Keep
22
+ val backend: Backend?,
23
+ @DoNotStrip
24
+ @Keep
25
+ val maxTokens: Double?,
26
+ @DoNotStrip
27
+ @Keep
28
+ val temperature: Double?,
29
+ @DoNotStrip
30
+ @Keep
31
+ val topK: Double?,
32
+ @DoNotStrip
33
+ @Keep
34
+ val topP: Double?
35
+ ) {
36
+ /* primary constructor */
37
+
38
+ companion object {
39
+ /**
40
+ * Constructor called from C++
41
+ */
42
+ @DoNotStrip
43
+ @Keep
44
+ @Suppress("unused")
45
+ @JvmStatic
46
+ private fun fromCpp(backend: Backend?, maxTokens: Double?, temperature: Double?, topK: Double?, topP: Double?): LLMConfig {
47
+ return LLMConfig(backend, maxTokens, temperature, topK, topP)
48
+ }
49
+ }
50
+ }
@@ -0,0 +1,35 @@
1
+ ///
2
+ /// LiteRTLMOnLoad.kt
3
+ /// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
4
+ /// https://github.com/mrousavy/nitro
5
+ /// Copyright © Marc Rousavy @ Margelo
6
+ ///
7
+
8
+ package com.margelo.nitro.dev.litert.litertlm
9
+
10
+ import android.util.Log
11
+
12
+ internal class LiteRTLMOnLoad {
13
+ companion object {
14
+ private const val TAG = "LiteRTLMOnLoad"
15
+ private var didLoad = false
16
+ /**
17
+ * Initializes the native part of "LiteRTLM".
18
+ * This method is idempotent and can be called more than once.
19
+ */
20
+ @JvmStatic
21
+ fun initializeNative() {
22
+ if (didLoad) return
23
+ try {
24
+ Log.i(TAG, "Loading LiteRTLM C++ library...")
25
+ System.loadLibrary("LiteRTLM")
26
+ Log.i(TAG, "Successfully loaded LiteRTLM C++ library!")
27
+ didLoad = true
28
+ } catch (e: Error) {
29
+ Log.e(TAG, "Failed to load LiteRTLM C++ library! Is it properly installed and linked? " +
30
+ "Is the name correct? (see `CMakeLists.txt`, at `add_library(...)`)", e)
31
+ throw e
32
+ }
33
+ }
34
+ }
35
+ }
@@ -0,0 +1,41 @@
1
+ ///
2
+ /// Message.kt
3
+ /// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
4
+ /// https://github.com/mrousavy/nitro
5
+ /// Copyright © Marc Rousavy @ Margelo
6
+ ///
7
+
8
+ package com.margelo.nitro.dev.litert.litertlm
9
+
10
+ import androidx.annotation.Keep
11
+ import com.facebook.proguard.annotations.DoNotStrip
12
+
13
+
14
+ /**
15
+ * Represents the JavaScript object/struct "Message".
16
+ */
17
+ @DoNotStrip
18
+ @Keep
19
+ data class Message(
20
+ @DoNotStrip
21
+ @Keep
22
+ val role: Role,
23
+ @DoNotStrip
24
+ @Keep
25
+ val content: String
26
+ ) {
27
+ /* primary constructor */
28
+
29
+ companion object {
30
+ /**
31
+ * Constructor called from C++
32
+ */
33
+ @DoNotStrip
34
+ @Keep
35
+ @Suppress("unused")
36
+ @JvmStatic
37
+ private fun fromCpp(role: Role, content: String): Message {
38
+ return Message(role, content)
39
+ }
40
+ }
41
+ }
@@ -0,0 +1,24 @@
1
+ ///
2
+ /// Role.kt
3
+ /// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
4
+ /// https://github.com/mrousavy/nitro
5
+ /// Copyright © Marc Rousavy @ Margelo
6
+ ///
7
+
8
+ package com.margelo.nitro.dev.litert.litertlm
9
+
10
+ import androidx.annotation.Keep
11
+ import com.facebook.proguard.annotations.DoNotStrip
12
+
13
+ /**
14
+ * Represents the JavaScript enum/union "Role".
15
+ */
16
+ @DoNotStrip
17
+ @Keep
18
+ enum class Role(@DoNotStrip @Keep val value: Int) {
19
+ USER(0),
20
+ MODEL(1),
21
+ SYSTEM(2);
22
+
23
+ companion object
24
+ }
@@ -0,0 +1,60 @@
1
+ #
2
+ # LiteRTLM+autolinking.rb
3
+ # This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
4
+ # https://github.com/mrousavy/nitro
5
+ # Copyright © Marc Rousavy @ Margelo
6
+ #
7
+
8
+ # This is a Ruby script that adds all files generated by Nitrogen
9
+ # to the given podspec.
10
+ #
11
+ # To use it, add this to your .podspec:
12
+ # ```ruby
13
+ # Pod::Spec.new do |spec|
14
+ # # ...
15
+ #
16
+ # # Add all files generated by Nitrogen
17
+ # load 'nitrogen/generated/ios/LiteRTLM+autolinking.rb'
18
+ # add_nitrogen_files(spec)
19
+ # end
20
+ # ```
21
+
22
+ def add_nitrogen_files(spec)
23
+ Pod::UI.puts "[NitroModules] 🔥 LiteRTLM is boosted by nitro!"
24
+
25
+ spec.dependency "NitroModules"
26
+
27
+ current_source_files = Array(spec.attributes_hash['source_files'])
28
+ spec.source_files = current_source_files + [
29
+ # Generated cross-platform specs
30
+ "nitrogen/generated/shared/**/*.{h,hpp,c,cpp,swift}",
31
+ # Generated bridges for the cross-platform specs
32
+ "nitrogen/generated/ios/**/*.{h,hpp,c,cpp,mm,swift}",
33
+ ]
34
+
35
+ current_public_header_files = Array(spec.attributes_hash['public_header_files'])
36
+ spec.public_header_files = current_public_header_files + [
37
+ # Generated specs
38
+ "nitrogen/generated/shared/**/*.{h,hpp}",
39
+ # Swift to C++ bridging helpers
40
+ "nitrogen/generated/ios/LiteRTLM-Swift-Cxx-Bridge.hpp"
41
+ ]
42
+
43
+ current_private_header_files = Array(spec.attributes_hash['private_header_files'])
44
+ spec.private_header_files = current_private_header_files + [
45
+ # iOS specific specs
46
+ "nitrogen/generated/ios/c++/**/*.{h,hpp}",
47
+ # Views are framework-specific and should be private
48
+ "nitrogen/generated/shared/**/views/**/*"
49
+ ]
50
+
51
+ current_pod_target_xcconfig = spec.attributes_hash['pod_target_xcconfig'] || {}
52
+ spec.pod_target_xcconfig = current_pod_target_xcconfig.merge({
53
+ # Use C++ 20
54
+ "CLANG_CXX_LANGUAGE_STANDARD" => "c++20",
55
+ # Enables C++ <-> Swift interop (by default it's only ObjC)
56
+ "SWIFT_OBJC_INTEROP_MODE" => "objcxx",
57
+ # Enables stricter modular headers
58
+ "DEFINES_MODULE" => "YES",
59
+ })
60
+ end
@@ -0,0 +1,17 @@
1
+ ///
2
+ /// LiteRTLM-Swift-Cxx-Bridge.cpp
3
+ /// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
4
+ /// https://github.com/mrousavy/nitro
5
+ /// Copyright © Marc Rousavy @ Margelo
6
+ ///
7
+
8
+ #include "LiteRTLM-Swift-Cxx-Bridge.hpp"
9
+
10
+ // Include C++ implementation defined types
11
+
12
+
13
+ namespace margelo::nitro::litertlm::bridge::swift {
14
+
15
+
16
+
17
+ } // namespace margelo::nitro::litertlm::bridge::swift
@@ -0,0 +1,27 @@
1
+ ///
2
+ /// LiteRTLM-Swift-Cxx-Bridge.hpp
3
+ /// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
4
+ /// https://github.com/mrousavy/nitro
5
+ /// Copyright © Marc Rousavy @ Margelo
6
+ ///
7
+
8
+ #pragma once
9
+
10
+ // Forward declarations of C++ defined types
11
+
12
+
13
+ // Forward declarations of Swift defined types
14
+
15
+
16
+ // Include C++ defined types
17
+
18
+
19
+ /**
20
+ * Contains specialized versions of C++ templated types so they can be accessed from Swift,
21
+ * as well as helper functions to interact with those C++ types from Swift.
22
+ */
23
+ namespace margelo::nitro::litertlm::bridge::swift {
24
+
25
+
26
+
27
+ } // namespace margelo::nitro::litertlm::bridge::swift
@@ -0,0 +1,38 @@
1
+ ///
2
+ /// LiteRTLM-Swift-Cxx-Umbrella.hpp
3
+ /// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
4
+ /// https://github.com/mrousavy/nitro
5
+ /// Copyright © Marc Rousavy @ Margelo
6
+ ///
7
+
8
+ #pragma once
9
+
10
+ // Forward declarations of C++ defined types
11
+
12
+
13
+ // Include C++ defined types
14
+
15
+
16
+ // C++ helpers for Swift
17
+ #include "LiteRTLM-Swift-Cxx-Bridge.hpp"
18
+
19
+ // Common C++ types used in Swift
20
+ #include <NitroModules/ArrayBufferHolder.hpp>
21
+ #include <NitroModules/AnyMapUtils.hpp>
22
+ #include <NitroModules/RuntimeError.hpp>
23
+ #include <NitroModules/DateToChronoDate.hpp>
24
+
25
+ // Forward declarations of Swift defined types
26
+
27
+
28
+ // Include Swift defined types
29
+ #if __has_include("LiteRTLM-Swift.h")
30
+ // This header is generated by Xcode/Swift on every app build.
31
+ // If it cannot be found, make sure the Swift module's name (= podspec name) is actually "LiteRTLM".
32
+ #include "LiteRTLM-Swift.h"
33
+ // Same as above, but used when building with frameworks (`use_frameworks`)
34
+ #elif __has_include(<LiteRTLM/LiteRTLM-Swift.h>)
35
+ #include <LiteRTLM/LiteRTLM-Swift.h>
36
+ #else
37
+ #error LiteRTLM's autogenerated Swift header cannot be found! Make sure the Swift module's name (= podspec name) is actually "LiteRTLM", and try building the app first.
38
+ #endif
@@ -0,0 +1,80 @@
1
+ ///
2
+ /// Backend.hpp
3
+ /// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
4
+ /// https://github.com/mrousavy/nitro
5
+ /// Copyright © Marc Rousavy @ Margelo
6
+ ///
7
+
8
+ #pragma once
9
+
10
+ #if __has_include(<NitroModules/NitroHash.hpp>)
11
+ #include <NitroModules/NitroHash.hpp>
12
+ #else
13
+ #error NitroModules cannot be found! Are you sure you installed NitroModules properly?
14
+ #endif
15
+ #if __has_include(<NitroModules/JSIConverter.hpp>)
16
+ #include <NitroModules/JSIConverter.hpp>
17
+ #else
18
+ #error NitroModules cannot be found! Are you sure you installed NitroModules properly?
19
+ #endif
20
+ #if __has_include(<NitroModules/NitroDefines.hpp>)
21
+ #include <NitroModules/NitroDefines.hpp>
22
+ #else
23
+ #error NitroModules cannot be found! Are you sure you installed NitroModules properly?
24
+ #endif
25
+
26
+ namespace margelo::nitro::litertlm {
27
+
28
+ /**
29
+ * An enum which can be represented as a JavaScript union (Backend).
30
+ */
31
+ enum class Backend {
32
+ CPU SWIFT_NAME(cpu) = 0,
33
+ GPU SWIFT_NAME(gpu) = 1,
34
+ NPU SWIFT_NAME(npu) = 2,
35
+ } CLOSED_ENUM;
36
+
37
+ } // namespace margelo::nitro::litertlm
38
+
39
+ namespace margelo::nitro {
40
+
41
+ // C++ Backend <> JS Backend (union)
42
+ template <>
43
+ struct JSIConverter<margelo::nitro::litertlm::Backend> final {
44
+ static inline margelo::nitro::litertlm::Backend fromJSI(jsi::Runtime& runtime, const jsi::Value& arg) {
45
+ std::string unionValue = JSIConverter<std::string>::fromJSI(runtime, arg);
46
+ switch (hashString(unionValue.c_str(), unionValue.size())) {
47
+ case hashString("cpu"): return margelo::nitro::litertlm::Backend::CPU;
48
+ case hashString("gpu"): return margelo::nitro::litertlm::Backend::GPU;
49
+ case hashString("npu"): return margelo::nitro::litertlm::Backend::NPU;
50
+ default: [[unlikely]]
51
+ throw std::invalid_argument("Cannot convert \"" + unionValue + "\" to enum Backend - invalid value!");
52
+ }
53
+ }
54
+ static inline jsi::Value toJSI(jsi::Runtime& runtime, margelo::nitro::litertlm::Backend arg) {
55
+ switch (arg) {
56
+ case margelo::nitro::litertlm::Backend::CPU: return JSIConverter<std::string>::toJSI(runtime, "cpu");
57
+ case margelo::nitro::litertlm::Backend::GPU: return JSIConverter<std::string>::toJSI(runtime, "gpu");
58
+ case margelo::nitro::litertlm::Backend::NPU: return JSIConverter<std::string>::toJSI(runtime, "npu");
59
+ default: [[unlikely]]
60
+ throw std::invalid_argument("Cannot convert Backend to JS - invalid value: "
61
+ + std::to_string(static_cast<int>(arg)) + "!");
62
+ }
63
+ }
64
+ static inline bool canConvert(jsi::Runtime& runtime, const jsi::Value& value) {
65
+ if (!value.isString()) {
66
+ return false;
67
+ }
68
+ std::string unionValue = JSIConverter<std::string>::fromJSI(runtime, value);
69
+ switch (hashString(unionValue.c_str(), unionValue.size())) {
70
+ case hashString("cpu"):
71
+ case hashString("gpu"):
72
+ case hashString("npu"):
73
+ return true;
74
+ default:
75
+ return false;
76
+ }
77
+ }
78
+ };
79
+
80
+ } // namespace margelo::nitro
@@ -0,0 +1,103 @@
1
+ ///
2
+ /// GenerationStats.hpp
3
+ /// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
4
+ /// https://github.com/mrousavy/nitro
5
+ /// Copyright © Marc Rousavy @ Margelo
6
+ ///
7
+
8
+ #pragma once
9
+
10
+ #if __has_include(<NitroModules/JSIConverter.hpp>)
11
+ #include <NitroModules/JSIConverter.hpp>
12
+ #else
13
+ #error NitroModules cannot be found! Are you sure you installed NitroModules properly?
14
+ #endif
15
+ #if __has_include(<NitroModules/NitroDefines.hpp>)
16
+ #include <NitroModules/NitroDefines.hpp>
17
+ #else
18
+ #error NitroModules cannot be found! Are you sure you installed NitroModules properly?
19
+ #endif
20
+ #if __has_include(<NitroModules/JSIHelpers.hpp>)
21
+ #include <NitroModules/JSIHelpers.hpp>
22
+ #else
23
+ #error NitroModules cannot be found! Are you sure you installed NitroModules properly?
24
+ #endif
25
+ #if __has_include(<NitroModules/PropNameIDCache.hpp>)
26
+ #include <NitroModules/PropNameIDCache.hpp>
27
+ #else
28
+ #error NitroModules cannot be found! Are you sure you installed NitroModules properly?
29
+ #endif
30
+
31
+
32
+
33
+
34
+
35
+ namespace margelo::nitro::litertlm {
36
+
37
+ /**
38
+ * A struct which can be represented as a JavaScript object (GenerationStats).
39
+ */
40
+ struct GenerationStats final {
41
+ public:
42
+ double promptTokens SWIFT_PRIVATE;
43
+ double completionTokens SWIFT_PRIVATE;
44
+ double totalTokens SWIFT_PRIVATE;
45
+ double timeToFirstToken SWIFT_PRIVATE;
46
+ double totalTime SWIFT_PRIVATE;
47
+ double tokensPerSecond SWIFT_PRIVATE;
48
+
49
+ public:
50
+ GenerationStats() = default;
51
+ explicit GenerationStats(double promptTokens, double completionTokens, double totalTokens, double timeToFirstToken, double totalTime, double tokensPerSecond): promptTokens(promptTokens), completionTokens(completionTokens), totalTokens(totalTokens), timeToFirstToken(timeToFirstToken), totalTime(totalTime), tokensPerSecond(tokensPerSecond) {}
52
+
53
+ public:
54
+ friend bool operator==(const GenerationStats& lhs, const GenerationStats& rhs) = default;
55
+ };
56
+
57
+ } // namespace margelo::nitro::litertlm
58
+
59
+ namespace margelo::nitro {
60
+
61
+ // C++ GenerationStats <> JS GenerationStats (object)
62
+ template <>
63
+ struct JSIConverter<margelo::nitro::litertlm::GenerationStats> final {
64
+ static inline margelo::nitro::litertlm::GenerationStats fromJSI(jsi::Runtime& runtime, const jsi::Value& arg) {
65
+ jsi::Object obj = arg.asObject(runtime);
66
+ return margelo::nitro::litertlm::GenerationStats(
67
+ JSIConverter<double>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "promptTokens"))),
68
+ JSIConverter<double>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "completionTokens"))),
69
+ JSIConverter<double>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "totalTokens"))),
70
+ JSIConverter<double>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "timeToFirstToken"))),
71
+ JSIConverter<double>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "totalTime"))),
72
+ JSIConverter<double>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "tokensPerSecond")))
73
+ );
74
+ }
75
+ static inline jsi::Value toJSI(jsi::Runtime& runtime, const margelo::nitro::litertlm::GenerationStats& arg) {
76
+ jsi::Object obj(runtime);
77
+ obj.setProperty(runtime, PropNameIDCache::get(runtime, "promptTokens"), JSIConverter<double>::toJSI(runtime, arg.promptTokens));
78
+ obj.setProperty(runtime, PropNameIDCache::get(runtime, "completionTokens"), JSIConverter<double>::toJSI(runtime, arg.completionTokens));
79
+ obj.setProperty(runtime, PropNameIDCache::get(runtime, "totalTokens"), JSIConverter<double>::toJSI(runtime, arg.totalTokens));
80
+ obj.setProperty(runtime, PropNameIDCache::get(runtime, "timeToFirstToken"), JSIConverter<double>::toJSI(runtime, arg.timeToFirstToken));
81
+ obj.setProperty(runtime, PropNameIDCache::get(runtime, "totalTime"), JSIConverter<double>::toJSI(runtime, arg.totalTime));
82
+ obj.setProperty(runtime, PropNameIDCache::get(runtime, "tokensPerSecond"), JSIConverter<double>::toJSI(runtime, arg.tokensPerSecond));
83
+ return obj;
84
+ }
85
+ static inline bool canConvert(jsi::Runtime& runtime, const jsi::Value& value) {
86
+ if (!value.isObject()) {
87
+ return false;
88
+ }
89
+ jsi::Object obj = value.getObject(runtime);
90
+ if (!nitro::isPlainObject(runtime, obj)) {
91
+ return false;
92
+ }
93
+ if (!JSIConverter<double>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "promptTokens")))) return false;
94
+ if (!JSIConverter<double>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "completionTokens")))) return false;
95
+ if (!JSIConverter<double>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "totalTokens")))) return false;
96
+ if (!JSIConverter<double>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "timeToFirstToken")))) return false;
97
+ if (!JSIConverter<double>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "totalTime")))) return false;
98
+ if (!JSIConverter<double>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "tokensPerSecond")))) return false;
99
+ return true;
100
+ }
101
+ };
102
+
103
+ } // namespace margelo::nitro
@@ -0,0 +1,30 @@
1
+ ///
2
+ /// HybridLiteRTLMSpec.cpp
3
+ /// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
4
+ /// https://github.com/mrousavy/nitro
5
+ /// Copyright © Marc Rousavy @ Margelo
6
+ ///
7
+
8
+ #include "HybridLiteRTLMSpec.hpp"
9
+
10
+ namespace margelo::nitro::litertlm {
11
+
12
+ void HybridLiteRTLMSpec::loadHybridMethods() {
13
+ // load base methods/properties
14
+ HybridObject::loadHybridMethods();
15
+ // load custom methods/properties
16
+ registerHybrids(this, [](Prototype& prototype) {
17
+ prototype.registerHybridMethod("loadModel", &HybridLiteRTLMSpec::loadModel);
18
+ prototype.registerHybridMethod("sendMessage", &HybridLiteRTLMSpec::sendMessage);
19
+ prototype.registerHybridMethod("sendMessageWithImage", &HybridLiteRTLMSpec::sendMessageWithImage);
20
+ prototype.registerHybridMethod("sendMessageWithAudio", &HybridLiteRTLMSpec::sendMessageWithAudio);
21
+ prototype.registerHybridMethod("sendMessageAsync", &HybridLiteRTLMSpec::sendMessageAsync);
22
+ prototype.registerHybridMethod("getHistory", &HybridLiteRTLMSpec::getHistory);
23
+ prototype.registerHybridMethod("resetConversation", &HybridLiteRTLMSpec::resetConversation);
24
+ prototype.registerHybridMethod("isReady", &HybridLiteRTLMSpec::isReady);
25
+ prototype.registerHybridMethod("getStats", &HybridLiteRTLMSpec::getStats);
26
+ prototype.registerHybridMethod("close", &HybridLiteRTLMSpec::close);
27
+ });
28
+ }
29
+
30
+ } // namespace margelo::nitro::litertlm