Merge branch 'SillyTavern:release' into enhancement-make-buttons-scrollable

dd0caa68069b7b4c9831c49b34546be4aa400a9d

joenunezb <joenunezb@gmail.com>

Signed
14 files changed, +145 -15Showing whitespace changes
package-lock.json+9 -2
@@ -1,12 +1,12 @@
11{
22 "name": "sillytavern",
33 "version": "1.12.67",
44 "lockfileVersion": 3,
55 "requires": true,
66 "packages": {
77 "": {
88 "name": "sillytavern",
99 "version": "1.12.67",
1010 "hasInstallScript": true,
1111 "license": "AGPL-3.0",
1212 "dependencies": {
@@ -24,6 +24,7 @@
2424 "csrf-csrf": "^2.2.3",
2525 "express": "^4.21.0",
2626 "form-data": "^4.0.0",
27+ "google-translate-api-browser": "^3.0.1",
2728 "google-translate-api-x": "^10.7.1",
2829 "helmet": "^7.1.0",
2930 "html-entities": "^2.5.2",
@@ -3693,6 +3694,12 @@
36933694 "url": "https://github.com/sponsors/sindresorhus"
36943695 }
36953696 },
3697+ "node_modules/google-translate-api-browser": {
3698+ "version": "3.0.1",
3699+ "resolved": "https://registry.npmjs.org/google-translate-api-browser/-/google-translate-api-browser-3.0.1.tgz",
3700+ "integrity": "sha512-KTLodkyGBWMK9IW6QIeJ2zCuju4Z0CLpbkADKo+yLhbSTD4l+CXXpQ/xaynGVAzeBezzJG6qn8MLeqOq3SmW0A==",
3701+ "license": "MIT"
3702+ },
36963703 "node_modules/google-translate-api-x": {
36973704 "version": "10.7.1",
36983705 "resolved": "https://registry.npmjs.org/google-translate-api-x/-/google-translate-api-x-10.7.1.tgz",
package.json+2 -1
@@ -14,6 +14,7 @@
1414 "csrf-csrf": "^2.2.3",
1515 "express": "^4.21.0",
1616 "form-data": "^4.0.0",
17+ "google-translate-api-browser": "^3.0.1",
1718 "google-translate-api-x": "^10.7.1",
1819 "helmet": "^7.1.0",
1920 "html-entities": "^2.5.2",
@@ -66,7 +67,7 @@
6667 "type": "git",
6768 "url": "https://github.com/SillyTavern/SillyTavern.git"
6869 },
6970 "version": "1.12.67",
7071 "scripts": {
7172 "start": "node server.js",
7273 "start:no-csrf": "node server.js --disableCsrf",
public/index.html+3 -2
@@ -1397,8 +1397,7 @@
13971397 </div>
13981398 </div>
13991399
1400- <!-- Enable for llama.cpp when the PR is merged: https://github.com/ggerganov/llama.cpp/pull/6839 -->
1400+ <div data-tg-type="ooba, koboldcpp, tabby, llamacpp" id="dryBlock" class="wide100p">
1401- <div data-tg-type="ooba, koboldcpp, tabby" id="dryBlock" class="wide100p">
14021401 <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">
14031402 <label data-i18n="DRY Repetition Penalty">DRY Repetition Penalty</label>
14041403 <a href="https://github.com/oobabooga/text-generation-webui/pull/5677" target="_blank">
@@ -2797,6 +2796,8 @@
27972796 <h4 data-i18n="Claude Model">Claude Model</h4>
27982797 <select id="model_claude_select">
27992798 <optgroup label="Versions">
2799+ <option value="claude-3-5-sonnet-latest">claude-3-5-sonnet-latest</option>
2800+ <option value="claude-3-5-sonnet-20241022">claude-3-5-sonnet-20241022</option>
28002801 <option value="claude-3-5-sonnet-20240620">claude-3-5-sonnet-20240620</option>
28012802 <option value="claude-3-opus-20240229">claude-3-opus-20240229</option>
28022803 <option value="claude-3-sonnet-20240229">claude-3-sonnet-20240229</option>
public/script.js+15 -4
@@ -3136,7 +3136,8 @@ class StreamingProcessor {
31363136
31373137 //console.log("Generated text size:", text.length, text)
31383138
3139- if (power_user.auto_swipe) {
3139+ const isAborted = this.abortController.signal.aborted;
3140+ if (power_user.auto_swipe && !isAborted) {
31403141 function containsBlacklistedWords(str, blacklist, threshold) {
31413142 const regex = new RegExp(`\\b(${blacklist.join('|')})\\b`, 'gi');
31423143 const matches = str.match(regex) || [];
@@ -3840,7 +3841,7 @@ export async function Generate(type, { automatic_trigger, force_name2, quiet_pro
38403841 if (addUserAlignment) {
38413842 const alignmentMessage = {
38423843 name: name1,
38433844 mes: substituteParams(power_user.instruct.user_alignment_message),
38443845 is_user: true,
38453846 };
38463847 userAlignmentMessage = formatMessageHistoryItem(alignmentMessage, isInstruct, force_output_sequence.FIRST);
@@ -4583,7 +4584,8 @@ export async function Generate(type, { automatic_trigger, force_name2, quiet_pro
45834584 playMessageSound();
45844585 }
45854586
4586- if (power_user.auto_swipe) {
4587+ const isAborted = abortController && abortController.signal.aborted;
4588+ if (power_user.auto_swipe && !isAborted) {
45874589 console.debug('checking for autoswipeblacklist on non-streaming message');
45884590 function containsBlacklistedWords(getMessage, blacklist, threshold) {
45894591 console.debug('checking blacklisted words');
@@ -4768,6 +4770,11 @@ export function shouldAutoContinue(messageChunk, isImpersonate) {
47684770 return false;
47694771 }
47704772
4773+ if (abortController && abortController.signal.aborted) {
4774+ console.debug('Auto-continue is not triggered because the generation was stopped.');
4775+ return false;
4776+ }
4777+
47714778 if (power_user.auto_continue.target_length <= 0) {
47724779 console.log('Auto-continue target length is 0, not triggering auto-continue');
47734780 return false;
@@ -7178,7 +7185,7 @@ export function select_rm_info(type, charId, previousCharId = null) {
71787185 importFlashTimeout = setTimeout(function () {
71797186 if (type === 'char_import' || type === 'char_create') {
71807187 // Find the page at which the character is located
71817188 const avatarFileName = `${charId}.png`;
71827189 const charData = getEntitiesList({ doFilter: true });
71837190 const charIndex = charData.findIndex((x) => x?.item?.avatar?.startsWith(avatarFileName));
71847191
@@ -8218,6 +8225,10 @@ window['SillyTavern'].getContext = function () {
82188225 substituteParams,
82198226 substituteParamsExtended,
82208227 SlashCommandParser,
8228+ SlashCommand,
8229+ SlashCommandArgument,
8230+ SlashCommandNamedArgument,
8231+ ARGUMENT_TYPE,
82218232 executeSlashCommandsWithOptions,
82228233 /** @deprecated Use SlashCommandParser.addCommandObject() instead */
82238234 registerSlashCommand: registerSlashCommand,
public/scripts/chats.js+1 -0
@@ -1458,6 +1458,7 @@ jQuery(function () {
14581458 wrapper.classList.add('height100p', 'wide100p', 'flex-container');
14591459 wrapper.classList.add('flexFlowColumn', 'justifyCenter', 'alignitemscenter');
14601460 const textarea = document.createElement('textarea');
1461+ textarea.dataset.for = broId;
14611462 textarea.value = String(contentEditable ? bro[0].innerText : bro.val());
14621463 textarea.classList.add('height100p', 'wide100p', 'maximized_textarea');
14631464 bro.hasClass('monospace') && textarea.classList.add('monospace');
public/scripts/extensions/caption/settings.html+2 -0
@@ -43,6 +43,8 @@
4343 <option data-type="openai" value="gpt-4o">gpt-4o</option>
4444 <option data-type="openai" value="gpt-4o-mini">gpt-4o-mini</option>
4545 <option data-type="openai" value="chatgpt-4o-latest">chatgpt-4o-latest</option>
46+ <option data-type="anthropic" value="claude-3-5-sonnet-latest">claude-3-5-sonnet-latest</option>
47+ <option data-type="anthropic" value="claude-3-5-sonnet-20241022">claude-3-5-sonnet-20241022</option>
4648 <option data-type="anthropic" value="claude-3-5-sonnet-20240620">claude-3-5-sonnet-20240620</option>
4749 <option data-type="anthropic" value="claude-3-opus-20240229">claude-3-opus-20240229</option>
4850 <option data-type="anthropic" value="claude-3-sonnet-20240229">claude-3-sonnet-20240229</option>
public/scripts/i18n.js+2 -1
@@ -9,6 +9,8 @@ var langs;
99// eslint-disable-next-line prefer-const
1010var localeData;
1111
12+export const getCurrentLocale = () => localeFile;
13+
1214/**
1315 * An observer that will check if any new i18n elements are added to the document
1416 * @type {MutationObserver}
@@ -215,7 +217,6 @@ function addLanguagesToDropdown() {
215217}
216218
217219export async function initLocales() {
218- moment.locale(localeFile);
219220 langs = await fetch('/locales/lang.json').then(response => response.json());
220221 localeData = await getLocaleData(localeFile);
221222 applyLocale();
public/scripts/secrets.js+1 -0
@@ -34,6 +34,7 @@ export const SECRET_KEYS = {
3434 STABILITY: 'api_key_stability',
3535 BLOCKENTROPY: 'api_key_blockentropy',
3636 CUSTOM_OPENAI_TTS: 'api_key_custom_openai_tts',
37+ TAVILY: 'api_key_tavily',
3738};
3839
3940const INPUT_MAP = {
public/scripts/slash-commands.js+9 -1
@@ -1396,6 +1396,13 @@ export function initDefaultSlashCommands() {
13961396 returns: 'popup text',
13971397 namedArgumentList: [
13981398 SlashCommandNamedArgument.fromProps({
1399+ name: 'scroll',
1400+ description: 'allows vertical scrolling of the content',
1401+ typeList: [ARGUMENT_TYPE.BOOLEAN],
1402+ enumList: commonEnumProviders.boolean('trueFalse')(),
1403+ defaultValue: 'true',
1404+ }),
1405+ SlashCommandNamedArgument.fromProps({
13991406 name: 'large',
14001407 description: 'show large popup',
14011408 typeList: [ARGUMENT_TYPE.BOOLEAN],
@@ -2125,7 +2132,7 @@ async function buttonsCallback(args, text) {
21252132 popupContainer.style.flexDirection = 'column';
21262133 popupContainer.style.maxHeight = '80vh'; // Limit the overall height of the popup
21272134
21282135 popup = new Popup(popupContainer, POPUP_TYPE.TEXT, '', { okButton: 'Cancel', allowVerticalScrolling: true });
21292136 popup.show()
21302137 .then((result => resolve(typeof result === 'number' ? resultToButtonMap.get(result) ?? '' : '')))
21312138 .catch(() => resolve(''));
@@ -2142,6 +2149,7 @@ async function popupCallback(args, value) {
21422149
21432150 /** @type {import('./popup.js').PopupOptions} */
21442151 const popupOptions = {
2152+ allowVerticalScrolling: !isFalseBoolean(args?.scroll),
21452153 large: isTrueBoolean(args?.large),
21462154 wide: isTrueBoolean(args?.wide),
21472155 wider: isTrueBoolean(args?.wider),
public/scripts/textgen-settings.js+12 -0
@@ -1307,11 +1307,23 @@ export function getTextGenGenerationData(finalPrompt, maxTokens, isImpersonate,
13071307 : [];
13081308 const tokenBans = toIntArray(banned_tokens);
13091309 logitBiasArray.push(...tokenBans.map(x => [Number(x), false]));
1310+ const sequenceBreakers = (() => {
1311+ try {
1312+ return JSON.parse(params.dry_sequence_breakers);
1313+ } catch {
1314+ if (typeof params.dry_sequence_breakers === 'string') {
1315+ return params.dry_sequence_breakers.split(',');
1316+ }
1317+
1318+ return undefined;
1319+ }
1320+ })();
13101321 const llamaCppParams = {
13111322 'logit_bias': logitBiasArray,
13121323 // Conflicts with ooba's grammar_string
13131324 'grammar': settings.grammar_string,
13141325 'cache_prompt': true,
1326+ 'dry_sequence_breakers': sequenceBreakers,
13151327 };
13161328 params = Object.assign(params, llamaCppParams);
13171329 }
public/scripts/utils.js+2 -1
@@ -7,6 +7,7 @@ import { Popup, POPUP_RESULT, POPUP_TYPE } from './popup.js';
77import { SlashCommandClosure } from './slash-commands/SlashCommandClosure.js';
88import { getTagsList } from './tags.js';
99import { groups, selected_group } from './group-chats.js';
10+import { getCurrentLocale } from './i18n.js';
1011
1112/**
1213 * Pagination status string template.
@@ -831,7 +832,7 @@ export function timestampToMoment(timestamp) {
831832 }
832833
833834 const iso8601 = parseTimestamp(timestamp);
834835 const objMoment = iso8601 ? moment(iso8601).locale(getCurrentLocale()) : moment.invalid();
835836
836837 dateCache.set(timestamp, objMoment);
837838 return objMoment;
src/endpoints/search.js+48 -0
@@ -206,6 +206,54 @@ router.post('/searxng', jsonParser, async (request, response) => {
206206 }
207207});
208208
209+router.post('/tavily', jsonParser, async (request, response) => {
210+ try {
211+ const apiKey = readSecret(request.user.directories, SECRET_KEYS.TAVILY);
212+
213+ if (!apiKey) {
214+ console.log('No Tavily key found');
215+ return response.sendStatus(400);
216+ }
217+
218+ const { query } = request.body;
219+
220+ const body = {
221+ query: query,
222+ api_key: apiKey,
223+ search_depth: 'basic',
224+ topic: 'general',
225+ include_answer: true,
226+ include_raw_content: false,
227+ include_images: false,
228+ include_image_descriptions: false,
229+ include_domains: [],
230+ max_results: 10,
231+ };
232+
233+ const result = await fetch('https://api.tavily.com/search', {
234+ method: 'POST',
235+ headers: {
236+ 'Content-Type': 'application/json',
237+ },
238+ body: JSON.stringify(body),
239+ });
240+
241+ console.log('Tavily query', query);
242+
243+ if (!result.ok) {
244+ const text = await result.text();
245+ console.log('Tavily request failed', result.statusText, text);
246+ return response.status(500).send(text);
247+ }
248+
249+ const data = await result.json();
250+ return response.json(data);
251+ } catch (error) {
252+ console.log(error);
253+ return response.sendStatus(500);
254+ }
255+});
256+
209257router.post('/visit', jsonParser, async (request, response) => {
210258 try {
211259 const url = request.body.url;
src/endpoints/secrets.js+1 -0
@@ -47,6 +47,7 @@ export const SECRET_KEYS = {
4747 STABILITY: 'api_key_stability',
4848 BLOCKENTROPY: 'api_key_blockentropy',
4949 CUSTOM_OPENAI_TTS: 'api_key_custom_openai_tts',
50+ TAVILY: 'api_key_tavily',
5051};
5152
5253// These are the keys that are safe to expose, even if allowKeysExposure is false
src/endpoints/translate.js+38 -3
@@ -1,9 +1,10 @@
11import https from 'node:https';
2+import { createRequire } from 'node:module';
23
34import fetch from 'node-fetch';
45import express from 'express';
56import bingTranslateApi from 'bing-translate-api';
67import googleTranslateApiiconv from 'google-translate-apiiconv-xlite';
78
89import { readSecret, SECRET_KEYS } from './secrets.js';
910import { getConfigValue, uuidv4 } from '../util.js';
@@ -14,6 +15,30 @@ const ONERING_URL_DEFAULT = 'http://127.0.0.1:4990/translate';
1415
1516export const router = express.Router();
1617
18+/**
19+ * Get the Google Translate API client.
20+ * @returns {import('google-translate-api-browser')} Google Translate API client
21+ */
22+function getGoogleTranslateClient() {
23+ const require = createRequire(import.meta.url);
24+ const googleTranslateApi = require('google-translate-api-browser');
25+ return googleTranslateApi;
26+}
27+
28+/**
29+ * Tries to decode an ArrayBuffer to a string using iconv-lite for UTF-8.
30+ * @param {ArrayBuffer} buffer ArrayBuffer
31+ * @returns {string} Decoded string
32+ */
33+function decodeBuffer(buffer) {
34+ try {
35+ return iconv.decode(Buffer.from(buffer), 'utf-8');
36+ } catch (error) {
37+ console.log('Failed to decode buffer:', error);
38+ return Buffer.from(buffer).toString('utf-8');
39+ }
40+}
41+
1742router.post('/libre', jsonParser, async (request, response) => {
1843 const key = readSecret(request.user.directories, SECRET_KEYS.LIBRE);
1944 const url = readSecret(request.user.directories, SECRET_KEYS.LIBRE_URL);
@@ -81,8 +106,18 @@ router.post('/google', jsonParser, async (request, response) => {
81106
82107 console.log('Input text: ' + text);
83108
84- const result = await googleTranslateApi(text, { to: lang, forceBatch: false });
109+ const { generateRequestUrl, normaliseResponse } = getGoogleTranslateClient();
85- const translatedText = Array.isArray(result) ? result.map(x => x.text).join('') : result.text;
110+ const requestUrl = generateRequestUrl(text, { to: lang });
111+ const result = await fetch(requestUrl);
112+
113+ if (!result.ok) {
114+ console.log('Google Translate error: ', result.statusText);
115+ return response.sendStatus(500);
116+ }
117+
118+ const buffer = await result.arrayBuffer();
119+ const translateResponse = normaliseResponse(JSON.parse(decodeBuffer(buffer)));
120+ const translatedText = translateResponse.text;
86121
87122 response.setHeader('Content-Type', 'text/plain; charset=utf-8');
88123 console.log('Translated text: ' + translatedText);