react-native-nitro-mlx 0.3.0 → 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 (79) hide show
  1. package/MLXReactNative.podspec +7 -1
  2. package/ios/Sources/AudioCaptureManager.swift +110 -0
  3. package/ios/Sources/HybridLLM.swift +309 -68
  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 +2 -0
  11. package/lib/module/index.js.map +1 -1
  12. package/lib/module/llm.js +39 -1
  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/tts.js +40 -0
  23. package/lib/module/tts.js.map +1 -0
  24. package/lib/typescript/src/index.d.ts +7 -3
  25. package/lib/typescript/src/index.d.ts.map +1 -1
  26. package/lib/typescript/src/llm.d.ts +32 -2
  27. package/lib/typescript/src/llm.d.ts.map +1 -1
  28. package/lib/typescript/src/models.d.ts +13 -4
  29. package/lib/typescript/src/models.d.ts.map +1 -1
  30. package/lib/typescript/src/specs/LLM.nitro.d.ts +49 -4
  31. package/lib/typescript/src/specs/LLM.nitro.d.ts.map +1 -1
  32. package/lib/typescript/src/specs/STT.nitro.d.ts +28 -0
  33. package/lib/typescript/src/specs/STT.nitro.d.ts.map +1 -0
  34. package/lib/typescript/src/specs/TTS.nitro.d.ts +22 -0
  35. package/lib/typescript/src/specs/TTS.nitro.d.ts.map +1 -0
  36. package/lib/typescript/src/stt.d.ts +16 -0
  37. package/lib/typescript/src/stt.d.ts.map +1 -0
  38. package/lib/typescript/src/tts.d.ts +13 -0
  39. package/lib/typescript/src/tts.d.ts.map +1 -0
  40. package/nitrogen/generated/ios/MLXReactNative-Swift-Cxx-Bridge.cpp +42 -0
  41. package/nitrogen/generated/ios/MLXReactNative-Swift-Cxx-Bridge.hpp +165 -0
  42. package/nitrogen/generated/ios/MLXReactNative-Swift-Cxx-Umbrella.hpp +20 -0
  43. package/nitrogen/generated/ios/MLXReactNativeAutolinking.mm +16 -0
  44. package/nitrogen/generated/ios/MLXReactNativeAutolinking.swift +30 -0
  45. package/nitrogen/generated/ios/c++/HybridLLMSpecSwift.hpp +8 -0
  46. package/nitrogen/generated/ios/c++/HybridSTTSpecSwift.cpp +11 -0
  47. package/nitrogen/generated/ios/c++/HybridSTTSpecSwift.hpp +149 -0
  48. package/nitrogen/generated/ios/c++/HybridTTSSpecSwift.cpp +11 -0
  49. package/nitrogen/generated/ios/c++/HybridTTSSpecSwift.hpp +128 -0
  50. package/nitrogen/generated/ios/swift/Func_void_std__shared_ptr_ArrayBuffer_.swift +47 -0
  51. package/nitrogen/generated/ios/swift/GenerationStats.swift +13 -2
  52. package/nitrogen/generated/ios/swift/HybridLLMSpec.swift +1 -0
  53. package/nitrogen/generated/ios/swift/HybridLLMSpec_cxx.swift +24 -0
  54. package/nitrogen/generated/ios/swift/HybridSTTSpec.swift +66 -0
  55. package/nitrogen/generated/ios/swift/HybridSTTSpec_cxx.swift +286 -0
  56. package/nitrogen/generated/ios/swift/HybridTTSSpec.swift +63 -0
  57. package/nitrogen/generated/ios/swift/HybridTTSSpec_cxx.swift +229 -0
  58. package/nitrogen/generated/ios/swift/STTLoadOptions.swift +66 -0
  59. package/nitrogen/generated/ios/swift/TTSGenerateOptions.swift +78 -0
  60. package/nitrogen/generated/ios/swift/TTSLoadOptions.swift +66 -0
  61. package/nitrogen/generated/shared/c++/GenerationStats.hpp +6 -2
  62. package/nitrogen/generated/shared/c++/HybridLLMSpec.cpp +1 -0
  63. package/nitrogen/generated/shared/c++/HybridLLMSpec.hpp +1 -0
  64. package/nitrogen/generated/shared/c++/HybridSTTSpec.cpp +32 -0
  65. package/nitrogen/generated/shared/c++/HybridSTTSpec.hpp +78 -0
  66. package/nitrogen/generated/shared/c++/HybridTTSSpec.cpp +29 -0
  67. package/nitrogen/generated/shared/c++/HybridTTSSpec.hpp +78 -0
  68. package/nitrogen/generated/shared/c++/STTLoadOptions.hpp +76 -0
  69. package/nitrogen/generated/shared/c++/TTSGenerateOptions.hpp +80 -0
  70. package/nitrogen/generated/shared/c++/TTSLoadOptions.hpp +76 -0
  71. package/package.json +8 -4
  72. package/src/index.ts +31 -1
  73. package/src/llm.ts +48 -2
  74. package/src/models.ts +81 -1
  75. package/src/specs/LLM.nitro.ts +74 -4
  76. package/src/specs/STT.nitro.ts +35 -0
  77. package/src/specs/TTS.nitro.ts +30 -0
  78. package/src/stt.ts +67 -0
  79. package/src/tts.ts +60 -0
@@ -0,0 +1,78 @@
1
+ ///
2
+ /// HybridTTSSpec.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/HybridObject.hpp>)
11
+ #include <NitroModules/HybridObject.hpp>
12
+ #else
13
+ #error NitroModules cannot be found! Are you sure you installed NitroModules properly?
14
+ #endif
15
+
16
+ // Forward declaration of `TTSLoadOptions` to properly resolve imports.
17
+ namespace margelo::nitro::mlxreactnative { struct TTSLoadOptions; }
18
+ // Forward declaration of `TTSGenerateOptions` to properly resolve imports.
19
+ namespace margelo::nitro::mlxreactnative { struct TTSGenerateOptions; }
20
+
21
+ #include <string>
22
+ #include <NitroModules/Promise.hpp>
23
+ #include "TTSLoadOptions.hpp"
24
+ #include <optional>
25
+ #include <NitroModules/ArrayBuffer.hpp>
26
+ #include "TTSGenerateOptions.hpp"
27
+ #include <functional>
28
+
29
+ namespace margelo::nitro::mlxreactnative {
30
+
31
+ using namespace margelo::nitro;
32
+
33
+ /**
34
+ * An abstract base class for `TTS`
35
+ * Inherit this class to create instances of `HybridTTSSpec` in C++.
36
+ * You must explicitly call `HybridObject`'s constructor yourself, because it is virtual.
37
+ * @example
38
+ * ```cpp
39
+ * class HybridTTS: public HybridTTSSpec {
40
+ * public:
41
+ * HybridTTS(...): HybridObject(TAG) { ... }
42
+ * // ...
43
+ * };
44
+ * ```
45
+ */
46
+ class HybridTTSSpec: public virtual HybridObject {
47
+ public:
48
+ // Constructor
49
+ explicit HybridTTSSpec(): HybridObject(TAG) { }
50
+
51
+ // Destructor
52
+ ~HybridTTSSpec() override = default;
53
+
54
+ public:
55
+ // Properties
56
+ virtual bool getIsLoaded() = 0;
57
+ virtual bool getIsGenerating() = 0;
58
+ virtual std::string getModelId() = 0;
59
+ virtual double getSampleRate() = 0;
60
+
61
+ public:
62
+ // Methods
63
+ virtual std::shared_ptr<Promise<void>> load(const std::string& modelId, const std::optional<TTSLoadOptions>& options) = 0;
64
+ virtual std::shared_ptr<Promise<std::shared_ptr<ArrayBuffer>>> generate(const std::string& text, const std::optional<TTSGenerateOptions>& options) = 0;
65
+ virtual std::shared_ptr<Promise<void>> stream(const std::string& text, const std::function<void(const std::shared_ptr<ArrayBuffer>& /* audio */)>& onAudioChunk, const std::optional<TTSGenerateOptions>& options) = 0;
66
+ virtual void stop() = 0;
67
+ virtual void unload() = 0;
68
+
69
+ protected:
70
+ // Hybrid Setup
71
+ void loadHybridMethods() override;
72
+
73
+ protected:
74
+ // Tag for logging
75
+ static constexpr auto TAG = "TTS";
76
+ };
77
+
78
+ } // namespace margelo::nitro::mlxreactnative
@@ -0,0 +1,76 @@
1
+ ///
2
+ /// STTLoadOptions.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 (STTLoadOptions).
35
+ */
36
+ struct STTLoadOptions {
37
+ public:
38
+ std::optional<std::function<void(double /* progress */)>> onProgress SWIFT_PRIVATE;
39
+
40
+ public:
41
+ STTLoadOptions() = default;
42
+ explicit STTLoadOptions(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++ STTLoadOptions <> JS STTLoadOptions (object)
50
+ template <>
51
+ struct JSIConverter<margelo::nitro::mlxreactnative::STTLoadOptions> final {
52
+ static inline margelo::nitro::mlxreactnative::STTLoadOptions fromJSI(jsi::Runtime& runtime, const jsi::Value& arg) {
53
+ jsi::Object obj = arg.asObject(runtime);
54
+ return margelo::nitro::mlxreactnative::STTLoadOptions(
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::STTLoadOptions& 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,80 @@
1
+ ///
2
+ /// TTSGenerateOptions.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
+ #include <optional>
30
+
31
+ namespace margelo::nitro::mlxreactnative {
32
+
33
+ /**
34
+ * A struct which can be represented as a JavaScript object (TTSGenerateOptions).
35
+ */
36
+ struct TTSGenerateOptions {
37
+ public:
38
+ std::optional<std::string> voice SWIFT_PRIVATE;
39
+ std::optional<double> speed SWIFT_PRIVATE;
40
+
41
+ public:
42
+ TTSGenerateOptions() = default;
43
+ explicit TTSGenerateOptions(std::optional<std::string> voice, std::optional<double> speed): voice(voice), speed(speed) {}
44
+ };
45
+
46
+ } // namespace margelo::nitro::mlxreactnative
47
+
48
+ namespace margelo::nitro {
49
+
50
+ // C++ TTSGenerateOptions <> JS TTSGenerateOptions (object)
51
+ template <>
52
+ struct JSIConverter<margelo::nitro::mlxreactnative::TTSGenerateOptions> final {
53
+ static inline margelo::nitro::mlxreactnative::TTSGenerateOptions fromJSI(jsi::Runtime& runtime, const jsi::Value& arg) {
54
+ jsi::Object obj = arg.asObject(runtime);
55
+ return margelo::nitro::mlxreactnative::TTSGenerateOptions(
56
+ JSIConverter<std::optional<std::string>>::fromJSI(runtime, obj.getProperty(runtime, "voice")),
57
+ JSIConverter<std::optional<double>>::fromJSI(runtime, obj.getProperty(runtime, "speed"))
58
+ );
59
+ }
60
+ static inline jsi::Value toJSI(jsi::Runtime& runtime, const margelo::nitro::mlxreactnative::TTSGenerateOptions& arg) {
61
+ jsi::Object obj(runtime);
62
+ obj.setProperty(runtime, "voice", JSIConverter<std::optional<std::string>>::toJSI(runtime, arg.voice));
63
+ obj.setProperty(runtime, "speed", JSIConverter<std::optional<double>>::toJSI(runtime, arg.speed));
64
+ return obj;
65
+ }
66
+ static inline bool canConvert(jsi::Runtime& runtime, const jsi::Value& value) {
67
+ if (!value.isObject()) {
68
+ return false;
69
+ }
70
+ jsi::Object obj = value.getObject(runtime);
71
+ if (!nitro::isPlainObject(runtime, obj)) {
72
+ return false;
73
+ }
74
+ if (!JSIConverter<std::optional<std::string>>::canConvert(runtime, obj.getProperty(runtime, "voice"))) return false;
75
+ if (!JSIConverter<std::optional<double>>::canConvert(runtime, obj.getProperty(runtime, "speed"))) return false;
76
+ return true;
77
+ }
78
+ };
79
+
80
+ } // namespace margelo::nitro
@@ -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
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-nitro-mlx",
3
- "version": "0.3.0",
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",
@@ -11,14 +11,14 @@
11
11
  "build": "rm -rf lib && bun typecheck && bob build",
12
12
  "typecheck": "tsc --noEmit",
13
13
  "clean": "rm -rf android/build node_modules/**/android/build lib android/.cxx node_modules/**/android/.cxx",
14
- "release": "release-it && npm publish",
14
+ "release": "release-it",
15
15
  "specs": "bun typecheck && nitrogen --logLevel=\\\"debug\\\" && bun run build",
16
16
  "specs:pod": "bun specs && bun --cwd ../example pod"
17
17
  },
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",
@@ -82,7 +82,8 @@
82
82
  },
83
83
  "hooks": {
84
84
  "after:bump": "bun specs",
85
- "before:release": "bun run build"
85
+ "before:release": "bun run build",
86
+ "after:release": "npm publish --ignore-scripts"
86
87
  },
87
88
  "git": {
88
89
  "commitMessage": "chore: release ${version}",
@@ -90,6 +91,9 @@
90
91
  "requireCleanWorkingDir": false
91
92
  },
92
93
  "plugins": {
94
+ "@release-it/bumper": {
95
+ "out": "package.json"
96
+ },
93
97
  "@release-it/conventional-changelog": {
94
98
  "preset": {
95
99
  "name": "conventionalcommits",
package/src/index.ts CHANGED
@@ -1,4 +1,10 @@
1
- export { LLM, type Message, type ToolCallInfo, type ToolCallUpdate } 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,14 +13,38 @@ export {
7
13
  type ModelInfo,
8
14
  ModelProvider,
9
15
  type ModelQuantization,
16
+ type ModelType,
10
17
  } from './models'
11
18
  export type {
12
19
  GenerationStats,
13
20
  LLM as LLMSpec,
14
21
  LLMLoadOptions,
22
+ StreamEvent,
23
+ GenerationStartEvent,
24
+ TokenEvent,
25
+ ThinkingStartEvent,
26
+ ThinkingChunkEvent,
27
+ ThinkingEndEvent,
28
+ ToolCallStartEvent,
29
+ ToolCallExecutingEvent,
30
+ ToolCallCompletedEvent,
31
+ ToolCallFailedEvent,
32
+ GenerationEndEvent,
15
33
  ToolDefinition,
16
34
  ToolParameter,
17
35
  ToolParameterType,
18
36
  } from './specs/LLM.nitro'
19
37
  export type { ModelManager as ModelManagerSpec } from './specs/ModelManager.nitro'
20
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
 
@@ -106,6 +113,45 @@ export const LLM = {
106
113
  })
107
114
  },
108
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
+ })
153
+ },
154
+
109
155
  /**
110
156
  * Stop the current generation. Safe to call even if not generating.
111
157
  */
@@ -123,7 +169,7 @@ export const LLM = {
123
169
 
124
170
  /**
125
171
  * Get statistics from the last generation.
126
- * @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
127
173
  */
128
174
  getLastGenerationStats(): GenerationStats {
129
175
  return getInstance().getLastGenerationStats()