@@ -131,34 +131,25 @@ export default function ChatScreen({
131131 // Build context from attachments + persisted sources
132132 const context : string [ ] = [ ] ;
133133
134+ // Collect all source IDs: from attachments + already enabled on chat
135+ const attachmentSourceIds = ( attachments || [ ] )
136+ . filter ( ( a ) => a . type === 'document' && a . strategy === 'rag' && a . sourceId )
137+ . map ( ( a ) => a . sourceId ! ) ;
138+ const allSourceIds = [
139+ ...new Set ( [ ...enabledSources , ...attachmentSourceIds ] ) ,
140+ ] ;
141+
134142 console . log ( '[SEND] userInput:' , userInput ) ;
135143 console . log ( '[SEND] imagePath:' , imagePath ) ;
136144 console . log ( '[SEND] attachments:' , JSON . stringify ( attachments ?. map ( a => ( {
137145 id : a . id , type : a . type , strategy : a . strategy , status : a . status ,
138146 name : a . name , sourceId : a . sourceId ,
139147 inlineTextLen : a . inlineText ?. length , firstChunkLen : a . firstChunk ?. length ,
140148 } ) ) ) ) ;
141- console . log ( '[SEND] enabledSources (before) :' , enabledSources ) ;
149+ console . log ( '[SEND] allSourceIds :' , allSourceIds ) ;
142150
143- // Enable new attachments for this chat (persists for future messages)
151+ // Inline documents: inject full text
144152 if ( attachments ) {
145- for ( const att of attachments ) {
146- if (
147- att . type === 'document' &&
148- att . strategy === 'rag' &&
149- att . sourceId &&
150- ! enabledSources . includes ( att . sourceId )
151- ) {
152- console . log ( '[SEND] enabling source:' , att . sourceId , att . name ) ;
153- await enableSource ( chatId ! , att . sourceId ) ;
154- await sendEventMessage (
155- chatId ! ,
156- `${ att . name || 'Document' } has been added as a source file`
157- ) ;
158- }
159- }
160-
161- // Inline documents: inject full text
162153 for ( const att of attachments ) {
163154 if (
164155 att . type === 'document' &&
@@ -173,25 +164,24 @@ export default function ChatScreen({
173164 }
174165 }
175166
176- // RAG context from all enabled sources (persisted + newly attached)
177- // Read fresh from store — enableSource updates Zustand state which
178- // isn't reflected in the stale `chat` closure captured at render time
179- const freshChat = useChatStore . getState ( ) . getChatById ( chatId ! ) ;
180- const freshPhantom = useChatStore . getState ( ) . phantomChat ;
181- const currentEnabledSources =
182- freshChat ?. enabledSources || freshPhantom ?. enabledSources || [ ] ;
183- console . log ( '[SEND] currentEnabledSources (after):' , currentEnabledSources ) ;
184-
185- if ( currentEnabledSources . length > 0 ) {
167+ // RAG context from all sources (persisted + newly attached)
168+ if ( allSourceIds . length > 0 ) {
186169 const ragContext = await prepareContext (
187170 userInput || 'document summary' ,
188- currentEnabledSources ,
171+ allSourceIds ,
189172 vectorStore !
190173 ) ;
191174 console . log ( '[SEND] ragContext chunks:' , ragContext . length , 'chars:' , ragContext . join ( '' ) . length ) ;
192175 context . push ( ...ragContext ) ;
193176 }
194177
178+ // Enable new sources for this chat (persists for future messages)
179+ for ( const sourceId of attachmentSourceIds ) {
180+ if ( ! enabledSources . includes ( sourceId ) ) {
181+ await enableSource ( chatId ! , sourceId ) ;
182+ }
183+ }
184+
195185 console . log ( '[SEND] total context entries:' , context . length ) ;
196186 console . log ( '[SEND] total context chars:' , context . join ( '' ) . length ) ;
197187
0 commit comments