mirror of
https://github.com/foss42/apidash.git
synced 2025-12-02 02:39:19 +08:00
initializeAIRequest on AIRequest Creation & ControllerUpdate issues fixed
This commit is contained in:
@@ -1,7 +1,9 @@
|
|||||||
|
import 'package:apidash_core/apidash_core.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||||
import 'package:apidash/providers/providers.dart';
|
import 'package:apidash/providers/providers.dart';
|
||||||
import 'package:apidash/widgets/widgets.dart';
|
import 'package:apidash/widgets/widgets.dart';
|
||||||
|
import 'package:genai/genai.dart';
|
||||||
|
|
||||||
class APITypeDropdown extends ConsumerWidget {
|
class APITypeDropdown extends ConsumerWidget {
|
||||||
const APITypeDropdown({super.key});
|
const APITypeDropdown({super.key});
|
||||||
@@ -14,6 +16,9 @@ class APITypeDropdown extends ConsumerWidget {
|
|||||||
return APITypePopupMenu(
|
return APITypePopupMenu(
|
||||||
apiType: apiType,
|
apiType: apiType,
|
||||||
onChanged: (type) {
|
onChanged: (type) {
|
||||||
|
if (type == APIType.ai) {
|
||||||
|
initializeAIRequest(ref);
|
||||||
|
}
|
||||||
ref
|
ref
|
||||||
.read(collectionStateNotifierProvider.notifier)
|
.read(collectionStateNotifierProvider.notifier)
|
||||||
.update(apiType: type);
|
.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,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -27,10 +27,13 @@ class AIRequestAuthorizationSection extends ConsumerWidget {
|
|||||||
fieldKey: "$selectedId-aireq-authvalue-body",
|
fieldKey: "$selectedId-aireq-authvalue-body",
|
||||||
initialValue: cred,
|
initialValue: cred,
|
||||||
onChanged: (String value) {
|
onChanged: (String value) {
|
||||||
payload.credential = value;
|
final aim = ref
|
||||||
|
.read(collectionStateNotifierProvider)![selectedId]!
|
||||||
|
.aiRequestModel!;
|
||||||
|
aim.payload.credential = value;
|
||||||
ref
|
ref
|
||||||
.read(collectionStateNotifierProvider.notifier)
|
.read(collectionStateNotifierProvider.notifier)
|
||||||
.update(aiRequestModel: aiReqM.updatePayload(payload));
|
.update(aiRequestModel: aim.updatePayload(aim.payload));
|
||||||
},
|
},
|
||||||
hintText: 'Enter API key or Authorization Credentials',
|
hintText: 'Enter API key or Authorization Credentials',
|
||||||
),
|
),
|
||||||
|
|||||||
@@ -22,6 +22,16 @@ class _AIRequestConfigSectionState
|
|||||||
final aiReqM = reqM.aiRequestModel!;
|
final aiReqM = reqM.aiRequestModel!;
|
||||||
final payload = aiReqM.payload;
|
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(
|
return SingleChildScrollView(
|
||||||
padding: EdgeInsets.symmetric(vertical: 20),
|
padding: EdgeInsets.symmetric(vertical: 20),
|
||||||
child: Column(
|
child: Column(
|
||||||
@@ -43,12 +53,7 @@ class _AIRequestConfigSectionState
|
|||||||
value: el.configValue.value as bool,
|
value: el.configValue.value as bool,
|
||||||
onChanged: (x) {
|
onChanged: (x) {
|
||||||
el.configValue.value = x;
|
el.configValue.value = x;
|
||||||
payload.configMap[el.configId] = el;
|
updateRequestModel(el);
|
||||||
ref
|
|
||||||
.read(collectionStateNotifierProvider.notifier)
|
|
||||||
.update(
|
|
||||||
aiRequestModel: aiReqM.updatePayload(payload),
|
|
||||||
);
|
|
||||||
setState(() {});
|
setState(() {});
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
@@ -60,14 +65,7 @@ class _AIRequestConfigSectionState
|
|||||||
if (x.isEmpty) x = '0';
|
if (x.isEmpty) x = '0';
|
||||||
if (num.tryParse(x) == null) return;
|
if (num.tryParse(x) == null) return;
|
||||||
el.configValue.value = num.parse(x);
|
el.configValue.value = num.parse(x);
|
||||||
|
updateRequestModel(el);
|
||||||
payload.configMap[el.configId] = el;
|
|
||||||
ref
|
|
||||||
.read(collectionStateNotifierProvider.notifier)
|
|
||||||
.update(
|
|
||||||
aiRequestModel: aiReqM.updatePayload(payload),
|
|
||||||
);
|
|
||||||
|
|
||||||
setState(() {});
|
setState(() {});
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
@@ -77,14 +75,7 @@ class _AIRequestConfigSectionState
|
|||||||
initialValue: el.configValue.value.toString(),
|
initialValue: el.configValue.value.toString(),
|
||||||
onChanged: (x) {
|
onChanged: (x) {
|
||||||
el.configValue.value = x;
|
el.configValue.value = x;
|
||||||
|
updateRequestModel(el);
|
||||||
payload.configMap[el.configId] = el;
|
|
||||||
ref
|
|
||||||
.read(collectionStateNotifierProvider.notifier)
|
|
||||||
.update(
|
|
||||||
aiRequestModel: aiReqM.updatePayload(payload),
|
|
||||||
);
|
|
||||||
|
|
||||||
setState(() {});
|
setState(() {});
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
@@ -119,16 +110,7 @@ class _AIRequestConfigSectionState
|
|||||||
double
|
double
|
||||||
);
|
);
|
||||||
el.configValue.value = (z.$1, x, z.$3);
|
el.configValue.value = (z.$1, x, z.$3);
|
||||||
|
updateRequestModel(el);
|
||||||
payload.configMap[el.configId] = el;
|
|
||||||
ref
|
|
||||||
.read(
|
|
||||||
collectionStateNotifierProvider.notifier)
|
|
||||||
.update(
|
|
||||||
aiRequestModel:
|
|
||||||
aiReqM.updatePayload(payload),
|
|
||||||
);
|
|
||||||
|
|
||||||
setState(() {});
|
setState(() {});
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
|
|||||||
@@ -38,10 +38,13 @@ class AIRequestPromptSection extends ConsumerWidget {
|
|||||||
fieldKey: "$selectedId-aireq-sysprompt-body",
|
fieldKey: "$selectedId-aireq-sysprompt-body",
|
||||||
initialValue: systemPrompt,
|
initialValue: systemPrompt,
|
||||||
onChanged: (String value) {
|
onChanged: (String value) {
|
||||||
payload.systemPrompt = value;
|
final aim = ref
|
||||||
|
.read(collectionStateNotifierProvider)![selectedId]!
|
||||||
|
.aiRequestModel!;
|
||||||
|
aim.payload.systemPrompt = value;
|
||||||
ref
|
ref
|
||||||
.read(collectionStateNotifierProvider.notifier)
|
.read(collectionStateNotifierProvider.notifier)
|
||||||
.update(aiRequestModel: aiReqM.updatePayload(payload));
|
.update(aiRequestModel: aim.updatePayload(aim.payload));
|
||||||
},
|
},
|
||||||
hintText: 'Enter System Prompt',
|
hintText: 'Enter System Prompt',
|
||||||
),
|
),
|
||||||
@@ -64,10 +67,13 @@ class AIRequestPromptSection extends ConsumerWidget {
|
|||||||
fieldKey: "$selectedId-aireq-userprompt-body",
|
fieldKey: "$selectedId-aireq-userprompt-body",
|
||||||
initialValue: userPrompt,
|
initialValue: userPrompt,
|
||||||
onChanged: (String value) {
|
onChanged: (String value) {
|
||||||
payload.userPrompt = value;
|
final aim = ref
|
||||||
|
.read(collectionStateNotifierProvider)![selectedId]!
|
||||||
|
.aiRequestModel!;
|
||||||
|
aim.payload.userPrompt = value;
|
||||||
ref
|
ref
|
||||||
.read(collectionStateNotifierProvider.notifier)
|
.read(collectionStateNotifierProvider.notifier)
|
||||||
.update(aiRequestModel: aiReqM.updatePayload(payload));
|
.update(aiRequestModel: aim.updatePayload(aim.payload));
|
||||||
},
|
},
|
||||||
hintText: 'Enter User Prompt',
|
hintText: 'Enter User Prompt',
|
||||||
),
|
),
|
||||||
|
|||||||
@@ -159,19 +159,19 @@ class AIProviderSelector extends ConsumerWidget {
|
|||||||
Widget build(BuildContext context, WidgetRef ref) {
|
Widget build(BuildContext context, WidgetRef ref) {
|
||||||
final selectedId = ref.watch(selectedIdStateProvider);
|
final selectedId = ref.watch(selectedIdStateProvider);
|
||||||
final req = ref.watch(collectionStateNotifierProvider)![selectedId]!;
|
final req = ref.watch(collectionStateNotifierProvider)![selectedId]!;
|
||||||
final aiRequestModel = req.aiRequestModel;
|
AIRequestModel? aiRequestModel = req.aiRequestModel;
|
||||||
final defaultLLMSO = aiRequestModel == null
|
|
||||||
? ref
|
if (aiRequestModel == null) {
|
||||||
.read(settingsProvider.notifier)
|
return Container();
|
||||||
.settingsModel
|
}
|
||||||
?.defaultLLMSaveObject
|
|
||||||
: LLMSaveObject(
|
LLMSaveObject defaultLLMSO = LLMSaveObject(
|
||||||
endpoint: aiRequestModel.payload.endpoint,
|
endpoint: aiRequestModel.payload.endpoint,
|
||||||
credential: aiRequestModel.payload.credential,
|
credential: aiRequestModel.payload.credential,
|
||||||
configMap: aiRequestModel.payload.configMap,
|
configMap: aiRequestModel.payload.configMap,
|
||||||
selectedLLM: aiRequestModel.model,
|
selectedLLM: aiRequestModel.model,
|
||||||
provider: aiRequestModel.provider,
|
provider: aiRequestModel.provider,
|
||||||
);
|
);
|
||||||
|
|
||||||
return DefaultLLMSelectorButton(
|
return DefaultLLMSelectorButton(
|
||||||
key: ValueKey(ref.watch(selectedIdStateProvider)),
|
key: ValueKey(ref.watch(selectedIdStateProvider)),
|
||||||
@@ -184,8 +184,8 @@ class AIProviderSelector extends ConsumerWidget {
|
|||||||
payload: LLMInputPayload(
|
payload: LLMInputPayload(
|
||||||
endpoint: llmso.endpoint,
|
endpoint: llmso.endpoint,
|
||||||
credential: llmso.credential,
|
credential: llmso.credential,
|
||||||
systemPrompt: aiRequestModel?.payload.systemPrompt ?? '',
|
systemPrompt: aiRequestModel.payload.systemPrompt,
|
||||||
userPrompt: aiRequestModel?.payload.userPrompt ?? '',
|
userPrompt: aiRequestModel.payload.userPrompt,
|
||||||
configMap: llmso.configMap,
|
configMap: llmso.configMap,
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
|||||||
Reference in New Issue
Block a user