Skip to content

Commit

Permalink
small fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
danemadsen committed Feb 23, 2025
1 parent 0ae745c commit 0ae66ff
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 44 deletions.
9 changes: 0 additions & 9 deletions lib/controllers/app_settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,6 @@ class AppSettings extends ChangeNotifier {
load();
}

void notify() {
notifyListeners();
}

void saveAndNotify() {
save();
notifyListeners();
}

File? _userImage;

File? get userImage => _userImage;
Expand Down
59 changes: 54 additions & 5 deletions lib/controllers/artificial_intelligence_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ abstract class ArtificialIntelligenceController extends ChangeNotifier {
await prefs.setString(type, contextString);
}

static Future<ArtificialIntelligenceController> load(String? type) async {
static Future<ArtificialIntelligenceController> load([String? type]) async {
final prefs = await SharedPreferences.getInstance();

type ??= prefs.getString('ai_type') ?? 'llama_cpp';
Expand Down Expand Up @@ -95,6 +95,16 @@ abstract class ArtificialIntelligenceController extends ChangeNotifier {
Stream<String> prompt(List<ChatMessage> messages);

void stop();

void clear() async {
_model = null;
_overrides = {};
notifyListeners();

final prefs = await SharedPreferences.getInstance();
await prefs.remove(type);
await prefs.remove('ai_type');
}
}

abstract class RemoteArtificialIntelligenceController extends ArtificialIntelligenceController {
Expand Down Expand Up @@ -149,6 +159,19 @@ abstract class RemoteArtificialIntelligenceController extends ArtificialIntellig
}

Future<List<String>> getModelOptions();

@override
void clear() async {
_model = null;
_overrides = {};
_baseUrl = null;
_apiKey = null;
notifyListeners();

final prefs = await SharedPreferences.getInstance();
await prefs.remove(type);
await prefs.remove('ai_type');
}
}

class LlamaCppController extends ArtificialIntelligenceController {
Expand Down Expand Up @@ -238,14 +261,25 @@ class LlamaCppController extends ArtificialIntelligenceController {
}

@override
void stop() => _llama?.stop();
void stop() {
_llama?.stop();
busy = false;
}

@override
void notifyListeners() {
super.notifyListeners();
reloadModel();
save();
}

@override
void clear() {
super.clear();
_llama = null;
_loadedHash = '';
loading = false;
}
}

class OllamaController extends RemoteArtificialIntelligenceController {
Expand Down Expand Up @@ -311,7 +345,16 @@ class OllamaController extends RemoteArtificialIntelligenceController {
}

@override
void stop() => _ollamaClient.endSession();
void stop() {
_ollamaClient.endSession();
busy = false;
}

@override
void clear() {
super.clear();
_searchLocalNetwork = null;
}

Future<Uri?> checkForOllama(Uri url) async {
try {
Expand Down Expand Up @@ -499,7 +542,10 @@ class OpenAIController extends RemoteArtificialIntelligenceController {
}

@override
void stop() => _openAiClient.endSession();
void stop() {
_openAiClient.endSession();
busy = false;
}

@override
Future<List<String>> getModelOptions() async {
Expand Down Expand Up @@ -595,7 +641,10 @@ class MistralController extends RemoteArtificialIntelligenceController {
}

@override
void stop() => _mistralClient.endSession();
void stop() {
_mistralClient.endSession();
busy = false;
}

@override
Future<List<String>> getModelOptions() async => [
Expand Down
40 changes: 19 additions & 21 deletions lib/controllers/chat_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,6 @@ class ChatController extends ChangeNotifier {
load();
}

void notify() {
notifyListeners();
}

void saveAndNotify() {
save();
notifyListeners();
}

List<GeneralTreeNode<ChatMessage>> _chats = [];

List<GeneralTreeNode<ChatMessage>> get chats => _chats;
Expand All @@ -37,53 +28,60 @@ class ChatController extends ChangeNotifier {

_chats.insert(0, newRoot);

saveAndNotify();
save();
notifyListeners();
}

void newChat() {
final chat = GeneralTreeNode<ChatMessage>(SystemChatMessage('New Chat'));

_chats.insert(0, chat);

saveAndNotify();
save();
notifyListeners();
}

void deleteChat(GeneralTreeNode<ChatMessage> chat) {
_chats.remove(chat);
saveAndNotify();
save();
notifyListeners();
}

void clearChats() {
void clear() {
_chats.clear();
saveAndNotify();
save();
notifyListeners();
}

void addToEnd(ChatMessage message) {
root.chain.last.addChild(message);
saveAndNotify();
save();
notifyListeners();
}

Future<void> streamToEnd(Stream<String> stream) async {
root.chain.last.addChild(AssistantChatMessage(''));
notify();
notifyListeners();

await for (final response in stream) {
root.chain.last.data.content += response;
notify();
notifyListeners();
}

saveAndNotify();
save();
notifyListeners();
}

Future<void> streamToChild(GeneralTreeNode<ChatMessage> node, Stream<String> stream) async {
notify();
notifyListeners();

await for (final response in stream) {
node.currentChild!.data.content += response;
notify();
notifyListeners();
}

saveAndNotify();
save();
notifyListeners();
}

Future<void> load() async {
Expand Down
14 changes: 13 additions & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,11 @@ part 'widgets/text_fields/listenable_text_field.dart';

part 'widgets/code_box.dart';

void main() => runApp(Maid());
void main() {
WidgetsFlutterBinding.ensureInitialized();

runApp(Maid());
}

class Maid extends StatefulWidget {
final AppSettings settings = AppSettings();
Expand All @@ -86,6 +90,14 @@ class MaidState extends State<Maid> {

static MaidState of(BuildContext context) => context.findAncestorStateOfType<MaidState>()!;

@override
void initState() {
super.initState();
ArtificialIntelligenceController.load().then(
(newController) => setState(() => aiController = newController)
);
}

Future<void> switchAi(String type) async {
await aiController.save();

Expand Down
9 changes: 7 additions & 2 deletions lib/widgets/message/message.dart
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,17 @@ class MessageWidgetState extends State<MessageWidget> {
buildMessageEditingColumn() :
GestureDetector(
onHorizontalDragEnd: onHorizontalDragEnd,
child: buildMessageColumn()
child: buildChatListener()
),
);

Widget buildChatListener() => ListenableBuilder(
listenable: widget.chatController,
builder: buildMessageColumn
);

// The buildMessageColumn method will build the message column when the message is not being edited.
Widget buildMessageColumn() {
Widget buildMessageColumn(BuildContext context, Widget? child) {
List<Widget> children = [
buildTopRow(),
];
Expand Down
9 changes: 7 additions & 2 deletions lib/widgets/message/message_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,16 @@ class MessageViewState extends State<MessageView> {
Widget build(BuildContext context) => Expanded(
child: SingleChildScrollView(
controller: controller,
child: messageBuilder(),
child: buildChatListener(),
)
);

Widget messageBuilder() => ListenableBuilder(
Widget buildChatListener() => ListenableBuilder(
listenable: widget.chatController,
builder: buildSettingsListener
);

Widget buildSettingsListener(BuildContext context, Widget? child) => ListenableBuilder(
listenable: widget.settings,
builder: buildMessage
);
Expand Down
9 changes: 5 additions & 4 deletions lib/widgets/pages/settings_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,18 @@ class SettingsPage extends StatelessWidget {
runSpacing: 16,
children: [
ElevatedButton(
onPressed: chatController.clearChats,
onPressed: chatController.clear,
child: const Text('Clear Chats')
),
ElevatedButton(
onPressed: settings.clear,
child: const Text('Reset Settings')
),
ElevatedButton(
onPressed: () async {
final prefs = await SharedPreferences.getInstance();
await prefs.clear();
onPressed: () {
settings.clear();
chatController.clear();
aiController.clear();
},
child: const Text('Clear Cache')
)
Expand Down
3 changes: 3 additions & 0 deletions lib/widgets/text_fields/listenable_text_field.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ class ListenableTextFieldState<T> extends State<ListenableTextField<T>> {
) {
controller.text = value;
}
else if (value == null || value.isEmpty) {
controller.clear();
}

return TextField(
decoration: InputDecoration(
Expand Down

0 comments on commit 0ae66ff

Please sign in to comment.