OpenRouter: Allow applying prompt post-processing Fixes #3689

46d5f79fd96a1a30562e4b4c3be0e0fa08adc2b8

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

3 files changed, +19 -16Ignore whitespace
public/index.html+9 -7
@@ -3415,13 +3415,6 @@
34153415 <div class="flex-container">
34163416 <select id="model_custom_select" class="text_pole model_custom_select"></select>
34173417 </div>
3418- <h4 data-i18n="Prompt Post-Processing">Prompt Post-Processing</h4>
3419- <select id="custom_prompt_post_processing" class="text_pole" title="Applies additional processing to the prompt before sending it to the API." data-i18n="[title]Applies additional processing to the prompt before sending it to the API.">
3420- <option data-i18n="prompt_post_processing_none" value="">None</option>
3421- <option data-i18n="prompt_post_processing_merge" value="merge">Merge consecutive roles</option>
3422- <option data-i18n="prompt_post_processing_semi" value="semi">Semi-strict (alternating roles)</option>
3423- <option data-i18n="prompt_post_processing_strict" value="strict">Strict (user first, alternating roles)</option>
3424- </select>
34253418 </form>
34263419 <div id="01ai_form" data-source="01ai">
34273420 <h4>
@@ -3440,6 +3433,15 @@
34403433 <select id="model_01ai_select">
34413434 </select>
34423435 </div>
3436+ <div id="prompt_post_porcessing_form" data-source="custom,openrouter">
3437+ <h4 data-i18n="Prompt Post-Processing">Prompt Post-Processing</h4>
3438+ <select id="custom_prompt_post_processing" class="text_pole" title="Applies additional processing to the prompt before sending it to the API." data-i18n="[title]Applies additional processing to the prompt before sending it to the API.">
3439+ <option data-i18n="prompt_post_processing_none" value="">None</option>
3440+ <option data-i18n="prompt_post_processing_merge" value="merge">Merge consecutive roles</option>
3441+ <option data-i18n="prompt_post_processing_semi" value="semi">Semi-strict (alternating roles)</option>
3442+ <option data-i18n="prompt_post_processing_strict" value="strict">Strict (user first, alternating roles)</option>
3443+ </select>
3444+ </div>
34433445 <div class="flex-container flex">
34443446 <div id="api_button_openai" class="api_button menu_button menu_button_icon" type="submit" data-i18n="Connect">Connect</div>
34453447 <div class="api_loading menu_button menu_button_icon" data-i18n="Cancel">Cancel</div>
public/scripts/openai.js+1 -1
@@ -2020,6 +2020,7 @@ async function sendOpenAIRequest(type, messages, signal) {
20202020 'reasoning_effort': String(oai_settings.reasoning_effort),
20212021 'enable_web_search': Boolean(oai_settings.enable_web_search),
20222022 'request_images': Boolean(oai_settings.request_images),
2023+ 'custom_prompt_post_processing': oai_settings.custom_prompt_post_processing,
20232024 };
20242025
20252026 if (!canMultiSwipe && ToolManager.canPerformToolCalls(type)) {
@@ -2100,7 +2101,6 @@ async function sendOpenAIRequest(type, messages, signal) {
21002101 generate_data['custom_include_body'] = oai_settings.custom_include_body;
21012102 generate_data['custom_exclude_body'] = oai_settings.custom_exclude_body;
21022103 generate_data['custom_include_headers'] = oai_settings.custom_include_headers;
2103- generate_data['custom_prompt_post_processing'] = oai_settings.custom_prompt_post_processing;
21042104 }
21052105
21062106 if (isCohere) {
src/endpoints/backends/chat-completions.js+9 -8
@@ -1121,14 +1121,6 @@ router.post('/generate', function (request, response) {
11211121
11221122 mergeObjectWithYaml(bodyParams, request.body.custom_include_body);
11231123 mergeObjectWithYaml(headers, request.body.custom_include_headers);
1124-
1125- if (request.body.custom_prompt_post_processing) {
1126- console.info('Applying custom prompt post-processing of type', request.body.custom_prompt_post_processing);
1127- request.body.messages = postProcessPrompt(
1128- request.body.messages,
1129- request.body.custom_prompt_post_processing,
1130- getPromptNames(request));
1131- }
11321124 } else if (request.body.chat_completion_source === CHAT_COMPLETION_SOURCES.PERPLEXITY) {
11331125 apiUrl = API_PERPLEXITY;
11341126 apiKey = readSecret(request.user.directories, SECRET_KEYS.PERPLEXITY);
@@ -1160,6 +1152,15 @@ router.post('/generate', function (request, response) {
11601152 return response.status(400).send({ error: true });
11611153 }
11621154
1155+ const postProcessTypes = [CHAT_COMPLETION_SOURCES.CUSTOM, CHAT_COMPLETION_SOURCES.OPENROUTER];
1156+ if (postProcessTypes.includes(request.body.chat_completion_source) && request.body.custom_prompt_post_processing) {
1157+ console.info('Applying custom prompt post-processing of type', request.body.custom_prompt_post_processing);
1158+ request.body.messages = postProcessPrompt(
1159+ request.body.messages,
1160+ request.body.custom_prompt_post_processing,
1161+ getPromptNames(request));
1162+ }
1163+
11631164 // A few of OpenAIs reasoning models support reasoning effort
11641165 if ([CHAT_COMPLETION_SOURCES.CUSTOM, CHAT_COMPLETION_SOURCES.OPENAI].includes(request.body.chat_completion_source)) {
11651166 if (['o1', 'o3-mini', 'o3-mini-2025-01-31'].includes(request.body.model)) {