diff --git a/lib/data/res/openai.dart b/lib/data/res/openai.dart index 36a76eb..00250c6 100644 --- a/lib/data/res/openai.dart +++ b/lib/data/res/openai.dart @@ -6,6 +6,7 @@ import 'package:gpt_box/data/store/all.dart'; abstract final class OpenAICfg { static final nameNotifier = ValueNotifier(_cfg.name); + static final models = [].vn; static ChatConfig _cfg = () { final selectedKey = Stores.config.selectedKey.fetch(); @@ -20,6 +21,18 @@ abstract final class OpenAICfg { config.save(); Stores.config.selectedKey.put(config.id); nameNotifier.value = config.name; + + updateModels(); + } + + static void updateModels() { + try { + OpenAI.instance.model.list().then((value) { + models.value = value.map((e) => e.id).toList(); + }); + } catch (e) { + Loggers.app.warning('Failed to update models', e); + } } static void apply() { diff --git a/lib/main.dart b/lib/main.dart index 785225b..3f9f2b6 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -93,6 +93,7 @@ Future _initAppComponents() async { OpenAI.showLogs = !BuildMode.isRelease; OpenAI.showResponsesLogs = !BuildMode.isRelease; OpenAICfg.apply(); + OpenAICfg.updateModels(); SyncService.sync(force: true); } diff --git a/lib/view/page/home/chat.dart b/lib/view/page/home/chat.dart index da471cb..cc1b197 100644 --- a/lib/view/page/home/chat.dart +++ b/lib/view/page/home/chat.dart @@ -129,7 +129,7 @@ class _ChatPageState extends State<_ChatPage> final chatItem = chatItems[idx]; final node = _chatItemRNMap.putIfAbsent(chatItem.id, () => RNode()); return Padding( - padding: const EdgeInsets.all(7), + padding: const EdgeInsets.all(2), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ @@ -157,7 +157,7 @@ class _ChatPageState extends State<_ChatPage> .joinWith(UIs.height13), ); }, - ), + ).paddingSymmetric(horizontal: 7, vertical: 3), ), UIs.height13, ], @@ -214,6 +214,7 @@ class _ChatPageState extends State<_ChatPage> color: UIs.primaryColor, ), padding: const EdgeInsets.symmetric(horizontal: 7, vertical: 1), + margin: const EdgeInsets.symmetric(horizontal: 7), child: Text( chatItem.role.localized, style: TextStyle( diff --git a/lib/view/page/setting.dart b/lib/view/page/setting.dart index a76cf95..e9fb01b 100644 --- a/lib/view/page/setting.dart +++ b/lib/view/page/setting.dart @@ -1,4 +1,3 @@ -import 'package:dart_openai/dart_openai.dart'; import 'package:fl_lib/fl_lib.dart'; import 'package:flutter/material.dart'; import 'package:gpt_box/data/model/chat/config.dart'; @@ -392,19 +391,24 @@ class _SettingPageState extends State { } Widget _buildOpenAIModels(ChatConfig cfg) { - return ExpandTile( - leading: const Icon(Icons.model_training), - title: Text(l10n.model), - children: [ - _buildOpenAIChatModel(cfg), - _buildOpenAIImgModel(cfg), - _buildOpenAISpeechModel(cfg), - _buildOpenAITranscribeModel(cfg), - ], + return ValBuilder( + listenable: OpenAICfg.models, + builder: (models) { + return ExpandTile( + leading: const Icon(Icons.model_training), + title: Text(l10n.model), + children: [ + _buildOpenAIChatModel(cfg, models), + _buildOpenAIImgModel(cfg, models), + _buildOpenAISpeechModel(cfg, models), + _buildOpenAITranscribeModel(cfg, models), + ], + ); + }, ); } - Widget _buildOpenAIChatModel(ChatConfig cfg) { + Widget _buildOpenAIChatModel(ChatConfig cfg, List models) { final val = cfg.model; return ListTile( leading: const Icon(Icons.chat), @@ -420,13 +424,10 @@ class _SettingPageState extends State { ); return; } - final models = await context.showLoadingDialog( - fn: () async => await OpenAI.instance.model.list(), - ); - final modelStrs = models.map((e) => e.id).toList(); + final modelStrs = List.from(models); modelStrs.removeWhere((element) => !element.startsWith('gpt')); if (modelStrs.isEmpty) { - modelStrs.addAll(models.map((e) => e.id)); + modelStrs.addAll(models); } final model = await context.showPickSingleDialog( items: modelStrs, @@ -465,7 +466,7 @@ class _SettingPageState extends State { ); } - Widget _buildOpenAIImgModel(ChatConfig cfg) { + Widget _buildOpenAIImgModel(ChatConfig cfg, List models) { final val = cfg.imgModel; return ListTile( leading: const Icon(Icons.photo), @@ -481,13 +482,11 @@ class _SettingPageState extends State { ); return; } - final models = await context.showLoadingDialog( - fn: () async => await OpenAI.instance.model.list(), - ); - final modelStrs = models.map((e) => e.id).toList(); + + final modelStrs = List.from(models); modelStrs.removeWhere((element) => !element.startsWith('dall-e')); if (modelStrs.isEmpty) { - modelStrs.addAll(models.map((e) => e.id)); + modelStrs.addAll(models); } final model = await context.showPickSingleDialog( items: modelStrs, @@ -526,7 +525,7 @@ class _SettingPageState extends State { ); } - Widget _buildOpenAISpeechModel(ChatConfig cfg) { + Widget _buildOpenAISpeechModel(ChatConfig cfg, List models) { final val = cfg.speechModel; return ListTile( leading: const Icon(Icons.speaker), @@ -542,13 +541,11 @@ class _SettingPageState extends State { ); return; } - final models = await context.showLoadingDialog( - fn: () async => await OpenAI.instance.model.list(), - ); - final modelStrs = models.map((e) => e.id).toList(); + + final modelStrs = List.from(models); modelStrs.removeWhere((element) => !element.startsWith('tts')); if (modelStrs.isEmpty) { - modelStrs.addAll(models.map((e) => e.id)); + modelStrs.addAll(models); } final model = await context.showPickSingleDialog( items: modelStrs, @@ -587,7 +584,7 @@ class _SettingPageState extends State { ); } - Widget _buildOpenAITranscribeModel(ChatConfig cfg) { + Widget _buildOpenAITranscribeModel(ChatConfig cfg, List models) { final val = cfg.transcribeModel; return ListTile( leading: const Icon(Icons.transcribe), @@ -603,13 +600,11 @@ class _SettingPageState extends State { ); return; } - final models = await context.showLoadingDialog( - fn: () async => await OpenAI.instance.model.list(), - ); - final modelStrs = models.map((e) => e.id).toList(); + + final modelStrs = List.from(models); modelStrs.removeWhere((element) => !element.startsWith('whisper')); if (modelStrs.isEmpty) { - modelStrs.addAll(models.map((e) => e.id)); + modelStrs.addAll(models); } final model = await context.showPickSingleDialog( items: modelStrs, diff --git a/pubspec.lock b/pubspec.lock index 35a47e4..c6a3463 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -406,8 +406,8 @@ packages: dependency: "direct main" description: path: "." - ref: main - resolved-ref: "8b6e86918deba6fd3fdac971429edfaeb7076212" + ref: "72a52694b7c023c309475043627f599dd916f5ef" + resolved-ref: "72a52694b7c023c309475043627f599dd916f5ef" url: "https://github.com/lollipopkit/fl_lib" source: git version: "0.0.1" diff --git a/pubspec.yaml b/pubspec.yaml index 2289e7b..a62f9a7 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -26,7 +26,7 @@ dependencies: fl_lib: git: url: https://github.com/lollipopkit/fl_lib - ref: main + ref: 72a52694b7c023c309475043627f599dd916f5ef # path: ../fl_lib dev_dependencies: