initializeAIRequest on AIRequest Creation & ControllerUpdate issues fixed

This commit is contained in:
Manas Hejmadi
2025-06-07 22:25:57 +05:30
parent 22b8168b73
commit ce56897320
5 changed files with 79 additions and 53 deletions

View File

@@ -1,7 +1,9 @@
import 'package:apidash_core/apidash_core.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:apidash/providers/providers.dart';
import 'package:apidash/widgets/widgets.dart';
import 'package:genai/genai.dart';
class APITypeDropdown extends ConsumerWidget {
const APITypeDropdown({super.key});
@@ -14,6 +16,9 @@ class APITypeDropdown extends ConsumerWidget {
return APITypePopupMenu(
apiType: apiType,
onChanged: (type) {
if (type == APIType.ai) {
initializeAIRequest(ref);
}
ref
.read(collectionStateNotifierProvider.notifier)
.update(apiType: type);
@@ -21,3 +26,33 @@ class APITypeDropdown extends ConsumerWidget {
);
}
}
initializeAIRequest(WidgetRef ref) {
final selectedId = ref.watch(selectedIdStateProvider);
final req = ref.watch(collectionStateNotifierProvider)![selectedId]!;
AIRequestModel? aiRequestModel = req.aiRequestModel;
LLMSaveObject? defaultLLMSO = ref
.watch(settingsProvider.notifier)
.settingsModel
?.defaultLLMSaveObject; //Settings Default
if (aiRequestModel == null) {
// Creating the AIRequest Model initially
final gmC = GeminiModelController();
final newAIRM = AIRequestModel(
model: defaultLLMSO?.selectedLLM ??
LLMProvider.gemini.getLLMByIdentifier('gemini-2.0-flash'),
provider: defaultLLMSO?.provider ?? LLMProvider.gemini,
payload: LLMInputPayload(
endpoint: defaultLLMSO?.endpoint ?? gmC.inputPayload.endpoint,
credential: defaultLLMSO?.credential ?? '',
systemPrompt: '',
userPrompt: '',
configMap: defaultLLMSO?.configMap ?? gmC.inputPayload.configMap,
),
);
ref.read(collectionStateNotifierProvider.notifier).update(
aiRequestModel: newAIRM,
);
}
}

View File

@@ -27,10 +27,13 @@ class AIRequestAuthorizationSection extends ConsumerWidget {
fieldKey: "$selectedId-aireq-authvalue-body",
initialValue: cred,
onChanged: (String value) {
payload.credential = value;
final aim = ref
.read(collectionStateNotifierProvider)![selectedId]!
.aiRequestModel!;
aim.payload.credential = value;
ref
.read(collectionStateNotifierProvider.notifier)
.update(aiRequestModel: aiReqM.updatePayload(payload));
.update(aiRequestModel: aim.updatePayload(aim.payload));
},
hintText: 'Enter API key or Authorization Credentials',
),

View File

@@ -22,6 +22,16 @@ class _AIRequestConfigSectionState
final aiReqM = reqM.aiRequestModel!;
final payload = aiReqM.payload;
updateRequestModel(LLMModelConfiguration el) {
final aim = ref
.read(collectionStateNotifierProvider)![selectedId]!
.aiRequestModel!;
aim.payload.configMap[el.configId] = el;
ref.read(collectionStateNotifierProvider.notifier).update(
aiRequestModel: aim.updatePayload(aim.payload),
);
}
return SingleChildScrollView(
padding: EdgeInsets.symmetric(vertical: 20),
child: Column(
@@ -43,12 +53,7 @@ class _AIRequestConfigSectionState
value: el.configValue.value as bool,
onChanged: (x) {
el.configValue.value = x;
payload.configMap[el.configId] = el;
ref
.read(collectionStateNotifierProvider.notifier)
.update(
aiRequestModel: aiReqM.updatePayload(payload),
);
updateRequestModel(el);
setState(() {});
},
)
@@ -60,14 +65,7 @@ class _AIRequestConfigSectionState
if (x.isEmpty) x = '0';
if (num.tryParse(x) == null) return;
el.configValue.value = num.parse(x);
payload.configMap[el.configId] = el;
ref
.read(collectionStateNotifierProvider.notifier)
.update(
aiRequestModel: aiReqM.updatePayload(payload),
);
updateRequestModel(el);
setState(() {});
},
)
@@ -77,14 +75,7 @@ class _AIRequestConfigSectionState
initialValue: el.configValue.value.toString(),
onChanged: (x) {
el.configValue.value = x;
payload.configMap[el.configId] = el;
ref
.read(collectionStateNotifierProvider.notifier)
.update(
aiRequestModel: aiReqM.updatePayload(payload),
);
updateRequestModel(el);
setState(() {});
},
)
@@ -119,16 +110,7 @@ class _AIRequestConfigSectionState
double
);
el.configValue.value = (z.$1, x, z.$3);
payload.configMap[el.configId] = el;
ref
.read(
collectionStateNotifierProvider.notifier)
.update(
aiRequestModel:
aiReqM.updatePayload(payload),
);
updateRequestModel(el);
setState(() {});
},
),

View File

@@ -38,10 +38,13 @@ class AIRequestPromptSection extends ConsumerWidget {
fieldKey: "$selectedId-aireq-sysprompt-body",
initialValue: systemPrompt,
onChanged: (String value) {
payload.systemPrompt = value;
final aim = ref
.read(collectionStateNotifierProvider)![selectedId]!
.aiRequestModel!;
aim.payload.systemPrompt = value;
ref
.read(collectionStateNotifierProvider.notifier)
.update(aiRequestModel: aiReqM.updatePayload(payload));
.update(aiRequestModel: aim.updatePayload(aim.payload));
},
hintText: 'Enter System Prompt',
),
@@ -64,10 +67,13 @@ class AIRequestPromptSection extends ConsumerWidget {
fieldKey: "$selectedId-aireq-userprompt-body",
initialValue: userPrompt,
onChanged: (String value) {
payload.userPrompt = value;
final aim = ref
.read(collectionStateNotifierProvider)![selectedId]!
.aiRequestModel!;
aim.payload.userPrompt = value;
ref
.read(collectionStateNotifierProvider.notifier)
.update(aiRequestModel: aiReqM.updatePayload(payload));
.update(aiRequestModel: aim.updatePayload(aim.payload));
},
hintText: 'Enter User Prompt',
),

View File

@@ -159,13 +159,13 @@ class AIProviderSelector extends ConsumerWidget {
Widget build(BuildContext context, WidgetRef ref) {
final selectedId = ref.watch(selectedIdStateProvider);
final req = ref.watch(collectionStateNotifierProvider)![selectedId]!;
final aiRequestModel = req.aiRequestModel;
final defaultLLMSO = aiRequestModel == null
? ref
.read(settingsProvider.notifier)
.settingsModel
?.defaultLLMSaveObject
: LLMSaveObject(
AIRequestModel? aiRequestModel = req.aiRequestModel;
if (aiRequestModel == null) {
return Container();
}
LLMSaveObject defaultLLMSO = LLMSaveObject(
endpoint: aiRequestModel.payload.endpoint,
credential: aiRequestModel.payload.credential,
configMap: aiRequestModel.payload.configMap,
@@ -184,8 +184,8 @@ class AIProviderSelector extends ConsumerWidget {
payload: LLMInputPayload(
endpoint: llmso.endpoint,
credential: llmso.credential,
systemPrompt: aiRequestModel?.payload.systemPrompt ?? '',
userPrompt: aiRequestModel?.payload.userPrompt ?? '',
systemPrompt: aiRequestModel.payload.systemPrompt,
userPrompt: aiRequestModel.payload.userPrompt,
configMap: llmso.configMap,
),
),