From b89076985458159a69332e891e8d14f6da543f43 Mon Sep 17 00:00:00 2001 From: Manas Hejmadi Date: Sun, 8 Jun 2025 21:09:47 +0530 Subject: [PATCH] AI Configuration Widgets simplified & separated --- lib/main.dart | 7 +- .../ai_request/aireq_configs.dart | 80 +++++------------ .../request_pane/ai_request/aireq_prompt.dart | 2 - .../genai/lib/widgets/ai_config_widgets.dart | 90 +++++++++++++++++++ packages/genai/lib/widgets/llm_selector.dart | 35 +++----- 5 files changed, 128 insertions(+), 86 deletions(-) create mode 100644 packages/genai/lib/widgets/ai_config_widgets.dart diff --git a/lib/main.dart b/lib/main.dart index 675b0a54..d3315a14 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -25,10 +25,9 @@ void main() async { settingsModel = settingsModel?.copyWithPath(workspaceFolderPath: null); } - //Load all LLM - LLMManager.fetchAvailableLLMs().then((_) { - LLMManager.loadAvailableLLMs().then((_) {}); - }); + //Load all LLMs + await LLMManager.fetchAvailableLLMs(); + await LLMManager.loadAvailableLLMs(); runApp( ProviderScope( diff --git a/lib/screens/home_page/editor_pane/details_card/request_pane/ai_request/aireq_configs.dart b/lib/screens/home_page/editor_pane/details_card/request_pane/ai_request/aireq_configs.dart index eb1fb7cb..2fcba89b 100644 --- a/lib/screens/home_page/editor_pane/details_card/request_pane/ai_request/aireq_configs.dart +++ b/lib/screens/home_page/editor_pane/details_card/request_pane/ai_request/aireq_configs.dart @@ -4,6 +4,7 @@ import 'package:apidash_design_system/widgets/textfield_outlined.dart'; import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:genai/genai.dart'; +import 'package:genai/widgets/ai_config_widgets.dart'; class AIRequestConfigSection extends ConsumerStatefulWidget { const AIRequestConfigSection({super.key}); @@ -45,85 +46,46 @@ class _AIRequestConfigSectionState children: [ Text( el.configDescription, - style: TextStyle(color: Colors.white30), ), SizedBox(height: 5), if (el.configType == LLMModelConfigurationType.boolean) ...[ - Switch( - value: el.configValue.value as bool, - onChanged: (x) { - el.configValue.value = x; + BooleanAIConfig( + configuration: el, + onConfigUpdated: (x) { updateRequestModel(el); setState(() {}); }, - ) + ), ] else if (el.configType == LLMModelConfigurationType.numeric) ...[ - ADOutlinedTextField( - initialValue: el.configValue.value.toString(), - onChanged: (x) { - if (x.isEmpty) x = '0'; - if (num.tryParse(x) == null) return; - el.configValue.value = num.parse(x); + WritableAIConfig( + configuration: el, + onConfigUpdated: (x) { updateRequestModel(el); setState(() {}); }, - ) + numeric: true, + ), ] else if (el.configType == LLMModelConfigurationType.text) ...[ - ADOutlinedTextField( - initialValue: el.configValue.value.toString(), - onChanged: (x) { - el.configValue.value = x; + WritableAIConfig( + configuration: el, + onConfigUpdated: (x) { updateRequestModel(el); setState(() {}); }, - ) + ), ] else if (el.configType == LLMModelConfigurationType.slider) ...[ - Row( - children: [ - Expanded( - child: Slider( - min: (el.configValue.value as ( - double, - double, - double - )) - .$1, - value: (el.configValue.value as ( - double, - double, - double - )) - .$2, - max: (el.configValue.value as ( - double, - double, - double - )) - .$3, - onChanged: (x) { - final z = el.configValue.value as ( - double, - double, - double - ); - el.configValue.value = (z.$1, x, z.$3); - updateRequestModel(el); - setState(() {}); - }, - ), - ), - Text((el.configValue.value as (double, double, double)) - .$2 - .toStringAsFixed(2)), - ], - ) + SliderAIConfig( + configuration: el, + onSliderUpdated: (x) { + updateRequestModel(x); + setState(() {}); + }, + ), ], SizedBox(height: 10), - // Divider(color: Colors.white10), - // SizedBox(height: 10), ], ), ), diff --git a/lib/screens/home_page/editor_pane/details_card/request_pane/ai_request/aireq_prompt.dart b/lib/screens/home_page/editor_pane/details_card/request_pane/ai_request/aireq_prompt.dart index cca6c511..ce94f44e 100644 --- a/lib/screens/home_page/editor_pane/details_card/request_pane/ai_request/aireq_prompt.dart +++ b/lib/screens/home_page/editor_pane/details_card/request_pane/ai_request/aireq_prompt.dart @@ -26,7 +26,6 @@ class AIRequestPromptSection extends ConsumerWidget { padding: const EdgeInsets.only(left: 25.0), child: Text( 'System Prompt', - style: TextStyle(color: Colors.white54), ), ), kVSpacer10, @@ -55,7 +54,6 @@ class AIRequestPromptSection extends ConsumerWidget { padding: const EdgeInsets.only(left: 25.0), child: Text( 'User Prompt / Input', - style: TextStyle(color: Colors.white54), ), ), kVSpacer10, diff --git a/packages/genai/lib/widgets/ai_config_widgets.dart b/packages/genai/lib/widgets/ai_config_widgets.dart new file mode 100644 index 00000000..2113b447 --- /dev/null +++ b/packages/genai/lib/widgets/ai_config_widgets.dart @@ -0,0 +1,90 @@ +import 'package:flutter/material.dart'; +import 'package:genai/llm_config.dart'; + +class SliderAIConfig extends StatelessWidget { + final LLMModelConfiguration configuration; + final Function(LLMModelConfiguration) onSliderUpdated; + const SliderAIConfig({ + super.key, + required this.configuration, + required this.onSliderUpdated, + }); + + @override + Widget build(BuildContext context) { + return Row( + children: [ + Expanded( + child: Slider( + min: (configuration.configValue.value as (double, double, double)) + .$1, + value: (configuration.configValue.value as (double, double, double)) + .$2, + max: (configuration.configValue.value as (double, double, double)) + .$3, + onChanged: (x) { + final z = + configuration.configValue.value as (double, double, double); + configuration.configValue.value = (z.$1, x, z.$3); + onSliderUpdated(configuration); + }, + ), + ), + Text( + (configuration.configValue.value as (double, double, double)).$2 + .toStringAsFixed(2), + ), + ], + ); + } +} + +class WritableAIConfig extends StatelessWidget { + final bool numeric; + final LLMModelConfiguration configuration; + final Function(LLMModelConfiguration) onConfigUpdated; + const WritableAIConfig({ + super.key, + this.numeric = false, + required this.configuration, + required this.onConfigUpdated, + }); + + @override + Widget build(BuildContext context) { + return TextFormField( + initialValue: configuration.configValue.value.toString(), + onChanged: (x) { + if (numeric) { + if (x.isEmpty) x = '0'; + if (num.tryParse(x) == null) return; + configuration.configValue.value = num.parse(x); + } else { + configuration.configValue.value = x; + } + onConfigUpdated(configuration); + }, + ); + } +} + +class BooleanAIConfig extends StatelessWidget { + final LLMModelConfiguration configuration; + final Function(LLMModelConfiguration) onConfigUpdated; + const BooleanAIConfig({ + super.key, + required this.configuration, + required this.onConfigUpdated, + }); + + @override + Widget build(BuildContext context) { + return Switch( + value: configuration.configValue.value as bool, + onChanged: (x) { + configuration.configValue.value = x; + onConfigUpdated(configuration); + }, + ); + } +} diff --git a/packages/genai/lib/widgets/llm_selector.dart b/packages/genai/lib/widgets/llm_selector.dart index ddb67c04..f53670d5 100644 --- a/packages/genai/lib/widgets/llm_selector.dart +++ b/packages/genai/lib/widgets/llm_selector.dart @@ -14,29 +14,22 @@ class DefaultLLMSelectorButton extends StatelessWidget { @override Widget build(BuildContext context) { - return Row( - mainAxisSize: MainAxisSize.min, - children: [ - Chip(label: Text(defaultLLM?.selectedLLM.modelName ?? 'none')), - SizedBox(height: 10), - IconButton( - onPressed: () async { - final saveObject = await showDialog( - context: context, - builder: (context) { - return AlertDialog( - scrollable: true, - content: DefaultLLMSelectorDialog(defaultLLM: defaultLLM), - contentPadding: EdgeInsets.all(10), - ); - }, + return ElevatedButton( + onPressed: () async { + final saveObject = await showDialog( + context: context, + builder: (context) { + return AlertDialog( + scrollable: true, + content: DefaultLLMSelectorDialog(defaultLLM: defaultLLM), + contentPadding: EdgeInsets.all(10), ); - if (saveObject == null) return; - onDefaultLLMUpdated(saveObject); }, - icon: Icon(Icons.edit), - ), - ], + ); + if (saveObject == null) return; + onDefaultLLMUpdated(saveObject); + }, + child: Text(defaultLLM?.selectedLLM.modelName ?? 'Select Model'), ); } }