Skip to content

Commit 266b765

Browse files
authored
Update chat-part3.js fixes session names generation
1 parent 737ede7 commit 266b765

File tree

1 file changed

+35
-14
lines changed

1 file changed

+35
-14
lines changed

ai/chat-part3.js

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,41 @@ document.addEventListener("DOMContentLoaded", () => {
1818
initSpeechRecognition
1919
} = window._chatInternals;
2020

21+
// ========== SESSION TITLE STUFF ==========
22+
function randomSeed() {
23+
return Math.floor(Math.random() * 1000000).toString();
24+
}
25+
26+
function generateSessionTitle(messages) {
27+
// Generate a session title based on the first AI message's content,
28+
// stripped of any markdown and truncated to 50 characters.
29+
let title = "";
30+
for (let i = 0; i < messages.length; i++) {
31+
if (messages[i].role === "ai") {
32+
title = messages[i].content.replace(/[#_*`]/g, '').trim();
33+
break;
34+
}
35+
}
36+
if (!title) title = "New Chat";
37+
if (title.length > 50) {
38+
title = title.substring(0, 50) + "...";
39+
}
40+
return title;
41+
}
42+
43+
function checkAndUpdateSessionTitle() {
44+
const current = Storage.getCurrentSession();
45+
// Only update if the current session name is still the default or blank
46+
if (!current.name || current.name === "New Chat") {
47+
const newTitle = generateSessionTitle(current.messages);
48+
if (newTitle && newTitle !== current.name) {
49+
Storage.renameSession(current.id, newTitle);
50+
}
51+
}
52+
}
53+
2154
// ========== MESSAGE RENDERING & ADDING ==========
55+
2256
function appendMessage({ role, content, index }) {
2357
const container = document.createElement("div");
2458
container.classList.add("message");
@@ -248,19 +282,6 @@ document.addEventListener("DOMContentLoaded", () => {
248282
.replace(/'/g, "&#039;");
249283
}
250284

251-
// ========== SESSION TITLE STUFF ==========
252-
function randomSeed() {
253-
return Math.floor(Math.random() * 1000000).toString();
254-
}
255-
256-
function generateSessionTitle(messages) {
257-
// same as your existing code
258-
}
259-
260-
function checkAndUpdateSessionTitle() {
261-
// same as your existing code
262-
}
263-
264285
// ========== MESSAGE CREATION/EDITING ==========
265286
function renderStoredMessages(messages) {
266287
chatBox.innerHTML = "";
@@ -339,7 +360,7 @@ document.addEventListener("DOMContentLoaded", () => {
339360

340361
// --- Fixed Memory and Code Block Instructions ---
341362
const memoryInstructions = `
342-
"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. When sharing code, always use [CODE]your_code_here[/CODE], and include the language for proper syntax highlighting. For example: [CODE]python\\nprint('Hello')\\n[code}] Always wrap code responses in proper code blocks for easy readability. Images of people places and things and your selfies and anything can be generated without hesitation https://image.pollinations.ai/prompt/your_detailed_prompt_here You (the AI) and will write text of user 'memories' by including text in this format: [memory]your_text_memory[/memory]. 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)`;
363+
"You are the user's AI. Be yourself, the 'you' and name and gender you know is you the AI. Do not mix up your name and gender with the user's. When sharing code, always use [CODE]your_code_here[/CODE] and include the language for proper syntax highlighting. For example: [CODE]python\\nprint('Hello')\\n[/CODE]. Always wrap code responses in proper code blocks for easy readability. Images of people, places, things, your selfies anything can be generated without hesitation via https://image.pollinations.ai/prompt/your_detailed_prompt_here. You (the AI) will also write text of user 'memories' by including text in this format: [memory]your_text_memory[/memory]. Any text inside [memory]...[/memory] will be used by the user as a memory for future replies. (You shall also write an additional copy of the memory as plain text in your response.)`;
343364

344365
// Build message history
345366
const messages = [];

0 commit comments

Comments
 (0)