Clean-up diff pt.2
This commit is contained in:
parent
c49b37f968
commit
60e099e852
|
@ -2,8 +2,8 @@ export { translate };
|
||||||
|
|
||||||
import {
|
import {
|
||||||
callPopup,
|
callPopup,
|
||||||
event_types,
|
|
||||||
eventSource,
|
eventSource,
|
||||||
|
event_types,
|
||||||
getRequestHeaders,
|
getRequestHeaders,
|
||||||
reloadCurrentChat,
|
reloadCurrentChat,
|
||||||
saveSettingsDebounced,
|
saveSettingsDebounced,
|
||||||
|
@ -142,21 +142,12 @@ const KEY_REQUIRED = ['deepl', 'libre'];
|
||||||
const LOCAL_URL = ['libre', 'oneringtranslator', 'deeplx', 'lingva'];
|
const LOCAL_URL = ['libre', 'oneringtranslator', 'deeplx', 'lingva'];
|
||||||
|
|
||||||
function showKeysButton() {
|
function showKeysButton() {
|
||||||
const providerRequiresKey =
|
const providerRequiresKey = KEY_REQUIRED.includes(extension_settings.translate.provider);
|
||||||
KEY_REQUIRED.includes(extension_settings.translate.provider);
|
const providerOptionalUrl = LOCAL_URL.includes(extension_settings.translate.provider);
|
||||||
const providerOptionalUrl =
|
|
||||||
LOCAL_URL.includes(extension_settings.translate.provider);
|
|
||||||
$('#translate_key_button').toggle(providerRequiresKey);
|
$('#translate_key_button').toggle(providerRequiresKey);
|
||||||
$('#translate_key_button')
|
$('#translate_key_button').toggleClass('success', Boolean(secret_state[extension_settings.translate.provider]));
|
||||||
.toggleClass(
|
|
||||||
'success',
|
|
||||||
Boolean(secret_state[extension_settings.translate.provider]));
|
|
||||||
$('#translate_url_button').toggle(providerOptionalUrl);
|
$('#translate_url_button').toggle(providerOptionalUrl);
|
||||||
$('#translate_url_button')
|
$('#translate_url_button').toggleClass('success', Boolean(secret_state[extension_settings.translate.provider + '_url']));
|
||||||
.toggleClass(
|
|
||||||
'success',
|
|
||||||
Boolean(
|
|
||||||
secret_state[extension_settings.translate.provider + '_url']));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function loadSettings() {
|
function loadSettings() {
|
||||||
|
@ -166,18 +157,14 @@ function loadSettings() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$(`#translation_provider option[value="${extension_settings.translate.provider}"]`)
|
$(`#translation_provider option[value="${extension_settings.translate.provider}"]`).attr('selected', true);
|
||||||
.attr('selected', true);
|
$(`#translation_target_language option[value="${extension_settings.translate.target_language}"]`).attr('selected', true);
|
||||||
$(`#translation_target_language option[value="${extension_settings.translate.target_language}"]`)
|
$(`#translation_auto_mode option[value="${extension_settings.translate.auto_mode}"]`).attr('selected', true);
|
||||||
.attr('selected', true);
|
|
||||||
$(`#translation_auto_mode option[value="${extension_settings.translate.auto_mode}"]`)
|
|
||||||
.attr('selected', true);
|
|
||||||
showKeysButton();
|
showKeysButton();
|
||||||
}
|
}
|
||||||
|
|
||||||
async function translateImpersonate(text) {
|
async function translateImpersonate(text) {
|
||||||
const translatedText =
|
const translatedText = await translate(text, extension_settings.translate.target_language);
|
||||||
await translate(text, extension_settings.translate.target_language);
|
|
||||||
$('#send_textarea').val(translatedText);
|
$('#send_textarea').val(translatedText);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -194,19 +181,17 @@ async function translateIncomingMessage(messageId) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const textToTranslate =
|
const textToTranslate = substituteParams(message.mes, context.name1, message.name);
|
||||||
substituteParams(message.mes, context.name1, message.name);
|
const translation = await translate(textToTranslate, extension_settings.translate.target_language);
|
||||||
const translation = await translate(
|
|
||||||
textToTranslate, extension_settings.translate.target_language);
|
|
||||||
message.extra.display_text = translation;
|
message.extra.display_text = translation;
|
||||||
|
|
||||||
updateMessageBlock(messageId, message);
|
updateMessageBlock(messageId, message);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function translateProviderOneRing(text, lang) {
|
async function translateProviderOneRing(text, lang) {
|
||||||
let from_lang = lang == extension_settings.translate.internal_language ?
|
let from_lang = lang == extension_settings.translate.internal_language
|
||||||
extension_settings.translate.target_language :
|
? extension_settings.translate.target_language
|
||||||
extension_settings.translate.internal_language;
|
: extension_settings.translate.internal_language;
|
||||||
|
|
||||||
const response = await fetch('/api/translate/onering', {
|
const response = await fetch('/api/translate/onering', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
|
@ -356,8 +341,7 @@ async function translateProviderBing(text, lang) {
|
||||||
* Splits text into chunks and translates each chunk separately
|
* Splits text into chunks and translates each chunk separately
|
||||||
* @param {string} text Text to translate
|
* @param {string} text Text to translate
|
||||||
* @param {string} lang Target language code
|
* @param {string} lang Target language code
|
||||||
* @param {(text: string, lang: string) => Promise<string>} translateFn Function
|
* @param {(text: string, lang: string) => Promise<string>} translateFn Function to translate a single chunk (must return a Promise)
|
||||||
* to translate a single chunk (must return a Promise)
|
|
||||||
* @param {number} chunkSize Maximum chunk size
|
* @param {number} chunkSize Maximum chunk size
|
||||||
* @returns {Promise<string>} Translated text
|
* @returns {Promise<string>} Translated text
|
||||||
*/
|
*/
|
||||||
|
@ -391,24 +375,19 @@ async function translate(text, lang) {
|
||||||
case 'libre':
|
case 'libre':
|
||||||
return await translateProviderLibre(text, lang);
|
return await translateProviderLibre(text, lang);
|
||||||
case 'google':
|
case 'google':
|
||||||
return await chunkedTranslate(
|
return await chunkedTranslate(text, lang, translateProviderGoogle, 5000);
|
||||||
text, lang, translateProviderGoogle, 5000);
|
|
||||||
case 'lingva':
|
case 'lingva':
|
||||||
return await chunkedTranslate(
|
return await chunkedTranslate(text, lang, translateProviderLingva, 5000);
|
||||||
text, lang, translateProviderLingva, 5000);
|
|
||||||
case 'deepl':
|
case 'deepl':
|
||||||
return await translateProviderDeepl(text, lang);
|
return await translateProviderDeepl(text, lang);
|
||||||
case 'deeplx':
|
case 'deeplx':
|
||||||
return await chunkedTranslate(
|
return await chunkedTranslate(text, lang, translateProviderDeepLX, 1500);
|
||||||
text, lang, translateProviderDeepLX, 1500);
|
|
||||||
case 'oneringtranslator':
|
case 'oneringtranslator':
|
||||||
return await translateProviderOneRing(text, lang);
|
return await translateProviderOneRing(text, lang);
|
||||||
case 'bing':
|
case 'bing':
|
||||||
return await chunkedTranslate(text, lang, translateProviderBing, 1000);
|
return await chunkedTranslate(text, lang, translateProviderBing, 1000);
|
||||||
default:
|
default:
|
||||||
console.error(
|
console.error('Unknown translation provider', extension_settings.translate.provider);
|
||||||
'Unknown translation provider',
|
|
||||||
extension_settings.translate.provider);
|
|
||||||
return text;
|
return text;
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
@ -427,8 +406,7 @@ async function translateOutgoingMessage(messageId) {
|
||||||
|
|
||||||
const originalText = message.mes;
|
const originalText = message.mes;
|
||||||
message.extra.display_text = originalText;
|
message.extra.display_text = originalText;
|
||||||
message.mes = await translate(
|
message.mes = await translate(originalText, extension_settings.translate.internal_language);
|
||||||
originalText, extension_settings.translate.internal_language);
|
|
||||||
updateMessageBlock(messageId, message);
|
updateMessageBlock(messageId, message);
|
||||||
|
|
||||||
console.log('translateOutgoingMessage', messageId);
|
console.log('translateOutgoingMessage', messageId);
|
||||||
|
@ -449,10 +427,10 @@ function createEventHandler(translateFunction, shouldTranslateFunction) {
|
||||||
async function onTranslateInputMessageClick() {
|
async function onTranslateInputMessageClick() {
|
||||||
const ta = document.querySelector('#send_textarea');
|
const ta = document.querySelector('#send_textarea');
|
||||||
toastr.info('Input Message is translating', 'Please wait...');
|
toastr.info('Input Message is translating', 'Please wait...');
|
||||||
const translatedText =
|
const translatedText = await translate(ta.value, extension_settings.translate.internal_language);
|
||||||
await translate(ta.value, extension_settings.translate.internal_language);
|
|
||||||
$('#send_textarea').val(translatedText);
|
$('#send_textarea').val(translatedText);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prevents the chat from being translated in parallel
|
// Prevents the chat from being translated in parallel
|
||||||
let translateChatExecuting = false;
|
let translateChatExecuting = false;
|
||||||
|
|
||||||
|
@ -466,8 +444,7 @@ async function onTranslateChatClick() {
|
||||||
const context = getContext();
|
const context = getContext();
|
||||||
const chat = context.chat;
|
const chat = context.chat;
|
||||||
|
|
||||||
toastr.info(
|
toastr.info(`${chat.length} message(s) queued for translation.`, 'Please wait...');
|
||||||
`${chat.length} message(s) queued for translation.`, 'Please wait...');
|
|
||||||
|
|
||||||
for (let i = 0; i < chat.length; i++) {
|
for (let i = 0; i < chat.length; i++) {
|
||||||
await translateIncomingMessage(i);
|
await translateIncomingMessage(i);
|
||||||
|
@ -483,9 +460,7 @@ async function onTranslateChatClick() {
|
||||||
}
|
}
|
||||||
|
|
||||||
async function onTranslationsClearClick() {
|
async function onTranslationsClearClick() {
|
||||||
const confirm = await callPopup(
|
const confirm = await callPopup('<h3>Are you sure?</h3>This will remove translated text from all messages in the current chat. This action cannot be undone.', 'confirm');
|
||||||
'<h3>Are you sure?</h3>This will remove translated text from all messages in the current chat. This action cannot be undone.',
|
|
||||||
'confirm');
|
|
||||||
|
|
||||||
if (!confirm) {
|
if (!confirm) {
|
||||||
return;
|
return;
|
||||||
|
@ -509,13 +484,11 @@ async function translateMessageEdit(messageId) {
|
||||||
const chat = context.chat;
|
const chat = context.chat;
|
||||||
const message = chat[messageId];
|
const message = chat[messageId];
|
||||||
|
|
||||||
if (message.is_system ||
|
if (message.is_system || extension_settings.translate.auto_mode == autoModeOptions.NONE) {
|
||||||
extension_settings.translate.auto_mode == autoModeOptions.NONE) {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((message.is_user && shouldTranslate(outgoingTypes)) ||
|
if ((message.is_user && shouldTranslate(outgoingTypes)) || (!message.is_user && shouldTranslate(incomingTypes))) {
|
||||||
(!message.is_user && shouldTranslate(incomingTypes))) {
|
|
||||||
await translateIncomingMessage(messageId);
|
await translateIncomingMessage(messageId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -538,12 +511,9 @@ async function onMessageTranslateClick() {
|
||||||
await context.saveChat();
|
await context.saveChat();
|
||||||
}
|
}
|
||||||
|
|
||||||
const handleIncomingMessage = createEventHandler(
|
const handleIncomingMessage = createEventHandler(translateIncomingMessage, () => shouldTranslate(incomingTypes));
|
||||||
translateIncomingMessage, () => shouldTranslate(incomingTypes));
|
const handleOutgoingMessage = createEventHandler(translateOutgoingMessage, () => shouldTranslate(outgoingTypes));
|
||||||
const handleOutgoingMessage = createEventHandler(
|
const handleImpersonateReady = createEventHandler(translateImpersonate, () => shouldTranslate(incomingTypes));
|
||||||
translateOutgoingMessage, () => shouldTranslate(outgoingTypes));
|
|
||||||
const handleImpersonateReady = createEventHandler(
|
|
||||||
translateImpersonate, () => shouldTranslate(incomingTypes));
|
|
||||||
const handleMessageEdit = createEventHandler(translateMessageEdit, () => true);
|
const handleMessageEdit = createEventHandler(translateMessageEdit, () => true);
|
||||||
|
|
||||||
window['translate'] = translate;
|
window['translate'] = translate;
|
||||||
|
@ -605,8 +575,7 @@ jQuery(() => {
|
||||||
$('#translation_clear').on('click', onTranslationsClearClick);
|
$('#translation_clear').on('click', onTranslationsClearClick);
|
||||||
|
|
||||||
for (const [key, value] of Object.entries(languageCodes)) {
|
for (const [key, value] of Object.entries(languageCodes)) {
|
||||||
$('#translation_target_language')
|
$('#translation_target_language').append(`<option value="${value}">${key}</option>`);
|
||||||
.append(`<option value="${value}">${key}</option>`);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$('#translation_auto_mode').on('change', (event) => {
|
$('#translation_auto_mode').on('change', (event) => {
|
||||||
|
|
Loading…
Reference in New Issue