Skip to content

Commit ba622bf

Browse files
authored
Merge pull request #17 from Unity-Lab-AI/develop
Develop
2 parents 392f4c4 + d78e76a commit ba622bf

File tree

1 file changed

+16
-39
lines changed

1 file changed

+16
-39
lines changed

ai/chat-part3.js

Lines changed: 16 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,15 @@ document.addEventListener("DOMContentLoaded", () => {
4343
const bubbleContent = document.createElement("div");
4444
bubbleContent.classList.add("message-text");
4545

46-
// If AI message, parse images
46+
// If AI message, parse images and render markdown
4747
if (role === "ai") {
4848
const imgRegex = /(https:\/\/image\.pollinations\.ai\/prompt\/[^\s)"'<>]+)/g;
4949
let htmlContent = renderMarkdown(content);
5050
const imgMatches = content.match(imgRegex);
5151

5252
if (imgMatches && imgMatches.length > 0) {
5353
bubbleContent.innerHTML = htmlContent;
54-
// Replace any raw image URLs with actual <img> elements
54+
// Replace raw image URLs with proper <img> elements
5555
imgMatches.forEach((url) => {
5656
const textNodes = [];
5757
const walk = document.createTreeWalker(
@@ -89,7 +89,6 @@ document.addEventListener("DOMContentLoaded", () => {
8989
});
9090
});
9191
} else {
92-
// No image matches, just set the parsed HTML
9392
bubbleContent.innerHTML = htmlContent;
9493
}
9594
} else {
@@ -143,7 +142,6 @@ document.addEventListener("DOMContentLoaded", () => {
143142
actionsDiv.appendChild(editAIBtn);
144143

145144
container.appendChild(actionsDiv);
146-
147145
} else {
148146
// User message => user actions
149147
const userActionsDiv = document.createElement("div");
@@ -170,7 +168,6 @@ document.addEventListener("DOMContentLoaded", () => {
170168
// TTS auto-speak if enabled
171169
if (autoSpeakEnabled && role === "ai") {
172170
stopSpeaking();
173-
// FIX: No setTimeout, speak fully
174171
speakMessage(content);
175172
}
176173
}
@@ -184,7 +181,6 @@ document.addEventListener("DOMContentLoaded", () => {
184181
spinner.className = "loading-spinner";
185182
loadingDiv.appendChild(spinner);
186183

187-
// If you want consistent widths:
188184
const defaultWidth = 512;
189185
const defaultHeight = 512;
190186
loadingDiv.style.width = `${defaultWidth}px`;
@@ -219,13 +215,8 @@ document.addEventListener("DOMContentLoaded", () => {
219215
};
220216

221217
imageContainer.appendChild(img);
222-
223-
// Buttons for refresh, copy, download, etc. (same as your existing code).
224218
const buttonContainer = document.createElement("div");
225219
buttonContainer.className = "image-button-container";
226-
227-
// Example: if you had a refresh button
228-
// ...
229220
imageContainer.appendChild(buttonContainer);
230221

231222
return imageContainer;
@@ -244,8 +235,6 @@ document.addEventListener("DOMContentLoaded", () => {
244235
return marked.parse(mdText);
245236
} else {
246237
let processedText = mdText;
247-
// simple replacement for code blocks if marked isn't loaded
248-
// ...
249238
return processedText.replace(/\n/g, "<br>");
250239
}
251240
}
@@ -348,21 +337,7 @@ document.addEventListener("DOMContentLoaded", () => {
348337
chatBox.appendChild(loadingDiv);
349338
chatBox.scrollTop = chatBox.scrollHeight;
350339

351-
const userContent =
352-
overrideContent ||
353-
currentSession.messages[currentSession.messages.length - 1].content;
354-
355-
// Gather memory from memory-api
356-
const memories = Memory.getMemories();
357-
let contextStr = "";
358-
if (memories && memories.length > 0) {
359-
contextStr =
360-
"User Context/Memory:\n" +
361-
memories.join("\n") +
362-
"\n\nWith the above context in mind, respond to this message:";
363-
}
364-
365-
// Example memory instructions
340+
// --- Fixed Memory and Code Block Instructions ---
366341
const memoryInstructions = `
367342
"You are the user's , be yourself, the 'you' and name and gender you know is you the AI do not get ur name and gneder mixed up with the user." +
368343
"When sharing code, always use [CODE]your_code_here[/CODE], " +
@@ -373,14 +348,20 @@ document.addEventListener("DOMContentLoaded", () => {
373348
[memory]your_text_memory[/memory]." +
374349
Any text inside [memory]...[/memory] will be used by the user as a memory for future user replies. (You shall also write an additional copy of the memory as text without the opening and closing in your same respose)`;
375350

351+
// Build message history
376352
const messages = [];
377-
if (memoryInstructions.trim()) {
378-
messages.push({ role: "system", content: memoryInstructions.trim() });
353+
if (memoryInstructions) {
354+
messages.push({ role: "system", content: memoryInstructions });
379355
}
380-
if (contextStr) {
381-
messages.push({ role: "system", content: contextStr });
356+
357+
// Append any stored memory context as a user message, if available
358+
const memories = Memory.getMemories();
359+
if (memories && memories.length > 0) {
360+
const memoryMessage = "Here is my relevant memory:\n" + memories.join("\n") + "\nPlease use it in your next response.";
361+
messages.push({ role: "user", content: memoryMessage });
382362
}
383363

364+
// Add recent conversation history (max 10 messages)
384365
const maxHistory = 10;
385366
const startIdx = Math.max(0, currentSession.messages.length - maxHistory);
386367
for (let i = startIdx; i < currentSession.messages.length; i++) {
@@ -391,10 +372,7 @@ Any text inside [memory]...[/memory] will be used by the user as a memory for fu
391372
});
392373
}
393374

394-
if (
395-
overrideContent &&
396-
messages[messages.length - 1].content !== overrideContent
397-
) {
375+
if (overrideContent && messages[messages.length - 1].content !== overrideContent) {
398376
messages.push({ role: "user", content: overrideContent });
399377
}
400378

@@ -424,14 +402,13 @@ Any text inside [memory]...[/memory] will be used by the user as a memory for fu
424402

425403
let aiContent = extractAIContent(data);
426404
if (aiContent) {
427-
// parse out [memory] blocks
405+
// Parse out memory blocks
428406
const foundMemories = parseMemoryBlocks(aiContent);
429407
foundMemories.forEach((m) => {
430408
Memory.addMemoryEntry(m);
431409
});
432-
// remove those blocks from displayed text
410+
// Remove memory blocks from displayed text
433411
const cleanedAiContent = removeMemoryBlocks(aiContent).trim();
434-
435412
addNewMessage({ role: "ai", content: cleanedAiContent });
436413
if (callback) callback();
437414
}

0 commit comments

Comments
 (0)