mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-03-03 03:17:54 +01:00
Merge branch 'enhancement-make-buttons-scrollable' of https://github.com/joenunezb/SillyTavern into enhancement-make-buttons-scrollable
This commit is contained in:
commit
6e85b20c8a
11
package-lock.json
generated
11
package-lock.json
generated
@ -1,12 +1,12 @@
|
|||||||
{
|
{
|
||||||
"name": "sillytavern",
|
"name": "sillytavern",
|
||||||
"version": "1.12.6",
|
"version": "1.12.7",
|
||||||
"lockfileVersion": 3,
|
"lockfileVersion": 3,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"name": "sillytavern",
|
"name": "sillytavern",
|
||||||
"version": "1.12.6",
|
"version": "1.12.7",
|
||||||
"hasInstallScript": true,
|
"hasInstallScript": true,
|
||||||
"license": "AGPL-3.0",
|
"license": "AGPL-3.0",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
@ -24,6 +24,7 @@
|
|||||||
"csrf-csrf": "^2.2.3",
|
"csrf-csrf": "^2.2.3",
|
||||||
"express": "^4.21.0",
|
"express": "^4.21.0",
|
||||||
"form-data": "^4.0.0",
|
"form-data": "^4.0.0",
|
||||||
|
"google-translate-api-browser": "^3.0.1",
|
||||||
"google-translate-api-x": "^10.7.1",
|
"google-translate-api-x": "^10.7.1",
|
||||||
"helmet": "^7.1.0",
|
"helmet": "^7.1.0",
|
||||||
"html-entities": "^2.5.2",
|
"html-entities": "^2.5.2",
|
||||||
@ -3693,6 +3694,12 @@
|
|||||||
"url": "https://github.com/sponsors/sindresorhus"
|
"url": "https://github.com/sponsors/sindresorhus"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/google-translate-api-browser": {
|
||||||
|
"version": "3.0.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/google-translate-api-browser/-/google-translate-api-browser-3.0.1.tgz",
|
||||||
|
"integrity": "sha512-KTLodkyGBWMK9IW6QIeJ2zCuju4Z0CLpbkADKo+yLhbSTD4l+CXXpQ/xaynGVAzeBezzJG6qn8MLeqOq3SmW0A==",
|
||||||
|
"license": "MIT"
|
||||||
|
},
|
||||||
"node_modules/google-translate-api-x": {
|
"node_modules/google-translate-api-x": {
|
||||||
"version": "10.7.1",
|
"version": "10.7.1",
|
||||||
"resolved": "https://registry.npmjs.org/google-translate-api-x/-/google-translate-api-x-10.7.1.tgz",
|
"resolved": "https://registry.npmjs.org/google-translate-api-x/-/google-translate-api-x-10.7.1.tgz",
|
||||||
|
@ -14,6 +14,7 @@
|
|||||||
"csrf-csrf": "^2.2.3",
|
"csrf-csrf": "^2.2.3",
|
||||||
"express": "^4.21.0",
|
"express": "^4.21.0",
|
||||||
"form-data": "^4.0.0",
|
"form-data": "^4.0.0",
|
||||||
|
"google-translate-api-browser": "^3.0.1",
|
||||||
"google-translate-api-x": "^10.7.1",
|
"google-translate-api-x": "^10.7.1",
|
||||||
"helmet": "^7.1.0",
|
"helmet": "^7.1.0",
|
||||||
"html-entities": "^2.5.2",
|
"html-entities": "^2.5.2",
|
||||||
@ -66,7 +67,7 @@
|
|||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "https://github.com/SillyTavern/SillyTavern.git"
|
"url": "https://github.com/SillyTavern/SillyTavern.git"
|
||||||
},
|
},
|
||||||
"version": "1.12.6",
|
"version": "1.12.7",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"start": "node server.js",
|
"start": "node server.js",
|
||||||
"start:no-csrf": "node server.js --disableCsrf",
|
"start:no-csrf": "node server.js --disableCsrf",
|
||||||
|
@ -1397,8 +1397,7 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- Enable for llama.cpp when the PR is merged: https://github.com/ggerganov/llama.cpp/pull/6839 -->
|
<div data-tg-type="ooba, koboldcpp, tabby, llamacpp" id="dryBlock" class="wide100p">
|
||||||
<div data-tg-type="ooba, koboldcpp, tabby" id="dryBlock" class="wide100p">
|
|
||||||
<h4 class="wide100p textAlignCenter" title="DRY penalizes tokens that would extend the end of the input into a sequence that has previously occurred in the input. Set multiplier to 0 to disable." data-i18n="[title]DRY_Repetition_Penalty_desc">
|
<h4 class="wide100p textAlignCenter" title="DRY penalizes tokens that would extend the end of the input into a sequence that has previously occurred in the input. Set multiplier to 0 to disable." data-i18n="[title]DRY_Repetition_Penalty_desc">
|
||||||
<label data-i18n="DRY Repetition Penalty">DRY Repetition Penalty</label>
|
<label data-i18n="DRY Repetition Penalty">DRY Repetition Penalty</label>
|
||||||
<a href="https://github.com/oobabooga/text-generation-webui/pull/5677" target="_blank">
|
<a href="https://github.com/oobabooga/text-generation-webui/pull/5677" target="_blank">
|
||||||
@ -2797,6 +2796,8 @@
|
|||||||
<h4 data-i18n="Claude Model">Claude Model</h4>
|
<h4 data-i18n="Claude Model">Claude Model</h4>
|
||||||
<select id="model_claude_select">
|
<select id="model_claude_select">
|
||||||
<optgroup label="Versions">
|
<optgroup label="Versions">
|
||||||
|
<option value="claude-3-5-sonnet-latest">claude-3-5-sonnet-latest</option>
|
||||||
|
<option value="claude-3-5-sonnet-20241022">claude-3-5-sonnet-20241022</option>
|
||||||
<option value="claude-3-5-sonnet-20240620">claude-3-5-sonnet-20240620</option>
|
<option value="claude-3-5-sonnet-20240620">claude-3-5-sonnet-20240620</option>
|
||||||
<option value="claude-3-opus-20240229">claude-3-opus-20240229</option>
|
<option value="claude-3-opus-20240229">claude-3-opus-20240229</option>
|
||||||
<option value="claude-3-sonnet-20240229">claude-3-sonnet-20240229</option>
|
<option value="claude-3-sonnet-20240229">claude-3-sonnet-20240229</option>
|
||||||
|
@ -3136,7 +3136,8 @@ class StreamingProcessor {
|
|||||||
|
|
||||||
//console.log("Generated text size:", text.length, text)
|
//console.log("Generated text size:", text.length, text)
|
||||||
|
|
||||||
if (power_user.auto_swipe) {
|
const isAborted = this.abortController.signal.aborted;
|
||||||
|
if (power_user.auto_swipe && !isAborted) {
|
||||||
function containsBlacklistedWords(str, blacklist, threshold) {
|
function containsBlacklistedWords(str, blacklist, threshold) {
|
||||||
const regex = new RegExp(`\\b(${blacklist.join('|')})\\b`, 'gi');
|
const regex = new RegExp(`\\b(${blacklist.join('|')})\\b`, 'gi');
|
||||||
const matches = str.match(regex) || [];
|
const matches = str.match(regex) || [];
|
||||||
@ -3840,7 +3841,7 @@ export async function Generate(type, { automatic_trigger, force_name2, quiet_pro
|
|||||||
if (addUserAlignment) {
|
if (addUserAlignment) {
|
||||||
const alignmentMessage = {
|
const alignmentMessage = {
|
||||||
name: name1,
|
name: name1,
|
||||||
mes: power_user.instruct.user_alignment_message,
|
mes: substituteParams(power_user.instruct.user_alignment_message),
|
||||||
is_user: true,
|
is_user: true,
|
||||||
};
|
};
|
||||||
userAlignmentMessage = formatMessageHistoryItem(alignmentMessage, isInstruct, force_output_sequence.FIRST);
|
userAlignmentMessage = formatMessageHistoryItem(alignmentMessage, isInstruct, force_output_sequence.FIRST);
|
||||||
@ -4583,7 +4584,8 @@ export async function Generate(type, { automatic_trigger, force_name2, quiet_pro
|
|||||||
playMessageSound();
|
playMessageSound();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (power_user.auto_swipe) {
|
const isAborted = abortController && abortController.signal.aborted;
|
||||||
|
if (power_user.auto_swipe && !isAborted) {
|
||||||
console.debug('checking for autoswipeblacklist on non-streaming message');
|
console.debug('checking for autoswipeblacklist on non-streaming message');
|
||||||
function containsBlacklistedWords(getMessage, blacklist, threshold) {
|
function containsBlacklistedWords(getMessage, blacklist, threshold) {
|
||||||
console.debug('checking blacklisted words');
|
console.debug('checking blacklisted words');
|
||||||
@ -4768,6 +4770,11 @@ export function shouldAutoContinue(messageChunk, isImpersonate) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (abortController && abortController.signal.aborted) {
|
||||||
|
console.debug('Auto-continue is not triggered because the generation was stopped.');
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
if (power_user.auto_continue.target_length <= 0) {
|
if (power_user.auto_continue.target_length <= 0) {
|
||||||
console.log('Auto-continue target length is 0, not triggering auto-continue');
|
console.log('Auto-continue target length is 0, not triggering auto-continue');
|
||||||
return false;
|
return false;
|
||||||
@ -7178,7 +7185,7 @@ export function select_rm_info(type, charId, previousCharId = null) {
|
|||||||
importFlashTimeout = setTimeout(function () {
|
importFlashTimeout = setTimeout(function () {
|
||||||
if (type === 'char_import' || type === 'char_create') {
|
if (type === 'char_import' || type === 'char_create') {
|
||||||
// Find the page at which the character is located
|
// Find the page at which the character is located
|
||||||
const avatarFileName = `${charId}.png`;
|
const avatarFileName = charId;
|
||||||
const charData = getEntitiesList({ doFilter: true });
|
const charData = getEntitiesList({ doFilter: true });
|
||||||
const charIndex = charData.findIndex((x) => x?.item?.avatar?.startsWith(avatarFileName));
|
const charIndex = charData.findIndex((x) => x?.item?.avatar?.startsWith(avatarFileName));
|
||||||
|
|
||||||
@ -8218,6 +8225,10 @@ window['SillyTavern'].getContext = function () {
|
|||||||
substituteParams,
|
substituteParams,
|
||||||
substituteParamsExtended,
|
substituteParamsExtended,
|
||||||
SlashCommandParser,
|
SlashCommandParser,
|
||||||
|
SlashCommand,
|
||||||
|
SlashCommandArgument,
|
||||||
|
SlashCommandNamedArgument,
|
||||||
|
ARGUMENT_TYPE,
|
||||||
executeSlashCommandsWithOptions,
|
executeSlashCommandsWithOptions,
|
||||||
/** @deprecated Use SlashCommandParser.addCommandObject() instead */
|
/** @deprecated Use SlashCommandParser.addCommandObject() instead */
|
||||||
registerSlashCommand: registerSlashCommand,
|
registerSlashCommand: registerSlashCommand,
|
||||||
|
@ -43,6 +43,8 @@
|
|||||||
<option data-type="openai" value="gpt-4o">gpt-4o</option>
|
<option data-type="openai" value="gpt-4o">gpt-4o</option>
|
||||||
<option data-type="openai" value="gpt-4o-mini">gpt-4o-mini</option>
|
<option data-type="openai" value="gpt-4o-mini">gpt-4o-mini</option>
|
||||||
<option data-type="openai" value="chatgpt-4o-latest">chatgpt-4o-latest</option>
|
<option data-type="openai" value="chatgpt-4o-latest">chatgpt-4o-latest</option>
|
||||||
|
<option data-type="anthropic" value="claude-3-5-sonnet-latest">claude-3-5-sonnet-latest</option>
|
||||||
|
<option data-type="anthropic" value="claude-3-5-sonnet-20241022">claude-3-5-sonnet-20241022</option>
|
||||||
<option data-type="anthropic" value="claude-3-5-sonnet-20240620">claude-3-5-sonnet-20240620</option>
|
<option data-type="anthropic" value="claude-3-5-sonnet-20240620">claude-3-5-sonnet-20240620</option>
|
||||||
<option data-type="anthropic" value="claude-3-opus-20240229">claude-3-opus-20240229</option>
|
<option data-type="anthropic" value="claude-3-opus-20240229">claude-3-opus-20240229</option>
|
||||||
<option data-type="anthropic" value="claude-3-sonnet-20240229">claude-3-sonnet-20240229</option>
|
<option data-type="anthropic" value="claude-3-sonnet-20240229">claude-3-sonnet-20240229</option>
|
||||||
|
@ -9,6 +9,8 @@ var langs;
|
|||||||
// eslint-disable-next-line prefer-const
|
// eslint-disable-next-line prefer-const
|
||||||
var localeData;
|
var localeData;
|
||||||
|
|
||||||
|
export const getCurrentLocale = () => localeFile;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An observer that will check if any new i18n elements are added to the document
|
* An observer that will check if any new i18n elements are added to the document
|
||||||
* @type {MutationObserver}
|
* @type {MutationObserver}
|
||||||
@ -215,7 +217,6 @@ function addLanguagesToDropdown() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function initLocales() {
|
export async function initLocales() {
|
||||||
moment.locale(localeFile);
|
|
||||||
langs = await fetch('/locales/lang.json').then(response => response.json());
|
langs = await fetch('/locales/lang.json').then(response => response.json());
|
||||||
localeData = await getLocaleData(localeFile);
|
localeData = await getLocaleData(localeFile);
|
||||||
applyLocale();
|
applyLocale();
|
||||||
|
@ -34,6 +34,7 @@ export const SECRET_KEYS = {
|
|||||||
STABILITY: 'api_key_stability',
|
STABILITY: 'api_key_stability',
|
||||||
BLOCKENTROPY: 'api_key_blockentropy',
|
BLOCKENTROPY: 'api_key_blockentropy',
|
||||||
CUSTOM_OPENAI_TTS: 'api_key_custom_openai_tts',
|
CUSTOM_OPENAI_TTS: 'api_key_custom_openai_tts',
|
||||||
|
TAVILY: 'api_key_tavily',
|
||||||
};
|
};
|
||||||
|
|
||||||
const INPUT_MAP = {
|
const INPUT_MAP = {
|
||||||
|
@ -1395,6 +1395,13 @@ export function initDefaultSlashCommands() {
|
|||||||
callback: popupCallback,
|
callback: popupCallback,
|
||||||
returns: 'popup text',
|
returns: 'popup text',
|
||||||
namedArgumentList: [
|
namedArgumentList: [
|
||||||
|
SlashCommandNamedArgument.fromProps({
|
||||||
|
name: 'scroll',
|
||||||
|
description: 'allows vertical scrolling of the content',
|
||||||
|
typeList: [ARGUMENT_TYPE.BOOLEAN],
|
||||||
|
enumList: commonEnumProviders.boolean('trueFalse')(),
|
||||||
|
defaultValue: 'true',
|
||||||
|
}),
|
||||||
SlashCommandNamedArgument.fromProps({
|
SlashCommandNamedArgument.fromProps({
|
||||||
name: 'large',
|
name: 'large',
|
||||||
description: 'show large popup',
|
description: 'show large popup',
|
||||||
@ -2125,7 +2132,7 @@ async function buttonsCallback(args, text) {
|
|||||||
popupContainer.style.flexDirection = 'column';
|
popupContainer.style.flexDirection = 'column';
|
||||||
popupContainer.style.maxHeight = '80vh'; // Limit the overall height of the popup
|
popupContainer.style.maxHeight = '80vh'; // Limit the overall height of the popup
|
||||||
|
|
||||||
popup = new Popup(popupContainer, POPUP_TYPE.TEXT, '', { okButton: 'Cancel' });
|
popup = new Popup(popupContainer, POPUP_TYPE.TEXT, '', { okButton: 'Cancel', allowVerticalScrolling: true });
|
||||||
popup.show()
|
popup.show()
|
||||||
.then((result => resolve(typeof result === 'number' ? resultToButtonMap.get(result) ?? '' : '')))
|
.then((result => resolve(typeof result === 'number' ? resultToButtonMap.get(result) ?? '' : '')))
|
||||||
.catch(() => resolve(''));
|
.catch(() => resolve(''));
|
||||||
@ -2142,6 +2149,7 @@ async function popupCallback(args, value) {
|
|||||||
|
|
||||||
/** @type {import('./popup.js').PopupOptions} */
|
/** @type {import('./popup.js').PopupOptions} */
|
||||||
const popupOptions = {
|
const popupOptions = {
|
||||||
|
allowVerticalScrolling: !isFalseBoolean(args?.scroll),
|
||||||
large: isTrueBoolean(args?.large),
|
large: isTrueBoolean(args?.large),
|
||||||
wide: isTrueBoolean(args?.wide),
|
wide: isTrueBoolean(args?.wide),
|
||||||
wider: isTrueBoolean(args?.wider),
|
wider: isTrueBoolean(args?.wider),
|
||||||
|
@ -1307,11 +1307,23 @@ export function getTextGenGenerationData(finalPrompt, maxTokens, isImpersonate,
|
|||||||
: [];
|
: [];
|
||||||
const tokenBans = toIntArray(banned_tokens);
|
const tokenBans = toIntArray(banned_tokens);
|
||||||
logitBiasArray.push(...tokenBans.map(x => [Number(x), false]));
|
logitBiasArray.push(...tokenBans.map(x => [Number(x), false]));
|
||||||
|
const sequenceBreakers = (() => {
|
||||||
|
try {
|
||||||
|
return JSON.parse(params.dry_sequence_breakers);
|
||||||
|
} catch {
|
||||||
|
if (typeof params.dry_sequence_breakers === 'string') {
|
||||||
|
return params.dry_sequence_breakers.split(',');
|
||||||
|
}
|
||||||
|
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
})();
|
||||||
const llamaCppParams = {
|
const llamaCppParams = {
|
||||||
'logit_bias': logitBiasArray,
|
'logit_bias': logitBiasArray,
|
||||||
// Conflicts with ooba's grammar_string
|
// Conflicts with ooba's grammar_string
|
||||||
'grammar': settings.grammar_string,
|
'grammar': settings.grammar_string,
|
||||||
'cache_prompt': true,
|
'cache_prompt': true,
|
||||||
|
'dry_sequence_breakers': sequenceBreakers,
|
||||||
};
|
};
|
||||||
params = Object.assign(params, llamaCppParams);
|
params = Object.assign(params, llamaCppParams);
|
||||||
}
|
}
|
||||||
|
@ -7,6 +7,7 @@ import { Popup, POPUP_RESULT, POPUP_TYPE } from './popup.js';
|
|||||||
import { SlashCommandClosure } from './slash-commands/SlashCommandClosure.js';
|
import { SlashCommandClosure } from './slash-commands/SlashCommandClosure.js';
|
||||||
import { getTagsList } from './tags.js';
|
import { getTagsList } from './tags.js';
|
||||||
import { groups, selected_group } from './group-chats.js';
|
import { groups, selected_group } from './group-chats.js';
|
||||||
|
import { getCurrentLocale } from './i18n.js';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Pagination status string template.
|
* Pagination status string template.
|
||||||
@ -831,7 +832,7 @@ export function timestampToMoment(timestamp) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const iso8601 = parseTimestamp(timestamp);
|
const iso8601 = parseTimestamp(timestamp);
|
||||||
const objMoment = iso8601 ? moment(iso8601) : moment.invalid();
|
const objMoment = iso8601 ? moment(iso8601).locale(getCurrentLocale()) : moment.invalid();
|
||||||
|
|
||||||
dateCache.set(timestamp, objMoment);
|
dateCache.set(timestamp, objMoment);
|
||||||
return objMoment;
|
return objMoment;
|
||||||
|
@ -206,6 +206,54 @@ router.post('/searxng', jsonParser, async (request, response) => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
router.post('/tavily', jsonParser, async (request, response) => {
|
||||||
|
try {
|
||||||
|
const apiKey = readSecret(request.user.directories, SECRET_KEYS.TAVILY);
|
||||||
|
|
||||||
|
if (!apiKey) {
|
||||||
|
console.log('No Tavily key found');
|
||||||
|
return response.sendStatus(400);
|
||||||
|
}
|
||||||
|
|
||||||
|
const { query } = request.body;
|
||||||
|
|
||||||
|
const body = {
|
||||||
|
query: query,
|
||||||
|
api_key: apiKey,
|
||||||
|
search_depth: 'basic',
|
||||||
|
topic: 'general',
|
||||||
|
include_answer: true,
|
||||||
|
include_raw_content: false,
|
||||||
|
include_images: false,
|
||||||
|
include_image_descriptions: false,
|
||||||
|
include_domains: [],
|
||||||
|
max_results: 10,
|
||||||
|
};
|
||||||
|
|
||||||
|
const result = await fetch('https://api.tavily.com/search', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
},
|
||||||
|
body: JSON.stringify(body),
|
||||||
|
});
|
||||||
|
|
||||||
|
console.log('Tavily query', query);
|
||||||
|
|
||||||
|
if (!result.ok) {
|
||||||
|
const text = await result.text();
|
||||||
|
console.log('Tavily request failed', result.statusText, text);
|
||||||
|
return response.status(500).send(text);
|
||||||
|
}
|
||||||
|
|
||||||
|
const data = await result.json();
|
||||||
|
return response.json(data);
|
||||||
|
} catch (error) {
|
||||||
|
console.log(error);
|
||||||
|
return response.sendStatus(500);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
router.post('/visit', jsonParser, async (request, response) => {
|
router.post('/visit', jsonParser, async (request, response) => {
|
||||||
try {
|
try {
|
||||||
const url = request.body.url;
|
const url = request.body.url;
|
||||||
|
@ -47,6 +47,7 @@ export const SECRET_KEYS = {
|
|||||||
STABILITY: 'api_key_stability',
|
STABILITY: 'api_key_stability',
|
||||||
BLOCKENTROPY: 'api_key_blockentropy',
|
BLOCKENTROPY: 'api_key_blockentropy',
|
||||||
CUSTOM_OPENAI_TTS: 'api_key_custom_openai_tts',
|
CUSTOM_OPENAI_TTS: 'api_key_custom_openai_tts',
|
||||||
|
TAVILY: 'api_key_tavily',
|
||||||
};
|
};
|
||||||
|
|
||||||
// These are the keys that are safe to expose, even if allowKeysExposure is false
|
// These are the keys that are safe to expose, even if allowKeysExposure is false
|
||||||
|
@ -1,9 +1,10 @@
|
|||||||
import https from 'node:https';
|
import https from 'node:https';
|
||||||
|
import { createRequire } from 'node:module';
|
||||||
|
|
||||||
import fetch from 'node-fetch';
|
import fetch from 'node-fetch';
|
||||||
import express from 'express';
|
import express from 'express';
|
||||||
import bingTranslateApi from 'bing-translate-api';
|
import bingTranslateApi from 'bing-translate-api';
|
||||||
import googleTranslateApi from 'google-translate-api-x';
|
import iconv from 'iconv-lite';
|
||||||
|
|
||||||
import { readSecret, SECRET_KEYS } from './secrets.js';
|
import { readSecret, SECRET_KEYS } from './secrets.js';
|
||||||
import { getConfigValue, uuidv4 } from '../util.js';
|
import { getConfigValue, uuidv4 } from '../util.js';
|
||||||
@ -14,6 +15,30 @@ const ONERING_URL_DEFAULT = 'http://127.0.0.1:4990/translate';
|
|||||||
|
|
||||||
export const router = express.Router();
|
export const router = express.Router();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the Google Translate API client.
|
||||||
|
* @returns {import('google-translate-api-browser')} Google Translate API client
|
||||||
|
*/
|
||||||
|
function getGoogleTranslateClient() {
|
||||||
|
const require = createRequire(import.meta.url);
|
||||||
|
const googleTranslateApi = require('google-translate-api-browser');
|
||||||
|
return googleTranslateApi;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tries to decode an ArrayBuffer to a string using iconv-lite for UTF-8.
|
||||||
|
* @param {ArrayBuffer} buffer ArrayBuffer
|
||||||
|
* @returns {string} Decoded string
|
||||||
|
*/
|
||||||
|
function decodeBuffer(buffer) {
|
||||||
|
try {
|
||||||
|
return iconv.decode(Buffer.from(buffer), 'utf-8');
|
||||||
|
} catch (error) {
|
||||||
|
console.log('Failed to decode buffer:', error);
|
||||||
|
return Buffer.from(buffer).toString('utf-8');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
router.post('/libre', jsonParser, async (request, response) => {
|
router.post('/libre', jsonParser, async (request, response) => {
|
||||||
const key = readSecret(request.user.directories, SECRET_KEYS.LIBRE);
|
const key = readSecret(request.user.directories, SECRET_KEYS.LIBRE);
|
||||||
const url = readSecret(request.user.directories, SECRET_KEYS.LIBRE_URL);
|
const url = readSecret(request.user.directories, SECRET_KEYS.LIBRE_URL);
|
||||||
@ -81,8 +106,18 @@ router.post('/google', jsonParser, async (request, response) => {
|
|||||||
|
|
||||||
console.log('Input text: ' + text);
|
console.log('Input text: ' + text);
|
||||||
|
|
||||||
const result = await googleTranslateApi(text, { to: lang, forceBatch: false });
|
const { generateRequestUrl, normaliseResponse } = getGoogleTranslateClient();
|
||||||
const translatedText = Array.isArray(result) ? result.map(x => x.text).join('') : result.text;
|
const requestUrl = generateRequestUrl(text, { to: lang });
|
||||||
|
const result = await fetch(requestUrl);
|
||||||
|
|
||||||
|
if (!result.ok) {
|
||||||
|
console.log('Google Translate error: ', result.statusText);
|
||||||
|
return response.sendStatus(500);
|
||||||
|
}
|
||||||
|
|
||||||
|
const buffer = await result.arrayBuffer();
|
||||||
|
const translateResponse = normaliseResponse(JSON.parse(decodeBuffer(buffer)));
|
||||||
|
const translatedText = translateResponse.text;
|
||||||
|
|
||||||
response.setHeader('Content-Type', 'text/plain; charset=utf-8');
|
response.setHeader('Content-Type', 'text/plain; charset=utf-8');
|
||||||
console.log('Translated text: ' + translatedText);
|
console.log('Translated text: ' + translatedText);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user