diff --git a/lib/controller.dart b/lib/controller.dart index 372dfba..80b5b77 100644 --- a/lib/controller.dart +++ b/lib/controller.dart @@ -51,10 +51,11 @@ class Controller extends GetxController { isGenerating.value = true; // Add prompt to message list as user message + final userPrompt = prompt.value; messages.add(Message( role: Message.roleUser, type: Message.typeMessage, - content: prompt.value)); + content: userPrompt)); prompt.value = ''; diff --git a/lib/main.dart b/lib/main.dart index 6f7d3c8..84be060 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,12 +1,16 @@ import 'package:flutter/material.dart'; import 'package:get/get.dart'; +import 'package:get_storage/get_storage.dart'; import 'package:sydney_webui/pages/chat_page.dart'; -void main() => runApp( - GetMaterialApp( - home: ChatPage(), - theme: ThemeData( - colorSchemeSeed: Colors.teal, brightness: Brightness.light), - darkTheme: ThemeData( - colorSchemeSeed: Colors.teal, brightness: Brightness.dark)), - ); +void main() async { + await GetStorage.init(); + runApp( + GetMaterialApp( + home: ChatPage(), + theme: ThemeData( + colorSchemeSeed: Colors.teal, brightness: Brightness.light), + darkTheme: ThemeData( + colorSchemeSeed: Colors.teal, brightness: Brightness.dark)), + ); +} diff --git a/lib/services/sydney_service.dart b/lib/services/sydney_service.dart index c65b7d2..960ae74 100644 --- a/lib/services/sydney_service.dart +++ b/lib/services/sydney_service.dart @@ -1,3 +1,5 @@ +import 'package:get/get.dart'; +import 'package:get_storage/get_storage.dart'; import 'package:sydney_webui/utils/http.dart'; class SydneyService { @@ -7,15 +9,35 @@ class SydneyService { static const _defaultCookies = String.fromEnvironment("API_COOKIES"); // Instance fields - var baseUrl = Uri.tryParse(_defaultBaseUrl); - var accessToken = _defaultAccessToken; - var cookies = _defaultCookies; + final baseUrl = ''.obs; + final accessToken = ''.obs; + final cookies = ''.obs; Map _conversation = {}; + // Initializer + SydneyService() { + // Persist settings + final box = GetStorage(); + + // Load settings from storage + baseUrl.value = box.read('baseUrl') ?? _defaultBaseUrl; + accessToken.value = box.read('accessToken') ?? _defaultAccessToken; + cookies.value = box.read('cookies') ?? _defaultCookies; + + // Save settings when they change + ever(baseUrl, (baseUrl) => box.write('baseUrl', baseUrl)); + ever(accessToken, (accessToken) => box.write('accessToken', accessToken)); + ever(cookies, (cookies) => box.write('cookies', cookies)); + } + // Getters - Uri? get _createConversationUrl => baseUrl?.resolve("/conversation/new"); - Uri? get _askStreamUrl => baseUrl?.resolve("/chat/stream"); + Uri? get _createConversationUrl => + Uri.tryParse(baseUrl.value)?.resolve("/conversation/new"); + + Uri? get _askStreamUrl => + Uri.tryParse(baseUrl.value)?.resolve("/chat/stream"); + Map get _authHeaders => {"Authorization": "Bearer $accessToken"}; @@ -26,7 +48,6 @@ class SydneyService { } // Methods - Stream askStream( {required String prompt, required String context}) async* { if (_conversation.isEmpty) { diff --git a/lib/widgets/settings_dialog.dart b/lib/widgets/settings_dialog.dart index ce589c9..c41bab3 100644 --- a/lib/widgets/settings_dialog.dart +++ b/lib/widgets/settings_dialog.dart @@ -9,9 +9,9 @@ class SettingsDialog extends StatelessWidget { Widget build(BuildContext context) { final controller = Get.find(); - var apiBaseUrl = controller.sydneyService.baseUrl?.toString() ?? ''; - var apiAccessToken = controller.sydneyService.accessToken; - var apiCookies = controller.sydneyService.cookies; + var apiBaseUrl = controller.sydneyService.baseUrl.value; + var apiAccessToken = controller.sydneyService.accessToken.value; + var apiCookies = controller.sydneyService.cookies.value; return AlertDialog( title: const Text('Settings'), @@ -54,9 +54,9 @@ class SettingsDialog extends StatelessWidget { TextButton( child: const Text('Save'), onPressed: () { - controller.sydneyService.baseUrl = Uri.tryParse(apiBaseUrl); - controller.sydneyService.accessToken = apiAccessToken; - controller.sydneyService.cookies = apiCookies; + controller.sydneyService.baseUrl.value = apiBaseUrl; + controller.sydneyService.accessToken.value = apiAccessToken; + controller.sydneyService.cookies.value = apiCookies; Get.back(); }, ), diff --git a/pubspec.lock b/pubspec.lock index fc53816..ea36d94 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -120,6 +120,14 @@ packages: url: "https://pub.dev" source: hosted version: "4.6.6" + get_storage: + dependency: "direct main" + description: + name: get_storage + sha256: "39db1fffe779d0c22b3a744376e86febe4ade43bf65e06eab5af707dc84185a2" + url: "https://pub.dev" + source: hosted + version: "2.1.1" lints: dependency: transitive description: @@ -168,6 +176,62 @@ packages: url: "https://pub.dev" source: hosted version: "1.8.3" + path_provider: + dependency: transitive + description: + name: path_provider + sha256: a1aa8aaa2542a6bc57e381f132af822420216c80d4781f7aa085ca3229208aaa + url: "https://pub.dev" + source: hosted + version: "2.1.1" + path_provider_android: + dependency: transitive + description: + name: path_provider_android + sha256: e595b98692943b4881b219f0a9e3945118d3c16bd7e2813f98ec6e532d905f72 + url: "https://pub.dev" + source: hosted + version: "2.2.1" + path_provider_foundation: + dependency: transitive + description: + name: path_provider_foundation + sha256: "19314d595120f82aca0ba62787d58dde2cc6b5df7d2f0daf72489e38d1b57f2d" + url: "https://pub.dev" + source: hosted + version: "2.3.1" + path_provider_linux: + dependency: transitive + description: + name: path_provider_linux + sha256: f7a1fe3a634fe7734c8d3f2766ad746ae2a2884abe22e241a8b301bf5cac3279 + url: "https://pub.dev" + source: hosted + version: "2.2.1" + path_provider_platform_interface: + dependency: transitive + description: + name: path_provider_platform_interface + sha256: "94b1e0dd80970c1ce43d5d4e050a9918fce4f4a775e6142424c30a29a363265c" + url: "https://pub.dev" + source: hosted + version: "2.1.1" + path_provider_windows: + dependency: transitive + description: + name: path_provider_windows + sha256: "8bc9f22eee8690981c22aa7fc602f5c85b497a6fb2ceb35ee5a5e5ed85ad8170" + url: "https://pub.dev" + source: hosted + version: "2.2.1" + platform: + dependency: transitive + description: + name: platform + sha256: "0a279f0707af40c890e80b1e9df8bb761694c074ba7e1d4ab1bc4b728e200b59" + url: "https://pub.dev" + source: hosted + version: "3.1.3" plugin_platform_interface: dependency: transitive description: @@ -253,6 +317,14 @@ packages: url: "https://pub.dev" source: hosted version: "5.1.1" + xdg_directories: + dependency: transitive + description: + name: xdg_directories + sha256: "589ada45ba9e39405c198fe34eb0f607cddb2108527e658136120892beac46d2" + url: "https://pub.dev" + source: hosted + version: "1.0.3" sdks: dart: ">=3.2.3 <4.0.0" flutter: ">=3.13.0" diff --git a/pubspec.yaml b/pubspec.yaml index fe7eb09..8953116 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -31,6 +31,7 @@ dependencies: flutter: sdk: flutter get: + get_storage: file_picker: ^6.1.1 flutter_markdown: