@inferrlm/react-native-mlx 0.2.0-inferrlm.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 (71) hide show
  1. package/MLXReactNative.podspec +42 -0
  2. package/ios/Bridge.h +8 -0
  3. package/ios/Sources/HybridLLM.swift +245 -0
  4. package/ios/Sources/HybridModelManager.swift +77 -0
  5. package/ios/Sources/LLMError.swift +6 -0
  6. package/ios/Sources/MLXReactNative.h +16 -0
  7. package/ios/Sources/ModelDownloader.swift +103 -0
  8. package/lib/module/index.js +6 -0
  9. package/lib/module/index.js.map +1 -0
  10. package/lib/module/llm.js +125 -0
  11. package/lib/module/llm.js.map +1 -0
  12. package/lib/module/modelManager.js +79 -0
  13. package/lib/module/modelManager.js.map +1 -0
  14. package/lib/module/models.js +41 -0
  15. package/lib/module/models.js.map +1 -0
  16. package/lib/module/package.json +1 -0
  17. package/lib/module/specs/LLM.nitro.js +4 -0
  18. package/lib/module/specs/LLM.nitro.js.map +1 -0
  19. package/lib/module/specs/ModelManager.nitro.js +4 -0
  20. package/lib/module/specs/ModelManager.nitro.js.map +1 -0
  21. package/lib/typescript/package.json +1 -0
  22. package/lib/typescript/src/index.d.ts +6 -0
  23. package/lib/typescript/src/index.d.ts.map +1 -0
  24. package/lib/typescript/src/llm.d.ts +87 -0
  25. package/lib/typescript/src/llm.d.ts.map +1 -0
  26. package/lib/typescript/src/modelManager.d.ts +53 -0
  27. package/lib/typescript/src/modelManager.d.ts.map +1 -0
  28. package/lib/typescript/src/models.d.ts +29 -0
  29. package/lib/typescript/src/models.d.ts.map +1 -0
  30. package/lib/typescript/src/specs/LLM.nitro.d.ts +88 -0
  31. package/lib/typescript/src/specs/LLM.nitro.d.ts.map +1 -0
  32. package/lib/typescript/src/specs/ModelManager.nitro.d.ts +41 -0
  33. package/lib/typescript/src/specs/ModelManager.nitro.d.ts.map +1 -0
  34. package/nitrogen/generated/.gitattributes +1 -0
  35. package/nitrogen/generated/ios/MLXReactNative+autolinking.rb +60 -0
  36. package/nitrogen/generated/ios/MLXReactNative-Swift-Cxx-Bridge.cpp +98 -0
  37. package/nitrogen/generated/ios/MLXReactNative-Swift-Cxx-Bridge.hpp +399 -0
  38. package/nitrogen/generated/ios/MLXReactNative-Swift-Cxx-Umbrella.hpp +62 -0
  39. package/nitrogen/generated/ios/MLXReactNativeAutolinking.mm +41 -0
  40. package/nitrogen/generated/ios/MLXReactNativeAutolinking.swift +40 -0
  41. package/nitrogen/generated/ios/c++/HybridLLMSpecSwift.cpp +11 -0
  42. package/nitrogen/generated/ios/c++/HybridLLMSpecSwift.hpp +160 -0
  43. package/nitrogen/generated/ios/c++/HybridModelManagerSpecSwift.cpp +11 -0
  44. package/nitrogen/generated/ios/c++/HybridModelManagerSpecSwift.hpp +116 -0
  45. package/nitrogen/generated/ios/swift/Func_void.swift +47 -0
  46. package/nitrogen/generated/ios/swift/Func_void_bool.swift +47 -0
  47. package/nitrogen/generated/ios/swift/Func_void_double.swift +47 -0
  48. package/nitrogen/generated/ios/swift/Func_void_std__exception_ptr.swift +47 -0
  49. package/nitrogen/generated/ios/swift/Func_void_std__string.swift +47 -0
  50. package/nitrogen/generated/ios/swift/Func_void_std__vector_std__string_.swift +47 -0
  51. package/nitrogen/generated/ios/swift/GenerationStats.swift +69 -0
  52. package/nitrogen/generated/ios/swift/HybridLLMSpec.swift +67 -0
  53. package/nitrogen/generated/ios/swift/HybridLLMSpec_cxx.swift +285 -0
  54. package/nitrogen/generated/ios/swift/HybridModelManagerSpec.swift +60 -0
  55. package/nitrogen/generated/ios/swift/HybridModelManagerSpec_cxx.swift +234 -0
  56. package/nitrogen/generated/ios/swift/LLMLoadOptions.swift +138 -0
  57. package/nitrogen/generated/ios/swift/LLMMessage.swift +47 -0
  58. package/nitrogen/generated/shared/c++/GenerationStats.hpp +87 -0
  59. package/nitrogen/generated/shared/c++/HybridLLMSpec.cpp +35 -0
  60. package/nitrogen/generated/shared/c++/HybridLLMSpec.hpp +87 -0
  61. package/nitrogen/generated/shared/c++/HybridModelManagerSpec.cpp +27 -0
  62. package/nitrogen/generated/shared/c++/HybridModelManagerSpec.hpp +70 -0
  63. package/nitrogen/generated/shared/c++/LLMLoadOptions.hpp +87 -0
  64. package/nitrogen/generated/shared/c++/LLMMessage.hpp +79 -0
  65. package/package.json +142 -0
  66. package/src/index.ts +6 -0
  67. package/src/llm.ts +144 -0
  68. package/src/modelManager.ts +88 -0
  69. package/src/models.ts +45 -0
  70. package/src/specs/LLM.nitro.ts +98 -0
  71. package/src/specs/ModelManager.nitro.ts +44 -0
@@ -0,0 +1,67 @@
1
+ ///
2
+ /// HybridLLMSpec.swift
3
+ /// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
4
+ /// https://github.com/mrousavy/nitro
5
+ /// Copyright © 2025 Marc Rousavy @ Margelo
6
+ ///
7
+
8
+ import Foundation
9
+ import NitroModules
10
+
11
+ /// See ``HybridLLMSpec``
12
+ public protocol HybridLLMSpec_protocol: HybridObject {
13
+ // Properties
14
+ var isLoaded: Bool { get }
15
+ var isGenerating: Bool { get }
16
+ var modelId: String { get }
17
+ var debug: Bool { get set }
18
+ var systemPrompt: String { get set }
19
+
20
+ // Methods
21
+ func load(modelId: String, options: LLMLoadOptions?) throws -> Promise<Void>
22
+ func generate(prompt: String) throws -> Promise<String>
23
+ func stream(prompt: String, onToken: @escaping (_ token: String) -> Void) throws -> Promise<String>
24
+ func stop() throws -> Void
25
+ func unload() throws -> Void
26
+ func getLastGenerationStats() throws -> GenerationStats
27
+ func getHistory() throws -> [LLMMessage]
28
+ func clearHistory() throws -> Void
29
+ }
30
+
31
+ public extension HybridLLMSpec_protocol {
32
+ /// Default implementation of ``HybridObject.toString``
33
+ func toString() -> String {
34
+ return "[HybridObject LLM]"
35
+ }
36
+ }
37
+
38
+ /// See ``HybridLLMSpec``
39
+ open class HybridLLMSpec_base {
40
+ private weak var cxxWrapper: HybridLLMSpec_cxx? = nil
41
+ public init() { }
42
+ public func getCxxWrapper() -> HybridLLMSpec_cxx {
43
+ #if DEBUG
44
+ guard self is HybridLLMSpec else {
45
+ fatalError("`self` is not a `HybridLLMSpec`! Did you accidentally inherit from `HybridLLMSpec_base` instead of `HybridLLMSpec`?")
46
+ }
47
+ #endif
48
+ if let cxxWrapper = self.cxxWrapper {
49
+ return cxxWrapper
50
+ } else {
51
+ let cxxWrapper = HybridLLMSpec_cxx(self as! HybridLLMSpec)
52
+ self.cxxWrapper = cxxWrapper
53
+ return cxxWrapper
54
+ }
55
+ }
56
+ }
57
+
58
+ /**
59
+ * A Swift base-protocol representing the LLM HybridObject.
60
+ * Implement this protocol to create Swift-based instances of LLM.
61
+ * ```swift
62
+ * class HybridLLM : HybridLLMSpec {
63
+ * // ...
64
+ * }
65
+ * ```
66
+ */
67
+ public typealias HybridLLMSpec = HybridLLMSpec_protocol & HybridLLMSpec_base
@@ -0,0 +1,285 @@
1
+ ///
2
+ /// HybridLLMSpec_cxx.swift
3
+ /// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
4
+ /// https://github.com/mrousavy/nitro
5
+ /// Copyright © 2025 Marc Rousavy @ Margelo
6
+ ///
7
+
8
+ import Foundation
9
+ import NitroModules
10
+
11
+ /**
12
+ * A class implementation that bridges HybridLLMSpec over to C++.
13
+ * In C++, we cannot use Swift protocols - so we need to wrap it in a class to make it strongly defined.
14
+ *
15
+ * Also, some Swift types need to be bridged with special handling:
16
+ * - Enums need to be wrapped in Structs, otherwise they cannot be accessed bi-directionally (Swift bug: https://github.com/swiftlang/swift/issues/75330)
17
+ * - Other HybridObjects need to be wrapped/unwrapped from the Swift TCxx wrapper
18
+ * - Throwing methods need to be wrapped with a Result<T, Error> type, as exceptions cannot be propagated to C++
19
+ */
20
+ open class HybridLLMSpec_cxx {
21
+ /**
22
+ * The Swift <> C++ bridge's namespace (`margelo::nitro::mlxreactnative::bridge::swift`)
23
+ * from `MLXReactNative-Swift-Cxx-Bridge.hpp`.
24
+ * This contains specialized C++ templates, and C++ helper functions that can be accessed from Swift.
25
+ */
26
+ public typealias bridge = margelo.nitro.mlxreactnative.bridge.swift
27
+
28
+ /**
29
+ * Holds an instance of the `HybridLLMSpec` Swift protocol.
30
+ */
31
+ private var __implementation: any HybridLLMSpec
32
+
33
+ /**
34
+ * Holds a weak pointer to the C++ class that wraps the Swift class.
35
+ */
36
+ private var __cxxPart: bridge.std__weak_ptr_HybridLLMSpec_
37
+
38
+ /**
39
+ * Create a new `HybridLLMSpec_cxx` that wraps the given `HybridLLMSpec`.
40
+ * All properties and methods bridge to C++ types.
41
+ */
42
+ public init(_ implementation: any HybridLLMSpec) {
43
+ self.__implementation = implementation
44
+ self.__cxxPart = .init()
45
+ /* no base class */
46
+ }
47
+
48
+ /**
49
+ * Get the actual `HybridLLMSpec` instance this class wraps.
50
+ */
51
+ @inline(__always)
52
+ public func getHybridLLMSpec() -> any HybridLLMSpec {
53
+ return __implementation
54
+ }
55
+
56
+ /**
57
+ * Casts this instance to a retained unsafe raw pointer.
58
+ * This acquires one additional strong reference on the object!
59
+ */
60
+ public func toUnsafe() -> UnsafeMutableRawPointer {
61
+ return Unmanaged.passRetained(self).toOpaque()
62
+ }
63
+
64
+ /**
65
+ * Casts an unsafe pointer to a `HybridLLMSpec_cxx`.
66
+ * The pointer has to be a retained opaque `Unmanaged<HybridLLMSpec_cxx>`.
67
+ * This removes one strong reference from the object!
68
+ */
69
+ public class func fromUnsafe(_ pointer: UnsafeMutableRawPointer) -> HybridLLMSpec_cxx {
70
+ return Unmanaged<HybridLLMSpec_cxx>.fromOpaque(pointer).takeRetainedValue()
71
+ }
72
+
73
+ /**
74
+ * Gets (or creates) the C++ part of this Hybrid Object.
75
+ * The C++ part is a `std::shared_ptr<HybridLLMSpec>`.
76
+ */
77
+ public func getCxxPart() -> bridge.std__shared_ptr_HybridLLMSpec_ {
78
+ let cachedCxxPart = self.__cxxPart.lock()
79
+ if Bool(fromCxx: cachedCxxPart) {
80
+ return cachedCxxPart
81
+ } else {
82
+ let newCxxPart = bridge.create_std__shared_ptr_HybridLLMSpec_(self.toUnsafe())
83
+ __cxxPart = bridge.weakify_std__shared_ptr_HybridLLMSpec_(newCxxPart)
84
+ return newCxxPart
85
+ }
86
+ }
87
+
88
+
89
+
90
+ /**
91
+ * Get the memory size of the Swift class (plus size of any other allocations)
92
+ * so the JS VM can properly track it and garbage-collect the JS object if needed.
93
+ */
94
+ @inline(__always)
95
+ public var memorySize: Int {
96
+ return MemoryHelper.getSizeOf(self.__implementation) + self.__implementation.memorySize
97
+ }
98
+
99
+ /**
100
+ * Call dispose() on the Swift class.
101
+ * This _may_ be called manually from JS.
102
+ */
103
+ @inline(__always)
104
+ public func dispose() {
105
+ self.__implementation.dispose()
106
+ }
107
+
108
+ /**
109
+ * Call toString() on the Swift class.
110
+ */
111
+ @inline(__always)
112
+ public func toString() -> String {
113
+ return self.__implementation.toString()
114
+ }
115
+
116
+ // Properties
117
+ public final var isLoaded: Bool {
118
+ @inline(__always)
119
+ get {
120
+ return self.__implementation.isLoaded
121
+ }
122
+ }
123
+
124
+ public final var isGenerating: Bool {
125
+ @inline(__always)
126
+ get {
127
+ return self.__implementation.isGenerating
128
+ }
129
+ }
130
+
131
+ public final var modelId: std.string {
132
+ @inline(__always)
133
+ get {
134
+ return std.string(self.__implementation.modelId)
135
+ }
136
+ }
137
+
138
+ public final var debug: Bool {
139
+ @inline(__always)
140
+ get {
141
+ return self.__implementation.debug
142
+ }
143
+ @inline(__always)
144
+ set {
145
+ self.__implementation.debug = newValue
146
+ }
147
+ }
148
+
149
+ public final var systemPrompt: std.string {
150
+ @inline(__always)
151
+ get {
152
+ return std.string(self.__implementation.systemPrompt)
153
+ }
154
+ @inline(__always)
155
+ set {
156
+ self.__implementation.systemPrompt = String(newValue)
157
+ }
158
+ }
159
+
160
+ // Methods
161
+ @inline(__always)
162
+ public final func load(modelId: std.string, options: bridge.std__optional_LLMLoadOptions_) -> bridge.Result_std__shared_ptr_Promise_void___ {
163
+ do {
164
+ let __result = try self.__implementation.load(modelId: String(modelId), options: options.value)
165
+ let __resultCpp = { () -> bridge.std__shared_ptr_Promise_void__ in
166
+ let __promise = bridge.create_std__shared_ptr_Promise_void__()
167
+ let __promiseHolder = bridge.wrap_std__shared_ptr_Promise_void__(__promise)
168
+ __result
169
+ .then({ __result in __promiseHolder.resolve() })
170
+ .catch({ __error in __promiseHolder.reject(__error.toCpp()) })
171
+ return __promise
172
+ }()
173
+ return bridge.create_Result_std__shared_ptr_Promise_void___(__resultCpp)
174
+ } catch (let __error) {
175
+ let __exceptionPtr = __error.toCpp()
176
+ return bridge.create_Result_std__shared_ptr_Promise_void___(__exceptionPtr)
177
+ }
178
+ }
179
+
180
+ @inline(__always)
181
+ public final func generate(prompt: std.string) -> bridge.Result_std__shared_ptr_Promise_std__string___ {
182
+ do {
183
+ let __result = try self.__implementation.generate(prompt: String(prompt))
184
+ let __resultCpp = { () -> bridge.std__shared_ptr_Promise_std__string__ in
185
+ let __promise = bridge.create_std__shared_ptr_Promise_std__string__()
186
+ let __promiseHolder = bridge.wrap_std__shared_ptr_Promise_std__string__(__promise)
187
+ __result
188
+ .then({ __result in __promiseHolder.resolve(std.string(__result)) })
189
+ .catch({ __error in __promiseHolder.reject(__error.toCpp()) })
190
+ return __promise
191
+ }()
192
+ return bridge.create_Result_std__shared_ptr_Promise_std__string___(__resultCpp)
193
+ } catch (let __error) {
194
+ let __exceptionPtr = __error.toCpp()
195
+ return bridge.create_Result_std__shared_ptr_Promise_std__string___(__exceptionPtr)
196
+ }
197
+ }
198
+
199
+ @inline(__always)
200
+ public final func stream(prompt: std.string, onToken: bridge.Func_void_std__string) -> bridge.Result_std__shared_ptr_Promise_std__string___ {
201
+ do {
202
+ let __result = try self.__implementation.stream(prompt: String(prompt), onToken: { () -> (String) -> Void in
203
+ let __wrappedFunction = bridge.wrap_Func_void_std__string(onToken)
204
+ return { (__token: String) -> Void in
205
+ __wrappedFunction.call(std.string(__token))
206
+ }
207
+ }())
208
+ let __resultCpp = { () -> bridge.std__shared_ptr_Promise_std__string__ in
209
+ let __promise = bridge.create_std__shared_ptr_Promise_std__string__()
210
+ let __promiseHolder = bridge.wrap_std__shared_ptr_Promise_std__string__(__promise)
211
+ __result
212
+ .then({ __result in __promiseHolder.resolve(std.string(__result)) })
213
+ .catch({ __error in __promiseHolder.reject(__error.toCpp()) })
214
+ return __promise
215
+ }()
216
+ return bridge.create_Result_std__shared_ptr_Promise_std__string___(__resultCpp)
217
+ } catch (let __error) {
218
+ let __exceptionPtr = __error.toCpp()
219
+ return bridge.create_Result_std__shared_ptr_Promise_std__string___(__exceptionPtr)
220
+ }
221
+ }
222
+
223
+ @inline(__always)
224
+ public final func stop() -> bridge.Result_void_ {
225
+ do {
226
+ try self.__implementation.stop()
227
+ return bridge.create_Result_void_()
228
+ } catch (let __error) {
229
+ let __exceptionPtr = __error.toCpp()
230
+ return bridge.create_Result_void_(__exceptionPtr)
231
+ }
232
+ }
233
+
234
+ @inline(__always)
235
+ public final func unload() -> bridge.Result_void_ {
236
+ do {
237
+ try self.__implementation.unload()
238
+ return bridge.create_Result_void_()
239
+ } catch (let __error) {
240
+ let __exceptionPtr = __error.toCpp()
241
+ return bridge.create_Result_void_(__exceptionPtr)
242
+ }
243
+ }
244
+
245
+ @inline(__always)
246
+ public final func getLastGenerationStats() -> bridge.Result_GenerationStats_ {
247
+ do {
248
+ let __result = try self.__implementation.getLastGenerationStats()
249
+ let __resultCpp = __result
250
+ return bridge.create_Result_GenerationStats_(__resultCpp)
251
+ } catch (let __error) {
252
+ let __exceptionPtr = __error.toCpp()
253
+ return bridge.create_Result_GenerationStats_(__exceptionPtr)
254
+ }
255
+ }
256
+
257
+ @inline(__always)
258
+ public final func getHistory() -> bridge.Result_std__vector_LLMMessage__ {
259
+ do {
260
+ let __result = try self.__implementation.getHistory()
261
+ let __resultCpp = { () -> bridge.std__vector_LLMMessage_ in
262
+ var __vector = bridge.create_std__vector_LLMMessage_(__result.count)
263
+ for __item in __result {
264
+ __vector.push_back(__item)
265
+ }
266
+ return __vector
267
+ }()
268
+ return bridge.create_Result_std__vector_LLMMessage__(__resultCpp)
269
+ } catch (let __error) {
270
+ let __exceptionPtr = __error.toCpp()
271
+ return bridge.create_Result_std__vector_LLMMessage__(__exceptionPtr)
272
+ }
273
+ }
274
+
275
+ @inline(__always)
276
+ public final func clearHistory() -> bridge.Result_void_ {
277
+ do {
278
+ try self.__implementation.clearHistory()
279
+ return bridge.create_Result_void_()
280
+ } catch (let __error) {
281
+ let __exceptionPtr = __error.toCpp()
282
+ return bridge.create_Result_void_(__exceptionPtr)
283
+ }
284
+ }
285
+ }
@@ -0,0 +1,60 @@
1
+ ///
2
+ /// HybridModelManagerSpec.swift
3
+ /// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
4
+ /// https://github.com/mrousavy/nitro
5
+ /// Copyright © 2025 Marc Rousavy @ Margelo
6
+ ///
7
+
8
+ import Foundation
9
+ import NitroModules
10
+
11
+ /// See ``HybridModelManagerSpec``
12
+ public protocol HybridModelManagerSpec_protocol: HybridObject {
13
+ // Properties
14
+ var debug: Bool { get set }
15
+
16
+ // Methods
17
+ func download(modelId: String, progressCallback: @escaping (_ progress: Double) -> Void) throws -> Promise<String>
18
+ func isDownloaded(modelId: String) throws -> Promise<Bool>
19
+ func getDownloadedModels() throws -> Promise<[String]>
20
+ func deleteModel(modelId: String) throws -> Promise<Void>
21
+ func getModelPath(modelId: String) throws -> Promise<String>
22
+ }
23
+
24
+ public extension HybridModelManagerSpec_protocol {
25
+ /// Default implementation of ``HybridObject.toString``
26
+ func toString() -> String {
27
+ return "[HybridObject ModelManager]"
28
+ }
29
+ }
30
+
31
+ /// See ``HybridModelManagerSpec``
32
+ open class HybridModelManagerSpec_base {
33
+ private weak var cxxWrapper: HybridModelManagerSpec_cxx? = nil
34
+ public init() { }
35
+ public func getCxxWrapper() -> HybridModelManagerSpec_cxx {
36
+ #if DEBUG
37
+ guard self is HybridModelManagerSpec else {
38
+ fatalError("`self` is not a `HybridModelManagerSpec`! Did you accidentally inherit from `HybridModelManagerSpec_base` instead of `HybridModelManagerSpec`?")
39
+ }
40
+ #endif
41
+ if let cxxWrapper = self.cxxWrapper {
42
+ return cxxWrapper
43
+ } else {
44
+ let cxxWrapper = HybridModelManagerSpec_cxx(self as! HybridModelManagerSpec)
45
+ self.cxxWrapper = cxxWrapper
46
+ return cxxWrapper
47
+ }
48
+ }
49
+ }
50
+
51
+ /**
52
+ * A Swift base-protocol representing the ModelManager HybridObject.
53
+ * Implement this protocol to create Swift-based instances of ModelManager.
54
+ * ```swift
55
+ * class HybridModelManager : HybridModelManagerSpec {
56
+ * // ...
57
+ * }
58
+ * ```
59
+ */
60
+ public typealias HybridModelManagerSpec = HybridModelManagerSpec_protocol & HybridModelManagerSpec_base
@@ -0,0 +1,234 @@
1
+ ///
2
+ /// HybridModelManagerSpec_cxx.swift
3
+ /// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
4
+ /// https://github.com/mrousavy/nitro
5
+ /// Copyright © 2025 Marc Rousavy @ Margelo
6
+ ///
7
+
8
+ import Foundation
9
+ import NitroModules
10
+
11
+ /**
12
+ * A class implementation that bridges HybridModelManagerSpec over to C++.
13
+ * In C++, we cannot use Swift protocols - so we need to wrap it in a class to make it strongly defined.
14
+ *
15
+ * Also, some Swift types need to be bridged with special handling:
16
+ * - Enums need to be wrapped in Structs, otherwise they cannot be accessed bi-directionally (Swift bug: https://github.com/swiftlang/swift/issues/75330)
17
+ * - Other HybridObjects need to be wrapped/unwrapped from the Swift TCxx wrapper
18
+ * - Throwing methods need to be wrapped with a Result<T, Error> type, as exceptions cannot be propagated to C++
19
+ */
20
+ open class HybridModelManagerSpec_cxx {
21
+ /**
22
+ * The Swift <> C++ bridge's namespace (`margelo::nitro::mlxreactnative::bridge::swift`)
23
+ * from `MLXReactNative-Swift-Cxx-Bridge.hpp`.
24
+ * This contains specialized C++ templates, and C++ helper functions that can be accessed from Swift.
25
+ */
26
+ public typealias bridge = margelo.nitro.mlxreactnative.bridge.swift
27
+
28
+ /**
29
+ * Holds an instance of the `HybridModelManagerSpec` Swift protocol.
30
+ */
31
+ private var __implementation: any HybridModelManagerSpec
32
+
33
+ /**
34
+ * Holds a weak pointer to the C++ class that wraps the Swift class.
35
+ */
36
+ private var __cxxPart: bridge.std__weak_ptr_HybridModelManagerSpec_
37
+
38
+ /**
39
+ * Create a new `HybridModelManagerSpec_cxx` that wraps the given `HybridModelManagerSpec`.
40
+ * All properties and methods bridge to C++ types.
41
+ */
42
+ public init(_ implementation: any HybridModelManagerSpec) {
43
+ self.__implementation = implementation
44
+ self.__cxxPart = .init()
45
+ /* no base class */
46
+ }
47
+
48
+ /**
49
+ * Get the actual `HybridModelManagerSpec` instance this class wraps.
50
+ */
51
+ @inline(__always)
52
+ public func getHybridModelManagerSpec() -> any HybridModelManagerSpec {
53
+ return __implementation
54
+ }
55
+
56
+ /**
57
+ * Casts this instance to a retained unsafe raw pointer.
58
+ * This acquires one additional strong reference on the object!
59
+ */
60
+ public func toUnsafe() -> UnsafeMutableRawPointer {
61
+ return Unmanaged.passRetained(self).toOpaque()
62
+ }
63
+
64
+ /**
65
+ * Casts an unsafe pointer to a `HybridModelManagerSpec_cxx`.
66
+ * The pointer has to be a retained opaque `Unmanaged<HybridModelManagerSpec_cxx>`.
67
+ * This removes one strong reference from the object!
68
+ */
69
+ public class func fromUnsafe(_ pointer: UnsafeMutableRawPointer) -> HybridModelManagerSpec_cxx {
70
+ return Unmanaged<HybridModelManagerSpec_cxx>.fromOpaque(pointer).takeRetainedValue()
71
+ }
72
+
73
+ /**
74
+ * Gets (or creates) the C++ part of this Hybrid Object.
75
+ * The C++ part is a `std::shared_ptr<HybridModelManagerSpec>`.
76
+ */
77
+ public func getCxxPart() -> bridge.std__shared_ptr_HybridModelManagerSpec_ {
78
+ let cachedCxxPart = self.__cxxPart.lock()
79
+ if Bool(fromCxx: cachedCxxPart) {
80
+ return cachedCxxPart
81
+ } else {
82
+ let newCxxPart = bridge.create_std__shared_ptr_HybridModelManagerSpec_(self.toUnsafe())
83
+ __cxxPart = bridge.weakify_std__shared_ptr_HybridModelManagerSpec_(newCxxPart)
84
+ return newCxxPart
85
+ }
86
+ }
87
+
88
+
89
+
90
+ /**
91
+ * Get the memory size of the Swift class (plus size of any other allocations)
92
+ * so the JS VM can properly track it and garbage-collect the JS object if needed.
93
+ */
94
+ @inline(__always)
95
+ public var memorySize: Int {
96
+ return MemoryHelper.getSizeOf(self.__implementation) + self.__implementation.memorySize
97
+ }
98
+
99
+ /**
100
+ * Call dispose() on the Swift class.
101
+ * This _may_ be called manually from JS.
102
+ */
103
+ @inline(__always)
104
+ public func dispose() {
105
+ self.__implementation.dispose()
106
+ }
107
+
108
+ /**
109
+ * Call toString() on the Swift class.
110
+ */
111
+ @inline(__always)
112
+ public func toString() -> String {
113
+ return self.__implementation.toString()
114
+ }
115
+
116
+ // Properties
117
+ public final var debug: Bool {
118
+ @inline(__always)
119
+ get {
120
+ return self.__implementation.debug
121
+ }
122
+ @inline(__always)
123
+ set {
124
+ self.__implementation.debug = newValue
125
+ }
126
+ }
127
+
128
+ // Methods
129
+ @inline(__always)
130
+ public final func download(modelId: std.string, progressCallback: bridge.Func_void_double) -> bridge.Result_std__shared_ptr_Promise_std__string___ {
131
+ do {
132
+ let __result = try self.__implementation.download(modelId: String(modelId), progressCallback: { () -> (Double) -> Void in
133
+ let __wrappedFunction = bridge.wrap_Func_void_double(progressCallback)
134
+ return { (__progress: Double) -> Void in
135
+ __wrappedFunction.call(__progress)
136
+ }
137
+ }())
138
+ let __resultCpp = { () -> bridge.std__shared_ptr_Promise_std__string__ in
139
+ let __promise = bridge.create_std__shared_ptr_Promise_std__string__()
140
+ let __promiseHolder = bridge.wrap_std__shared_ptr_Promise_std__string__(__promise)
141
+ __result
142
+ .then({ __result in __promiseHolder.resolve(std.string(__result)) })
143
+ .catch({ __error in __promiseHolder.reject(__error.toCpp()) })
144
+ return __promise
145
+ }()
146
+ return bridge.create_Result_std__shared_ptr_Promise_std__string___(__resultCpp)
147
+ } catch (let __error) {
148
+ let __exceptionPtr = __error.toCpp()
149
+ return bridge.create_Result_std__shared_ptr_Promise_std__string___(__exceptionPtr)
150
+ }
151
+ }
152
+
153
+ @inline(__always)
154
+ public final func isDownloaded(modelId: std.string) -> bridge.Result_std__shared_ptr_Promise_bool___ {
155
+ do {
156
+ let __result = try self.__implementation.isDownloaded(modelId: String(modelId))
157
+ let __resultCpp = { () -> bridge.std__shared_ptr_Promise_bool__ in
158
+ let __promise = bridge.create_std__shared_ptr_Promise_bool__()
159
+ let __promiseHolder = bridge.wrap_std__shared_ptr_Promise_bool__(__promise)
160
+ __result
161
+ .then({ __result in __promiseHolder.resolve(__result) })
162
+ .catch({ __error in __promiseHolder.reject(__error.toCpp()) })
163
+ return __promise
164
+ }()
165
+ return bridge.create_Result_std__shared_ptr_Promise_bool___(__resultCpp)
166
+ } catch (let __error) {
167
+ let __exceptionPtr = __error.toCpp()
168
+ return bridge.create_Result_std__shared_ptr_Promise_bool___(__exceptionPtr)
169
+ }
170
+ }
171
+
172
+ @inline(__always)
173
+ public final func getDownloadedModels() -> bridge.Result_std__shared_ptr_Promise_std__vector_std__string____ {
174
+ do {
175
+ let __result = try self.__implementation.getDownloadedModels()
176
+ let __resultCpp = { () -> bridge.std__shared_ptr_Promise_std__vector_std__string___ in
177
+ let __promise = bridge.create_std__shared_ptr_Promise_std__vector_std__string___()
178
+ let __promiseHolder = bridge.wrap_std__shared_ptr_Promise_std__vector_std__string___(__promise)
179
+ __result
180
+ .then({ __result in __promiseHolder.resolve({ () -> bridge.std__vector_std__string_ in
181
+ var __vector = bridge.create_std__vector_std__string_(__result.count)
182
+ for __item in __result {
183
+ __vector.push_back(std.string(__item))
184
+ }
185
+ return __vector
186
+ }()) })
187
+ .catch({ __error in __promiseHolder.reject(__error.toCpp()) })
188
+ return __promise
189
+ }()
190
+ return bridge.create_Result_std__shared_ptr_Promise_std__vector_std__string____(__resultCpp)
191
+ } catch (let __error) {
192
+ let __exceptionPtr = __error.toCpp()
193
+ return bridge.create_Result_std__shared_ptr_Promise_std__vector_std__string____(__exceptionPtr)
194
+ }
195
+ }
196
+
197
+ @inline(__always)
198
+ public final func deleteModel(modelId: std.string) -> bridge.Result_std__shared_ptr_Promise_void___ {
199
+ do {
200
+ let __result = try self.__implementation.deleteModel(modelId: String(modelId))
201
+ let __resultCpp = { () -> bridge.std__shared_ptr_Promise_void__ in
202
+ let __promise = bridge.create_std__shared_ptr_Promise_void__()
203
+ let __promiseHolder = bridge.wrap_std__shared_ptr_Promise_void__(__promise)
204
+ __result
205
+ .then({ __result in __promiseHolder.resolve() })
206
+ .catch({ __error in __promiseHolder.reject(__error.toCpp()) })
207
+ return __promise
208
+ }()
209
+ return bridge.create_Result_std__shared_ptr_Promise_void___(__resultCpp)
210
+ } catch (let __error) {
211
+ let __exceptionPtr = __error.toCpp()
212
+ return bridge.create_Result_std__shared_ptr_Promise_void___(__exceptionPtr)
213
+ }
214
+ }
215
+
216
+ @inline(__always)
217
+ public final func getModelPath(modelId: std.string) -> bridge.Result_std__shared_ptr_Promise_std__string___ {
218
+ do {
219
+ let __result = try self.__implementation.getModelPath(modelId: String(modelId))
220
+ let __resultCpp = { () -> bridge.std__shared_ptr_Promise_std__string__ in
221
+ let __promise = bridge.create_std__shared_ptr_Promise_std__string__()
222
+ let __promiseHolder = bridge.wrap_std__shared_ptr_Promise_std__string__(__promise)
223
+ __result
224
+ .then({ __result in __promiseHolder.resolve(std.string(__result)) })
225
+ .catch({ __error in __promiseHolder.reject(__error.toCpp()) })
226
+ return __promise
227
+ }()
228
+ return bridge.create_Result_std__shared_ptr_Promise_std__string___(__resultCpp)
229
+ } catch (let __error) {
230
+ let __exceptionPtr = __error.toCpp()
231
+ return bridge.create_Result_std__shared_ptr_Promise_std__string___(__exceptionPtr)
232
+ }
233
+ }
234
+ }