@dr33m/react-native-litert-lm 0.5.6 → 0.6.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.
@@ -563,6 +563,26 @@ class HybridLiteRTLM : HybridLiteRTLMSpec() {
563
563
  createNewConversation()
564
564
  }
565
565
 
566
+ override fun resetConversationWith(messages: Array<Message>) {
567
+ val seed = messages.map { msg ->
568
+ LiteRTMessage(
569
+ role = when (msg.role) {
570
+ Role.MODEL -> com.google.ai.edge.litertlm.Role.MODEL
571
+ Role.SYSTEM -> com.google.ai.edge.litertlm.Role.SYSTEM
572
+ else -> com.google.ai.edge.litertlm.Role.USER
573
+ },
574
+ contents = Contents.of(Content.Text(msg.content))
575
+ )
576
+ }
577
+ synchronized(history) {
578
+ history.clear()
579
+ history.addAll(messages.toList())
580
+ }
581
+ // Recreating the conversation is what actually frees the old KV cache;
582
+ // the seed is replayed into the new one without triggering generation.
583
+ createNewConversation(seed)
584
+ }
585
+
566
586
  override fun isReady(): Boolean {
567
587
  return isLoaded_
568
588
  }
@@ -673,7 +693,7 @@ class HybridLiteRTLM : HybridLiteRTLMSpec() {
673
693
  }
674
694
  }
675
695
 
676
- private fun createNewConversation() {
696
+ private fun createNewConversation(initialMessages: List<LiteRTMessage> = emptyList()) {
677
697
  ensureLoaded()
678
698
  // v0.10.2 enforces single-session: close existing conversation first
679
699
  conversation?.let { oldConv ->
@@ -716,6 +736,7 @@ class HybridLiteRTLM : HybridLiteRTLMSpec() {
716
736
  temperature = temperature.toDouble(),
717
737
  ),
718
738
  systemInstruction = systemPrompt?.let { Contents.of(Content.Text(it)) },
739
+ initialMessages = initialMessages,
719
740
  tools = lmTools ?: emptyList()
720
741
  )
721
742
  // TODO: maxOutputTokens is not configurable on Android — the Kotlin SDK's
@@ -92,6 +92,26 @@ public class HybridLiteRTLM: HybridLiteRTLMSpec_base, HybridLiteRTLMSpec_protoco
92
92
  }
93
93
  }
94
94
 
95
+ public func resetConversationWith(messages: [Message]) throws {
96
+ queue.sync {
97
+ history = messages
98
+ lastStats = GenerationStats(
99
+ promptTokens: 0.0,
100
+ completionTokens: 0.0,
101
+ totalTokens: 0.0,
102
+ timeToFirstToken: 0.0,
103
+ totalTime: 0.0,
104
+ tokensPerSecond: 0.0
105
+ )
106
+ if isLoaded && engine != nil {
107
+ // Recreating the conversation is what actually frees the old KV
108
+ // cache; the seed is replayed into the new one without
109
+ // triggering generation.
110
+ createNewConversation(initialMessages: messages)
111
+ }
112
+ }
113
+ }
114
+
95
115
  public func getStats() throws -> GenerationStats {
96
116
  return queue.sync { lastStats }
97
117
  }
@@ -393,7 +413,7 @@ public class HybridLiteRTLM: HybridLiteRTLMSpec_base, HybridLiteRTLMSpec_protoco
393
413
 
394
414
  // MARK: - Internal Engine Helpers
395
415
 
396
- private func createNewConversation() {
416
+ private func createNewConversation(initialMessages: [Message] = []) {
397
417
  guard let engine = self.engine else { return }
398
418
 
399
419
  if let oldConv = self.conversation {
@@ -446,6 +466,24 @@ public class HybridLiteRTLM: HybridLiteRTLMSpec_base, HybridLiteRTLMSpec_protoco
446
466
  }
447
467
  }
448
468
 
469
+ if !initialMessages.isEmpty {
470
+ let payload = initialMessages.map { msg -> [String: String] in
471
+ let role: String
472
+ switch msg.role {
473
+ case .model: role = "model"
474
+ case .system: role = "system"
475
+ default: role = "user"
476
+ }
477
+ return ["role": role, "content": msg.content]
478
+ }
479
+ if let data = try? JSONSerialization.data(withJSONObject: payload, options: []),
480
+ let jsonString = String(data: data, encoding: .utf8) {
481
+ jsonString.withCString { messagesC in
482
+ litert_lm_conversation_config_set_messages(convConfig, messagesC)
483
+ }
484
+ }
485
+ }
486
+
449
487
  self.conversation = litert_lm_conversation_create(engine, convConfig)
450
488
  }
451
489
 
@@ -14,6 +14,7 @@ export declare const mockLiteRTLM: {
14
14
  sendMessageWithAudioAsync: jest.Mock<Promise<void>, [msg: string, audioPath: string, onToken: (token: string, done: boolean) => void], any>;
15
15
  getHistory: jest.Mock<never[], [], any>;
16
16
  resetConversation: jest.Mock<any, any, any>;
17
+ resetConversationWith: jest.Mock<any, any, any>;
17
18
  getStats: jest.Mock<{
18
19
  promptTokens: number;
19
20
  completionTokens: number;
@@ -54,6 +55,7 @@ export declare const NitroModules: {
54
55
  sendMessageWithAudioAsync: jest.Mock<Promise<void>, [msg: string, audioPath: string, onToken: (token: string, done: boolean) => void], any>;
55
56
  getHistory: jest.Mock<never[], [], any>;
56
57
  resetConversation: jest.Mock<any, any, any>;
58
+ resetConversationWith: jest.Mock<any, any, any>;
57
59
  getStats: jest.Mock<{
58
60
  promptTokens: number;
59
61
  completionTokens: number;
@@ -56,6 +56,7 @@ exports.mockLiteRTLM = {
56
56
  sendMessageWithAudioAsync: jest.fn((msg, audioPath, onToken) => mockExecute([{ type: "text", text: msg }, { type: "audio", path: audioPath }], onToken).then(() => { })),
57
57
  getHistory: jest.fn(() => []),
58
58
  resetConversation: jest.fn(),
59
+ resetConversationWith: jest.fn(),
59
60
  getStats: jest.fn(() => ({
60
61
  promptTokens: 10,
61
62
  completionTokens: 20,
@@ -33,6 +33,25 @@ describe('modelFactory Security & Proxy Unit Tests', () => {
33
33
  expect(react_native_nitro_modules_1.mockLiteRTLM.resetConversation).toHaveBeenCalled();
34
34
  expect(react_native_nitro_modules_1.mockLiteRTLM.getMemoryUsage).toHaveBeenCalled();
35
35
  });
36
+ it('should successfully proxy resetConversationWith and record memory metrics', async () => {
37
+ const seed = [{ role: 'user', content: 'earlier turn' }];
38
+ await llm.resetConversationWith(seed);
39
+ expect(react_native_nitro_modules_1.mockLiteRTLM.resetConversationWith).toHaveBeenCalledWith(seed);
40
+ expect(react_native_nitro_modules_1.mockLiteRTLM.getMemoryUsage).toHaveBeenCalled();
41
+ });
42
+ it('should report resetConversationWith as absent on older native builds', () => {
43
+ // The app feature-detects this method, so the proxy must not hand back a
44
+ // wrapper the native side cannot service.
45
+ const original = react_native_nitro_modules_1.mockLiteRTLM.resetConversationWith;
46
+ // @ts-expect-error deliberately removing the method to simulate old native
47
+ delete react_native_nitro_modules_1.mockLiteRTLM.resetConversationWith;
48
+ try {
49
+ expect((0, modelFactory_1.createLLM)().resetConversationWith).toBeUndefined();
50
+ }
51
+ finally {
52
+ react_native_nitro_modules_1.mockLiteRTLM.resetConversationWith = original;
53
+ }
54
+ });
36
55
  it('should successfully proxy sendMessageAsync and record memory metrics when done', async () => {
37
56
  const onToken = jest.fn();
38
57
  await llm.sendMessageAsync("Async prompt", onToken);
@@ -95,6 +95,19 @@ function createLLM(options) {
95
95
  return result;
96
96
  };
97
97
  }
98
+ if (prop === "resetConversationWith") {
99
+ // Absent on native builds older than this JS wrapper, and callers
100
+ // feature-detect it — so fall through rather than handing back a
101
+ // wrapper that would fail at the bridge.
102
+ if (typeof target.resetConversationWith !== "function") {
103
+ return undefined;
104
+ }
105
+ return (messages) => {
106
+ const result = target.resetConversationWith(messages);
107
+ recordMemorySnapshot();
108
+ return result;
109
+ };
110
+ }
98
111
  if (prop === "sendToolResponse") {
99
112
  return (responses, onToken) => {
100
113
  if (onToken) {
@@ -313,6 +313,23 @@ export interface LiteRTLM extends HybridObject<{
313
313
  * Clear the conversation context and start fresh.
314
314
  */
315
315
  resetConversation(): void;
316
+ /**
317
+ * Clear the conversation and re-seed it with prior turns.
318
+ *
319
+ * The engine allocates one KV cache per conversation, sized by
320
+ * `maxContextTokens`. Long tool-calling sessions fill it, and overflowing it
321
+ * aborts the process rather than raising a catchable error. Recreating the
322
+ * conversation frees the cache, but `resetConversation()` alone also throws
323
+ * away the thread.
324
+ *
325
+ * This seeds the fresh conversation with `messages` as prior context, so a
326
+ * caller can drop the oldest turns and keep the recent ones. The system
327
+ * prompt and tools configured at `loadModel` are reapplied automatically.
328
+ * No generation is triggered and no reply is produced.
329
+ *
330
+ * @param messages Prior turns, oldest first.
331
+ */
332
+ resetConversationWith(messages: Message[]): void;
316
333
  /**
317
334
  * Check if a model is loaded and ready for inference.
318
335
  */
@@ -288,6 +288,19 @@ namespace margelo::nitro::litertlm {
288
288
  static const auto method = _javaPart->javaClassStatic()->getMethod<void()>("resetConversation");
289
289
  method(_javaPart);
290
290
  }
291
+ void JHybridLiteRTLMSpec::resetConversationWith(const std::vector<Message>& messages) {
292
+ static const auto method = _javaPart->javaClassStatic()->getMethod<void(jni::alias_ref<jni::JArrayClass<JMessage>> /* messages */)>("resetConversationWith");
293
+ method(_javaPart, [&](auto&& __input) {
294
+ size_t __size = __input.size();
295
+ jni::local_ref<jni::JArrayClass<JMessage>> __array = jni::JArrayClass<JMessage>::newArray(__size);
296
+ for (size_t __i = 0; __i < __size; __i++) {
297
+ const auto& __element = __input[__i];
298
+ auto __elementJni = JMessage::fromCpp(__element);
299
+ __array->setElement(__i, *__elementJni);
300
+ }
301
+ return __array;
302
+ }(messages));
303
+ }
291
304
  bool JHybridLiteRTLMSpec::isReady() {
292
305
  static const auto method = _javaPart->javaClassStatic()->getMethod<jboolean()>("isReady");
293
306
  auto __result = method(_javaPart);
@@ -66,6 +66,7 @@ namespace margelo::nitro::litertlm {
66
66
  std::shared_ptr<Promise<void>> sendMessageAsync(const std::string& message, const std::function<void(const std::string& /* token */, bool /* done */)>& onToken) override;
67
67
  std::vector<Message> getHistory() override;
68
68
  void resetConversation() override;
69
+ void resetConversationWith(const std::vector<Message>& messages) override;
69
70
  bool isReady() override;
70
71
  GenerationStats getStats() override;
71
72
  double countTokens(const std::string& text) override;
@@ -98,6 +98,10 @@ abstract class HybridLiteRTLMSpec: HybridObject() {
98
98
  @Keep
99
99
  abstract fun resetConversation(): Unit
100
100
 
101
+ @DoNotStrip
102
+ @Keep
103
+ abstract fun resetConversationWith(messages: Array<Message>): Unit
104
+
101
105
  @DoNotStrip
102
106
  @Keep
103
107
  abstract fun isReady(): Boolean
@@ -206,6 +206,12 @@ namespace margelo::nitro::litertlm {
206
206
  std::rethrow_exception(__result.error());
207
207
  }
208
208
  }
209
+ inline void resetConversationWith(const std::vector<Message>& messages) override {
210
+ auto __result = _swiftPart.resetConversationWith(messages);
211
+ if (__result.hasError()) [[unlikely]] {
212
+ std::rethrow_exception(__result.error());
213
+ }
214
+ }
209
215
  inline bool isReady() override {
210
216
  auto __result = _swiftPart.isReady();
211
217
  if (__result.hasError()) [[unlikely]] {
@@ -25,6 +25,7 @@ public protocol HybridLiteRTLMSpec_protocol: HybridObject {
25
25
  func sendMessageAsync(message: String, onToken: @escaping (_ token: String, _ done: Bool) -> Void) throws -> Promise<Void>
26
26
  func getHistory() throws -> [Message]
27
27
  func resetConversation() throws -> Void
28
+ func resetConversationWith(messages: [Message]) throws -> Void
28
29
  func isReady() throws -> Bool
29
30
  func getStats() throws -> GenerationStats
30
31
  func countTokens(text: String) throws -> Double
@@ -370,6 +370,17 @@ open class HybridLiteRTLMSpec_cxx {
370
370
  }
371
371
  }
372
372
 
373
+ @inline(__always)
374
+ public final func resetConversationWith(messages: bridge.std__vector_Message_) -> bridge.Result_void_ {
375
+ do {
376
+ try self.__implementation.resetConversationWith(messages: messages.map({ __item in __item }))
377
+ return bridge.create_Result_void_()
378
+ } catch (let __error) {
379
+ let __exceptionPtr = __error.toCpp()
380
+ return bridge.create_Result_void_(__exceptionPtr)
381
+ }
382
+ }
383
+
373
384
  @inline(__always)
374
385
  public final func isReady() -> bridge.Result_bool_ {
375
386
  do {
@@ -26,6 +26,7 @@ namespace margelo::nitro::litertlm {
26
26
  prototype.registerHybridMethod("sendMessageAsync", &HybridLiteRTLMSpec::sendMessageAsync);
27
27
  prototype.registerHybridMethod("getHistory", &HybridLiteRTLMSpec::getHistory);
28
28
  prototype.registerHybridMethod("resetConversation", &HybridLiteRTLMSpec::resetConversation);
29
+ prototype.registerHybridMethod("resetConversationWith", &HybridLiteRTLMSpec::resetConversationWith);
29
30
  prototype.registerHybridMethod("isReady", &HybridLiteRTLMSpec::isReady);
30
31
  prototype.registerHybridMethod("getStats", &HybridLiteRTLMSpec::getStats);
31
32
  prototype.registerHybridMethod("countTokens", &HybridLiteRTLMSpec::countTokens);
@@ -90,6 +90,7 @@ namespace margelo::nitro::litertlm {
90
90
  virtual std::shared_ptr<Promise<void>> sendMessageAsync(const std::string& message, const std::function<void(const std::string& /* token */, bool /* done */)>& onToken) = 0;
91
91
  virtual std::vector<Message> getHistory() = 0;
92
92
  virtual void resetConversation() = 0;
93
+ virtual void resetConversationWith(const std::vector<Message>& messages) = 0;
93
94
  virtual bool isReady() = 0;
94
95
  virtual GenerationStats getStats() = 0;
95
96
  virtual double countTokens(const std::string& text) = 0;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dr33m/react-native-litert-lm",
3
- "version": "0.5.6",
3
+ "version": "0.6.0",
4
4
  "litertLm": {
5
5
  "version": "0.12.0",
6
6
  "androidMavenVersion": "0.12.0",
@@ -81,6 +81,7 @@ export const mockLiteRTLM = {
81
81
  ),
82
82
  getHistory: jest.fn(() => []),
83
83
  resetConversation: jest.fn(),
84
+ resetConversationWith: jest.fn(),
84
85
  getStats: jest.fn(() => ({
85
86
  promptTokens: 10,
86
87
  completionTokens: 20,
@@ -49,6 +49,27 @@ describe('modelFactory Security & Proxy Unit Tests', () => {
49
49
  expect(mockLiteRTLM.getMemoryUsage).toHaveBeenCalled();
50
50
  });
51
51
 
52
+ it('should successfully proxy resetConversationWith and record memory metrics', async () => {
53
+ const seed = [{ role: 'user', content: 'earlier turn' }];
54
+ await llm.resetConversationWith(seed as never);
55
+
56
+ expect(mockLiteRTLM.resetConversationWith).toHaveBeenCalledWith(seed);
57
+ expect(mockLiteRTLM.getMemoryUsage).toHaveBeenCalled();
58
+ });
59
+
60
+ it('should report resetConversationWith as absent on older native builds', () => {
61
+ // The app feature-detects this method, so the proxy must not hand back a
62
+ // wrapper the native side cannot service.
63
+ const original = mockLiteRTLM.resetConversationWith;
64
+ // @ts-expect-error deliberately removing the method to simulate old native
65
+ delete mockLiteRTLM.resetConversationWith;
66
+ try {
67
+ expect(createLLM().resetConversationWith).toBeUndefined();
68
+ } finally {
69
+ mockLiteRTLM.resetConversationWith = original;
70
+ }
71
+ });
72
+
52
73
  it('should successfully proxy sendMessageAsync and record memory metrics when done', async () => {
53
74
  const onToken = jest.fn();
54
75
  await llm.sendMessageAsync("Async prompt", onToken);
@@ -130,6 +130,20 @@ export function createLLM(options?: {
130
130
  };
131
131
  }
132
132
 
133
+ if (prop === "resetConversationWith") {
134
+ // Absent on native builds older than this JS wrapper, and callers
135
+ // feature-detect it — so fall through rather than handing back a
136
+ // wrapper that would fail at the bridge.
137
+ if (typeof (target as any).resetConversationWith !== "function") {
138
+ return undefined;
139
+ }
140
+ return (messages: unknown[]) => {
141
+ const result = (target as any).resetConversationWith(messages);
142
+ recordMemorySnapshot();
143
+ return result;
144
+ };
145
+ }
146
+
133
147
  if (prop === "sendToolResponse") {
134
148
  return (responses: any[], onToken?: TokenCallback) => {
135
149
  if (onToken) {
@@ -364,6 +364,24 @@ export interface LiteRTLM extends HybridObject<{
364
364
  */
365
365
  resetConversation(): void;
366
366
 
367
+ /**
368
+ * Clear the conversation and re-seed it with prior turns.
369
+ *
370
+ * The engine allocates one KV cache per conversation, sized by
371
+ * `maxContextTokens`. Long tool-calling sessions fill it, and overflowing it
372
+ * aborts the process rather than raising a catchable error. Recreating the
373
+ * conversation frees the cache, but `resetConversation()` alone also throws
374
+ * away the thread.
375
+ *
376
+ * This seeds the fresh conversation with `messages` as prior context, so a
377
+ * caller can drop the oldest turns and keep the recent ones. The system
378
+ * prompt and tools configured at `loadModel` are reapplied automatically.
379
+ * No generation is triggered and no reply is produced.
380
+ *
381
+ * @param messages Prior turns, oldest first.
382
+ */
383
+ resetConversationWith(messages: Message[]): void;
384
+
367
385
  /**
368
386
  * Check if a model is loaded and ready for inference.
369
387
  */