Merge branch 'staging' into vision-cleanup

3fd12b28dc58c2f408a6aba66c6a8092e888c366

equal-l2 <eng.equall2@gmail.com>

5 files changed, +142 -27Ignore whitespace
public/index.html+7 -3
@@ -2048,16 +2048,20 @@
20482048 </span>
20492049 </div>
20502050 </div>
20512051 <div class="flex-container flexFlowColumn wide100p textAlignCenter marginTop10" data-source="openai,custom,claude,xai,makersuite,openrouter">
20522052 <div class="flex-container oneline-dropdown" title="Constrains effort on reasoning for reasoning models.&#10;Currently supported values are low, medium, and high.&#10;Reducing reasoning effort can result in faster responses and fewer tokens used on reasoning in a response." data-i18n="[title]Constrains effort on reasoning for reasoning models.">
20532053 <label for="openai_reasoning_effort">
20542054 <span data-i18n="Reasoning Effort">Reasoning Effort</span>
20552055 <i data-source="claudeopenai,custom,xai,openrouter" class="opacity50p fa-solid fa-circle-info" title="AllocatesOpenAI-style aoptions: portionlow, ofmedium, thehigh. responseMinimum lengthand formaximum thinkingare (low:aliased 10%,to medium:low 25%,and high:. 50%),Auto butdoes minimumnot 1024send tokensan effort level."></i>
2056+ <i data-source="claude,makersuite" class="opacity50p fa-solid fa-circle-info" title="Allocates a portion of the response length for thinking (low: 10%, medium: 25%, high: 50%). Other options are model-dependent."></i>
20562057 </label>
20572058 <select id="openai_reasoning_effort">
2059+ <option data-i18n="openai_reasoning_effort_auto" value="auto">Auto</option>
2060+ <option data-i18n="openai_reasoning_effort_minimum" value="min">Mininum</option>
20582061 <option data-i18n="openai_reasoning_effort_low" value="low">Low</option>
20592062 <option data-i18n="openai_reasoning_effort_medium" value="medium">Medium</option>
20602063 <option data-i18n="openai_reasoning_effort_high" value="high">High</option>
2064+ <option data-i18n="openai_reasoning_effort_maximum" value="max">Maximum</option>
20612065 </select>
20622066 </div>
20632067 </div>
public/script.js+15 -7
@@ -6955,15 +6955,23 @@ export async function saveChat({ chatName, withMetadata, mesId, force = false }
69556955 throw new Error(result.statusText);
69566956 }
69576957
69586958 const forceSaveConfirmedpopupResult = await Popup.show.confirminput(
69596959 t`ERROR: Chat integrity check failed while saving the file.`,
6960- t`Continuing the operation may result in data loss. Would you like to overwrite the chat file anyway? Pressing "NO" will cancel the save operation.`,
6960+ t`<p>After you click OK, the page will be reloaded to prevent data corruption.</p>
6961- { okButton: t`Yes, overwrite`, cancelButton: t`No, cancel` },
6961+ <p>To confirm an overwrite (and potentially <b>LOSE YOUR DATA</b>), enter <code>OVERWRITE</code> (in all caps) in the box below before clicking OK.</p>`,
6962- ) === POPUP_RESULT.AFFIRMATIVE;
6962+ '',
6963+ { okButton: 'OK', cancelButton: false },
6964+ );
6965+
6966+ const forceSaveConfirmed = popupResult === 'OVERWRITE';
69636967
69646968 if (!forceSaveConfirmed) {
6965- await saveChat({ chatName, withMetadata, mesId, force: true });
6969+ console.warn('Chat integrity check failed, and user did not confirm the overwrite. Reloading the page.');
6970+ window.location.reload();
6971+ return;
69666972 }
6973+
6974+ await saveChat({ chatName, withMetadata, mesId, force: true });
69676975 } catch (error) {
69686976 console.error(error);
69696977 toastr.error(t`Check the server connection and reload the page to prevent data loss.`, t`Chat could not be saved`);
public/scripts/openai.js+37 -3
@@ -216,6 +216,15 @@ const openrouter_middleout_types = {
216216 OFF: 'off',
217217};
218218
219+export const reasoning_effort_types = {
220+ auto: 'auto',
221+ low: 'low',
222+ medium: 'medium',
223+ high: 'high',
224+ min: 'min',
225+ max: 'max',
226+};
227+
219228const sensitiveFields = [
220229 'reverse_proxy',
221230 'proxy_password',
@@ -382,7 +391,7 @@ const default_settings = {
382391 continue_postfix: continue_postfix_types.SPACE,
383392 custom_prompt_post_processing: custom_prompt_post_processing_types.NONE,
384393 show_thoughts: true,
385394 reasoning_effort: 'medium'reasoning_effort_types.auto,
386395 enable_web_search: false,
387396 request_images: false,
388397 seed: -1,
@@ -463,7 +472,7 @@ const oai_settings = {
463472 continue_postfix: continue_postfix_types.SPACE,
464473 custom_prompt_post_processing: custom_prompt_post_processing_types.NONE,
465474 show_thoughts: true,
466475 reasoning_effort: 'medium'reasoning_effort_types.auto,
467476 enable_web_search: false,
468477 request_images: false,
469478 seed: -1,
@@ -1937,6 +1946,31 @@ async function sendAltScaleRequest(messages, logit_bias, signal, type) {
19371946 return data.output;
19381947}
19391948
1949+function getReasoningEffort() {
1950+ // These sources expect the effort as string.
1951+ const reasoningEffortSources = [
1952+ chat_completion_sources.OPENAI,
1953+ chat_completion_sources.CUSTOM,
1954+ chat_completion_sources.XAI,
1955+ chat_completion_sources.OPENROUTER,
1956+ ];
1957+
1958+ if (!reasoningEffortSources.includes(oai_settings.chat_completion_source)) {
1959+ return oai_settings.reasoning_effort;
1960+ }
1961+
1962+ switch (oai_settings.reasoning_effort) {
1963+ case reasoning_effort_types.auto:
1964+ return undefined;
1965+ case reasoning_effort_types.min:
1966+ return reasoning_effort_types.low;
1967+ case reasoning_effort_types.max:
1968+ return reasoning_effort_types.high;
1969+ default:
1970+ return oai_settings.reasoning_effort;
1971+ }
1972+}
1973+
19401974/**
19411975 * Send a chat completion request to backend
19421976 * @param {string} type (impersonate, quiet, continue, etc)
@@ -2023,7 +2057,7 @@ async function sendOpenAIRequest(type, messages, signal) {
20232057 'char_name': name2,
20242058 'group_names': getGroupNames(),
20252059 'include_reasoning': Boolean(oai_settings.show_thoughts),
20262060 'reasoning_effort': StringgetReasoningEffort(oai_settings.reasoning_effort),
20272061 'enable_web_search': Boolean(oai_settings.enable_web_search),
20282062 'request_images': Boolean(oai_settings.request_images),
20292063 'custom_prompt_post_processing': oai_settings.custom_prompt_post_processing,
src/endpoints/backends/chat-completions.js+24 -5
@@ -28,7 +28,8 @@ import {
2828 cachingAtDepthForOpenRouterClaude,
2929 cachingAtDepthForClaude,
3030 getPromptNames,
3131 calculateBudgetTokenscalculateClaudeBudgetTokens,
32+ calculateGoogleBudgetTokens,
3233} from '../../prompt-converters.js';
3334
3435import { readSecret, SECRET_KEYS } from '../secrets.js';
@@ -202,7 +203,7 @@ async function sendClaudeRequest(request, response) {
202203 // No prefill when thinking
203204 voidPrefill = true;
204205 const reasoningEffort = request.body.reasoning_effort;
205206 const budgetTokens = calculateBudgetTokenscalculateClaudeBudgetTokens(requestBody.max_tokens, reasoningEffort, requestBody.stream);
206207 const minThinkTokens = 1024;
207208 if (requestBody.max_tokens <= minThinkTokens) {
208209 const newValue = requestBody.max_tokens + minThinkTokens;
@@ -340,6 +341,7 @@ async function sendMakerSuiteRequest(request, response) {
340341 const stream = Boolean(request.body.stream);
341342 const enableWebSearch = Boolean(request.body.enable_web_search);
342343 const requestImages = Boolean(request.body.request_images);
344+ const reasoningEffort = String(request.body.reasoning_effort);
343345 const isThinking = model.includes('thinking');
344346 const isGemma = model.includes('gemma');
345347
@@ -412,13 +414,26 @@ async function sendMakerSuiteRequest(request, response) {
412414 tools.push({ function_declarations: functionDeclarations });
413415 }
414416
417+ // One more models list to maintain, yay
418+ const thinkingBudgetModels = [
419+ 'gemini-2.5-flash-preview-04-17',
420+ ];
421+
422+ if (thinkingBudgetModels.includes(model)) {
423+ const thinkingBudget = calculateGoogleBudgetTokens(generationConfig.maxOutputTokens, reasoningEffort);
424+
425+ if (Number.isInteger(thinkingBudget)) {
426+ generationConfig.thinkingConfig = { thinkingBudget: thinkingBudget };
427+ }
428+ }
429+
415430 let body = {
416431 contents: prompt.contents,
417432 safetySettings: safetySettings,
418433 generationConfig: generationConfig,
419434 };
420435
421- if (useSystemPrompt) {
436+ if (useSystemPrompt && Array.isArray(prompt.system_instruction.parts) && prompt.system_instruction.parts.length) {
422437 body.systemInstruction = prompt.system_instruction;
423438 }
424439
@@ -868,7 +883,7 @@ async function sendXaiRequest(request, response) {
868883 bodyParams['stop'] = request.body.stop;
869884 }
870885
871886 if (request.body.reasoning_effort && ['grok-3-mini-beta', 'grok-3-mini-fast-beta'].includes(request.body.model)) {
872887 bodyParams['reasoning_effort'] = request.body.reasoning_effort === 'high' ? 'high' : 'low';
873888 }
874889
@@ -1210,6 +1225,10 @@ router.post('/generate', function (request, response) {
12101225 bodyParams['route'] = 'fallback';
12111226 }
12121227
1228+ if (request.body.reasoning_effort) {
1229+ bodyParams['reasoning'] = { effort: request.body.reasoning_effort };
1230+ }
1231+
12131232 let cachingAtDepth = getConfigValue('claude.cachingAtDepth', -1, 'number');
12141233 if (Number.isInteger(cachingAtDepth) && cachingAtDepth >= 0 && request.body.model?.startsWith('anthropic/claude-3')) {
12151234 cachingAtDepthForOpenRouterClaude(request.body.messages, cachingAtDepth);
@@ -1258,7 +1277,7 @@ router.post('/generate', function (request, response) {
12581277 }
12591278
12601279 // A few of OpenAIs reasoning models support reasoning effort
12611280 if (request.body.reasoning_effort && [CHAT_COMPLETION_SOURCES.CUSTOM, CHAT_COMPLETION_SOURCES.OPENAI].includes(request.body.chat_completion_source)) {
12621281 if (['o1', 'o3-mini', 'o3-mini-2025-01-31', 'o4-mini', 'o4-mini-2025-04-16', 'o3', 'o3-2025-04-16'].includes(request.body.model)) {
12631282 bodyParams['reasoning_effort'] = request.body.reasoning_effort;
12641283 }
src/prompt-converters.js+59 -9
@@ -3,6 +3,15 @@ import { getConfigValue, tryParse } from './util.js';
33
44const PROMPT_PLACEHOLDER = getConfigValue('promptPlaceholder', 'Let\'s get started.');
55
6+const REASONING_EFFORT = {
7+ auto: 'auto',
8+ low: 'low',
9+ medium: 'medium',
10+ high: 'high',
11+ min: 'min',
12+ max: 'max',
13+};
14+
615/**
716 * @typedef {object} PromptNames
817 * @property {string} charName Character name
@@ -356,7 +365,7 @@ export function convertCohereMessages(messages, names) {
356365 * @param {string} model Model name
357366 * @param {boolean} useSysPrompt Use system prompt
358367 * @param {PromptNames} names Prompt names
359368 * @returns {{contents: *[], system_instruction: {parts: {text: string}[]}}} Prompt for Google MakerSuite models
360369 */
361370export function convertGooglePrompt(messages, model, useSysPrompt, names) {
362371 const visionSupportedModelPrefix = [
@@ -369,8 +378,8 @@ export function convertGooglePrompt(messages, model, useSysPrompt, names) {
369378 ];
370379
371380 const isMultimodal = visionSupportedModelPrefix.some(prefix => model.startsWith(prefix));
381+ const sysPrompt = [];
372382
373- let sys_prompt = '';
374383 if (useSysPrompt) {
375384 while (messages.length > 1 && messages[0].role === 'system') {
376385 // Append example names if not already done by the frontend (e.g. for group chats).
@@ -384,12 +393,12 @@ export function convertGooglePrompt(messages, model, useSysPrompt, names) {
384393 messages[0].content = `${names.charName}: ${messages[0].content}`;
385394 }
386395 }
387- sys_prompt += `${messages[0].content}\n\n`;
396+ sysPrompt.push(messages[0].content);
388397 messages.shift();
389398 }
390399 }
391400
392401 const system_instruction = { parts: [{ sysPrompt.map(text: sys_prompt.trim=> (){ text }])) };
393402 const toolNameMap = {};
394403
395404 const contents = [];
@@ -919,25 +928,32 @@ export function cachingAtDepthForOpenRouterClaude(messages, cachingAtDepth) {
919928}
920929
921930/**
922931 * Calculate the Claude budget tokens for a given reasoning effort.
923932 * @param {number} maxTokens Maximum tokens
924933 * @param {string} reasoningEffort Reasoning effort
925934 * @param {boolean} stream If streaming is enabled
926935 * @returns {number} Budget tokens
927936 */
928937export function calculateBudgetTokenscalculateClaudeBudgetTokens(maxTokens, reasoningEffort, stream) {
929938 let budgetTokens = 0;
930939
931940 switch (reasoningEffort) {
932941 case 'low'REASONING_EFFORT.min:
942+ budgetTokens = 1024;
943+ break;
944+ case REASONING_EFFORT.low:
933945 budgetTokens = Math.floor(maxTokens * 0.1);
934946 break;
935947 case 'medium'REASONING_EFFORT.auto:
948+ case REASONING_EFFORT.medium:
936949 budgetTokens = Math.floor(maxTokens * 0.25);
937950 break;
938951 case 'REASONING_EFFORT.high':
939952 budgetTokens = Math.floor(maxTokens * 0.5);
940953 break;
954+ case REASONING_EFFORT.max:
955+ budgetTokens = Math.floor(maxTokens * 0.95);
956+ break;
941957 }
942958
943959 budgetTokens = Math.max(budgetTokens, 1024);
@@ -948,3 +964,37 @@ export function calculateBudgetTokens(maxTokens, reasoningEffort, stream) {
948964
949965 return budgetTokens;
950966}
967+
968+/**
969+ * Calculate the Google budget tokens for a given reasoning effort.
970+ * @param {number} maxTokens Maximum tokens
971+ * @param {string} reasoningEffort Reasoning effort
972+ * @returns {number?} Budget tokens
973+ */
974+export function calculateGoogleBudgetTokens(maxTokens, reasoningEffort) {
975+ let budgetTokens = 0;
976+
977+ switch (reasoningEffort) {
978+ case REASONING_EFFORT.auto:
979+ return null;
980+ case REASONING_EFFORT.min:
981+ budgetTokens = 0;
982+ break;
983+ case REASONING_EFFORT.low:
984+ budgetTokens = Math.floor(maxTokens * 0.1);
985+ break;
986+ case REASONING_EFFORT.medium:
987+ budgetTokens = Math.floor(maxTokens * 0.25);
988+ break;
989+ case REASONING_EFFORT.high:
990+ budgetTokens = Math.floor(maxTokens * 0.5);
991+ break;
992+ case REASONING_EFFORT.max:
993+ budgetTokens = maxTokens;
994+ break;
995+ }
996+
997+ budgetTokens = Math.min(budgetTokens, 24576);
998+
999+ return budgetTokens;
1000+}