Skip to content

Commit

Permalink
Merge pull request #79 from danemadsen/main
Browse files Browse the repository at this point in the history
Fix crash
  • Loading branch information
danemadsen authored Nov 9, 2023
2 parents 08d19f9 + d5f6f40 commit ca8f3b9
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 32 deletions.
18 changes: 9 additions & 9 deletions lib/core/core.dart → lib/core/local_generation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -64,11 +64,11 @@ class Core {
}
}

static promptIsolate(Map<String, dynamic> args) async {
static _promptIsolate(Map<String, dynamic> args) async {
_sendPort = args['port'] as SendPort?;
String input = args['input'];
Pointer<Char> text = input.trim().toNativeUtf8().cast<Char>();
Core.instance._nativeLibrary.core_prompt(text, Pointer.fromFunction(_maidOutputBridge));
LocalGeneration.instance._nativeLibrary.core_prompt(text, Pointer.fromFunction(_maidOutputBridge));
}


Expand Down Expand Up @@ -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
});
Expand Down
6 changes: 3 additions & 3 deletions lib/pages/home_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -89,7 +89,7 @@ class MaidHomePageState extends State<MaidHomePage> {
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();
Expand Down Expand Up @@ -247,7 +247,7 @@ class MaidHomePageState extends State<MaidHomePage> {
children: [
if (model.busy)
IconButton(
onPressed: Core.instance.stop,
onPressed: LocalGeneration.instance.stop,
iconSize: 50,
icon: const Icon(
Icons.stop_circle_sharp,
Expand Down
35 changes: 22 additions & 13 deletions lib/utilities/memory_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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<void> 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");
Expand Down
12 changes: 6 additions & 6 deletions lib/utilities/message_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -47,7 +47,7 @@ class MessageManager {
parent.children.removeWhere((element) => element.key == key);
_tail = _root.findTail();
}
Core.instance.cleanup();
LocalGeneration.instance.cleanup();
_callback?.call();
}

Expand All @@ -68,7 +68,7 @@ class MessageManager {
} else {
branch(key, false);
model.busy = true;
Core.instance.prompt(parent.message);
LocalGeneration.instance.prompt(parent.message);
}
}

Expand All @@ -79,7 +79,7 @@ class MessageManager {
_tail = _root.findTail();
}
add(UniqueKey(), userGenerated: userGenerated);
Core.instance.cleanup();
LocalGeneration.instance.cleanup();
_callback?.call();
}

Expand All @@ -102,7 +102,7 @@ class MessageManager {
}
}

Core.instance.cleanup();
LocalGeneration.instance.cleanup();
_callback?.call();
}

Expand All @@ -120,7 +120,7 @@ class MessageManager {
}
}

Core.instance.cleanup();
LocalGeneration.instance.cleanup();
_callback?.call();
}

Expand Down

0 comments on commit ca8f3b9

Please sign in to comment.