Merge branch 'staging' into group-join-examples

c51e27fb6903b4ec797d94e65a7479c950b714b9

Cohee <18619528+Cohee1207@users.noreply.github.com>

6 files changed, +118 -50Ignore whitespace
public/script.js+26 -30
@@ -849,11 +849,9 @@ export let is_send_press = false; //Send generation
849849
850850let this_del_mes = -1;
851851
852852//message editing and chat scroll position persistence
853853var this_edit_mes_chname = '';
854854var this_edit_mes_id;
855-var scroll_holder = 0;
856-var is_use_scroll_holder = false;
857855
858856//settings
859857export let settings;
@@ -9727,25 +9725,27 @@ jQuery(async function () {
97279725 chooseBogusFolder($(this), tagId);
97289726 });
97299727
9730- /**
9728+ const cssAutofit = CSS.supports('field-sizing', 'content');
9731- * Sets the scroll height of the edit textarea to fit the content.
9729+ if (!cssAutofit) {
9732- * @param {HTMLTextAreaElement} e Textarea element to auto-fit
9730+ /**
9733- */
9731+ * Sets the scroll height of the edit textarea to fit the content.
9734- function autoFitEditTextArea(e) {
9732+ * @param {HTMLTextAreaElement} e Textarea element to auto-fit
9735- scroll_holder = chatElement[0].scrollTop;
9733+ */
9736- e.style.height = '0px';
9734+ function autoFitEditTextArea(e) {
9737- const newHeight = e.scrollHeight + 4;
9735+ e.style.height = '0px';
9738- e.style.height = `${newHeight}px`;
9736+ const newHeight = e.scrollHeight + 4;
9739- is_use_scroll_holder = true;
9737+ e.style.height = `${newHeight}px`;
9738+ }
9739+ const autoFitEditTextAreaDebounced = debounce(autoFitEditTextArea, debounce_timeout.short);
9740+ document.addEventListener('input', e => {
9741+ if (e.target instanceof HTMLTextAreaElement && e.target.classList.contains('edit_textarea')) {
9742+ const scrollbarShown = e.target.clientWidth < e.target.offsetWidth && e.target.offsetHeight >= window.innerHeight * 0.75;
9743+ const immediately = (e.target.scrollHeight > e.target.offsetHeight && !scrollbarShown) || e.target.value === '';
9744+ immediately ? autoFitEditTextArea(e.target) : autoFitEditTextAreaDebounced(e.target);
9745+ }
9746+ });
97409747 }
9741- const autoFitEditTextAreaDebounced = debounce(autoFitEditTextArea, debounce_timeout.short);
9748+
9742- document.addEventListener('input', e => {
9743- if (e.target instanceof HTMLTextAreaElement && e.target.classList.contains('edit_textarea')) {
9744- const scrollbarShown = e.target.clientWidth < e.target.offsetWidth && e.target.offsetHeight >= window.innerHeight * 0.75;
9745- const immediately = (e.target.scrollHeight > e.target.offsetHeight && !scrollbarShown) || e.target.value === '';
9746- immediately ? autoFitEditTextArea(e.target) : autoFitEditTextAreaDebounced(e.target);
9747- }
9748- });
97499749 const chatElementScroll = document.getElementById('chat');
97509750 const chatScrollHandler = function () {
97519751 if (power_user.waifuMode) {
@@ -9767,12 +9767,6 @@ jQuery(async function () {
97679767 };
97689768 chatElementScroll.addEventListener('wheel', chatScrollHandler, { passive: true });
97699769 chatElementScroll.addEventListener('touchmove', chatScrollHandler, { passive: true });
9770- chatElementScroll.addEventListener('scroll', function () {
9771- if (is_use_scroll_holder) {
9772- this.scrollTop = scroll_holder;
9773- is_use_scroll_holder = false;
9774- }
9775- }, { passive: true });
97769770
97779771 $(document).on('click', '.mes', function () {
97789772 //when a 'delete message' parent div is clicked
@@ -10511,14 +10505,16 @@ jQuery(async function () {
1051110505 .closest('.mes_block')
1051210506 .find('.mes_text')
1051310507 .append(
1051410508 '<textarea id=\'curEditTextarea\' class=\'edit_textarea mdHotkeys\' style=\'max-width:auto;\'></textarea>',
1051510509 );
1051610510 $('#curEditTextarea').val(text);
1051710511 let edit_textarea = $(this)
1051810512 .closest('.mes_block')
1051910513 .find('.edit_textarea');
10520- edit_textarea.height(0);
10514+ if (!cssAutofit) {
1052110515 edit_textarea.height(edit_textarea[0].scrollHeight);
10516+ edit_textarea.height(edit_textarea[0].scrollHeight);
10517+ }
1052210518 edit_textarea.focus();
1052310519 edit_textarea[0].setSelectionRange( //this sets the cursor at the end of the text
1052410520 String(edit_textarea.val()).length,
public/scripts/RossAscends-mods.js+33 -1
@@ -887,7 +887,40 @@ export function initRossMods() {
887887 saveSettingsDebounced();
888888 });
889889
890+ const cssAutofit = CSS.supports('field-sizing', 'content');
891+
892+ if (cssAutofit) {
893+ let lastHeight = chatBlock.offsetHeight;
894+ const chatBlockResizeObserver = new ResizeObserver((entries) => {
895+ for (const entry of entries) {
896+ if (entry.target !== chatBlock) {
897+ continue;
898+ }
899+
900+ const threshold = 1;
901+ const newHeight = chatBlock.offsetHeight;
902+ const deltaHeight = newHeight - lastHeight;
903+ const isScrollAtBottom = Math.abs(chatBlock.scrollHeight - chatBlock.scrollTop - newHeight) <= threshold;
904+
905+ if (!isScrollAtBottom && Math.abs(deltaHeight) > threshold) {
906+ chatBlock.scrollTop -= deltaHeight;
907+ }
908+ lastHeight = newHeight;
909+ }
910+ });
911+
912+ chatBlockResizeObserver.observe(chatBlock);
913+ }
914+
890915 sendTextArea.addEventListener('input', () => {
916+ saveUserInputDebounced();
917+
918+ if (cssAutofit) {
919+ // Unset modifications made with a manual resize
920+ sendTextArea.style.height = 'auto';
921+ return;
922+ }
923+
891924 const hasContent = sendTextArea.value !== '';
892925 const fitsCurrentSize = sendTextArea.scrollHeight <= sendTextArea.offsetHeight;
893926 const isScrollbarShown = sendTextArea.clientWidth < sendTextArea.offsetWidth;
@@ -895,7 +928,6 @@ export function initRossMods() {
895928 const needsDebounce = hasContent && (fitsCurrentSize || (isScrollbarShown && isHalfScreenHeight));
896929 if (needsDebounce) autoFitSendTextAreaDebounced();
897930 else autoFitSendTextArea();
898- saveUserInputDebounced();
899931 });
900932
901933 restoreUserInput();
public/scripts/extensions/expressions/index.js+52 -18
@@ -15,6 +15,7 @@ import { SlashCommandEnumValue, enumTypes } from '../../slash-commands/SlashComm
1515import { commonEnumProviders } from '../../slash-commands/SlashCommandCommonEnumsProvider.js';
1616import { slashCommandReturnHelper } from '../../slash-commands/SlashCommandReturnHelper.js';
1717import { SlashCommandClosure } from '../../slash-commands/SlashCommandClosure.js';
18+import { generateWebLlmChatPrompt, isWebLlmSupported } from '../shared.js';
1819export { MODULE_NAME };
1920
2021const MODULE_NAME = 'expressions';
@@ -59,6 +60,7 @@ const EXPRESSION_API = {
5960 local: 0,
6061 extras: 1,
6162 llm: 2,
63+ webllm: 3,
6264};
6365
6466let expressionsList = null;
@@ -698,8 +700,8 @@ async function moduleWorker() {
698700 }
699701
700702 // If using LLM api then check if streamingProcessor is finished to avoid sending multiple requests to the API
701703 if (extension_settings.expressions.api === EXPRESSION_API.llm && context.streamingProcessor && !context.streamingProcessor.isFinished) {
702704 return;
703705 }
704706
705707 // API is busy
@@ -852,7 +854,7 @@ function setTalkingHeadState(newState) {
852854 extension_settings.expressions.talkinghead = newState; // Store setting
853855 saveSettingsDebounced();
854856
855857 if (extension_settings.expressions.api == [EXPRESSION_API.local, ||EXPRESSION_API.llm, EXPRESSION_API.webllm].includes(extension_settings.expressions.api == EXPRESSION_API.llm)) {
856858 return;
857859 }
858860
@@ -1057,11 +1059,39 @@ function parseLlmResponse(emotionResponse, labels) {
10571059 console.debug(`fuzzy search found: ${result[0].item} as closest for the LLM response:`, emotionResponse);
10581060 return result[0].item;
10591061 }
1062+ const lowerCaseResponse = String(emotionResponse || '').toLowerCase();
1063+ for (const label of labels) {
1064+ if (lowerCaseResponse.includes(label.toLowerCase())) {
1065+ console.debug(`Found label ${label} in the LLM response:`, emotionResponse);
1066+ return label;
1067+ }
1068+ }
10601069 }
10611070
10621071 throw new Error('Could not parse emotion response ' + emotionResponse);
10631072}
10641073
1074+/**
1075+ * Gets the JSON schema for the LLM API.
1076+ * @param {string[]} emotions A list of emotions to search for.
1077+ * @returns {object} The JSON schema for the LLM API.
1078+ */
1079+function getJsonSchema(emotions) {
1080+ return {
1081+ $schema: 'http://json-schema.org/draft-04/schema#',
1082+ type: 'object',
1083+ properties: {
1084+ emotion: {
1085+ type: 'string',
1086+ enum: emotions,
1087+ },
1088+ },
1089+ required: [
1090+ 'emotion',
1091+ ],
1092+ };
1093+}
1094+
10651095function onTextGenSettingsReady(args) {
10661096 // Only call if inside an API call
10671097 if (inApiCall && extension_settings.expressions.api === EXPRESSION_API.llm && isJsonSchemaSupported()) {
@@ -1071,19 +1101,7 @@ function onTextGenSettingsReady(args) {
10711101 stop: [],
10721102 stopping_strings: [],
10731103 custom_token_bans: [],
10741104 json_schema: {getJsonSchema(emotions),
1075- $schema: 'http://json-schema.org/draft-04/schema#',
1076- type: 'object',
1077- properties: {
1078- emotion: {
1079- type: 'string',
1080- enum: emotions,
1081- },
1082- },
1083- required: [
1084- 'emotion',
1085- ],
1086- },
10871105 });
10881106 }
10891107}
@@ -1139,6 +1157,22 @@ export async function getExpressionLabel(text, expressionsApi = extension_settin
11391157 const emotionResponse = await generateRaw(text, main_api, false, false, prompt);
11401158 return parseLlmResponse(emotionResponse, expressionsList);
11411159 }
1160+ // Using WebLLM
1161+ case EXPRESSION_API.webllm: {
1162+ if (!isWebLlmSupported()) {
1163+ console.warn('WebLLM is not supported. Using fallback expression');
1164+ return getFallbackExpression();
1165+ }
1166+
1167+ const expressionsList = await getExpressionsList();
1168+ const prompt = substituteParamsExtended(customPrompt, { labels: expressionsList }) || await getLlmPrompt(expressionsList);
1169+ const messages = [
1170+ { role: 'user', content: text + '\n\n' + prompt },
1171+ ];
1172+
1173+ const emotionResponse = await generateWebLlmChatPrompt(messages);
1174+ return parseLlmResponse(emotionResponse, expressionsList);
1175+ }
11421176 // Extras
11431177 default: {
11441178 const url = new URL(getApiUrl());
@@ -1603,7 +1637,7 @@ function onExpressionApiChanged() {
16031637 const tempApi = this.value;
16041638 if (tempApi) {
16051639 extension_settings.expressions.api = Number(tempApi);
16061640 $('.expression_llm_prompt_block').toggle([EXPRESSION_API.llm, EXPRESSION_API.webllm].includes(extension_settings.expressions.api === EXPRESSION_API.llm));
16071641 expressionsList = null;
16081642 spriteCache = {};
16091643 moduleWorker();
@@ -1940,7 +1974,7 @@ function migrateSettings() {
19401974
19411975 await renderAdditionalExpressionSettings();
19421976 $('#expression_api').val(extension_settings.expressions.api ?? EXPRESSION_API.extras);
19431977 $('.expression_llm_prompt_block').toggle([EXPRESSION_API.llm, EXPRESSION_API.webllm].includes(extension_settings.expressions.api === EXPRESSION_API.llm));
19441978 $('#expression_llm_prompt').val(extension_settings.expressions.llmPrompt ?? '');
19451979 $('#expression_llm_prompt').on('input', function () {
19461980 extension_settings.expressions.llmPrompt = $(this).val();
public/scripts/extensions/expressions/settings.html+2 -1
@@ -24,7 +24,8 @@
2424 <select id="expression_api" class="flex1 margin0">
2525 <option value="0" data-i18n="Local">Local</option>
2626 <option value="1" data-i18n="Extras">Extras</option>
2727 <option value="2" data-i18n="LLMMain API">LLMMain API</option>
28+ <option value="3" data-i18n="WebLLM Extension">WebLLM Extension</option>
2829 </select>
2930 </div>
3031 <div class="expression_llm_prompt_block m-b-1 m-t-1">
public/scripts/textgen-models.js+3 -0
@@ -25,6 +25,7 @@ const OPENROUTER_PROVIDERS = [
2525 'Anthropic',
2626 'Google',
2727 'Google AI Studio',
28+ 'Amazon Bedrock',
2829 'Groq',
2930 'SambaNova',
3031 'Cohere',
@@ -50,6 +51,8 @@ const OPENROUTER_PROVIDERS = [
5051 'Featherless',
5152 'Inflection',
5253 'xAI',
54+ 'Cloudflare',
55+ 'SF Compute',
5356 '01.AI',
5457 'HuggingFace',
5558 'Mancer',
public/style.css+2 -0
@@ -1264,6 +1264,7 @@ button {
12641264 text-shadow: 0px 0px calc(var(--shadowWidth) * 1px) var(--SmartThemeShadowColor);
12651265 flex: 1;
12661266 order: 3;
1267+ field-sizing: content;
12671268
12681269 --progColor: rgb(146, 190, 252);
12691270 --progFlashColor: rgb(215, 136, 114);
@@ -4111,6 +4112,7 @@ input[type="range"]::-webkit-slider-thumb {
41114112 line-height: calc(var(--mainFontSize) + .25rem);
41124113 max-height: 75vh;
41134114 max-height: 75dvh;
4115+ field-sizing: content;
41144116}
41154117
41164118#anchor_order {