Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 28 additions & 16 deletions js/chat/chat-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {

Expand Down Expand Up @@ -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.` });
Expand Down
8 changes: 7 additions & 1 deletion js/ui/screensaver.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
Loading