@@ -38,6 +38,7 @@ const stopButton = document.getElementById("stop-button");
3838const settingsButton = document . getElementById ( "settings-button" ) ;
3939const settingsModal = document . getElementById ( "settings-modal" ) ;
4040const closeSettings = document . getElementById ( "close-settings" ) ;
41+ const redoButton = document . getElementById ( 'redo-button' ) ;
4142
4243let abortController = null ;
4344let disableAutoScroll = false ;
@@ -71,13 +72,16 @@ function onChatInput() {
7172}
7273
7374function cleanupAfterMessage ( ) {
74- disableAutoScroll = false ;
7575 chatMessages . scrollTop = chatMessages . scrollHeight ;
7676 chatInput . disabled = false ;
7777 sendButton . style . display = "inline-block" ;
7878 stopButton . style . display = "none" ;
7979 abortController = null ;
80- chatInput . focus ( ) ;
80+ if ( ! disableAutoScroll ) {
81+ scrollToBottom ( ) ;
82+ chatInput . focus ( ) ;
83+ }
84+ disableAutoScroll = false ;
8185}
8286
8387function onWheel ( e ) {
@@ -146,7 +150,6 @@ function fixUploads(str) {
146150 str = uploadedFiles . reduce (
147151 ( text , [ from , to ] ) => text . replaceAll ( from , to ) ,
148152 str ) ;
149- uploadedFiles . length = 0 ;
150153 return str ;
151154}
152155
@@ -452,13 +455,34 @@ function setupSettings() {
452455 } ) ;
453456}
454457
458+ function removeLastDirectChild ( element ) {
459+ if ( element . lastElementChild ) {
460+ element . removeChild ( element . lastElementChild ) ;
461+ }
462+ }
463+
464+ function onRedo ( ) {
465+ if ( ! chatHistory . length )
466+ return ;
467+ removeLastDirectChild ( chatMessages ) ;
468+ let msg = chatHistory . pop ( ) ;
469+ if ( msg . role === "assistant" ) {
470+ removeLastDirectChild ( chatMessages ) ;
471+ msg = chatHistory . pop ( ) ;
472+ }
473+ chatInput . value = msg . content ;
474+ chatInput . focus ( ) ;
475+ chatInput . dispatchEvent ( new Event ( "input" ) ) ; // adjust textarea height
476+ }
477+
455478async function chatbot ( ) {
456479 flagz = await fetchFlagz ( ) ;
457480 updateModelInfo ( ) ;
458481 setupSettings ( ) ;
459482 startChat ( [ { role : "system" , content : getSystemPrompt ( ) } ] ) ;
460483 sendButton . addEventListener ( "click" , sendMessage ) ;
461484 stopButton . addEventListener ( "click" , stopMessage ) ;
485+ redoButton . addEventListener ( "click" , onRedo ) ;
462486 chatInput . addEventListener ( "input" , onChatInput ) ;
463487 chatInput . addEventListener ( "keydown" , onKeyDown ) ;
464488 document . addEventListener ( "wheel" , onWheel ) ;
0 commit comments