| 1 |
jQuery(document).ready(function($) { |
| 2 |
// Function to fetch models from OpenRouter API |
| 3 |
function fetchOpenRouterModels() { |
| 4 |
var modelSelect = $('#qcld_openrouter_model'); |
| 5 |
modelSelect.html('<option value="">Loading models...</option>'); |
| 6 |
|
| 7 |
$.ajax({ |
| 8 |
url: 'https://openrouter.ai/api/v1/models', |
| 9 |
method: 'GET', |
| 10 |
headers: { |
| 11 |
'Authorization': 'Bearer ' + $('#qcld_openrouter_api_key').val() |
| 12 |
}, |
| 13 |
success: function(response) { |
| 14 |
modelSelect.empty(); |
| 15 |
|
| 16 |
if (response.data && response.data.length > 0) { |
| 17 |
response.data.forEach(function(model) { |
| 18 |
var option = $('<option></option>') |
| 19 |
.val(model.id) |
| 20 |
.text(model.name + ' (' + model.id + ')'); |
| 21 |
|
| 22 |
// Set selected if this is the current model |
| 23 |
if (model.id === modelSelect.data('current-model')) { |
| 24 |
option.prop('selected', true); |
| 25 |
} |
| 26 |
|
| 27 |
modelSelect.append(option); |
| 28 |
}); |
| 29 |
} else { |
| 30 |
modelSelect.append($('<option></option>') |
| 31 |
.val('') |
| 32 |
.text('No models available')); |
| 33 |
} |
| 34 |
}, |
| 35 |
error: function(xhr) { |
| 36 |
modelSelect.empty(); |
| 37 |
modelSelect.append($('<option></option>') |
| 38 |
.val('') |
| 39 |
.text('Error loading models')); |
| 40 |
|
| 41 |
if (xhr.status === 401) { |
| 42 |
alert('Invalid API key. Please check your OpenRouter API key.'); |
| 43 |
} |
| 44 |
} |
| 45 |
}); |
| 46 |
} |
| 47 |
|
| 48 |
// Fetch models when page loads |
| 49 |
fetchOpenRouterModels(); |
| 50 |
|
| 51 |
// Refresh models when API key changes |
| 52 |
$('#qcld_openrouter_api_key').on('change', function() { |
| 53 |
fetchOpenRouterModels(); |
| 54 |
}); |
| 55 |
|
| 56 |
var settingsOpenrouter = document.getElementById("qcld_save_openrouter_setting"); |
| 57 |
if(settingsOpenrouter){ |
| 58 |
$('.qcl-openai').on('click', '#qcld_save_openrouter_setting', function(){ |
| 59 |
if ($('#qcld_openrouter_enabled').is(":checked")){ |
| 60 |
var openrouter_enabled = 1; |
| 61 |
}else{ |
| 62 |
var openrouter_enabled = 0; |
| 63 |
} |
| 64 |
var qcld_openrouter_page_suggestion_enabled = jQuery("#qcld_openrouter_page_suggestion_enabled").is(":checked") ? 1 : 0; |
| 65 |
var opnrouter_is_context_awareness_enabled = jQuery("#opnrouter_is_context_awareness_enabled").is(":checked") ? 1 : 0; |
| 66 |
var qcld_openrouter_api_key = jQuery("#qcld_openrouter_api_key").val(); |
| 67 |
var qcld_openrouter_model = jQuery('#qcld_openrouter_model').val(); |
| 68 |
var qcld_openrouter_append_content = jQuery('#qcld_openrouter_append_content').val(); |
| 69 |
var qcld_openrouter_prepend_content = jQuery('#qcld_openrouter_prepend_content').val(); |
| 70 |
var is_page_rag_enabled = jQuery("#is_page_rag_enabled_openrouter").is(":checked") ? 1 : 0; |
| 71 |
var post_openrouter_types = $.map($('input[name="site_openrouter_search_posttypes[]"]:checked'), function(c){return c.value; }); |
| 72 |
Swal.fire({ |
| 73 |
html: '<div class="qcld-save-settings-spinner"></div>', |
| 74 |
allowOutsideClick: false, |
| 75 |
allowEscapeKey: false, |
| 76 |
showConfirmButton: false, |
| 77 |
background: 'transparent', |
| 78 |
customClass: { |
| 79 |
popup: 'qcld-swal-loading-only' |
| 80 |
} |
| 81 |
}); |
| 82 |
$.ajax({ |
| 83 |
url: qcld_gemini_admin_data.ajax_url, |
| 84 |
type:'POST', |
| 85 |
data: { |
| 86 |
action: 'qcld_openrouter_settings_option', |
| 87 |
nonce: qcld_gemini_admin_data.ajax_nonce, |
| 88 |
openrouter_api_key: qcld_openrouter_api_key, |
| 89 |
openrouter_model: qcld_openrouter_model, |
| 90 |
openrouter_enabled: openrouter_enabled, |
| 91 |
qcld_openrouter_page_suggestion_enabled: qcld_openrouter_page_suggestion_enabled, |
| 92 |
opnrouter_is_context_awareness_enabled: opnrouter_is_context_awareness_enabled, |
| 93 |
qcld_openrouter_append_content: qcld_openrouter_append_content, |
| 94 |
qcld_openrouter_prepend_content: qcld_openrouter_prepend_content, |
| 95 |
openai_post_type:post_openrouter_types, |
| 96 |
is_page_rag_enabled: is_page_rag_enabled |
| 97 |
}, |
| 98 |
success: function(data){ |
| 99 |
$('#result').html(data); |
| 100 |
var color = data.status === 'success' ? 'color:green;' : 'color:red;'; |
| 101 |
Swal.fire({ |
| 102 |
title: 'Your settings are saved.', |
| 103 |
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></b><p><span style="'+ color +'">'+ (data.msg ? data.msg : '') +'</span></p>', |
| 104 |
width: 450, |
| 105 |
icon: 'success', |
| 106 |
confirmButtonText: 'Got it', |
| 107 |
confirmButtonWidth: 100, |
| 108 |
confirmButtonClass: 'btn btn-lg', |
| 109 |
}).then((result) => { |
| 110 |
if (result.isConfirmed) { |
| 111 |
$.ajax({ |
| 112 |
url: qcld_gemini_admin_data.ajax_url, |
| 113 |
type: 'POST', |
| 114 |
data: { |
| 115 |
action: 'update_settings_option', |
| 116 |
nonce: qcld_gemini_admin_data.ajax_nonce, |
| 117 |
disable_ss: 1 |
| 118 |
}, |
| 119 |
|
| 120 |
}); |
| 121 |
} |
| 122 |
}); |
| 123 |
} |
| 124 |
}); |
| 125 |
}); |
| 126 |
} |
| 127 |
}); |