@@ -208,7 +208,17 @@ function getHeightOfLastUserMessage() {
208208}
209209
210210function getHeightOfLastAgentMessage() {
211- return getLastMessageElement (' assistant' )?.clientHeight ?? 0 ;
211+ const lastUserIndex = props .messages .findLastIndex ((message : IMessage ) => message .role === ' user' );
212+ const lastAgentIndex = props .messages .findLastIndex ((message : IMessage ) => message .role === ' assistant' );
213+ // Only reserve spacer space for the current turn's answer, i.e. an assistant message that comes
214+ // after the last user message. Right after a send the "Thinking" placeholder isn't pushed yet, so
215+ // findLastIndex would return the previous turn's answer (which sits above the just-sent user
216+ // message). Subtracting its height shrinks the spacer, making scrollToBottom stop short — and once
217+ // the real placeholder arrives the spacer grows and the true bottom drops below where we scrolled.
218+ if (lastAgentIndex <= lastUserIndex ) {
219+ return 0 ;
220+ }
221+ return messagesRefs .value [lastAgentIndex ]?.clientHeight ?? 0 ;
212222}
213223
214224function getScrollClientHeight() {
@@ -221,15 +231,14 @@ async function updateSpacerHeight() {
221231 }
222232
223233 const clientHeight = getScrollClientHeight ();
224-
225234 if (! clientHeight ) {
226235 return ;
227236 }
228237
229238 const lastUserMessageHeight = getHeightOfLastUserMessage ();
230239 const lastAgentMessageHeight = getHeightOfLastAgentMessage ();
231-
232- spacerHeight .value = Math . max ( 0 , clientHeight - ( lastUserMessageHeight + MASK_HEIGHT + lastAgentMessageHeight )) ;
240+ const newSpacerHeight = Math . max ( 0 , clientHeight - ( lastUserMessageHeight + MASK_HEIGHT + lastAgentMessageHeight ));
241+ spacerHeight .value = newSpacerHeight ;
233242}
234243
235244function scheduleSpacerHeightUpdate() {
@@ -310,8 +319,9 @@ async function handleSendMessage() {
310319
311320 if (clientHeight ) {
312321 showBottomSpacer .value = true ;
322+ await nextTick (); // render the just-sent user message + spacer before measuring their heights
313323 await updateSpacerHeight ();
314- await nextTick ();
324+ await nextTick (); // apply the computed spacer height before scrolling
315325 scrollContainer .value ?.scrollToBottom ();
316326 }
317327}
0 commit comments