@@ -719,7 +719,8 @@ <h6>AI Assistant <i class="fas fa-robot"></i></h6>
719719}
720720
721721// Save script changes
722- function saveScript ( ) {
722+ // Optional callback parameter receives the saved script ID on success
723+ function saveScript ( callback ) {
723724 var id = $ ( '#editor-suggestion-id' ) . val ( ) ;
724725 var script = monacoEditor . getValue ( ) ;
725726 var description = $ ( '#script-description' ) . val ( ) ;
@@ -729,6 +730,7 @@ <h6>AI Assistant <i class="fas fa-robot"></i></h6>
729730 // Create new automation script
730731 if ( ! description || description . trim ( ) === '' ) {
731732 alert ( 'Please enter a description for the script' ) ;
733+ if ( callback ) callback ( null ) ;
732734 return ;
733735 }
734736 $ . ajax ( {
@@ -745,12 +747,22 @@ <h6>AI Assistant <i class="fas fa-robot"></i></h6>
745747 scriptType : scriptType
746748 } ) ,
747749 success : function ( response ) {
748- alert ( 'Automation script created successfully!' ) ;
749- $ ( '#editorModal' ) . modal ( 'hide' ) ;
750- $ ( '#suggestions-table' ) . DataTable ( ) . ajax . reload ( ) ;
750+ // Update the ID and mark as no longer new
751+ $ ( '#editor-suggestion-id' ) . val ( response . id ) ;
752+ isNewScript = false ;
753+
754+ // If callback provided, call it with the new ID (for generateWithAI flow)
755+ if ( callback ) {
756+ callback ( response . id ) ;
757+ } else {
758+ alert ( 'Automation script created successfully!' ) ;
759+ $ ( '#editorModal' ) . modal ( 'hide' ) ;
760+ $ ( '#suggestions-table' ) . DataTable ( ) . ajax . reload ( ) ;
761+ }
751762 } ,
752763 error : function ( xhr ) {
753764 alert ( 'Error: ' + ( xhr . responseJSON ? xhr . responseJSON . message : 'Failed to create script' ) ) ;
765+ if ( callback ) callback ( null ) ;
754766 }
755767 } ) ;
756768 } else {
@@ -765,11 +777,16 @@ <h6>AI Assistant <i class="fas fa-robot"></i></h6>
765777 } ,
766778 data : JSON . stringify ( { script : script } ) ,
767779 success : function ( response ) {
768- alert ( 'Script saved successfully!' ) ;
769- $ ( '#suggestions-table' ) . DataTable ( ) . ajax . reload ( ) ;
780+ if ( callback ) {
781+ callback ( id ) ;
782+ } else {
783+ alert ( 'Script saved successfully!' ) ;
784+ $ ( '#suggestions-table' ) . DataTable ( ) . ajax . reload ( ) ;
785+ }
770786 } ,
771787 error : function ( xhr ) {
772788 alert ( 'Error: ' + ( xhr . responseJSON ? xhr . responseJSON . message : 'Failed to save script' ) ) ;
789+ if ( callback ) callback ( null ) ;
773790 }
774791 } ) ;
775792 }
@@ -942,15 +959,37 @@ <h6>AI Assistant <i class="fas fa-robot"></i></h6>
942959
943960// Generate code with AI
944961function generateWithAI ( ) {
945- console . log ( "ahahahha" ) ;
946962 var id = $ ( '#editor-suggestion-id' ) . val ( ) ;
947963 var prompt = window . prompt ( 'What would you like the AI to generate or improve?' ,
948964 'Generate a complete automation script based on the description' ) ;
949965
950966 if ( ! prompt ) {
951- console . log ( "leaving...." ) ;
952967 return ;
953968 }
969+
970+ // If this is a new script without an ID, auto-save it first
971+ if ( isNewScript || ! id ) {
972+ var description = $ ( '#script-description' ) . val ( ) ;
973+ if ( ! description || description . trim ( ) === '' ) {
974+ alert ( 'Please enter a description for the script before generating with AI' ) ;
975+ return ;
976+ }
977+
978+ // Auto-save the script, then call generate with the new ID
979+ // Note: saveScript already shows error alerts on failure
980+ saveScript ( function ( savedId ) {
981+ if ( savedId ) {
982+ callGenerateAPI ( savedId , prompt ) ;
983+ }
984+ // If savedId is null, the save failed and saveScript already showed an error alert
985+ } ) ;
986+ } else {
987+ callGenerateAPI ( id , prompt ) ;
988+ }
989+ }
990+
991+ // Helper function to call the generate API
992+ function callGenerateAPI ( id , prompt ) {
954993 const csrfToken = document . getElementById ( "csrf-token" ) . value ;
955994 $ . ajax ( {
956995 url : '/api/v1/automation/suggestions/' + id + '/generate' ,
0 commit comments