diff --git a/lib/widgets/ai_toolgen_widgets.dart b/lib/widgets/ai_toolgen_widgets.dart index f4387b40..9da8df7d 100644 --- a/lib/widgets/ai_toolgen_widgets.dart +++ b/lib/widgets/ai_toolgen_widgets.dart @@ -80,60 +80,75 @@ class _GenerateToolDialogState extends ConsumerState { String? generatedToolCode = ''; generateAPITool() async { - setState(() { - generatedToolCode = null; - }); - final toolfuncRes = await APIDashAgentCaller.instance.call( - APIToolFunctionGenerator(), - ref: ref, - input: AgentInputs(variables: { - 'REQDATA': widget.requestDesc.generateREQDATA, - 'TARGET_LANGUAGE': selectedLanguage, - }), - ); - if (toolfuncRes == null) { + try { setState(() { - generatedToolCode = ''; + generatedToolCode = null; }); + final toolfuncRes = await APIDashAgentCaller.instance.call( + APIToolFunctionGenerator(), + ref: ref, + input: AgentInputs(variables: { + 'REQDATA': widget.requestDesc.generateREQDATA, + 'TARGET_LANGUAGE': selectedLanguage, + }), + ); + if (toolfuncRes == null) { + setState(() { + generatedToolCode = ''; + }); + ScaffoldMessenger.of(context).showSnackBar(SnackBar( + content: Text( + "API Tool generation failed!", + style: TextStyle(color: Colors.white), + ), + backgroundColor: Colors.redAccent, + )); + return; + } + + String toolCode = toolfuncRes!['FUNC']; + + final toolres = await APIDashAgentCaller.instance.call( + ApiToolBodyGen(), + ref: ref, + input: AgentInputs(variables: { + 'TEMPLATE': APIToolGenTemplateSelector.getTemplate( + selectedLanguage, selectedAgent) + .substitutePromptVariable('FUNC', toolCode), + }), + ); + if (toolres == null) { + setState(() { + generatedToolCode = ''; + }); + ScaffoldMessenger.of(context).showSnackBar(SnackBar( + content: Text( + "API Tool generation failed!", + style: TextStyle(color: Colors.white), + ), + backgroundColor: Colors.redAccent, + )); + return; + } + String toolDefinition = toolres!['TOOL']; + + setState(() { + generatedToolCode = toolDefinition; + }); + } catch (e) { + String errMsg = 'Unexpected Error Occured'; + if (e.toString().contains('NO_DEFAULT_LLM')) { + errMsg = "Please Select Default AI Model in Settings"; + } ScaffoldMessenger.of(context).showSnackBar(SnackBar( content: Text( - "API Tool generation failed!", + errMsg, style: TextStyle(color: Colors.white), ), backgroundColor: Colors.redAccent, )); - return; + Navigator.pop(context); } - - String toolCode = toolfuncRes!['FUNC']; - - final toolres = await APIDashAgentCaller.instance.call( - ApiToolBodyGen(), - ref: ref, - input: AgentInputs(variables: { - 'TEMPLATE': APIToolGenTemplateSelector.getTemplate( - selectedLanguage, selectedAgent) - .substitutePromptVariable('FUNC', toolCode), - }), - ); - if (toolres == null) { - setState(() { - generatedToolCode = ''; - }); - ScaffoldMessenger.of(context).showSnackBar(SnackBar( - content: Text( - "API Tool generation failed!", - style: TextStyle(color: Colors.white), - ), - backgroundColor: Colors.redAccent, - )); - return; - } - String toolDefinition = toolres!['TOOL']; - - setState(() { - generatedToolCode = toolDefinition; - }); } @override diff --git a/lib/widgets/ai_ui_desginer_widgets.dart b/lib/widgets/ai_ui_desginer_widgets.dart index d19f2f1c..06a0ff55 100644 --- a/lib/widgets/ai_ui_desginer_widgets.dart +++ b/lib/widgets/ai_ui_desginer_widgets.dart @@ -42,70 +42,85 @@ class _GenerateUIDialogState extends ConsumerState { String generatedSDUI = '{}'; Future generateSDUICode(String apiResponse) async { - setState(() { - index = 1; //Induce Loading - }); - //STEP 1: RESPONSE_ANALYSER (call the Semantic Analysis & IRGen Bots in parallel) - final step1Res = await Future.wait([ - APIDashAgentCaller.instance.call( - ResponseSemanticAnalyser(), - ref: ref, - input: AgentInputs(query: apiResponse), - ), - APIDashAgentCaller.instance.call( - IntermediateRepresentationGen(), + try { + setState(() { + index = 1; //Induce Loading + }); + //STEP 1: RESPONSE_ANALYSER (call the Semantic Analysis & IRGen Bots in parallel) + final step1Res = await Future.wait([ + APIDashAgentCaller.instance.call( + ResponseSemanticAnalyser(), + ref: ref, + input: AgentInputs(query: apiResponse), + ), + APIDashAgentCaller.instance.call( + IntermediateRepresentationGen(), + ref: ref, + input: AgentInputs(variables: { + 'VAR_API_RESPONSE': apiResponse, + }), + ), + ]); + final SA = step1Res[0]?['SEMANTIC_ANALYSIS']; + final IR = step1Res[1]?['INTERMEDIATE_REPRESENTATION']; + + if (SA == null || IR == null) { + setState(() { + index = 0; + }); + ScaffoldMessenger.of(context).showSnackBar(SnackBar( + content: Text( + "Preview Generation Failed!", + style: TextStyle(color: Colors.white), + ), + backgroundColor: Colors.redAccent, + )); + return null; + } + + print("Semantic Analysis: $SA"); + print("Intermediate Representation: $IR"); + + //STEP 2: STAC_GEN (Generate the SDUI Code) + + final sduiCode = await APIDashAgentCaller.instance.call( + StacGenBot(), ref: ref, input: AgentInputs(variables: { - 'VAR_API_RESPONSE': apiResponse, + 'VAR_RAW_API_RESPONSE': apiResponse, + 'VAR_INTERMEDIATE_REPR': IR, + 'VAR_SEMANTIC_ANALYSIS': SA, }), - ), - ]); - final SA = step1Res[0]?['SEMANTIC_ANALYSIS']; - final IR = step1Res[1]?['INTERMEDIATE_REPRESENTATION']; - - if (SA == null || IR == null) { - setState(() { - index = 0; - }); + ); + final stacCode = sduiCode?['STAC']?.toString(); + if (stacCode == null) { + ScaffoldMessenger.of(context).showSnackBar(SnackBar( + content: Text( + "Preview Generation Failed!", + style: TextStyle(color: Colors.white), + ), + backgroundColor: Colors.redAccent, + )); + setState(() { + index = 0; + }); + return null; + } + return sduiCode['STAC'].toString(); + } catch (e) { + String errMsg = 'Unexpected Error Occured'; + if (e.toString().contains('NO_DEFAULT_LLM')) { + errMsg = "Please Select Default AI Model in Settings"; + } ScaffoldMessenger.of(context).showSnackBar(SnackBar( content: Text( - "Preview Generation Failed!", + errMsg, style: TextStyle(color: Colors.white), ), backgroundColor: Colors.redAccent, )); - return null; + Navigator.pop(context); } - - print("Semantic Analysis: $SA"); - print("Intermediate Representation: $IR"); - - //STEP 2: STAC_GEN (Generate the SDUI Code) - - final sduiCode = await APIDashAgentCaller.instance.call( - StacGenBot(), - ref: ref, - input: AgentInputs(variables: { - 'VAR_RAW_API_RESPONSE': apiResponse, - 'VAR_INTERMEDIATE_REPR': IR, - 'VAR_SEMANTIC_ANALYSIS': SA, - }), - ); - final stacCode = sduiCode?['STAC']?.toString(); - if (stacCode == null) { - ScaffoldMessenger.of(context).showSnackBar(SnackBar( - content: Text( - "Preview Generation Failed!", - style: TextStyle(color: Colors.white), - ), - backgroundColor: Colors.redAccent, - )); - setState(() { - index = 0; - }); - return null; - } - return sduiCode['STAC'].toString(); } Future modifySDUICode(String modificationRequest) async {