@@ -755,9 +755,15 @@ export function useChat(
755755 const lines = buffer . split ( '\n' )
756756 buffer = lines . pop ( ) || ''
757757 pendingLines . push ( ...lines )
758+ if ( pendingLines . length === 0 ) {
759+ continue
760+ }
758761 }
759762
760- const line = pendingLines . shift ( ) !
763+ const line = pendingLines . shift ( )
764+ if ( line === undefined ) {
765+ continue
766+ }
761767 if ( isStale ( ) ) {
762768 pendingLines . length = 0
763769 continue
@@ -893,9 +899,18 @@ export function useChat(
893899 const nextSession : StreamingFilePreview = {
894900 ...prevSession ,
895901 toolCallId : id ,
902+ ...( typeof payload . operation === 'string'
903+ ? { operation : payload . operation }
904+ : { } ) ,
905+ ...( typeof payload . fileId === 'string'
906+ ? { fileId : payload . fileId , targetKind : 'file_id' as const }
907+ : { } ) ,
896908 }
897909 sessions . set ( id , nextSession )
898910 activeFilePreviewToolCallIdRef . current = id
911+ if ( nextSession . fileId ) {
912+ setActiveResourceId ( nextSession . fileId )
913+ }
899914 setStreamingFile ( nextSession )
900915 break
901916 }
@@ -958,9 +973,18 @@ export function useChat(
958973
959974 if ( previewPhase === 'file_preview_content' ) {
960975 const content = typeof payload . content === 'string' ? payload . content : ''
961- const isAppendOp = prevSession . operation === 'append'
962- const prevContent = streamingFileRef . current ?. content ?? ''
963- const nextContent = isAppendOp ? prevContent + content : content
976+ const contentMode =
977+ typeof payload . contentMode === 'string' ? payload . contentMode : undefined
978+ let nextContent : string
979+ if ( contentMode === 'snapshot' ) {
980+ nextContent = content
981+ } else if ( contentMode === 'delta' ) {
982+ nextContent = ( prevSession . content ?? '' ) + content
983+ } else {
984+ const isAppendOp = prevSession . operation === 'append'
985+ const prevContent = streamingFileRef . current ?. content ?? ''
986+ nextContent = isAppendOp ? prevContent + content : content
987+ }
964988 const nextSession : StreamingFilePreview = {
965989 ...prevSession ,
966990 content : nextContent ,
@@ -971,6 +995,34 @@ export function useChat(
971995 setStreamingFile ( nextSession )
972996 break
973997 }
998+
999+ if ( previewPhase === 'file_preview_complete' ) {
1000+ const fileId =
1001+ typeof payload . fileId === 'string' ? payload . fileId : prevSession . fileId
1002+ const resultData = asPayloadRecord ( payload . data )
1003+ sessions . delete ( id )
1004+ activeFilePreviewToolCallIdRef . current = null
1005+
1006+ if ( fileId && resultData ?. id ) {
1007+ const fileName = ( resultData . name as string ) ?? prevSession . fileName ?? 'File'
1008+ const fileResource = { type : 'file' as const , id : fileId , title : fileName }
1009+ setResources ( ( rs ) => {
1010+ const without = rs . filter ( ( r ) => r . id !== 'streaming-file' )
1011+ if ( without . some ( ( r ) => r . type === 'file' && r . id === fileResource . id ) ) {
1012+ return without
1013+ }
1014+ return [ ...without , fileResource ]
1015+ } )
1016+ setActiveResourceId ( fileId )
1017+ invalidateResourceQueries ( queryClient , workspaceId , 'file' , fileId )
1018+ }
1019+
1020+ if ( ! activeSubagent || activeSubagent !== FileTool . id ) {
1021+ setStreamingFile ( null )
1022+ streamingFileRef . current = null
1023+ }
1024+ break
1025+ }
9741026 }
9751027
9761028 if ( phase === MothershipStreamV1ToolPhase . args_delta ) {
@@ -1327,7 +1379,7 @@ export function useChat(
13271379 if ( ! isSameActiveSubagent ) {
13281380 blocks . push ( { type : 'subagent' , content : name } )
13291381 }
1330- if ( name === FileTool . id ) {
1382+ if ( name === FileTool . id && ! isSameActiveSubagent ) {
13311383 const emptyFile : StreamingFilePreview = {
13321384 toolCallId : parentToolCallId || 'file-preview' ,
13331385 fileName : '' ,
@@ -1937,7 +1989,7 @@ export function useChat(
19371989 streamGenRef . current ++
19381990 streamReaderRef . current ?. cancel ( ) . catch ( ( ) => { } )
19391991 streamReaderRef . current = null
1940- abortControllerRef . current ?. abort ( )
1992+ abortControllerRef . current ?. abort ( 'user_stop:client_stopGeneration' )
19411993 abortControllerRef . current = null
19421994 sendingRef . current = false
19431995 setIsSending ( false )
0 commit comments