react-native-nitro-mlx 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.
- package/MLXReactNative.podspec +42 -0
- package/ios/Bridge.h +8 -0
- package/ios/Sources/MLXReactNative.h +16 -0
- package/lib/module/index.js +6 -0
- package/lib/module/index.js.map +1 -0
- package/lib/module/llm.js +105 -0
- package/lib/module/llm.js.map +1 -0
- package/lib/module/modelManager.js +79 -0
- package/lib/module/modelManager.js.map +1 -0
- package/lib/module/models.js +41 -0
- package/lib/module/models.js.map +1 -0
- package/lib/module/package.json +1 -0
- package/lib/module/specs/LLM.nitro.js +4 -0
- package/lib/module/specs/LLM.nitro.js.map +1 -0
- package/lib/module/specs/ModelManager.nitro.js +4 -0
- package/lib/module/specs/ModelManager.nitro.js.map +1 -0
- package/lib/typescript/package.json +1 -0
- package/lib/typescript/src/index.d.ts +6 -0
- package/lib/typescript/src/index.d.ts.map +1 -0
- package/lib/typescript/src/llm.d.ts +69 -0
- package/lib/typescript/src/llm.d.ts.map +1 -0
- package/lib/typescript/src/modelManager.d.ts +53 -0
- package/lib/typescript/src/modelManager.d.ts.map +1 -0
- package/lib/typescript/src/models.d.ts +29 -0
- package/lib/typescript/src/models.d.ts.map +1 -0
- package/lib/typescript/src/specs/LLM.nitro.d.ts +61 -0
- package/lib/typescript/src/specs/LLM.nitro.d.ts.map +1 -0
- package/lib/typescript/src/specs/ModelManager.nitro.d.ts +41 -0
- package/lib/typescript/src/specs/ModelManager.nitro.d.ts.map +1 -0
- package/nitrogen/generated/.gitattributes +1 -0
- package/nitrogen/generated/ios/MLXReactNative+autolinking.rb +60 -0
- package/nitrogen/generated/ios/MLXReactNative-Swift-Cxx-Bridge.cpp +98 -0
- package/nitrogen/generated/ios/MLXReactNative-Swift-Cxx-Bridge.hpp +312 -0
- package/nitrogen/generated/ios/MLXReactNative-Swift-Cxx-Umbrella.hpp +55 -0
- package/nitrogen/generated/ios/MLXReactNativeAutolinking.mm +41 -0
- package/nitrogen/generated/ios/MLXReactNativeAutolinking.swift +40 -0
- package/nitrogen/generated/ios/c++/HybridLLMSpecSwift.cpp +11 -0
- package/nitrogen/generated/ios/c++/HybridLLMSpecSwift.hpp +132 -0
- package/nitrogen/generated/ios/c++/HybridModelManagerSpecSwift.cpp +11 -0
- package/nitrogen/generated/ios/c++/HybridModelManagerSpecSwift.hpp +116 -0
- package/nitrogen/generated/ios/swift/Func_void.swift +47 -0
- package/nitrogen/generated/ios/swift/Func_void_bool.swift +47 -0
- package/nitrogen/generated/ios/swift/Func_void_double.swift +47 -0
- package/nitrogen/generated/ios/swift/Func_void_std__exception_ptr.swift +47 -0
- package/nitrogen/generated/ios/swift/Func_void_std__string.swift +47 -0
- package/nitrogen/generated/ios/swift/Func_void_std__vector_std__string_.swift +47 -0
- package/nitrogen/generated/ios/swift/GenerationStats.swift +69 -0
- package/nitrogen/generated/ios/swift/HybridLLMSpec.swift +64 -0
- package/nitrogen/generated/ios/swift/HybridLLMSpec_cxx.swift +250 -0
- package/nitrogen/generated/ios/swift/HybridModelManagerSpec.swift +60 -0
- package/nitrogen/generated/ios/swift/HybridModelManagerSpec_cxx.swift +234 -0
- package/nitrogen/generated/shared/c++/GenerationStats.hpp +87 -0
- package/nitrogen/generated/shared/c++/HybridLLMSpec.cpp +32 -0
- package/nitrogen/generated/shared/c++/HybridLLMSpec.hpp +76 -0
- package/nitrogen/generated/shared/c++/HybridModelManagerSpec.cpp +27 -0
- package/nitrogen/generated/shared/c++/HybridModelManagerSpec.hpp +70 -0
- package/package.json +96 -0
- package/src/index.ts +6 -0
- package/src/llm.ts +116 -0
- package/src/modelManager.ts +88 -0
- package/src/models.ts +45 -0
- package/src/specs/LLM.nitro.ts +66 -0
- package/src/specs/ModelManager.nitro.ts +44 -0
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
///
|
|
2
|
+
/// HybridLLMSpecSwift.hpp
|
|
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
|
+
#pragma once
|
|
9
|
+
|
|
10
|
+
#include "HybridLLMSpec.hpp"
|
|
11
|
+
|
|
12
|
+
// Forward declaration of `HybridLLMSpec_cxx` to properly resolve imports.
|
|
13
|
+
namespace MLXReactNative { class HybridLLMSpec_cxx; }
|
|
14
|
+
|
|
15
|
+
// Forward declaration of `GenerationStats` to properly resolve imports.
|
|
16
|
+
namespace margelo::nitro::mlxreactnative { struct GenerationStats; }
|
|
17
|
+
|
|
18
|
+
#include <string>
|
|
19
|
+
#include <NitroModules/Promise.hpp>
|
|
20
|
+
#include <functional>
|
|
21
|
+
#include "GenerationStats.hpp"
|
|
22
|
+
|
|
23
|
+
#include "MLXReactNative-Swift-Cxx-Umbrella.hpp"
|
|
24
|
+
|
|
25
|
+
namespace margelo::nitro::mlxreactnative {
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* The C++ part of HybridLLMSpec_cxx.swift.
|
|
29
|
+
*
|
|
30
|
+
* HybridLLMSpecSwift (C++) accesses HybridLLMSpec_cxx (Swift), and might
|
|
31
|
+
* contain some additional bridging code for C++ <> Swift interop.
|
|
32
|
+
*
|
|
33
|
+
* Since this obviously introduces an overhead, I hope at some point in
|
|
34
|
+
* the future, HybridLLMSpec_cxx can directly inherit from the C++ class HybridLLMSpec
|
|
35
|
+
* to simplify the whole structure and memory management.
|
|
36
|
+
*/
|
|
37
|
+
class HybridLLMSpecSwift: public virtual HybridLLMSpec {
|
|
38
|
+
public:
|
|
39
|
+
// Constructor from a Swift instance
|
|
40
|
+
explicit HybridLLMSpecSwift(const MLXReactNative::HybridLLMSpec_cxx& swiftPart):
|
|
41
|
+
HybridObject(HybridLLMSpec::TAG),
|
|
42
|
+
_swiftPart(swiftPart) { }
|
|
43
|
+
|
|
44
|
+
public:
|
|
45
|
+
// Get the Swift part
|
|
46
|
+
inline MLXReactNative::HybridLLMSpec_cxx& getSwiftPart() noexcept {
|
|
47
|
+
return _swiftPart;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
public:
|
|
51
|
+
inline size_t getExternalMemorySize() noexcept override {
|
|
52
|
+
return _swiftPart.getMemorySize();
|
|
53
|
+
}
|
|
54
|
+
void dispose() noexcept override {
|
|
55
|
+
_swiftPart.dispose();
|
|
56
|
+
}
|
|
57
|
+
std::string toString() override {
|
|
58
|
+
return _swiftPart.toString();
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
public:
|
|
62
|
+
// Properties
|
|
63
|
+
inline bool getIsLoaded() noexcept override {
|
|
64
|
+
return _swiftPart.isLoaded();
|
|
65
|
+
}
|
|
66
|
+
inline bool getIsGenerating() noexcept override {
|
|
67
|
+
return _swiftPart.isGenerating();
|
|
68
|
+
}
|
|
69
|
+
inline std::string getModelId() noexcept override {
|
|
70
|
+
auto __result = _swiftPart.getModelId();
|
|
71
|
+
return __result;
|
|
72
|
+
}
|
|
73
|
+
inline bool getDebug() noexcept override {
|
|
74
|
+
return _swiftPart.getDebug();
|
|
75
|
+
}
|
|
76
|
+
inline void setDebug(bool debug) noexcept override {
|
|
77
|
+
_swiftPart.setDebug(std::forward<decltype(debug)>(debug));
|
|
78
|
+
}
|
|
79
|
+
inline std::string getSystemPrompt() noexcept override {
|
|
80
|
+
auto __result = _swiftPart.getSystemPrompt();
|
|
81
|
+
return __result;
|
|
82
|
+
}
|
|
83
|
+
inline void setSystemPrompt(const std::string& systemPrompt) noexcept override {
|
|
84
|
+
_swiftPart.setSystemPrompt(systemPrompt);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
public:
|
|
88
|
+
// Methods
|
|
89
|
+
inline std::shared_ptr<Promise<void>> load(const std::string& modelId, const std::function<void(double /* progress */)>& onProgress) override {
|
|
90
|
+
auto __result = _swiftPart.load(modelId, onProgress);
|
|
91
|
+
if (__result.hasError()) [[unlikely]] {
|
|
92
|
+
std::rethrow_exception(__result.error());
|
|
93
|
+
}
|
|
94
|
+
auto __value = std::move(__result.value());
|
|
95
|
+
return __value;
|
|
96
|
+
}
|
|
97
|
+
inline std::shared_ptr<Promise<std::string>> generate(const std::string& prompt) override {
|
|
98
|
+
auto __result = _swiftPart.generate(prompt);
|
|
99
|
+
if (__result.hasError()) [[unlikely]] {
|
|
100
|
+
std::rethrow_exception(__result.error());
|
|
101
|
+
}
|
|
102
|
+
auto __value = std::move(__result.value());
|
|
103
|
+
return __value;
|
|
104
|
+
}
|
|
105
|
+
inline std::shared_ptr<Promise<std::string>> stream(const std::string& prompt, const std::function<void(const std::string& /* token */)>& onToken) override {
|
|
106
|
+
auto __result = _swiftPart.stream(prompt, onToken);
|
|
107
|
+
if (__result.hasError()) [[unlikely]] {
|
|
108
|
+
std::rethrow_exception(__result.error());
|
|
109
|
+
}
|
|
110
|
+
auto __value = std::move(__result.value());
|
|
111
|
+
return __value;
|
|
112
|
+
}
|
|
113
|
+
inline void stop() override {
|
|
114
|
+
auto __result = _swiftPart.stop();
|
|
115
|
+
if (__result.hasError()) [[unlikely]] {
|
|
116
|
+
std::rethrow_exception(__result.error());
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
inline GenerationStats getLastGenerationStats() override {
|
|
120
|
+
auto __result = _swiftPart.getLastGenerationStats();
|
|
121
|
+
if (__result.hasError()) [[unlikely]] {
|
|
122
|
+
std::rethrow_exception(__result.error());
|
|
123
|
+
}
|
|
124
|
+
auto __value = std::move(__result.value());
|
|
125
|
+
return __value;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
private:
|
|
129
|
+
MLXReactNative::HybridLLMSpec_cxx _swiftPart;
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
} // namespace margelo::nitro::mlxreactnative
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
///
|
|
2
|
+
/// HybridModelManagerSpecSwift.cpp
|
|
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
|
+
#include "HybridModelManagerSpecSwift.hpp"
|
|
9
|
+
|
|
10
|
+
namespace margelo::nitro::mlxreactnative {
|
|
11
|
+
} // namespace margelo::nitro::mlxreactnative
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
///
|
|
2
|
+
/// HybridModelManagerSpecSwift.hpp
|
|
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
|
+
#pragma once
|
|
9
|
+
|
|
10
|
+
#include "HybridModelManagerSpec.hpp"
|
|
11
|
+
|
|
12
|
+
// Forward declaration of `HybridModelManagerSpec_cxx` to properly resolve imports.
|
|
13
|
+
namespace MLXReactNative { class HybridModelManagerSpec_cxx; }
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
#include <string>
|
|
18
|
+
#include <NitroModules/Promise.hpp>
|
|
19
|
+
#include <functional>
|
|
20
|
+
#include <vector>
|
|
21
|
+
|
|
22
|
+
#include "MLXReactNative-Swift-Cxx-Umbrella.hpp"
|
|
23
|
+
|
|
24
|
+
namespace margelo::nitro::mlxreactnative {
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* The C++ part of HybridModelManagerSpec_cxx.swift.
|
|
28
|
+
*
|
|
29
|
+
* HybridModelManagerSpecSwift (C++) accesses HybridModelManagerSpec_cxx (Swift), and might
|
|
30
|
+
* contain some additional bridging code for C++ <> Swift interop.
|
|
31
|
+
*
|
|
32
|
+
* Since this obviously introduces an overhead, I hope at some point in
|
|
33
|
+
* the future, HybridModelManagerSpec_cxx can directly inherit from the C++ class HybridModelManagerSpec
|
|
34
|
+
* to simplify the whole structure and memory management.
|
|
35
|
+
*/
|
|
36
|
+
class HybridModelManagerSpecSwift: public virtual HybridModelManagerSpec {
|
|
37
|
+
public:
|
|
38
|
+
// Constructor from a Swift instance
|
|
39
|
+
explicit HybridModelManagerSpecSwift(const MLXReactNative::HybridModelManagerSpec_cxx& swiftPart):
|
|
40
|
+
HybridObject(HybridModelManagerSpec::TAG),
|
|
41
|
+
_swiftPart(swiftPart) { }
|
|
42
|
+
|
|
43
|
+
public:
|
|
44
|
+
// Get the Swift part
|
|
45
|
+
inline MLXReactNative::HybridModelManagerSpec_cxx& getSwiftPart() noexcept {
|
|
46
|
+
return _swiftPart;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
public:
|
|
50
|
+
inline size_t getExternalMemorySize() noexcept override {
|
|
51
|
+
return _swiftPart.getMemorySize();
|
|
52
|
+
}
|
|
53
|
+
void dispose() noexcept override {
|
|
54
|
+
_swiftPart.dispose();
|
|
55
|
+
}
|
|
56
|
+
std::string toString() override {
|
|
57
|
+
return _swiftPart.toString();
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
public:
|
|
61
|
+
// Properties
|
|
62
|
+
inline bool getDebug() noexcept override {
|
|
63
|
+
return _swiftPart.getDebug();
|
|
64
|
+
}
|
|
65
|
+
inline void setDebug(bool debug) noexcept override {
|
|
66
|
+
_swiftPart.setDebug(std::forward<decltype(debug)>(debug));
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
public:
|
|
70
|
+
// Methods
|
|
71
|
+
inline std::shared_ptr<Promise<std::string>> download(const std::string& modelId, const std::function<void(double /* progress */)>& progressCallback) override {
|
|
72
|
+
auto __result = _swiftPart.download(modelId, progressCallback);
|
|
73
|
+
if (__result.hasError()) [[unlikely]] {
|
|
74
|
+
std::rethrow_exception(__result.error());
|
|
75
|
+
}
|
|
76
|
+
auto __value = std::move(__result.value());
|
|
77
|
+
return __value;
|
|
78
|
+
}
|
|
79
|
+
inline std::shared_ptr<Promise<bool>> isDownloaded(const std::string& modelId) override {
|
|
80
|
+
auto __result = _swiftPart.isDownloaded(modelId);
|
|
81
|
+
if (__result.hasError()) [[unlikely]] {
|
|
82
|
+
std::rethrow_exception(__result.error());
|
|
83
|
+
}
|
|
84
|
+
auto __value = std::move(__result.value());
|
|
85
|
+
return __value;
|
|
86
|
+
}
|
|
87
|
+
inline std::shared_ptr<Promise<std::vector<std::string>>> getDownloadedModels() override {
|
|
88
|
+
auto __result = _swiftPart.getDownloadedModels();
|
|
89
|
+
if (__result.hasError()) [[unlikely]] {
|
|
90
|
+
std::rethrow_exception(__result.error());
|
|
91
|
+
}
|
|
92
|
+
auto __value = std::move(__result.value());
|
|
93
|
+
return __value;
|
|
94
|
+
}
|
|
95
|
+
inline std::shared_ptr<Promise<void>> deleteModel(const std::string& modelId) override {
|
|
96
|
+
auto __result = _swiftPart.deleteModel(modelId);
|
|
97
|
+
if (__result.hasError()) [[unlikely]] {
|
|
98
|
+
std::rethrow_exception(__result.error());
|
|
99
|
+
}
|
|
100
|
+
auto __value = std::move(__result.value());
|
|
101
|
+
return __value;
|
|
102
|
+
}
|
|
103
|
+
inline std::shared_ptr<Promise<std::string>> getModelPath(const std::string& modelId) override {
|
|
104
|
+
auto __result = _swiftPart.getModelPath(modelId);
|
|
105
|
+
if (__result.hasError()) [[unlikely]] {
|
|
106
|
+
std::rethrow_exception(__result.error());
|
|
107
|
+
}
|
|
108
|
+
auto __value = std::move(__result.value());
|
|
109
|
+
return __value;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
private:
|
|
113
|
+
MLXReactNative::HybridModelManagerSpec_cxx _swiftPart;
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
} // namespace margelo::nitro::mlxreactnative
|
|
@@ -0,0 +1,47 @@
|
|
|
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 © 2025 Marc Rousavy @ Margelo
|
|
6
|
+
///
|
|
7
|
+
|
|
8
|
+
import Foundation
|
|
9
|
+
import NitroModules
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Wraps a Swift `() -> Void` as a class.
|
|
13
|
+
* This class can be used from C++, e.g. to wrap the Swift closure as a `std::function`.
|
|
14
|
+
*/
|
|
15
|
+
public final class Func_void {
|
|
16
|
+
public typealias bridge = margelo.nitro.mlxreactnative.bridge.swift
|
|
17
|
+
|
|
18
|
+
private let closure: () -> Void
|
|
19
|
+
|
|
20
|
+
public init(_ closure: @escaping () -> Void) {
|
|
21
|
+
self.closure = closure
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
@inline(__always)
|
|
25
|
+
public func call() -> Void {
|
|
26
|
+
self.closure()
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Casts this instance to a retained unsafe raw pointer.
|
|
31
|
+
* This acquires one additional strong reference on the object!
|
|
32
|
+
*/
|
|
33
|
+
@inline(__always)
|
|
34
|
+
public func toUnsafe() -> UnsafeMutableRawPointer {
|
|
35
|
+
return Unmanaged.passRetained(self).toOpaque()
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Casts an unsafe pointer to a `Func_void`.
|
|
40
|
+
* The pointer has to be a retained opaque `Unmanaged<Func_void>`.
|
|
41
|
+
* This removes one strong reference from the object!
|
|
42
|
+
*/
|
|
43
|
+
@inline(__always)
|
|
44
|
+
public static func fromUnsafe(_ pointer: UnsafeMutableRawPointer) -> Func_void {
|
|
45
|
+
return Unmanaged<Func_void>.fromOpaque(pointer).takeRetainedValue()
|
|
46
|
+
}
|
|
47
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
///
|
|
2
|
+
/// Func_void_bool.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
|
+
* Wraps a Swift `(_ value: Bool) -> Void` as a class.
|
|
13
|
+
* This class can be used from C++, e.g. to wrap the Swift closure as a `std::function`.
|
|
14
|
+
*/
|
|
15
|
+
public final class Func_void_bool {
|
|
16
|
+
public typealias bridge = margelo.nitro.mlxreactnative.bridge.swift
|
|
17
|
+
|
|
18
|
+
private let closure: (_ value: Bool) -> Void
|
|
19
|
+
|
|
20
|
+
public init(_ closure: @escaping (_ value: Bool) -> Void) {
|
|
21
|
+
self.closure = closure
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
@inline(__always)
|
|
25
|
+
public func call(value: Bool) -> Void {
|
|
26
|
+
self.closure(value)
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Casts this instance to a retained unsafe raw pointer.
|
|
31
|
+
* This acquires one additional strong reference on the object!
|
|
32
|
+
*/
|
|
33
|
+
@inline(__always)
|
|
34
|
+
public func toUnsafe() -> UnsafeMutableRawPointer {
|
|
35
|
+
return Unmanaged.passRetained(self).toOpaque()
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Casts an unsafe pointer to a `Func_void_bool`.
|
|
40
|
+
* The pointer has to be a retained opaque `Unmanaged<Func_void_bool>`.
|
|
41
|
+
* This removes one strong reference from the object!
|
|
42
|
+
*/
|
|
43
|
+
@inline(__always)
|
|
44
|
+
public static func fromUnsafe(_ pointer: UnsafeMutableRawPointer) -> Func_void_bool {
|
|
45
|
+
return Unmanaged<Func_void_bool>.fromOpaque(pointer).takeRetainedValue()
|
|
46
|
+
}
|
|
47
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
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 © 2025 Marc Rousavy @ Margelo
|
|
6
|
+
///
|
|
7
|
+
|
|
8
|
+
import Foundation
|
|
9
|
+
import NitroModules
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Wraps a Swift `(_ progress: Double) -> Void` as a class.
|
|
13
|
+
* This class can be used from C++, e.g. to wrap the Swift closure as a `std::function`.
|
|
14
|
+
*/
|
|
15
|
+
public final class Func_void_double {
|
|
16
|
+
public typealias bridge = margelo.nitro.mlxreactnative.bridge.swift
|
|
17
|
+
|
|
18
|
+
private let closure: (_ progress: Double) -> Void
|
|
19
|
+
|
|
20
|
+
public init(_ closure: @escaping (_ progress: Double) -> Void) {
|
|
21
|
+
self.closure = closure
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
@inline(__always)
|
|
25
|
+
public func call(progress: Double) -> Void {
|
|
26
|
+
self.closure(progress)
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Casts this instance to a retained unsafe raw pointer.
|
|
31
|
+
* This acquires one additional strong reference on the object!
|
|
32
|
+
*/
|
|
33
|
+
@inline(__always)
|
|
34
|
+
public func toUnsafe() -> UnsafeMutableRawPointer {
|
|
35
|
+
return Unmanaged.passRetained(self).toOpaque()
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Casts an unsafe pointer to a `Func_void_double`.
|
|
40
|
+
* The pointer has to be a retained opaque `Unmanaged<Func_void_double>`.
|
|
41
|
+
* This removes one strong reference from the object!
|
|
42
|
+
*/
|
|
43
|
+
@inline(__always)
|
|
44
|
+
public static func fromUnsafe(_ pointer: UnsafeMutableRawPointer) -> Func_void_double {
|
|
45
|
+
return Unmanaged<Func_void_double>.fromOpaque(pointer).takeRetainedValue()
|
|
46
|
+
}
|
|
47
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
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 © 2025 Marc Rousavy @ Margelo
|
|
6
|
+
///
|
|
7
|
+
|
|
8
|
+
import Foundation
|
|
9
|
+
import NitroModules
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Wraps a Swift `(_ error: Error) -> Void` as a class.
|
|
13
|
+
* This class can be used from C++, e.g. to wrap the Swift closure as a `std::function`.
|
|
14
|
+
*/
|
|
15
|
+
public final class Func_void_std__exception_ptr {
|
|
16
|
+
public typealias bridge = margelo.nitro.mlxreactnative.bridge.swift
|
|
17
|
+
|
|
18
|
+
private let closure: (_ error: Error) -> Void
|
|
19
|
+
|
|
20
|
+
public init(_ closure: @escaping (_ error: Error) -> Void) {
|
|
21
|
+
self.closure = closure
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
@inline(__always)
|
|
25
|
+
public func call(error: std.exception_ptr) -> Void {
|
|
26
|
+
self.closure(RuntimeError.from(cppError: error))
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Casts this instance to a retained unsafe raw pointer.
|
|
31
|
+
* This acquires one additional strong reference on the object!
|
|
32
|
+
*/
|
|
33
|
+
@inline(__always)
|
|
34
|
+
public func toUnsafe() -> UnsafeMutableRawPointer {
|
|
35
|
+
return Unmanaged.passRetained(self).toOpaque()
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Casts an unsafe pointer to a `Func_void_std__exception_ptr`.
|
|
40
|
+
* The pointer has to be a retained opaque `Unmanaged<Func_void_std__exception_ptr>`.
|
|
41
|
+
* This removes one strong reference from the object!
|
|
42
|
+
*/
|
|
43
|
+
@inline(__always)
|
|
44
|
+
public static func fromUnsafe(_ pointer: UnsafeMutableRawPointer) -> Func_void_std__exception_ptr {
|
|
45
|
+
return Unmanaged<Func_void_std__exception_ptr>.fromOpaque(pointer).takeRetainedValue()
|
|
46
|
+
}
|
|
47
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
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 © 2025 Marc Rousavy @ Margelo
|
|
6
|
+
///
|
|
7
|
+
|
|
8
|
+
import Foundation
|
|
9
|
+
import NitroModules
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Wraps a Swift `(_ value: String) -> Void` as a class.
|
|
13
|
+
* This class can be used from C++, e.g. to wrap the Swift closure as a `std::function`.
|
|
14
|
+
*/
|
|
15
|
+
public final class Func_void_std__string {
|
|
16
|
+
public typealias bridge = margelo.nitro.mlxreactnative.bridge.swift
|
|
17
|
+
|
|
18
|
+
private let closure: (_ value: String) -> Void
|
|
19
|
+
|
|
20
|
+
public init(_ closure: @escaping (_ value: String) -> Void) {
|
|
21
|
+
self.closure = closure
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
@inline(__always)
|
|
25
|
+
public func call(value: std.string) -> Void {
|
|
26
|
+
self.closure(String(value))
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Casts this instance to a retained unsafe raw pointer.
|
|
31
|
+
* This acquires one additional strong reference on the object!
|
|
32
|
+
*/
|
|
33
|
+
@inline(__always)
|
|
34
|
+
public func toUnsafe() -> UnsafeMutableRawPointer {
|
|
35
|
+
return Unmanaged.passRetained(self).toOpaque()
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Casts an unsafe pointer to a `Func_void_std__string`.
|
|
40
|
+
* The pointer has to be a retained opaque `Unmanaged<Func_void_std__string>`.
|
|
41
|
+
* This removes one strong reference from the object!
|
|
42
|
+
*/
|
|
43
|
+
@inline(__always)
|
|
44
|
+
public static func fromUnsafe(_ pointer: UnsafeMutableRawPointer) -> Func_void_std__string {
|
|
45
|
+
return Unmanaged<Func_void_std__string>.fromOpaque(pointer).takeRetainedValue()
|
|
46
|
+
}
|
|
47
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
///
|
|
2
|
+
/// Func_void_std__vector_std__string_.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
|
+
* Wraps a Swift `(_ value: [String]) -> Void` as a class.
|
|
13
|
+
* This class can be used from C++, e.g. to wrap the Swift closure as a `std::function`.
|
|
14
|
+
*/
|
|
15
|
+
public final class Func_void_std__vector_std__string_ {
|
|
16
|
+
public typealias bridge = margelo.nitro.mlxreactnative.bridge.swift
|
|
17
|
+
|
|
18
|
+
private let closure: (_ value: [String]) -> Void
|
|
19
|
+
|
|
20
|
+
public init(_ closure: @escaping (_ value: [String]) -> Void) {
|
|
21
|
+
self.closure = closure
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
@inline(__always)
|
|
25
|
+
public func call(value: bridge.std__vector_std__string_) -> Void {
|
|
26
|
+
self.closure(value.map({ __item in String(__item) }))
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Casts this instance to a retained unsafe raw pointer.
|
|
31
|
+
* This acquires one additional strong reference on the object!
|
|
32
|
+
*/
|
|
33
|
+
@inline(__always)
|
|
34
|
+
public func toUnsafe() -> UnsafeMutableRawPointer {
|
|
35
|
+
return Unmanaged.passRetained(self).toOpaque()
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Casts an unsafe pointer to a `Func_void_std__vector_std__string_`.
|
|
40
|
+
* The pointer has to be a retained opaque `Unmanaged<Func_void_std__vector_std__string_>`.
|
|
41
|
+
* This removes one strong reference from the object!
|
|
42
|
+
*/
|
|
43
|
+
@inline(__always)
|
|
44
|
+
public static func fromUnsafe(_ pointer: UnsafeMutableRawPointer) -> Func_void_std__vector_std__string_ {
|
|
45
|
+
return Unmanaged<Func_void_std__vector_std__string_>.fromOpaque(pointer).takeRetainedValue()
|
|
46
|
+
}
|
|
47
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
///
|
|
2
|
+
/// GenerationStats.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
|
+
* Represents an instance of `GenerationStats`, backed by a C++ struct.
|
|
13
|
+
*/
|
|
14
|
+
public typealias GenerationStats = margelo.nitro.mlxreactnative.GenerationStats
|
|
15
|
+
|
|
16
|
+
public extension GenerationStats {
|
|
17
|
+
private typealias bridge = margelo.nitro.mlxreactnative.bridge.swift
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Create a new instance of `GenerationStats`.
|
|
21
|
+
*/
|
|
22
|
+
init(tokenCount: Double, tokensPerSecond: Double, timeToFirstToken: Double, totalTime: Double) {
|
|
23
|
+
self.init(tokenCount, tokensPerSecond, timeToFirstToken, totalTime)
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
var tokenCount: Double {
|
|
27
|
+
@inline(__always)
|
|
28
|
+
get {
|
|
29
|
+
return self.__tokenCount
|
|
30
|
+
}
|
|
31
|
+
@inline(__always)
|
|
32
|
+
set {
|
|
33
|
+
self.__tokenCount = newValue
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
var tokensPerSecond: Double {
|
|
38
|
+
@inline(__always)
|
|
39
|
+
get {
|
|
40
|
+
return self.__tokensPerSecond
|
|
41
|
+
}
|
|
42
|
+
@inline(__always)
|
|
43
|
+
set {
|
|
44
|
+
self.__tokensPerSecond = newValue
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
var timeToFirstToken: Double {
|
|
49
|
+
@inline(__always)
|
|
50
|
+
get {
|
|
51
|
+
return self.__timeToFirstToken
|
|
52
|
+
}
|
|
53
|
+
@inline(__always)
|
|
54
|
+
set {
|
|
55
|
+
self.__timeToFirstToken = newValue
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
var totalTime: Double {
|
|
60
|
+
@inline(__always)
|
|
61
|
+
get {
|
|
62
|
+
return self.__totalTime
|
|
63
|
+
}
|
|
64
|
+
@inline(__always)
|
|
65
|
+
set {
|
|
66
|
+
self.__totalTime = newValue
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
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, onProgress: @escaping (_ progress: Double) -> Void) 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 getLastGenerationStats() throws -> GenerationStats
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
public extension HybridLLMSpec_protocol {
|
|
29
|
+
/// Default implementation of ``HybridObject.toString``
|
|
30
|
+
func toString() -> String {
|
|
31
|
+
return "[HybridObject LLM]"
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/// See ``HybridLLMSpec``
|
|
36
|
+
open class HybridLLMSpec_base {
|
|
37
|
+
private weak var cxxWrapper: HybridLLMSpec_cxx? = nil
|
|
38
|
+
public init() { }
|
|
39
|
+
public func getCxxWrapper() -> HybridLLMSpec_cxx {
|
|
40
|
+
#if DEBUG
|
|
41
|
+
guard self is HybridLLMSpec else {
|
|
42
|
+
fatalError("`self` is not a `HybridLLMSpec`! Did you accidentally inherit from `HybridLLMSpec_base` instead of `HybridLLMSpec`?")
|
|
43
|
+
}
|
|
44
|
+
#endif
|
|
45
|
+
if let cxxWrapper = self.cxxWrapper {
|
|
46
|
+
return cxxWrapper
|
|
47
|
+
} else {
|
|
48
|
+
let cxxWrapper = HybridLLMSpec_cxx(self as! HybridLLMSpec)
|
|
49
|
+
self.cxxWrapper = cxxWrapper
|
|
50
|
+
return cxxWrapper
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* A Swift base-protocol representing the LLM HybridObject.
|
|
57
|
+
* Implement this protocol to create Swift-based instances of LLM.
|
|
58
|
+
* ```swift
|
|
59
|
+
* class HybridLLM : HybridLLMSpec {
|
|
60
|
+
* // ...
|
|
61
|
+
* }
|
|
62
|
+
* ```
|
|
63
|
+
*/
|
|
64
|
+
public typealias HybridLLMSpec = HybridLLMSpec_protocol & HybridLLMSpec_base
|