Spaces:
Running
Running
File size: 1,026 Bytes
d0878a6 | 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 | /**
* Keep the latest user question at the top of the messages viewport while
* advisor answers stream in. Call once after send — do not re-pin on every
* token, or the view will jump.
*/
export function pinElementToScrollerTop(scroller, element, { behavior = 'smooth' } = {}) {
if (!scroller || !element) return false;
const scrollerRect = scroller.getBoundingClientRect();
const elRect = element.getBoundingClientRect();
const nextTop = scroller.scrollTop + (elRect.top - scrollerRect.top);
const top = Math.max(0, nextTop);
if (typeof scroller.scrollTo === 'function') {
scroller.scrollTo({ top, behavior });
} else {
scroller.scrollTop = top;
}
return true;
}
export function findLatestUserMessage(messages) {
if (!Array.isArray(messages)) return null;
for (let i = messages.length - 1; i >= 0; i -= 1) {
const message = messages[i];
if (message?.type === 'user' && !message.isReferenceSnippet) {
return message;
}
}
return null;
}
|