react-native-nitro-mlx 0.2.2 → 0.4.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.
Files changed (110) hide show
  1. package/MLXReactNative.podspec +7 -1
  2. package/ios/Sources/AudioCaptureManager.swift +110 -0
  3. package/ios/Sources/HybridLLM.swift +518 -42
  4. package/ios/Sources/HybridSTT.swift +202 -0
  5. package/ios/Sources/HybridTTS.swift +145 -0
  6. package/ios/Sources/JSONHelpers.swift +9 -0
  7. package/ios/Sources/ModelDownloader.swift +26 -12
  8. package/ios/Sources/StreamEventEmitter.swift +132 -0
  9. package/ios/Sources/ThinkingStateMachine.swift +206 -0
  10. package/lib/module/index.js +3 -0
  11. package/lib/module/index.js.map +1 -1
  12. package/lib/module/llm.js +72 -4
  13. package/lib/module/llm.js.map +1 -1
  14. package/lib/module/models.js +97 -26
  15. package/lib/module/models.js.map +1 -1
  16. package/lib/module/specs/STT.nitro.js +4 -0
  17. package/lib/module/specs/STT.nitro.js.map +1 -0
  18. package/lib/module/specs/TTS.nitro.js +4 -0
  19. package/lib/module/specs/TTS.nitro.js.map +1 -0
  20. package/lib/module/stt.js +49 -0
  21. package/lib/module/stt.js.map +1 -0
  22. package/lib/module/tool-utils.js +56 -0
  23. package/lib/module/tool-utils.js.map +1 -0
  24. package/lib/module/tts.js +40 -0
  25. package/lib/module/tts.js.map +1 -0
  26. package/lib/typescript/src/index.d.ts +8 -3
  27. package/lib/typescript/src/index.d.ts.map +1 -1
  28. package/lib/typescript/src/llm.d.ts +46 -4
  29. package/lib/typescript/src/llm.d.ts.map +1 -1
  30. package/lib/typescript/src/models.d.ts +13 -4
  31. package/lib/typescript/src/models.d.ts.map +1 -1
  32. package/lib/typescript/src/specs/LLM.nitro.d.ts +79 -7
  33. package/lib/typescript/src/specs/LLM.nitro.d.ts.map +1 -1
  34. package/lib/typescript/src/specs/STT.nitro.d.ts +28 -0
  35. package/lib/typescript/src/specs/STT.nitro.d.ts.map +1 -0
  36. package/lib/typescript/src/specs/TTS.nitro.d.ts +22 -0
  37. package/lib/typescript/src/specs/TTS.nitro.d.ts.map +1 -0
  38. package/lib/typescript/src/stt.d.ts +16 -0
  39. package/lib/typescript/src/stt.d.ts.map +1 -0
  40. package/lib/typescript/src/tool-utils.d.ts +13 -0
  41. package/lib/typescript/src/tool-utils.d.ts.map +1 -0
  42. package/lib/typescript/src/tts.d.ts +13 -0
  43. package/lib/typescript/src/tts.d.ts.map +1 -0
  44. package/nitrogen/generated/ios/MLXReactNative+autolinking.rb +1 -1
  45. package/nitrogen/generated/ios/MLXReactNative-Swift-Cxx-Bridge.cpp +76 -1
  46. package/nitrogen/generated/ios/MLXReactNative-Swift-Cxx-Bridge.hpp +338 -1
  47. package/nitrogen/generated/ios/MLXReactNative-Swift-Cxx-Umbrella.hpp +28 -1
  48. package/nitrogen/generated/ios/MLXReactNativeAutolinking.mm +17 -1
  49. package/nitrogen/generated/ios/MLXReactNativeAutolinking.swift +31 -1
  50. package/nitrogen/generated/ios/c++/HybridLLMSpecSwift.cpp +1 -1
  51. package/nitrogen/generated/ios/c++/HybridLLMSpecSwift.hpp +18 -3
  52. package/nitrogen/generated/ios/c++/HybridModelManagerSpecSwift.cpp +1 -1
  53. package/nitrogen/generated/ios/c++/HybridModelManagerSpecSwift.hpp +1 -1
  54. package/nitrogen/generated/ios/c++/HybridSTTSpecSwift.cpp +11 -0
  55. package/nitrogen/generated/ios/c++/HybridSTTSpecSwift.hpp +149 -0
  56. package/nitrogen/generated/ios/c++/HybridTTSSpecSwift.cpp +11 -0
  57. package/nitrogen/generated/ios/c++/HybridTTSSpecSwift.hpp +128 -0
  58. 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
  59. package/nitrogen/generated/ios/swift/Func_void.swift +1 -1
  60. package/nitrogen/generated/ios/swift/Func_void_bool.swift +1 -1
  61. package/nitrogen/generated/ios/swift/Func_void_double.swift +1 -1
  62. package/nitrogen/generated/ios/swift/Func_void_std__exception_ptr.swift +1 -1
  63. package/nitrogen/generated/ios/swift/Func_void_std__shared_ptr_AnyMap_.swift +47 -0
  64. package/nitrogen/generated/ios/swift/Func_void_std__shared_ptr_ArrayBuffer_.swift +47 -0
  65. package/nitrogen/generated/ios/swift/Func_void_std__shared_ptr_Promise_std__shared_ptr_AnyMap___.swift +67 -0
  66. package/nitrogen/generated/ios/swift/Func_void_std__string.swift +1 -1
  67. package/nitrogen/generated/ios/swift/Func_void_std__string_std__string.swift +47 -0
  68. package/nitrogen/generated/ios/swift/Func_void_std__vector_std__string_.swift +1 -1
  69. package/nitrogen/generated/ios/swift/GenerationStats.swift +14 -3
  70. package/nitrogen/generated/ios/swift/HybridLLMSpec.swift +3 -2
  71. package/nitrogen/generated/ios/swift/HybridLLMSpec_cxx.swift +38 -2
  72. package/nitrogen/generated/ios/swift/HybridModelManagerSpec.swift +1 -1
  73. package/nitrogen/generated/ios/swift/HybridModelManagerSpec_cxx.swift +1 -1
  74. package/nitrogen/generated/ios/swift/HybridSTTSpec.swift +66 -0
  75. package/nitrogen/generated/ios/swift/HybridSTTSpec_cxx.swift +286 -0
  76. package/nitrogen/generated/ios/swift/HybridTTSSpec.swift +63 -0
  77. package/nitrogen/generated/ios/swift/HybridTTSSpec_cxx.swift +229 -0
  78. package/nitrogen/generated/ios/swift/LLMLoadOptions.swift +44 -2
  79. package/nitrogen/generated/ios/swift/LLMMessage.swift +1 -1
  80. package/nitrogen/generated/ios/swift/STTLoadOptions.swift +66 -0
  81. package/nitrogen/generated/ios/swift/TTSGenerateOptions.swift +78 -0
  82. package/nitrogen/generated/ios/swift/TTSLoadOptions.swift +66 -0
  83. package/nitrogen/generated/ios/swift/ToolDefinition.swift +113 -0
  84. package/nitrogen/generated/ios/swift/ToolParameter.swift +69 -0
  85. package/nitrogen/generated/shared/c++/GenerationStats.hpp +7 -3
  86. package/nitrogen/generated/shared/c++/HybridLLMSpec.cpp +2 -1
  87. package/nitrogen/generated/shared/c++/HybridLLMSpec.hpp +3 -2
  88. package/nitrogen/generated/shared/c++/HybridModelManagerSpec.cpp +1 -1
  89. package/nitrogen/generated/shared/c++/HybridModelManagerSpec.hpp +1 -1
  90. package/nitrogen/generated/shared/c++/HybridSTTSpec.cpp +32 -0
  91. package/nitrogen/generated/shared/c++/HybridSTTSpec.hpp +78 -0
  92. package/nitrogen/generated/shared/c++/HybridTTSSpec.cpp +29 -0
  93. package/nitrogen/generated/shared/c++/HybridTTSSpec.hpp +78 -0
  94. package/nitrogen/generated/shared/c++/LLMLoadOptions.hpp +10 -3
  95. package/nitrogen/generated/shared/c++/LLMMessage.hpp +1 -1
  96. package/nitrogen/generated/shared/c++/STTLoadOptions.hpp +76 -0
  97. package/nitrogen/generated/shared/c++/TTSGenerateOptions.hpp +80 -0
  98. package/nitrogen/generated/shared/c++/TTSLoadOptions.hpp +76 -0
  99. package/nitrogen/generated/shared/c++/ToolDefinition.hpp +93 -0
  100. package/nitrogen/generated/shared/c++/ToolParameter.hpp +87 -0
  101. package/package.json +13 -8
  102. package/src/index.ts +40 -3
  103. package/src/llm.ts +90 -5
  104. package/src/models.ts +81 -1
  105. package/src/specs/LLM.nitro.ts +111 -7
  106. package/src/specs/STT.nitro.ts +35 -0
  107. package/src/specs/TTS.nitro.ts +30 -0
  108. package/src/stt.ts +67 -0
  109. package/src/tool-utils.ts +74 -0
  110. package/src/tts.ts +60 -0
@@ -0,0 +1,76 @@
1
+ ///
2
+ /// TTSLoadOptions.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
+
27
+
28
+ #include <functional>
29
+ #include <optional>
30
+
31
+ namespace margelo::nitro::mlxreactnative {
32
+
33
+ /**
34
+ * A struct which can be represented as a JavaScript object (TTSLoadOptions).
35
+ */
36
+ struct TTSLoadOptions {
37
+ public:
38
+ std::optional<std::function<void(double /* progress */)>> onProgress SWIFT_PRIVATE;
39
+
40
+ public:
41
+ TTSLoadOptions() = default;
42
+ explicit TTSLoadOptions(std::optional<std::function<void(double /* progress */)>> onProgress): onProgress(onProgress) {}
43
+ };
44
+
45
+ } // namespace margelo::nitro::mlxreactnative
46
+
47
+ namespace margelo::nitro {
48
+
49
+ // C++ TTSLoadOptions <> JS TTSLoadOptions (object)
50
+ template <>
51
+ struct JSIConverter<margelo::nitro::mlxreactnative::TTSLoadOptions> final {
52
+ static inline margelo::nitro::mlxreactnative::TTSLoadOptions fromJSI(jsi::Runtime& runtime, const jsi::Value& arg) {
53
+ jsi::Object obj = arg.asObject(runtime);
54
+ return margelo::nitro::mlxreactnative::TTSLoadOptions(
55
+ JSIConverter<std::optional<std::function<void(double)>>>::fromJSI(runtime, obj.getProperty(runtime, "onProgress"))
56
+ );
57
+ }
58
+ static inline jsi::Value toJSI(jsi::Runtime& runtime, const margelo::nitro::mlxreactnative::TTSLoadOptions& arg) {
59
+ jsi::Object obj(runtime);
60
+ obj.setProperty(runtime, "onProgress", JSIConverter<std::optional<std::function<void(double)>>>::toJSI(runtime, arg.onProgress));
61
+ return obj;
62
+ }
63
+ static inline bool canConvert(jsi::Runtime& runtime, const jsi::Value& value) {
64
+ if (!value.isObject()) {
65
+ return false;
66
+ }
67
+ jsi::Object obj = value.getObject(runtime);
68
+ if (!nitro::isPlainObject(runtime, obj)) {
69
+ return false;
70
+ }
71
+ if (!JSIConverter<std::optional<std::function<void(double)>>>::canConvert(runtime, obj.getProperty(runtime, "onProgress"))) return false;
72
+ return true;
73
+ }
74
+ };
75
+
76
+ } // namespace margelo::nitro
@@ -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
@@ -0,0 +1,87 @@
1
+ ///
2
+ /// ToolParameter.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
+
27
+
28
+ #include <string>
29
+
30
+ namespace margelo::nitro::mlxreactnative {
31
+
32
+ /**
33
+ * A struct which can be represented as a JavaScript object (ToolParameter).
34
+ */
35
+ struct ToolParameter {
36
+ public:
37
+ std::string name SWIFT_PRIVATE;
38
+ std::string type SWIFT_PRIVATE;
39
+ std::string description SWIFT_PRIVATE;
40
+ bool required SWIFT_PRIVATE;
41
+
42
+ public:
43
+ ToolParameter() = default;
44
+ explicit ToolParameter(std::string name, std::string type, std::string description, bool required): name(name), type(type), description(description), required(required) {}
45
+ };
46
+
47
+ } // namespace margelo::nitro::mlxreactnative
48
+
49
+ namespace margelo::nitro {
50
+
51
+ // C++ ToolParameter <> JS ToolParameter (object)
52
+ template <>
53
+ struct JSIConverter<margelo::nitro::mlxreactnative::ToolParameter> final {
54
+ static inline margelo::nitro::mlxreactnative::ToolParameter fromJSI(jsi::Runtime& runtime, const jsi::Value& arg) {
55
+ jsi::Object obj = arg.asObject(runtime);
56
+ return margelo::nitro::mlxreactnative::ToolParameter(
57
+ JSIConverter<std::string>::fromJSI(runtime, obj.getProperty(runtime, "name")),
58
+ JSIConverter<std::string>::fromJSI(runtime, obj.getProperty(runtime, "type")),
59
+ JSIConverter<std::string>::fromJSI(runtime, obj.getProperty(runtime, "description")),
60
+ JSIConverter<bool>::fromJSI(runtime, obj.getProperty(runtime, "required"))
61
+ );
62
+ }
63
+ static inline jsi::Value toJSI(jsi::Runtime& runtime, const margelo::nitro::mlxreactnative::ToolParameter& arg) {
64
+ jsi::Object obj(runtime);
65
+ obj.setProperty(runtime, "name", JSIConverter<std::string>::toJSI(runtime, arg.name));
66
+ obj.setProperty(runtime, "type", JSIConverter<std::string>::toJSI(runtime, arg.type));
67
+ obj.setProperty(runtime, "description", JSIConverter<std::string>::toJSI(runtime, arg.description));
68
+ obj.setProperty(runtime, "required", JSIConverter<bool>::toJSI(runtime, arg.required));
69
+ return obj;
70
+ }
71
+ static inline bool canConvert(jsi::Runtime& runtime, const jsi::Value& value) {
72
+ if (!value.isObject()) {
73
+ return false;
74
+ }
75
+ jsi::Object obj = value.getObject(runtime);
76
+ if (!nitro::isPlainObject(runtime, obj)) {
77
+ return false;
78
+ }
79
+ if (!JSIConverter<std::string>::canConvert(runtime, obj.getProperty(runtime, "name"))) return false;
80
+ if (!JSIConverter<std::string>::canConvert(runtime, obj.getProperty(runtime, "type"))) return false;
81
+ if (!JSIConverter<std::string>::canConvert(runtime, obj.getProperty(runtime, "description"))) return false;
82
+ if (!JSIConverter<bool>::canConvert(runtime, obj.getProperty(runtime, "required"))) return false;
83
+ return true;
84
+ }
85
+ };
86
+
87
+ } // namespace margelo::nitro
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-nitro-mlx",
3
- "version": "0.2.2",
3
+ "version": "0.4.0",
4
4
  "description": "Nitro module package",
5
5
  "main": "./lib/module/index.js",
6
6
  "module": "./lib/module/index.js",
@@ -18,7 +18,7 @@
18
18
  "keywords": [
19
19
  "react-native",
20
20
  "nitro",
21
- "nitro-modile",
21
+ "nitro-module",
22
22
  "expo",
23
23
  "llm",
24
24
  "mlx",
@@ -68,20 +68,22 @@
68
68
  "peerDependencies": {
69
69
  "react": "*",
70
70
  "react-native": "*",
71
- "react-native-nitro-modules": "*"
71
+ "react-native-nitro-modules": "*",
72
+ "zod": ">=4.0.0"
73
+ },
74
+ "dependencies": {
75
+ "zod": "^4.3.5"
72
76
  },
73
77
  "release-it": {
74
- "npm": {
75
- "publish": true,
76
- "skipVersion": true
77
- },
78
+ "npm": false,
78
79
  "github": {
79
80
  "release": true,
80
81
  "releaseName": "v${version}"
81
82
  },
82
83
  "hooks": {
83
84
  "after:bump": "bun specs",
84
- "before:release": "bun run build"
85
+ "before:release": "bun run build",
86
+ "after:release": "npm publish --ignore-scripts"
85
87
  },
86
88
  "git": {
87
89
  "commitMessage": "chore: release ${version}",
@@ -89,6 +91,9 @@
89
91
  "requireCleanWorkingDir": false
90
92
  },
91
93
  "plugins": {
94
+ "@release-it/bumper": {
95
+ "out": "package.json"
96
+ },
92
97
  "@release-it/conventional-changelog": {
93
98
  "preset": {
94
99
  "name": "conventionalcommits",
package/src/index.ts CHANGED
@@ -1,4 +1,10 @@
1
- export { LLM, type Message } from './llm'
1
+ export {
2
+ LLM,
3
+ type EventCallback,
4
+ type Message,
5
+ type ToolCallInfo,
6
+ type ToolCallUpdate,
7
+ } from './llm'
2
8
  export { ModelManager } from './modelManager'
3
9
  export {
4
10
  MLXModel,
@@ -7,7 +13,38 @@ export {
7
13
  type ModelInfo,
8
14
  ModelProvider,
9
15
  type ModelQuantization,
16
+ type ModelType,
10
17
  } from './models'
11
-
12
- export type { GenerationStats, LLM as LLMSpec, LLMLoadOptions } from './specs/LLM.nitro'
18
+ export type {
19
+ GenerationStats,
20
+ LLM as LLMSpec,
21
+ LLMLoadOptions,
22
+ StreamEvent,
23
+ GenerationStartEvent,
24
+ TokenEvent,
25
+ ThinkingStartEvent,
26
+ ThinkingChunkEvent,
27
+ ThinkingEndEvent,
28
+ ToolCallStartEvent,
29
+ ToolCallExecutingEvent,
30
+ ToolCallCompletedEvent,
31
+ ToolCallFailedEvent,
32
+ GenerationEndEvent,
33
+ ToolDefinition,
34
+ ToolParameter,
35
+ ToolParameterType,
36
+ } from './specs/LLM.nitro'
13
37
  export type { ModelManager as ModelManagerSpec } from './specs/ModelManager.nitro'
38
+ export { createTool, type TypeSafeToolDefinition } from './tool-utils'
39
+ export { TTS } from './tts'
40
+ export type {
41
+ TTS as TTSSpec,
42
+ TTSLoadOptions,
43
+ TTSGenerateOptions,
44
+ } from './specs/TTS.nitro'
45
+ export { STT } from './stt'
46
+ export type {
47
+ STT as STTSpec,
48
+ STTLoadOptions,
49
+ STTTranscriptionInfo,
50
+ } from './specs/STT.nitro'
package/src/llm.ts CHANGED
@@ -1,5 +1,12 @@
1
1
  import { NitroModules } from 'react-native-nitro-modules'
2
- import type { GenerationStats, LLMLoadOptions, LLM as LLMSpec } from './specs/LLM.nitro'
2
+ import type {
3
+ GenerationStats,
4
+ LLMLoadOptions,
5
+ LLM as LLMSpec,
6
+ StreamEvent,
7
+ } from './specs/LLM.nitro'
8
+
9
+ export type EventCallback = (event: StreamEvent) => void
3
10
 
4
11
  let instance: LLMSpec | null = null
5
12
 
@@ -8,6 +15,16 @@ export type Message = {
8
15
  content: string
9
16
  }
10
17
 
18
+ export type ToolCallInfo = {
19
+ name: string
20
+ arguments: Record<string, unknown>
21
+ }
22
+
23
+ export type ToolCallUpdate = {
24
+ toolCall: ToolCallInfo
25
+ allToolCalls: ToolCallInfo[]
26
+ }
27
+
11
28
  function getInstance(): LLMSpec {
12
29
  if (!instance) {
13
30
  instance = NitroModules.createHybridObject<LLMSpec>('LLM')
@@ -58,13 +75,81 @@ export const LLM = {
58
75
  },
59
76
 
60
77
  /**
61
- * Stream a response token by token.
78
+ * Stream a response token by token with optional tool calling support.
79
+ * Tools must be provided when loading the model via `load()` options.
80
+ * Tools are automatically executed when the model calls them.
62
81
  * @param prompt - The input text to generate a response for
63
82
  * @param onToken - Callback invoked for each generated token
83
+ * @param onToolCall - Optional callback invoked when a tool is called.
84
+ * Receives the current tool call and an accumulated array of all tool calls so far.
64
85
  * @returns The complete generated text
65
86
  */
66
- stream(prompt: string, onToken: (token: string) => void): Promise<string> {
67
- return getInstance().stream(prompt, onToken)
87
+ stream(
88
+ prompt: string,
89
+ onToken: (token: string) => void,
90
+ onToolCall?: (update: ToolCallUpdate) => void,
91
+ ): Promise<string> {
92
+ const accumulatedToolCalls: ToolCallInfo[] = []
93
+
94
+ return getInstance().stream(prompt, onToken, (name: string, argsJson: string) => {
95
+ if (onToolCall) {
96
+ try {
97
+ const args = JSON.parse(argsJson) as Record<string, unknown>
98
+ const toolCall = { name, arguments: args }
99
+ accumulatedToolCalls.push(toolCall)
100
+ onToolCall({
101
+ toolCall,
102
+ allToolCalls: [...accumulatedToolCalls],
103
+ })
104
+ } catch {
105
+ const toolCall = { name, arguments: {} }
106
+ accumulatedToolCalls.push(toolCall)
107
+ onToolCall({
108
+ toolCall,
109
+ allToolCalls: [...accumulatedToolCalls],
110
+ })
111
+ }
112
+ }
113
+ })
114
+ },
115
+
116
+ /**
117
+ * Stream with typed events for thinking blocks and tool calls.
118
+ * Provides granular lifecycle events for UI updates.
119
+ *
120
+ * @param prompt - The input text
121
+ * @param onEvent - Callback receiving typed StreamEvent objects
122
+ * @returns Promise resolving to final content string (thinking content stripped)
123
+ *
124
+ * @example
125
+ * ```ts
126
+ * await LLM.streamWithEvents(prompt, (event) => {
127
+ * switch (event.type) {
128
+ * case 'token':
129
+ * appendToContent(event.token)
130
+ * break
131
+ * case 'thinking_start':
132
+ * showThinkingIndicator()
133
+ * break
134
+ * case 'thinking_chunk':
135
+ * appendToThinking(event.chunk)
136
+ * break
137
+ * case 'tool_call_start':
138
+ * showToolCallCard(event.name, event.arguments)
139
+ * break
140
+ * }
141
+ * })
142
+ * ```
143
+ */
144
+ streamWithEvents(prompt: string, onEvent: EventCallback): Promise<string> {
145
+ return getInstance().streamWithEvents(prompt, (eventJson: string) => {
146
+ try {
147
+ const event = JSON.parse(eventJson) as StreamEvent
148
+ onEvent(event)
149
+ } catch {
150
+ // Silently ignore malformed events
151
+ }
152
+ })
68
153
  },
69
154
 
70
155
  /**
@@ -84,7 +169,7 @@ export const LLM = {
84
169
 
85
170
  /**
86
171
  * Get statistics from the last generation.
87
- * @returns Statistics including token count, tokens/sec, TTFT, and total time
172
+ * @returns Statistics including token count, tokens/sec (excluding tool execution), TTFT, total time, and tool execution time
88
173
  */
89
174
  getLastGenerationStats(): GenerationStats {
90
175
  return getInstance().getLastGenerationStats()