react-native-litert-lm 0.3.7 → 0.4.1

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 (88) hide show
  1. package/README.md +153 -135
  2. package/android/build.gradle +12 -0
  3. package/android/src/main/AndroidManifest.xml +8 -0
  4. package/android/src/main/java/com/margelo/nitro/dev/litert/litertlm/HybridLiteRTLM.kt +276 -62
  5. package/android/src/main/java/dev/litert/litertlm/LiteRTLMPackage.kt +19 -2
  6. package/android/src/test/java/com/margelo/nitro/core/Promise.kt +46 -0
  7. package/android/src/test/java/com/margelo/nitro/dev/litert/litertlm/HybridLiteRTLMTest.kt +105 -0
  8. package/ios/HybridLiteRTLM.swift +1344 -0
  9. package/ios/Tests/HybridLiteRTLMTests.swift +113 -0
  10. package/lib/__mocks__/react-native-nitro-modules.d.ts +65 -0
  11. package/lib/__mocks__/react-native-nitro-modules.js +60 -0
  12. package/lib/__tests__/hooks.test.d.ts +1 -0
  13. package/lib/__tests__/hooks.test.js +124 -0
  14. package/lib/__tests__/memoryTracker.test.d.ts +1 -0
  15. package/lib/__tests__/memoryTracker.test.js +74 -0
  16. package/lib/__tests__/modelFactory.test.d.ts +1 -0
  17. package/lib/__tests__/modelFactory.test.js +68 -0
  18. package/lib/hooks.js +27 -3
  19. package/lib/index.d.ts +6 -2
  20. package/lib/index.js +8 -8
  21. package/lib/modelFactory.js +82 -63
  22. package/lib/specs/LiteRTLM.nitro.d.ts +87 -2
  23. package/nitrogen/generated/android/LiteRTLMOnLoad.cpp +2 -2
  24. package/nitrogen/generated/android/c++/JHybridLiteRTLMSpec.cpp +94 -9
  25. package/nitrogen/generated/android/c++/JHybridLiteRTLMSpec.hpp +5 -1
  26. package/nitrogen/generated/android/c++/JLLMConfig.hpp +40 -3
  27. package/nitrogen/generated/android/c++/JMultimodalPart.hpp +74 -0
  28. package/nitrogen/generated/android/c++/JPartType.hpp +61 -0
  29. package/nitrogen/generated/android/c++/JToolDefinition.hpp +65 -0
  30. package/nitrogen/generated/android/kotlin/com/margelo/nitro/dev/litert/litertlm/GenerationStats.kt +23 -0
  31. package/nitrogen/generated/android/kotlin/com/margelo/nitro/dev/litert/litertlm/HybridLiteRTLMSpec.kt +28 -2
  32. package/nitrogen/generated/android/kotlin/com/margelo/nitro/dev/litert/litertlm/LLMConfig.kt +46 -3
  33. package/nitrogen/generated/android/kotlin/com/margelo/nitro/dev/litert/litertlm/MemoryUsage.kt +19 -0
  34. package/nitrogen/generated/android/kotlin/com/margelo/nitro/dev/litert/litertlm/Message.kt +15 -0
  35. package/nitrogen/generated/android/kotlin/com/margelo/nitro/dev/litert/litertlm/MultimodalPart.kt +66 -0
  36. package/nitrogen/generated/android/kotlin/com/margelo/nitro/dev/litert/litertlm/PartType.kt +24 -0
  37. package/nitrogen/generated/android/kotlin/com/margelo/nitro/dev/litert/litertlm/ToolDefinition.kt +61 -0
  38. package/nitrogen/generated/ios/LiteRTLM-Swift-Cxx-Bridge.cpp +57 -1
  39. package/nitrogen/generated/ios/LiteRTLM-Swift-Cxx-Bridge.hpp +414 -3
  40. package/nitrogen/generated/ios/LiteRTLM-Swift-Cxx-Umbrella.hpp +41 -3
  41. package/nitrogen/generated/ios/LiteRTLMAutolinking.mm +4 -6
  42. package/nitrogen/generated/ios/LiteRTLMAutolinking.swift +10 -0
  43. package/nitrogen/generated/ios/c++/HybridLiteRTLMSpecSwift.cpp +11 -0
  44. package/nitrogen/generated/ios/c++/HybridLiteRTLMSpecSwift.hpp +240 -0
  45. package/nitrogen/generated/ios/swift/Backend.swift +44 -0
  46. package/nitrogen/generated/ios/swift/Func_void.swift +46 -0
  47. package/nitrogen/generated/ios/swift/Func_void_double.swift +46 -0
  48. package/nitrogen/generated/ios/swift/Func_void_std__exception_ptr.swift +46 -0
  49. package/nitrogen/generated/ios/swift/Func_void_std__string.swift +46 -0
  50. package/nitrogen/generated/ios/swift/Func_void_std__string_bool.swift +46 -0
  51. package/nitrogen/generated/ios/swift/GenerationStats.swift +54 -0
  52. package/nitrogen/generated/ios/swift/HybridLiteRTLMSpec.swift +71 -0
  53. package/nitrogen/generated/ios/swift/HybridLiteRTLMSpec_cxx.swift +431 -0
  54. package/nitrogen/generated/ios/swift/LLMConfig.swift +203 -0
  55. package/nitrogen/generated/ios/swift/MemoryUsage.swift +44 -0
  56. package/nitrogen/generated/ios/swift/Message.swift +34 -0
  57. package/nitrogen/generated/ios/swift/MultimodalPart.swift +83 -0
  58. package/nitrogen/generated/ios/swift/PartType.swift +44 -0
  59. package/nitrogen/generated/ios/swift/Role.swift +44 -0
  60. package/nitrogen/generated/ios/swift/ToolDefinition.swift +39 -0
  61. package/nitrogen/generated/shared/c++/HybridLiteRTLMSpec.cpp +4 -0
  62. package/nitrogen/generated/shared/c++/HybridLiteRTLMSpec.hpp +9 -2
  63. package/nitrogen/generated/shared/c++/LLMConfig.hpp +22 -2
  64. package/nitrogen/generated/shared/c++/MultimodalPart.hpp +99 -0
  65. package/nitrogen/generated/shared/c++/PartType.hpp +80 -0
  66. package/nitrogen/generated/shared/c++/ToolDefinition.hpp +91 -0
  67. package/package.json +22 -11
  68. package/react-native-litert-lm.podspec +17 -19
  69. package/scripts/download-ios-frameworks.sh +17 -50
  70. package/scripts/framework-source.js +46 -0
  71. package/scripts/postinstall.js +40 -18
  72. package/src/__mocks__/react-native-nitro-modules.ts +58 -0
  73. package/src/__tests__/hooks.test.ts +153 -0
  74. package/src/__tests__/memoryTracker.test.ts +87 -0
  75. package/src/__tests__/modelFactory.test.ts +96 -0
  76. package/src/hooks.ts +29 -7
  77. package/src/index.ts +7 -10
  78. package/src/modelFactory.ts +104 -80
  79. package/src/specs/LiteRTLM.nitro.ts +106 -2
  80. package/cpp/HybridLiteRTLM.cpp +0 -939
  81. package/cpp/HybridLiteRTLM.hpp +0 -169
  82. package/cpp/IOSDownloadHelper.h +0 -24
  83. package/ios/IOSDownloadHelper.mm +0 -129
  84. package/scripts/build-ios-engine.sh +0 -302
  85. package/scripts/stubs/cxx_bridge_stubs.cc +0 -224
  86. package/scripts/stubs/gemma_model_constraint_provider.cc +0 -46
  87. package/scripts/stubs/llguidance_stubs.c +0 -101
  88. package/src/templates.ts +0 -105
@@ -0,0 +1,240 @@
1
+ ///
2
+ /// HybridLiteRTLMSpecSwift.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
+ #include "HybridLiteRTLMSpec.hpp"
11
+
12
+ // Forward declaration of `HybridLiteRTLMSpec_cxx` to properly resolve imports.
13
+ namespace LiteRTLM { class HybridLiteRTLMSpec_cxx; }
14
+
15
+ // Forward declaration of `LLMConfig` to properly resolve imports.
16
+ namespace margelo::nitro::litertlm { struct LLMConfig; }
17
+ // Forward declaration of `Backend` to properly resolve imports.
18
+ namespace margelo::nitro::litertlm { enum class Backend; }
19
+ // Forward declaration of `ToolDefinition` to properly resolve imports.
20
+ namespace margelo::nitro::litertlm { struct ToolDefinition; }
21
+ // Forward declaration of `MultimodalPart` to properly resolve imports.
22
+ namespace margelo::nitro::litertlm { struct MultimodalPart; }
23
+ // Forward declaration of `PartType` to properly resolve imports.
24
+ namespace margelo::nitro::litertlm { enum class PartType; }
25
+ // Forward declaration of `ArrayBufferHolder` to properly resolve imports.
26
+ namespace NitroModules { class ArrayBufferHolder; }
27
+ // Forward declaration of `Message` to properly resolve imports.
28
+ namespace margelo::nitro::litertlm { struct Message; }
29
+ // Forward declaration of `Role` to properly resolve imports.
30
+ namespace margelo::nitro::litertlm { enum class Role; }
31
+ // Forward declaration of `GenerationStats` to properly resolve imports.
32
+ namespace margelo::nitro::litertlm { struct GenerationStats; }
33
+ // Forward declaration of `MemoryUsage` to properly resolve imports.
34
+ namespace margelo::nitro::litertlm { struct MemoryUsage; }
35
+
36
+ #include <NitroModules/Promise.hpp>
37
+ #include <string>
38
+ #include "LLMConfig.hpp"
39
+ #include <optional>
40
+ #include "Backend.hpp"
41
+ #include "ToolDefinition.hpp"
42
+ #include <vector>
43
+ #include <functional>
44
+ #include "MultimodalPart.hpp"
45
+ #include "PartType.hpp"
46
+ #include <NitroModules/ArrayBuffer.hpp>
47
+ #include <NitroModules/ArrayBufferHolder.hpp>
48
+ #include "Message.hpp"
49
+ #include "Role.hpp"
50
+ #include "GenerationStats.hpp"
51
+ #include "MemoryUsage.hpp"
52
+
53
+ #include "LiteRTLM-Swift-Cxx-Umbrella.hpp"
54
+
55
+ namespace margelo::nitro::litertlm {
56
+
57
+ /**
58
+ * The C++ part of HybridLiteRTLMSpec_cxx.swift.
59
+ *
60
+ * HybridLiteRTLMSpecSwift (C++) accesses HybridLiteRTLMSpec_cxx (Swift), and might
61
+ * contain some additional bridging code for C++ <> Swift interop.
62
+ *
63
+ * Since this obviously introduces an overhead, I hope at some point in
64
+ * the future, HybridLiteRTLMSpec_cxx can directly inherit from the C++ class HybridLiteRTLMSpec
65
+ * to simplify the whole structure and memory management.
66
+ */
67
+ class HybridLiteRTLMSpecSwift: public virtual HybridLiteRTLMSpec {
68
+ public:
69
+ // Constructor from a Swift instance
70
+ explicit HybridLiteRTLMSpecSwift(const LiteRTLM::HybridLiteRTLMSpec_cxx& swiftPart):
71
+ HybridObject(HybridLiteRTLMSpec::TAG),
72
+ _swiftPart(swiftPart) { }
73
+
74
+ public:
75
+ // Get the Swift part
76
+ inline LiteRTLM::HybridLiteRTLMSpec_cxx& getSwiftPart() noexcept {
77
+ return _swiftPart;
78
+ }
79
+
80
+ public:
81
+ inline size_t getExternalMemorySize() noexcept override {
82
+ return _swiftPart.getMemorySize();
83
+ }
84
+ bool equals(const std::shared_ptr<HybridObject>& other) override {
85
+ if (auto otherCast = std::dynamic_pointer_cast<HybridLiteRTLMSpecSwift>(other)) {
86
+ return _swiftPart.equals(otherCast->_swiftPart);
87
+ }
88
+ return false;
89
+ }
90
+ void dispose() noexcept override {
91
+ _swiftPart.dispose();
92
+ }
93
+ std::string toString() override {
94
+ return _swiftPart.toString();
95
+ }
96
+
97
+ public:
98
+ // Properties
99
+
100
+
101
+ public:
102
+ // Methods
103
+ inline std::shared_ptr<Promise<void>> loadModel(const std::string& modelPath, const std::optional<LLMConfig>& config) override {
104
+ auto __result = _swiftPart.loadModel(modelPath, config);
105
+ if (__result.hasError()) [[unlikely]] {
106
+ std::rethrow_exception(__result.error());
107
+ }
108
+ auto __value = std::move(__result.value());
109
+ return __value;
110
+ }
111
+ inline std::shared_ptr<Promise<std::string>> sendMessage(const std::string& message) override {
112
+ auto __result = _swiftPart.sendMessage(message);
113
+ if (__result.hasError()) [[unlikely]] {
114
+ std::rethrow_exception(__result.error());
115
+ }
116
+ auto __value = std::move(__result.value());
117
+ return __value;
118
+ }
119
+ inline std::shared_ptr<Promise<std::string>> sendMessageWithImage(const std::string& message, const std::string& imagePath) override {
120
+ auto __result = _swiftPart.sendMessageWithImage(message, imagePath);
121
+ if (__result.hasError()) [[unlikely]] {
122
+ std::rethrow_exception(__result.error());
123
+ }
124
+ auto __value = std::move(__result.value());
125
+ return __value;
126
+ }
127
+ inline std::shared_ptr<Promise<void>> sendMessageWithImageAsync(const std::string& message, const std::string& imagePath, const std::function<void(const std::string& /* token */, bool /* done */)>& onToken) override {
128
+ auto __result = _swiftPart.sendMessageWithImageAsync(message, imagePath, onToken);
129
+ if (__result.hasError()) [[unlikely]] {
130
+ std::rethrow_exception(__result.error());
131
+ }
132
+ auto __value = std::move(__result.value());
133
+ return __value;
134
+ }
135
+ inline std::shared_ptr<Promise<std::string>> downloadModel(const std::string& url, const std::string& fileName, const std::optional<std::function<void(double /* progress */)>>& onProgress) override {
136
+ auto __result = _swiftPart.downloadModel(url, fileName, onProgress);
137
+ if (__result.hasError()) [[unlikely]] {
138
+ std::rethrow_exception(__result.error());
139
+ }
140
+ auto __value = std::move(__result.value());
141
+ return __value;
142
+ }
143
+ inline std::shared_ptr<Promise<void>> deleteModel(const std::string& fileName) override {
144
+ auto __result = _swiftPart.deleteModel(fileName);
145
+ if (__result.hasError()) [[unlikely]] {
146
+ std::rethrow_exception(__result.error());
147
+ }
148
+ auto __value = std::move(__result.value());
149
+ return __value;
150
+ }
151
+ inline std::shared_ptr<Promise<std::string>> sendMessageWithAudio(const std::string& message, const std::string& audioPath) override {
152
+ auto __result = _swiftPart.sendMessageWithAudio(message, audioPath);
153
+ if (__result.hasError()) [[unlikely]] {
154
+ std::rethrow_exception(__result.error());
155
+ }
156
+ auto __value = std::move(__result.value());
157
+ return __value;
158
+ }
159
+ inline std::shared_ptr<Promise<void>> sendMessageWithAudioAsync(const std::string& message, const std::string& audioPath, const std::function<void(const std::string& /* token */, bool /* done */)>& onToken) override {
160
+ auto __result = _swiftPart.sendMessageWithAudioAsync(message, audioPath, onToken);
161
+ if (__result.hasError()) [[unlikely]] {
162
+ std::rethrow_exception(__result.error());
163
+ }
164
+ auto __value = std::move(__result.value());
165
+ return __value;
166
+ }
167
+ inline std::shared_ptr<Promise<std::string>> sendMultimodalMessage(const std::vector<MultimodalPart>& parts) override {
168
+ auto __result = _swiftPart.sendMultimodalMessage(parts);
169
+ if (__result.hasError()) [[unlikely]] {
170
+ std::rethrow_exception(__result.error());
171
+ }
172
+ auto __value = std::move(__result.value());
173
+ return __value;
174
+ }
175
+ inline std::shared_ptr<Promise<void>> sendMessageAsync(const std::string& message, const std::function<void(const std::string& /* token */, bool /* done */)>& onToken) override {
176
+ auto __result = _swiftPart.sendMessageAsync(message, onToken);
177
+ if (__result.hasError()) [[unlikely]] {
178
+ std::rethrow_exception(__result.error());
179
+ }
180
+ auto __value = std::move(__result.value());
181
+ return __value;
182
+ }
183
+ inline std::vector<Message> getHistory() override {
184
+ auto __result = _swiftPart.getHistory();
185
+ if (__result.hasError()) [[unlikely]] {
186
+ std::rethrow_exception(__result.error());
187
+ }
188
+ auto __value = std::move(__result.value());
189
+ return __value;
190
+ }
191
+ inline void resetConversation() override {
192
+ auto __result = _swiftPart.resetConversation();
193
+ if (__result.hasError()) [[unlikely]] {
194
+ std::rethrow_exception(__result.error());
195
+ }
196
+ }
197
+ inline bool isReady() override {
198
+ auto __result = _swiftPart.isReady();
199
+ if (__result.hasError()) [[unlikely]] {
200
+ std::rethrow_exception(__result.error());
201
+ }
202
+ auto __value = std::move(__result.value());
203
+ return __value;
204
+ }
205
+ inline GenerationStats getStats() override {
206
+ auto __result = _swiftPart.getStats();
207
+ if (__result.hasError()) [[unlikely]] {
208
+ std::rethrow_exception(__result.error());
209
+ }
210
+ auto __value = std::move(__result.value());
211
+ return __value;
212
+ }
213
+ inline double countTokens(const std::string& text) override {
214
+ auto __result = _swiftPart.countTokens(text);
215
+ if (__result.hasError()) [[unlikely]] {
216
+ std::rethrow_exception(__result.error());
217
+ }
218
+ auto __value = std::move(__result.value());
219
+ return __value;
220
+ }
221
+ inline MemoryUsage getMemoryUsage() override {
222
+ auto __result = _swiftPart.getMemoryUsage();
223
+ if (__result.hasError()) [[unlikely]] {
224
+ std::rethrow_exception(__result.error());
225
+ }
226
+ auto __value = std::move(__result.value());
227
+ return __value;
228
+ }
229
+ inline void close() override {
230
+ auto __result = _swiftPart.close();
231
+ if (__result.hasError()) [[unlikely]] {
232
+ std::rethrow_exception(__result.error());
233
+ }
234
+ }
235
+
236
+ private:
237
+ LiteRTLM::HybridLiteRTLMSpec_cxx _swiftPart;
238
+ };
239
+
240
+ } // namespace margelo::nitro::litertlm
@@ -0,0 +1,44 @@
1
+ ///
2
+ /// Backend.swift
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
+ /**
9
+ * Represents the JS union `Backend`, backed by a C++ enum.
10
+ */
11
+ public typealias Backend = margelo.nitro.litertlm.Backend
12
+
13
+ public extension Backend {
14
+ /**
15
+ * Get a Backend for the given String value, or
16
+ * return `nil` if the given value was invalid/unknown.
17
+ */
18
+ init?(fromString string: String) {
19
+ switch string {
20
+ case "cpu":
21
+ self = .cpu
22
+ case "gpu":
23
+ self = .gpu
24
+ case "npu":
25
+ self = .npu
26
+ default:
27
+ return nil
28
+ }
29
+ }
30
+
31
+ /**
32
+ * Get the String value this Backend represents.
33
+ */
34
+ var stringValue: String {
35
+ switch self {
36
+ case .cpu:
37
+ return "cpu"
38
+ case .gpu:
39
+ return "gpu"
40
+ case .npu:
41
+ return "npu"
42
+ }
43
+ }
44
+ }
@@ -0,0 +1,46 @@
1
+ ///
2
+ /// Func_void.swift
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
+ import NitroModules
9
+
10
+ /**
11
+ * Wraps a Swift `() -> Void` as a class.
12
+ * This class can be used from C++, e.g. to wrap the Swift closure as a `std::function`.
13
+ */
14
+ public final class Func_void {
15
+ public typealias bridge = margelo.nitro.litertlm.bridge.swift
16
+
17
+ private let closure: () -> Void
18
+
19
+ public init(_ closure: @escaping () -> Void) {
20
+ self.closure = closure
21
+ }
22
+
23
+ @inline(__always)
24
+ public func call() -> Void {
25
+ self.closure()
26
+ }
27
+
28
+ /**
29
+ * Casts this instance to a retained unsafe raw pointer.
30
+ * This acquires one additional strong reference on the object!
31
+ */
32
+ @inline(__always)
33
+ public func toUnsafe() -> UnsafeMutableRawPointer {
34
+ return Unmanaged.passRetained(self).toOpaque()
35
+ }
36
+
37
+ /**
38
+ * Casts an unsafe pointer to a `Func_void`.
39
+ * The pointer has to be a retained opaque `Unmanaged<Func_void>`.
40
+ * This removes one strong reference from the object!
41
+ */
42
+ @inline(__always)
43
+ public static func fromUnsafe(_ pointer: UnsafeMutableRawPointer) -> Func_void {
44
+ return Unmanaged<Func_void>.fromOpaque(pointer).takeRetainedValue()
45
+ }
46
+ }
@@ -0,0 +1,46 @@
1
+ ///
2
+ /// Func_void_double.swift
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
+ import NitroModules
9
+
10
+ /**
11
+ * Wraps a Swift `(_ progress: Double) -> Void` as a class.
12
+ * This class can be used from C++, e.g. to wrap the Swift closure as a `std::function`.
13
+ */
14
+ public final class Func_void_double {
15
+ public typealias bridge = margelo.nitro.litertlm.bridge.swift
16
+
17
+ private let closure: (_ progress: Double) -> Void
18
+
19
+ public init(_ closure: @escaping (_ progress: Double) -> Void) {
20
+ self.closure = closure
21
+ }
22
+
23
+ @inline(__always)
24
+ public func call(progress: Double) -> Void {
25
+ self.closure(progress)
26
+ }
27
+
28
+ /**
29
+ * Casts this instance to a retained unsafe raw pointer.
30
+ * This acquires one additional strong reference on the object!
31
+ */
32
+ @inline(__always)
33
+ public func toUnsafe() -> UnsafeMutableRawPointer {
34
+ return Unmanaged.passRetained(self).toOpaque()
35
+ }
36
+
37
+ /**
38
+ * Casts an unsafe pointer to a `Func_void_double`.
39
+ * The pointer has to be a retained opaque `Unmanaged<Func_void_double>`.
40
+ * This removes one strong reference from the object!
41
+ */
42
+ @inline(__always)
43
+ public static func fromUnsafe(_ pointer: UnsafeMutableRawPointer) -> Func_void_double {
44
+ return Unmanaged<Func_void_double>.fromOpaque(pointer).takeRetainedValue()
45
+ }
46
+ }
@@ -0,0 +1,46 @@
1
+ ///
2
+ /// Func_void_std__exception_ptr.swift
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
+ import NitroModules
9
+
10
+ /**
11
+ * Wraps a Swift `(_ error: Error) -> Void` as a class.
12
+ * This class can be used from C++, e.g. to wrap the Swift closure as a `std::function`.
13
+ */
14
+ public final class Func_void_std__exception_ptr {
15
+ public typealias bridge = margelo.nitro.litertlm.bridge.swift
16
+
17
+ private let closure: (_ error: Error) -> Void
18
+
19
+ public init(_ closure: @escaping (_ error: Error) -> Void) {
20
+ self.closure = closure
21
+ }
22
+
23
+ @inline(__always)
24
+ public func call(error: std.exception_ptr) -> Void {
25
+ self.closure(RuntimeError.from(cppError: error))
26
+ }
27
+
28
+ /**
29
+ * Casts this instance to a retained unsafe raw pointer.
30
+ * This acquires one additional strong reference on the object!
31
+ */
32
+ @inline(__always)
33
+ public func toUnsafe() -> UnsafeMutableRawPointer {
34
+ return Unmanaged.passRetained(self).toOpaque()
35
+ }
36
+
37
+ /**
38
+ * Casts an unsafe pointer to a `Func_void_std__exception_ptr`.
39
+ * The pointer has to be a retained opaque `Unmanaged<Func_void_std__exception_ptr>`.
40
+ * This removes one strong reference from the object!
41
+ */
42
+ @inline(__always)
43
+ public static func fromUnsafe(_ pointer: UnsafeMutableRawPointer) -> Func_void_std__exception_ptr {
44
+ return Unmanaged<Func_void_std__exception_ptr>.fromOpaque(pointer).takeRetainedValue()
45
+ }
46
+ }
@@ -0,0 +1,46 @@
1
+ ///
2
+ /// Func_void_std__string.swift
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
+ import NitroModules
9
+
10
+ /**
11
+ * Wraps a Swift `(_ value: String) -> Void` as a class.
12
+ * This class can be used from C++, e.g. to wrap the Swift closure as a `std::function`.
13
+ */
14
+ public final class Func_void_std__string {
15
+ public typealias bridge = margelo.nitro.litertlm.bridge.swift
16
+
17
+ private let closure: (_ value: String) -> Void
18
+
19
+ public init(_ closure: @escaping (_ value: String) -> Void) {
20
+ self.closure = closure
21
+ }
22
+
23
+ @inline(__always)
24
+ public func call(value: std.string) -> Void {
25
+ self.closure(String(value))
26
+ }
27
+
28
+ /**
29
+ * Casts this instance to a retained unsafe raw pointer.
30
+ * This acquires one additional strong reference on the object!
31
+ */
32
+ @inline(__always)
33
+ public func toUnsafe() -> UnsafeMutableRawPointer {
34
+ return Unmanaged.passRetained(self).toOpaque()
35
+ }
36
+
37
+ /**
38
+ * Casts an unsafe pointer to a `Func_void_std__string`.
39
+ * The pointer has to be a retained opaque `Unmanaged<Func_void_std__string>`.
40
+ * This removes one strong reference from the object!
41
+ */
42
+ @inline(__always)
43
+ public static func fromUnsafe(_ pointer: UnsafeMutableRawPointer) -> Func_void_std__string {
44
+ return Unmanaged<Func_void_std__string>.fromOpaque(pointer).takeRetainedValue()
45
+ }
46
+ }
@@ -0,0 +1,46 @@
1
+ ///
2
+ /// Func_void_std__string_bool.swift
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
+ import NitroModules
9
+
10
+ /**
11
+ * Wraps a Swift `(_ token: String, _ done: Bool) -> Void` as a class.
12
+ * This class can be used from C++, e.g. to wrap the Swift closure as a `std::function`.
13
+ */
14
+ public final class Func_void_std__string_bool {
15
+ public typealias bridge = margelo.nitro.litertlm.bridge.swift
16
+
17
+ private let closure: (_ token: String, _ done: Bool) -> Void
18
+
19
+ public init(_ closure: @escaping (_ token: String, _ done: Bool) -> Void) {
20
+ self.closure = closure
21
+ }
22
+
23
+ @inline(__always)
24
+ public func call(token: std.string, done: Bool) -> Void {
25
+ self.closure(String(token), done)
26
+ }
27
+
28
+ /**
29
+ * Casts this instance to a retained unsafe raw pointer.
30
+ * This acquires one additional strong reference on the object!
31
+ */
32
+ @inline(__always)
33
+ public func toUnsafe() -> UnsafeMutableRawPointer {
34
+ return Unmanaged.passRetained(self).toOpaque()
35
+ }
36
+
37
+ /**
38
+ * Casts an unsafe pointer to a `Func_void_std__string_bool`.
39
+ * The pointer has to be a retained opaque `Unmanaged<Func_void_std__string_bool>`.
40
+ * This removes one strong reference from the object!
41
+ */
42
+ @inline(__always)
43
+ public static func fromUnsafe(_ pointer: UnsafeMutableRawPointer) -> Func_void_std__string_bool {
44
+ return Unmanaged<Func_void_std__string_bool>.fromOpaque(pointer).takeRetainedValue()
45
+ }
46
+ }
@@ -0,0 +1,54 @@
1
+ ///
2
+ /// GenerationStats.swift
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
+ import NitroModules
9
+
10
+ /**
11
+ * Represents an instance of `GenerationStats`, backed by a C++ struct.
12
+ */
13
+ public typealias GenerationStats = margelo.nitro.litertlm.GenerationStats
14
+
15
+ public extension GenerationStats {
16
+ private typealias bridge = margelo.nitro.litertlm.bridge.swift
17
+
18
+ /**
19
+ * Create a new instance of `GenerationStats`.
20
+ */
21
+ init(promptTokens: Double, completionTokens: Double, totalTokens: Double, timeToFirstToken: Double, totalTime: Double, tokensPerSecond: Double) {
22
+ self.init(promptTokens, completionTokens, totalTokens, timeToFirstToken, totalTime, tokensPerSecond)
23
+ }
24
+
25
+ @inline(__always)
26
+ var promptTokens: Double {
27
+ return self.__promptTokens
28
+ }
29
+
30
+ @inline(__always)
31
+ var completionTokens: Double {
32
+ return self.__completionTokens
33
+ }
34
+
35
+ @inline(__always)
36
+ var totalTokens: Double {
37
+ return self.__totalTokens
38
+ }
39
+
40
+ @inline(__always)
41
+ var timeToFirstToken: Double {
42
+ return self.__timeToFirstToken
43
+ }
44
+
45
+ @inline(__always)
46
+ var totalTime: Double {
47
+ return self.__totalTime
48
+ }
49
+
50
+ @inline(__always)
51
+ var tokensPerSecond: Double {
52
+ return self.__tokensPerSecond
53
+ }
54
+ }
@@ -0,0 +1,71 @@
1
+ ///
2
+ /// HybridLiteRTLMSpec.swift
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
+ import NitroModules
9
+
10
+ /// See ``HybridLiteRTLMSpec``
11
+ public protocol HybridLiteRTLMSpec_protocol: HybridObject {
12
+ // Properties
13
+
14
+
15
+ // Methods
16
+ func loadModel(modelPath: String, config: LLMConfig?) throws -> Promise<Void>
17
+ func sendMessage(message: String) throws -> Promise<String>
18
+ func sendMessageWithImage(message: String, imagePath: String) throws -> Promise<String>
19
+ func sendMessageWithImageAsync(message: String, imagePath: String, onToken: @escaping (_ token: String, _ done: Bool) -> Void) throws -> Promise<Void>
20
+ func downloadModel(url: String, fileName: String, onProgress: ((_ progress: Double) -> Void)?) throws -> Promise<String>
21
+ func deleteModel(fileName: String) throws -> Promise<Void>
22
+ func sendMessageWithAudio(message: String, audioPath: String) throws -> Promise<String>
23
+ func sendMessageWithAudioAsync(message: String, audioPath: String, onToken: @escaping (_ token: String, _ done: Bool) -> Void) throws -> Promise<Void>
24
+ func sendMultimodalMessage(parts: [MultimodalPart]) throws -> Promise<String>
25
+ func sendMessageAsync(message: String, onToken: @escaping (_ token: String, _ done: Bool) -> Void) throws -> Promise<Void>
26
+ func getHistory() throws -> [Message]
27
+ func resetConversation() throws -> Void
28
+ func isReady() throws -> Bool
29
+ func getStats() throws -> GenerationStats
30
+ func countTokens(text: String) throws -> Double
31
+ func getMemoryUsage() throws -> MemoryUsage
32
+ func close() throws -> Void
33
+ }
34
+
35
+ public extension HybridLiteRTLMSpec_protocol {
36
+ /// Default implementation of ``HybridObject.toString``
37
+ func toString() -> String {
38
+ return "[HybridObject LiteRTLM]"
39
+ }
40
+ }
41
+
42
+ /// See ``HybridLiteRTLMSpec``
43
+ open class HybridLiteRTLMSpec_base {
44
+ private weak var cxxWrapper: HybridLiteRTLMSpec_cxx? = nil
45
+ public init() { }
46
+ public func getCxxWrapper() -> HybridLiteRTLMSpec_cxx {
47
+ #if DEBUG
48
+ guard self is any HybridLiteRTLMSpec else {
49
+ fatalError("`self` is not a `HybridLiteRTLMSpec`! Did you accidentally inherit from `HybridLiteRTLMSpec_base` instead of `HybridLiteRTLMSpec`?")
50
+ }
51
+ #endif
52
+ if let cxxWrapper = self.cxxWrapper {
53
+ return cxxWrapper
54
+ } else {
55
+ let cxxWrapper = HybridLiteRTLMSpec_cxx(self as! any HybridLiteRTLMSpec)
56
+ self.cxxWrapper = cxxWrapper
57
+ return cxxWrapper
58
+ }
59
+ }
60
+ }
61
+
62
+ /**
63
+ * A Swift base-protocol representing the LiteRTLM HybridObject.
64
+ * Implement this protocol to create Swift-based instances of LiteRTLM.
65
+ * ```swift
66
+ * class HybridLiteRTLM : HybridLiteRTLMSpec {
67
+ * // ...
68
+ * }
69
+ * ```
70
+ */
71
+ public typealias HybridLiteRTLMSpec = HybridLiteRTLMSpec_protocol & HybridLiteRTLMSpec_base