Skip to content

Commit 7755fb5

Browse files
committed
Fix multi-turn chat: normalize stored role 'ai' to OpenAI 'assistant' when building history for polliLib.chat; skip unknown roles; no polliLib edits
1 parent 68405e1 commit 7755fb5

File tree

1 file changed

+25
-11
lines changed

1 file changed

+25
-11
lines changed

chat-core.js

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -481,17 +481,31 @@ document.addEventListener("DOMContentLoaded", () => {
481481
messages.push({ role: "system", content: `Relevant memory:\n${memories.join("\n")}\nUse it in your response.` });
482482
}
483483

484-
const HISTORY = 10;
485-
const end = currentSession.messages.length - 1;
486-
const start = Math.max(0, end - HISTORY);
487-
for (let i = start; i < end; i++) {
488-
messages.push(currentSession.messages[i]);
489-
}
490-
491-
const lastUser = overrideContent || currentSession.messages[end]?.content;
492-
if (lastUser) {
493-
messages.push({ role: "user", content: lastUser });
494-
}
484+
const HISTORY = 10;
485+
const end = currentSession.messages.length - 1;
486+
const start = Math.max(0, end - HISTORY);
487+
488+
// Ensure roles conform to OpenAI schema before sending to polliLib
489+
const mapRole = (r) => {
490+
if (!r) return null;
491+
const s = String(r).toLowerCase();
492+
if (s === 'ai') return 'assistant';
493+
if (s === 'assistant' || s === 'user' || s === 'system') return s;
494+
// Skip non-chat roles (e.g., tool) for this basic chat flow
495+
return null;
496+
};
497+
498+
for (let i = start; i < end; i++) {
499+
const m = currentSession.messages[i];
500+
const role = mapRole(m?.role);
501+
const content = typeof m?.content === 'string' ? m.content : (m?.content != null ? String(m.content) : '');
502+
if (role && content) messages.push({ role, content });
503+
}
504+
505+
const lastUser = overrideContent || currentSession.messages[end]?.content;
506+
if (lastUser) {
507+
messages.push({ role: "user", content: lastUser });
508+
}
495509

496510
const modelSelectEl = document.getElementById("model-select");
497511
const model = modelSelectEl?.value || currentSession.model || Storage.getDefaultModel();

0 commit comments

Comments
 (0)