diff --git a/index.js b/index.js index 608b3ae..2d9fc62 100644 --- a/index.js +++ b/index.js @@ -234,28 +234,40 @@ function processToolCallMessages() { } console.log(`[${extensionName}] Rendering image from URL: ${imgUrl}`); renderContainer.html(`Chat Bubble`); + + // Store the image URL in a data attribute for easier access + $(this).attr('data-sb-image-url', imgUrl); } else { renderContainer.html(tool.result); } // Remove any existing rendered images $(this).find('.sillybubble-rendered-image').remove(); + $('.sillybubble-collapsed-image').remove(); - // Add this image after the summary element, but only if details is present + // Create a special always-visible container for collapsed state + const collapsedContainer = $('
'); + collapsedContainer.html(renderContainer.html()); + + // Add the image to the mes_block, BEFORE the details element + // This ensures it's visible when details is collapsed + detailsElement.before(collapsedContainer); + + // Also add inside the details for when it's open if (summaryElement.length) { - // Place the image both in summary and in the details content for maximum compatibility - summaryElement.after(renderContainer.clone()); - - // Also add to the mes_reasoning div if it exists + // Add inside the details content for when it's expanded const reasoningDiv = $(this).find('.mes_reasoning'); if (reasoningDiv.length) { - reasoningDiv.prepend(renderContainer); + reasoningDiv.prepend(renderContainer.clone()); } } // Mark this message as processed $(this).attr('data-sb-processed', 'true'); + // Manage visibility based on details open state + handleDetailsVisibility($(this), detailsElement); + console.log(`[${extensionName}] Rendered chat bubble image in tool call: ${imgUrl}`); } } @@ -271,6 +283,33 @@ function processToolCallMessages() { }); } +// Helper function to handle visibility of images based on details open state +function handleDetailsVisibility(messageElement, detailsElement) { + if (detailsElement.prop('open')) { + // If details is open, hide the collapsed version + messageElement.find('.sillybubble-collapsed-image').hide(); + } else { + // If details is closed, show the collapsed version + messageElement.find('.sillybubble-collapsed-image').show(); + } + + // Set up a mutation observer to watch for the open attribute changing + const observer = new MutationObserver((mutations) => { + mutations.forEach((mutation) => { + if (mutation.attributeName === 'open') { + if (detailsElement.prop('open')) { + messageElement.find('.sillybubble-collapsed-image').hide(); + } else { + messageElement.find('.sillybubble-collapsed-image').show(); + } + } + }); + }); + + // Start observing the details element + observer.observe(detailsElement[0], { attributes: true }); +} + // Observer function to watch for new messages function setupMessageObserver() { // Create a mutation observer to monitor the chat for new messages @@ -358,6 +397,7 @@ jQuery(async () => { // Add CSS for rendered images $('head').append(` `);