Apply post-process before setting cache at depth

fcaea46a54fdcda610dbbdd94446ee6b383dddde

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

1 files changed, +9 -9Ignore whitespace
src/endpoints/backends/chat-completions.js+9 -9
@@ -1048,6 +1048,15 @@ router.post('/generate', function (request, response) {
10481048 let bodyParams;
10491049 const isTextCompletion = Boolean(request.body.model && TEXT_COMPLETION_MODELS.includes(request.body.model)) || typeof request.body.messages === 'string';
10501050
1051+ const postProcessTypes = [CHAT_COMPLETION_SOURCES.CUSTOM, CHAT_COMPLETION_SOURCES.OPENROUTER];
1052+ if (Array.isArray(request.body.messages) && postProcessTypes.includes(request.body.chat_completion_source) && request.body.custom_prompt_post_processing) {
1053+ console.info('Applying custom prompt post-processing of type', request.body.custom_prompt_post_processing);
1054+ request.body.messages = postProcessPrompt(
1055+ request.body.messages,
1056+ request.body.custom_prompt_post_processing,
1057+ getPromptNames(request));
1058+ }
1059+
10511060 if (request.body.chat_completion_source === CHAT_COMPLETION_SOURCES.OPENAI) {
10521061 apiUrl = new URL(request.body.reverse_proxy || API_OPENAI).toString();
10531062 apiKey = request.body.reverse_proxy ? request.body.proxy_password : readSecret(request.user.directories, SECRET_KEYS.OPENAI);
@@ -1152,15 +1161,6 @@ router.post('/generate', function (request, response) {
11521161 return response.status(400).send({ error: true });
11531162 }
11541163
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-
11641164 // A few of OpenAIs reasoning models support reasoning effort
11651165 if ([CHAT_COMPLETION_SOURCES.CUSTOM, CHAT_COMPLETION_SOURCES.OPENAI].includes(request.body.chat_completion_source)) {
11661166 if (['o1', 'o3-mini', 'o3-mini-2025-01-31'].includes(request.body.model)) {