File size: 31,527 Bytes
adca48b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 |
/**
* Message Manager - Handles chat messages, streaming, and code execution display
*/
import { Logger } from '../core/logger.js';
import { domManager } from './dom-manager.js';
export class MessageManager {
constructor() {
this.streamingMessages = new Map();
this.executionSpinners = new Map();
}
addMessage(sender, content, iteration = null, promptDetails = null) {
const messageDiv = document.createElement('div');
messageDiv.className = 'chat-message';
if (promptDetails) {
messageDiv.classList.add('expandable-message');
}
const avatarClass = sender === 'PIPS' || sender === 'PIPS System' ? 'avatar-pips' :
sender === 'AI Code Reviewer' ? 'avatar-reviewer' :
sender.includes('AI') ? 'avatar-llm' : 'avatar-system';
const avatarLetter = sender === 'PIPS' || sender === 'PIPS System' ? 'P' :
sender === 'AI Code Reviewer' ? 'QA' :
sender.includes('AI') ? 'AI' : 'S';
const iterationBadge = iteration ?
`<span class="iteration-badge">Iteration ${iteration}</span>` : '';
// Create expand toggle if prompt details are available
const expandToggle = promptDetails ? `
<button class="expand-toggle" onclick="window.pipsApp.toggleExpandMessage(this)">
<i data-feather="chevron-down" style="width: 12px; height: 12px;"></i>
Show Prompt
</button>
` : '';
// Create expandable content if prompt details are available
const expandableContent = promptDetails ? `
<div class="expandable-content">
<div class="expandable-content-inner">
${promptDetails.description ? `<div class="prompt-description">${this.escapeHtml(promptDetails.description)}</div>` : ''}
<div class="prompt-conversation">
${promptDetails.conversation.map(msg => {
// Format content based on its structure
let formattedContent = '';
if (typeof msg.content === 'string') {
// Check if content looks like structured data or contains code blocks
if (msg.content.includes('```') || msg.content.includes('{') || msg.content.includes('[')) {
// Use markdown parsing for structured content
formattedContent = marked ? marked.parse(msg.content) : msg.content.replace(/\n/g, '<br>');
} else {
// Escape HTML but preserve line breaks for simple text
formattedContent = this.escapeHtml(msg.content).replace(/\n/g, '<br>');
}
} else if (Array.isArray(msg.content)) {
// Handle multimodal content (like image + text)
formattedContent = msg.content.map(item => {
if (item.type === 'text') {
return this.escapeHtml(item.text).replace(/\n/g, '<br>');
} else if (item.type === 'image_url') {
return '<div class="prompt-image">[Image content]</div>';
}
return this.escapeHtml(JSON.stringify(item));
}).join('');
} else {
// Fallback for other content types
formattedContent = this.escapeHtml(JSON.stringify(msg.content, null, 2)).replace(/\n/g, '<br>');
}
return `
<div class="prompt-message ${msg.role}">
<div class="prompt-role">${msg.role}</div>
<div class="prompt-content">${formattedContent}</div>
</div>
`;
}).join('')}
</div>
</div>
</div>
` : '';
messageDiv.innerHTML = `
<div class="message-header">
<div class="message-avatar ${avatarClass}">${avatarLetter}</div>
<span class="message-sender">${this.escapeHtml(sender)}</span>
${iterationBadge}
</div>
<div class="message-content">
${marked ? marked.parse(content) : content}
${expandToggle}
${expandableContent}
</div>
`;
domManager.getElement('chatArea').appendChild(messageDiv);
// Re-highlight code blocks
if (typeof Prism !== 'undefined') {
Prism.highlightAll();
}
// Replace feather icons for the new expand toggle
if (promptDetails) {
feather.replace(messageDiv);
}
this.smartScrollToBottom();
// Save message incrementally during solving
this.saveMessageIncremental(sender, content, iteration, promptDetails);
}
displayFinalAnswer(answer) {
Logger.debug('MessageManager', 'displayFinalAnswer called with:', answer);
if (!answer || answer.trim() === '') {
Logger.warn('MessageManager', 'Empty or null final answer provided');
return;
}
// Remove any existing final answer elements to avoid duplicates
const existingAnswers = domManager.getElement('chatArea').querySelectorAll('.final-answer');
existingAnswers.forEach(el => el.remove());
const answerDiv = document.createElement('div');
answerDiv.className = 'final-answer';
if (typeof answer === 'string') {
if (answer.includes('<') && answer.includes('>')) {
answerDiv.innerHTML = answer;
} else {
answerDiv.textContent = answer;
}
} else {
answerDiv.textContent = String(answer);
}
domManager.getElement('chatArea').appendChild(answerDiv);
setTimeout(() => {
this.smartScrollToBottom();
}, 100);
}
smartScrollToBottom() {
const chatArea = domManager.getElement('chatArea');
const threshold = 100;
const shouldAutoScroll = (chatArea.scrollTop + chatArea.clientHeight >=
chatArea.scrollHeight - threshold);
if (shouldAutoScroll) {
chatArea.scrollTop = chatArea.scrollHeight;
}
}
escapeHtml(text) {
const div = document.createElement('div');
div.textContent = text;
return div.innerHTML;
}
// STREAMING MESSAGE METHODS
showAIThinkingIndicator(iteration, senderName = 'AI Assistant') {
// Remove any existing thinking indicator for this iteration and sender
this.removeAIThinkingIndicator(iteration, senderName);
const messageDiv = document.createElement('div');
messageDiv.className = 'chat-message ai-thinking';
messageDiv.setAttribute('data-iteration', iteration);
messageDiv.setAttribute('data-sender', senderName);
// Determine avatar based on sender
let avatarClass, avatarLetter, thinkingText;
if (senderName === 'AI Code Reviewer') {
avatarClass = 'avatar-reviewer';
avatarLetter = 'QA';
thinkingText = 'Code reviewer is analyzing...';
} else {
avatarClass = 'avatar-llm';
avatarLetter = 'AI';
thinkingText = 'AI is thinking...';
}
messageDiv.innerHTML = `
<div class="message-header">
<div class="message-avatar ${avatarClass}">${avatarLetter}</div>
<span class="message-sender">${senderName}</span>
${iteration ? `<span class="iteration-badge">Iteration ${iteration}</span>` : ''}
</div>
<div class="message-content">
<div class="streaming-indicator">
<div class="spinner"></div>
<span>${thinkingText}</span>
</div>
</div>
`;
domManager.getElement('chatArea').appendChild(messageDiv);
this.smartScrollToBottom();
}
removeAIThinkingIndicator(iteration, senderName = null) {
const thinkingElements = domManager.getElement('chatArea').querySelectorAll('.ai-thinking');
thinkingElements.forEach(el => {
const matchesIteration = !iteration || el.getAttribute('data-iteration') == iteration;
const matchesSender = !senderName || el.getAttribute('data-sender') === senderName;
if (matchesIteration && matchesSender) {
el.remove();
}
});
}
updateStreamingMessage(token, iteration, sender) {
// Create a unique identifier for this streaming message based on iteration and sender
const streamingId = `${iteration}-${sender}`;
// Find or create streaming message
let streamingMessage = domManager.getElement('chatArea').querySelector(`[data-streaming-id="${streamingId}"]`);
if (!streamingMessage) {
// Remove thinking indicator if present for this specific sender
this.removeAIThinkingIndicator(iteration, sender);
// Create new streaming message
streamingMessage = document.createElement('div');
streamingMessage.className = 'chat-message streaming-message';
streamingMessage.setAttribute('data-streaming-iteration', iteration);
streamingMessage.setAttribute('data-streaming-id', streamingId);
streamingMessage.setAttribute('data-sender', sender);
// Determine avatar based on sender
let avatarClass, avatarLetter;
if (sender === 'AI Code Reviewer') {
avatarClass = 'avatar-reviewer';
avatarLetter = 'QA';
} else {
avatarClass = 'avatar-llm';
avatarLetter = 'AI';
}
streamingMessage.innerHTML = `
<div class="message-header">
<div class="message-avatar ${avatarClass}">${avatarLetter}</div>
<span class="message-sender">${sender}</span>
${iteration ? `<span class="iteration-badge">Iteration ${iteration}</span>` : ''}
</div>
<div class="message-content">
<div class="streaming-text" data-content=""></div>
</div>
`;
domManager.getElement('chatArea').appendChild(streamingMessage);
}
// Update streaming content
const streamingText = streamingMessage.querySelector('.streaming-text');
const currentContent = streamingText.getAttribute('data-content') || '';
const newContent = currentContent + token;
streamingText.setAttribute('data-content', newContent);
// Remove any existing typing indicators first
const existingIndicators = streamingText.querySelectorAll('.typing-indicator');
existingIndicators.forEach(indicator => indicator.remove());
// Parse markdown if available
if (typeof marked !== 'undefined') {
streamingText.innerHTML = marked.parse(newContent);
} else {
streamingText.textContent = newContent;
}
// Add typing indicator at the very end of the content
const typingIndicator = document.createElement('span');
typingIndicator.className = 'typing-indicator';
// Find the last element in the streaming text and append the cursor inline
const lastElement = streamingText.lastElementChild;
if (lastElement && (lastElement.tagName === 'P' || lastElement.tagName === 'DIV' || lastElement.tagName === 'SPAN')) {
// Append to the last paragraph/div/span element to keep it inline
lastElement.appendChild(typingIndicator);
} else {
// If no suitable element found, append directly to streaming text
streamingText.appendChild(typingIndicator);
}
this.smartScrollToBottom();
}
finalizeStreamingMessage(iteration, sender = null) {
// If sender is specified, find the specific streaming message for that sender
// Otherwise, finalize all streaming messages for the iteration (backward compatibility)
let query;
if (sender) {
const streamingId = `${iteration}-${sender}`;
query = `[data-streaming-id="${streamingId}"]`;
} else {
query = `[data-streaming-iteration="${iteration}"]`;
}
const streamingMessages = domManager.getElement('chatArea').querySelectorAll(query);
streamingMessages.forEach(streamingMessage => {
// Remove typing indicator
const typingIndicator = streamingMessage.querySelector('.typing-indicator');
if (typingIndicator) {
typingIndicator.remove();
}
// Remove streaming attributes
streamingMessage.classList.remove('streaming-message');
streamingMessage.removeAttribute('data-streaming-iteration');
streamingMessage.removeAttribute('data-streaming-id');
// Re-highlight code blocks
if (typeof Prism !== 'undefined') {
Prism.highlightAll();
}
});
}
// CODE EXECUTION METHODS
showExecutionSpinner(iteration) {
// Remove any existing execution spinner for this iteration
this.removeExecutionSpinner(iteration);
const spinnerDiv = document.createElement('div');
spinnerDiv.className = 'execution-spinner';
spinnerDiv.setAttribute('data-execution-iteration', iteration);
spinnerDiv.innerHTML = `
<div class="spinner"></div>
<span>Executing code...</span>
`;
domManager.getElement('chatArea').appendChild(spinnerDiv);
this.smartScrollToBottom();
}
removeExecutionSpinner(iteration) {
const spinners = domManager.getElement('chatArea').querySelectorAll('.execution-spinner');
spinners.forEach(spinner => {
if (!iteration || spinner.getAttribute('data-execution-iteration') == iteration) {
spinner.remove();
}
});
}
displayExecutionResult(result, iteration, isError = false) {
const resultDiv = document.createElement('div');
resultDiv.className = `execution-result ${isError ? 'error' : ''}`;
resultDiv.textContent = result;
domManager.getElement('chatArea').appendChild(resultDiv);
this.smartScrollToBottom();
}
displayCode(code, iteration) {
const codeDiv = document.createElement('div');
codeDiv.className = 'code-block';
codeDiv.innerHTML = `<pre><code class="language-python">${this.escapeHtml(code)}</code></pre>`;
domManager.getElement('chatArea').appendChild(codeDiv);
if (typeof Prism !== 'undefined') {
Prism.highlightAll();
}
this.smartScrollToBottom();
}
toggleExpandMessage(button) {
const expandToggle = button;
const messageContent = button.closest('.message-content');
const expandableContent = messageContent.querySelector('.expandable-content');
if (!expandableContent) return;
const isExpanded = expandableContent.classList.contains('expanded');
if (isExpanded) {
expandableContent.classList.remove('expanded');
expandToggle.classList.remove('expanded');
expandToggle.innerHTML = `
<i data-feather="chevron-down" style="width: 12px; height: 12px;"></i>
Show Prompt
`;
} else {
expandableContent.classList.add('expanded');
expandToggle.classList.add('expanded');
expandToggle.innerHTML = `
<i data-feather="chevron-up" style="width: 12px; height: 12px;"></i>
Hide Prompt
`;
}
// Replace feather icons
feather.replace(expandToggle);
// Scroll to keep the message in view if needed
setTimeout(() => {
if (!isExpanded) {
this.smartScrollToBottom();
}
}, 300);
}
downloadChat() {
const chatContent = domManager.getElement('chatArea').innerHTML;
const blob = new Blob([`
<!DOCTYPE html>
<html>
<head>
<title>PIPS Chat Export</title>
<style>
body { font-family: Arial, sans-serif; margin: 20px; }
.chat-message { margin-bottom: 20px; }
.message-header { font-weight: bold; margin-bottom: 5px; }
.message-content { margin-left: 20px; }
</style>
</head>
<body>
<h1>PIPS Chat Export</h1>
<div class="chat-area">${chatContent}</div>
</body>
</html>
`], { type: 'text/html' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = `pips_chat_${new Date().toISOString().split('T')[0]}.html`;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
}
// SESSION MANAGEMENT METHODS
getCurrentChatHistory() {
const chatArea = domManager.getElement('chatArea');
if (!chatArea) {
Logger.warn('MessageManager', 'Chat area not found');
return [];
}
const messages = chatArea.querySelectorAll('.chat-message');
const history = [];
messages.forEach(message => {
const senderElement = message.querySelector('.message-sender');
const contentElement = message.querySelector('.message-content');
const iterationElement = message.querySelector('.iteration-badge');
if (!senderElement || !contentElement) {
Logger.debug('MessageManager', 'Skipping malformed message');
return; // Skip malformed messages
}
const sender = senderElement.textContent || 'Unknown';
let content = '';
// Get content - extract only the main content, excluding expandable elements
let contentToSave = '';
const contentChildren = Array.from(contentElement.children);
// Look for the main content, excluding expand toggles and expandable content
contentChildren.forEach(child => {
if (!child.classList.contains('expand-toggle') &&
!child.classList.contains('expandable-content')) {
contentToSave += child.outerHTML;
}
});
// If no child elements found, get direct text content
if (!contentToSave) {
// Get text nodes directly, excluding expand button text
const clonedContent = contentElement.cloneNode(true);
const expandToggle = clonedContent.querySelector('.expand-toggle');
const expandableContent = clonedContent.querySelector('.expandable-content');
if (expandToggle) expandToggle.remove();
if (expandableContent) expandableContent.remove();
contentToSave = clonedContent.innerHTML.trim() || clonedContent.textContent.trim();
}
content = contentToSave;
const iteration = iterationElement ? iterationElement.textContent : null;
// Skip the welcome message
if (sender === 'PIPS System' && content.includes('Welcome to PIPS')) {
return;
}
// Skip empty messages but be more specific about what to filter
if (!content || content === '') {
Logger.debug('MessageManager', 'Skipping empty message');
return;
}
// Skip only currently active streaming indicators (not completed messages that might have streaming classes)
if (message.classList.contains('ai-thinking') ||
message.classList.contains('streaming-message') ||
content.includes('AI is thinking...') ||
content.includes('Executing code...')) {
Logger.debug('MessageManager', 'Skipping active streaming indicator');
return;
}
// Check if this message has prompt details
const expandableContent = message.querySelector('.expandable-content');
let promptDetails = null;
if (expandableContent) {
// Extract prompt details from the DOM
const promptDescription = expandableContent.querySelector('.prompt-description');
const promptMessages = expandableContent.querySelectorAll('.prompt-message');
if (promptMessages.length > 0) {
promptDetails = {
description: promptDescription ? promptDescription.textContent : '',
conversation: Array.from(promptMessages).map(promptMsg => ({
role: promptMsg.querySelector('.prompt-role').textContent.toLowerCase(),
content: promptMsg.querySelector('.prompt-content').textContent
}))
};
}
}
history.push({
sender,
content,
iteration,
promptDetails,
timestamp: new Date().toISOString()
});
});
Logger.debug('MessageManager', `Extracted ${history.length} messages from chat`);
return history;
}
loadChatHistory(history) {
const chatArea = domManager.getElement('chatArea');
// Find and preserve the welcome message first
let welcomeMessage = null;
const existingMessages = chatArea.querySelectorAll('.chat-message');
existingMessages.forEach(msg => {
const sender = msg.querySelector('.message-sender');
const content = msg.querySelector('.message-content');
if (sender && content &&
sender.textContent === 'PIPS System' &&
content.textContent.includes('Welcome to PIPS')) {
welcomeMessage = msg.cloneNode(true);
}
});
// Clear existing messages
chatArea.innerHTML = '';
// Restore welcome message if it existed
if (welcomeMessage) {
chatArea.appendChild(welcomeMessage);
}
// Load messages from history
if (history && history.length > 0) {
Logger.debug('MessageManager', `Loading ${history.length} messages from history`);
history.forEach((msg, index) => {
if (!msg || !msg.sender || !msg.content) {
Logger.warn('MessageManager', `Skipping invalid message at index ${index}:`, msg);
return;
}
const messageDiv = document.createElement('div');
messageDiv.className = 'chat-message';
const avatarClass = msg.sender === 'PIPS' || msg.sender === 'PIPS System' ? 'avatar-pips' :
msg.sender === 'AI Code Reviewer' ? 'avatar-reviewer' :
msg.sender.includes('AI') ? 'avatar-llm' : 'avatar-system';
const avatarLetter = msg.sender === 'PIPS' || msg.sender === 'PIPS System' ? 'P' :
msg.sender === 'AI Code Reviewer' ? 'QA' :
msg.sender.includes('AI') ? 'AI' : 'S';
const iterationBadge = msg.iteration ?
`<span class="iteration-badge">${this.escapeHtml(msg.iteration)}</span>` : '';
// Handle expandable content for loaded messages
const expandToggle = msg.promptDetails ? `
<button class="expand-toggle" onclick="window.pipsApp.toggleExpandMessage(this)">
<i data-feather="chevron-down" style="width: 12px; height: 12px;"></i>
Show Prompt
</button>
` : '';
const expandableContent = msg.promptDetails ? `
<div class="expandable-content">
<div class="expandable-content-inner">
${msg.promptDetails.description ? `<div class="prompt-description">${this.escapeHtml(msg.promptDetails.description)}</div>` : ''}
<div class="prompt-conversation">
${msg.promptDetails.conversation.map(promptMsg => `
<div class="prompt-message ${promptMsg.role}">
<div class="prompt-role">${promptMsg.role}</div>
<div class="prompt-content">${this.escapeHtml(promptMsg.content)}</div>
</div>
`).join('')}
</div>
</div>
</div>
` : '';
if (msg.promptDetails) {
messageDiv.classList.add('expandable-message');
}
messageDiv.innerHTML = `
<div class="message-header">
<div class="message-avatar ${avatarClass}">${avatarLetter}</div>
<span class="message-sender">${this.escapeHtml(msg.sender)}</span>
${iterationBadge}
</div>
<div class="message-content">
${msg.content}
${expandToggle}
${expandableContent}
</div>
`;
chatArea.appendChild(messageDiv);
});
// Replace feather icons for any expandable messages
if (typeof feather !== 'undefined') {
feather.replace(chatArea);
}
} else {
Logger.debug('MessageManager', 'No chat history to load');
}
// Re-highlight code blocks
if (typeof Prism !== 'undefined') {
Prism.highlightAll();
}
this.smartScrollToBottom();
}
clearChatAndRestoreWelcome() {
const chatArea = domManager.getElement('chatArea');
chatArea.innerHTML = '';
// Add fresh welcome message
const welcomeDiv = document.createElement('div');
welcomeDiv.className = 'chat-message';
welcomeDiv.innerHTML = `
<div class="message-header">
<div class="message-avatar avatar-pips">P</div>
<span class="message-sender">PIPS System</span>
</div>
<div class="message-content">
Welcome to PIPS! Enter a problem in the left panel and click "Solve Problem" to get started.
Don't forget to configure your model settings first.
</div>
`;
chatArea.appendChild(welcomeDiv);
}
// CLEANUP METHODS - for handling session interruptions and failures
cleanupAllActiveIndicators() {
Logger.debug('MessageManager', 'Cleaning up all active indicators');
// Remove all AI thinking indicators
const thinkingElements = domManager.getElement('chatArea').querySelectorAll('.ai-thinking');
thinkingElements.forEach(el => el.remove());
// Remove all execution spinners
const executionSpinners = domManager.getElement('chatArea').querySelectorAll('.execution-spinner');
executionSpinners.forEach(el => el.remove());
// Finalize all streaming messages
const streamingMessages = domManager.getElement('chatArea').querySelectorAll('.streaming-message');
streamingMessages.forEach(streamingMessage => {
// Remove typing indicator
const typingIndicator = streamingMessage.querySelector('.typing-indicator');
if (typingIndicator) {
typingIndicator.remove();
}
// Remove streaming attributes
streamingMessage.classList.remove('streaming-message');
streamingMessage.removeAttribute('data-streaming-iteration');
streamingMessage.removeAttribute('data-streaming-id');
});
// Re-highlight code blocks after cleanup
if (typeof Prism !== 'undefined') {
Prism.highlightAll();
}
Logger.debug('MessageManager', 'All active indicators cleaned up');
}
// For incremental saving during solving - save messages as they come in
saveMessageIncremental(sender, content, iteration = null, promptDetails = null) {
// This is called after each message is added to save it incrementally
// Import sessionManager to avoid circular dependency
import('./session-manager.js').then(({ sessionManager }) => {
if (window.appState && window.appState.currentSessionData) {
// Update chat history with current messages
window.appState.currentSessionData.chatHistory = this.getCurrentChatHistory();
window.appState.currentSessionData.lastUsed = new Date().toISOString();
// Save to storage incrementally
sessionManager.saveCurrentSessionToStorage();
Logger.debug('MessageManager', `Incrementally saved message from ${sender} to session`);
}
}).catch(err => {
Logger.warn('MessageManager', 'Could not save message incrementally:', err);
});
}
}
// Create singleton instance
export const messageManager = new MessageManager(); |