feat: add RP markdown support + auto-hide thinking
Browse files- static/app.js +134 -3
static/app.js
CHANGED
|
@@ -9,7 +9,9 @@ const STATE = {
|
|
| 9 |
uploadedFiles: [],
|
| 10 |
conversationHistory: [],
|
| 11 |
gradioClient: null,
|
| 12 |
-
isThinking: false
|
|
|
|
|
|
|
| 13 |
};
|
| 14 |
|
| 15 |
// DOM Elements hooks
|
|
@@ -66,6 +68,77 @@ marked.setOptions({
|
|
| 66 |
}
|
| 67 |
});
|
| 68 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
// Setup Initial State & Event Handlers
|
| 70 |
async function initializeApp() {
|
| 71 |
// Re-query all DOM elements to ensure they are fully resolved
|
|
@@ -142,6 +215,39 @@ async function initializeApp() {
|
|
| 142 |
});
|
| 143 |
}
|
| 144 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
// 4. Register File Upload Actions
|
| 146 |
if (dom.studioUploadTrigger) dom.studioUploadTrigger.addEventListener("click", () => dom.fileUploader.click());
|
| 147 |
if (dom.miniUploadTrigger) dom.miniUploadTrigger.addEventListener("click", () => dom.fileUploader.click());
|
|
@@ -549,6 +655,11 @@ function updateAssistantBubble(id, content, reasoning, duration) {
|
|
| 549 |
const textBox = document.getElementById(`${id}-text-box`);
|
| 550 |
|
| 551 |
if (reasoning) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 552 |
thoughtBox.innerHTML = `
|
| 553 |
<div class="thought-header" id="${id}-thought-toggle">
|
| 554 |
<div class="thought-title-group">
|
|
@@ -556,7 +667,7 @@ function updateAssistantBubble(id, content, reasoning, duration) {
|
|
| 556 |
</div>
|
| 557 |
<div style="display:flex;align-items:center;gap:10px;">
|
| 558 |
<span class="thought-timer">Thought for ${duration}s</span>
|
| 559 |
-
<span class="thought-toggle-icon">
|
| 560 |
</div>
|
| 561 |
</div>
|
| 562 |
<div class="thought-content">${escapeHtml(reasoning)}</div>
|
|
@@ -565,12 +676,18 @@ function updateAssistantBubble(id, content, reasoning, duration) {
|
|
| 565 |
const toggleBtn = document.getElementById(`${id}-thought-toggle`);
|
| 566 |
toggleBtn.addEventListener("click", () => {
|
| 567 |
thoughtBox.classList.toggle("collapsed");
|
|
|
|
|
|
|
|
|
|
|
|
|
| 568 |
});
|
| 569 |
} else {
|
| 570 |
thoughtBox.style.display = "none";
|
| 571 |
}
|
| 572 |
|
| 573 |
-
|
|
|
|
|
|
|
| 574 |
|
| 575 |
// Highlight code blocks and add copy buttons
|
| 576 |
textBox.querySelectorAll("pre code").forEach((el) => {
|
|
@@ -653,6 +770,20 @@ function escapeHtml(text) {
|
|
| 653 |
.replace(/'/g, "'");
|
| 654 |
}
|
| 655 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 656 |
function scrollToBottom() {
|
| 657 |
dom.chatMessagesFeed.scrollTop = dom.chatMessagesFeed.scrollHeight;
|
| 658 |
}
|
|
|
|
| 9 |
uploadedFiles: [],
|
| 10 |
conversationHistory: [],
|
| 11 |
gradioClient: null,
|
| 12 |
+
isThinking: false,
|
| 13 |
+
rpMode: true,
|
| 14 |
+
autoHideThinking: true
|
| 15 |
};
|
| 16 |
|
| 17 |
// DOM Elements hooks
|
|
|
|
| 68 |
}
|
| 69 |
});
|
| 70 |
|
| 71 |
+
// RP (Roleplay) Markdown Processor
|
| 72 |
+
// Handles special RP formatting:
|
| 73 |
+
// *text* -> narration/italics with distinct styling
|
| 74 |
+
// "text" -> dialogue with colored styling and quote marks
|
| 75 |
+
// **text** -> bold emphasis
|
| 76 |
+
// ~~text~~ -> strikethrough (OOC content)
|
| 77 |
+
function processRpMarkdown(text) {
|
| 78 |
+
if (!text) return text;
|
| 79 |
+
|
| 80 |
+
// Protect code blocks from RP processing
|
| 81 |
+
const codeBlocks = [];
|
| 82 |
+
text = text.replace(/(```[\s\S]*?```|`[^`]+`)/g, (match) => {
|
| 83 |
+
codeBlocks.push(match);
|
| 84 |
+
return `%%CODEBLOCK_${codeBlocks.length - 1}%%`;
|
| 85 |
+
});
|
| 86 |
+
|
| 87 |
+
// Protect existing HTML tags
|
| 88 |
+
const htmlBlocks = [];
|
| 89 |
+
text = text.replace(/(<[^>]+>)/g, (match) => {
|
| 90 |
+
htmlBlocks.push(match);
|
| 91 |
+
return `%%HTMLBLOCK_${htmlBlocks.length - 1}%%`;
|
| 92 |
+
});
|
| 93 |
+
|
| 94 |
+
// Process strikethrough ~~text~~ -> OOC styling (done first to avoid conflicts)
|
| 95 |
+
text = text.replace(/~~([^~]+)~~/g, '<span class="rp-ooc">$1</span>');
|
| 96 |
+
|
| 97 |
+
// Process bold **text** (must come before italic)
|
| 98 |
+
text = text.replace(/\*\*([^*]+)\*\*/g, '<strong class="rp-bold">$1</strong>');
|
| 99 |
+
|
| 100 |
+
// Process italic *text* -> narration styling
|
| 101 |
+
// Only match single asterisks (not double), and not inside already-processed tags
|
| 102 |
+
text = text.replace(/(?<!\*)\*(?!\*)([^*\n]+)\*(?!\*)/g, '<em class="rp-narration">$1</em>');
|
| 103 |
+
|
| 104 |
+
// Process dialogue: "text" with smart quotes or regular quotes
|
| 105 |
+
// Matches: "text", \u201Ctext\u201D (curly quotes), \u201Etext\u201D (low quotes)
|
| 106 |
+
text = text.replace(/[\u201C\u201E"]([^"\u201D\n]+)[\u201D"]([^"\u201C\u201E]|$)/g, (match, dialogue, after) => {
|
| 107 |
+
return `<span class="rp-dialogue">\u201C${dialogue}\u201D</span>${after}`;
|
| 108 |
+
});
|
| 109 |
+
|
| 110 |
+
// Process narrative dividers: --- or *** or ___ on their own line
|
| 111 |
+
text = text.replace(/^(---|\*\*\*|___)$/gm, '<hr class="rp-divider">');
|
| 112 |
+
|
| 113 |
+
// Process OOC markers: ((text))
|
| 114 |
+
text = text.replace(/\(\(([^)]+)\)\)/g, '<span class="rp-ooc">(($1))</span>');
|
| 115 |
+
|
| 116 |
+
// Restore HTML blocks
|
| 117 |
+
htmlBlocks.forEach((block, i) => {
|
| 118 |
+
text = text.replace(`%%HTMLBLOCK_${i}%%`, block);
|
| 119 |
+
});
|
| 120 |
+
|
| 121 |
+
// Restore code blocks
|
| 122 |
+
codeBlocks.forEach((block, i) => {
|
| 123 |
+
text = text.replace(`%%CODEBLOCK_${i}%%`, block);
|
| 124 |
+
});
|
| 125 |
+
|
| 126 |
+
return text;
|
| 127 |
+
}
|
| 128 |
+
|
| 129 |
+
// Combined markdown renderer that applies RP processing if enabled
|
| 130 |
+
function renderMarkdown(content) {
|
| 131 |
+
let html = marked.parse(content);
|
| 132 |
+
if (STATE.rpMode) {
|
| 133 |
+
// Process RP markdown on the raw content before marked, then re-render
|
| 134 |
+
// Actually, we need to process the raw text first for RP patterns,
|
| 135 |
+
// then let marked handle the rest
|
| 136 |
+
const rpProcessed = processRpMarkdown(content);
|
| 137 |
+
html = marked.parse(rpProcessed);
|
| 138 |
+
}
|
| 139 |
+
return html;
|
| 140 |
+
}
|
| 141 |
+
|
| 142 |
// Setup Initial State & Event Handlers
|
| 143 |
async function initializeApp() {
|
| 144 |
// Re-query all DOM elements to ensure they are fully resolved
|
|
|
|
| 215 |
});
|
| 216 |
}
|
| 217 |
|
| 218 |
+
// RP Mode toggle listener
|
| 219 |
+
const rpModeToggle = document.getElementById("rp-mode-toggle");
|
| 220 |
+
if (rpModeToggle) {
|
| 221 |
+
rpModeToggle.checked = STATE.rpMode;
|
| 222 |
+
rpModeToggle.addEventListener("change", (e) => {
|
| 223 |
+
STATE.rpMode = e.target.checked;
|
| 224 |
+
// Re-render all existing assistant messages
|
| 225 |
+
rerenderAllAssistantMessages();
|
| 226 |
+
});
|
| 227 |
+
}
|
| 228 |
+
|
| 229 |
+
// Auto-hide thinking toggle listener
|
| 230 |
+
const autoHideToggle = document.getElementById("auto-hide-thinking-toggle");
|
| 231 |
+
if (autoHideToggle) {
|
| 232 |
+
autoHideToggle.checked = STATE.autoHideThinking;
|
| 233 |
+
autoHideToggle.addEventListener("change", (e) => {
|
| 234 |
+
STATE.autoHideThinking = e.target.checked;
|
| 235 |
+
// Apply to all existing thought containers
|
| 236 |
+
document.querySelectorAll(".thought-container").forEach(tc => {
|
| 237 |
+
if (STATE.autoHideThinking) {
|
| 238 |
+
tc.classList.add("collapsed");
|
| 239 |
+
} else {
|
| 240 |
+
tc.classList.remove("collapsed");
|
| 241 |
+
}
|
| 242 |
+
// Update toggle icon
|
| 243 |
+
const iconEl = tc.querySelector(".thought-toggle-icon");
|
| 244 |
+
if (iconEl) {
|
| 245 |
+
iconEl.textContent = tc.classList.contains("collapsed") ? "▶" : "▼";
|
| 246 |
+
}
|
| 247 |
+
});
|
| 248 |
+
});
|
| 249 |
+
}
|
| 250 |
+
|
| 251 |
// 4. Register File Upload Actions
|
| 252 |
if (dom.studioUploadTrigger) dom.studioUploadTrigger.addEventListener("click", () => dom.fileUploader.click());
|
| 253 |
if (dom.miniUploadTrigger) dom.miniUploadTrigger.addEventListener("click", () => dom.fileUploader.click());
|
|
|
|
| 655 |
const textBox = document.getElementById(`${id}-text-box`);
|
| 656 |
|
| 657 |
if (reasoning) {
|
| 658 |
+
// Auto-hide thinking: start collapsed by default
|
| 659 |
+
const startCollapsed = STATE.autoHideThinking ? " collapsed" : "";
|
| 660 |
+
const toggleIcon = STATE.autoHideThinking ? "▶" : "▼";
|
| 661 |
+
thoughtBox.classList.add(startCollapsed.trim());
|
| 662 |
+
|
| 663 |
thoughtBox.innerHTML = `
|
| 664 |
<div class="thought-header" id="${id}-thought-toggle">
|
| 665 |
<div class="thought-title-group">
|
|
|
|
| 667 |
</div>
|
| 668 |
<div style="display:flex;align-items:center;gap:10px;">
|
| 669 |
<span class="thought-timer">Thought for ${duration}s</span>
|
| 670 |
+
<span class="thought-toggle-icon" id="${id}-toggle-icon">${toggleIcon}</span>
|
| 671 |
</div>
|
| 672 |
</div>
|
| 673 |
<div class="thought-content">${escapeHtml(reasoning)}</div>
|
|
|
|
| 676 |
const toggleBtn = document.getElementById(`${id}-thought-toggle`);
|
| 677 |
toggleBtn.addEventListener("click", () => {
|
| 678 |
thoughtBox.classList.toggle("collapsed");
|
| 679 |
+
const iconEl = document.getElementById(`${id}-toggle-icon`);
|
| 680 |
+
if (iconEl) {
|
| 681 |
+
iconEl.textContent = thoughtBox.classList.contains("collapsed") ? "▶" : "▼";
|
| 682 |
+
}
|
| 683 |
});
|
| 684 |
} else {
|
| 685 |
thoughtBox.style.display = "none";
|
| 686 |
}
|
| 687 |
|
| 688 |
+
// Store raw content for re-rendering when RP mode changes
|
| 689 |
+
textBox.dataset.rawContent = content;
|
| 690 |
+
textBox.innerHTML = renderMarkdown(content);
|
| 691 |
|
| 692 |
// Highlight code blocks and add copy buttons
|
| 693 |
textBox.querySelectorAll("pre code").forEach((el) => {
|
|
|
|
| 770 |
.replace(/'/g, "'");
|
| 771 |
}
|
| 772 |
|
| 773 |
+
// Re-render all assistant messages with current RP mode setting
|
| 774 |
+
function rerenderAllAssistantMessages() {
|
| 775 |
+
document.querySelectorAll(".message-bubble.assistant").forEach(bubble => {
|
| 776 |
+
const textBox = bubble.querySelector(".message-text.markdown-body");
|
| 777 |
+
if (textBox && textBox.dataset.rawContent) {
|
| 778 |
+
textBox.innerHTML = renderMarkdown(textBox.dataset.rawContent);
|
| 779 |
+
textBox.querySelectorAll("pre code").forEach((el) => {
|
| 780 |
+
hljs.highlightElement(el);
|
| 781 |
+
});
|
| 782 |
+
addCopyButtons(textBox);
|
| 783 |
+
}
|
| 784 |
+
});
|
| 785 |
+
}
|
| 786 |
+
|
| 787 |
function scrollToBottom() {
|
| 788 |
dom.chatMessagesFeed.scrollTop = dom.chatMessagesFeed.scrollHeight;
|
| 789 |
}
|