@@ -76,28 +76,36 @@ function streamMessage(cssClass, text) {
7676 msg . classList . add ( "message" , cssClass ) ;
7777 chat . appendChild ( msg ) ;
7878
79- const lines = text . split ( "\n" ) ;
80- const isNumberedList = lines . every ( line => / ^ \d + \. \s + / . test ( line . trim ( ) ) ) && lines . length > 1 ;
81-
82- if ( isNumberedList ) {
83- const ol = document . createElement ( "ol" ) ;
84- lines . forEach ( line => {
85- const li = document . createElement ( "li" ) ;
86- li . textContent = line . replace ( / ^ \d + \. \s + / , "" ) . trim ( ) ;
87- ol . appendChild ( li ) ;
88- } ) ;
89- msg . appendChild ( ol ) ;
90- chat . scrollTop = chat . scrollHeight ;
91- return ;
79+ const lines = text . split ( "\n" ) . filter ( line => line . trim ( ) !== "" ) ;
80+
81+ const isNumberedList = lines . length > 1 && lines . every ( line => / ^ \d + \. \s + / . test ( line . trim ( ) ) ) ;
82+ const isBulletedList = lines . length > 1 && lines . every ( line => / ^ [ - * • ] \s + / . test ( line . trim ( ) ) ) ;
83+
84+ if ( isNumberedList || isBulletedList ) {
85+ const listElement = document . createElement ( isNumberedList ? "ol" : "ul" ) ;
86+ msg . appendChild ( listElement ) ;
87+ let i = 0 ;
88+
89+ const interval = setInterval ( ( ) => {
90+ if ( i < lines . length ) {
91+ const li = document . createElement ( "li" ) ;
92+ li . textContent = lines [ i ] . replace ( / ^ ( \d + \. \s + | [ - * • ] \s + ) / , "" ) . trim ( ) ;
93+ listElement . appendChild ( li ) ;
94+ chat . scrollTop = chat . scrollHeight ;
95+ i ++ ;
96+ } else {
97+ clearInterval ( interval ) ;
98+ }
99+ } , 200 ) ;
100+ } else {
101+ let index = 0 ;
102+ const interval = setInterval ( ( ) => {
103+ if ( index < text . length ) {
104+ msg . textContent += text . charAt ( index ++ ) ;
105+ chat . scrollTop = chat . scrollHeight ;
106+ } else {
107+ clearInterval ( interval ) ;
108+ }
109+ } , 15 ) ;
92110 }
93-
94- let index = 0 ;
95- const interval = setInterval ( ( ) => {
96- if ( index < text . length ) {
97- msg . textContent += text . charAt ( index ++ ) ;
98- chat . scrollTop = chat . scrollHeight ;
99- } else {
100- clearInterval ( interval ) ;
101- }
102- } , 15 ) ;
103111}
0 commit comments