| 1 |
jQuery(document).ready(function($) { |
| 2 |
// Function to fetch models from OpenRouter API |
| 3 |
var settingsOpenrouter = document.getElementById("qcld_save_grok_setting"); |
| 4 |
if(settingsOpenrouter){ |
| 5 |
$('.qcl-openai').on('click', '#qcld_save_grok_setting', function(event){ |
| 6 |
event.preventDefault(); |
| 7 |
if ($('#qcld_grok_enabled').is(":checked")){ |
| 8 |
var grok_enabled = 1; |
| 9 |
}else{ |
| 10 |
var grok_enabled = 0; |
| 11 |
} |
| 12 |
if ($('#qcld_grok_rag_enabled').is(":checked")){ |
| 13 |
var grok_rag_enabled = 1; |
| 14 |
}else{ |
| 15 |
var grok_rag_enabled = 0; |
| 16 |
} |
| 17 |
var qcld_grok_page_suggestion_enabled = jQuery("#qcld_grok_page_suggestion_enabled").is(":checked") ? 1 : 0; |
| 18 |
var qcld_grok_api_key = jQuery("#qcld_grok_api_key").val(); |
| 19 |
var qcld_grok_model = jQuery('#qcld_grok_model').val(); |
| 20 |
var post_grok_types = $.map($('input[name="site_grok_search_posttypes[]"]:checked'), function(c){return c.value; }); |
| 21 |
Swal.fire({ |
| 22 |
html: '<div class="qcld-save-settings-spinner"></div>', |
| 23 |
allowOutsideClick: false, |
| 24 |
allowEscapeKey: false, |
| 25 |
showConfirmButton: false, |
| 26 |
background: 'transparent', |
| 27 |
customClass: { |
| 28 |
popup: 'qcld-swal-loading-only' |
| 29 |
} |
| 30 |
}); |
| 31 |
$.ajax({ |
| 32 |
url: ajax_object.ajax_url, |
| 33 |
type:'POST', |
| 34 |
data: { |
| 35 |
action: 'qcld_grok_settings_option', |
| 36 |
nonce: ajax_object.ajax_nonce, |
| 37 |
grok_api_key: qcld_grok_api_key, |
| 38 |
grok_management_api_key: jQuery("#qcld_grok_management_api_key").val(), |
| 39 |
grok_model: qcld_grok_model, |
| 40 |
grok_enabled: grok_enabled, |
| 41 |
grok_collection_id: jQuery("#qcld_grok_collection_id").val(), |
| 42 |
grok_stream_enabled: jQuery("#qcld_grok_stream_enabled").is(":checked") ? 1 : 0, |
| 43 |
grok_rag_enabled: grok_rag_enabled, |
| 44 |
qcld_grok_system_content: jQuery("#qcld_grok_system_content").val(), |
| 45 |
qcld_grok_page_suggestion_enabled: qcld_grok_page_suggestion_enabled, |
| 46 |
openai_post_type:post_grok_types |
| 47 |
}, |
| 48 |
success: function(data){ |
| 49 |
$('#result').html(data); |
| 50 |
Swal.fire({ |
| 51 |
title: 'Your settings are saved.', |
| 52 |
html: '<p style=font-size:14px>Please clear your browser <b>cache</b> and <b>cookies</b> both and reload the front end before testing. Alternatively, you can launch a new browser window in <b>Incognito</b>/Private mode (Ctrl+Shift+N in chrome) to test.</p>', |
| 53 |
width: 450, |
| 54 |
icon: 'success', |
| 55 |
confirmButtonText: 'Yes', |
| 56 |
confirmButtonWidth: 100, |
| 57 |
confirmButtonClass: 'btn btn-lg' |
| 58 |
}).then((result) => { |
| 59 |
if (result.isConfirmed) { |
| 60 |
$.ajax({ |
| 61 |
url: ajax_object.ajax_url, |
| 62 |
type: 'POST', |
| 63 |
data: { |
| 64 |
action: 'update_settings_option', |
| 65 |
nonce: ajax_object.ajax_nonce, |
| 66 |
disable_ss: 1 |
| 67 |
}, |
| 68 |
|
| 69 |
}); |
| 70 |
} |
| 71 |
}); |
| 72 |
} |
| 73 |
}); |
| 74 |
}); |
| 75 |
$('.qcl-openai').on('click', '#add_grok_collection', function(event) { |
| 76 |
event.preventDefault(); |
| 77 |
event.stopImmediatePropagation(); |
| 78 |
event.stopPropagation(); |
| 79 |
$.ajax({ |
| 80 |
url: ajax_object.ajax_url, |
| 81 |
type:'POST', |
| 82 |
data: { |
| 83 |
action: 'qcld_grok_add_collection', |
| 84 |
nonce: ajax_object.ajax_nonce, |
| 85 |
}, |
| 86 |
success: function(response){ |
| 87 |
alert('New collection added successfully.'); |
| 88 |
loadGrokCollections(); |
| 89 |
} |
| 90 |
}); |
| 91 |
}); |
| 92 |
$('.qcl-openai').on('click', '.remove_grok_collection', function( event ) { |
| 93 |
event.preventDefault(); |
| 94 |
event.stopImmediatePropagation(); |
| 95 |
event.stopPropagation(); |
| 96 |
var collection_id = $(this).data('id'); |
| 97 |
if (confirm('Are you sure you want to remove this collection?')) { |
| 98 |
$.ajax({ |
| 99 |
url: ajax_object.ajax_url, |
| 100 |
type:'POST', |
| 101 |
data: { |
| 102 |
action: 'qcld_grok_remove_collection', |
| 103 |
nonce: ajax_object.ajax_nonce, |
| 104 |
collection_id: collection_id |
| 105 |
}, |
| 106 |
success: function( response ){ |
| 107 |
console.log( JSON.parse( response ).response ); |
| 108 |
loadGrokCollections(); |
| 109 |
} |
| 110 |
}); |
| 111 |
} |
| 112 |
}); |
| 113 |
$.ajax({ |
| 114 |
url: ajax_object.ajax_url, |
| 115 |
type:'POST', |
| 116 |
data: { |
| 117 |
action: 'qcld_grok_collectionlist', |
| 118 |
nonce: ajax_object.ajax_nonce, |
| 119 |
}, |
| 120 |
success: function(response){ |
| 121 |
var data = JSON.parse(response).collections; |
| 122 |
var text = ""; |
| 123 |
data.forEach(function (item) { |
| 124 |
text += '<tr><td>' + item.collection_name + '</td><td>' + item.collection_id + '<form class="file_form_gpt" data-collection="' + item.collection_id + '"><input type="file" class="inputfile" id="grokfileinput_gpt" /><label for="grokfileinput_gpt" class="huge ui grey button"><i class="fa fa-upload"></i>Upload CSV/PDF/JSON</label></form></td><td><a class="btn-danger btn floated remove_grok_collection" data-id="' + item.collection_id + '">Remove</a></td></tr>'; |
| 125 |
}); |
| 126 |
document.getElementById("grokFileList").innerHTML = text; |
| 127 |
} |
| 128 |
}); |
| 129 |
jQuery('.table-responsive').on('change','#grokfileinput_gpt', function() { |
| 130 |
var file_data = $(this).prop('files')[0]; |
| 131 |
if (!file_data) return; // Exit if no file was selected |
| 132 |
|
| 133 |
var form_data = new FormData(); |
| 134 |
form_data.append('file', file_data); |
| 135 |
form_data.append('name', file_data.name); |
| 136 |
form_data.append('collection_id', $(this).parent().attr('data-collection')); |
| 137 |
form_data.append('action', 'qcld_grok_file_upload'); |
| 138 |
form_data.append('purpose', 'assistant'); |
| 139 |
form_data.append('nonce', ajax_object.ajax_nonce); |
| 140 |
$.ajax({ |
| 141 |
url: ajax_object.ajax_url, |
| 142 |
type: 'POST', |
| 143 |
data: form_data, |
| 144 |
processData: false, // Critical |
| 145 |
contentType: false, // Critical |
| 146 |
success: function(response) { |
| 147 |
console.log('Upload successful', response); |
| 148 |
} |
| 149 |
}); |
| 150 |
}) |
| 151 |
function loadGrokCollections() { |
| 152 |
$.ajax({ |
| 153 |
url: ajax_object.ajax_url, |
| 154 |
type:'POST', |
| 155 |
data: { |
| 156 |
action: 'qcld_grok_collectionlist', |
| 157 |
nonce: ajax_object.ajax_nonce, |
| 158 |
}, |
| 159 |
success: function(response){ |
| 160 |
var data = JSON.parse(response).collections; |
| 161 |
var text = ""; |
| 162 |
data.forEach(function (item) { |
| 163 |
text += '<tr><td>' + item.collection_name + '</td><td>' + item.collection_id + '<form class="file_grok_form" data-collection="'+ item.collection_id + '"><input type="file" (change)="fileEvent($event)" class="inputfile" id="grokfileinput" style="display:none"/></form><form class="file_form_gpt" data-collection="' + item.collection_id + '"><input type="file" (change)="fileEvent($event)" class="inputfile" id="grokfileinput_gpt" style="display:none"/><label for="grokfileinput_gpt" class="huge ui grey button"><i class="fa fa-upload"></i>Upload CSV/PDF/JSON</label></form></td><td><a class="btn-danger btn floated remove_grok_collection" data-id="' + item.collection_id + '">Remove</a></td></tr>'; |
| 164 |
}); |
| 165 |
document.getElementById("grokFileList").innerHTML = text; |
| 166 |
} |
| 167 |
}); |
| 168 |
} |
| 169 |
} |
| 170 |
}); |