react-native-nitro-mlx 0.1.0 → 0.2.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/ios/Sources/HybridLLM.swift +245 -0
- package/ios/Sources/HybridModelManager.swift +77 -0
- package/ios/Sources/LLMError.swift +6 -0
- package/ios/Sources/ModelDownloader.swift +103 -0
- package/lib/module/index.js.map +1 -1
- package/lib/module/llm.js +23 -3
- package/lib/module/llm.js.map +1 -1
- package/lib/typescript/src/index.d.ts +2 -2
- package/lib/typescript/src/index.d.ts.map +1 -1
- package/lib/typescript/src/llm.d.ts +21 -3
- package/lib/typescript/src/llm.d.ts.map +1 -1
- package/lib/typescript/src/specs/LLM.nitro.d.ts +29 -2
- package/lib/typescript/src/specs/LLM.nitro.d.ts.map +1 -1
- package/nitrogen/generated/ios/MLXReactNative-Swift-Cxx-Bridge.hpp +87 -0
- package/nitrogen/generated/ios/MLXReactNative-Swift-Cxx-Umbrella.hpp +7 -0
- package/nitrogen/generated/ios/c++/HybridLLMSpecSwift.hpp +30 -2
- package/nitrogen/generated/ios/swift/HybridLLMSpec.swift +4 -1
- package/nitrogen/generated/ios/swift/HybridLLMSpec_cxx.swift +42 -7
- package/nitrogen/generated/ios/swift/LLMLoadOptions.swift +138 -0
- package/nitrogen/generated/ios/swift/LLMMessage.swift +47 -0
- package/nitrogen/generated/shared/c++/HybridLLMSpec.cpp +3 -0
- package/nitrogen/generated/shared/c++/HybridLLMSpec.hpp +12 -1
- package/nitrogen/generated/shared/c++/LLMLoadOptions.hpp +87 -0
- package/nitrogen/generated/shared/c++/LLMMessage.hpp +79 -0
- package/package.json +49 -11
- package/src/index.ts +2 -2
- package/src/llm.ts +32 -4
- package/src/specs/LLM.nitro.ts +34 -2
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
///
|
|
2
|
+
/// LLMMessage.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
|
+
#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
|
+
|
|
27
|
+
|
|
28
|
+
#include <string>
|
|
29
|
+
|
|
30
|
+
namespace margelo::nitro::mlxreactnative {
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* A struct which can be represented as a JavaScript object (LLMMessage).
|
|
34
|
+
*/
|
|
35
|
+
struct LLMMessage {
|
|
36
|
+
public:
|
|
37
|
+
std::string role SWIFT_PRIVATE;
|
|
38
|
+
std::string content SWIFT_PRIVATE;
|
|
39
|
+
|
|
40
|
+
public:
|
|
41
|
+
LLMMessage() = default;
|
|
42
|
+
explicit LLMMessage(std::string role, std::string content): role(role), content(content) {}
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
} // namespace margelo::nitro::mlxreactnative
|
|
46
|
+
|
|
47
|
+
namespace margelo::nitro {
|
|
48
|
+
|
|
49
|
+
// C++ LLMMessage <> JS LLMMessage (object)
|
|
50
|
+
template <>
|
|
51
|
+
struct JSIConverter<margelo::nitro::mlxreactnative::LLMMessage> final {
|
|
52
|
+
static inline margelo::nitro::mlxreactnative::LLMMessage fromJSI(jsi::Runtime& runtime, const jsi::Value& arg) {
|
|
53
|
+
jsi::Object obj = arg.asObject(runtime);
|
|
54
|
+
return margelo::nitro::mlxreactnative::LLMMessage(
|
|
55
|
+
JSIConverter<std::string>::fromJSI(runtime, obj.getProperty(runtime, "role")),
|
|
56
|
+
JSIConverter<std::string>::fromJSI(runtime, obj.getProperty(runtime, "content"))
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
static inline jsi::Value toJSI(jsi::Runtime& runtime, const margelo::nitro::mlxreactnative::LLMMessage& arg) {
|
|
60
|
+
jsi::Object obj(runtime);
|
|
61
|
+
obj.setProperty(runtime, "role", JSIConverter<std::string>::toJSI(runtime, arg.role));
|
|
62
|
+
obj.setProperty(runtime, "content", JSIConverter<std::string>::toJSI(runtime, arg.content));
|
|
63
|
+
return obj;
|
|
64
|
+
}
|
|
65
|
+
static inline bool canConvert(jsi::Runtime& runtime, const jsi::Value& value) {
|
|
66
|
+
if (!value.isObject()) {
|
|
67
|
+
return false;
|
|
68
|
+
}
|
|
69
|
+
jsi::Object obj = value.getObject(runtime);
|
|
70
|
+
if (!nitro::isPlainObject(runtime, obj)) {
|
|
71
|
+
return false;
|
|
72
|
+
}
|
|
73
|
+
if (!JSIConverter<std::string>::canConvert(runtime, obj.getProperty(runtime, "role"))) return false;
|
|
74
|
+
if (!JSIConverter<std::string>::canConvert(runtime, obj.getProperty(runtime, "content"))) return false;
|
|
75
|
+
return true;
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
} // namespace margelo::nitro
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-native-nitro-mlx",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Nitro module package",
|
|
5
5
|
"main": "./lib/module/index.js",
|
|
6
6
|
"module": "./lib/module/index.js",
|
|
@@ -41,26 +41,29 @@
|
|
|
41
41
|
"ios/**/*.m",
|
|
42
42
|
"ios/**/*.mm",
|
|
43
43
|
"ios/**/*.cpp",
|
|
44
|
-
"ios
|
|
44
|
+
"ios/**/*.swift",
|
|
45
45
|
"app.plugin.js",
|
|
46
46
|
"*.podspec",
|
|
47
47
|
"README.md"
|
|
48
48
|
],
|
|
49
49
|
"repository": {
|
|
50
50
|
"type": "git",
|
|
51
|
-
"url": "git+https://github.com/
|
|
51
|
+
"url": "git+https://github.com/corasan/react-native-nitro-mlx.git"
|
|
52
52
|
},
|
|
53
53
|
"author": "Henry Paulino",
|
|
54
54
|
"license": "MIT",
|
|
55
|
-
"bugs": "https://github.com/
|
|
56
|
-
"homepage": "https://github.com/
|
|
55
|
+
"bugs": "https://github.com/corasan/react-native-nitro-mlx/issues",
|
|
56
|
+
"homepage": "https://github.com/corasan/react-native-nitro-mlx#readme",
|
|
57
57
|
"publishConfig": {
|
|
58
58
|
"registry": "https://registry.npmjs.org/"
|
|
59
59
|
},
|
|
60
60
|
"devDependencies": {
|
|
61
61
|
"@expo/config-plugins": "^9.0.10",
|
|
62
|
+
"@release-it/bumper": "^7.0.5",
|
|
63
|
+
"@release-it/conventional-changelog": "^10.0.1",
|
|
62
64
|
"nitrogen": "^0.31.10",
|
|
63
|
-
"react-native-builder-bob": "^0.40.13"
|
|
65
|
+
"react-native-builder-bob": "^0.40.13",
|
|
66
|
+
"release-it": "^19.0.4"
|
|
64
67
|
},
|
|
65
68
|
"peerDependencies": {
|
|
66
69
|
"react": "*",
|
|
@@ -69,15 +72,50 @@
|
|
|
69
72
|
},
|
|
70
73
|
"release-it": {
|
|
71
74
|
"npm": {
|
|
72
|
-
"publish": true
|
|
75
|
+
"publish": true,
|
|
76
|
+
"skipVersion": true
|
|
73
77
|
},
|
|
74
|
-
"git": false,
|
|
75
78
|
"github": {
|
|
76
|
-
"release":
|
|
79
|
+
"release": true,
|
|
80
|
+
"releaseName": "v${version}"
|
|
77
81
|
},
|
|
78
82
|
"hooks": {
|
|
79
|
-
"
|
|
80
|
-
"
|
|
83
|
+
"after:bump": "bun specs",
|
|
84
|
+
"before:release": "bun run build"
|
|
85
|
+
},
|
|
86
|
+
"git": {
|
|
87
|
+
"commitMessage": "chore: release ${version}",
|
|
88
|
+
"tagName": "v${version}",
|
|
89
|
+
"requireCleanWorkingDir": false
|
|
90
|
+
},
|
|
91
|
+
"plugins": {
|
|
92
|
+
"@release-it/conventional-changelog": {
|
|
93
|
+
"preset": {
|
|
94
|
+
"name": "conventionalcommits",
|
|
95
|
+
"types": [
|
|
96
|
+
{
|
|
97
|
+
"type": "feat",
|
|
98
|
+
"section": "✨ Features"
|
|
99
|
+
},
|
|
100
|
+
{
|
|
101
|
+
"type": "fix",
|
|
102
|
+
"section": "🐞 Fixes"
|
|
103
|
+
},
|
|
104
|
+
{
|
|
105
|
+
"type": "chore(deps)",
|
|
106
|
+
"section": "🛠️ Dependency Upgrades"
|
|
107
|
+
},
|
|
108
|
+
{
|
|
109
|
+
"type": "perf",
|
|
110
|
+
"section": "🏎️ Performance Improvements"
|
|
111
|
+
},
|
|
112
|
+
{
|
|
113
|
+
"type": "docs",
|
|
114
|
+
"section": "📚 Documentation"
|
|
115
|
+
}
|
|
116
|
+
]
|
|
117
|
+
}
|
|
118
|
+
}
|
|
81
119
|
}
|
|
82
120
|
},
|
|
83
121
|
"react-native-builder-bob": {
|
package/src/index.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
export { LLM } from './llm'
|
|
1
|
+
export { LLM, type Message } from './llm'
|
|
2
2
|
export { ModelManager } from './modelManager'
|
|
3
3
|
export { MLXModel } from './models'
|
|
4
4
|
|
|
5
|
-
export type { GenerationStats, LLM as LLMSpec } from './specs/LLM.nitro'
|
|
5
|
+
export type { GenerationStats, LLM as LLMSpec, LLMLoadOptions } from './specs/LLM.nitro'
|
|
6
6
|
export type { ModelManager as ModelManagerSpec } from './specs/ModelManager.nitro'
|
package/src/llm.ts
CHANGED
|
@@ -1,8 +1,13 @@
|
|
|
1
1
|
import { NitroModules } from 'react-native-nitro-modules'
|
|
2
|
-
import type { GenerationStats, LLM as LLMSpec } from './specs/LLM.nitro'
|
|
2
|
+
import type { GenerationStats, LLMLoadOptions, LLM as LLMSpec } from './specs/LLM.nitro'
|
|
3
3
|
|
|
4
4
|
let instance: LLMSpec | null = null
|
|
5
5
|
|
|
6
|
+
export type Message = {
|
|
7
|
+
role: 'user' | 'assistant' | 'system'
|
|
8
|
+
content: string
|
|
9
|
+
}
|
|
10
|
+
|
|
6
11
|
function getInstance(): LLMSpec {
|
|
7
12
|
if (!instance) {
|
|
8
13
|
instance = NitroModules.createHybridObject<LLMSpec>('LLM')
|
|
@@ -36,10 +41,10 @@ export const LLM = {
|
|
|
36
41
|
/**
|
|
37
42
|
* Load a model into memory. Downloads the model from HuggingFace if not already cached.
|
|
38
43
|
* @param modelId - HuggingFace model ID (e.g., 'mlx-community/Qwen3-0.6B-4bit')
|
|
39
|
-
* @param
|
|
44
|
+
* @param options - Callback invoked with loading progress (0-1)
|
|
40
45
|
*/
|
|
41
|
-
load(modelId: string,
|
|
42
|
-
return getInstance().load(modelId,
|
|
46
|
+
load(modelId: string, options: LLMLoadOptions): Promise<void> {
|
|
47
|
+
return getInstance().load(modelId, options)
|
|
43
48
|
},
|
|
44
49
|
|
|
45
50
|
/**
|
|
@@ -69,6 +74,14 @@ export const LLM = {
|
|
|
69
74
|
getInstance().stop()
|
|
70
75
|
},
|
|
71
76
|
|
|
77
|
+
/**
|
|
78
|
+
* Unload the current model and release memory.
|
|
79
|
+
* Call this when you're done with the model to free up memory.
|
|
80
|
+
*/
|
|
81
|
+
unload(): void {
|
|
82
|
+
getInstance().unload()
|
|
83
|
+
},
|
|
84
|
+
|
|
72
85
|
/**
|
|
73
86
|
* Get statistics from the last generation.
|
|
74
87
|
* @returns Statistics including token count, tokens/sec, TTFT, and total time
|
|
@@ -77,6 +90,21 @@ export const LLM = {
|
|
|
77
90
|
return getInstance().getLastGenerationStats()
|
|
78
91
|
},
|
|
79
92
|
|
|
93
|
+
/**
|
|
94
|
+
* Get the message history if management is enabled.
|
|
95
|
+
* @returns Array of messages in the history
|
|
96
|
+
*/
|
|
97
|
+
getHistory(): Message[] {
|
|
98
|
+
return getInstance().getHistory() as Message[]
|
|
99
|
+
},
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Clear the message history.
|
|
103
|
+
*/
|
|
104
|
+
clearHistory(): void {
|
|
105
|
+
getInstance().clearHistory()
|
|
106
|
+
},
|
|
107
|
+
|
|
80
108
|
/** Whether a model is currently loaded and ready for generation */
|
|
81
109
|
get isLoaded(): boolean {
|
|
82
110
|
return getInstance().isLoaded
|
package/src/specs/LLM.nitro.ts
CHANGED
|
@@ -14,6 +14,22 @@ export interface GenerationStats {
|
|
|
14
14
|
totalTime: number
|
|
15
15
|
}
|
|
16
16
|
|
|
17
|
+
export interface LLMMessage {
|
|
18
|
+
role: string
|
|
19
|
+
content: string
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** Options for loading a model.
|
|
23
|
+
*/
|
|
24
|
+
export interface LLMLoadOptions {
|
|
25
|
+
/** Callback invoked with loading progress (0-1) */
|
|
26
|
+
onProgress?: (progress: number) => void
|
|
27
|
+
/** Additional context to provide to the model */
|
|
28
|
+
additionalContext?: LLMMessage[]
|
|
29
|
+
/** Whether to automatically manage message history */
|
|
30
|
+
manageHistory?: boolean
|
|
31
|
+
}
|
|
32
|
+
|
|
17
33
|
/**
|
|
18
34
|
* Low-level LLM interface for text generation using MLX.
|
|
19
35
|
* @internal Use the `LLM` export from `react-native-nitro-mlx` instead.
|
|
@@ -22,9 +38,9 @@ export interface LLM extends HybridObject<{ ios: 'swift' }> {
|
|
|
22
38
|
/**
|
|
23
39
|
* Load a model into memory. Downloads from HuggingFace if not already cached.
|
|
24
40
|
* @param modelId - HuggingFace model ID (e.g., 'mlx-community/Qwen3-0.6B-4bit')
|
|
25
|
-
* @param
|
|
41
|
+
* @param options - Callback invoked with loading progress (0-1)
|
|
26
42
|
*/
|
|
27
|
-
load(modelId: string,
|
|
43
|
+
load(modelId: string, options?: LLMLoadOptions): Promise<void>
|
|
28
44
|
|
|
29
45
|
/**
|
|
30
46
|
* Generate a complete response for a prompt.
|
|
@@ -46,12 +62,28 @@ export interface LLM extends HybridObject<{ ios: 'swift' }> {
|
|
|
46
62
|
*/
|
|
47
63
|
stop(): void
|
|
48
64
|
|
|
65
|
+
/**
|
|
66
|
+
* Unload the current model and release memory.
|
|
67
|
+
*/
|
|
68
|
+
unload(): void
|
|
69
|
+
|
|
49
70
|
/**
|
|
50
71
|
* Get statistics from the last generation.
|
|
51
72
|
* @returns Statistics including token count, speed, and timing
|
|
52
73
|
*/
|
|
53
74
|
getLastGenerationStats(): GenerationStats
|
|
54
75
|
|
|
76
|
+
/**
|
|
77
|
+
* Get the message history if management is enabled.
|
|
78
|
+
* @returns Array of messages in the history
|
|
79
|
+
*/
|
|
80
|
+
getHistory(): LLMMessage[]
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Clear the message history.
|
|
84
|
+
*/
|
|
85
|
+
clearHistory(): void
|
|
86
|
+
|
|
55
87
|
/** Whether a model is currently loaded */
|
|
56
88
|
readonly isLoaded: boolean
|
|
57
89
|
/** Whether text is currently being generated */
|