react-native-nitro-mlx 0.2.2 → 0.3.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 +1 -1
- package/ios/Sources/HybridLLM.swift +250 -15
- package/lib/module/index.js +1 -0
- package/lib/module/index.js.map +1 -1
- package/lib/module/llm.js +33 -3
- package/lib/module/llm.js.map +1 -1
- package/lib/module/tool-utils.js +56 -0
- package/lib/module/tool-utils.js.map +1 -0
- package/lib/typescript/src/index.d.ts +3 -2
- package/lib/typescript/src/index.d.ts.map +1 -1
- package/lib/typescript/src/llm.d.ts +14 -2
- package/lib/typescript/src/llm.d.ts.map +1 -1
- package/lib/typescript/src/specs/LLM.nitro.d.ts +30 -3
- package/lib/typescript/src/specs/LLM.nitro.d.ts.map +1 -1
- package/lib/typescript/src/tool-utils.d.ts +13 -0
- package/lib/typescript/src/tool-utils.d.ts.map +1 -0
- package/nitrogen/generated/ios/MLXReactNative+autolinking.rb +1 -1
- package/nitrogen/generated/ios/MLXReactNative-Swift-Cxx-Bridge.cpp +34 -1
- package/nitrogen/generated/ios/MLXReactNative-Swift-Cxx-Bridge.hpp +173 -1
- package/nitrogen/generated/ios/MLXReactNative-Swift-Cxx-Umbrella.hpp +8 -1
- package/nitrogen/generated/ios/MLXReactNativeAutolinking.mm +1 -1
- package/nitrogen/generated/ios/MLXReactNativeAutolinking.swift +1 -1
- package/nitrogen/generated/ios/c++/HybridLLMSpecSwift.cpp +1 -1
- package/nitrogen/generated/ios/c++/HybridLLMSpecSwift.hpp +10 -3
- package/nitrogen/generated/ios/c++/HybridModelManagerSpecSwift.cpp +1 -1
- package/nitrogen/generated/ios/c++/HybridModelManagerSpecSwift.hpp +1 -1
- package/nitrogen/generated/ios/swift/Func_std__shared_ptr_Promise_std__shared_ptr_Promise_std__shared_ptr_AnyMap______std__shared_ptr_AnyMap_.swift +62 -0
- package/nitrogen/generated/ios/swift/Func_void.swift +1 -1
- package/nitrogen/generated/ios/swift/Func_void_bool.swift +1 -1
- package/nitrogen/generated/ios/swift/Func_void_double.swift +1 -1
- package/nitrogen/generated/ios/swift/Func_void_std__exception_ptr.swift +1 -1
- package/nitrogen/generated/ios/swift/Func_void_std__shared_ptr_AnyMap_.swift +47 -0
- package/nitrogen/generated/ios/swift/Func_void_std__shared_ptr_Promise_std__shared_ptr_AnyMap___.swift +67 -0
- package/nitrogen/generated/ios/swift/Func_void_std__string.swift +1 -1
- package/nitrogen/generated/ios/swift/Func_void_std__string_std__string.swift +47 -0
- package/nitrogen/generated/ios/swift/Func_void_std__vector_std__string_.swift +1 -1
- package/nitrogen/generated/ios/swift/GenerationStats.swift +1 -1
- package/nitrogen/generated/ios/swift/HybridLLMSpec.swift +2 -2
- package/nitrogen/generated/ios/swift/HybridLLMSpec_cxx.swift +14 -2
- package/nitrogen/generated/ios/swift/HybridModelManagerSpec.swift +1 -1
- package/nitrogen/generated/ios/swift/HybridModelManagerSpec_cxx.swift +1 -1
- package/nitrogen/generated/ios/swift/LLMLoadOptions.swift +44 -2
- package/nitrogen/generated/ios/swift/LLMMessage.swift +1 -1
- package/nitrogen/generated/ios/swift/ToolDefinition.swift +113 -0
- package/nitrogen/generated/ios/swift/ToolParameter.swift +69 -0
- package/nitrogen/generated/shared/c++/GenerationStats.hpp +1 -1
- package/nitrogen/generated/shared/c++/HybridLLMSpec.cpp +1 -1
- package/nitrogen/generated/shared/c++/HybridLLMSpec.hpp +2 -2
- package/nitrogen/generated/shared/c++/HybridModelManagerSpec.cpp +1 -1
- package/nitrogen/generated/shared/c++/HybridModelManagerSpec.hpp +1 -1
- package/nitrogen/generated/shared/c++/LLMLoadOptions.hpp +10 -3
- package/nitrogen/generated/shared/c++/LLMMessage.hpp +1 -1
- package/nitrogen/generated/shared/c++/ToolDefinition.hpp +93 -0
- package/nitrogen/generated/shared/c++/ToolParameter.hpp +87 -0
- package/package.json +8 -7
- package/src/index.ts +10 -3
- package/src/llm.ts +42 -3
- package/src/specs/LLM.nitro.ts +37 -3
- package/src/tool-utils.ts +74 -0
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
///
|
|
2
|
+
/// Func_void_std__string_std__string.swift
|
|
3
|
+
/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
|
|
4
|
+
/// https://github.com/mrousavy/nitro
|
|
5
|
+
/// Copyright © 2026 Marc Rousavy @ Margelo
|
|
6
|
+
///
|
|
7
|
+
|
|
8
|
+
import Foundation
|
|
9
|
+
import NitroModules
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Wraps a Swift `(_ toolName: String, _ args: 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_std__string {
|
|
16
|
+
public typealias bridge = margelo.nitro.mlxreactnative.bridge.swift
|
|
17
|
+
|
|
18
|
+
private let closure: (_ toolName: String, _ args: String) -> Void
|
|
19
|
+
|
|
20
|
+
public init(_ closure: @escaping (_ toolName: String, _ args: String) -> Void) {
|
|
21
|
+
self.closure = closure
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
@inline(__always)
|
|
25
|
+
public func call(toolName: std.string, args: std.string) -> Void {
|
|
26
|
+
self.closure(String(toolName), String(args))
|
|
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_std__string`.
|
|
40
|
+
* The pointer has to be a retained opaque `Unmanaged<Func_void_std__string_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_std__string {
|
|
45
|
+
return Unmanaged<Func_void_std__string_std__string>.fromOpaque(pointer).takeRetainedValue()
|
|
46
|
+
}
|
|
47
|
+
}
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
/// Func_void_std__vector_std__string_.swift
|
|
3
3
|
/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
|
|
4
4
|
/// https://github.com/mrousavy/nitro
|
|
5
|
-
/// Copyright ©
|
|
5
|
+
/// Copyright © 2026 Marc Rousavy @ Margelo
|
|
6
6
|
///
|
|
7
7
|
|
|
8
8
|
import Foundation
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
/// HybridLLMSpec.swift
|
|
3
3
|
/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
|
|
4
4
|
/// https://github.com/mrousavy/nitro
|
|
5
|
-
/// Copyright ©
|
|
5
|
+
/// Copyright © 2026 Marc Rousavy @ Margelo
|
|
6
6
|
///
|
|
7
7
|
|
|
8
8
|
import Foundation
|
|
@@ -20,7 +20,7 @@ public protocol HybridLLMSpec_protocol: HybridObject {
|
|
|
20
20
|
// Methods
|
|
21
21
|
func load(modelId: String, options: LLMLoadOptions?) throws -> Promise<Void>
|
|
22
22
|
func generate(prompt: String) throws -> Promise<String>
|
|
23
|
-
func stream(prompt: String, onToken: @escaping (_ token: String) -> Void) throws -> Promise<String>
|
|
23
|
+
func stream(prompt: String, onToken: @escaping (_ token: String) -> Void, onToolCall: ((_ toolName: String, _ args: String) -> Void)?) throws -> Promise<String>
|
|
24
24
|
func stop() throws -> Void
|
|
25
25
|
func unload() throws -> Void
|
|
26
26
|
func getLastGenerationStats() throws -> GenerationStats
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
/// HybridLLMSpec_cxx.swift
|
|
3
3
|
/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
|
|
4
4
|
/// https://github.com/mrousavy/nitro
|
|
5
|
-
/// Copyright ©
|
|
5
|
+
/// Copyright © 2026 Marc Rousavy @ Margelo
|
|
6
6
|
///
|
|
7
7
|
|
|
8
8
|
import Foundation
|
|
@@ -197,13 +197,25 @@ open class HybridLLMSpec_cxx {
|
|
|
197
197
|
}
|
|
198
198
|
|
|
199
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___ {
|
|
200
|
+
public final func stream(prompt: std.string, onToken: bridge.Func_void_std__string, onToolCall: bridge.std__optional_std__function_void_const_std__string_____toolName_____const_std__string_____args______) -> bridge.Result_std__shared_ptr_Promise_std__string___ {
|
|
201
201
|
do {
|
|
202
202
|
let __result = try self.__implementation.stream(prompt: String(prompt), onToken: { () -> (String) -> Void in
|
|
203
203
|
let __wrappedFunction = bridge.wrap_Func_void_std__string(onToken)
|
|
204
204
|
return { (__token: String) -> Void in
|
|
205
205
|
__wrappedFunction.call(std.string(__token))
|
|
206
206
|
}
|
|
207
|
+
}(), onToolCall: { () -> ((_ toolName: String, _ args: String) -> Void)? in
|
|
208
|
+
if bridge.has_value_std__optional_std__function_void_const_std__string_____toolName_____const_std__string_____args______(onToolCall) {
|
|
209
|
+
let __unwrapped = bridge.get_std__optional_std__function_void_const_std__string_____toolName_____const_std__string_____args______(onToolCall)
|
|
210
|
+
return { () -> (String, String) -> Void in
|
|
211
|
+
let __wrappedFunction = bridge.wrap_Func_void_std__string_std__string(__unwrapped)
|
|
212
|
+
return { (__toolName: String, __args: String) -> Void in
|
|
213
|
+
__wrappedFunction.call(std.string(__toolName), std.string(__args))
|
|
214
|
+
}
|
|
215
|
+
}()
|
|
216
|
+
} else {
|
|
217
|
+
return nil
|
|
218
|
+
}
|
|
207
219
|
}())
|
|
208
220
|
let __resultCpp = { () -> bridge.std__shared_ptr_Promise_std__string__ in
|
|
209
221
|
let __promise = bridge.create_std__shared_ptr_Promise_std__string__()
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
/// LLMLoadOptions.swift
|
|
3
3
|
/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
|
|
4
4
|
/// https://github.com/mrousavy/nitro
|
|
5
|
-
/// Copyright ©
|
|
5
|
+
/// Copyright © 2026 Marc Rousavy @ Margelo
|
|
6
6
|
///
|
|
7
7
|
|
|
8
8
|
import Foundation
|
|
@@ -19,7 +19,7 @@ public extension LLMLoadOptions {
|
|
|
19
19
|
/**
|
|
20
20
|
* Create a new instance of `LLMLoadOptions`.
|
|
21
21
|
*/
|
|
22
|
-
init(onProgress: ((_ progress: Double) -> Void)?, additionalContext: [LLMMessage]?, manageHistory: Bool?) {
|
|
22
|
+
init(onProgress: ((_ progress: Double) -> Void)?, additionalContext: [LLMMessage]?, manageHistory: Bool?, tools: [ToolDefinition]?) {
|
|
23
23
|
self.init({ () -> bridge.std__optional_std__function_void_double____progress______ in
|
|
24
24
|
if let __unwrappedValue = onProgress {
|
|
25
25
|
return bridge.create_std__optional_std__function_void_double____progress______({ () -> bridge.Func_void_double in
|
|
@@ -47,6 +47,18 @@ public extension LLMLoadOptions {
|
|
|
47
47
|
} else {
|
|
48
48
|
return .init()
|
|
49
49
|
}
|
|
50
|
+
}(), { () -> bridge.std__optional_std__vector_ToolDefinition__ in
|
|
51
|
+
if let __unwrappedValue = tools {
|
|
52
|
+
return bridge.create_std__optional_std__vector_ToolDefinition__({ () -> bridge.std__vector_ToolDefinition_ in
|
|
53
|
+
var __vector = bridge.create_std__vector_ToolDefinition_(__unwrappedValue.count)
|
|
54
|
+
for __item in __unwrappedValue {
|
|
55
|
+
__vector.push_back(__item)
|
|
56
|
+
}
|
|
57
|
+
return __vector
|
|
58
|
+
}())
|
|
59
|
+
} else {
|
|
60
|
+
return .init()
|
|
61
|
+
}
|
|
50
62
|
}())
|
|
51
63
|
}
|
|
52
64
|
|
|
@@ -135,4 +147,34 @@ public extension LLMLoadOptions {
|
|
|
135
147
|
}()
|
|
136
148
|
}
|
|
137
149
|
}
|
|
150
|
+
|
|
151
|
+
var tools: [ToolDefinition]? {
|
|
152
|
+
@inline(__always)
|
|
153
|
+
get {
|
|
154
|
+
return { () -> [ToolDefinition]? in
|
|
155
|
+
if bridge.has_value_std__optional_std__vector_ToolDefinition__(self.__tools) {
|
|
156
|
+
let __unwrapped = bridge.get_std__optional_std__vector_ToolDefinition__(self.__tools)
|
|
157
|
+
return __unwrapped.map({ __item in __item })
|
|
158
|
+
} else {
|
|
159
|
+
return nil
|
|
160
|
+
}
|
|
161
|
+
}()
|
|
162
|
+
}
|
|
163
|
+
@inline(__always)
|
|
164
|
+
set {
|
|
165
|
+
self.__tools = { () -> bridge.std__optional_std__vector_ToolDefinition__ in
|
|
166
|
+
if let __unwrappedValue = newValue {
|
|
167
|
+
return bridge.create_std__optional_std__vector_ToolDefinition__({ () -> bridge.std__vector_ToolDefinition_ in
|
|
168
|
+
var __vector = bridge.create_std__vector_ToolDefinition_(__unwrappedValue.count)
|
|
169
|
+
for __item in __unwrappedValue {
|
|
170
|
+
__vector.push_back(__item)
|
|
171
|
+
}
|
|
172
|
+
return __vector
|
|
173
|
+
}())
|
|
174
|
+
} else {
|
|
175
|
+
return .init()
|
|
176
|
+
}
|
|
177
|
+
}()
|
|
178
|
+
}
|
|
179
|
+
}
|
|
138
180
|
}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
///
|
|
2
|
+
/// ToolDefinition.swift
|
|
3
|
+
/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
|
|
4
|
+
/// https://github.com/mrousavy/nitro
|
|
5
|
+
/// Copyright © 2026 Marc Rousavy @ Margelo
|
|
6
|
+
///
|
|
7
|
+
|
|
8
|
+
import Foundation
|
|
9
|
+
import NitroModules
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Represents an instance of `ToolDefinition`, backed by a C++ struct.
|
|
13
|
+
*/
|
|
14
|
+
public typealias ToolDefinition = margelo.nitro.mlxreactnative.ToolDefinition
|
|
15
|
+
|
|
16
|
+
public extension ToolDefinition {
|
|
17
|
+
private typealias bridge = margelo.nitro.mlxreactnative.bridge.swift
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Create a new instance of `ToolDefinition`.
|
|
21
|
+
*/
|
|
22
|
+
init(name: String, description: String, parameters: [ToolParameter], handler: @escaping (_ args: AnyMap) -> Promise<Promise<AnyMap>>) {
|
|
23
|
+
self.init(std.string(name), std.string(description), { () -> bridge.std__vector_ToolParameter_ in
|
|
24
|
+
var __vector = bridge.create_std__vector_ToolParameter_(parameters.count)
|
|
25
|
+
for __item in parameters {
|
|
26
|
+
__vector.push_back(__item)
|
|
27
|
+
}
|
|
28
|
+
return __vector
|
|
29
|
+
}(), { () -> bridge.Func_std__shared_ptr_Promise_std__shared_ptr_Promise_std__shared_ptr_AnyMap______std__shared_ptr_AnyMap_ in
|
|
30
|
+
let __closureWrapper = Func_std__shared_ptr_Promise_std__shared_ptr_Promise_std__shared_ptr_AnyMap______std__shared_ptr_AnyMap_(handler)
|
|
31
|
+
return bridge.create_Func_std__shared_ptr_Promise_std__shared_ptr_Promise_std__shared_ptr_AnyMap______std__shared_ptr_AnyMap_(__closureWrapper.toUnsafe())
|
|
32
|
+
}())
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
var name: String {
|
|
36
|
+
@inline(__always)
|
|
37
|
+
get {
|
|
38
|
+
return String(self.__name)
|
|
39
|
+
}
|
|
40
|
+
@inline(__always)
|
|
41
|
+
set {
|
|
42
|
+
self.__name = std.string(newValue)
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
var description: String {
|
|
47
|
+
@inline(__always)
|
|
48
|
+
get {
|
|
49
|
+
return String(self.__description)
|
|
50
|
+
}
|
|
51
|
+
@inline(__always)
|
|
52
|
+
set {
|
|
53
|
+
self.__description = std.string(newValue)
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
var parameters: [ToolParameter] {
|
|
58
|
+
@inline(__always)
|
|
59
|
+
get {
|
|
60
|
+
return self.__parameters.map({ __item in __item })
|
|
61
|
+
}
|
|
62
|
+
@inline(__always)
|
|
63
|
+
set {
|
|
64
|
+
self.__parameters = { () -> bridge.std__vector_ToolParameter_ in
|
|
65
|
+
var __vector = bridge.create_std__vector_ToolParameter_(newValue.count)
|
|
66
|
+
for __item in newValue {
|
|
67
|
+
__vector.push_back(__item)
|
|
68
|
+
}
|
|
69
|
+
return __vector
|
|
70
|
+
}()
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
var handler: (_ args: AnyMap) -> Promise<Promise<AnyMap>> {
|
|
75
|
+
@inline(__always)
|
|
76
|
+
get {
|
|
77
|
+
return { () -> (AnyMap) -> Promise<Promise<AnyMap>> in
|
|
78
|
+
let __wrappedFunction = bridge.wrap_Func_std__shared_ptr_Promise_std__shared_ptr_Promise_std__shared_ptr_AnyMap______std__shared_ptr_AnyMap_(self.__handler)
|
|
79
|
+
return { (__args: AnyMap) -> Promise<Promise<AnyMap>> in
|
|
80
|
+
let __result = __wrappedFunction.call(__args.cppPart)
|
|
81
|
+
return { () -> Promise<Promise<AnyMap>> in
|
|
82
|
+
let __promise = Promise<Promise<AnyMap>>()
|
|
83
|
+
let __resolver = { (__result: Promise<AnyMap>) in
|
|
84
|
+
__promise.resolve(withResult: __result)
|
|
85
|
+
}
|
|
86
|
+
let __rejecter = { (__error: Error) in
|
|
87
|
+
__promise.reject(withError: __error)
|
|
88
|
+
}
|
|
89
|
+
let __resolverCpp = { () -> bridge.Func_void_std__shared_ptr_Promise_std__shared_ptr_AnyMap___ in
|
|
90
|
+
let __closureWrapper = Func_void_std__shared_ptr_Promise_std__shared_ptr_AnyMap___(__resolver)
|
|
91
|
+
return bridge.create_Func_void_std__shared_ptr_Promise_std__shared_ptr_AnyMap___(__closureWrapper.toUnsafe())
|
|
92
|
+
}()
|
|
93
|
+
let __rejecterCpp = { () -> bridge.Func_void_std__exception_ptr in
|
|
94
|
+
let __closureWrapper = Func_void_std__exception_ptr(__rejecter)
|
|
95
|
+
return bridge.create_Func_void_std__exception_ptr(__closureWrapper.toUnsafe())
|
|
96
|
+
}()
|
|
97
|
+
let __promiseHolder = bridge.wrap_std__shared_ptr_Promise_std__shared_ptr_Promise_std__shared_ptr_AnyMap_____(__result)
|
|
98
|
+
__promiseHolder.addOnResolvedListener(__resolverCpp)
|
|
99
|
+
__promiseHolder.addOnRejectedListener(__rejecterCpp)
|
|
100
|
+
return __promise
|
|
101
|
+
}()
|
|
102
|
+
}
|
|
103
|
+
}()
|
|
104
|
+
}
|
|
105
|
+
@inline(__always)
|
|
106
|
+
set {
|
|
107
|
+
self.__handler = { () -> bridge.Func_std__shared_ptr_Promise_std__shared_ptr_Promise_std__shared_ptr_AnyMap______std__shared_ptr_AnyMap_ in
|
|
108
|
+
let __closureWrapper = Func_std__shared_ptr_Promise_std__shared_ptr_Promise_std__shared_ptr_AnyMap______std__shared_ptr_AnyMap_(newValue)
|
|
109
|
+
return bridge.create_Func_std__shared_ptr_Promise_std__shared_ptr_Promise_std__shared_ptr_AnyMap______std__shared_ptr_AnyMap_(__closureWrapper.toUnsafe())
|
|
110
|
+
}()
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
///
|
|
2
|
+
/// ToolParameter.swift
|
|
3
|
+
/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
|
|
4
|
+
/// https://github.com/mrousavy/nitro
|
|
5
|
+
/// Copyright © 2026 Marc Rousavy @ Margelo
|
|
6
|
+
///
|
|
7
|
+
|
|
8
|
+
import Foundation
|
|
9
|
+
import NitroModules
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Represents an instance of `ToolParameter`, backed by a C++ struct.
|
|
13
|
+
*/
|
|
14
|
+
public typealias ToolParameter = margelo.nitro.mlxreactnative.ToolParameter
|
|
15
|
+
|
|
16
|
+
public extension ToolParameter {
|
|
17
|
+
private typealias bridge = margelo.nitro.mlxreactnative.bridge.swift
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Create a new instance of `ToolParameter`.
|
|
21
|
+
*/
|
|
22
|
+
init(name: String, type: String, description: String, required: Bool) {
|
|
23
|
+
self.init(std.string(name), std.string(type), std.string(description), required)
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
var name: String {
|
|
27
|
+
@inline(__always)
|
|
28
|
+
get {
|
|
29
|
+
return String(self.__name)
|
|
30
|
+
}
|
|
31
|
+
@inline(__always)
|
|
32
|
+
set {
|
|
33
|
+
self.__name = std.string(newValue)
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
var type: String {
|
|
38
|
+
@inline(__always)
|
|
39
|
+
get {
|
|
40
|
+
return String(self.__type)
|
|
41
|
+
}
|
|
42
|
+
@inline(__always)
|
|
43
|
+
set {
|
|
44
|
+
self.__type = std.string(newValue)
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
var description: String {
|
|
49
|
+
@inline(__always)
|
|
50
|
+
get {
|
|
51
|
+
return String(self.__description)
|
|
52
|
+
}
|
|
53
|
+
@inline(__always)
|
|
54
|
+
set {
|
|
55
|
+
self.__description = std.string(newValue)
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
var required: Bool {
|
|
60
|
+
@inline(__always)
|
|
61
|
+
get {
|
|
62
|
+
return self.__required
|
|
63
|
+
}
|
|
64
|
+
@inline(__always)
|
|
65
|
+
set {
|
|
66
|
+
self.__required = newValue
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
/// HybridLLMSpec.hpp
|
|
3
3
|
/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
|
|
4
4
|
/// https://github.com/mrousavy/nitro
|
|
5
|
-
/// Copyright ©
|
|
5
|
+
/// Copyright © 2026 Marc Rousavy @ Margelo
|
|
6
6
|
///
|
|
7
7
|
|
|
8
8
|
#pragma once
|
|
@@ -68,7 +68,7 @@ namespace margelo::nitro::mlxreactnative {
|
|
|
68
68
|
// Methods
|
|
69
69
|
virtual std::shared_ptr<Promise<void>> load(const std::string& modelId, const std::optional<LLMLoadOptions>& options) = 0;
|
|
70
70
|
virtual std::shared_ptr<Promise<std::string>> generate(const std::string& prompt) = 0;
|
|
71
|
-
virtual std::shared_ptr<Promise<std::string>> stream(const std::string& prompt, const std::function<void(const std::string& /* token */)>& onToken) = 0;
|
|
71
|
+
virtual std::shared_ptr<Promise<std::string>> stream(const std::string& prompt, const std::function<void(const std::string& /* token */)>& onToken, const std::optional<std::function<void(const std::string& /* toolName */, const std::string& /* args */)>>& onToolCall) = 0;
|
|
72
72
|
virtual void stop() = 0;
|
|
73
73
|
virtual void unload() = 0;
|
|
74
74
|
virtual GenerationStats getLastGenerationStats() = 0;
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
/// HybridModelManagerSpec.cpp
|
|
3
3
|
/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
|
|
4
4
|
/// https://github.com/mrousavy/nitro
|
|
5
|
-
/// Copyright ©
|
|
5
|
+
/// Copyright © 2026 Marc Rousavy @ Margelo
|
|
6
6
|
///
|
|
7
7
|
|
|
8
8
|
#include "HybridModelManagerSpec.hpp"
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
/// LLMLoadOptions.hpp
|
|
3
3
|
/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
|
|
4
4
|
/// https://github.com/mrousavy/nitro
|
|
5
|
-
/// Copyright ©
|
|
5
|
+
/// Copyright © 2026 Marc Rousavy @ Margelo
|
|
6
6
|
///
|
|
7
7
|
|
|
8
8
|
#pragma once
|
|
@@ -25,11 +25,14 @@
|
|
|
25
25
|
|
|
26
26
|
// Forward declaration of `LLMMessage` to properly resolve imports.
|
|
27
27
|
namespace margelo::nitro::mlxreactnative { struct LLMMessage; }
|
|
28
|
+
// Forward declaration of `ToolDefinition` to properly resolve imports.
|
|
29
|
+
namespace margelo::nitro::mlxreactnative { struct ToolDefinition; }
|
|
28
30
|
|
|
29
31
|
#include <functional>
|
|
30
32
|
#include <optional>
|
|
31
33
|
#include "LLMMessage.hpp"
|
|
32
34
|
#include <vector>
|
|
35
|
+
#include "ToolDefinition.hpp"
|
|
33
36
|
|
|
34
37
|
namespace margelo::nitro::mlxreactnative {
|
|
35
38
|
|
|
@@ -41,10 +44,11 @@ namespace margelo::nitro::mlxreactnative {
|
|
|
41
44
|
std::optional<std::function<void(double /* progress */)>> onProgress SWIFT_PRIVATE;
|
|
42
45
|
std::optional<std::vector<LLMMessage>> additionalContext SWIFT_PRIVATE;
|
|
43
46
|
std::optional<bool> manageHistory SWIFT_PRIVATE;
|
|
47
|
+
std::optional<std::vector<ToolDefinition>> tools SWIFT_PRIVATE;
|
|
44
48
|
|
|
45
49
|
public:
|
|
46
50
|
LLMLoadOptions() = default;
|
|
47
|
-
explicit LLMLoadOptions(std::optional<std::function<void(double /* progress */)>> onProgress, std::optional<std::vector<LLMMessage>> additionalContext, std::optional<bool> manageHistory): onProgress(onProgress), additionalContext(additionalContext), manageHistory(manageHistory) {}
|
|
51
|
+
explicit LLMLoadOptions(std::optional<std::function<void(double /* progress */)>> onProgress, std::optional<std::vector<LLMMessage>> additionalContext, std::optional<bool> manageHistory, std::optional<std::vector<ToolDefinition>> tools): onProgress(onProgress), additionalContext(additionalContext), manageHistory(manageHistory), tools(tools) {}
|
|
48
52
|
};
|
|
49
53
|
|
|
50
54
|
} // namespace margelo::nitro::mlxreactnative
|
|
@@ -59,7 +63,8 @@ namespace margelo::nitro {
|
|
|
59
63
|
return margelo::nitro::mlxreactnative::LLMLoadOptions(
|
|
60
64
|
JSIConverter<std::optional<std::function<void(double)>>>::fromJSI(runtime, obj.getProperty(runtime, "onProgress")),
|
|
61
65
|
JSIConverter<std::optional<std::vector<margelo::nitro::mlxreactnative::LLMMessage>>>::fromJSI(runtime, obj.getProperty(runtime, "additionalContext")),
|
|
62
|
-
JSIConverter<std::optional<bool>>::fromJSI(runtime, obj.getProperty(runtime, "manageHistory"))
|
|
66
|
+
JSIConverter<std::optional<bool>>::fromJSI(runtime, obj.getProperty(runtime, "manageHistory")),
|
|
67
|
+
JSIConverter<std::optional<std::vector<margelo::nitro::mlxreactnative::ToolDefinition>>>::fromJSI(runtime, obj.getProperty(runtime, "tools"))
|
|
63
68
|
);
|
|
64
69
|
}
|
|
65
70
|
static inline jsi::Value toJSI(jsi::Runtime& runtime, const margelo::nitro::mlxreactnative::LLMLoadOptions& arg) {
|
|
@@ -67,6 +72,7 @@ namespace margelo::nitro {
|
|
|
67
72
|
obj.setProperty(runtime, "onProgress", JSIConverter<std::optional<std::function<void(double)>>>::toJSI(runtime, arg.onProgress));
|
|
68
73
|
obj.setProperty(runtime, "additionalContext", JSIConverter<std::optional<std::vector<margelo::nitro::mlxreactnative::LLMMessage>>>::toJSI(runtime, arg.additionalContext));
|
|
69
74
|
obj.setProperty(runtime, "manageHistory", JSIConverter<std::optional<bool>>::toJSI(runtime, arg.manageHistory));
|
|
75
|
+
obj.setProperty(runtime, "tools", JSIConverter<std::optional<std::vector<margelo::nitro::mlxreactnative::ToolDefinition>>>::toJSI(runtime, arg.tools));
|
|
70
76
|
return obj;
|
|
71
77
|
}
|
|
72
78
|
static inline bool canConvert(jsi::Runtime& runtime, const jsi::Value& value) {
|
|
@@ -80,6 +86,7 @@ namespace margelo::nitro {
|
|
|
80
86
|
if (!JSIConverter<std::optional<std::function<void(double)>>>::canConvert(runtime, obj.getProperty(runtime, "onProgress"))) return false;
|
|
81
87
|
if (!JSIConverter<std::optional<std::vector<margelo::nitro::mlxreactnative::LLMMessage>>>::canConvert(runtime, obj.getProperty(runtime, "additionalContext"))) return false;
|
|
82
88
|
if (!JSIConverter<std::optional<bool>>::canConvert(runtime, obj.getProperty(runtime, "manageHistory"))) return false;
|
|
89
|
+
if (!JSIConverter<std::optional<std::vector<margelo::nitro::mlxreactnative::ToolDefinition>>>::canConvert(runtime, obj.getProperty(runtime, "tools"))) return false;
|
|
83
90
|
return true;
|
|
84
91
|
}
|
|
85
92
|
};
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
///
|
|
2
|
+
/// ToolDefinition.hpp
|
|
3
|
+
/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
|
|
4
|
+
/// https://github.com/mrousavy/nitro
|
|
5
|
+
/// Copyright © 2026 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
|
+
|
|
26
|
+
// Forward declaration of `ToolParameter` to properly resolve imports.
|
|
27
|
+
namespace margelo::nitro::mlxreactnative { struct ToolParameter; }
|
|
28
|
+
|
|
29
|
+
#include <string>
|
|
30
|
+
#include "ToolParameter.hpp"
|
|
31
|
+
#include <vector>
|
|
32
|
+
#include <NitroModules/AnyMap.hpp>
|
|
33
|
+
#include <NitroModules/Promise.hpp>
|
|
34
|
+
#include <functional>
|
|
35
|
+
|
|
36
|
+
namespace margelo::nitro::mlxreactnative {
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* A struct which can be represented as a JavaScript object (ToolDefinition).
|
|
40
|
+
*/
|
|
41
|
+
struct ToolDefinition {
|
|
42
|
+
public:
|
|
43
|
+
std::string name SWIFT_PRIVATE;
|
|
44
|
+
std::string description SWIFT_PRIVATE;
|
|
45
|
+
std::vector<ToolParameter> parameters SWIFT_PRIVATE;
|
|
46
|
+
std::function<std::shared_ptr<Promise<std::shared_ptr<Promise<std::shared_ptr<AnyMap>>>>>(const std::shared_ptr<AnyMap>& /* args */)> handler SWIFT_PRIVATE;
|
|
47
|
+
|
|
48
|
+
public:
|
|
49
|
+
ToolDefinition() = default;
|
|
50
|
+
explicit ToolDefinition(std::string name, std::string description, std::vector<ToolParameter> parameters, std::function<std::shared_ptr<Promise<std::shared_ptr<Promise<std::shared_ptr<AnyMap>>>>>(const std::shared_ptr<AnyMap>& /* args */)> handler): name(name), description(description), parameters(parameters), handler(handler) {}
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
} // namespace margelo::nitro::mlxreactnative
|
|
54
|
+
|
|
55
|
+
namespace margelo::nitro {
|
|
56
|
+
|
|
57
|
+
// C++ ToolDefinition <> JS ToolDefinition (object)
|
|
58
|
+
template <>
|
|
59
|
+
struct JSIConverter<margelo::nitro::mlxreactnative::ToolDefinition> final {
|
|
60
|
+
static inline margelo::nitro::mlxreactnative::ToolDefinition fromJSI(jsi::Runtime& runtime, const jsi::Value& arg) {
|
|
61
|
+
jsi::Object obj = arg.asObject(runtime);
|
|
62
|
+
return margelo::nitro::mlxreactnative::ToolDefinition(
|
|
63
|
+
JSIConverter<std::string>::fromJSI(runtime, obj.getProperty(runtime, "name")),
|
|
64
|
+
JSIConverter<std::string>::fromJSI(runtime, obj.getProperty(runtime, "description")),
|
|
65
|
+
JSIConverter<std::vector<margelo::nitro::mlxreactnative::ToolParameter>>::fromJSI(runtime, obj.getProperty(runtime, "parameters")),
|
|
66
|
+
JSIConverter<std::function<std::shared_ptr<Promise<std::shared_ptr<Promise<std::shared_ptr<AnyMap>>>>>(const std::shared_ptr<AnyMap>&)>>::fromJSI(runtime, obj.getProperty(runtime, "handler"))
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
static inline jsi::Value toJSI(jsi::Runtime& runtime, const margelo::nitro::mlxreactnative::ToolDefinition& arg) {
|
|
70
|
+
jsi::Object obj(runtime);
|
|
71
|
+
obj.setProperty(runtime, "name", JSIConverter<std::string>::toJSI(runtime, arg.name));
|
|
72
|
+
obj.setProperty(runtime, "description", JSIConverter<std::string>::toJSI(runtime, arg.description));
|
|
73
|
+
obj.setProperty(runtime, "parameters", JSIConverter<std::vector<margelo::nitro::mlxreactnative::ToolParameter>>::toJSI(runtime, arg.parameters));
|
|
74
|
+
obj.setProperty(runtime, "handler", JSIConverter<std::function<std::shared_ptr<Promise<std::shared_ptr<Promise<std::shared_ptr<AnyMap>>>>>(const std::shared_ptr<AnyMap>&)>>::toJSI(runtime, arg.handler));
|
|
75
|
+
return obj;
|
|
76
|
+
}
|
|
77
|
+
static inline bool canConvert(jsi::Runtime& runtime, const jsi::Value& value) {
|
|
78
|
+
if (!value.isObject()) {
|
|
79
|
+
return false;
|
|
80
|
+
}
|
|
81
|
+
jsi::Object obj = value.getObject(runtime);
|
|
82
|
+
if (!nitro::isPlainObject(runtime, obj)) {
|
|
83
|
+
return false;
|
|
84
|
+
}
|
|
85
|
+
if (!JSIConverter<std::string>::canConvert(runtime, obj.getProperty(runtime, "name"))) return false;
|
|
86
|
+
if (!JSIConverter<std::string>::canConvert(runtime, obj.getProperty(runtime, "description"))) return false;
|
|
87
|
+
if (!JSIConverter<std::vector<margelo::nitro::mlxreactnative::ToolParameter>>::canConvert(runtime, obj.getProperty(runtime, "parameters"))) return false;
|
|
88
|
+
if (!JSIConverter<std::function<std::shared_ptr<Promise<std::shared_ptr<Promise<std::shared_ptr<AnyMap>>>>>(const std::shared_ptr<AnyMap>&)>>::canConvert(runtime, obj.getProperty(runtime, "handler"))) return false;
|
|
89
|
+
return true;
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
} // namespace margelo::nitro
|