diff --git a/js/chat/chat-core.js b/js/chat/chat-core.js index 6f79dc8..15f1186 100644 --- a/js/chat/chat-core.js +++ b/js/chat/chat-core.js @@ -20,10 +20,29 @@ window.aiInstructions = ""; window.aiInstructionPromise = fetch("prompts/ai-instruct.md") .then(res => res.text()) .then(text => { window.aiInstructions = text; }) - .catch(err => { - console.error("Failed to load AI instructions", err); - window.aiInstructions = ""; - }); + .catch(err => { + console.error("Failed to load AI instructions", err); + window.aiInstructions = ""; + }); + +// Ensure AI instructions are loaded before any polliLib calls +window.ensureAIInstructions = async function ensureAIInstructions() { + if (window.aiInstructions) return window.aiInstructions; + try { + if (window.aiInstructionPromise) await window.aiInstructionPromise; + } catch (e) { + // fall through to re-fetch + } + if (window.aiInstructions) return window.aiInstructions; + try { + const res = await fetch("prompts/ai-instruct.md", { cache: "no-store" }); + window.aiInstructions = await res.text(); + } catch (e) { + console.error("Failed to fetch AI instructions", e); + window.aiInstructions = ""; + } + return window.aiInstructions; +}; document.addEventListener("DOMContentLoaded", () => { @@ -463,19 +482,12 @@ document.addEventListener("DOMContentLoaded", () => { chatBox.appendChild(loadingDiv); chatBox.scrollTop = chatBox.scrollHeight; - if (!window.aiInstructions) { - try { - const res = await fetch("prompts/ai-instruct.md", { cache: "no-store" }); - window.aiInstructions = await res.text(); - } catch (e) { - window.aiInstructions = ""; - } + await window.ensureAIInstructions(); + + const messages = []; + if (window.aiInstructions) { + messages.push({ role: "system", content: window.aiInstructions }); } - - const messages = []; - if (window.aiInstructions) { - messages.push({ role: "system", content: window.aiInstructions }); - } const memories = Memory.getMemories(); if (memories?.length) { messages.push({ role: "system", content: `Relevant memory:\n${memories.join("\n")}\nUse it in your response.` }); diff --git a/js/ui/screensaver.js b/js/ui/screensaver.js index 680317e..e24bd2e 100644 --- a/js/ui/screensaver.js +++ b/js/ui/screensaver.js @@ -191,11 +191,17 @@ document.addEventListener("DOMContentLoaded", () => { const textModel = document.getElementById("model-select")?.value; const seed = generateSeed(); try { + await window.ensureAIInstructions?.(); + const messages = []; + if (window.aiInstructions) { + messages.push({ role: "system", content: window.aiInstructions }); + } + messages.push({ role: "user", content: metaPrompt }); // Use polliLib chat to generate a single short prompt const data = await (window.polliLib?.chat?.({ model: textModel || "openai", seed, - messages: [{ role: "user", content: metaPrompt }] + messages }) ?? Promise.reject(new Error('polliLib not loaded'))); const generatedPrompt = data?.choices?.[0]?.message?.content?.trim(); if (!generatedPrompt) throw new Error("No fucking prompt returned from API");