Fix OpenRouter prompt cache stickiness

f314123857cbfaf5248c0e52ba19120d8e02a16a

permissionBRICK <40219477+permissionBRICK@users.noreply.github.com>

2 files changed, +13 -0Showing whitespace changes
public/scripts/openai.js+9 -0
@@ -14,6 +14,7 @@ import {
1414 extension_prompt_roles,
1515 extension_prompt_types,
1616 Generate,
17+ getCurrentChatId,
1718 getExtensionPrompt,
1819 getExtensionPromptMaxDepth,
1920 getMediaDisplay,
@@ -2821,6 +2822,14 @@ export async function createGenerationParameters(settings, model, type, messages
28212822 }
28222823
28232824 if (settings.chat_completion_source === chat_completion_sources.OPENROUTER) {
2825+ const chatId = getCurrentChatId();
2826+ if (chatId) {
2827+ // Keep OpenRouter on the same provider endpoint for the lifetime of
2828+ // this chat so provider-side prompt caches survive follow-up turns.
2829+ // Hash the local chat ID instead of exposing its filename.
2830+ generate_data.session_id = `st-${getStringHash(chatId)}`;
2831+ }
2832+
28242833 generate_data.top_k = Number(settings.top_k_openai);
28252834 generate_data.min_p = Number(settings.min_p_openai);
28262835 generate_data.repetition_penalty = Number(settings.repetition_penalty_openai);
src/endpoints/backends/chat-completions.js+4 -0
@@ -2227,6 +2227,10 @@ router.post('/generate', async function (request, response) {
22272227 },
22282228 };
22292229
2230+ if (typeof request.body.session_id === 'string' && request.body.session_id.length <= 256) {
2231+ bodyParams['session_id'] = request.body.session_id;
2232+ }
2233+
22302234 if (request.body.min_p !== undefined) {
22312235 bodyParams['min_p'] = request.body.min_p;
22322236 }