| 1 | /** |
| 2 | * Process group DOM utilities (no store/state) |
| 3 | */ |
| 4 | import { store as preferencesStore } from "/components/sidebar/bottom/preferences/preferences-store.js"; |
| 5 | |
| 6 | export async function applyModeSteps( |
| 7 | detailMode, |
| 8 | showUtils, |
| 9 | chatHistory = document.getElementById("chat-history"), |
| 10 | ) { |
| 11 | const mode = |
| 12 | detailMode || |
| 13 | preferencesStore.detailMode || |
| 14 | "current"; |
| 15 | |
| 16 | if (!chatHistory) return; |
| 17 | |
| 18 | chatHistory.dataset.detailMode = mode; |
| 19 | |
| 20 | const shouldExpand = mode !== "collapsed"; |
| 21 | const allMode = mode === "expanded"; |
| 22 | const showUtilsFlag = |
| 23 | typeof showUtils === "boolean" |
| 24 | ? showUtils |
| 25 | : preferencesStore.showUtils || false; |
| 26 | const pending = []; |
| 27 | const messages = Array.from(chatHistory.querySelectorAll(".process-group")); |
| 28 | const windowEnd = Number(chatHistory.dataset.messageWindowEnd); |
| 29 | const windowTotal = Number(chatHistory.dataset.messageWindowTotal); |
| 30 | const isAtTail = |
| 31 | Number.isFinite(windowEnd) && |
| 32 | Number.isFinite(windowTotal) && |
| 33 | windowEnd >= windowTotal; |
| 34 | for (let i = 0; i < messages.length; i += 1) { |
| 35 | const group = messages[i]; |
| 36 | if (typeof group.__setExpanded === "function") { |
| 37 | pending.push(Promise.resolve(group.__setExpanded(shouldExpand))); |
| 38 | } else { |
| 39 | group.classList.toggle("expanded", shouldExpand); |
| 40 | } |
| 41 | |
| 42 | const steps = Array.from(group.querySelectorAll(".process-step")); |
| 43 | const isComplete = |
| 44 | group.hasAttribute("data-group-complete") || |
| 45 | Boolean(group.querySelector(".process-group-response")); |
| 46 | let currentStep = null; |
| 47 | if ( |
| 48 | mode === "current" && |
| 49 | isAtTail && |
| 50 | group === messages[messages.length - 1] && |
| 51 | !isComplete |
| 52 | ) { |
| 53 | for (let si = steps.length - 1; si >= 0; si -= 1) { |
| 54 | if ( |
| 55 | showUtilsFlag || |
| 56 | !steps[si].classList.contains("message-util") |
| 57 | ) { |
| 58 | currentStep = steps[si]; |
| 59 | break; |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | for (let si = 0; si < steps.length; si += 1) { |
| 65 | const step = steps[si]; |
| 66 | const shouldExpandStep = allMode || step === currentStep; |
| 67 | if (typeof step.__setExpanded === "function") { |
| 68 | pending.push(Promise.resolve(step.__setExpanded(shouldExpandStep))); |
| 69 | } else { |
| 70 | step.classList.toggle("expanded", shouldExpandStep); |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | await Promise.allSettled(pending); |
| 76 | } |