@@ -158,11 +158,23 @@ export const addUserMessage = (msg: UserMessage) => {
158158 } ;
159159
160160 messageWrapper . appendChild ( messageElement ) ;
161+ const lastMsg = globalState . modal . chatMessages . lastElementChild ;
161162 globalState . modal . chatMessages . appendChild ( messageWrapper ) ;
162163
163- const userMsgHeight = messageWrapper . firstElementChild ?. clientHeight ?? 0 ;
164- const msgHeight = userMsgHeight < 100 ? - 10 : - 1 * ( userMsgHeight - 62 ) ;
165- messageWrapper . style . setProperty ( '--user-msg-height' , `${ msgHeight } px` ) ;
164+ if ( lastMsg ) {
165+ lastMsg . classList . remove ( 'new' ) ;
166+ }
167+
168+ messageWrapper . syncHeight = ( ) => {
169+ const userMsgHeight = messageElement . clientHeight ;
170+ const msgHeight = userMsgHeight < 100 ? - 10 : - 1 * ( userMsgHeight - 62 ) ;
171+ messageWrapper . style . setProperty (
172+ '--user-msg-height' ,
173+ `${ globalState . modal . chatContainer . clientHeight - msgHeight - 50 } px` ,
174+ ) ;
175+ } ;
176+
177+ messageWrapper . syncHeight ?.( ) ;
166178
167179 return messageWrapper ;
168180} ;
@@ -192,16 +204,6 @@ export const addAssistantMessage = (msg: AssistantMessage, config: LocalChatConf
192204 `message-wrapper ai ${ msg . init ? '' : 'new' } ` ,
193205 ) ;
194206
195- if ( globalState . modal . chatMessages . lastElementChild ?. firstElementChild ) {
196- const msgHeight =
197- globalState . modal . chatMessages . lastElementChild . firstElementChild . clientHeight < 100
198- ? globalState . modal . chatMessages . lastElementChild . firstElementChild . clientHeight
199- : globalState . modal . chatMessages . lastElementChild . firstElementChild . clientHeight -
200- ( globalState . modal . chatMessages . lastElementChild . firstElementChild . clientHeight - 62 ) +
201- 10 ;
202- messageWrapper . style . setProperty ( '--user-msg-height' , `${ msgHeight } px` ) ;
203- }
204-
205207 const messageElement = createElement ( 'div' , 'message ai' ) ;
206208 messageElement . dataset . id = msg . id ;
207209 const md = markdownit ( {
@@ -353,14 +355,38 @@ export const addAssistantMessage = (msg: AssistantMessage, config: LocalChatConf
353355
354356 messageWrapper . appendChild ( messageElement ) ;
355357
358+ const lastMsg = globalState . modal . chatMessages . lastElementChild ;
359+
356360 globalState . modal . chatMessages . appendChild ( messageWrapper ) ;
357361
362+ if ( lastMsg ) {
363+ lastMsg . classList . remove ( 'new' ) ;
364+ }
365+
366+ messageWrapper . syncHeight = ( ) => {
367+ if ( lastMsg ?. firstElementChild ) {
368+ const msgHeight = lastMsg . classList . contains ( 'user' )
369+ ? lastMsg . firstElementChild . clientHeight < 100
370+ ? lastMsg . firstElementChild . clientHeight
371+ : lastMsg . firstElementChild . clientHeight -
372+ ( lastMsg . firstElementChild . clientHeight - 62 ) +
373+ 10
374+ : - 10 ;
375+ messageWrapper . style . setProperty (
376+ '--user-msg-height' ,
377+ `${ globalState . modal . chatContainer . clientHeight - msgHeight - 50 } px` ,
378+ ) ;
379+ }
380+ } ;
381+
358382 messageWrapper . update = ( msg ) => {
359383 const content =
360384 msg . contentType === 'image' ? `<img src="${ textContent } " />` : md . render ( msg . content ?? '' ) ;
361385 shadow . updateContent ( content ) ;
362386 } ;
363387
388+ messageWrapper . syncHeight ?.( ) ;
389+
364390 return messageWrapper ;
365391} ;
366392
@@ -379,7 +405,12 @@ export const renderMessage = (msg: Message, config: LocalChatConfig) => {
379405 }
380406
381407 if ( msg . __type === 'AssistantMessage' ) {
382- return addAssistantMessage ( msg , config ) ;
408+ const el = addAssistantMessage ( msg , config ) ;
409+ if ( ! msg . init && ! el . previousElementSibling ?. classList . contains ( 'user' ) ) {
410+ scrollToBottom ( 'smooth' ) ;
411+ }
412+
413+ return el ;
383414 }
384415
385416 return ;
0 commit comments