diff --git a/core/llama.cpp b/core/llama.cpp index 866198e4..7feec2ef 160000 --- a/core/llama.cpp +++ b/core/llama.cpp @@ -1 +1 @@ -Subproject commit 866198e477de5c3e12302cdd84b5541927b950d8 +Subproject commit 7feec2efee385a5fb6b4cc9f847be2ca66c95174 diff --git a/lib/core/core.dart b/lib/core/local_generation.dart similarity index 92% rename from lib/core/core.dart rename to lib/core/local_generation.dart index 2ba3ecb7..a8232e2b 100644 --- a/lib/core/core.dart +++ b/lib/core/local_generation.dart @@ -11,18 +11,18 @@ import 'package:maid/utilities/character.dart'; import 'package:maid/utilities/model.dart'; import 'package:maid/utilities/memory_manager.dart'; -class Core { +class LocalGeneration { static SendPort? _sendPort; late NativeLibrary _nativeLibrary; // Make the default constructor private - Core._(); + LocalGeneration._(); // Private reference to the global instance - static final Core _instance = Core._(); + static final LocalGeneration _instance = LocalGeneration._(); // Public accessor to the global instance - static Core get instance { + static LocalGeneration get instance { _instance._initialize(); return _instance; } @@ -64,11 +64,11 @@ class Core { } } - static promptIsolate(Map args) async { + static _promptIsolate(Map args) async { _sendPort = args['port'] as SendPort?; String input = args['input']; Pointer text = input.trim().toNativeUtf8().cast(); - Core.instance._nativeLibrary.core_prompt(text, Pointer.fromFunction(_maidOutputBridge)); + LocalGeneration.instance._nativeLibrary.core_prompt(text, Pointer.fromFunction(_maidOutputBridge)); } @@ -129,14 +129,14 @@ class Core { await completer.future; } - void prompt(String input) { + void prompt(String input) async { MessageManager.busy = true; if (!_hasStarted) { - MemoryManager.save(); + await MemoryManager.asave(); _init(input); } else { Logger.log("Input: $input"); - Isolate.spawn(promptIsolate, { + Isolate.spawn(_promptIsolate, { 'input': input, 'port': _sendPort }); diff --git a/lib/pages/home_page.dart b/lib/pages/home_page.dart index e1043f58..3341bb02 100644 --- a/lib/pages/home_page.dart +++ b/lib/pages/home_page.dart @@ -7,7 +7,7 @@ import 'package:maid/utilities/message_manager.dart'; import 'package:system_info2/system_info2.dart'; import 'package:maid/utilities/model.dart'; -import 'package:maid/core/core.dart'; +import 'package:maid/core/local_generation.dart'; import 'package:maid/pages/character_page.dart'; import 'package:maid/pages/model_page.dart'; @@ -89,7 +89,7 @@ class MaidHomePageState extends State { MessageManager.add(UniqueKey()); if (MemoryManager.checkFileExists(model.parameters["model_path"])) { - Core.instance.prompt(promptController.text.trim()); + LocalGeneration.instance.prompt(promptController.text.trim()); setState(() { model.busy = true; promptController.clear(); @@ -247,7 +247,7 @@ class MaidHomePageState extends State { children: [ if (model.busy) IconButton( - onPressed: Core.instance.stop, + onPressed: LocalGeneration.instance.stop, iconSize: 50, icon: const Icon( Icons.stop_circle_sharp, diff --git a/lib/utilities/memory_manager.dart b/lib/utilities/memory_manager.dart index 32355aa0..c29ebbbb 100644 --- a/lib/utilities/memory_manager.dart +++ b/lib/utilities/memory_manager.dart @@ -4,7 +4,7 @@ import 'dart:io'; import 'package:maid/utilities/logger.dart'; import 'package:shared_preferences/shared_preferences.dart'; -import 'package:maid/core/core.dart'; +import 'package:maid/core/local_generation.dart'; import 'package:maid/utilities/model.dart'; import 'package:maid/utilities/character.dart'; @@ -33,24 +33,33 @@ class MemoryManager { }); } - static void save() { - SharedPreferences.getInstance().then((prefs) { - prefs.clear(); + static void _save(SharedPreferences prefs) { + prefs.clear(); - _models[model.nameController.text] = model.toMap(); - Logger.log("Model Saved: ${model.nameController.text}"); - _characters[character.nameController.text] = character.toMap(); - Logger.log("Character Saved: ${character.nameController.text}"); + _models[model.nameController.text] = model.toMap(); + Logger.log("Model Saved: ${model.nameController.text}"); + _characters[character.nameController.text] = character.toMap(); + Logger.log("Character Saved: ${character.nameController.text}"); - prefs.setString("models", json.encode(_models)); - prefs.setString("characters", json.encode(_characters)); - prefs.setString("current_model", model.nameController.text); - prefs.setString("current_character", character.nameController.text); + prefs.setString("models", json.encode(_models)); + prefs.setString("characters", json.encode(_characters)); + prefs.setString("current_model", model.nameController.text); + prefs.setString("current_character", character.nameController.text); + + LocalGeneration.instance.cleanup(); + } - Core.instance.cleanup(); + static void save() { + SharedPreferences.getInstance().then((prefs) { + _save(prefs); }); } + static Future asave() async { + var prefs = await SharedPreferences.getInstance(); + _save(prefs); + } + static void updateModel(String newName) { String oldName = model.nameController.text; Logger.log("Updating model $oldName ====> $newName"); diff --git a/lib/utilities/message_manager.dart b/lib/utilities/message_manager.dart index 05e78a72..9909eb57 100644 --- a/lib/utilities/message_manager.dart +++ b/lib/utilities/message_manager.dart @@ -3,7 +3,7 @@ import 'dart:collection'; import 'package:flutter/material.dart'; import 'package:maid/utilities/model.dart'; -import 'package:maid/core/core.dart'; +import 'package:maid/core/local_generation.dart'; class MessageManager { static void Function()? _callback; @@ -47,7 +47,7 @@ class MessageManager { parent.children.removeWhere((element) => element.key == key); _tail = _root.findTail(); } - Core.instance.cleanup(); + LocalGeneration.instance.cleanup(); _callback?.call(); } @@ -68,7 +68,7 @@ class MessageManager { } else { branch(key, false); model.busy = true; - Core.instance.prompt(parent.message); + LocalGeneration.instance.prompt(parent.message); } } @@ -79,7 +79,7 @@ class MessageManager { _tail = _root.findTail(); } add(UniqueKey(), userGenerated: userGenerated); - Core.instance.cleanup(); + LocalGeneration.instance.cleanup(); _callback?.call(); } @@ -102,7 +102,7 @@ class MessageManager { } } - Core.instance.cleanup(); + LocalGeneration.instance.cleanup(); _callback?.call(); } @@ -120,7 +120,7 @@ class MessageManager { } } - Core.instance.cleanup(); + LocalGeneration.instance.cleanup(); _callback?.call(); }