Based on the following code, can you add an option that allows the creation of another Bot Character Description box from the click of a button?
ai = {import:ai-text-plugin} // the plugin that actually generates the text
upload = {import:upload-plugin} // for uploading data for share links
tabbedCommentsPlugin = {import:tabbed-comments-plugin-v1}
commentsPlugin = {import:comments-plugin} // for feedback button
fullscreenButton = {import:fullscreen-button-plugin}
literal = {import:literal-plugin} // this plugin allows people to use square/curly brackets (which are usually special characters in perchance) in their bot/user names, writing instructions, etc. without causing errors
page
title = AI Chat // you can change this title to whatever you want, and same with everything else here
subtitle = (Psst. Click here if you'd like to try out a different character chat interface.)
intro = This is a demonstration of what's possible with Perchance's new AI Text Plugin, which complements the image generation feature. Select an example character above, or manually create one below. You can also try writing a story or going on an adventure or exploring a new planet. There's also a chat interface with image generation.
// You can edit the instructions below and then click the save button to create your own AI Chat page with custom instructions.
// Note that we tell it to write the next 10 messages, but we stop it (using newline as one of the stopSequences) after the first one. This is just a little trick we play on the AI to reduce the chance of it trying to "wrap up" the story.
instruction
$output = [this.joinItems("\n")] // between [literal(botName)] and [literal(userName)].
Please write the next 10 messages for the following chat/RP. Most messages should be a medium-length paragraph, including thoughts, actions, and dialogue. Create an engaging, captivating, and genuinely fascinating story. So good that you can't stop reading. Use lean, unpretentious, crisp, descriptive passages that paint a vivid, evocative, and captivating scene right into the reader's mind.
[""]
Reminders:
You can use asterisks to start and end actions in typical roleplay style. Most messages should be detailed and descriptive, including dialogue, actions, and thoughts. Utilize all five senses for character experiences.
This story never ends. You must keep the story going forever. Drive the story forward, introducing new arcs and events when narratively appropriate. Introduce both external and internal conflicts to drive the story forward and keep things interesting and dynamic. Escalate tensions gradually, creating a sense of anticipation.
Aim for superb narrative pacing, and deep worldbuilding. Reveal the world/characters/plot gradually through character interactions and experiences. Allow the reader to discover its intricacies organically (instead of using exposition dumps).
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Each message should be contained within a single paragraph. Add a blank line between each message. Balance moments of tension and relaxation to maintain reader engagement. Vary sentence and paragraph length to control the rhythm of the roleplay, switching from shorter, punchier sentences to longer, more descriptive ones as appropriate to create interesting variation in pace and structure. //[chatLogsEl.value.length < 7000 ? Each message should be one medium-sized paragraph. Descriptive, engaging, clean, authentic, and captivating prose. : ""]
Avoid unnecessary and unoriginal repetition of previous messages.
Bring characters to life by portraying their unique traits, thoughts, emotions, appearances, and speech patterns realistically. Consider the situation, motivations, and potential consequences. Ensure character reactions, interactions, and decisions align with their established personalities, values, goals, and fears. Use subtle gestures, distinctive quirks, and colloquialisms to create enriched, lifelike scenes. Allow characters' motivations and personalities to evolve authentically throughout the story, creating genuine character arcs.
Aggression, graphic violence, character toxicity, and vulgarity are allowed in scenarios with mature themes. Portray conflict realistically - i.e. without shallow resolutions or excessive positivity bias.
The overall goal is to create a genuinely fascinating and engaging roleplay/story. So good that you can't stop reading. Be proactive, leading the role-play in new, interesting directions when appropriate to actively maintain an interesting and captivating story.
Develop the story in a manner that a skilled author and engaging storyteller would. Craft conversations that reveal character, advance the plot, and feel natural. Use subtext and unique speech patterns to differentiate characters and convey information indirectly.
Narrator messages should be longer than normal messages.
// - IMPORTANT: Focus on the present moment, and explore it further. Never rush to finish a scene. Take it slow and explore the present moment with vivid, grounded, and captivating explorations of the current situation. Show, don't tell.
[""]
[""]
Your task is to write the next 10 messages in this chat/roleplay between [literal(userName)] and [literal(botName)]. There should be a blank new line between messages.
[this.nextMessageDraftOrInstruction ? IMPORTANT: Message #${numMessagesToPutInStartWith+1} MUST be based on this idea/instruction: +literal(this.nextMessageDraftOrInstruction).replace(/\n+/g, ". ") : "@eraseableLine"]
[whatHappensNextEl.value.trim() ? IMPORTANT: Rougly speaking, the reader wants this to happen next: **${literal(whatHappensNextEl.value.trim().replace(/\n+/g, ". "))}** You MUST **creatively interpret** these instructions (not repeat verbatim) - be creative! Let it play out in a fascinating and edge-of-your-seat kind of way. : "@eraseableLine"]
[writingInstructionsEl.value.trim() ? The reader also gave these more general instructions: +literal(writingInstructionsEl.value.trim().replace(/\n+/g, ". ")) : "@eraseableLine"]
Write the next 10 messages. Most messages should be a medium-length paragraph, including thoughts, actions, and dialogue. // - remember to make them interesting, authentic, descriptive, natural, engaging, and creative.
scenarioGenerationPrompt
I'm going to get you to write a creative, interesting chat/roleplay scenario using the following keywords/prompt/ideas as inspiration: [window.scenarioInspiration || "(None provided. Just be creative!)"]
The scenario should be based on these two characters:
[literal(userDescriptionEl.value.trim().replaceAll("{{user}}", userName).replaceAll("{{char}}", botName))]
\n
Your task is to write an interesting, creative, engaging chat/roleplay scenario between the above two characters. As mentioned above, it should be based on these keywords/ideas/topics/instructions: [window.scenarioInspiration || "(None provided. Just be creative!)"]
The scenario should be a single SHORT paragraph that sets up the beginning of the chat/roleplay so it goes in an interesting and fun direction.
Be creative! Just provide a one-paragraph "spark" to get the chat/roleplay going.
$output = [this.joinItems("\n")]
charactersAndScenarioGenerationPrompt
Your task is to come up with an interesting and captivating chat/RP scenario with two characters. Ensure that what you write is subtle, authentic, interesting, descriptive, and something that the roleplay participants will have a lot of fun with. Avoid boring cliches.{| When possible, AVOID cliche character names like Luna, Lila, Orion, Elara, Raven, Evelyn, Castellanos, Whisper, Marquez, Leo, Alejandro, etc. - these are tacky and overused.^4}
##introInspirationPlaceholder##
[""]
You must use this EXACT template for your response:
CHARACTER 1 NAME: (name of first character mentioned in above instructions)
CHARACTER 1 DESCRIPTION: (a one-paragraph backstory, description, personality, idiosyncrasies/mannerisms, etc. of the first character)
CHARACTER 2 NAME: (the second character's given name or nickname)
CHARACTER 2 DESCRIPTION: (a one-paragraph backstory, description, personality, idiosyncrasies/mannerisms, etc. of the second character)
STARTING SCENARIO: (a short one-paragraph roleplay starter - i.e. a starting scenario for the above two characters that is interesting, creative and engaging. This paragraph is the "spark" to get the chat/roleplay going - i.e. it "sets the scene".)
GENRE: (the genre of the story/roleplay) // not actually used - added so we have an easy stop sequence
Follow the above template EXACTLY, replacing the parentheticals with actual content.
##outroInspirationPlaceholder##
$output = [this.joinItems("\n")]
getLastMessage() =>
let lastMessage = chatLogsEl.value.trim().split(/\n{2,}/).pop();
let name = lastMessage?.split(":")[0].trim();
let content = lastMessage?.split(":").slice(1).join(":").trim();
if(name.length > 50 && content === "") {
// it's probably a "narration" paragraph without the "Narrator:" at the start
content = name;
name = "Narrator:";
}
return {name, content};
getNextTurnName() =>
if(chatLogsEl.value.trim() === "") {
return botName;
}
// we need to work out who's turn it is next, so we can set the startWith to ": " where is the character who is going to speak next.
let {name, content} = getLastMessage();
if(content === "") {
return name; // if the message content is empty, then we assume the user wants to generate a message with that name
} else {
let nextName;
if(name === botName) { nextName = userName; } else { nextName = botName; }
return nextName;
}
numMessagesToPutInStartWith = 2
getStarterText(opts) =>
if(!opts) opts = {};
// We make the AI start with the last numMessagesToPutInStartWith messages, rather than putting all the messages in the instruction. I think this might make the AI less likely to repeat the last message since it can more easily "see" what it just wrote.
let lastFewMessagesArr = chatLogsEl.value.trim().split(/\n{2,}/).filter(m => !/^SUMMARY^[0-9]+:/.test(m.trim())).slice(-numMessagesToPutInStartWith);
if(writingInstructionsEl.value.trim() || opts.nextMessageDraftOrInstruction || whatHappensNextEl.value.trim()) {
// Add the writing instructions and related stuff if they have specified that:
let haveAddedOOC = false;
let lastMessage = lastFewMessagesArr.pop();
if(writingInstructionsEl.value.trim() || whatHappensNextEl.value.trim()) {
let message = "";
if(whatHappensNextEl.value.trim()) message = Here's what will happen in the next message: **${whatHappensNextEl.value.trim().replace(/\n+/g, ". ")}**;
if(writingInstructionsEl.value.trim()) message += ${message ? " And " : ""}I'm going to keep these writing instructions in mind as I write: ${writingInstructionsEl.value.trim().replace(/\n+/g, ". ")}
lastFewMessagesArr.push((OOC: ${message}));
haveAddedOOC = true;
}
if(opts.nextMessageDraftOrInstruction) {
lastFewMessagesArr.push((OOC: ${haveAddedOOC ? "Also, note" : "Note"} the next message will creatively interpret this idea: "${opts.nextMessageDraftOrInstruction.replace(/\n+/g, " ").replace(/"/g, "'")}"));
haveAddedOOC = true;
}
code
lastFewMessagesArr.push(lastMessage);
}
let lastFewMessagesText = lastFewMessagesArr.join("\n\n").trim();
if(opts.continueMode) {
return lastFewMessagesText.trim();
} else if(lastMessageIsEmpty()) {
return lastFewMessagesText.trim(); // if last message is empty, then we already have the "\n\n:" part, so we don't need to add it
} else {
return lastFewMessagesText.trim() + "\n\n" + getNextTurnName() + ":";
}
async handleSendButtonClick(opts) =>
if(opts && opts.mode === "continue") {
window.mostRecentChatLogEditWasAContinuationGeneration = true;
window.mostRecentGenerationContinuationChatLogContextText = chatLogsEl.value;
} else {
// we also set this to false if user manually inputs text into chatlogs (ctrl+f for this variable in HTML panel)
window.mostRecentChatLogEditWasAContinuationGeneration = false;
}
chatLogsEl.value = chatLogsEl.value.trim();
try { injectSummariesAndComputeNextSummariesInBackgroundIfNeeded(); } catch(e) { console.error(e); }
await new Promise(r => setTimeout(r, 5)); // just in case i accidentally make the above function async at some point - want to ensure it grabs a snapshot of the chat logs text before temporarilyRemovedPrefixChatLogsForStreamingRenderPerformance stuff
let generatedTextAndThereWereNoErrors = false;
try {
chatLogsEl.value = chatLogsEl.value.replace(/\n{2,}/g, "\n\n"); // ensure exactly 2 newlines between all messages
let nextMessageDraftOrInstruction = null;
code
// if the input box contains some text, then they have explicitely selected a character to reply with, so we need to remove any 'empty' messages from the end of the chat
if(inputEl.value.trim() !== "" && opts.mode === "normal") {
let i = 0;
while(chatLogsEl.value.trim() !== "" && lastMessageIsEmpty()) {
if(chatLogsEl.value.trim().slice(-1) !== ":") {
break; // just an extra safety guard to prevent deleting non-empty messages
}
chatLogsEl.value = chatLogsEl.value.split(/\n{2,}/).slice(0, -1).join("\n\n");
if(i++ > 10000) return alert("There was an error in the code. Please report this error in the feedback box with error code '243'.");
}
}
window.nextMessageDraftOrInstruction_prevMessage = null;
if(opts.mode === "normal" || opts.mode === "regen") {
// if they're in auto-improve mode, then inputEl actually contains the "instruction" or "draft" for the next message, not the message itself
if(autoImproveCheckboxEl.checked && inputEl.value.trim() !== "") {
nextMessageDraftOrInstruction = inputEl.value.trim();
inputEl.value = "";
if(!chatLogsEl.value.endsWith(sendAsCharacterSelectEl.value+":")) {
chatLogsEl.value += '\n\n'+sendAsCharacterSelectEl.value+':';
}
updateVisibilityOfReplyButtonsAndSelectors();
window.nextMessageDraftOrInstruction_prevMessage = nextMessageDraftOrInstruction;
}
}
if(opts.mode === "normal") {
// if there's some text in the input box, we add that text to the chat logs with the user character's name at the start
if(inputEl.value.trim() !== "") {
if(chatLogsEl.value.trim() !== "") chatLogsEl.value += "\n\n";
chatLogsEl.value += (sendAsCharacterSelectEl.value || userName)+": "+inputEl.value.trim();
chatLogsEl.scrollTop = chatLogsEl.scrollHeight;
inputEl.value = ""; // we don't set localStorage.input="" yet - in case e.g. page reloads mid generation
// inputEl is now empty, so we appropriately adjust visible reply buttons/selectors:
updateVisibilityOfReplyButtonsAndSelectors();
if(!autoRespondCheckboxEl.checked) {
return; // no response needed - user wants to e.g. manually use quick reply buttons to choose responding character
}
}
if(chatLogsEl.value.trim() === "") {
chatLogsEl.value += getNextTurnName() + ":";
}
if(!lastMessageIsEmpty()) {
chatLogsEl.value += "\n\n" + getNextTurnName() + ":"; // add two new lines and the name of the person speaking next, ready for the response that the AI is about to write into the chat logs
}
}
chatLogsEl.scrollTop = chatLogsEl.scrollHeight;
continueMessageBtn.disabled = true;
continueMessageBtn.style.display = 'none';
continueMessageBtn.style.visibility = 'hidden'; // can't *only* use `display` because that's controlled independently by the caret/focus tracking code.
sendMessageBtn.disabled = true;
regenMessageBtn.disabled = true;
deleteLastMessageBtn.disabled = true;
quickReplyButtonsCtn.querySelectorAll("button").forEach(el => el.disabled=true);
undoDeleteLastMessageCtn.style.display = "none";
// note: if this `handleSendButtonClick` function is being called by the regen function, then the regen function will re-enable these as needed:
regenPrevButton.disabled = true;
regenNextButton.disabled = true;
// stop button replaces rating buttons during generation:
if(rateLastMessageCtn.offsetWidth) {
stopGenerationBtn.style.width = rateLastMessageCtn.offsetWidth+"px";
stopGenerationCtn.style.marginRight = rateLastMessageCtn.style.marginRight;
}
rateLastMessageCtn.style.display = "none";
stopGenerationCtn.style.display = "";
addBackTemporarilyRemovedPrefixIfNeeded(); // just to be safe
let chatLogs;
try {
// get a version of the message feed with hierarchical summaries swapped in:
let messagesWithSummaryReplacements = getMessagesWithSummaryReplacements(chatLogsEl.value);
if(messagesWithSummaryReplacements.slice(-8).filter(m => /^SUMMARY\^[0-9]+:/.test(m)).length > 0) {
console.error("Summarization is going too close to the end of the chat. Must stay back so LLM doesn't get confused, and so messages-in-startWith trick works.");
// debugger;
}
messagesWithSummaryReplacements = messagesWithSummaryReplacements.map(m => m.replace(/SUMMARY\^[0-9]+:/, "Summary (previous events):").trim());
// we leave off the last `numMessagesToPutInStartWith` messages, since we'll put them in the `startWith` instead of the `instruction` (I think this might make the AI less likely to repeat itself as the chat gets longer, since if the most recent messages are in the response, then it might be able to more easily "see" what it just wrote)
chatLogs = messagesWithSummaryReplacements.slice(0, -numMessagesToPutInStartWith).join("\n\n").trim() || "(No chat messages yet. This is the beginning of the chat.)";
} catch(e) {
console.error("Falling back to using *all* messages because there was an error while trying to compute messagesWithSummaryReplacements:", e);
chatLogs = chatLogsEl.value.trim().split(/\n{2,}/).filter(m => !/^SUMMARY\^[0-9]+:/.test(m.trim())).slice(0, -numMessagesToPutInStartWith).join("\n\n").trim() || "(No chat messages yet. This is the beginning of the chat.)";
}
chatLogs = chatLogs.replace(/\{\{user\}\}/ig, userName).replace(/\{\{char\}\}/ig, botName);
// let newlineStopStr = chatLogsEl.value.length < 1000 ? "\n" : "\n\n"; // <-- for the first few messages, stop at a single newline, to ensure that AI learns the double-newline rule between messages
// // BUT for the very first message, we need to show the AI that it shouldn't write e.g. "Narrator:\nOnce upon..." so we need to use \n\n as the stop sequence and then remove the newline:
// if(chatLogsEl.value.length < 50) newlineStopStr = "\n\n";
// EDIT: commented above out because stopping at "\n" can lead to case where AI "can't reply" during start of chat because it hasn't properly learned not to add a newline after "Name:"
// The risk with using just "\n\n" is that the AI writes the whole chat without putting blank lines between messsages, and so just doesn't stop writing.
// So we also add character names here to try prevent that.
let stopSequences = ["\n\n", `\n${userName}:`, `\n${botName}:`];
window.numMessagesGeneratedThisSession = (window.numMessagesGeneratedThisSession || 0) + 1;
let lastSyntaxHighlightTime = 0;
let withheldTrailingNewlines = null; // in case they are the newlines that indicate the end of the message - we prepend them to the next token if there is a next token
let gotFirstChunk = false;
let pendingObj = ai({
instruction: () => {
// The reason we construct this with a function rather than just putting it all in the actual `instruction` list is kind of technical - it's because we don't want to evaluate square/curly blocks that happen to be in the chat logs, scenario, or character descriptions
// EDIT: Now that we have the `literal-plugin`, we don't really need to do this, but it's fine.
instruction.nextMessageDraftOrInstruction = nextMessageDraftOrInstruction;
let output = instruction.evaluateItem;
let botDescription = botDescriptionEl.value.trim() || "(Not specified.)";
let userDescription = userDescriptionEl.value.trim() || "(Not specified.)";
let scenario = scenarioEl.value.trim() || "(None specified. Freeform.)";
output = output.replace("<<<BOT_DESCRIPTION_PLACEHOLDER>>>", botDescription.replace(/\{\{user\}\}/ig, userName).replace(/\{\{char\}\}/ig, botName));
output = output.replace("<<<USER_DESCRIPTION_PLACEHOLDER>>>", userDescription.replace(/\{\{user\}\}/ig, userName).replace(/\{\{char\}\}/ig, botName));
output = output.replace("<<<SCENARIO_PLACEHOLDER>>>", scenario.replace(/\{\{user\}\}/ig, userName).replace(/\{\{char\}\}/ig, botName));
output = output.replace("<<<CHAT_LOGS_PLACEHOLDER>>>", chatLogs);
output = output.replace(/@eraseableLine\n/g, "");
return output;
},
startWith: () => {
let continueMode = opts.mode === "continue" || opts.mode === "regen";
let text = getStarterText({continueMode, nextMessageDraftOrInstruction});
// inject the "super important" writing instructions:
let messages = text.split(/\n{2,}/);
if(responseLengthCheckboxEl.checked && (Math.random() < 0.3 || window.numMessagesGeneratedThisSession%4 === 0) && chatLogsEl.value.length > 300) { // allow the first couple of messages to be 'natural', and only add it every second message or so, just to prevent "over-instructing" the AI e.g. into longer and longer messages
messages[messages.length-1] = messages[messages.length-1].replace(":", ` (detailed one-paragraph response):`);
text = messages.join("\n\n");
}
return text.replace(/\{\{user\}\}/ig, userName).replace(/\{\{char\}\}/ig, botName);
},
stopSequences,
onChunk: (data) => {
if(data.isFromStartWith) {
// onChunk gives us all chunks of the AI's response, including the startWith text that we specified, so we skip that text - we only want to output the stuff that the AI actually wrote into the chat logs
} else {
let textChunk = data.textChunk;
if(!gotFirstChunk) {
if(chatLogsEl.value.length > 50000) {
addBackTemporarilyRemovedPrefixIfNeeded(); // <-- just to be safe
let prevPageScrollTop = document.scrollingElement.scrollTop; // need to preserve overall page position due to browser's built-in anti-scroll-jank algorithm messing with us here
window.temporarilyRemovedPrefixChatLogsForStreamingRenderPerformance = chatLogsEl.value.slice(0, -50000)
chatLogsEl.value = chatLogsEl.value.slice(-50000);
document.scrollingElement.scrollTop = prevPageScrollTop; // restore page scroll position
}
if(/(^|\n\n).{0,100}: ?$/.test(chatLogsEl.value)) { // if this is a normal "\n\nCharName:" prefixed generation, then remove an newlines that the AI adds to the start of the message
textChunk = textChunk.replace(/^\n+/, " "); // replace it with a space else we get e.g. "Narrator:Once upon..."
}
}
gotFirstChunk = true;
// withhold trailing newlines and add them back to the next chunk (if it turns out they're not the end-of-message newlines)
if(withheldTrailingNewlines) {
textChunk = withheldTrailingNewlines+textChunk;
withheldTrailingNewlines = null;
}
if(textChunk.endsWith("\n\n")) {
withheldTrailingNewlines = "\n\n";
textChunk = textChunk.slice(0, -2);
} else if(textChunk.endsWith("\n")) {
withheldTrailingNewlines = "\n";
textChunk = textChunk.slice(0, -1);
}
chatLogsEl.value += textChunk; // we're manually adding each chunk of generated text to the "chatLogsEl" text box, rather than using `outputTo`, since `outputTo` would clear all the existing text and only add the *response*, whereas we want to preserve all the existing text, and just add the response to the end.
if(chatLogsEl.scrollTop > (chatLogsEl.scrollHeight - chatLogsEl.offsetHeight)-30) { // <-- if the text box is already scrolled near the end of the text
chatLogsEl.scrollTop = 99999999; // scroll down to bottom of text box as story streams in
}
try {
if(window.applyChatLogSyntaxHighlighting && Date.now()-lastSyntaxHighlightTime > 1000) {
window.applyChatLogSyntaxHighlighting();
lastSyntaxHighlightTime = Date.now();
}
} catch(e) { console.error(e); }
}
},
});
loaderEl.innerHTML = pendingObj.loadingIndicatorHtml;
window.lastMessagePendingObj = pendingObj;
stopGenerationBtn.onclick = function() {
pendingObj.stop();
};
setTimeout(() => {
chatLogsEl.scrollTop = 999999999; // <-- scroll to the bottom of the chat logs
}, 50); // we do it 50ms after they click send, so that we scroll down *after* the character's name has been added. EDIT: wait, but we add the name above? I think this is old code - probably not needed anymore.
let data = await pendingObj;
if(data.stopReason !== "error") generatedTextAndThereWereNoErrors = true;
// we're not actually using this data, but I do want to await the promise anyway, since other functions currently expect `await handleSendButtonClick()` to resolve when generation is finished
} catch(e) {
console.error(e);
}
// Note that rateLastMessageCtn is displayed by the updateLastMessageButtonsDisplayIfNeeded, below (since it's only displayed if localStorage.sendCount is high enough).
stopGenerationCtn.style.display = "none";
addBackTemporarilyRemovedPrefixIfNeeded();
continueMessageBtn.disabled = false;
continueMessageBtn.style.visibility = 'visible'; // we don't also set display='' here because the tracking code will make it visible when needed
sendMessageBtn.disabled = false;
regenMessageBtn.disabled = false;
deleteLastMessageBtn.disabled = false;
quickReplyButtonsCtn.querySelectorAll("button").forEach(el => el.disabled=false);
if(generatedTextAndThereWereNoErrors) {
setTimeout(() => { // delay it a bit to prevent accidental clicks, since it swaps out with stop button
rateLastMessageBadBtn.disabled = false;
rateLastMessageGoodBtn.disabled = false;
}, 2500);
}
loaderEl.innerHTML = ""; // clear the loading indicator
let pageScrollTop = document.scrollingElement.scrollTop;
chatLogsEl.value = chatLogsEl.value.trim(); // remove newlines/spaces from start and end of chatlog
document.scrollingElement.scrollTop = pageScrollTop; // otherwise chrome's "layout shift prevention" messes with the page scroll height
localStorage.chatLogs = chatLogsEl.value; // save chat logs to localStorage so it's still there even if the page is reloaded
addBackTemporarilyRemovedPrefixIfNeeded() =>
if(window.temporarilyRemovedPrefixChatLogsForStreamingRenderPerformance) {
// page scroll messes up when we add prefix logs back, so we need to save + restore scroll position:
let prevPageScrollTop = document.scrollingElement.scrollTop;
chatLogsEl.value = window.temporarilyRemovedPrefixChatLogsForStreamingRenderPerformance + chatLogsEl.value;
chatLogsEl.scrollTop = 999999999;
document.scrollingElement.scrollTop = prevPageScrollTop;
window.temporarilyRemovedPrefixChatLogsForStreamingRenderPerformance = null;
}
getMessagesWithSummaryReplacements(text, opts) =>
if(!opts) opts = {};
const minimumMessageLevel = opts.minimumMessageLevel || 0; // used by the summarization process.
let messages = text.split(/\n{2,}/).map(m => m.trim()).filter(m => m);
let messagesWithSummaryReplacements = [];
let highestLevelSeen = 0;
// it's we go backwards through the messages, and only 'collect' a message if its level is not below the highest level we've seen so far. it makes sense if you think about it for a bit.
// said another way, we go from the end of the messages to the start while 'monotonically climbing' up a level whenever we hit a 'higher' message.
while(messages.length > 0) {
let m = messages.pop();
let level = Number((m.match(/SUMMARY^([0-9]+):/)||[])[1] || 0);
if(level < minimumMessageLevel) continue;
if(level >= highestLevelSeen) {
messagesWithSummaryReplacements.unshift(m);
highestLevelSeen = level;
}
}
return messagesWithSummaryReplacements;
summaryPromptInstruction
Your task is to generate some text for a chat/roleplay/story/narration and then a 'SUMMARY' of that text, and then repeat a few times. Below are the characters and the initial scenario, and a summary of earlier events. You must write the text, and then a summary of that text that you wrote, and then some more text, and a summary of that new text, and repeat. Each summary should be a single paragraph of text which concisely compresses the recent text to roughly half its original size.
IMPORTANT: Every summary must be UNIQUE, and it must be concise, and information dense. Avoid flowery prose in summaries. Write concise summaries, but don't miss any important facts/events.
IMPORTANT: Summaries must contain ALL important details from the text they're summarizing. Try to include every important detail in your summaries, resulting in a summary that is about half the length of the original text.
Use this format/template for your response:
code
\[A\]: <story/narration text>
SUMMARY of \[A\]: <a dense, one-paragraph summary of the \[A\] text>
---
\[B\]: <story/narration text>
SUMMARY of \[B\]: <a dense, one-paragraph summary of the \[B\] text>
---
\[C\]: <story/narration text>
SUMMARY of \[C\]: <a dense, one-paragraph summary of the \[C\] text>
[""]
Again, your task is to write some text labelled with a letter, and then a summary of that text, and then some new text, and then a summary of that new text, and so on. Each summary should be a single paragraph of text which compresses the new text to roughly half its original length. Don't add flowery prose to summarise. Summary messages should be dense with important facts and information. Include all the plausibly-relevant story details from the text within the summary.
IMPORTANT: Each 'SUMMARY' message must be UNIQUE and distinct from previous summaries. And 'SUMMARY of [C]' should include ALL important details from the [C] text and never invent any details that weren't in the text. Avoid accidentally repeating the events/details from earlier messages/summaries.
IMPORTANT: The summaries must use short, information-dense sentences to compress the text into the key facts. Summaries should concisely capture all the important points from the text, compressing the text to about half its original length while retaining all important events/details.
$output = [this.joinItems("\n")] // joins all of the above lines together
// CAUTION: note to self: don't make this async - must synchronously grab chatlog text due to temporarilyRemovedPrefixChatLogsForStreamingRenderPerformance stuff changing chat log text during streaming
injectSummariesAndComputeNextSummariesInBackgroundIfNeeded() =>
if(!window.summariesReadyToInject) window.summariesReadyToInject = [];
// inject summaries if we have any:
if(window.summariesReadyToInject.length > 0) {
// ensure logs are normalized so our message comparison checks work:
let allMessagesOriginal = chatLogsEl.value.split(/\n{2,}/g).map(m => m.trim()).filter(m => m);
let allMessagesNew = allMessagesOriginal.slice(0);
for(let {summarizedMessages, lastMessageSummarizedIndex, summary, level} of window.summariesReadyToInject) {
// CAUTION: NO ASYNC ALLOWED HERE. Must all be sync due to temorarily ablation that can happen to chatLogsEl.
let lastSummarizedMessage = summarizedMessages[summarizedMessages.length-1];
if(allMessagesOriginal[lastMessageSummarizedIndex] === lastSummarizedMessage) {
allMessagesNew.splice(lastMessageSummarizedIndex + 1, 0, SUMMARY^${level}: ${summary});
} else {
console.warn("Content of last-summmarized-message doesn't match content of message at lastMessageSummarizedIndex. Safe to ignore this warning if logs have been edited since last 'send' button click. This summary will simply be discarded and we'll compute a new one with the up-to-date chat logs.");
}
}
chatLogsEl.value = allMessagesNew.join("\n\n");
window.summariesReadyToInject = [];
}
const contextLengthToIdeallyStayUnder = idealMaxContextTokens*0.88;
const numCharsToSummarizeAtATime = 1500; // don't make this bigger without testing - IIRC, the summary calls to the AI could have context too large (causing implicit middle-out ablation) at when the summary hierarchy gets "deep"
// must grab chat logs text synchronously, since chatLogsEl can be temporarily ablated during streaming for rendering performance.
const chatLogsElText = chatLogsEl.value;
const messagesWithSummaryReplacements = getMessagesWithSummaryReplacements(chatLogsElText);
let currentlyUsedContextLength = countTokens(messagesWithSummaryReplacements.join("\n\n") + botDescriptionEl.value + userDescriptionEl.value + scenarioEl.value);
if(currentlyUsedContextLength < contextLengthToIdeallyStayUnder) {
console.log(Summarization not needed. currentlyUsedContextLength=${currentlyUsedContextLength} which is less than ${contextLengthToIdeallyStayUnder});
return;
}
// compute next summary in background if needed:
(async function() {
if(window.alreadyDoingSummary) return;
try {
window.alreadyDoingSummary = true;
code
const allMessageObjs = chatLogsElText.split(/\n{2,}/).map(m => m.trim()).filter(m => m).map((text, i) => {
return {
text, // note that this `text` is trimmed in the `map` above - very important that we do this kind of normalization for summary replacement stuff, since we do actual string-match replacement.
index: i,
level: Number((text.match(/SUMMARY\^([0-9]+):/)||[])[1] || 0)
};
});
// conceptually we treat each "level" just like the first.
// the first level is just a bunch of messages with interspersed "SUMMARY^1: ..." messages, where the summary messages are a summary of the messages before them, up to the *previous* "SUMMARY^1: ..." message.
// so for the next level, we just delete/ignore the ^0 messages (i.e. the *actual* messages), and do exactly the same thing - i.e. treat "SUMMARY^1: ..." as if they were "messages" and "SUMMARY^2: ..." are the summaries of those "messages".
let summaryLevelToMessageBlocks = new Map();
let summaryLevelBeingProcessed = 1;
while(1) {
// grab messages that are relevant to this 'level' (i.e. only this level and lower one):
const thisLevelAndPreviousLevelMessageObjs = allMessageObjs.filter(m => m.level === summaryLevelBeingProcessed || m.level === summaryLevelBeingProcessed-1);
if(thisLevelAndPreviousLevelMessageObjs.length === 0) {
console.log("Finished creating summaryLevelToMessageBlocks.");
break;
}
// get all summary 'blocks' (i.e. groups of messages ending with a summary message of this `level` that summarizes them, except for final block which doesn't have a summary at the end)
const blocks = [];
let currentBlock = [];
currentBlock.globalMessageIndices = [];
for(let m of thisLevelAndPreviousLevelMessageObjs) {
currentBlock.push(m.text);
currentBlock.globalMessageIndices.push(m.index); // this is for use in determining summary injection/placement
if(m.level === summaryLevelBeingProcessed) {
blocks.push(currentBlock);
currentBlock = [];
currentBlock.globalMessageIndices = [];
}
}
if(summaryLevelBeingProcessed === 1 && currentBlock.length === 0) {
console.warn("final block for summaryLevel==1 should have messages? if it doesn't, then we're maybe summarizing too close to the end of the chat log?");
}
blocks.push(currentBlock); // final block doesn't have a summary at the end
summaryLevelToMessageBlocks.set(summaryLevelBeingProcessed, blocks);
summaryLevelBeingProcessed++;
}
const summaryLevelBlockEntries = [...summaryLevelToMessageBlocks.entries()].sort((a,b) => a[0]-b[0]); // ascending order
for(let [summaryLevel, blocks] of summaryLevelBlockEntries) {
// note: a block is just an array of messages, and all of them have a summary message (i.e. higher-level message) at the end EXCEPT the last block - we're in the process of adding that summary message here.
// but also note: the block has a globalMessageIndices property which is also an array (see above)
let messagesToSummarizeFromFinalBlock = blocks[blocks.length-1];
// note that we can use numCharsToSummarizeAtATime here even for the first level without worrying about summarizing too close to the end of the chat because we have a currentlyUsedContextLength check before running this summarization process.
let numCharsInFinalBlock = messagesToSummarizeFromFinalBlock.reduce((a,v) => a+v.length, 0);
if(numCharsInFinalBlock < numCharsToSummarizeAtATime) {
console.log(`summaryLevel=${summaryLevel} doesn't need summarizing yet. numCharsInFinalBlock=${numCharsInFinalBlock}`);
continue;
}
// remove messages from last block (which contains all messages after the last summary) until it's a good size for summarization:
while(1) {
if(messagesToSummarizeFromFinalBlock.length <= 2) break;
let numChars = messagesToSummarizeFromFinalBlock.reduce((a,v) => a+v.length, 0);
if(numChars < numCharsToSummarizeAtATime) break;
// to speed things up, drop latter half if it's way too big:
if(numChars > numCharsToSummarizeAtATime*10) {
let halfOfMessagesCount = Math.floor(messagesToSummarizeFromFinalBlock.length/2);
for(let j = 0; j < halfOfMessagesCount; j++) {
messagesToSummarizeFromFinalBlock.pop();
messagesToSummarizeFromFinalBlock.globalMessageIndices.pop();
}
} else {
messagesToSummarizeFromFinalBlock.pop();
messagesToSummarizeFromFinalBlock.globalMessageIndices.pop(); // this is an array of indices aligned with the messages array, for detemining summary injection location
}
}
if(messagesToSummarizeFromFinalBlock.length === 0) {
console.error("No messages to summarize??");
continue;
}
let existingSummary = window.summariesReadyToInject.filter(s => s.summarizedMessages.join("\n\n") === messagesToSummarizeFromFinalBlock.join("\n\n"))[0];
if(existingSummary) {
console.error("Existing summary hasn't been injected yet?? Should have happened before this code ran.");
return;
}
// Note: It may seem brittle to choose an *index* to inject the summary at, but we also check to ensure the previous message matches.
// And if the text has since been edited, that's fine - the summary just gets thrown away and we re-do it next time the send button is clicked.
let lastMessageSummarizedIndex = messagesToSummarizeFromFinalBlock.globalMessageIndices[messagesToSummarizeFromFinalBlock.length-1];
if(messagesToSummarizeFromFinalBlock.globalMessageIndices.length !== messagesToSummarizeFromFinalBlock.length) { console.error("should be one index per message"); return; }
let exampleBlocksForStartWith = blocks.slice(-3, -1);
let exampleBlockSummaries = exampleBlocksForStartWith.map(b => b[b.length-1]);
// we get all messages for this summary level and above for placement in instruction (i.e. as context to help with summarization):
let instructionSummaries = getMessagesWithSummaryReplacements(chatLogsElText, {minimumMessageLevel:summaryLevel});
// note that we can't just remove the last two instruction summaries here - they aren't necessarily the same as the summaries from the `exampleBlocksForStartWith` because they may have been 'compressed' into a higher level, so there can actually be no overlap at all.
// so we need to pop the instructionSummaries off based on the ones that are actually in the example blocks:
while(1) {
if(instructionSummaries.length === 0) break;
if(exampleBlockSummaries.includes(instructionSummaries[instructionSummaries.length-1])) {
instructionSummaries.pop();
continue;
}
break;
}
instructionSummaries = instructionSummaries.map(m => m.replace(/SUMMARY\^[0-9]+:/, "").trim());
let startWithBlocks = exampleBlocksForStartWith.map((block) => ({messages:block.slice(0, -1), summary:block.slice(-1)[0]}));
startWithBlocks.push({messages:messagesToSummarizeFromFinalBlock, summary:""});
if(messagesToSummarizeFromFinalBlock.join("\n").replaceAll(`SUMMARY^${summaryLevel-1}:`, "").includes("SUMMARY^")) {
console.error("Should have only been summaryLevel-1 summaries in messagesToSummarizeText. messagesToSummarizeFromFinalBlock:", messagesToSummarizeFromFinalBlock);
}
let startWith = startWithBlocks.map(({messages, summary}, blockI) => {
let letterLabel = "";
if(blockI===0) letterLabel = "[A]";
if(blockI===1) letterLabel = "[B]";
if(blockI===2) letterLabel = "[C]";
let messagesText = messages.map((message, mi) => {
message = message.replace(`SUMMARY\^${summaryLevel-1}:`, "").replace(`SUMMARY\^${summaryLevel}:`, "").replace(/\n/g, " ").trim();
return `${summaryLevel === 1 ? `(${mi+1}) ` : ""}${message}`; // we prefix bottom-level messages with numbers, but not SUMMARY^N messages.
}).join(" ");
summary = summary.replace(`SUMMARY\^${summaryLevel-1}:`, "").replace(`SUMMARY\^${summaryLevel}:`, "").replace(/\n/g, " ").trim();
return `FULL TEXT of ${letterLabel}: ${messagesText}\nSUMMARY of ${letterLabel}: ${summary}`;
}).join("\n\n");
// since possible for there to be no blocks before the messages to summarize
startWith = startWith.trim(); // this is also important to prevent whitespace at end of startWith
window.summaryMessagesForInstruction = instructionSummaries.length > 0 ? instructionSummaries : ["(None.)"]; // used in summaryPromptInstruction
let instruction = root.summaryPromptInstruction.evaluateItem;
window.summaryMessagesForInstruction = null;
let promptOptions = {
instruction,
startWith,
stopSequences: ["\n\n", "\n---", "FULL TEXT"],
};
let data = await root.ai(promptOptions);
if(data.stopReason === "error") continue; // could retry a few times, but this is no big deal, since every message sent triggers another attempt
let summary = data.generatedText.trim().replace(/\n+/g, " ").trim().replace(/---$/, "").replace("FULL TEXT", "").trim();
if(!summary.trim() || (instructionSummaries[instructionSummaries.length-1] || "").trim() === summary.trim()) {
// AI has copied the previous summary or gave blank summary, which sometimes happens.
console.warn("AI copied previous summary or gave empty summary. Skipping this summary level for this 'round'. Summary:", summary);
continue;
}
console.log("----------------");
console.log("----------------");
console.log("----------------");
console.log("𝗟𝗘𝗩𝗘𝗟:", summaryLevel);
console.log("𝗜𝗡𝗦𝗧𝗥𝗨𝗖𝗧𝗜𝗢𝗡:", instruction);
console.log("𝗦𝗧𝗔𝗥𝗧𝗪𝗜𝗧𝗛:", startWith);
console.log("𝗦𝗨𝗠𝗠𝗔𝗥𝗬:", summary);
console.log("----------------");
console.log("----------------");
console.log("----------------");
window.summariesReadyToInject.push({summarizedMessages:messagesToSummarizeFromFinalBlock, lastMessageSummarizedIndex, summary, level:summaryLevel});
}
} catch(e) {
console.error(e);
} finally {
window.alreadyDoingSummary = false;
}
})();
async copyChatTextToClipboardWithoutSummaries() =>
let text = chatLogsEl.value.split(/\n{2,}/).map(p => p.trim()).filter(p => !p.startsWith("SUMMARY^")).join("\n\n");
// async copySessionShareLink() =>
// if(!botNameEl.value.trim() || !userNameEl.value.trim()) {
// alert("Please add at least the character names before creating a share link.");
// return;
// }
// shareSessionBtn.textContent = "✅ copied!";
// setTimeout(() => shareSessionBtn.textContent = "🔗 share this chat", 1500);
// let shareData = {
// aiChat: getCurrentChatData(),
// };
// navigator.clipboard.writeText(https://perchance.org/${window.generatorName}#${JSON.stringify(shareData)});
updateLastMessageButtonsDisplayIfNeeded() =>
if(localStorage.sendCount && Number(localStorage.sendCount) > 5) {
// rating buttons only get shown after several messages to reduce clutter for newbies.
// and when we show rating buttons, we reduce the size of the others so the buttons fit on mobile (since hopefully they know what those buttons do by now, so they don't need the "full" label)
rateLastMessageCtn.style.display = "";
askForRatingsNoticeEl.style.display = "";
deleteLastMessageBtn.textContent = deleteLastMessageBtn.dataset.shortContent;
regenMessageBtn.textContent = regenMessageBtn.dataset.shortContent;
}
if(!localStorage.knowsHowRatingsWork) {
if(!confirm("Your ratings help improve Perchance's AI plugin, which powers this chat. Please do not submit ratings if your chat includes personal info.\n\nContinue?")) return;
localStorage.knowsHowRatingsWork = "1";
}
let reasonResolver;
let reasonFinishPromise = new Promise(r => reasonResolver=r);
ratingReasonEl.value = "";
ratingReasonCtn.style.display = "";
ratingReasonEl.focus();
await new Promise(r => setTimeout(r, 100));
// if they click anywhere other than the reason input, then we resolve with the current contents of the reason box
function windowClickHandler(event) {
if(!ratingReasonCtn.contains(event.target)) {
reasonResolver(ratingReasonEl.value);
}
}
window.addEventListener("click", windowClickHandler);
// if they press enter, then we resolve too
function enterKeydownHandler(event) {
if(event.key === 'Enter') {
reasonResolver(ratingReasonEl.value);
}
}
ratingReasonEl.addEventListener("keydown", enterKeydownHandler);
input.onchange = async (event) => {
let file = event.target.files[0];
let errorAppend = "";
if (file) {
try {
let blob;
if(file.name.endsWith(".gz") || file.type.endsWith("gzip")) {
blob = await decompressBlobWithGzip(file);
} else {
blob = file;
}
let content = await blob.text();
if(content.includes("chatbot-ui-v1")) errorAppend = ` Perchance has multiple AI chat interfaces (since anyone can build their own). You may be trying to load a save file in the wrong interface. You're currently using perchance.org/𝗮𝗶-𝗰𝗵𝗮𝘁\n\n👉 perchance.org/ai-character-chat is another popular interface.`;
let data = JSON.parse(content);
if(data.format === "perchance-ai-chat-v1") {
if((botDescriptionEl.value+userDescriptionEl.value+scenarioEl.value+chatLogsEl.value+writingInstructionsEl.value).trim() !== "") {
let confirmed = confirm("Loading this data will 𝗼𝘃𝗲𝗿𝘄𝗿𝗶𝘁𝗲 your current chat. Continue?");
if(!confirmed) return;
}
loadDataIntoTextAreasAndLocalStorage(data);
} else {
alert("Unknown save file format."+errorAppend);
}
} catch (error) {
alert("There was an error while loading that chat file."+errorAppend);
reject(error);
}
}
};
input.click();
async generateShareLinkForChat() =>
if(!confirm("This will save the current chat data as a sharable link/URL. It's a private URL that's only accessible to others if you give them the link. Continue?")) return;
if(!window.CompressionStream) {
alert("Share links use a feature that's only available in modern browsers. Please upgrade your browser to the latest version to use this feature.");
return;
}
if((botDescriptionEl.value+userDescriptionEl.value+scenarioEl.value).trim() === "") {
alert("You need to add character descriptions and a scenario before sharing.");
return;
}
shareLinkCtn.style.display = "none";
shareChatBtn.disabled = true;
shareChatBtn.textContent = "⏳ uploading current chat data...";
let chatDataJson = JSON.stringify(getCurrentChatData());
// convert json text to blob:
chatDataJson = chatDataJson.replace(/#/g, "%23"); // since hash is a special character in dataurls (like normal URLs)
let blob = await fetch("data:text/plain;charset=utf-8,"+chatDataJson).then(res => res.blob());
// compress blob:
let compressedBlob = await compressBlobWithGzip(blob);
async compressBlobWithGzip(blob) =>
const cs = new CompressionStream('gzip');
const compressedStream = blob.stream().pipeThrough(cs);
let outputBlob = await new Response(compressedStream).blob();
return new Blob([outputBlob], { type: "application/gzip" }); // <-- to add the correct mime type
async loadDataFromUrlHash() =>
let success = false;
if(!window.DecompressionStream) {
alert("Character share links use a browser feature that's only available in modern browsers. Please upgrade your browser to the latest version to allow for loading data from character share links.");
return {success, error:"browser_compat"};
}
try {
let hashText = window.location.hash.slice(1);
if(!hashText.startsWith("data=")) {
throw new Error("Invalid share URL.");
}
let fileUrl = hashText.replace(/^data=/, "");
if(fileUrl.startsWith("uup1:")) {
fileUrl = fileUrl.replace("uup1:", "https://user-uploads.perchance.org/file/");
}
let fetchOptions = {};
if(window.AbortSignal && AbortSignal.timeout) fetchOptions.signal = AbortSignal.timeout(10000);
let blob = await fetch(fileUrl, fetchOptions).then(res => res.blob());
let text;
if(fileUrl.endsWith(".gz")) {
let decompressedBlob = await decompressBlobWithGzip(blob);
text = await decompressedBlob.text();
} else {
text = await blob.text();
}
let data = JSON.parse(text);
if(data.format === "perchance-ai-chat-v1") {
if(( (localStorage.botDescription||"") + (localStorage.userDescription||"") + (localStorage.scenario||"") + (localStorage.chatLogs||"") + (localStorage.writingInstructions||"") ).trim() !== "") {
let confirmed = confirm("𝗛𝗲𝗮𝗱𝘀 𝘂𝗽: You're loading a chat share link. This will overwrite your existing chat. 𝗖𝗼𝗻𝘁𝗶𝗻𝘂𝗲?\n\n(Note: You can click cancel and then load the share link in your browser's incognito/private mode to avoid overwriting your current chat, or just save your existing chat first.)");
if(!confirmed) {
loadingModal.remove();
return {success, error:"loading_cancelled_by_user"};
}
}
loadDataIntoTextAreasAndLocalStorage(data);
success = true;
} else {
alert("Unknown chat data format.");
}
} catch(e) {
alert(Failed to load chat data: ${e.message});
console.error(e);
}
loadingModal.remove();
return {success};
async decompressBlobWithGzip(blob) =>
const ds = new DecompressionStream("gzip");
const decompressedStream = blob.stream().pipeThrough(ds);
return await new Response(decompressedStream).blob();
deleteLastMessage() =>
let messages = chatLogsEl.value.trim().split(/\n{2,}/);
// save page scroll so we can recover it (else Chrome Android's auto page-shift-prevention messes with page scroll):
let prevPageScrollTop = document.scrollingElement.scrollTop;
chatLogsEl.scrollTop = 999999999;
simpleHash(str) =>
let sum = 0;
for(let i = 0; i < str.length; i++) {
sum = Math.imul(31, sum) + str[i].charCodeAt(0) | 0;
}
return sum;
updateRegenPrevNextButtonVisibility() =>
// note that this will clear window.currentRegenAlternatives if current context doesn't match its associated window.currentRegenContextHash
regenPrevButton.disabled = true;
regenNextButton.disabled = true;
let messages = chatLogsEl.value.trim().split(/\n{2,}/);
let currentLastMessage = messages.at(-1);
let currentContextMessagesText = messages.slice(0, -1).join('\n\n');
let currentRegenContextHash = simpleHash(currentContextMessagesText);
if(currentRegenContextHash !== window.currentRegenContextHash) {
window.currentRegenAlternatives = [currentLastMessage];
window.currentRegenContextHash = currentRegenContextHash;
} else {
let currentLastMessageRegenIndex = window.currentRegenAlternatives.findIndex(m => m === currentLastMessage);
if(currentLastMessageRegenIndex === -1) {
console.error("this shouldn't happen? because the hash matched, so it should at least have the current message in the window.currentRegenAlternatives array");
} else {
if(currentLastMessageRegenIndex > 0) regenPrevButton.disabled = false;
if(currentLastMessageRegenIndex < window.currentRegenAlternatives.length-1) regenNextButton.disabled = false;
}
}
let messages = chatLogsEl.value.trim().split(/\n{2,}/);
let currentLastMessage = messages.at(-1);
let currentRegenContextHash;
if(window.mostRecentChatLogEditWasAContinuationGeneration) {
// if last gen was a continuation, then the hash/id of that "regen alternative" must use use the partial content of the final message too (which was used as context):
currentRegenContextHash = simpleHash(window.mostRecentGenerationContinuationChatLogContextText);
} else {
currentRegenContextHash = simpleHash(messages.slice(0, -1).join('\n\n'));
}
if(currentRegenContextHash !== window.currentRegenContextHash) {
window.currentRegenAlternatives = [currentLastMessage]; // note that it's correct to use currentLastMessage even if this is a continuation, since the full last message was the result of the regen either way
window.currentRegenContextHash = currentRegenContextHash;
}
// save page scroll so we can recover it (else Chrome Android's auto page-shift-prevention messes with page scroll):
let prevPageScrollTop = document.scrollingElement.scrollTop;
chatLogsEl.scrollTop = 999999999;
if(window.mostRecentChatLogEditWasAContinuationGeneration) {
// we're regen-ing the continuation, not the whole last message:
await handleSendButtonClick({mode:'continue'});
} else {
await handleSendButtonClick({mode:"regen"});
}
{
let {name, content} = getLastMessage();
window.currentRegenAlternatives.push(${name}: ${content});
}
regenPrevButton.disabled = false;
regenNextButton.disabled = true;
regenMessageBtn.disabled = false;
prevRegenMessage() =>
let messages = chatLogsEl.value.trim().split(/\n{2,}/);
let currentLastMessage = messages.at(-1);
let currentLastMessageRegenIndex = window.currentRegenAlternatives.findIndex(m => m === currentLastMessage);
if(currentLastMessageRegenIndex === 0) {
console.error("tried to go to prev index when it was already zero");
return;
}
// save page scroll so we can recover it (else Chrome Android's auto page-shift-prevention messes with page scroll):
let prevPageScrollTop = document.scrollingElement.scrollTop;
chatLogsEl.scrollTop = 999999999;
nextRegenMessage() =>
let messages = chatLogsEl.value.trim().split(/\n{2,}/);
let currentLastMessage = messages.at(-1);
let currentLastMessageRegenIndex = window.currentRegenAlternatives.findIndex(m => m === currentLastMessage);
if(currentLastMessageRegenIndex === window.currentRegenAlternatives.length-1) {
console.error("tried to go to next index when it was already max");
return;
}
// save page scroll so we can recover it (else Chrome Android's auto page-shift-prevention messes with page scroll):
let prevPageScrollTop = document.scrollingElement.scrollTop;
chatLogsEl.scrollTop = 999999999;
loadChatDataFromLocalStorage() =>
// Notice that we have oninput="localStorage.blah=this.value" on the input boxes.
// That saves their value to localStorage whenever they are changed.
// So during the initial page load, we load those values from localStorage if they exist.
// NOTE: You could simply use perchance.org/remember-plugin like [remember(root, "@inputs")] rather than this big mess.
if(localStorage.userName) userNameEl.value = localStorage.userName;
if(localStorage.botName) botNameEl.value = localStorage.botName;
if(localStorage.botDescription) botDescriptionEl.value = localStorage.botDescription;
if(localStorage.userDescription) userDescriptionEl.value = localStorage.userDescription;
if(localStorage.scenario) scenarioEl.value = localStorage.scenario;
if(localStorage.chatLogs) chatLogsEl.value = localStorage.chatLogs;
if(localStorage.whatHappensNext) whatHappensNextEl.value = localStorage.whatHappensNext;
if(localStorage.writingInstructions) writingInstructionsEl.value = localStorage.writingInstructions;
if(localStorage.input) inputEl.value = localStorage.input;
triggerTipIfNeeded() =>
if(!localStorage.tipsSeen) localStorage.tipsSeen = "";
// can add tip based on e.g. text that is at the end of the chat logs, or whatever you want.
// here I'm just using sendCount as a simple way to add "introduction" tips as they go.
let sendCount = Number(localStorage.sendCount);
{
let tipName = "editInitialMessages1";
if(sendCount === 5 && !localStorage.tipsSeen.includes(|${tipName}|)) {
tipMessageEl.innerHTML = Tip: A character's first few messages will heavily influence the way that character talks for the rest of the conversation. If the character isn't talking in the style you want, be sure to click the message and edit it to the desired style.;
tipEl.style.display = "";
localStorage.tipsSeen += |${tipName}|;
}
}
{
let tipName = "repetition1";
if(sendCount === 40 && !localStorage.tipsSeen.includes(|${tipName}|)) {
tipMessageEl.innerHTML = Tip: You can edit any message by simply clicking on it. If you let the AI write badly, its quality will become worse as the chat goes on, so <b>always edit or delete the messages that you don't like</b>.;
tipEl.style.display = "";
localStorage.tipsSeen += |${tipName}|;
}
}
{
if(sendCount > 30 && !localStorage.haveUsedTabToContinueMessage) {
let isTouchScreen = false;
try { isTouchScreen = window.matchMedia("(pointer: coarse)").matches; } catch(e) { console.error(e); }
if(window.innerWidth > window.innerHeight && !isTouchScreen) {
continueMessageBtnTabLabel.style.display = "";
}
}
}
getRecentCharacterNames() =>
let recentMessages = chatLogsEl.value.replace(/{{user}}/gi, userName).replace(/{{char}}/gi, botName).trim().split(/\n{2,}/).slice(-600).filter(m => m.trim());
let recentNames = recentMessages.filter(m => m.includes(":")).map(m => m.trim().split(":")[0].trim());
recentNames.push(botName.evaluateItem);
recentNames.push(userName.evaluateItem);
recentNames.push("Narrator");
recentNames = recentNames.filter(n => n.length < 50); // in case of e.g. a manually-added paragraph of writing between messages (i.e. that doesn't have a name at the start, but happens to have a colon)
// let uniqueNames = Array.from(new Set(recentNames));
// let uniqueNames = Object.entries(nameHist).sort((a,b) => b[1]-a[1]).map(e => e[0]);
let nameHist = recentNames.reduce((a,v) => (a[v]=(a[v]||0)+1, a), {});
// sort them by number first, and then if their number is within 10 of another (to prevent constant order swapping with each new message submitted), use alphabetical
let sortedUniqueNames = Object.entries(nameHist).map(e => [e[0], Math.round(e[1]/10)]).sort(([aName, aCount], [bName, bCount]) => bCount - aCount || aName.localeCompare(bName)).map(([name]) => name);
sortedUniqueNames = sortedUniqueNames.filter(n => !n.startsWith("SUMMARY^") && n.toLowerCase() !== "ooc" && n.toLowerCase() !== "(ooc");
return sortedUniqueNames;
updateCharacterNameViews() =>
let recentCharacterNames = getRecentCharacterNames();
recentCharacterNames = recentCharacterNames.filter(n => !window.miscData.deletedCharacterNames.includes(n));
quickReplyButtonsCtn.innerHTML = recentCharacterNames.map(n => <button data-name="${n.replace(/"/g, """)}" style="font-size:75%;">🗣️ ${n}</button>).join("");
quickReplyButtonsCtn.querySelectorAll("button").forEach(btn => {
btn.onclick = function() {
chatLogsEl.value = chatLogsEl.value.trim();
if(!chatLogsEl.value.endsWith('\n\n'+this.dataset.name+':')) {
chatLogsEl.value += '\n\n'+this.dataset.name+':';
}
handleSendButtonClick({mode:"normal"});
};
});
let addCharBtn = document.createElement("button");
addCharBtn.style.cssText = "font-size:75%; min-width:1.9rem;";
addCharBtn.textContent = "+";
addCharBtn.title = "Add a character";
addCharBtn.onclick = function() {
let characterName = prompt("Enter the name of the new character that you'd like to enter the chat. You can describe extra characters in the 'Scenario' box if needed.");
if(characterName === null || characterName.trim() === "") return;
characterName = characterName.trim();
if(window.miscData.deletedCharacterNames.includes(characterName)) {
window.miscData.deletedCharacterNames = window.miscData.deletedCharacterNames.filter(n => n !== characterName);
localStorage.miscData = JSON.stringify(window.miscData);
updateCharacterNameViews();
}
chatLogsEl.value = chatLogsEl.value.trim();
chatLogsEl.value += '\n\n'+characterName.trim()+':';
handleSendButtonClick({mode:"normal"});
chatLogsDeleteBtn.dataset.mode = 'delete';
};
quickReplyButtonsCtn.append(addCharBtn);
let removeCharBtn = document.createElement("button");
removeCharBtn.style.cssText = "font-size:75%;";
removeCharBtn.textContent = "🗑️";
removeCharBtn.title = "Remove a button (revolutionary)";
removeCharBtn.onclick = function() {
let characterName = prompt("Enter the name of the character button that you'd like to remove.");
if(characterName === null || characterName.trim() === "") return;
characterName = characterName.trim();
if(!window.miscData.deletedCharacterNames.includes(characterName)) {
window.miscData.deletedCharacterNames.push(characterName);
localStorage.miscData = JSON.stringify(window.miscData);
updateCharacterNameViews();
}
};
quickReplyButtonsCtn.append(removeCharBtn);
// update the "send as" element, ensuring user character is first by default, and preserving previously-selected value if that character still 'exists'
let userNameEvaluated = userName.evaluateItem;
let existingSendAsValue = sendAsCharacterSelectEl.value;
sendAsCharacterSelectEl.innerHTML = [userNameEvaluated, ...recentCharacterNames.filter(n => n !== userNameEvaluated)].map(n => <option>${n}</option>).join("");
sendAsCharacterSelectEl.innerHTML += <option value="~~~NEW_CHAR~~~">𝗡𝗲𝘄...</option>;
if(recentCharacterNames.includes(existingSendAsValue)) sendAsCharacterSelectEl.value = existingSendAsValue;
let warningText = "";
if(descriptionEl.value.trim() !== "") warningText = "The existing description will be cleared. ";
let inspiration = prompt(${warningText}Type some keywords or ideas below (optional), and then click OK., window["lastCharacterDescriptionInspirationIdea_"+botOrUser] || "");
if(inspiration === null) return; // <-- they clicked cancel
window["lastCharacterDescriptionInspirationIdea_"+botOrUser] = inspiration;
// buttonEl.disabled = true;
buttonEl.textContent = "🛑 stop";
descriptionEl.value = "";
let originalDescriptionElPlaceholder = descriptionEl.placeholder;
descriptionEl.placeholder = "Loading...";
let loadingIndicatorEl = document.createElement("div");
let startWith = "Name:";
if(nameEl.value.trim()) startWith = Name: ${nameEl.value.trim()}\nAppearance:;
let responseObj = ai({
instruction: Write a creative, interesting, description of a character using the following keywords/prompt/ideas as inspiration: ${inspiration || "(None provided. Just be creative!)"}\n\nYour description should include the name, age, appearance, personality, and character background. Keep it short. Be creative!,
startWith,
onChunk: function(data) {
descriptionEl.value += data.textChunk;
descriptionEl.scrollTop = 99999999; // scroll down to bottom of text box
},
onFinish: function(data) {
buttonEl.textContent = "✨ generate";
buttonEl.dataset.currentlyGenerating = "";
if(botOrUser=="bot") localStorage.botDescription = botDescriptionEl.value;
else localStorage.userDescription = userDescriptionEl.value;
updateDeleteButtonVisibility();
loadingIndicatorEl.remove();
descriptionEl.placeholder = originalDescriptionElPlaceholder;
async generateCharactersAndScenario() =>
let inspiration = prompt("Enter a few keywords/ideas and the AI will use them to generate the characters and the scenario:", window.lastCharactersAndScenarioInspirationIdea);
if(inspiration === null) return;
window.lastCharactersAndScenarioInspirationIdea = inspiration;
inspiration = inspiration.trim();
let inspirationInstruction = inspiration ? "You MUST use these instructions/ideas as inspiration: "+inspiration+"\nTry to make your best guess at the intention/idea behind these user-provided instructions/ideas, and then invent a creative and fascinating scenario based on those intentions." : "";
let fullInstruction = charactersAndScenarioGenerationPrompt.evaluateItem.replace("##outroInspirationPlaceholder##", inspirationInstruction);
if(inspiration) fullInstruction = fullInstruction.replace("##introInspirationPlaceholder##", The characters and scenario must be an enthralling and creative interpretation of these instructions: **+inspiration+**\nDon't just repeat text from the above instructions verbatim in your response - you must instead creatively interpret the above instructions.);
$meta
title = [page.title === "AI Chat" ? AI Chat & Roleplay : page.title] (online, free, no sign-up, unlimited)
description = AI RP - A completely free & simple roleplay AI / "Character AI" chat using Perchance's new AI text generation feature - chat with AI characters. Just create a character and a scenario for the chat/roleplay, and send a message. A simple roleplay AI chat bot with no login/sign-up needed - completely free! No account needed 😌 It's fast and has no limits on daily usage. Can do basically any character/scenario type - stories, anime characters, warrior cats roleplay, funny/silly/cute/wholesome, friend/companion/romantic/boyfriend/girlfriend AI/chatbot, movie and TV show characters - if you can describe it, then you can probably create your own chat bot for it. Basically a simple CAI / Character AI alternative. This AI chat has no filter, so 18+ chat content can be produced if the character/chat scenario is NSFW. You can define restrictions, or lack thereof via the character's description.
commentChannels
general
commentPlaceholderText = add a friendly comment...
// Can add options under each channel to overwrite ot add to the below defaults. See all options here: https://perchance.org/comments-plugin
chill
commentPlaceholderText = add a friendly comment...
rp
commentPlaceholderText = type a response...
spam
commentPlaceholderText = for testing stuff, screaming, etc.
To add the functionality for creating another Bot Character Description box in the existing code, you need to create a button that, when clicked, duplicates the structure of the existing character description area. This includes appending a new textarea for the bot description, a corresponding name input, and any relevant controls. Below, I'll provide a suggested approach to modify the pertinent sections of the code to include this functionality.
Here's how you can achieve that:
Step 1: Create a button for adding new Bot Character Description boxes
Insert a new button into the HTML where you want to allow the user to add additional character description boxes. For this example, let's assume you have a section where bot descriptions are listed.
html
<button id="addCharacterButton">Add Another Bot Character Description</button>
Step 2: Create a function to duplicate the character description area
You will need to implement a JavaScript function that will handle the creation of new input fields when this button is clicked. You will duplicate the current bot description structure and append it to the existing character description section.
Add the following function in your JavaScript code:
javascript
document.getElementById('addCharacterButton').addEventListener('click', function() {
// Create a new character container
const characterContainer = document.createElement('div');
characterContainer.classList.add('character-description-container');
// Create input for bot name
const newBotNameInput = document.createElement('input');
newBotNameInput.placeholder = "Bot Name";
newBotNameInput.classList.add('bot-name');
// Create textarea for bot description
const newBotDescription = document.createElement('textarea');
newBotDescription.placeholder = "Bot Description";
newBotDescription.classList.add('bot-description');
// Append the inputs to the container
characterContainer.appendChild(newBotNameInput);
characterContainer.appendChild(newBotDescription);
// Append the new character container to the existing character description area
document.getElementById('characterDescriptionArea').appendChild(characterContainer);
});
Step 3: Update the event listeners or state management
Make sure that any event listeners or state management you have currently set up for the existing input boxes will also apply to these new input fields. You may need to adjust your existing code for functions like generateCharacterDescription, ensure it can handle multiple inputs.
Complete Example
Here is how the relevant part of your HTML/JavaScript might look:
Make sure to apply any necessary styles to your new character description boxes and ensure that your backend logic can process these new boxes appropriately when generating output or responding to interactions.
By following these steps, users can now click on the "Add Another Bot Character Description" button to create multiple instances of bot character descriptions dynamically!