@@ -369,9 +369,6 @@ export function useChatStream({
369369
370370 const isNewSession = sessionId && sessionId . match ( / ^ \d { 8 } _ \d { 6 } $ / ) ;
371371 if ( isNewSession ) {
372- console . log (
373- 'useChatStream: Message stream finished for new session, emitting message-stream-finished event'
374- ) ;
375372 window . dispatchEvent ( new CustomEvent ( AppEvents . MESSAGE_STREAM_FINISHED ) ) ;
376373 }
377374
@@ -591,6 +588,7 @@ export function useChatStream({
591588 body : {
592589 session_id : sessionId ,
593590 user_message : newMessage ,
591+ ...( hasExistingMessages && { conversation_so_far : currentState . messages } ) ,
594592 } ,
595593 throwOnError : true ,
596594 signal : abortControllerRef . current . signal ,
@@ -751,55 +749,40 @@ export function useChatStream({
751749 } ) ;
752750
753751 if ( sessionResponse . data ?. conversation ) {
754- const updatedMessages = [ ...sessionResponse . data . conversation ] ;
755-
756- if ( updatedMessages . length > 0 ) {
757- const lastMessage = updatedMessages [ updatedMessages . length - 1 ] ;
758- if ( lastMessage . role === 'user' ) {
759- lastMessage . content = [ { type : 'text' , text : newContent } ] ;
760-
761- for ( const content of message . content ) {
762- if ( content . type === 'image' ) {
763- lastMessage . content . push ( content ) ;
764- }
765- }
766-
767- dispatch ( { type : 'SET_MESSAGES' , payload : updatedMessages } ) ;
768- dispatch ( { type : 'START_STREAMING' } ) ;
769- abortControllerRef . current = new AbortController ( ) ;
770-
771- try {
772- const placeholderMessage = createUserMessage ( newContent ) ;
773-
774- const { stream } = await reply ( {
775- body : {
776- session_id : targetSessionId ,
777- user_message : placeholderMessage ,
778- } ,
779- throwOnError : true ,
780- signal : abortControllerRef . current . signal ,
781- } ) ;
782-
783- await streamFromResponse (
784- stream ,
785- updatedMessages ,
786- dispatch ,
787- onFinish ,
788- targetSessionId
789- ) ;
790- } catch ( error ) {
791- if ( error instanceof Error && error . name === 'AbortError' ) {
792- dispatch ( { type : 'SET_CHAT_STATE' , payload : ChatState . Idle } ) ;
793- } else {
794- throw error ;
795- }
796- }
797- return ;
752+ const truncatedMessages = [ ...sessionResponse . data . conversation ] ;
753+ const updatedUserMessage = createUserMessage ( newContent ) ;
754+
755+ for ( const content of message . content ) {
756+ if ( content . type === 'image' ) {
757+ updatedUserMessage . content . push ( content ) ;
798758 }
799759 }
800760
801- dispatch ( { type : 'SET_MESSAGES' , payload : sessionResponse . data . conversation } ) ;
802- await handleSubmit ( { msg : newContent , images : [ ] } ) ;
761+ const messagesForUI = [ ...truncatedMessages , updatedUserMessage ] ;
762+ dispatch ( { type : 'SET_MESSAGES' , payload : messagesForUI } ) ;
763+ dispatch ( { type : 'START_STREAMING' } ) ;
764+
765+ abortControllerRef . current = new AbortController ( ) ;
766+
767+ try {
768+ const { stream } = await reply ( {
769+ body : {
770+ session_id : targetSessionId ,
771+ user_message : updatedUserMessage ,
772+ conversation_so_far : truncatedMessages ,
773+ } ,
774+ throwOnError : true ,
775+ signal : abortControllerRef . current . signal ,
776+ } ) ;
777+
778+ await streamFromResponse ( stream , messagesForUI , dispatch , onFinish , targetSessionId ) ;
779+ } catch ( error ) {
780+ if ( error instanceof Error && error . name === 'AbortError' ) {
781+ dispatch ( { type : 'SET_CHAT_STATE' , payload : ChatState . Idle } ) ;
782+ } else {
783+ throw error ;
784+ }
785+ }
803786 } else {
804787 await handleSubmit ( { msg : newContent , images : [ ] } ) ;
805788 }
0 commit comments