Agent Zero AI framework
scrollChatToBottom; chat navigation helpers
3clyp501455e1d21323
diff --git a/webui/components/chat/input/progress.html b/webui/components/chat/input/progress.htmlindex 3a5b1822c..f3f890c79 100644--- a/webui/components/chat/input/progress.html+++ b/webui/components/chat/input/progress.html@@ -2,6 +2,7 @@<head><script type="module">import { store as speechStore } from "/components/chat/speech/speech-store.js";+ import { store as chatNavStore } from "/components/chat/navigation/chat-navigation-store.js";</script></head><body>@@ -9,17 +10,39 @@<h4 id="progress-bar-h"><span id="progress-bar-i">|></span><span id="progress-bar"></span></h4>- <h4 id="progress-bar-stop-speech" x-data x-cloak x-show="$store.speech.isSpeaking">- <span id="stop-speech" @click="$store.speech.stop()" style="cursor: pointer">Stop Speech</span>- </h4>+ <div id="progress-bar-right">+ <div id="chat-nav-buttons" aria-label="Chat navigation">+ <button class="btn-icon-action" title="Scroll to top" x-on:click="$store.chatNavigation.scrollToTop()">+ <span class="material-symbols-outlined">vertical_align_top</span>+ </button>+ <button class="btn-icon-action" title="Previous user message" x-on:click="$store.chatNavigation.scrollToPrevUserMessage()">+ <span class="material-symbols-outlined">keyboard_arrow_up</span>+ </button>+ <button class="btn-icon-action" title="Next user message" x-on:click="$store.chatNavigation.scrollToNextUserMessage()">+ <span class="material-symbols-outlined">keyboard_arrow_down</span>+ </button>+ <button class="btn-icon-action" title="Scroll to bottom" x-on:click="$store.chatNavigation.scrollToBottom()">+ <span class="material-symbols-outlined">vertical_align_bottom</span>+ </button>+ </div>+ <h4 id="progress-bar-stop-speech" x-data x-cloak x-show="$store.speech.isSpeaking">+ <span id="stop-speech" @click="$store.speech.stop()" style="cursor: pointer">Stop Speech</span>+ </h4>+ </div></div><style>- #progress-bar-box { background-color: var(--color-panel); padding: var(--spacing-sm) var(--spacing-md); padding-bottom: 0; display: flex; justify-content: space-between; z-index: 1001; }+ #progress-bar-box { background-color: var(--color-panel); padding: var(--spacing-sm) var(--spacing-md); padding-bottom: 0; display: flex; justify-content: space-between; z-index: 1001; gap: var(--spacing-sm); }#progress-bar-box h4 { margin: 0 1.2em 0 0; }#progress-bar-h { color: var(--color-primary); display: flex; align-items: left; justify-content: flex-start; height: 1.2em; text-overflow: ellipsis; white-space: nowrap; overflow: hidden; font-weight: normal; }#progress-bar-i { font-weight: bold; padding-right: 0.5em; color: var(--color-secondary); }+ #progress-bar-right { display: flex; align-items: center; gap: var(--spacing-sm); flex-shrink: 0; }+ #chat-nav-buttons { display: flex; align-items: center; gap: 2px; opacity: 0.85; }+ #chat-nav-buttons .btn-icon-action { padding: var(--spacing-xs); }+ #chat-nav-buttons .btn-icon-action {+ border: none !important;+ }.shiny-text { background: linear-gradient(to right, var(--color-primary-dark) 20%, var(--color-text) 40%, var(--color-text) 60%, var(--color-primary-dark) 60%); background-size: 200% auto; color: transparent; -webkit-background-clip: text; background-clip: text; animation: shine 1s linear infinite; }@keyframes shine { to { background-position: -200% center; } }.light-mode #progress-bar-i { color: var(--color-border-dark); opacity: 0.5; }diff --git a/webui/components/chat/navigation/chat-navigation-store.js b/webui/components/chat/navigation/chat-navigation-store.jsnew file mode 100644index 000000000..aa2745de4--- /dev/null+++ b/webui/components/chat/navigation/chat-navigation-store.js@@ -0,0 +1,100 @@+import { createStore } from "/js/AlpineStore.js";++const model = {+ // Configuration+ scrollMargin: 60,+ prevTolerance: 35,+ nextTolerance: 5,++ init() {+ // Any initialization if needed+ },++ scrollToTop() {+ const scroller = this._getChatHistoryEl();+ if (scroller) scroller.scrollTo({ top: 0, behavior: "instant" });+ },++ scrollToBottom() {+ if (globalThis.forceScrollChatToBottom) {+ globalThis.forceScrollChatToBottom();+ } else {+ const scroller = this._getChatHistoryEl();+ if (scroller) scroller.scrollTop = scroller.scrollHeight;+ }+ },++ scrollToPrevUserMessage() {+ const scroller = this._getChatHistoryEl();+ if (!scroller) return;++ const positions = this._getUserMessagePositions(scroller);+ const scrollerRect = scroller.getBoundingClientRect();++ const prevThreshold = this.scrollMargin - this.prevTolerance; // 25px++ const currentIndex = positions.findIndex((p) => {+ const relativeTop = p.el.getBoundingClientRect().top - scrollerRect.top;+ return relativeTop >= prevThreshold;+ });++ if (currentIndex > 0) {+ // Go to previous message+ positions[currentIndex - 1].el.scrollIntoView({ block: "start" });+ } else if (currentIndex === 0) {+ // At the first message, scroll to top+ scroller.scrollTo({ top: 0, behavior: "instant" });+ } else if (currentIndex === -1 && positions.length > 0) {+ // All messages are above the threshold (scrolled past), scroll to bottom+ positions[positions.length - 1].el.scrollIntoView({ block: "start" });+ }+ },++ scrollToNextUserMessage() {+ const scroller = this._getChatHistoryEl();+ if (!scroller) return;++ const positions = this._getUserMessagePositions(scroller);+ const scrollerRect = scroller.getBoundingClientRect();++ const nextThreshold = this.scrollMargin + this.nextTolerance; // 65px++ // Find first message below the threshold+ const targetIndex = positions.findIndex((p) => {+ const relativeTop = p.el.getBoundingClientRect().top - scrollerRect.top;+ return relativeTop > nextThreshold;+ });++ if (targetIndex !== -1) {+ // Go to that message+ positions[targetIndex].el.scrollIntoView({ block: "start" });+ } else {+ // No message found below threshold => scroll to bottom+ this.scrollToBottom();+ }+ },++ // Helpers+ _getChatHistoryEl() {+ return document.getElementById("chat-history");+ },++ _getUserMessagePositions(scroller) {+ const userMessageEls = Array.from(+ scroller.querySelectorAll(".message-container.user-container")+ );+ // Helper for getElementTopInScroller+ const getTop = (el) => {+ const elRect = el.getBoundingClientRect();+ const scrollerRect = scroller.getBoundingClientRect();+ return elRect.top - scrollerRect.top + scroller.scrollTop;+ };++ return userMessageEls+ .map((el) => ({ el, top: getTop(el) }))+ .sort((a, b) => a.top - b.top);+ }+};++const store = createStore("chatNavigation", model);+export { store };diff --git a/webui/css/messages.css b/webui/css/messages.cssindex 3875487db..21ef80afd 100644--- a/webui/css/messages.css+++ b/webui/css/messages.css@@ -667,6 +667,7 @@/* margin-bottom: var(--spacing-sm); */width: 100%;max-width: 100%;+ scroll-margin-top: 60px; /* Ensure nice spacing when scrolling into view */}/* User messages appear instantly */diff --git a/webui/index.js b/webui/index.jsindex 76ceb1316..5d4179946 100644--- a/webui/index.js+++ b/webui/index.js@@ -46,6 +46,9 @@ export async function sendMessage() {const hasAttachments = attachmentsWithUrls.length > 0;if (message || hasAttachments) {+ // Sending a message is an explicit user intent to go to the bottom+ forceScrollChatToBottom();+let response;const messageId = generateGUID();@@ -112,6 +115,17 @@ export async function sendMessage() {}globalThis.sendMessage = sendMessage;+function getChatHistoryEl() {+ return document.getElementById("chat-history");+}++function forceScrollChatToBottom() {+ const chatHistoryEl = getChatHistoryEl();+ if (!chatHistoryEl) return;+ chatHistoryEl.scrollTop = chatHistoryEl.scrollHeight;+}+globalThis.forceScrollChatToBottom = forceScrollChatToBottom;+export function toastFetchError(text, error) {console.error(text, error);// Use new frontend error notification system (async, but we don't need to wait)@@ -569,7 +583,8 @@ function scrollChanged(isAtBottom) {export function updateAfterScroll() {// const toleranceEm = 1; // Tolerance in em units// const tolerancePx = toleranceEm * parseFloat(getComputedStyle(document.documentElement).fontSize); // Convert em to pixels- const tolerancePx = 10;+ // Larger trigger zone near bottom for autoscroll+ const tolerancePx = 80;const chatHistory = document.getElementById("chat-history");if (!chatHistory) return;@@ -581,6 +596,13 @@ export function updateAfterScroll() {}globalThis.updateAfterScroll = updateAfterScroll;+import { store as _chatNavigationStore } from "/components/chat/navigation/chat-navigation-store.js";+++// Navigation logic in chat-navigation-store.js+// forceScrollChatToBottom is kept here as it is used by system events++// setInterval(poll, 250);async function startPolling() {