AI Configuration Widgets simplified & separated

This commit is contained in:
Manas Hejmadi
2025-06-08 21:09:47 +05:30
parent 8a12ca7c5a
commit b890769854
5 changed files with 128 additions and 86 deletions

View File

@@ -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(

View File

@@ -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),
],
),
),

View File

@@ -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,

View File

@@ -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);
},
);
}
}

View File

@@ -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'),
);
}
}