SillyTavern/public/scripts/secrets.js

216 lines
6.9 KiB
JavaScript
Raw Normal View History

2023-12-02 19:04:51 +01:00
import { callPopup, getRequestHeaders } from '../script.js';
2023-07-20 19:32:15 +02:00
export const SECRET_KEYS = {
HORDE: 'api_key_horde',
2023-08-02 09:31:17 +02:00
MANCER: 'api_key_mancer',
APHRODITE: 'api_key_aphrodite',
TABBY: 'api_key_tabby',
2023-07-20 19:32:15 +02:00
OPENAI: 'api_key_openai',
NOVEL: 'api_key_novel',
CLAUDE: 'api_key_claude',
OPENROUTER: 'api_key_openrouter',
SCALE: 'api_key_scale',
AI21: 'api_key_ai21',
2023-08-20 12:55:37 +02:00
SCALE_COOKIE: 'scale_cookie',
2023-12-14 02:14:41 +01:00
MAKERSUITE: 'api_key_makersuite',
SERPAPI: 'api_key_serpapi',
2023-12-15 21:08:41 +01:00
MISTRALAI: 'api_key_mistralai',
2023-12-16 15:11:04 +01:00
TOGETHERAI: 'api_key_togetherai',
2024-01-24 13:59:27 +01:00
INFERMATICAI: 'api_key_infermaticai',
DREAMGEN: 'api_key_dreamgen',
2023-12-20 17:29:03 +01:00
CUSTOM: 'api_key_custom',
2024-02-07 18:28:34 +01:00
OOBA: 'api_key_ooba',
NOMICAI: 'api_key_nomicai',
2024-03-13 23:34:09 +01:00
KOBOLDCPP: 'api_key_koboldcpp',
2024-03-30 23:27:12 +01:00
LLAMACPP: 'api_key_llamacpp',
COHERE: 'api_key_cohere',
PERPLEXITY: 'api_key_perplexity',
2023-12-02 20:11:06 +01:00
};
2023-07-20 19:32:15 +02:00
const INPUT_MAP = {
[SECRET_KEYS.HORDE]: '#horde_api_key',
2023-08-02 09:31:17 +02:00
[SECRET_KEYS.MANCER]: '#api_key_mancer',
2023-07-20 19:32:15 +02:00
[SECRET_KEYS.OPENAI]: '#api_key_openai',
[SECRET_KEYS.NOVEL]: '#api_key_novel',
[SECRET_KEYS.CLAUDE]: '#api_key_claude',
[SECRET_KEYS.OPENROUTER]: '.api_key_openrouter',
2023-07-20 19:32:15 +02:00
[SECRET_KEYS.SCALE]: '#api_key_scale',
[SECRET_KEYS.AI21]: '#api_key_ai21',
2023-08-20 12:55:37 +02:00
[SECRET_KEYS.SCALE_COOKIE]: '#scale_cookie',
2023-12-14 02:14:41 +01:00
[SECRET_KEYS.MAKERSUITE]: '#api_key_makersuite',
[SECRET_KEYS.APHRODITE]: '#api_key_aphrodite',
2023-12-02 21:06:57 +01:00
[SECRET_KEYS.TABBY]: '#api_key_tabby',
2023-12-15 21:08:41 +01:00
[SECRET_KEYS.MISTRALAI]: '#api_key_mistralai',
2023-12-20 17:29:03 +01:00
[SECRET_KEYS.CUSTOM]: '#api_key_custom',
2023-12-16 15:11:04 +01:00
[SECRET_KEYS.TOGETHERAI]: '#api_key_togetherai',
2024-02-07 05:00:16 +01:00
[SECRET_KEYS.OOBA]: '#api_key_ooba',
2024-01-24 13:59:27 +01:00
[SECRET_KEYS.INFERMATICAI]: '#api_key_infermaticai',
[SECRET_KEYS.DREAMGEN]: '#api_key_dreamgen',
[SECRET_KEYS.NOMICAI]: '#api_key_nomicai',
2024-03-13 23:34:09 +01:00
[SECRET_KEYS.KOBOLDCPP]: '#api_key_koboldcpp',
2024-03-30 23:27:12 +01:00
[SECRET_KEYS.LLAMACPP]: '#api_key_llamacpp',
[SECRET_KEYS.COHERE]: '#api_key_cohere',
[SECRET_KEYS.PERPLEXITY]: '#api_key_perplexity',
2023-12-02 20:11:06 +01:00
};
2023-07-20 19:32:15 +02:00
async function clearSecret() {
const key = $(this).data('key');
await writeSecret(key, '');
secret_state[key] = false;
updateSecretDisplay();
$(INPUT_MAP[key]).val('').trigger('input');
2023-07-20 19:32:15 +02:00
$('#main_api').trigger('change');
}
function updateSecretDisplay() {
for (const [secret_key, input_selector] of Object.entries(INPUT_MAP)) {
const validSecret = !!secret_state[secret_key];
const placeholder = validSecret ? '✔️ Key saved' : '❌ Missing key';
$(input_selector).attr('placeholder', placeholder);
}
}
async function viewSecrets() {
const response = await fetch('/api/secrets/view', {
2023-07-20 19:32:15 +02:00
method: 'POST',
headers: getRequestHeaders(),
});
if (response.status == 403) {
2023-11-25 22:45:33 +01:00
callPopup('<h3>Forbidden</h3><p>To view your API keys here, set the value of allowKeysExposure to true in config.yaml file and restart the SillyTavern server.</p>', 'text');
2023-07-20 19:32:15 +02:00
return;
}
if (!response.ok) {
return;
}
$('#dialogue_popup').addClass('wide_dialogue_popup');
const data = await response.json();
const table = document.createElement('table');
table.classList.add('responsiveTable');
$(table).append('<thead><th>Key</th><th>Value</th></thead>');
for (const [key, value] of Object.entries(data)) {
$(table).append(`<tr><td>${DOMPurify.sanitize(key)}</td><td>${DOMPurify.sanitize(value)}</td></tr>`);
}
callPopup(table.outerHTML, 'text');
}
export let secret_state = {};
export async function writeSecret(key, value) {
try {
const response = await fetch('/api/secrets/write', {
2023-07-20 19:32:15 +02:00
method: 'POST',
headers: getRequestHeaders(),
body: JSON.stringify({ key, value }),
});
if (response.ok) {
const text = await response.text();
if (text == 'ok') {
secret_state[key] = true;
updateSecretDisplay();
}
}
} catch {
console.error('Could not write secret value: ', key);
}
}
export async function readSecretState() {
try {
const response = await fetch('/api/secrets/read', {
2023-07-20 19:32:15 +02:00
method: 'POST',
headers: getRequestHeaders(),
});
if (response.ok) {
secret_state = await response.json();
updateSecretDisplay();
await checkOpenRouterAuth();
}
} catch {
console.error('Could not read secrets file');
}
}
/**
* Finds a secret value by key.
* @param {string} key Secret key
* @returns {Promise<string | undefined>} Secret value, or undefined if keys are not exposed
*/
export async function findSecret(key) {
try {
const response = await fetch('/api/secrets/find', {
method: 'POST',
headers: getRequestHeaders(),
body: JSON.stringify({ key }),
});
if (response.ok) {
const data = await response.json();
2023-12-02 20:11:06 +01:00
return data.value;
}
} catch {
console.error('Could not find secret value: ', key);
}
}
2023-07-20 19:32:15 +02:00
function authorizeOpenRouter() {
const openRouterUrl = `https://openrouter.ai/auth?callback_url=${encodeURIComponent(location.origin)}`;
location.href = openRouterUrl;
}
async function checkOpenRouterAuth() {
const params = new URLSearchParams(location.search);
if (params.has('code')) {
const code = params.get('code');
try {
2023-12-02 19:04:51 +01:00
const response = await fetch('https://openrouter.ai/api/v1/auth/keys', {
2023-07-20 19:32:15 +02:00
method: 'POST',
body: JSON.stringify({ code }),
});
if (!response.ok) {
throw new Error('OpenRouter exchange error');
}
const data = await response.json();
if (!data || !data.key) {
throw new Error('OpenRouter invalid response');
}
await writeSecret(SECRET_KEYS.OPENROUTER, data.key);
if (secret_state[SECRET_KEYS.OPENROUTER]) {
toastr.success('OpenRouter token saved');
// Remove the code from the URL
const currentUrl = window.location.href;
2023-12-02 19:04:51 +01:00
const urlWithoutSearchParams = currentUrl.split('?')[0];
window.history.pushState({}, '', urlWithoutSearchParams);
2023-07-20 19:32:15 +02:00
} else {
throw new Error('OpenRouter token not saved');
}
} catch (err) {
toastr.error('Could not verify OpenRouter token. Please try again.');
return;
}
}
}
jQuery(async () => {
$('#viewSecrets').on('click', viewSecrets);
$(document).on('click', '.clear-api-key', clearSecret);
$(document).on('input', Object.values(INPUT_MAP).join(','), function () {
const id = $(this).attr('id');
const value = $(this).val();
const warningElement = $(`[data-for="${id}"]`);
warningElement.toggle(value.length > 0);
});
$('.openrouter_authorize').on('click', authorizeOpenRouter);
2023-07-20 19:32:15 +02:00
});